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