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