nir: Add scoped_memory_barrier intrinsic
[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.image.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 #define FORMAT_CASE(x) case x: fprintf(fp, #x " "); break
471 switch (var->data.image.format) {
472 FORMAT_CASE(GL_RGBA32F);
473 FORMAT_CASE(GL_RGBA32UI);
474 FORMAT_CASE(GL_RGBA32I);
475 FORMAT_CASE(GL_R32F);
476 FORMAT_CASE(GL_R32UI);
477 FORMAT_CASE(GL_R32I);
478 FORMAT_CASE(GL_RG32F);
479 FORMAT_CASE(GL_RG32UI);
480 FORMAT_CASE(GL_RG32I);
481 FORMAT_CASE(GL_R8);
482 FORMAT_CASE(GL_RG8);
483 FORMAT_CASE(GL_RGBA8);
484 FORMAT_CASE(GL_R8_SNORM);
485 FORMAT_CASE(GL_RG8_SNORM);
486 FORMAT_CASE(GL_RGBA8_SNORM);
487 FORMAT_CASE(GL_R16);
488 FORMAT_CASE(GL_RG16);
489 FORMAT_CASE(GL_RGBA16);
490 FORMAT_CASE(GL_R16_SNORM);
491 FORMAT_CASE(GL_RG16_SNORM);
492 FORMAT_CASE(GL_RGBA16_SNORM);
493 FORMAT_CASE(GL_R16F);
494 FORMAT_CASE(GL_RG16F);
495 FORMAT_CASE(GL_RGBA16F);
496 FORMAT_CASE(GL_R8UI);
497 FORMAT_CASE(GL_R8I);
498 FORMAT_CASE(GL_RG8UI);
499 FORMAT_CASE(GL_RG8I);
500 FORMAT_CASE(GL_RGBA8UI);
501 FORMAT_CASE(GL_RGBA8I);
502 FORMAT_CASE(GL_R16UI);
503 FORMAT_CASE(GL_R16I);
504 FORMAT_CASE(GL_RG16UI);
505 FORMAT_CASE(GL_RG16I);
506 FORMAT_CASE(GL_RGBA16UI);
507 FORMAT_CASE(GL_RGBA16I);
508 FORMAT_CASE(GL_R11F_G11F_B10F);
509 FORMAT_CASE(GL_RGB9_E5);
510 FORMAT_CASE(GL_RGB10_A2);
511 FORMAT_CASE(GL_RGB10_A2UI);
512 default: /* Including the normal GL_NONE */
513 break;
514 }
515 #undef FORMAT_CASE
516
517 fprintf(fp, "%s %s", glsl_get_type_name(var->type),
518 get_var_name(var, state));
519
520 if (var->data.mode == nir_var_shader_in ||
521 var->data.mode == nir_var_shader_out ||
522 var->data.mode == nir_var_uniform ||
523 var->data.mode == nir_var_mem_ubo ||
524 var->data.mode == nir_var_mem_ssbo) {
525 const char *loc = NULL;
526 char buf[4];
527
528 switch (state->shader->info.stage) {
529 case MESA_SHADER_VERTEX:
530 if (var->data.mode == nir_var_shader_in)
531 loc = gl_vert_attrib_name(var->data.location);
532 else if (var->data.mode == nir_var_shader_out)
533 loc = gl_varying_slot_name(var->data.location);
534 break;
535 case MESA_SHADER_GEOMETRY:
536 if ((var->data.mode == nir_var_shader_in) ||
537 (var->data.mode == nir_var_shader_out))
538 loc = gl_varying_slot_name(var->data.location);
539 break;
540 case MESA_SHADER_FRAGMENT:
541 if (var->data.mode == nir_var_shader_in)
542 loc = gl_varying_slot_name(var->data.location);
543 else if (var->data.mode == nir_var_shader_out)
544 loc = gl_frag_result_name(var->data.location);
545 break;
546 case MESA_SHADER_TESS_CTRL:
547 case MESA_SHADER_TESS_EVAL:
548 case MESA_SHADER_COMPUTE:
549 case MESA_SHADER_KERNEL:
550 default:
551 /* TODO */
552 break;
553 }
554
555 if (!loc) {
556 snprintf(buf, sizeof(buf), "%u", var->data.location);
557 loc = buf;
558 }
559
560 /* For shader I/O vars that have been split to components or packed,
561 * print the fractional location within the input/output.
562 */
563 unsigned int num_components =
564 glsl_get_components(glsl_without_array(var->type));
565 const char *components = NULL;
566 char components_local[6] = {'.' /* the rest is 0-filled */};
567 switch (var->data.mode) {
568 case nir_var_shader_in:
569 case nir_var_shader_out:
570 if (num_components < 4 && num_components != 0) {
571 const char *xyzw = "xyzw";
572 for (int i = 0; i < num_components; i++)
573 components_local[i + 1] = xyzw[i + var->data.location_frac];
574
575 components = components_local;
576 }
577 break;
578 default:
579 break;
580 }
581
582 fprintf(fp, " (%s%s, %u, %u)%s", loc,
583 components ? components : "",
584 var->data.driver_location, var->data.binding,
585 var->data.compact ? " compact" : "");
586 }
587
588 if (var->constant_initializer) {
589 fprintf(fp, " = { ");
590 print_constant(var->constant_initializer, var->type, state);
591 fprintf(fp, " }");
592 }
593
594 fprintf(fp, "\n");
595 print_annotation(state, var);
596 }
597
598 static void
599 print_deref_link(const nir_deref_instr *instr, bool whole_chain, print_state *state)
600 {
601 FILE *fp = state->fp;
602
603 if (instr->deref_type == nir_deref_type_var) {
604 fprintf(fp, "%s", get_var_name(instr->var, state));
605 return;
606 } else if (instr->deref_type == nir_deref_type_cast) {
607 fprintf(fp, "(%s *)", glsl_get_type_name(instr->type));
608 print_src(&instr->parent, state);
609 return;
610 }
611
612 assert(instr->parent.is_ssa);
613 nir_deref_instr *parent =
614 nir_instr_as_deref(instr->parent.ssa->parent_instr);
615
616 /* Is the parent we're going to print a bare cast? */
617 const bool is_parent_cast =
618 whole_chain && parent->deref_type == nir_deref_type_cast;
619
620 /* If we're not printing the whole chain, the parent we print will be a SSA
621 * value that represents a pointer. The only deref type that naturally
622 * gives a pointer is a cast.
623 */
624 const bool is_parent_pointer =
625 !whole_chain || parent->deref_type == nir_deref_type_cast;
626
627 /* Struct derefs have a nice syntax that works on pointers, arrays derefs
628 * do not.
629 */
630 const bool need_deref =
631 is_parent_pointer && instr->deref_type != nir_deref_type_struct;
632
633 /* Cast need extra parens and so * dereferences */
634 if (is_parent_cast || need_deref)
635 fprintf(fp, "(");
636
637 if (need_deref)
638 fprintf(fp, "*");
639
640 if (whole_chain) {
641 print_deref_link(parent, whole_chain, state);
642 } else {
643 print_src(&instr->parent, state);
644 }
645
646 if (is_parent_cast || need_deref)
647 fprintf(fp, ")");
648
649 switch (instr->deref_type) {
650 case nir_deref_type_struct:
651 fprintf(fp, "%s%s", is_parent_pointer ? "->" : ".",
652 glsl_get_struct_elem_name(parent->type, instr->strct.index));
653 break;
654
655 case nir_deref_type_array:
656 case nir_deref_type_ptr_as_array: {
657 if (nir_src_is_const(instr->arr.index)) {
658 fprintf(fp, "[%"PRId64"]", nir_src_as_int(instr->arr.index));
659 } else {
660 fprintf(fp, "[");
661 print_src(&instr->arr.index, state);
662 fprintf(fp, "]");
663 }
664 break;
665 }
666
667 case nir_deref_type_array_wildcard:
668 fprintf(fp, "[*]");
669 break;
670
671 default:
672 unreachable("Invalid deref instruction type");
673 }
674 }
675
676 static void
677 print_deref_instr(nir_deref_instr *instr, print_state *state)
678 {
679 FILE *fp = state->fp;
680
681 print_dest(&instr->dest, state);
682
683 switch (instr->deref_type) {
684 case nir_deref_type_var:
685 fprintf(fp, " = deref_var ");
686 break;
687 case nir_deref_type_array:
688 case nir_deref_type_array_wildcard:
689 fprintf(fp, " = deref_array ");
690 break;
691 case nir_deref_type_struct:
692 fprintf(fp, " = deref_struct ");
693 break;
694 case nir_deref_type_cast:
695 fprintf(fp, " = deref_cast ");
696 break;
697 case nir_deref_type_ptr_as_array:
698 fprintf(fp, " = deref_ptr_as_array ");
699 break;
700 default:
701 unreachable("Invalid deref instruction type");
702 }
703
704 /* Only casts naturally return a pointer type */
705 if (instr->deref_type != nir_deref_type_cast)
706 fprintf(fp, "&");
707
708 print_deref_link(instr, false, state);
709
710 fprintf(fp, " (%s %s) ",
711 get_variable_mode_str(instr->mode, true),
712 glsl_get_type_name(instr->type));
713
714 if (instr->deref_type != nir_deref_type_var &&
715 instr->deref_type != nir_deref_type_cast) {
716 /* Print the entire chain as a comment */
717 fprintf(fp, "/* &");
718 print_deref_link(instr, true, state);
719 fprintf(fp, " */");
720 }
721
722 if (instr->deref_type == nir_deref_type_cast) {
723 fprintf(fp, " /* ptr_stride=%u */", instr->cast.ptr_stride);
724 }
725 }
726
727 static const char *
728 vulkan_descriptor_type_name(VkDescriptorType type)
729 {
730 switch (type) {
731 case VK_DESCRIPTOR_TYPE_SAMPLER: return "sampler";
732 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER: return "texture+sampler";
733 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE: return "texture";
734 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE: return "image";
735 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER: return "texture-buffer";
736 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER: return "image-buffer";
737 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER: return "UBO";
738 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER: return "SSBO";
739 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC: return "UBO";
740 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC: return "SSBO";
741 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT: return "input-att";
742 case VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT: return "inline-UBO";
743 default: return "unknown";
744 }
745 }
746
747 static void
748 print_intrinsic_instr(nir_intrinsic_instr *instr, print_state *state)
749 {
750 const nir_intrinsic_info *info = &nir_intrinsic_infos[instr->intrinsic];
751 unsigned num_srcs = info->num_srcs;
752 FILE *fp = state->fp;
753
754 if (info->has_dest) {
755 print_dest(&instr->dest, state);
756 fprintf(fp, " = ");
757 }
758
759 fprintf(fp, "intrinsic %s (", info->name);
760
761 for (unsigned i = 0; i < num_srcs; i++) {
762 if (i != 0)
763 fprintf(fp, ", ");
764
765 print_src(&instr->src[i], state);
766 }
767
768 fprintf(fp, ") (");
769
770 for (unsigned i = 0; i < info->num_indices; i++) {
771 if (i != 0)
772 fprintf(fp, ", ");
773
774 fprintf(fp, "%d", instr->const_index[i]);
775 }
776
777 fprintf(fp, ")");
778
779 static const char *index_name[NIR_INTRINSIC_NUM_INDEX_FLAGS] = {
780 [NIR_INTRINSIC_BASE] = "base",
781 [NIR_INTRINSIC_WRMASK] = "wrmask",
782 [NIR_INTRINSIC_STREAM_ID] = "stream-id",
783 [NIR_INTRINSIC_UCP_ID] = "ucp-id",
784 [NIR_INTRINSIC_RANGE] = "range",
785 [NIR_INTRINSIC_DESC_SET] = "desc-set",
786 [NIR_INTRINSIC_BINDING] = "binding",
787 [NIR_INTRINSIC_COMPONENT] = "component",
788 [NIR_INTRINSIC_INTERP_MODE] = "interp_mode",
789 [NIR_INTRINSIC_REDUCTION_OP] = "reduction_op",
790 [NIR_INTRINSIC_CLUSTER_SIZE] = "cluster_size",
791 [NIR_INTRINSIC_PARAM_IDX] = "param_idx",
792 [NIR_INTRINSIC_IMAGE_DIM] = "image_dim",
793 [NIR_INTRINSIC_IMAGE_ARRAY] = "image_array",
794 [NIR_INTRINSIC_ACCESS] = "access",
795 [NIR_INTRINSIC_SRC_ACCESS] = "src-access",
796 [NIR_INTRINSIC_DST_ACCESS] = "dst-access",
797 [NIR_INTRINSIC_FORMAT] = "format",
798 [NIR_INTRINSIC_ALIGN_MUL] = "align_mul",
799 [NIR_INTRINSIC_ALIGN_OFFSET] = "align_offset",
800 [NIR_INTRINSIC_DESC_TYPE] = "desc_type",
801 [NIR_INTRINSIC_TYPE] = "type",
802 [NIR_INTRINSIC_SWIZZLE_MASK] = "swizzle_mask",
803 [NIR_INTRINSIC_DRIVER_LOCATION] = "driver_location",
804 [NIR_INTRINSIC_MEMORY_SEMANTICS] = "mem_semantics",
805 [NIR_INTRINSIC_MEMORY_MODES] = "mem_modes",
806 [NIR_INTRINSIC_MEMORY_SCOPE] = "mem_scope",
807 };
808 for (unsigned idx = 1; idx < NIR_INTRINSIC_NUM_INDEX_FLAGS; idx++) {
809 if (!info->index_map[idx])
810 continue;
811 fprintf(fp, " /*");
812 switch (idx) {
813 case NIR_INTRINSIC_WRMASK: {
814 /* special case wrmask to show it as a writemask.. */
815 unsigned wrmask = nir_intrinsic_write_mask(instr);
816 fprintf(fp, " wrmask=");
817 for (unsigned i = 0; i < 4; i++)
818 if ((wrmask >> i) & 1)
819 fprintf(fp, "%c", "xyzw"[i]);
820 break;
821 }
822
823 case NIR_INTRINSIC_REDUCTION_OP: {
824 nir_op reduction_op = nir_intrinsic_reduction_op(instr);
825 fprintf(fp, " reduction_op=%s", nir_op_infos[reduction_op].name);
826 break;
827 }
828
829 case NIR_INTRINSIC_IMAGE_DIM: {
830 static const char *dim_name[] = {
831 [GLSL_SAMPLER_DIM_1D] = "1D",
832 [GLSL_SAMPLER_DIM_2D] = "2D",
833 [GLSL_SAMPLER_DIM_3D] = "3D",
834 [GLSL_SAMPLER_DIM_CUBE] = "Cube",
835 [GLSL_SAMPLER_DIM_RECT] = "Rect",
836 [GLSL_SAMPLER_DIM_BUF] = "Buf",
837 [GLSL_SAMPLER_DIM_MS] = "2D-MSAA",
838 [GLSL_SAMPLER_DIM_SUBPASS] = "Subpass",
839 [GLSL_SAMPLER_DIM_SUBPASS_MS] = "Subpass-MSAA",
840 };
841 enum glsl_sampler_dim dim = nir_intrinsic_image_dim(instr);
842 assert(dim < ARRAY_SIZE(dim_name) && dim_name[dim]);
843 fprintf(fp, " image_dim=%s", dim_name[dim]);
844 break;
845 }
846
847 case NIR_INTRINSIC_IMAGE_ARRAY: {
848 bool array = nir_intrinsic_image_array(instr);
849 fprintf(fp, " image_array=%s", array ? "true" : "false");
850 break;
851 }
852
853 case NIR_INTRINSIC_DESC_TYPE: {
854 VkDescriptorType desc_type = nir_intrinsic_desc_type(instr);
855 fprintf(fp, " desc_type=%s", vulkan_descriptor_type_name(desc_type));
856 break;
857 }
858
859 case NIR_INTRINSIC_TYPE: {
860 nir_alu_type type = nir_intrinsic_type(instr);
861 unsigned size = nir_alu_type_get_type_size(type);
862 const char *name;
863 switch (nir_alu_type_get_base_type(type)) {
864 case nir_type_int: name = "int"; break;
865 case nir_type_uint: name = "uint"; break;
866 case nir_type_bool: name = "bool"; break;
867 case nir_type_float: name = "float"; break;
868 default: name = "invalid";
869 }
870 if (size)
871 fprintf(fp, " type=%s%u", name, size);
872 else
873 fprintf(fp, " type=%s", name);
874 break;
875 }
876
877 case NIR_INTRINSIC_SWIZZLE_MASK: {
878 fprintf(fp, " swizzle_mask=");
879 unsigned mask = nir_intrinsic_swizzle_mask(instr);
880 if (instr->intrinsic == nir_intrinsic_quad_swizzle_amd) {
881 for (unsigned i = 0; i < 4; i++)
882 fprintf(fp, "%d", (mask >> (i * 2) & 3));
883 } else if (instr->intrinsic == nir_intrinsic_masked_swizzle_amd) {
884 fprintf(fp, "((id & %d) | %d) ^ %d", mask & 0x1F,
885 (mask >> 5) & 0x1F,
886 (mask >> 10) & 0x1F);
887 } else {
888 fprintf(fp, "%d", mask);
889 }
890 break;
891 }
892
893 case NIR_INTRINSIC_MEMORY_SEMANTICS: {
894 nir_memory_semantics semantics = nir_intrinsic_memory_semantics(instr);
895 fprintf(fp, " mem_semantics=");
896 switch (semantics & (NIR_MEMORY_ACQUIRE | NIR_MEMORY_RELEASE)) {
897 case 0: fprintf(fp, "NONE"); break;
898 case NIR_MEMORY_ACQUIRE: fprintf(fp, "ACQ"); break;
899 case NIR_MEMORY_RELEASE: fprintf(fp, "REL"); break;
900 default: fprintf(fp, "ACQ|REL"); break;
901 }
902 if (semantics & (NIR_MEMORY_MAKE_AVAILABLE)) fprintf(fp, "|AVAILABLE");
903 if (semantics & (NIR_MEMORY_MAKE_VISIBLE)) fprintf(fp, "|VISIBLE");
904 break;
905 }
906
907 case NIR_INTRINSIC_MEMORY_MODES: {
908 fprintf(fp, " mem_modes=");
909 unsigned int modes = nir_intrinsic_memory_modes(instr);
910 while (modes) {
911 nir_variable_mode m = u_bit_scan(&modes);
912 fprintf(fp, "%s%s", get_variable_mode_str(1 << m, true), modes ? "|" : "");
913 }
914 break;
915 }
916
917 case NIR_INTRINSIC_MEMORY_SCOPE: {
918 fprintf(fp, " mem_scope=");
919 switch (nir_intrinsic_memory_scope(instr)) {
920 case NIR_SCOPE_DEVICE: fprintf(fp, "DEVICE"); break;
921 case NIR_SCOPE_QUEUE_FAMILY: fprintf(fp, "QUEUE_FAMILY"); break;
922 case NIR_SCOPE_WORKGROUP: fprintf(fp, "WORKGROUP"); break;
923 case NIR_SCOPE_SUBGROUP: fprintf(fp, "SUBGROUP"); break;
924 case NIR_SCOPE_INVOCATION: fprintf(fp, "INVOCATION"); break;
925 }
926 break;
927 }
928
929 default: {
930 unsigned off = info->index_map[idx] - 1;
931 assert(index_name[idx]); /* forgot to update index_name table? */
932 fprintf(fp, " %s=%d", index_name[idx], instr->const_index[off]);
933 break;
934 }
935 }
936 fprintf(fp, " */");
937 }
938
939 if (!state->shader)
940 return;
941
942 struct exec_list *var_list = NULL;
943
944 switch (instr->intrinsic) {
945 case nir_intrinsic_load_uniform:
946 var_list = &state->shader->uniforms;
947 break;
948 case nir_intrinsic_load_input:
949 case nir_intrinsic_load_interpolated_input:
950 case nir_intrinsic_load_per_vertex_input:
951 var_list = &state->shader->inputs;
952 break;
953 case nir_intrinsic_load_output:
954 case nir_intrinsic_store_output:
955 case nir_intrinsic_store_per_vertex_output:
956 var_list = &state->shader->outputs;
957 break;
958 default:
959 return;
960 }
961
962 nir_foreach_variable(var, var_list) {
963 if ((var->data.driver_location == nir_intrinsic_base(instr)) &&
964 (instr->intrinsic == nir_intrinsic_load_uniform ||
965 (nir_intrinsic_component(instr) >= var->data.location_frac &&
966 nir_intrinsic_component(instr) <
967 (var->data.location_frac + glsl_get_components(var->type)))) &&
968 var->name) {
969 fprintf(fp, "\t/* %s */", var->name);
970 break;
971 }
972 }
973 }
974
975 static void
976 print_tex_instr(nir_tex_instr *instr, print_state *state)
977 {
978 FILE *fp = state->fp;
979
980 print_dest(&instr->dest, state);
981
982 fprintf(fp, " = ");
983
984 switch (instr->op) {
985 case nir_texop_tex:
986 fprintf(fp, "tex ");
987 break;
988 case nir_texop_txb:
989 fprintf(fp, "txb ");
990 break;
991 case nir_texop_txl:
992 fprintf(fp, "txl ");
993 break;
994 case nir_texop_txd:
995 fprintf(fp, "txd ");
996 break;
997 case nir_texop_txf:
998 fprintf(fp, "txf ");
999 break;
1000 case nir_texop_txf_ms:
1001 fprintf(fp, "txf_ms ");
1002 break;
1003 case nir_texop_txf_ms_fb:
1004 fprintf(fp, "txf_ms_fb ");
1005 break;
1006 case nir_texop_txf_ms_mcs:
1007 fprintf(fp, "txf_ms_mcs ");
1008 break;
1009 case nir_texop_txs:
1010 fprintf(fp, "txs ");
1011 break;
1012 case nir_texop_lod:
1013 fprintf(fp, "lod ");
1014 break;
1015 case nir_texop_tg4:
1016 fprintf(fp, "tg4 ");
1017 break;
1018 case nir_texop_query_levels:
1019 fprintf(fp, "query_levels ");
1020 break;
1021 case nir_texop_texture_samples:
1022 fprintf(fp, "texture_samples ");
1023 break;
1024 case nir_texop_samples_identical:
1025 fprintf(fp, "samples_identical ");
1026 break;
1027 case nir_texop_tex_prefetch:
1028 fprintf(fp, "tex (pre-dispatchable) ");
1029 break;
1030 default:
1031 unreachable("Invalid texture operation");
1032 break;
1033 }
1034
1035 bool has_texture_deref = false, has_sampler_deref = false;
1036 for (unsigned i = 0; i < instr->num_srcs; i++) {
1037 if (i > 0) {
1038 fprintf(fp, ", ");
1039 }
1040
1041 print_src(&instr->src[i].src, state);
1042 fprintf(fp, " ");
1043
1044 switch(instr->src[i].src_type) {
1045 case nir_tex_src_coord:
1046 fprintf(fp, "(coord)");
1047 break;
1048 case nir_tex_src_projector:
1049 fprintf(fp, "(projector)");
1050 break;
1051 case nir_tex_src_comparator:
1052 fprintf(fp, "(comparator)");
1053 break;
1054 case nir_tex_src_offset:
1055 fprintf(fp, "(offset)");
1056 break;
1057 case nir_tex_src_bias:
1058 fprintf(fp, "(bias)");
1059 break;
1060 case nir_tex_src_lod:
1061 fprintf(fp, "(lod)");
1062 break;
1063 case nir_tex_src_min_lod:
1064 fprintf(fp, "(min_lod)");
1065 break;
1066 case nir_tex_src_ms_index:
1067 fprintf(fp, "(ms_index)");
1068 break;
1069 case nir_tex_src_ms_mcs:
1070 fprintf(fp, "(ms_mcs)");
1071 break;
1072 case nir_tex_src_ddx:
1073 fprintf(fp, "(ddx)");
1074 break;
1075 case nir_tex_src_ddy:
1076 fprintf(fp, "(ddy)");
1077 break;
1078 case nir_tex_src_texture_deref:
1079 has_texture_deref = true;
1080 fprintf(fp, "(texture_deref)");
1081 break;
1082 case nir_tex_src_sampler_deref:
1083 has_sampler_deref = true;
1084 fprintf(fp, "(sampler_deref)");
1085 break;
1086 case nir_tex_src_texture_offset:
1087 fprintf(fp, "(texture_offset)");
1088 break;
1089 case nir_tex_src_sampler_offset:
1090 fprintf(fp, "(sampler_offset)");
1091 break;
1092 case nir_tex_src_texture_handle:
1093 fprintf(fp, "(texture_handle)");
1094 break;
1095 case nir_tex_src_sampler_handle:
1096 fprintf(fp, "(sampler_handle)");
1097 break;
1098 case nir_tex_src_plane:
1099 fprintf(fp, "(plane)");
1100 break;
1101
1102 default:
1103 unreachable("Invalid texture source type");
1104 break;
1105 }
1106 }
1107
1108 if (instr->op == nir_texop_tg4) {
1109 fprintf(fp, ", %u (gather_component)", instr->component);
1110 }
1111
1112 if (nir_tex_instr_has_explicit_tg4_offsets(instr)) {
1113 fprintf(fp, ", { (%i, %i)", instr->tg4_offsets[0][0], instr->tg4_offsets[0][1]);
1114 for (unsigned i = 1; i < 4; ++i)
1115 fprintf(fp, ", (%i, %i)", instr->tg4_offsets[i][0],
1116 instr->tg4_offsets[i][1]);
1117 fprintf(fp, " } (offsets)");
1118 }
1119
1120 if (instr->op != nir_texop_txf_ms_fb) {
1121 if (!has_texture_deref) {
1122 fprintf(fp, ", %u (texture)", instr->texture_index);
1123 }
1124
1125 if (!has_sampler_deref) {
1126 fprintf(fp, ", %u (sampler)", instr->sampler_index);
1127 }
1128 }
1129 }
1130
1131 static void
1132 print_call_instr(nir_call_instr *instr, print_state *state)
1133 {
1134 FILE *fp = state->fp;
1135
1136 fprintf(fp, "call %s ", instr->callee->name);
1137
1138 for (unsigned i = 0; i < instr->num_params; i++) {
1139 if (i != 0)
1140 fprintf(fp, ", ");
1141
1142 print_src(&instr->params[i], state);
1143 }
1144 }
1145
1146 static void
1147 print_load_const_instr(nir_load_const_instr *instr, print_state *state)
1148 {
1149 FILE *fp = state->fp;
1150
1151 print_ssa_def(&instr->def, state);
1152
1153 fprintf(fp, " = load_const (");
1154
1155 for (unsigned i = 0; i < instr->def.num_components; i++) {
1156 if (i != 0)
1157 fprintf(fp, ", ");
1158
1159 /*
1160 * we don't really know the type of the constant (if it will be used as a
1161 * float or an int), so just print the raw constant in hex for fidelity
1162 * and then print the float in a comment for readability.
1163 */
1164
1165 switch (instr->def.bit_size) {
1166 case 64:
1167 fprintf(fp, "0x%16" PRIx64 " /* %f */", instr->value[i].u64,
1168 instr->value[i].f64);
1169 break;
1170 case 32:
1171 fprintf(fp, "0x%08x /* %f */", instr->value[i].u32, instr->value[i].f32);
1172 break;
1173 case 16:
1174 fprintf(fp, "0x%04x /* %f */", instr->value[i].u16,
1175 _mesa_half_to_float(instr->value[i].u16));
1176 break;
1177 case 8:
1178 fprintf(fp, "0x%02x", instr->value[i].u8);
1179 break;
1180 case 1:
1181 fprintf(fp, "%s", instr->value[i].b ? "true" : "false");
1182 break;
1183 }
1184 }
1185
1186 fprintf(fp, ")");
1187 }
1188
1189 static void
1190 print_jump_instr(nir_jump_instr *instr, print_state *state)
1191 {
1192 FILE *fp = state->fp;
1193
1194 switch (instr->type) {
1195 case nir_jump_break:
1196 fprintf(fp, "break");
1197 break;
1198
1199 case nir_jump_continue:
1200 fprintf(fp, "continue");
1201 break;
1202
1203 case nir_jump_return:
1204 fprintf(fp, "return");
1205 break;
1206 }
1207 }
1208
1209 static void
1210 print_ssa_undef_instr(nir_ssa_undef_instr* instr, print_state *state)
1211 {
1212 FILE *fp = state->fp;
1213 print_ssa_def(&instr->def, state);
1214 fprintf(fp, " = undefined");
1215 }
1216
1217 static void
1218 print_phi_instr(nir_phi_instr *instr, print_state *state)
1219 {
1220 FILE *fp = state->fp;
1221 print_dest(&instr->dest, state);
1222 fprintf(fp, " = phi ");
1223 nir_foreach_phi_src(src, instr) {
1224 if (&src->node != exec_list_get_head(&instr->srcs))
1225 fprintf(fp, ", ");
1226
1227 fprintf(fp, "block_%u: ", src->pred->index);
1228 print_src(&src->src, state);
1229 }
1230 }
1231
1232 static void
1233 print_parallel_copy_instr(nir_parallel_copy_instr *instr, print_state *state)
1234 {
1235 FILE *fp = state->fp;
1236 nir_foreach_parallel_copy_entry(entry, instr) {
1237 if (&entry->node != exec_list_get_head(&instr->entries))
1238 fprintf(fp, "; ");
1239
1240 print_dest(&entry->dest, state);
1241 fprintf(fp, " = ");
1242 print_src(&entry->src, state);
1243 }
1244 }
1245
1246 static void
1247 print_instr(const nir_instr *instr, print_state *state, unsigned tabs)
1248 {
1249 FILE *fp = state->fp;
1250 print_tabs(tabs, fp);
1251
1252 switch (instr->type) {
1253 case nir_instr_type_alu:
1254 print_alu_instr(nir_instr_as_alu(instr), state);
1255 break;
1256
1257 case nir_instr_type_deref:
1258 print_deref_instr(nir_instr_as_deref(instr), state);
1259 break;
1260
1261 case nir_instr_type_call:
1262 print_call_instr(nir_instr_as_call(instr), state);
1263 break;
1264
1265 case nir_instr_type_intrinsic:
1266 print_intrinsic_instr(nir_instr_as_intrinsic(instr), state);
1267 break;
1268
1269 case nir_instr_type_tex:
1270 print_tex_instr(nir_instr_as_tex(instr), state);
1271 break;
1272
1273 case nir_instr_type_load_const:
1274 print_load_const_instr(nir_instr_as_load_const(instr), state);
1275 break;
1276
1277 case nir_instr_type_jump:
1278 print_jump_instr(nir_instr_as_jump(instr), state);
1279 break;
1280
1281 case nir_instr_type_ssa_undef:
1282 print_ssa_undef_instr(nir_instr_as_ssa_undef(instr), state);
1283 break;
1284
1285 case nir_instr_type_phi:
1286 print_phi_instr(nir_instr_as_phi(instr), state);
1287 break;
1288
1289 case nir_instr_type_parallel_copy:
1290 print_parallel_copy_instr(nir_instr_as_parallel_copy(instr), state);
1291 break;
1292
1293 default:
1294 unreachable("Invalid instruction type");
1295 break;
1296 }
1297 }
1298
1299 static int
1300 compare_block_index(const void *p1, const void *p2)
1301 {
1302 const nir_block *block1 = *((const nir_block **) p1);
1303 const nir_block *block2 = *((const nir_block **) p2);
1304
1305 return (int) block1->index - (int) block2->index;
1306 }
1307
1308 static void print_cf_node(nir_cf_node *node, print_state *state,
1309 unsigned tabs);
1310
1311 static void
1312 print_block(nir_block *block, print_state *state, unsigned tabs)
1313 {
1314 FILE *fp = state->fp;
1315
1316 print_tabs(tabs, fp);
1317 fprintf(fp, "block block_%u:\n", block->index);
1318
1319 /* sort the predecessors by index so we consistently print the same thing */
1320
1321 nir_block **preds =
1322 malloc(block->predecessors->entries * sizeof(nir_block *));
1323
1324 unsigned i = 0;
1325 set_foreach(block->predecessors, entry) {
1326 preds[i++] = (nir_block *) entry->key;
1327 }
1328
1329 qsort(preds, block->predecessors->entries, sizeof(nir_block *),
1330 compare_block_index);
1331
1332 print_tabs(tabs, fp);
1333 fprintf(fp, "/* preds: ");
1334 for (unsigned i = 0; i < block->predecessors->entries; i++) {
1335 fprintf(fp, "block_%u ", preds[i]->index);
1336 }
1337 fprintf(fp, "*/\n");
1338
1339 free(preds);
1340
1341 nir_foreach_instr(instr, block) {
1342 print_instr(instr, state, tabs);
1343 fprintf(fp, "\n");
1344 print_annotation(state, instr);
1345 }
1346
1347 print_tabs(tabs, fp);
1348 fprintf(fp, "/* succs: ");
1349 for (unsigned i = 0; i < 2; i++)
1350 if (block->successors[i]) {
1351 fprintf(fp, "block_%u ", block->successors[i]->index);
1352 }
1353 fprintf(fp, "*/\n");
1354 }
1355
1356 static void
1357 print_if(nir_if *if_stmt, print_state *state, unsigned tabs)
1358 {
1359 FILE *fp = state->fp;
1360
1361 print_tabs(tabs, fp);
1362 fprintf(fp, "if ");
1363 print_src(&if_stmt->condition, state);
1364 fprintf(fp, " {\n");
1365 foreach_list_typed(nir_cf_node, node, node, &if_stmt->then_list) {
1366 print_cf_node(node, state, tabs + 1);
1367 }
1368 print_tabs(tabs, fp);
1369 fprintf(fp, "} else {\n");
1370 foreach_list_typed(nir_cf_node, node, node, &if_stmt->else_list) {
1371 print_cf_node(node, state, tabs + 1);
1372 }
1373 print_tabs(tabs, fp);
1374 fprintf(fp, "}\n");
1375 }
1376
1377 static void
1378 print_loop(nir_loop *loop, print_state *state, unsigned tabs)
1379 {
1380 FILE *fp = state->fp;
1381
1382 print_tabs(tabs, fp);
1383 fprintf(fp, "loop {\n");
1384 foreach_list_typed(nir_cf_node, node, node, &loop->body) {
1385 print_cf_node(node, state, tabs + 1);
1386 }
1387 print_tabs(tabs, fp);
1388 fprintf(fp, "}\n");
1389 }
1390
1391 static void
1392 print_cf_node(nir_cf_node *node, print_state *state, unsigned int tabs)
1393 {
1394 switch (node->type) {
1395 case nir_cf_node_block:
1396 print_block(nir_cf_node_as_block(node), state, tabs);
1397 break;
1398
1399 case nir_cf_node_if:
1400 print_if(nir_cf_node_as_if(node), state, tabs);
1401 break;
1402
1403 case nir_cf_node_loop:
1404 print_loop(nir_cf_node_as_loop(node), state, tabs);
1405 break;
1406
1407 default:
1408 unreachable("Invalid CFG node type");
1409 }
1410 }
1411
1412 static void
1413 print_function_impl(nir_function_impl *impl, print_state *state)
1414 {
1415 FILE *fp = state->fp;
1416
1417 fprintf(fp, "\nimpl %s ", impl->function->name);
1418
1419 fprintf(fp, "{\n");
1420
1421 nir_foreach_variable(var, &impl->locals) {
1422 fprintf(fp, "\t");
1423 print_var_decl(var, state);
1424 }
1425
1426 foreach_list_typed(nir_register, reg, node, &impl->registers) {
1427 fprintf(fp, "\t");
1428 print_register_decl(reg, state);
1429 }
1430
1431 nir_index_blocks(impl);
1432
1433 foreach_list_typed(nir_cf_node, node, node, &impl->body) {
1434 print_cf_node(node, state, 1);
1435 }
1436
1437 fprintf(fp, "\tblock block_%u:\n}\n\n", impl->end_block->index);
1438 }
1439
1440 static void
1441 print_function(nir_function *function, print_state *state)
1442 {
1443 FILE *fp = state->fp;
1444
1445 fprintf(fp, "decl_function %s (%d params)", function->name,
1446 function->num_params);
1447
1448 fprintf(fp, "\n");
1449
1450 if (function->impl != NULL) {
1451 print_function_impl(function->impl, state);
1452 return;
1453 }
1454 }
1455
1456 static void
1457 init_print_state(print_state *state, nir_shader *shader, FILE *fp)
1458 {
1459 state->fp = fp;
1460 state->shader = shader;
1461 state->ht = _mesa_pointer_hash_table_create(NULL);
1462 state->syms = _mesa_set_create(NULL, _mesa_key_hash_string,
1463 _mesa_key_string_equal);
1464 state->index = 0;
1465 }
1466
1467 static void
1468 destroy_print_state(print_state *state)
1469 {
1470 _mesa_hash_table_destroy(state->ht, NULL);
1471 _mesa_set_destroy(state->syms, NULL);
1472 }
1473
1474 void
1475 nir_print_shader_annotated(nir_shader *shader, FILE *fp,
1476 struct hash_table *annotations)
1477 {
1478 print_state state;
1479 init_print_state(&state, shader, fp);
1480
1481 state.annotations = annotations;
1482
1483 fprintf(fp, "shader: %s\n", gl_shader_stage_name(shader->info.stage));
1484
1485 if (shader->info.name)
1486 fprintf(fp, "name: %s\n", shader->info.name);
1487
1488 if (shader->info.label)
1489 fprintf(fp, "label: %s\n", shader->info.label);
1490
1491 if (gl_shader_stage_is_compute(shader->info.stage)) {
1492 fprintf(fp, "local-size: %u, %u, %u%s\n",
1493 shader->info.cs.local_size[0],
1494 shader->info.cs.local_size[1],
1495 shader->info.cs.local_size[2],
1496 shader->info.cs.local_size_variable ? " (variable)" : "");
1497 fprintf(fp, "shared-size: %u\n", shader->info.cs.shared_size);
1498 }
1499
1500 fprintf(fp, "inputs: %u\n", shader->num_inputs);
1501 fprintf(fp, "outputs: %u\n", shader->num_outputs);
1502 fprintf(fp, "uniforms: %u\n", shader->num_uniforms);
1503 fprintf(fp, "shared: %u\n", shader->num_shared);
1504 if (shader->scratch_size)
1505 fprintf(fp, "scratch: %u\n", shader->scratch_size);
1506
1507 nir_foreach_variable(var, &shader->uniforms) {
1508 print_var_decl(var, &state);
1509 }
1510
1511 nir_foreach_variable(var, &shader->inputs) {
1512 print_var_decl(var, &state);
1513 }
1514
1515 nir_foreach_variable(var, &shader->outputs) {
1516 print_var_decl(var, &state);
1517 }
1518
1519 nir_foreach_variable(var, &shader->shared) {
1520 print_var_decl(var, &state);
1521 }
1522
1523 nir_foreach_variable(var, &shader->globals) {
1524 print_var_decl(var, &state);
1525 }
1526
1527 nir_foreach_variable(var, &shader->system_values) {
1528 print_var_decl(var, &state);
1529 }
1530
1531 foreach_list_typed(nir_function, func, node, &shader->functions) {
1532 print_function(func, &state);
1533 }
1534
1535 destroy_print_state(&state);
1536 }
1537
1538 void
1539 nir_print_shader(nir_shader *shader, FILE *fp)
1540 {
1541 nir_print_shader_annotated(shader, fp, NULL);
1542 fflush(fp);
1543 }
1544
1545 void
1546 nir_print_instr(const nir_instr *instr, FILE *fp)
1547 {
1548 print_state state = {
1549 .fp = fp,
1550 };
1551 print_instr(instr, &state, 0);
1552
1553 }
1554
1555 void
1556 nir_print_deref(const nir_deref_instr *deref, FILE *fp)
1557 {
1558 print_state state = {
1559 .fp = fp,
1560 };
1561 print_deref_link(deref, true, &state);
1562 }