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