nir/print: only print image.format for image variables
[mesa.git] / src / compiler / nir / nir_print.c
1 /*
2 * Copyright © 2014 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 *
23 * Authors:
24 * Connor Abbott (cwabbott0@gmail.com)
25 *
26 */
27
28 #include "nir.h"
29 #include "compiler/shader_enums.h"
30 #include "util/half_float.h"
31 #include "vulkan/vulkan_core.h"
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <inttypes.h> /* for PRIx64 macro */
35
36 static void
37 print_tabs(unsigned num_tabs, FILE *fp)
38 {
39 for (unsigned i = 0; i < num_tabs; i++)
40 fprintf(fp, "\t");
41 }
42
43 typedef struct {
44 FILE *fp;
45 nir_shader *shader;
46 /** map from nir_variable -> printable name */
47 struct hash_table *ht;
48
49 /** set of names used so far for nir_variables */
50 struct set *syms;
51
52 /* an index used to make new non-conflicting names */
53 unsigned index;
54
55 /**
56 * Optional table of annotations mapping nir object
57 * (such as instr or var) to message to print.
58 */
59 struct hash_table *annotations;
60 } print_state;
61
62 static void
63 print_annotation(print_state *state, void *obj)
64 {
65 FILE *fp = state->fp;
66
67 if (!state->annotations)
68 return;
69
70 struct hash_entry *entry = _mesa_hash_table_search(state->annotations, obj);
71 if (!entry)
72 return;
73
74 const char *note = entry->data;
75 _mesa_hash_table_remove(state->annotations, entry);
76
77 fprintf(fp, "%s\n\n", note);
78 }
79
80 static void
81 print_register(nir_register *reg, print_state *state)
82 {
83 FILE *fp = state->fp;
84 if (reg->name != NULL)
85 fprintf(fp, "/* %s */ ", reg->name);
86 fprintf(fp, "r%u", reg->index);
87 }
88
89 static const char *sizes[] = { "error", "vec1", "vec2", "vec3", "vec4",
90 "error", "error", "error", "vec8",
91 "error", "error", "error", "error",
92 "error", "error", "error", "vec16"};
93
94 static void
95 print_register_decl(nir_register *reg, print_state *state)
96 {
97 FILE *fp = state->fp;
98 fprintf(fp, "decl_reg %s %u ", sizes[reg->num_components], reg->bit_size);
99 print_register(reg, state);
100 if (reg->num_array_elems != 0)
101 fprintf(fp, "[%u]", reg->num_array_elems);
102 fprintf(fp, "\n");
103 }
104
105 static void
106 print_ssa_def(nir_ssa_def *def, print_state *state)
107 {
108 FILE *fp = state->fp;
109 if (def->name != NULL)
110 fprintf(fp, "/* %s */ ", def->name);
111 fprintf(fp, "%s %u ssa_%u", sizes[def->num_components], def->bit_size,
112 def->index);
113 }
114
115 static void
116 print_ssa_use(nir_ssa_def *def, print_state *state)
117 {
118 FILE *fp = state->fp;
119 if (def->name != NULL)
120 fprintf(fp, "/* %s */ ", def->name);
121 fprintf(fp, "ssa_%u", def->index);
122 }
123
124 static void print_src(const nir_src *src, print_state *state);
125
126 static void
127 print_reg_src(const nir_reg_src *src, print_state *state)
128 {
129 FILE *fp = state->fp;
130 print_register(src->reg, state);
131 if (src->reg->num_array_elems != 0) {
132 fprintf(fp, "[%u", src->base_offset);
133 if (src->indirect != NULL) {
134 fprintf(fp, " + ");
135 print_src(src->indirect, state);
136 }
137 fprintf(fp, "]");
138 }
139 }
140
141 static void
142 print_reg_dest(nir_reg_dest *dest, print_state *state)
143 {
144 FILE *fp = state->fp;
145 print_register(dest->reg, state);
146 if (dest->reg->num_array_elems != 0) {
147 fprintf(fp, "[%u", dest->base_offset);
148 if (dest->indirect != NULL) {
149 fprintf(fp, " + ");
150 print_src(dest->indirect, state);
151 }
152 fprintf(fp, "]");
153 }
154 }
155
156 static void
157 print_src(const nir_src *src, print_state *state)
158 {
159 if (src->is_ssa)
160 print_ssa_use(src->ssa, state);
161 else
162 print_reg_src(&src->reg, state);
163 }
164
165 static void
166 print_dest(nir_dest *dest, print_state *state)
167 {
168 if (dest->is_ssa)
169 print_ssa_def(&dest->ssa, state);
170 else
171 print_reg_dest(&dest->reg, state);
172 }
173
174 static void
175 print_alu_src(nir_alu_instr *instr, unsigned src, print_state *state)
176 {
177 FILE *fp = state->fp;
178
179 if (instr->src[src].negate)
180 fprintf(fp, "-");
181 if (instr->src[src].abs)
182 fprintf(fp, "abs(");
183
184 print_src(&instr->src[src].src, state);
185
186 bool print_swizzle = false;
187 nir_component_mask_t used_channels = 0;
188
189 for (unsigned i = 0; i < NIR_MAX_VEC_COMPONENTS; i++) {
190 if (!nir_alu_instr_channel_used(instr, src, i))
191 continue;
192
193 used_channels++;
194
195 if (instr->src[src].swizzle[i] != i) {
196 print_swizzle = true;
197 break;
198 }
199 }
200
201 unsigned live_channels = nir_src_num_components(instr->src[src].src);
202
203 if (print_swizzle || used_channels != live_channels) {
204 fprintf(fp, ".");
205 for (unsigned i = 0; i < NIR_MAX_VEC_COMPONENTS; i++) {
206 if (!nir_alu_instr_channel_used(instr, src, i))
207 continue;
208
209 fprintf(fp, "%c", "xyzw"[instr->src[src].swizzle[i]]);
210 }
211 }
212
213 if (instr->src[src].abs)
214 fprintf(fp, ")");
215 }
216
217 static void
218 print_alu_dest(nir_alu_dest *dest, print_state *state)
219 {
220 FILE *fp = state->fp;
221 /* we're going to print the saturate modifier later, after the opcode */
222
223 print_dest(&dest->dest, state);
224
225 if (!dest->dest.is_ssa &&
226 dest->write_mask != (1 << dest->dest.reg.reg->num_components) - 1) {
227 fprintf(fp, ".");
228 for (unsigned i = 0; i < NIR_MAX_VEC_COMPONENTS; i++)
229 if ((dest->write_mask >> i) & 1)
230 fprintf(fp, "%c", "xyzw"[i]);
231 }
232 }
233
234 static void
235 print_alu_instr(nir_alu_instr *instr, print_state *state)
236 {
237 FILE *fp = state->fp;
238
239 print_alu_dest(&instr->dest, state);
240
241 fprintf(fp, " = %s", nir_op_infos[instr->op].name);
242 if (instr->exact)
243 fprintf(fp, "!");
244 if (instr->dest.saturate)
245 fprintf(fp, ".sat");
246 if (instr->no_signed_wrap)
247 fprintf(fp, ".nsw");
248 if (instr->no_unsigned_wrap)
249 fprintf(fp, ".nuw");
250 fprintf(fp, " ");
251
252 for (unsigned i = 0; i < nir_op_infos[instr->op].num_inputs; i++) {
253 if (i != 0)
254 fprintf(fp, ", ");
255
256 print_alu_src(instr, i, state);
257 }
258 }
259
260 static const char *
261 get_var_name(nir_variable *var, print_state *state)
262 {
263 if (state->ht == NULL)
264 return var->name ? var->name : "unnamed";
265
266 assert(state->syms);
267
268 struct hash_entry *entry = _mesa_hash_table_search(state->ht, var);
269 if (entry)
270 return entry->data;
271
272 char *name;
273 if (var->name == NULL) {
274 name = ralloc_asprintf(state->syms, "@%u", state->index++);
275 } else {
276 struct set_entry *set_entry = _mesa_set_search(state->syms, var->name);
277 if (set_entry != NULL) {
278 /* we have a collision with another name, append an @ + a unique
279 * index */
280 name = ralloc_asprintf(state->syms, "%s@%u", var->name,
281 state->index++);
282 } else {
283 /* Mark this one as seen */
284 _mesa_set_add(state->syms, var->name);
285 name = var->name;
286 }
287 }
288
289 _mesa_hash_table_insert(state->ht, var, name);
290
291 return name;
292 }
293
294 static void
295 print_constant(nir_constant *c, const struct glsl_type *type, print_state *state)
296 {
297 FILE *fp = state->fp;
298 const unsigned rows = glsl_get_vector_elements(type);
299 const unsigned cols = glsl_get_matrix_columns(type);
300 unsigned i;
301
302 switch (glsl_get_base_type(type)) {
303 case GLSL_TYPE_BOOL:
304 /* Only float base types can be matrices. */
305 assert(cols == 1);
306
307 for (i = 0; i < rows; i++) {
308 if (i > 0) fprintf(fp, ", ");
309 fprintf(fp, "%s", c->values[i].b ? "true" : "false");
310 }
311 break;
312
313 case GLSL_TYPE_UINT8:
314 case GLSL_TYPE_INT8:
315 /* Only float base types can be matrices. */
316 assert(cols == 1);
317
318 for (i = 0; i < rows; i++) {
319 if (i > 0) fprintf(fp, ", ");
320 fprintf(fp, "0x%02x", c->values[i].u8);
321 }
322 break;
323
324 case GLSL_TYPE_UINT16:
325 case GLSL_TYPE_INT16:
326 /* Only float base types can be matrices. */
327 assert(cols == 1);
328
329 for (i = 0; i < rows; i++) {
330 if (i > 0) fprintf(fp, ", ");
331 fprintf(fp, "0x%04x", c->values[i].u16);
332 }
333 break;
334
335 case GLSL_TYPE_UINT:
336 case GLSL_TYPE_INT:
337 /* Only float base types can be matrices. */
338 assert(cols == 1);
339
340 for (i = 0; i < rows; i++) {
341 if (i > 0) fprintf(fp, ", ");
342 fprintf(fp, "0x%08x", c->values[i].u32);
343 }
344 break;
345
346 case GLSL_TYPE_FLOAT16:
347 case GLSL_TYPE_FLOAT:
348 case GLSL_TYPE_DOUBLE:
349 if (cols > 1) {
350 for (i = 0; i < cols; i++) {
351 if (i > 0) fprintf(fp, ", ");
352 print_constant(c->elements[i], glsl_get_column_type(type), state);
353 }
354 } else {
355 switch (glsl_get_base_type(type)) {
356 case GLSL_TYPE_FLOAT16:
357 for (i = 0; i < rows; i++) {
358 if (i > 0) fprintf(fp, ", ");
359 fprintf(fp, "%f", _mesa_half_to_float(c->values[i].u16));
360 }
361 break;
362
363 case GLSL_TYPE_FLOAT:
364 for (i = 0; i < rows; i++) {
365 if (i > 0) fprintf(fp, ", ");
366 fprintf(fp, "%f", c->values[i].f32);
367 }
368 break;
369
370 case GLSL_TYPE_DOUBLE:
371 for (i = 0; i < rows; i++) {
372 if (i > 0) fprintf(fp, ", ");
373 fprintf(fp, "%f", c->values[i].f64);
374 }
375 break;
376
377 default:
378 unreachable("Cannot get here from the first level switch");
379 }
380 }
381 break;
382
383 case GLSL_TYPE_UINT64:
384 case GLSL_TYPE_INT64:
385 /* Only float base types can be matrices. */
386 assert(cols == 1);
387
388 for (i = 0; i < cols; i++) {
389 if (i > 0) fprintf(fp, ", ");
390 fprintf(fp, "0x%08" PRIx64, c->values[i].u64);
391 }
392 break;
393
394 case GLSL_TYPE_STRUCT:
395 for (i = 0; i < c->num_elements; i++) {
396 if (i > 0) fprintf(fp, ", ");
397 fprintf(fp, "{ ");
398 print_constant(c->elements[i], glsl_get_struct_field(type, i), state);
399 fprintf(fp, " }");
400 }
401 break;
402
403 case GLSL_TYPE_ARRAY:
404 for (i = 0; i < c->num_elements; i++) {
405 if (i > 0) fprintf(fp, ", ");
406 fprintf(fp, "{ ");
407 print_constant(c->elements[i], glsl_get_array_element(type), state);
408 fprintf(fp, " }");
409 }
410 break;
411
412 default:
413 unreachable("not reached");
414 }
415 }
416
417 static const char *
418 get_variable_mode_str(nir_variable_mode mode, bool want_local_global_mode)
419 {
420 switch (mode) {
421 case nir_var_shader_in:
422 return "shader_in";
423 case nir_var_shader_out:
424 return "shader_out";
425 case nir_var_uniform:
426 return "uniform";
427 case nir_var_mem_ubo:
428 return "ubo";
429 case nir_var_system_value:
430 return "system";
431 case nir_var_mem_ssbo:
432 return "ssbo";
433 case nir_var_mem_shared:
434 return "shared";
435 case nir_var_mem_global:
436 return "global";
437 case nir_var_shader_temp:
438 return want_local_global_mode ? "shader_temp" : "";
439 case nir_var_function_temp:
440 return want_local_global_mode ? "function_temp" : "";
441 default:
442 return "";
443 }
444 }
445
446 static void
447 print_var_decl(nir_variable *var, print_state *state)
448 {
449 FILE *fp = state->fp;
450
451 fprintf(fp, "decl_var ");
452
453 const char *const cent = (var->data.centroid) ? "centroid " : "";
454 const char *const samp = (var->data.sample) ? "sample " : "";
455 const char *const patch = (var->data.patch) ? "patch " : "";
456 const char *const inv = (var->data.invariant) ? "invariant " : "";
457 fprintf(fp, "%s%s%s%s%s %s ",
458 cent, samp, patch, inv, get_variable_mode_str(var->data.mode, false),
459 glsl_interp_mode_name(var->data.interpolation));
460
461 enum gl_access_qualifier access = var->data.access;
462 const char *const coher = (access & ACCESS_COHERENT) ? "coherent " : "";
463 const char *const volat = (access & ACCESS_VOLATILE) ? "volatile " : "";
464 const char *const restr = (access & ACCESS_RESTRICT) ? "restrict " : "";
465 const char *const ronly = (access & ACCESS_NON_WRITEABLE) ? "readonly " : "";
466 const char *const wonly = (access & ACCESS_NON_READABLE) ? "writeonly " : "";
467 const char *const reorder = (access & ACCESS_CAN_REORDER) ? "reorderable " : "";
468 fprintf(fp, "%s%s%s%s%s%s", coher, volat, restr, ronly, wonly, reorder);
469
470 if (glsl_get_base_type(glsl_without_array(var->type)) == GLSL_TYPE_IMAGE) {
471 #define FORMAT_CASE(x) case x: fprintf(fp, #x " "); break
472 switch (var->data.image.format) {
473 FORMAT_CASE(GL_RGBA32F);
474 FORMAT_CASE(GL_RGBA32UI);
475 FORMAT_CASE(GL_RGBA32I);
476 FORMAT_CASE(GL_R32F);
477 FORMAT_CASE(GL_R32UI);
478 FORMAT_CASE(GL_R32I);
479 FORMAT_CASE(GL_RG32F);
480 FORMAT_CASE(GL_RG32UI);
481 FORMAT_CASE(GL_RG32I);
482 FORMAT_CASE(GL_R8);
483 FORMAT_CASE(GL_RG8);
484 FORMAT_CASE(GL_RGBA8);
485 FORMAT_CASE(GL_R8_SNORM);
486 FORMAT_CASE(GL_RG8_SNORM);
487 FORMAT_CASE(GL_RGBA8_SNORM);
488 FORMAT_CASE(GL_R16);
489 FORMAT_CASE(GL_RG16);
490 FORMAT_CASE(GL_RGBA16);
491 FORMAT_CASE(GL_R16_SNORM);
492 FORMAT_CASE(GL_RG16_SNORM);
493 FORMAT_CASE(GL_RGBA16_SNORM);
494 FORMAT_CASE(GL_R16F);
495 FORMAT_CASE(GL_RG16F);
496 FORMAT_CASE(GL_RGBA16F);
497 FORMAT_CASE(GL_R8UI);
498 FORMAT_CASE(GL_R8I);
499 FORMAT_CASE(GL_RG8UI);
500 FORMAT_CASE(GL_RG8I);
501 FORMAT_CASE(GL_RGBA8UI);
502 FORMAT_CASE(GL_RGBA8I);
503 FORMAT_CASE(GL_R16UI);
504 FORMAT_CASE(GL_R16I);
505 FORMAT_CASE(GL_RG16UI);
506 FORMAT_CASE(GL_RG16I);
507 FORMAT_CASE(GL_RGBA16UI);
508 FORMAT_CASE(GL_RGBA16I);
509 FORMAT_CASE(GL_R11F_G11F_B10F);
510 FORMAT_CASE(GL_RGB9_E5);
511 FORMAT_CASE(GL_RGB10_A2);
512 FORMAT_CASE(GL_RGB10_A2UI);
513 default: /* Including the normal GL_NONE */
514 break;
515 }
516 #undef FORMAT_CASE
517 }
518
519 fprintf(fp, "%s %s", glsl_get_type_name(var->type),
520 get_var_name(var, state));
521
522 if (var->data.mode == nir_var_shader_in ||
523 var->data.mode == nir_var_shader_out ||
524 var->data.mode == nir_var_uniform ||
525 var->data.mode == nir_var_mem_ubo ||
526 var->data.mode == nir_var_mem_ssbo) {
527 const char *loc = NULL;
528 char buf[4];
529
530 switch (state->shader->info.stage) {
531 case MESA_SHADER_VERTEX:
532 if (var->data.mode == nir_var_shader_in)
533 loc = gl_vert_attrib_name(var->data.location);
534 else if (var->data.mode == nir_var_shader_out)
535 loc = gl_varying_slot_name(var->data.location);
536 break;
537 case MESA_SHADER_GEOMETRY:
538 if ((var->data.mode == nir_var_shader_in) ||
539 (var->data.mode == nir_var_shader_out))
540 loc = gl_varying_slot_name(var->data.location);
541 break;
542 case MESA_SHADER_FRAGMENT:
543 if (var->data.mode == nir_var_shader_in)
544 loc = gl_varying_slot_name(var->data.location);
545 else if (var->data.mode == nir_var_shader_out)
546 loc = gl_frag_result_name(var->data.location);
547 break;
548 case MESA_SHADER_TESS_CTRL:
549 case MESA_SHADER_TESS_EVAL:
550 case MESA_SHADER_COMPUTE:
551 case MESA_SHADER_KERNEL:
552 default:
553 /* TODO */
554 break;
555 }
556
557 if (!loc) {
558 snprintf(buf, sizeof(buf), "%u", var->data.location);
559 loc = buf;
560 }
561
562 /* For shader I/O vars that have been split to components or packed,
563 * print the fractional location within the input/output.
564 */
565 unsigned int num_components =
566 glsl_get_components(glsl_without_array(var->type));
567 const char *components = NULL;
568 char components_local[6] = {'.' /* the rest is 0-filled */};
569 switch (var->data.mode) {
570 case nir_var_shader_in:
571 case nir_var_shader_out:
572 if (num_components < 4 && num_components != 0) {
573 const char *xyzw = "xyzw";
574 for (int i = 0; i < num_components; i++)
575 components_local[i + 1] = xyzw[i + var->data.location_frac];
576
577 components = components_local;
578 }
579 break;
580 default:
581 break;
582 }
583
584 fprintf(fp, " (%s%s, %u, %u)%s", loc,
585 components ? components : "",
586 var->data.driver_location, var->data.binding,
587 var->data.compact ? " compact" : "");
588 }
589
590 if (var->constant_initializer) {
591 fprintf(fp, " = { ");
592 print_constant(var->constant_initializer, var->type, state);
593 fprintf(fp, " }");
594 }
595
596 fprintf(fp, "\n");
597 print_annotation(state, var);
598 }
599
600 static void
601 print_deref_link(const nir_deref_instr *instr, bool whole_chain, print_state *state)
602 {
603 FILE *fp = state->fp;
604
605 if (instr->deref_type == nir_deref_type_var) {
606 fprintf(fp, "%s", get_var_name(instr->var, state));
607 return;
608 } else if (instr->deref_type == nir_deref_type_cast) {
609 fprintf(fp, "(%s *)", glsl_get_type_name(instr->type));
610 print_src(&instr->parent, state);
611 return;
612 }
613
614 assert(instr->parent.is_ssa);
615 nir_deref_instr *parent =
616 nir_instr_as_deref(instr->parent.ssa->parent_instr);
617
618 /* Is the parent we're going to print a bare cast? */
619 const bool is_parent_cast =
620 whole_chain && parent->deref_type == nir_deref_type_cast;
621
622 /* If we're not printing the whole chain, the parent we print will be a SSA
623 * value that represents a pointer. The only deref type that naturally
624 * gives a pointer is a cast.
625 */
626 const bool is_parent_pointer =
627 !whole_chain || parent->deref_type == nir_deref_type_cast;
628
629 /* Struct derefs have a nice syntax that works on pointers, arrays derefs
630 * do not.
631 */
632 const bool need_deref =
633 is_parent_pointer && instr->deref_type != nir_deref_type_struct;
634
635 /* Cast need extra parens and so * dereferences */
636 if (is_parent_cast || need_deref)
637 fprintf(fp, "(");
638
639 if (need_deref)
640 fprintf(fp, "*");
641
642 if (whole_chain) {
643 print_deref_link(parent, whole_chain, state);
644 } else {
645 print_src(&instr->parent, state);
646 }
647
648 if (is_parent_cast || need_deref)
649 fprintf(fp, ")");
650
651 switch (instr->deref_type) {
652 case nir_deref_type_struct:
653 fprintf(fp, "%s%s", is_parent_pointer ? "->" : ".",
654 glsl_get_struct_elem_name(parent->type, instr->strct.index));
655 break;
656
657 case nir_deref_type_array:
658 case nir_deref_type_ptr_as_array: {
659 if (nir_src_is_const(instr->arr.index)) {
660 fprintf(fp, "[%"PRId64"]", nir_src_as_int(instr->arr.index));
661 } else {
662 fprintf(fp, "[");
663 print_src(&instr->arr.index, state);
664 fprintf(fp, "]");
665 }
666 break;
667 }
668
669 case nir_deref_type_array_wildcard:
670 fprintf(fp, "[*]");
671 break;
672
673 default:
674 unreachable("Invalid deref instruction type");
675 }
676 }
677
678 static void
679 print_deref_instr(nir_deref_instr *instr, print_state *state)
680 {
681 FILE *fp = state->fp;
682
683 print_dest(&instr->dest, state);
684
685 switch (instr->deref_type) {
686 case nir_deref_type_var:
687 fprintf(fp, " = deref_var ");
688 break;
689 case nir_deref_type_array:
690 case nir_deref_type_array_wildcard:
691 fprintf(fp, " = deref_array ");
692 break;
693 case nir_deref_type_struct:
694 fprintf(fp, " = deref_struct ");
695 break;
696 case nir_deref_type_cast:
697 fprintf(fp, " = deref_cast ");
698 break;
699 case nir_deref_type_ptr_as_array:
700 fprintf(fp, " = deref_ptr_as_array ");
701 break;
702 default:
703 unreachable("Invalid deref instruction type");
704 }
705
706 /* Only casts naturally return a pointer type */
707 if (instr->deref_type != nir_deref_type_cast)
708 fprintf(fp, "&");
709
710 print_deref_link(instr, false, state);
711
712 fprintf(fp, " (%s %s) ",
713 get_variable_mode_str(instr->mode, true),
714 glsl_get_type_name(instr->type));
715
716 if (instr->deref_type != nir_deref_type_var &&
717 instr->deref_type != nir_deref_type_cast) {
718 /* Print the entire chain as a comment */
719 fprintf(fp, "/* &");
720 print_deref_link(instr, true, state);
721 fprintf(fp, " */");
722 }
723
724 if (instr->deref_type == nir_deref_type_cast) {
725 fprintf(fp, " /* ptr_stride=%u */", instr->cast.ptr_stride);
726 }
727 }
728
729 static const char *
730 vulkan_descriptor_type_name(VkDescriptorType type)
731 {
732 switch (type) {
733 case VK_DESCRIPTOR_TYPE_SAMPLER: return "sampler";
734 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER: return "texture+sampler";
735 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE: return "texture";
736 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE: return "image";
737 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER: return "texture-buffer";
738 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER: return "image-buffer";
739 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER: return "UBO";
740 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER: return "SSBO";
741 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC: return "UBO";
742 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC: return "SSBO";
743 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT: return "input-att";
744 case VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT: return "inline-UBO";
745 default: return "unknown";
746 }
747 }
748
749 static void
750 print_intrinsic_instr(nir_intrinsic_instr *instr, print_state *state)
751 {
752 const nir_intrinsic_info *info = &nir_intrinsic_infos[instr->intrinsic];
753 unsigned num_srcs = info->num_srcs;
754 FILE *fp = state->fp;
755
756 if (info->has_dest) {
757 print_dest(&instr->dest, state);
758 fprintf(fp, " = ");
759 }
760
761 fprintf(fp, "intrinsic %s (", info->name);
762
763 for (unsigned i = 0; i < num_srcs; i++) {
764 if (i != 0)
765 fprintf(fp, ", ");
766
767 print_src(&instr->src[i], state);
768 }
769
770 fprintf(fp, ") (");
771
772 for (unsigned i = 0; i < info->num_indices; i++) {
773 if (i != 0)
774 fprintf(fp, ", ");
775
776 fprintf(fp, "%d", instr->const_index[i]);
777 }
778
779 fprintf(fp, ")");
780
781 static const char *index_name[NIR_INTRINSIC_NUM_INDEX_FLAGS] = {
782 [NIR_INTRINSIC_BASE] = "base",
783 [NIR_INTRINSIC_WRMASK] = "wrmask",
784 [NIR_INTRINSIC_STREAM_ID] = "stream-id",
785 [NIR_INTRINSIC_UCP_ID] = "ucp-id",
786 [NIR_INTRINSIC_RANGE] = "range",
787 [NIR_INTRINSIC_DESC_SET] = "desc-set",
788 [NIR_INTRINSIC_BINDING] = "binding",
789 [NIR_INTRINSIC_COMPONENT] = "component",
790 [NIR_INTRINSIC_INTERP_MODE] = "interp_mode",
791 [NIR_INTRINSIC_REDUCTION_OP] = "reduction_op",
792 [NIR_INTRINSIC_CLUSTER_SIZE] = "cluster_size",
793 [NIR_INTRINSIC_PARAM_IDX] = "param_idx",
794 [NIR_INTRINSIC_IMAGE_DIM] = "image_dim",
795 [NIR_INTRINSIC_IMAGE_ARRAY] = "image_array",
796 [NIR_INTRINSIC_ACCESS] = "access",
797 [NIR_INTRINSIC_SRC_ACCESS] = "src-access",
798 [NIR_INTRINSIC_DST_ACCESS] = "dst-access",
799 [NIR_INTRINSIC_FORMAT] = "format",
800 [NIR_INTRINSIC_ALIGN_MUL] = "align_mul",
801 [NIR_INTRINSIC_ALIGN_OFFSET] = "align_offset",
802 [NIR_INTRINSIC_DESC_TYPE] = "desc_type",
803 [NIR_INTRINSIC_TYPE] = "type",
804 [NIR_INTRINSIC_SWIZZLE_MASK] = "swizzle_mask",
805 [NIR_INTRINSIC_DRIVER_LOCATION] = "driver_location",
806 [NIR_INTRINSIC_MEMORY_SEMANTICS] = "mem_semantics",
807 [NIR_INTRINSIC_MEMORY_MODES] = "mem_modes",
808 [NIR_INTRINSIC_MEMORY_SCOPE] = "mem_scope",
809 };
810 for (unsigned idx = 1; idx < NIR_INTRINSIC_NUM_INDEX_FLAGS; idx++) {
811 if (!info->index_map[idx])
812 continue;
813 fprintf(fp, " /*");
814 switch (idx) {
815 case NIR_INTRINSIC_WRMASK: {
816 /* special case wrmask to show it as a writemask.. */
817 unsigned wrmask = nir_intrinsic_write_mask(instr);
818 fprintf(fp, " wrmask=");
819 for (unsigned i = 0; i < 4; i++)
820 if ((wrmask >> i) & 1)
821 fprintf(fp, "%c", "xyzw"[i]);
822 break;
823 }
824
825 case NIR_INTRINSIC_REDUCTION_OP: {
826 nir_op reduction_op = nir_intrinsic_reduction_op(instr);
827 fprintf(fp, " reduction_op=%s", nir_op_infos[reduction_op].name);
828 break;
829 }
830
831 case NIR_INTRINSIC_IMAGE_DIM: {
832 static const char *dim_name[] = {
833 [GLSL_SAMPLER_DIM_1D] = "1D",
834 [GLSL_SAMPLER_DIM_2D] = "2D",
835 [GLSL_SAMPLER_DIM_3D] = "3D",
836 [GLSL_SAMPLER_DIM_CUBE] = "Cube",
837 [GLSL_SAMPLER_DIM_RECT] = "Rect",
838 [GLSL_SAMPLER_DIM_BUF] = "Buf",
839 [GLSL_SAMPLER_DIM_MS] = "2D-MSAA",
840 [GLSL_SAMPLER_DIM_SUBPASS] = "Subpass",
841 [GLSL_SAMPLER_DIM_SUBPASS_MS] = "Subpass-MSAA",
842 };
843 enum glsl_sampler_dim dim = nir_intrinsic_image_dim(instr);
844 assert(dim < ARRAY_SIZE(dim_name) && dim_name[dim]);
845 fprintf(fp, " image_dim=%s", dim_name[dim]);
846 break;
847 }
848
849 case NIR_INTRINSIC_IMAGE_ARRAY: {
850 bool array = nir_intrinsic_image_array(instr);
851 fprintf(fp, " image_array=%s", array ? "true" : "false");
852 break;
853 }
854
855 case NIR_INTRINSIC_DESC_TYPE: {
856 VkDescriptorType desc_type = nir_intrinsic_desc_type(instr);
857 fprintf(fp, " desc_type=%s", vulkan_descriptor_type_name(desc_type));
858 break;
859 }
860
861 case NIR_INTRINSIC_TYPE: {
862 nir_alu_type type = nir_intrinsic_type(instr);
863 unsigned size = nir_alu_type_get_type_size(type);
864 const char *name;
865 switch (nir_alu_type_get_base_type(type)) {
866 case nir_type_int: name = "int"; break;
867 case nir_type_uint: name = "uint"; break;
868 case nir_type_bool: name = "bool"; break;
869 case nir_type_float: name = "float"; break;
870 default: name = "invalid";
871 }
872 if (size)
873 fprintf(fp, " type=%s%u", name, size);
874 else
875 fprintf(fp, " type=%s", name);
876 break;
877 }
878
879 case NIR_INTRINSIC_SWIZZLE_MASK: {
880 fprintf(fp, " swizzle_mask=");
881 unsigned mask = nir_intrinsic_swizzle_mask(instr);
882 if (instr->intrinsic == nir_intrinsic_quad_swizzle_amd) {
883 for (unsigned i = 0; i < 4; i++)
884 fprintf(fp, "%d", (mask >> (i * 2) & 3));
885 } else if (instr->intrinsic == nir_intrinsic_masked_swizzle_amd) {
886 fprintf(fp, "((id & %d) | %d) ^ %d", mask & 0x1F,
887 (mask >> 5) & 0x1F,
888 (mask >> 10) & 0x1F);
889 } else {
890 fprintf(fp, "%d", mask);
891 }
892 break;
893 }
894
895 case NIR_INTRINSIC_MEMORY_SEMANTICS: {
896 nir_memory_semantics semantics = nir_intrinsic_memory_semantics(instr);
897 fprintf(fp, " mem_semantics=");
898 switch (semantics & (NIR_MEMORY_ACQUIRE | NIR_MEMORY_RELEASE)) {
899 case 0: fprintf(fp, "NONE"); break;
900 case NIR_MEMORY_ACQUIRE: fprintf(fp, "ACQ"); break;
901 case NIR_MEMORY_RELEASE: fprintf(fp, "REL"); break;
902 default: fprintf(fp, "ACQ|REL"); break;
903 }
904 if (semantics & (NIR_MEMORY_MAKE_AVAILABLE)) fprintf(fp, "|AVAILABLE");
905 if (semantics & (NIR_MEMORY_MAKE_VISIBLE)) fprintf(fp, "|VISIBLE");
906 break;
907 }
908
909 case NIR_INTRINSIC_MEMORY_MODES: {
910 fprintf(fp, " mem_modes=");
911 unsigned int modes = nir_intrinsic_memory_modes(instr);
912 while (modes) {
913 nir_variable_mode m = u_bit_scan(&modes);
914 fprintf(fp, "%s%s", get_variable_mode_str(1 << m, true), modes ? "|" : "");
915 }
916 break;
917 }
918
919 case NIR_INTRINSIC_MEMORY_SCOPE: {
920 fprintf(fp, " mem_scope=");
921 switch (nir_intrinsic_memory_scope(instr)) {
922 case NIR_SCOPE_DEVICE: fprintf(fp, "DEVICE"); break;
923 case NIR_SCOPE_QUEUE_FAMILY: fprintf(fp, "QUEUE_FAMILY"); break;
924 case NIR_SCOPE_WORKGROUP: fprintf(fp, "WORKGROUP"); break;
925 case NIR_SCOPE_SUBGROUP: fprintf(fp, "SUBGROUP"); break;
926 case NIR_SCOPE_INVOCATION: fprintf(fp, "INVOCATION"); break;
927 }
928 break;
929 }
930
931 default: {
932 unsigned off = info->index_map[idx] - 1;
933 assert(index_name[idx]); /* forgot to update index_name table? */
934 fprintf(fp, " %s=%d", index_name[idx], instr->const_index[off]);
935 break;
936 }
937 }
938 fprintf(fp, " */");
939 }
940
941 if (!state->shader)
942 return;
943
944 struct exec_list *var_list = NULL;
945
946 switch (instr->intrinsic) {
947 case nir_intrinsic_load_uniform:
948 var_list = &state->shader->uniforms;
949 break;
950 case nir_intrinsic_load_input:
951 case nir_intrinsic_load_interpolated_input:
952 case nir_intrinsic_load_per_vertex_input:
953 var_list = &state->shader->inputs;
954 break;
955 case nir_intrinsic_load_output:
956 case nir_intrinsic_store_output:
957 case nir_intrinsic_store_per_vertex_output:
958 var_list = &state->shader->outputs;
959 break;
960 default:
961 return;
962 }
963
964 nir_foreach_variable(var, var_list) {
965 if ((var->data.driver_location == nir_intrinsic_base(instr)) &&
966 (instr->intrinsic == nir_intrinsic_load_uniform ||
967 (nir_intrinsic_component(instr) >= var->data.location_frac &&
968 nir_intrinsic_component(instr) <
969 (var->data.location_frac + glsl_get_components(var->type)))) &&
970 var->name) {
971 fprintf(fp, "\t/* %s */", var->name);
972 break;
973 }
974 }
975 }
976
977 static void
978 print_tex_instr(nir_tex_instr *instr, print_state *state)
979 {
980 FILE *fp = state->fp;
981
982 print_dest(&instr->dest, state);
983
984 fprintf(fp, " = ");
985
986 switch (instr->op) {
987 case nir_texop_tex:
988 fprintf(fp, "tex ");
989 break;
990 case nir_texop_txb:
991 fprintf(fp, "txb ");
992 break;
993 case nir_texop_txl:
994 fprintf(fp, "txl ");
995 break;
996 case nir_texop_txd:
997 fprintf(fp, "txd ");
998 break;
999 case nir_texop_txf:
1000 fprintf(fp, "txf ");
1001 break;
1002 case nir_texop_txf_ms:
1003 fprintf(fp, "txf_ms ");
1004 break;
1005 case nir_texop_txf_ms_fb:
1006 fprintf(fp, "txf_ms_fb ");
1007 break;
1008 case nir_texop_txf_ms_mcs:
1009 fprintf(fp, "txf_ms_mcs ");
1010 break;
1011 case nir_texop_txs:
1012 fprintf(fp, "txs ");
1013 break;
1014 case nir_texop_lod:
1015 fprintf(fp, "lod ");
1016 break;
1017 case nir_texop_tg4:
1018 fprintf(fp, "tg4 ");
1019 break;
1020 case nir_texop_query_levels:
1021 fprintf(fp, "query_levels ");
1022 break;
1023 case nir_texop_texture_samples:
1024 fprintf(fp, "texture_samples ");
1025 break;
1026 case nir_texop_samples_identical:
1027 fprintf(fp, "samples_identical ");
1028 break;
1029 case nir_texop_tex_prefetch:
1030 fprintf(fp, "tex (pre-dispatchable) ");
1031 break;
1032 default:
1033 unreachable("Invalid texture operation");
1034 break;
1035 }
1036
1037 bool has_texture_deref = false, has_sampler_deref = false;
1038 for (unsigned i = 0; i < instr->num_srcs; i++) {
1039 if (i > 0) {
1040 fprintf(fp, ", ");
1041 }
1042
1043 print_src(&instr->src[i].src, state);
1044 fprintf(fp, " ");
1045
1046 switch(instr->src[i].src_type) {
1047 case nir_tex_src_coord:
1048 fprintf(fp, "(coord)");
1049 break;
1050 case nir_tex_src_projector:
1051 fprintf(fp, "(projector)");
1052 break;
1053 case nir_tex_src_comparator:
1054 fprintf(fp, "(comparator)");
1055 break;
1056 case nir_tex_src_offset:
1057 fprintf(fp, "(offset)");
1058 break;
1059 case nir_tex_src_bias:
1060 fprintf(fp, "(bias)");
1061 break;
1062 case nir_tex_src_lod:
1063 fprintf(fp, "(lod)");
1064 break;
1065 case nir_tex_src_min_lod:
1066 fprintf(fp, "(min_lod)");
1067 break;
1068 case nir_tex_src_ms_index:
1069 fprintf(fp, "(ms_index)");
1070 break;
1071 case nir_tex_src_ms_mcs:
1072 fprintf(fp, "(ms_mcs)");
1073 break;
1074 case nir_tex_src_ddx:
1075 fprintf(fp, "(ddx)");
1076 break;
1077 case nir_tex_src_ddy:
1078 fprintf(fp, "(ddy)");
1079 break;
1080 case nir_tex_src_texture_deref:
1081 has_texture_deref = true;
1082 fprintf(fp, "(texture_deref)");
1083 break;
1084 case nir_tex_src_sampler_deref:
1085 has_sampler_deref = true;
1086 fprintf(fp, "(sampler_deref)");
1087 break;
1088 case nir_tex_src_texture_offset:
1089 fprintf(fp, "(texture_offset)");
1090 break;
1091 case nir_tex_src_sampler_offset:
1092 fprintf(fp, "(sampler_offset)");
1093 break;
1094 case nir_tex_src_texture_handle:
1095 fprintf(fp, "(texture_handle)");
1096 break;
1097 case nir_tex_src_sampler_handle:
1098 fprintf(fp, "(sampler_handle)");
1099 break;
1100 case nir_tex_src_plane:
1101 fprintf(fp, "(plane)");
1102 break;
1103
1104 default:
1105 unreachable("Invalid texture source type");
1106 break;
1107 }
1108 }
1109
1110 if (instr->op == nir_texop_tg4) {
1111 fprintf(fp, ", %u (gather_component)", instr->component);
1112 }
1113
1114 if (nir_tex_instr_has_explicit_tg4_offsets(instr)) {
1115 fprintf(fp, ", { (%i, %i)", instr->tg4_offsets[0][0], instr->tg4_offsets[0][1]);
1116 for (unsigned i = 1; i < 4; ++i)
1117 fprintf(fp, ", (%i, %i)", instr->tg4_offsets[i][0],
1118 instr->tg4_offsets[i][1]);
1119 fprintf(fp, " } (offsets)");
1120 }
1121
1122 if (instr->op != nir_texop_txf_ms_fb) {
1123 if (!has_texture_deref) {
1124 fprintf(fp, ", %u (texture)", instr->texture_index);
1125 }
1126
1127 if (!has_sampler_deref) {
1128 fprintf(fp, ", %u (sampler)", instr->sampler_index);
1129 }
1130 }
1131 }
1132
1133 static void
1134 print_call_instr(nir_call_instr *instr, print_state *state)
1135 {
1136 FILE *fp = state->fp;
1137
1138 fprintf(fp, "call %s ", instr->callee->name);
1139
1140 for (unsigned i = 0; i < instr->num_params; i++) {
1141 if (i != 0)
1142 fprintf(fp, ", ");
1143
1144 print_src(&instr->params[i], state);
1145 }
1146 }
1147
1148 static void
1149 print_load_const_instr(nir_load_const_instr *instr, print_state *state)
1150 {
1151 FILE *fp = state->fp;
1152
1153 print_ssa_def(&instr->def, state);
1154
1155 fprintf(fp, " = load_const (");
1156
1157 for (unsigned i = 0; i < instr->def.num_components; i++) {
1158 if (i != 0)
1159 fprintf(fp, ", ");
1160
1161 /*
1162 * we don't really know the type of the constant (if it will be used as a
1163 * float or an int), so just print the raw constant in hex for fidelity
1164 * and then print the float in a comment for readability.
1165 */
1166
1167 switch (instr->def.bit_size) {
1168 case 64:
1169 fprintf(fp, "0x%16" PRIx64 " /* %f */", instr->value[i].u64,
1170 instr->value[i].f64);
1171 break;
1172 case 32:
1173 fprintf(fp, "0x%08x /* %f */", instr->value[i].u32, instr->value[i].f32);
1174 break;
1175 case 16:
1176 fprintf(fp, "0x%04x /* %f */", instr->value[i].u16,
1177 _mesa_half_to_float(instr->value[i].u16));
1178 break;
1179 case 8:
1180 fprintf(fp, "0x%02x", instr->value[i].u8);
1181 break;
1182 case 1:
1183 fprintf(fp, "%s", instr->value[i].b ? "true" : "false");
1184 break;
1185 }
1186 }
1187
1188 fprintf(fp, ")");
1189 }
1190
1191 static void
1192 print_jump_instr(nir_jump_instr *instr, print_state *state)
1193 {
1194 FILE *fp = state->fp;
1195
1196 switch (instr->type) {
1197 case nir_jump_break:
1198 fprintf(fp, "break");
1199 break;
1200
1201 case nir_jump_continue:
1202 fprintf(fp, "continue");
1203 break;
1204
1205 case nir_jump_return:
1206 fprintf(fp, "return");
1207 break;
1208 }
1209 }
1210
1211 static void
1212 print_ssa_undef_instr(nir_ssa_undef_instr* instr, print_state *state)
1213 {
1214 FILE *fp = state->fp;
1215 print_ssa_def(&instr->def, state);
1216 fprintf(fp, " = undefined");
1217 }
1218
1219 static void
1220 print_phi_instr(nir_phi_instr *instr, print_state *state)
1221 {
1222 FILE *fp = state->fp;
1223 print_dest(&instr->dest, state);
1224 fprintf(fp, " = phi ");
1225 nir_foreach_phi_src(src, instr) {
1226 if (&src->node != exec_list_get_head(&instr->srcs))
1227 fprintf(fp, ", ");
1228
1229 fprintf(fp, "block_%u: ", src->pred->index);
1230 print_src(&src->src, state);
1231 }
1232 }
1233
1234 static void
1235 print_parallel_copy_instr(nir_parallel_copy_instr *instr, print_state *state)
1236 {
1237 FILE *fp = state->fp;
1238 nir_foreach_parallel_copy_entry(entry, instr) {
1239 if (&entry->node != exec_list_get_head(&instr->entries))
1240 fprintf(fp, "; ");
1241
1242 print_dest(&entry->dest, state);
1243 fprintf(fp, " = ");
1244 print_src(&entry->src, state);
1245 }
1246 }
1247
1248 static void
1249 print_instr(const nir_instr *instr, print_state *state, unsigned tabs)
1250 {
1251 FILE *fp = state->fp;
1252 print_tabs(tabs, fp);
1253
1254 switch (instr->type) {
1255 case nir_instr_type_alu:
1256 print_alu_instr(nir_instr_as_alu(instr), state);
1257 break;
1258
1259 case nir_instr_type_deref:
1260 print_deref_instr(nir_instr_as_deref(instr), state);
1261 break;
1262
1263 case nir_instr_type_call:
1264 print_call_instr(nir_instr_as_call(instr), state);
1265 break;
1266
1267 case nir_instr_type_intrinsic:
1268 print_intrinsic_instr(nir_instr_as_intrinsic(instr), state);
1269 break;
1270
1271 case nir_instr_type_tex:
1272 print_tex_instr(nir_instr_as_tex(instr), state);
1273 break;
1274
1275 case nir_instr_type_load_const:
1276 print_load_const_instr(nir_instr_as_load_const(instr), state);
1277 break;
1278
1279 case nir_instr_type_jump:
1280 print_jump_instr(nir_instr_as_jump(instr), state);
1281 break;
1282
1283 case nir_instr_type_ssa_undef:
1284 print_ssa_undef_instr(nir_instr_as_ssa_undef(instr), state);
1285 break;
1286
1287 case nir_instr_type_phi:
1288 print_phi_instr(nir_instr_as_phi(instr), state);
1289 break;
1290
1291 case nir_instr_type_parallel_copy:
1292 print_parallel_copy_instr(nir_instr_as_parallel_copy(instr), state);
1293 break;
1294
1295 default:
1296 unreachable("Invalid instruction type");
1297 break;
1298 }
1299 }
1300
1301 static int
1302 compare_block_index(const void *p1, const void *p2)
1303 {
1304 const nir_block *block1 = *((const nir_block **) p1);
1305 const nir_block *block2 = *((const nir_block **) p2);
1306
1307 return (int) block1->index - (int) block2->index;
1308 }
1309
1310 static void print_cf_node(nir_cf_node *node, print_state *state,
1311 unsigned tabs);
1312
1313 static void
1314 print_block(nir_block *block, print_state *state, unsigned tabs)
1315 {
1316 FILE *fp = state->fp;
1317
1318 print_tabs(tabs, fp);
1319 fprintf(fp, "block block_%u:\n", block->index);
1320
1321 /* sort the predecessors by index so we consistently print the same thing */
1322
1323 nir_block **preds =
1324 malloc(block->predecessors->entries * sizeof(nir_block *));
1325
1326 unsigned i = 0;
1327 set_foreach(block->predecessors, entry) {
1328 preds[i++] = (nir_block *) entry->key;
1329 }
1330
1331 qsort(preds, block->predecessors->entries, sizeof(nir_block *),
1332 compare_block_index);
1333
1334 print_tabs(tabs, fp);
1335 fprintf(fp, "/* preds: ");
1336 for (unsigned i = 0; i < block->predecessors->entries; i++) {
1337 fprintf(fp, "block_%u ", preds[i]->index);
1338 }
1339 fprintf(fp, "*/\n");
1340
1341 free(preds);
1342
1343 nir_foreach_instr(instr, block) {
1344 print_instr(instr, state, tabs);
1345 fprintf(fp, "\n");
1346 print_annotation(state, instr);
1347 }
1348
1349 print_tabs(tabs, fp);
1350 fprintf(fp, "/* succs: ");
1351 for (unsigned i = 0; i < 2; i++)
1352 if (block->successors[i]) {
1353 fprintf(fp, "block_%u ", block->successors[i]->index);
1354 }
1355 fprintf(fp, "*/\n");
1356 }
1357
1358 static void
1359 print_if(nir_if *if_stmt, print_state *state, unsigned tabs)
1360 {
1361 FILE *fp = state->fp;
1362
1363 print_tabs(tabs, fp);
1364 fprintf(fp, "if ");
1365 print_src(&if_stmt->condition, state);
1366 fprintf(fp, " {\n");
1367 foreach_list_typed(nir_cf_node, node, node, &if_stmt->then_list) {
1368 print_cf_node(node, state, tabs + 1);
1369 }
1370 print_tabs(tabs, fp);
1371 fprintf(fp, "} else {\n");
1372 foreach_list_typed(nir_cf_node, node, node, &if_stmt->else_list) {
1373 print_cf_node(node, state, tabs + 1);
1374 }
1375 print_tabs(tabs, fp);
1376 fprintf(fp, "}\n");
1377 }
1378
1379 static void
1380 print_loop(nir_loop *loop, print_state *state, unsigned tabs)
1381 {
1382 FILE *fp = state->fp;
1383
1384 print_tabs(tabs, fp);
1385 fprintf(fp, "loop {\n");
1386 foreach_list_typed(nir_cf_node, node, node, &loop->body) {
1387 print_cf_node(node, state, tabs + 1);
1388 }
1389 print_tabs(tabs, fp);
1390 fprintf(fp, "}\n");
1391 }
1392
1393 static void
1394 print_cf_node(nir_cf_node *node, print_state *state, unsigned int tabs)
1395 {
1396 switch (node->type) {
1397 case nir_cf_node_block:
1398 print_block(nir_cf_node_as_block(node), state, tabs);
1399 break;
1400
1401 case nir_cf_node_if:
1402 print_if(nir_cf_node_as_if(node), state, tabs);
1403 break;
1404
1405 case nir_cf_node_loop:
1406 print_loop(nir_cf_node_as_loop(node), state, tabs);
1407 break;
1408
1409 default:
1410 unreachable("Invalid CFG node type");
1411 }
1412 }
1413
1414 static void
1415 print_function_impl(nir_function_impl *impl, print_state *state)
1416 {
1417 FILE *fp = state->fp;
1418
1419 fprintf(fp, "\nimpl %s ", impl->function->name);
1420
1421 fprintf(fp, "{\n");
1422
1423 nir_foreach_variable(var, &impl->locals) {
1424 fprintf(fp, "\t");
1425 print_var_decl(var, state);
1426 }
1427
1428 foreach_list_typed(nir_register, reg, node, &impl->registers) {
1429 fprintf(fp, "\t");
1430 print_register_decl(reg, state);
1431 }
1432
1433 nir_index_blocks(impl);
1434
1435 foreach_list_typed(nir_cf_node, node, node, &impl->body) {
1436 print_cf_node(node, state, 1);
1437 }
1438
1439 fprintf(fp, "\tblock block_%u:\n}\n\n", impl->end_block->index);
1440 }
1441
1442 static void
1443 print_function(nir_function *function, print_state *state)
1444 {
1445 FILE *fp = state->fp;
1446
1447 fprintf(fp, "decl_function %s (%d params)", function->name,
1448 function->num_params);
1449
1450 fprintf(fp, "\n");
1451
1452 if (function->impl != NULL) {
1453 print_function_impl(function->impl, state);
1454 return;
1455 }
1456 }
1457
1458 static void
1459 init_print_state(print_state *state, nir_shader *shader, FILE *fp)
1460 {
1461 state->fp = fp;
1462 state->shader = shader;
1463 state->ht = _mesa_pointer_hash_table_create(NULL);
1464 state->syms = _mesa_set_create(NULL, _mesa_key_hash_string,
1465 _mesa_key_string_equal);
1466 state->index = 0;
1467 }
1468
1469 static void
1470 destroy_print_state(print_state *state)
1471 {
1472 _mesa_hash_table_destroy(state->ht, NULL);
1473 _mesa_set_destroy(state->syms, NULL);
1474 }
1475
1476 void
1477 nir_print_shader_annotated(nir_shader *shader, FILE *fp,
1478 struct hash_table *annotations)
1479 {
1480 print_state state;
1481 init_print_state(&state, shader, fp);
1482
1483 state.annotations = annotations;
1484
1485 fprintf(fp, "shader: %s\n", gl_shader_stage_name(shader->info.stage));
1486
1487 if (shader->info.name)
1488 fprintf(fp, "name: %s\n", shader->info.name);
1489
1490 if (shader->info.label)
1491 fprintf(fp, "label: %s\n", shader->info.label);
1492
1493 if (gl_shader_stage_is_compute(shader->info.stage)) {
1494 fprintf(fp, "local-size: %u, %u, %u%s\n",
1495 shader->info.cs.local_size[0],
1496 shader->info.cs.local_size[1],
1497 shader->info.cs.local_size[2],
1498 shader->info.cs.local_size_variable ? " (variable)" : "");
1499 fprintf(fp, "shared-size: %u\n", shader->info.cs.shared_size);
1500 }
1501
1502 fprintf(fp, "inputs: %u\n", shader->num_inputs);
1503 fprintf(fp, "outputs: %u\n", shader->num_outputs);
1504 fprintf(fp, "uniforms: %u\n", shader->num_uniforms);
1505 fprintf(fp, "shared: %u\n", shader->num_shared);
1506 if (shader->scratch_size)
1507 fprintf(fp, "scratch: %u\n", shader->scratch_size);
1508
1509 nir_foreach_variable(var, &shader->uniforms) {
1510 print_var_decl(var, &state);
1511 }
1512
1513 nir_foreach_variable(var, &shader->inputs) {
1514 print_var_decl(var, &state);
1515 }
1516
1517 nir_foreach_variable(var, &shader->outputs) {
1518 print_var_decl(var, &state);
1519 }
1520
1521 nir_foreach_variable(var, &shader->shared) {
1522 print_var_decl(var, &state);
1523 }
1524
1525 nir_foreach_variable(var, &shader->globals) {
1526 print_var_decl(var, &state);
1527 }
1528
1529 nir_foreach_variable(var, &shader->system_values) {
1530 print_var_decl(var, &state);
1531 }
1532
1533 foreach_list_typed(nir_function, func, node, &shader->functions) {
1534 print_function(func, &state);
1535 }
1536
1537 destroy_print_state(&state);
1538 }
1539
1540 void
1541 nir_print_shader(nir_shader *shader, FILE *fp)
1542 {
1543 nir_print_shader_annotated(shader, fp, NULL);
1544 fflush(fp);
1545 }
1546
1547 void
1548 nir_print_instr(const nir_instr *instr, FILE *fp)
1549 {
1550 print_state state = {
1551 .fp = fp,
1552 };
1553 print_instr(instr, &state, 0);
1554
1555 }
1556
1557 void
1558 nir_print_deref(const nir_deref_instr *deref, FILE *fp)
1559 {
1560 print_state state = {
1561 .fp = fp,
1562 };
1563 print_deref_link(deref, true, &state);
1564 }