• 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 <stdio.h>
31 #include <stdlib.h>
32 #include <inttypes.h> /* for PRIx64 macro */
33 
34 static void
print_tabs(unsigned num_tabs,FILE * fp)35 print_tabs(unsigned num_tabs, FILE *fp)
36 {
37    for (unsigned i = 0; i < num_tabs; i++)
38       fprintf(fp, "\t");
39 }
40 
41 typedef struct {
42    FILE *fp;
43    nir_shader *shader;
44    /** map from nir_variable -> printable name */
45    struct hash_table *ht;
46 
47    /** set of names used so far for nir_variables */
48    struct set *syms;
49 
50    /* an index used to make new non-conflicting names */
51    unsigned index;
52 
53    /**
54     * Optional table of annotations mapping nir object
55     * (such as instr or var) to message to print.
56     */
57    struct hash_table *annotations;
58 } print_state;
59 
60 static void
print_annotation(print_state * state,void * obj)61 print_annotation(print_state *state, void *obj)
62 {
63    if (!state->annotations)
64       return;
65 
66    struct hash_entry *entry = _mesa_hash_table_search(state->annotations, obj);
67    if (!entry)
68       return;
69 
70    const char *note = entry->data;
71    _mesa_hash_table_remove(state->annotations, entry);
72 
73    fprintf(stderr, "%s\n\n", note);
74 }
75 
76 static void
print_register(nir_register * reg,print_state * state)77 print_register(nir_register *reg, print_state *state)
78 {
79    FILE *fp = state->fp;
80    if (reg->name != NULL)
81       fprintf(fp, "/* %s */ ", reg->name);
82    if (reg->is_global)
83       fprintf(fp, "gr%u", reg->index);
84    else
85       fprintf(fp, "r%u", reg->index);
86 }
87 
88 static const char *sizes[] = { "error", "vec1", "vec2", "vec3", "vec4" };
89 
90 static void
print_register_decl(nir_register * reg,print_state * state)91 print_register_decl(nir_register *reg, print_state *state)
92 {
93    FILE *fp = state->fp;
94    fprintf(fp, "decl_reg %s %u ", sizes[reg->num_components], reg->bit_size);
95    if (reg->is_packed)
96       fprintf(fp, "(packed) ");
97    print_register(reg, state);
98    if (reg->num_array_elems != 0)
99       fprintf(fp, "[%u]", reg->num_array_elems);
100    fprintf(fp, "\n");
101 }
102 
103 static void
print_ssa_def(nir_ssa_def * def,print_state * state)104 print_ssa_def(nir_ssa_def *def, print_state *state)
105 {
106    FILE *fp = state->fp;
107    if (def->name != NULL)
108       fprintf(fp, "/* %s */ ", def->name);
109    fprintf(fp, "%s %u ssa_%u", sizes[def->num_components], def->bit_size,
110            def->index);
111 }
112 
113 static void
print_ssa_use(nir_ssa_def * def,print_state * state)114 print_ssa_use(nir_ssa_def *def, print_state *state)
115 {
116    FILE *fp = state->fp;
117    if (def->name != NULL)
118       fprintf(fp, "/* %s */ ", def->name);
119    fprintf(fp, "ssa_%u", def->index);
120 }
121 
122 static void print_src(nir_src *src, print_state *state);
123 
124 static void
print_reg_src(nir_reg_src * src,print_state * state)125 print_reg_src(nir_reg_src *src, print_state *state)
126 {
127    FILE *fp = state->fp;
128    print_register(src->reg, state);
129    if (src->reg->num_array_elems != 0) {
130       fprintf(fp, "[%u", src->base_offset);
131       if (src->indirect != NULL) {
132          fprintf(fp, " + ");
133          print_src(src->indirect, state);
134       }
135       fprintf(fp, "]");
136    }
137 }
138 
139 static void
print_reg_dest(nir_reg_dest * dest,print_state * state)140 print_reg_dest(nir_reg_dest *dest, print_state *state)
141 {
142    FILE *fp = state->fp;
143    print_register(dest->reg, state);
144    if (dest->reg->num_array_elems != 0) {
145       fprintf(fp, "[%u", dest->base_offset);
146       if (dest->indirect != NULL) {
147          fprintf(fp, " + ");
148          print_src(dest->indirect, state);
149       }
150       fprintf(fp, "]");
151    }
152 }
153 
154 static void
print_src(nir_src * src,print_state * state)155 print_src(nir_src *src, print_state *state)
156 {
157    if (src->is_ssa)
158       print_ssa_use(src->ssa, state);
159    else
160       print_reg_src(&src->reg, state);
161 }
162 
163 static void
print_dest(nir_dest * dest,print_state * state)164 print_dest(nir_dest *dest, print_state *state)
165 {
166    if (dest->is_ssa)
167       print_ssa_def(&dest->ssa, state);
168    else
169       print_reg_dest(&dest->reg, state);
170 }
171 
172 static void
print_alu_src(nir_alu_instr * instr,unsigned src,print_state * state)173 print_alu_src(nir_alu_instr *instr, unsigned src, print_state *state)
174 {
175    FILE *fp = state->fp;
176 
177    if (instr->src[src].negate)
178       fprintf(fp, "-");
179    if (instr->src[src].abs)
180       fprintf(fp, "abs(");
181 
182    print_src(&instr->src[src].src, state);
183 
184    bool print_swizzle = false;
185    unsigned used_channels = 0;
186 
187    for (unsigned i = 0; i < 4; i++) {
188       if (!nir_alu_instr_channel_used(instr, src, i))
189          continue;
190 
191       used_channels++;
192 
193       if (instr->src[src].swizzle[i] != i) {
194          print_swizzle = true;
195          break;
196       }
197    }
198 
199    unsigned live_channels = instr->src[src].src.is_ssa
200       ? instr->src[src].src.ssa->num_components
201       : instr->src[src].src.reg.reg->num_components;
202 
203    if (print_swizzle || used_channels != live_channels) {
204       fprintf(fp, ".");
205       for (unsigned i = 0; i < 4; i++) {
206          if (!nir_alu_instr_channel_used(instr, src, i))
207             continue;
208 
209          fprintf(fp, "%c", "xyzw"[instr->src[src].swizzle[i]]);
210       }
211    }
212 
213    if (instr->src[src].abs)
214       fprintf(fp, ")");
215 }
216 
217 static void
print_alu_dest(nir_alu_dest * dest,print_state * state)218 print_alu_dest(nir_alu_dest *dest, print_state *state)
219 {
220    FILE *fp = state->fp;
221    /* we're going to print the saturate modifier later, after the opcode */
222 
223    print_dest(&dest->dest, state);
224 
225    if (!dest->dest.is_ssa &&
226        dest->write_mask != (1 << dest->dest.reg.reg->num_components) - 1) {
227       fprintf(fp, ".");
228       for (unsigned i = 0; i < 4; i++)
229          if ((dest->write_mask >> i) & 1)
230             fprintf(fp, "%c", "xyzw"[i]);
231    }
232 }
233 
234 static void
print_alu_instr(nir_alu_instr * instr,print_state * state)235 print_alu_instr(nir_alu_instr *instr, print_state *state)
236 {
237    FILE *fp = state->fp;
238 
239    print_alu_dest(&instr->dest, state);
240 
241    fprintf(fp, " = %s", nir_op_infos[instr->op].name);
242    if (instr->exact)
243       fprintf(fp, "!");
244    if (instr->dest.saturate)
245       fprintf(fp, ".sat");
246    fprintf(fp, " ");
247 
248    for (unsigned i = 0; i < nir_op_infos[instr->op].num_inputs; i++) {
249       if (i != 0)
250          fprintf(fp, ", ");
251 
252       print_alu_src(instr, i, state);
253    }
254 }
255 
256 static const char *
get_var_name(nir_variable * var,print_state * state)257 get_var_name(nir_variable *var, print_state *state)
258 {
259    if (state->ht == NULL)
260       return var->name ? var->name : "unnamed";
261 
262    assert(state->syms);
263 
264    struct hash_entry *entry = _mesa_hash_table_search(state->ht, var);
265    if (entry)
266       return entry->data;
267 
268    char *name;
269    if (var->name == NULL) {
270       name = ralloc_asprintf(state->syms, "@%u", state->index++);
271    } else {
272       struct set_entry *set_entry = _mesa_set_search(state->syms, var->name);
273       if (set_entry != NULL) {
274          /* we have a collision with another name, append an @ + a unique
275           * index */
276          name = ralloc_asprintf(state->syms, "%s@%u", var->name,
277                                 state->index++);
278       } else {
279          /* Mark this one as seen */
280          _mesa_set_add(state->syms, var->name);
281          name = var->name;
282       }
283    }
284 
285    _mesa_hash_table_insert(state->ht, var, name);
286 
287    return name;
288 }
289 
290 static void
print_constant(nir_constant * c,const struct glsl_type * type,print_state * state)291 print_constant(nir_constant *c, const struct glsl_type *type, print_state *state)
292 {
293    FILE *fp = state->fp;
294    const unsigned rows = glsl_get_vector_elements(type);
295    const unsigned cols = glsl_get_matrix_columns(type);
296    unsigned i, j;
297 
298    switch (glsl_get_base_type(type)) {
299    case GLSL_TYPE_UINT:
300    case GLSL_TYPE_INT:
301    case GLSL_TYPE_BOOL:
302       /* Only float base types can be matrices. */
303       assert(cols == 1);
304 
305       for (i = 0; i < rows; i++) {
306          if (i > 0) fprintf(fp, ", ");
307          fprintf(fp, "0x%08x", c->values[0].u32[i]);
308       }
309       break;
310 
311    case GLSL_TYPE_FLOAT:
312       for (i = 0; i < cols; i++) {
313          for (j = 0; j < rows; j++) {
314             if (i + j > 0) fprintf(fp, ", ");
315             fprintf(fp, "%f", c->values[i].f32[j]);
316          }
317       }
318       break;
319 
320    case GLSL_TYPE_DOUBLE:
321       for (i = 0; i < cols; i++) {
322          for (j = 0; j < rows; j++) {
323             if (i + j > 0) fprintf(fp, ", ");
324             fprintf(fp, "%f", c->values[i].f64[j]);
325          }
326       }
327       break;
328 
329    case GLSL_TYPE_UINT64:
330    case GLSL_TYPE_INT64:
331       /* Only float base types can be matrices. */
332       assert(cols == 1);
333 
334       for (i = 0; i < cols; i++) {
335          if (i > 0) fprintf(fp, ", ");
336          fprintf(fp, "0x%08" PRIx64, c->values[0].u64[i]);
337       }
338       break;
339 
340    case GLSL_TYPE_STRUCT:
341       for (i = 0; i < c->num_elements; i++) {
342          if (i > 0) fprintf(fp, ", ");
343          fprintf(fp, "{ ");
344          print_constant(c->elements[i], glsl_get_struct_field(type, i), state);
345          fprintf(fp, " }");
346       }
347       break;
348 
349    case GLSL_TYPE_ARRAY:
350       for (i = 0; i < c->num_elements; i++) {
351          if (i > 0) fprintf(fp, ", ");
352          fprintf(fp, "{ ");
353          print_constant(c->elements[i], glsl_get_array_element(type), state);
354          fprintf(fp, " }");
355       }
356       break;
357 
358    default:
359       unreachable("not reached");
360    }
361 }
362 
363 static const char *
get_variable_mode_str(nir_variable_mode mode)364 get_variable_mode_str(nir_variable_mode mode)
365 {
366    switch (mode) {
367    case nir_var_shader_in:
368       return "shader_in";
369    case nir_var_shader_out:
370       return "shader_out";
371    case nir_var_uniform:
372       return "uniform";
373    case nir_var_shader_storage:
374       return "shader_storage";
375    case nir_var_system_value:
376       return "system";
377    case nir_var_shared:
378       return "shared";
379    case nir_var_param:
380    case nir_var_global:
381    case nir_var_local:
382    default:
383       return "";
384    }
385 }
386 
387 static void
print_var_decl(nir_variable * var,print_state * state)388 print_var_decl(nir_variable *var, print_state *state)
389 {
390    FILE *fp = state->fp;
391 
392    fprintf(fp, "decl_var ");
393 
394    const char *const cent = (var->data.centroid) ? "centroid " : "";
395    const char *const samp = (var->data.sample) ? "sample " : "";
396    const char *const patch = (var->data.patch) ? "patch " : "";
397    const char *const inv = (var->data.invariant) ? "invariant " : "";
398    fprintf(fp, "%s%s%s%s%s %s ",
399            cent, samp, patch, inv, get_variable_mode_str(var->data.mode),
400            glsl_interp_mode_name(var->data.interpolation));
401 
402    const char *const coher = (var->data.image.coherent) ? "coherent " : "";
403    const char *const volat = (var->data.image._volatile) ? "volatile " : "";
404    const char *const restr = (var->data.image.restrict_flag) ? "restrict " : "";
405    const char *const ronly = (var->data.image.read_only) ? "readonly " : "";
406    const char *const wonly = (var->data.image.write_only) ? "writeonly " : "";
407    fprintf(fp, "%s%s%s%s%s", coher, volat, restr, ronly, wonly);
408 
409    fprintf(fp, "%s %s", glsl_get_type_name(var->type),
410            get_var_name(var, state));
411 
412    if (var->data.mode == nir_var_shader_in ||
413        var->data.mode == nir_var_shader_out ||
414        var->data.mode == nir_var_uniform ||
415        var->data.mode == nir_var_shader_storage) {
416       const char *loc = NULL;
417       char buf[4];
418 
419       switch (state->shader->info.stage) {
420       case MESA_SHADER_VERTEX:
421          if (var->data.mode == nir_var_shader_in)
422             loc = gl_vert_attrib_name(var->data.location);
423          else if (var->data.mode == nir_var_shader_out)
424             loc = gl_varying_slot_name(var->data.location);
425          break;
426       case MESA_SHADER_GEOMETRY:
427          if ((var->data.mode == nir_var_shader_in) ||
428              (var->data.mode == nir_var_shader_out))
429             loc = gl_varying_slot_name(var->data.location);
430          break;
431       case MESA_SHADER_FRAGMENT:
432          if (var->data.mode == nir_var_shader_in)
433             loc = gl_varying_slot_name(var->data.location);
434          else if (var->data.mode == nir_var_shader_out)
435             loc = gl_frag_result_name(var->data.location);
436          break;
437       case MESA_SHADER_TESS_CTRL:
438       case MESA_SHADER_TESS_EVAL:
439       case MESA_SHADER_COMPUTE:
440       default:
441          /* TODO */
442          break;
443       }
444 
445       if (!loc) {
446          snprintf(buf, sizeof(buf), "%u", var->data.location);
447          loc = buf;
448       }
449 
450       /* For shader I/O vars that have been split to components or packed,
451        * print the fractional location within the input/output.
452        */
453       unsigned int num_components =
454          glsl_get_components(glsl_without_array(var->type));
455       const char *components = NULL;
456       char components_local[6] = {'.' /* the rest is 0-filled */};
457       switch (var->data.mode) {
458       case nir_var_shader_in:
459       case nir_var_shader_out:
460          if (num_components < 4 && num_components != 0) {
461             const char *xyzw = "xyzw";
462             for (int i = 0; i < num_components; i++)
463                components_local[i + 1] = xyzw[i + var->data.location_frac];
464 
465             components = components_local;
466          }
467          break;
468       default:
469          break;
470       }
471 
472       fprintf(fp, " (%s%s, %u, %u)%s", loc,
473               components ? components : "",
474               var->data.driver_location, var->data.binding,
475               var->data.compact ? " compact" : "");
476    }
477 
478    if (var->constant_initializer) {
479       fprintf(fp, " = { ");
480       print_constant(var->constant_initializer, var->type, state);
481       fprintf(fp, " }");
482    }
483 
484    fprintf(fp, "\n");
485    print_annotation(state, var);
486 }
487 
488 static void
print_var(nir_variable * var,print_state * state)489 print_var(nir_variable *var, print_state *state)
490 {
491    FILE *fp = state->fp;
492    fprintf(fp, "%s", get_var_name(var, state));
493 }
494 
495 static void
print_arg(nir_variable * var,print_state * state)496 print_arg(nir_variable *var, print_state *state)
497 {
498    FILE *fp = state->fp;
499    fprintf(fp, "%s %s", glsl_get_type_name(var->type),
500            get_var_name(var, state));
501 }
502 
503 static void
print_deref_var(nir_deref_var * deref,print_state * state)504 print_deref_var(nir_deref_var *deref, print_state *state)
505 {
506    print_var(deref->var, state);
507 }
508 
509 static void
print_deref_array(nir_deref_array * deref,print_state * state)510 print_deref_array(nir_deref_array *deref, print_state *state)
511 {
512    FILE *fp = state->fp;
513    fprintf(fp, "[");
514    switch (deref->deref_array_type) {
515    case nir_deref_array_type_direct:
516       fprintf(fp, "%u", deref->base_offset);
517       break;
518    case nir_deref_array_type_indirect:
519       if (deref->base_offset != 0)
520          fprintf(fp, "%u + ", deref->base_offset);
521       print_src(&deref->indirect, state);
522       break;
523    case nir_deref_array_type_wildcard:
524       fprintf(fp, "*");
525       break;
526    }
527    fprintf(fp, "]");
528 }
529 
530 static void
print_deref_struct(nir_deref_struct * deref,const struct glsl_type * parent_type,print_state * state)531 print_deref_struct(nir_deref_struct *deref, const struct glsl_type *parent_type,
532                    print_state *state)
533 {
534    FILE *fp = state->fp;
535    fprintf(fp, ".%s", glsl_get_struct_elem_name(parent_type, deref->index));
536 }
537 
538 static void
print_deref(nir_deref_var * deref,print_state * state)539 print_deref(nir_deref_var *deref, print_state *state)
540 {
541    nir_deref *tail = &deref->deref;
542    nir_deref *pretail = NULL;
543    while (tail != NULL) {
544       switch (tail->deref_type) {
545       case nir_deref_type_var:
546          assert(pretail == NULL);
547          assert(tail == &deref->deref);
548          print_deref_var(deref, state);
549          break;
550 
551       case nir_deref_type_array:
552          assert(pretail != NULL);
553          print_deref_array(nir_deref_as_array(tail), state);
554          break;
555 
556       case nir_deref_type_struct:
557          assert(pretail != NULL);
558          print_deref_struct(nir_deref_as_struct(tail),
559                             pretail->type, state);
560          break;
561 
562       default:
563          unreachable("Invalid deref type");
564       }
565 
566       pretail = tail;
567       tail = pretail->child;
568    }
569 }
570 
571 static void
print_intrinsic_instr(nir_intrinsic_instr * instr,print_state * state)572 print_intrinsic_instr(nir_intrinsic_instr *instr, print_state *state)
573 {
574    const nir_intrinsic_info *info = &nir_intrinsic_infos[instr->intrinsic];
575    unsigned num_srcs = info->num_srcs;
576    FILE *fp = state->fp;
577 
578    if (info->has_dest) {
579       print_dest(&instr->dest, state);
580       fprintf(fp, " = ");
581    }
582 
583    fprintf(fp, "intrinsic %s (", info->name);
584 
585    for (unsigned i = 0; i < num_srcs; i++) {
586       if (i != 0)
587          fprintf(fp, ", ");
588 
589       print_src(&instr->src[i], state);
590    }
591 
592    fprintf(fp, ") (");
593 
594    for (unsigned i = 0; i < info->num_variables; i++) {
595       if (i != 0)
596          fprintf(fp, ", ");
597 
598       print_deref(instr->variables[i], state);
599    }
600 
601    fprintf(fp, ") (");
602 
603    for (unsigned i = 0; i < info->num_indices; i++) {
604       if (i != 0)
605          fprintf(fp, ", ");
606 
607       fprintf(fp, "%d", instr->const_index[i]);
608    }
609 
610    fprintf(fp, ")");
611 
612    static const char *index_name[NIR_INTRINSIC_NUM_INDEX_FLAGS] = {
613       [NIR_INTRINSIC_BASE] = "base",
614       [NIR_INTRINSIC_WRMASK] = "wrmask",
615       [NIR_INTRINSIC_STREAM_ID] = "stream-id",
616       [NIR_INTRINSIC_UCP_ID] = "ucp-id",
617       [NIR_INTRINSIC_RANGE] = "range",
618       [NIR_INTRINSIC_DESC_SET] = "desc-set",
619       [NIR_INTRINSIC_BINDING] = "binding",
620       [NIR_INTRINSIC_COMPONENT] = "component",
621       [NIR_INTRINSIC_INTERP_MODE] = "interp_mode",
622    };
623    for (unsigned idx = 1; idx < NIR_INTRINSIC_NUM_INDEX_FLAGS; idx++) {
624       if (!info->index_map[idx])
625          continue;
626       fprintf(fp, " /*");
627       if (idx == NIR_INTRINSIC_WRMASK) {
628          /* special case wrmask to show it as a writemask.. */
629          unsigned wrmask = nir_intrinsic_write_mask(instr);
630          fprintf(fp, " wrmask=");
631          for (unsigned i = 0; i < 4; i++)
632             if ((wrmask >> i) & 1)
633                fprintf(fp, "%c", "xyzw"[i]);
634       } else {
635          unsigned off = info->index_map[idx] - 1;
636          assert(index_name[idx]);  /* forgot to update index_name table? */
637          fprintf(fp, " %s=%d", index_name[idx], instr->const_index[off]);
638       }
639       fprintf(fp, " */");
640    }
641 
642    if (!state->shader)
643       return;
644 
645    struct exec_list *var_list = NULL;
646 
647    switch (instr->intrinsic) {
648    case nir_intrinsic_load_uniform:
649       var_list = &state->shader->uniforms;
650       break;
651    case nir_intrinsic_load_input:
652    case nir_intrinsic_load_per_vertex_input:
653       var_list = &state->shader->inputs;
654       break;
655    case nir_intrinsic_load_output:
656    case nir_intrinsic_store_output:
657    case nir_intrinsic_store_per_vertex_output:
658       var_list = &state->shader->outputs;
659       break;
660    default:
661       return;
662    }
663 
664    nir_foreach_variable(var, var_list) {
665       if ((var->data.driver_location == nir_intrinsic_base(instr)) &&
666           (instr->intrinsic == nir_intrinsic_load_uniform ||
667            var->data.location_frac == nir_intrinsic_component(instr)) &&
668           var->name) {
669          fprintf(fp, "\t/* %s */", var->name);
670          break;
671       }
672    }
673 }
674 
675 static void
print_tex_instr(nir_tex_instr * instr,print_state * state)676 print_tex_instr(nir_tex_instr *instr, print_state *state)
677 {
678    FILE *fp = state->fp;
679 
680    print_dest(&instr->dest, state);
681 
682    fprintf(fp, " = ");
683 
684    switch (instr->op) {
685    case nir_texop_tex:
686       fprintf(fp, "tex ");
687       break;
688    case nir_texop_txb:
689       fprintf(fp, "txb ");
690       break;
691    case nir_texop_txl:
692       fprintf(fp, "txl ");
693       break;
694    case nir_texop_txd:
695       fprintf(fp, "txd ");
696       break;
697    case nir_texop_txf:
698       fprintf(fp, "txf ");
699       break;
700    case nir_texop_txf_ms:
701       fprintf(fp, "txf_ms ");
702       break;
703    case nir_texop_txf_ms_mcs:
704       fprintf(fp, "txf_ms_mcs ");
705       break;
706    case nir_texop_txs:
707       fprintf(fp, "txs ");
708       break;
709    case nir_texop_lod:
710       fprintf(fp, "lod ");
711       break;
712    case nir_texop_tg4:
713       fprintf(fp, "tg4 ");
714       break;
715    case nir_texop_query_levels:
716       fprintf(fp, "query_levels ");
717       break;
718    case nir_texop_texture_samples:
719       fprintf(fp, "texture_samples ");
720       break;
721    case nir_texop_samples_identical:
722       fprintf(fp, "samples_identical ");
723       break;
724    default:
725       unreachable("Invalid texture operation");
726       break;
727    }
728 
729    for (unsigned i = 0; i < instr->num_srcs; i++) {
730       print_src(&instr->src[i].src, state);
731 
732       fprintf(fp, " ");
733 
734       switch(instr->src[i].src_type) {
735       case nir_tex_src_coord:
736          fprintf(fp, "(coord)");
737          break;
738       case nir_tex_src_projector:
739          fprintf(fp, "(projector)");
740          break;
741       case nir_tex_src_comparator:
742          fprintf(fp, "(comparator)");
743          break;
744       case nir_tex_src_offset:
745          fprintf(fp, "(offset)");
746          break;
747       case nir_tex_src_bias:
748          fprintf(fp, "(bias)");
749          break;
750       case nir_tex_src_lod:
751          fprintf(fp, "(lod)");
752          break;
753       case nir_tex_src_ms_index:
754          fprintf(fp, "(ms_index)");
755          break;
756       case nir_tex_src_ms_mcs:
757          fprintf(fp, "(ms_mcs)");
758          break;
759       case nir_tex_src_ddx:
760          fprintf(fp, "(ddx)");
761          break;
762       case nir_tex_src_ddy:
763          fprintf(fp, "(ddy)");
764          break;
765       case nir_tex_src_texture_offset:
766          fprintf(fp, "(texture_offset)");
767          break;
768       case nir_tex_src_sampler_offset:
769          fprintf(fp, "(sampler_offset)");
770          break;
771       case nir_tex_src_plane:
772          fprintf(fp, "(plane)");
773          break;
774 
775       default:
776          unreachable("Invalid texture source type");
777          break;
778       }
779 
780       fprintf(fp, ", ");
781    }
782 
783    if (instr->op == nir_texop_tg4) {
784       fprintf(fp, "%u (gather_component), ", instr->component);
785    }
786 
787    if (instr->texture) {
788       print_deref(instr->texture, state);
789       fprintf(fp, " (texture)");
790       if (instr->sampler) {
791          print_deref(instr->sampler, state);
792          fprintf(fp, " (sampler)");
793       }
794    } else {
795       assert(instr->sampler == NULL);
796       fprintf(fp, "%u (texture) %u (sampler)",
797               instr->texture_index, instr->sampler_index);
798    }
799 }
800 
801 static void
print_call_instr(nir_call_instr * instr,print_state * state)802 print_call_instr(nir_call_instr *instr, print_state *state)
803 {
804    FILE *fp = state->fp;
805 
806    fprintf(fp, "call %s ", instr->callee->name);
807 
808    for (unsigned i = 0; i < instr->num_params; i++) {
809       if (i != 0)
810          fprintf(fp, ", ");
811 
812       print_deref(instr->params[i], state);
813    }
814 
815    if (instr->return_deref != NULL) {
816       if (instr->num_params != 0)
817          fprintf(fp, ", ");
818       fprintf(fp, "returning ");
819       print_deref(instr->return_deref, state);
820    }
821 }
822 
823 static void
print_load_const_instr(nir_load_const_instr * instr,print_state * state)824 print_load_const_instr(nir_load_const_instr *instr, print_state *state)
825 {
826    FILE *fp = state->fp;
827 
828    print_ssa_def(&instr->def, state);
829 
830    fprintf(fp, " = load_const (");
831 
832    for (unsigned i = 0; i < instr->def.num_components; i++) {
833       if (i != 0)
834          fprintf(fp, ", ");
835 
836       /*
837        * we don't really know the type of the constant (if it will be used as a
838        * float or an int), so just print the raw constant in hex for fidelity
839        * and then print the float in a comment for readability.
840        */
841 
842       if (instr->def.bit_size == 64)
843          fprintf(fp, "0x%16" PRIx64 " /* %f */", instr->value.u64[i],
844                  instr->value.f64[i]);
845       else
846          fprintf(fp, "0x%08x /* %f */", instr->value.u32[i], instr->value.f32[i]);
847    }
848 
849    fprintf(fp, ")");
850 }
851 
852 static void
print_jump_instr(nir_jump_instr * instr,print_state * state)853 print_jump_instr(nir_jump_instr *instr, print_state *state)
854 {
855    FILE *fp = state->fp;
856 
857    switch (instr->type) {
858    case nir_jump_break:
859       fprintf(fp, "break");
860       break;
861 
862    case nir_jump_continue:
863       fprintf(fp, "continue");
864       break;
865 
866    case nir_jump_return:
867       fprintf(fp, "return");
868       break;
869    }
870 }
871 
872 static void
print_ssa_undef_instr(nir_ssa_undef_instr * instr,print_state * state)873 print_ssa_undef_instr(nir_ssa_undef_instr* instr, print_state *state)
874 {
875    FILE *fp = state->fp;
876    print_ssa_def(&instr->def, state);
877    fprintf(fp, " = undefined");
878 }
879 
880 static void
print_phi_instr(nir_phi_instr * instr,print_state * state)881 print_phi_instr(nir_phi_instr *instr, print_state *state)
882 {
883    FILE *fp = state->fp;
884    print_dest(&instr->dest, state);
885    fprintf(fp, " = phi ");
886    nir_foreach_phi_src(src, instr) {
887       if (&src->node != exec_list_get_head(&instr->srcs))
888          fprintf(fp, ", ");
889 
890       fprintf(fp, "block_%u: ", src->pred->index);
891       print_src(&src->src, state);
892    }
893 }
894 
895 static void
print_parallel_copy_instr(nir_parallel_copy_instr * instr,print_state * state)896 print_parallel_copy_instr(nir_parallel_copy_instr *instr, print_state *state)
897 {
898    FILE *fp = state->fp;
899    nir_foreach_parallel_copy_entry(entry, instr) {
900       if (&entry->node != exec_list_get_head(&instr->entries))
901          fprintf(fp, "; ");
902 
903       print_dest(&entry->dest, state);
904       fprintf(fp, " = ");
905       print_src(&entry->src, state);
906    }
907 }
908 
909 static void
print_instr(const nir_instr * instr,print_state * state,unsigned tabs)910 print_instr(const nir_instr *instr, print_state *state, unsigned tabs)
911 {
912    FILE *fp = state->fp;
913    print_tabs(tabs, fp);
914 
915    switch (instr->type) {
916    case nir_instr_type_alu:
917       print_alu_instr(nir_instr_as_alu(instr), state);
918       break;
919 
920    case nir_instr_type_call:
921       print_call_instr(nir_instr_as_call(instr), state);
922       break;
923 
924    case nir_instr_type_intrinsic:
925       print_intrinsic_instr(nir_instr_as_intrinsic(instr), state);
926       break;
927 
928    case nir_instr_type_tex:
929       print_tex_instr(nir_instr_as_tex(instr), state);
930       break;
931 
932    case nir_instr_type_load_const:
933       print_load_const_instr(nir_instr_as_load_const(instr), state);
934       break;
935 
936    case nir_instr_type_jump:
937       print_jump_instr(nir_instr_as_jump(instr), state);
938       break;
939 
940    case nir_instr_type_ssa_undef:
941       print_ssa_undef_instr(nir_instr_as_ssa_undef(instr), state);
942       break;
943 
944    case nir_instr_type_phi:
945       print_phi_instr(nir_instr_as_phi(instr), state);
946       break;
947 
948    case nir_instr_type_parallel_copy:
949       print_parallel_copy_instr(nir_instr_as_parallel_copy(instr), state);
950       break;
951 
952    default:
953       unreachable("Invalid instruction type");
954       break;
955    }
956 }
957 
958 static int
compare_block_index(const void * p1,const void * p2)959 compare_block_index(const void *p1, const void *p2)
960 {
961    const nir_block *block1 = *((const nir_block **) p1);
962    const nir_block *block2 = *((const nir_block **) p2);
963 
964    return (int) block1->index - (int) block2->index;
965 }
966 
967 static void print_cf_node(nir_cf_node *node, print_state *state,
968                           unsigned tabs);
969 
970 static void
print_block(nir_block * block,print_state * state,unsigned tabs)971 print_block(nir_block *block, print_state *state, unsigned tabs)
972 {
973    FILE *fp = state->fp;
974 
975    print_tabs(tabs, fp);
976    fprintf(fp, "block block_%u:\n", block->index);
977 
978    /* sort the predecessors by index so we consistently print the same thing */
979 
980    nir_block **preds =
981       malloc(block->predecessors->entries * sizeof(nir_block *));
982 
983    struct set_entry *entry;
984    unsigned i = 0;
985    set_foreach(block->predecessors, entry) {
986       preds[i++] = (nir_block *) entry->key;
987    }
988 
989    qsort(preds, block->predecessors->entries, sizeof(nir_block *),
990          compare_block_index);
991 
992    print_tabs(tabs, fp);
993    fprintf(fp, "/* preds: ");
994    for (unsigned i = 0; i < block->predecessors->entries; i++) {
995       fprintf(fp, "block_%u ", preds[i]->index);
996    }
997    fprintf(fp, "*/\n");
998 
999    free(preds);
1000 
1001    nir_foreach_instr(instr, block) {
1002       print_instr(instr, state, tabs);
1003       fprintf(fp, "\n");
1004       print_annotation(state, instr);
1005    }
1006 
1007    print_tabs(tabs, fp);
1008    fprintf(fp, "/* succs: ");
1009    for (unsigned i = 0; i < 2; i++)
1010       if (block->successors[i]) {
1011          fprintf(fp, "block_%u ", block->successors[i]->index);
1012       }
1013    fprintf(fp, "*/\n");
1014 }
1015 
1016 static void
print_if(nir_if * if_stmt,print_state * state,unsigned tabs)1017 print_if(nir_if *if_stmt, print_state *state, unsigned tabs)
1018 {
1019    FILE *fp = state->fp;
1020 
1021    print_tabs(tabs, fp);
1022    fprintf(fp, "if ");
1023    print_src(&if_stmt->condition, state);
1024    fprintf(fp, " {\n");
1025    foreach_list_typed(nir_cf_node, node, node, &if_stmt->then_list) {
1026       print_cf_node(node, state, tabs + 1);
1027    }
1028    print_tabs(tabs, fp);
1029    fprintf(fp, "} else {\n");
1030    foreach_list_typed(nir_cf_node, node, node, &if_stmt->else_list) {
1031       print_cf_node(node, state, tabs + 1);
1032    }
1033    print_tabs(tabs, fp);
1034    fprintf(fp, "}\n");
1035 }
1036 
1037 static void
print_loop(nir_loop * loop,print_state * state,unsigned tabs)1038 print_loop(nir_loop *loop, print_state *state, unsigned tabs)
1039 {
1040    FILE *fp = state->fp;
1041 
1042    print_tabs(tabs, fp);
1043    fprintf(fp, "loop {\n");
1044    foreach_list_typed(nir_cf_node, node, node, &loop->body) {
1045       print_cf_node(node, state, tabs + 1);
1046    }
1047    print_tabs(tabs, fp);
1048    fprintf(fp, "}\n");
1049 }
1050 
1051 static void
print_cf_node(nir_cf_node * node,print_state * state,unsigned int tabs)1052 print_cf_node(nir_cf_node *node, print_state *state, unsigned int tabs)
1053 {
1054    switch (node->type) {
1055    case nir_cf_node_block:
1056       print_block(nir_cf_node_as_block(node), state, tabs);
1057       break;
1058 
1059    case nir_cf_node_if:
1060       print_if(nir_cf_node_as_if(node), state, tabs);
1061       break;
1062 
1063    case nir_cf_node_loop:
1064       print_loop(nir_cf_node_as_loop(node), state, tabs);
1065       break;
1066 
1067    default:
1068       unreachable("Invalid CFG node type");
1069    }
1070 }
1071 
1072 static void
print_function_impl(nir_function_impl * impl,print_state * state)1073 print_function_impl(nir_function_impl *impl, print_state *state)
1074 {
1075    FILE *fp = state->fp;
1076 
1077    fprintf(fp, "\nimpl %s ", impl->function->name);
1078 
1079    for (unsigned i = 0; i < impl->num_params; i++) {
1080       if (i != 0)
1081          fprintf(fp, ", ");
1082 
1083       print_arg(impl->params[i], state);
1084    }
1085 
1086    if (impl->return_var != NULL) {
1087       if (impl->num_params != 0)
1088          fprintf(fp, ", ");
1089       fprintf(fp, "returning ");
1090       print_arg(impl->return_var, state);
1091    }
1092 
1093    fprintf(fp, "{\n");
1094 
1095    nir_foreach_variable(var, &impl->locals) {
1096       fprintf(fp, "\t");
1097       print_var_decl(var, state);
1098    }
1099 
1100    foreach_list_typed(nir_register, reg, node, &impl->registers) {
1101       fprintf(fp, "\t");
1102       print_register_decl(reg, state);
1103    }
1104 
1105    nir_index_blocks(impl);
1106 
1107    foreach_list_typed(nir_cf_node, node, node, &impl->body) {
1108       print_cf_node(node, state, 1);
1109    }
1110 
1111    fprintf(fp, "\tblock block_%u:\n}\n\n", impl->end_block->index);
1112 }
1113 
1114 static void
print_function(nir_function * function,print_state * state)1115 print_function(nir_function *function, print_state *state)
1116 {
1117    FILE *fp = state->fp;
1118 
1119    fprintf(fp, "decl_function %s ", function->name);
1120 
1121    for (unsigned i = 0; i < function->num_params; i++) {
1122       if (i != 0)
1123          fprintf(fp, ", ");
1124 
1125       switch (function->params[i].param_type) {
1126       case nir_parameter_in:
1127          fprintf(fp, "in ");
1128          break;
1129       case nir_parameter_out:
1130          fprintf(fp, "out ");
1131          break;
1132       case nir_parameter_inout:
1133          fprintf(fp, "inout ");
1134          break;
1135       default:
1136          unreachable("Invalid parameter type");
1137       }
1138 
1139       fprintf(fp, "%s", glsl_get_type_name(function->params[i].type));
1140    }
1141 
1142    if (function->return_type != NULL) {
1143       if (function->num_params != 0)
1144          fprintf(fp, ", ");
1145       fprintf(fp, "returning %s", glsl_get_type_name(function->return_type));
1146    }
1147 
1148    fprintf(fp, "\n");
1149 
1150    if (function->impl != NULL) {
1151       print_function_impl(function->impl, state);
1152       return;
1153    }
1154 }
1155 
1156 static void
init_print_state(print_state * state,nir_shader * shader,FILE * fp)1157 init_print_state(print_state *state, nir_shader *shader, FILE *fp)
1158 {
1159    state->fp = fp;
1160    state->shader = shader;
1161    state->ht = _mesa_hash_table_create(NULL, _mesa_hash_pointer,
1162                                        _mesa_key_pointer_equal);
1163    state->syms = _mesa_set_create(NULL, _mesa_key_hash_string,
1164                                   _mesa_key_string_equal);
1165    state->index = 0;
1166 }
1167 
1168 static void
destroy_print_state(print_state * state)1169 destroy_print_state(print_state *state)
1170 {
1171    _mesa_hash_table_destroy(state->ht, NULL);
1172    _mesa_set_destroy(state->syms, NULL);
1173 }
1174 
1175 void
nir_print_shader_annotated(nir_shader * shader,FILE * fp,struct hash_table * annotations)1176 nir_print_shader_annotated(nir_shader *shader, FILE *fp,
1177                            struct hash_table *annotations)
1178 {
1179    print_state state;
1180    init_print_state(&state, shader, fp);
1181 
1182    state.annotations = annotations;
1183 
1184    fprintf(fp, "shader: %s\n", gl_shader_stage_name(shader->info.stage));
1185 
1186    if (shader->info.name)
1187       fprintf(fp, "name: %s\n", shader->info.name);
1188 
1189    if (shader->info.label)
1190       fprintf(fp, "label: %s\n", shader->info.label);
1191 
1192    switch (shader->info.stage) {
1193    case MESA_SHADER_COMPUTE:
1194       fprintf(fp, "local-size: %u, %u, %u%s\n",
1195               shader->info.cs.local_size[0],
1196               shader->info.cs.local_size[1],
1197               shader->info.cs.local_size[2],
1198               shader->info.cs.local_size_variable ? " (variable)" : "");
1199       fprintf(fp, "shared-size: %u\n", shader->info.cs.shared_size);
1200       break;
1201    default:
1202       break;
1203    }
1204 
1205    fprintf(fp, "inputs: %u\n", shader->num_inputs);
1206    fprintf(fp, "outputs: %u\n", shader->num_outputs);
1207    fprintf(fp, "uniforms: %u\n", shader->num_uniforms);
1208    fprintf(fp, "shared: %u\n", shader->num_shared);
1209 
1210    nir_foreach_variable(var, &shader->uniforms) {
1211       print_var_decl(var, &state);
1212    }
1213 
1214    nir_foreach_variable(var, &shader->inputs) {
1215       print_var_decl(var, &state);
1216    }
1217 
1218    nir_foreach_variable(var, &shader->outputs) {
1219       print_var_decl(var, &state);
1220    }
1221 
1222    nir_foreach_variable(var, &shader->shared) {
1223       print_var_decl(var, &state);
1224    }
1225 
1226    nir_foreach_variable(var, &shader->globals) {
1227       print_var_decl(var, &state);
1228    }
1229 
1230    nir_foreach_variable(var, &shader->system_values) {
1231       print_var_decl(var, &state);
1232    }
1233 
1234    foreach_list_typed(nir_register, reg, node, &shader->registers) {
1235       print_register_decl(reg, &state);
1236    }
1237 
1238    foreach_list_typed(nir_function, func, node, &shader->functions) {
1239       print_function(func, &state);
1240    }
1241 
1242    destroy_print_state(&state);
1243 }
1244 
1245 void
nir_print_shader(nir_shader * shader,FILE * fp)1246 nir_print_shader(nir_shader *shader, FILE *fp)
1247 {
1248    nir_print_shader_annotated(shader, fp, NULL);
1249 }
1250 
1251 void
nir_print_instr(const nir_instr * instr,FILE * fp)1252 nir_print_instr(const nir_instr *instr, FILE *fp)
1253 {
1254    print_state state = {
1255       .fp = fp,
1256    };
1257    print_instr(instr, &state, 0);
1258 
1259 }
1260