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