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