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