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