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