5a626c66960c9470bd9b60bc304274dad17b487b
[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 if (instr->deref_type == nir_deref_type_cast) {
721 fprintf(fp, " /* ptr_stride=%u */", instr->cast.ptr_stride);
722 }
723 }
724
725 static const char *
726 vulkan_descriptor_type_name(VkDescriptorType type)
727 {
728 switch (type) {
729 case VK_DESCRIPTOR_TYPE_SAMPLER: return "sampler";
730 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER: return "texture+sampler";
731 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE: return "texture";
732 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE: return "image";
733 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER: return "texture-buffer";
734 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER: return "image-buffer";
735 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER: return "UBO";
736 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER: return "SSBO";
737 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC: return "UBO";
738 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC: return "SSBO";
739 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT: return "input-att";
740 case VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT: return "inline-UBO";
741 default: return "unknown";
742 }
743 }
744
745 static void
746 print_intrinsic_instr(nir_intrinsic_instr *instr, print_state *state)
747 {
748 const nir_intrinsic_info *info = &nir_intrinsic_infos[instr->intrinsic];
749 unsigned num_srcs = info->num_srcs;
750 FILE *fp = state->fp;
751
752 if (info->has_dest) {
753 print_dest(&instr->dest, state);
754 fprintf(fp, " = ");
755 }
756
757 fprintf(fp, "intrinsic %s (", info->name);
758
759 for (unsigned i = 0; i < num_srcs; i++) {
760 if (i != 0)
761 fprintf(fp, ", ");
762
763 print_src(&instr->src[i], state);
764 }
765
766 fprintf(fp, ") (");
767
768 for (unsigned i = 0; i < info->num_indices; i++) {
769 if (i != 0)
770 fprintf(fp, ", ");
771
772 fprintf(fp, "%d", instr->const_index[i]);
773 }
774
775 fprintf(fp, ")");
776
777 static const char *index_name[NIR_INTRINSIC_NUM_INDEX_FLAGS] = {
778 [NIR_INTRINSIC_BASE] = "base",
779 [NIR_INTRINSIC_WRMASK] = "wrmask",
780 [NIR_INTRINSIC_STREAM_ID] = "stream-id",
781 [NIR_INTRINSIC_UCP_ID] = "ucp-id",
782 [NIR_INTRINSIC_RANGE] = "range",
783 [NIR_INTRINSIC_DESC_SET] = "desc-set",
784 [NIR_INTRINSIC_BINDING] = "binding",
785 [NIR_INTRINSIC_COMPONENT] = "component",
786 [NIR_INTRINSIC_INTERP_MODE] = "interp_mode",
787 [NIR_INTRINSIC_REDUCTION_OP] = "reduction_op",
788 [NIR_INTRINSIC_CLUSTER_SIZE] = "cluster_size",
789 [NIR_INTRINSIC_PARAM_IDX] = "param_idx",
790 [NIR_INTRINSIC_IMAGE_DIM] = "image_dim",
791 [NIR_INTRINSIC_IMAGE_ARRAY] = "image_array",
792 [NIR_INTRINSIC_ACCESS] = "access",
793 [NIR_INTRINSIC_SRC_ACCESS] = "src-access",
794 [NIR_INTRINSIC_DST_ACCESS] = "dst-access",
795 [NIR_INTRINSIC_FORMAT] = "format",
796 [NIR_INTRINSIC_ALIGN_MUL] = "align_mul",
797 [NIR_INTRINSIC_ALIGN_OFFSET] = "align_offset",
798 [NIR_INTRINSIC_DESC_TYPE] = "desc_type",
799 [NIR_INTRINSIC_TYPE] = "type",
800 [NIR_INTRINSIC_SWIZZLE_MASK] = "swizzle_mask",
801 };
802 for (unsigned idx = 1; idx < NIR_INTRINSIC_NUM_INDEX_FLAGS; idx++) {
803 if (!info->index_map[idx])
804 continue;
805 fprintf(fp, " /*");
806 if (idx == NIR_INTRINSIC_WRMASK) {
807 /* special case wrmask to show it as a writemask.. */
808 unsigned wrmask = nir_intrinsic_write_mask(instr);
809 fprintf(fp, " wrmask=");
810 for (unsigned i = 0; i < 4; i++)
811 if ((wrmask >> i) & 1)
812 fprintf(fp, "%c", "xyzw"[i]);
813 } else if (idx == NIR_INTRINSIC_REDUCTION_OP) {
814 nir_op reduction_op = nir_intrinsic_reduction_op(instr);
815 fprintf(fp, " reduction_op=%s", nir_op_infos[reduction_op].name);
816 } else if (idx == NIR_INTRINSIC_IMAGE_DIM) {
817 static const char *dim_name[] = {
818 [GLSL_SAMPLER_DIM_1D] = "1D",
819 [GLSL_SAMPLER_DIM_2D] = "2D",
820 [GLSL_SAMPLER_DIM_3D] = "3D",
821 [GLSL_SAMPLER_DIM_CUBE] = "Cube",
822 [GLSL_SAMPLER_DIM_RECT] = "Rect",
823 [GLSL_SAMPLER_DIM_BUF] = "Buf",
824 [GLSL_SAMPLER_DIM_MS] = "2D-MSAA",
825 [GLSL_SAMPLER_DIM_SUBPASS] = "Subpass",
826 [GLSL_SAMPLER_DIM_SUBPASS_MS] = "Subpass-MSAA",
827 };
828 enum glsl_sampler_dim dim = nir_intrinsic_image_dim(instr);
829 assert(dim < ARRAY_SIZE(dim_name) && dim_name[dim]);
830 fprintf(fp, " image_dim=%s", dim_name[dim]);
831 } else if (idx == NIR_INTRINSIC_IMAGE_ARRAY) {
832 bool array = nir_intrinsic_image_array(instr);
833 fprintf(fp, " image_array=%s", array ? "true" : "false");
834 } else if (idx == NIR_INTRINSIC_DESC_TYPE) {
835 VkDescriptorType desc_type = nir_intrinsic_desc_type(instr);
836 fprintf(fp, " desc_type=%s", vulkan_descriptor_type_name(desc_type));
837 } else if (idx == NIR_INTRINSIC_TYPE) {
838 nir_alu_type type = nir_intrinsic_type(instr);
839 unsigned size = nir_alu_type_get_type_size(type);
840 const char *name;
841 switch (nir_alu_type_get_base_type(type)) {
842 case nir_type_int: name = "int"; break;
843 case nir_type_uint: name = "uint"; break;
844 case nir_type_bool: name = "bool"; break;
845 case nir_type_float: name = "float"; break;
846 default: name = "invalid";
847 }
848 if (size)
849 fprintf(fp, " type=%s%u", name, size);
850 else
851 fprintf(fp, " type=%s", name);
852 } else if (idx == NIR_INTRINSIC_SWIZZLE_MASK) {
853 fprintf(fp, " swizzle_mask=");
854 unsigned mask = nir_intrinsic_swizzle_mask(instr);
855 if (instr->intrinsic == nir_intrinsic_quad_swizzle_amd) {
856 for (unsigned i = 0; i < 4; i++)
857 fprintf(fp, "%d", (mask >> (i * 2) & 3));
858 } else if (instr->intrinsic == nir_intrinsic_masked_swizzle_amd) {
859 fprintf(fp, "((id & %d) | %d) ^ %d", mask & 0x1F,
860 (mask >> 5) & 0x1F,
861 (mask >> 10) & 0x1F);
862 } else {
863 fprintf(fp, "%d", mask);
864 }
865 } else {
866 unsigned off = info->index_map[idx] - 1;
867 assert(index_name[idx]); /* forgot to update index_name table? */
868 fprintf(fp, " %s=%d", index_name[idx], instr->const_index[off]);
869 }
870 fprintf(fp, " */");
871 }
872
873 if (!state->shader)
874 return;
875
876 struct exec_list *var_list = NULL;
877
878 switch (instr->intrinsic) {
879 case nir_intrinsic_load_uniform:
880 var_list = &state->shader->uniforms;
881 break;
882 case nir_intrinsic_load_input:
883 case nir_intrinsic_load_interpolated_input:
884 case nir_intrinsic_load_per_vertex_input:
885 var_list = &state->shader->inputs;
886 break;
887 case nir_intrinsic_load_output:
888 case nir_intrinsic_store_output:
889 case nir_intrinsic_store_per_vertex_output:
890 var_list = &state->shader->outputs;
891 break;
892 default:
893 return;
894 }
895
896 nir_foreach_variable(var, var_list) {
897 if ((var->data.driver_location == nir_intrinsic_base(instr)) &&
898 (instr->intrinsic == nir_intrinsic_load_uniform ||
899 (nir_intrinsic_component(instr) >= var->data.location_frac &&
900 nir_intrinsic_component(instr) <
901 (var->data.location_frac + glsl_get_components(var->type)))) &&
902 var->name) {
903 fprintf(fp, "\t/* %s */", var->name);
904 break;
905 }
906 }
907 }
908
909 static void
910 print_tex_instr(nir_tex_instr *instr, print_state *state)
911 {
912 FILE *fp = state->fp;
913
914 print_dest(&instr->dest, state);
915
916 fprintf(fp, " = ");
917
918 switch (instr->op) {
919 case nir_texop_tex:
920 fprintf(fp, "tex ");
921 break;
922 case nir_texop_txb:
923 fprintf(fp, "txb ");
924 break;
925 case nir_texop_txl:
926 fprintf(fp, "txl ");
927 break;
928 case nir_texop_txd:
929 fprintf(fp, "txd ");
930 break;
931 case nir_texop_txf:
932 fprintf(fp, "txf ");
933 break;
934 case nir_texop_txf_ms:
935 fprintf(fp, "txf_ms ");
936 break;
937 case nir_texop_txf_ms_fb:
938 fprintf(fp, "txf_ms_fb ");
939 break;
940 case nir_texop_txf_ms_mcs:
941 fprintf(fp, "txf_ms_mcs ");
942 break;
943 case nir_texop_txs:
944 fprintf(fp, "txs ");
945 break;
946 case nir_texop_lod:
947 fprintf(fp, "lod ");
948 break;
949 case nir_texop_tg4:
950 fprintf(fp, "tg4 ");
951 break;
952 case nir_texop_query_levels:
953 fprintf(fp, "query_levels ");
954 break;
955 case nir_texop_texture_samples:
956 fprintf(fp, "texture_samples ");
957 break;
958 case nir_texop_samples_identical:
959 fprintf(fp, "samples_identical ");
960 break;
961 default:
962 unreachable("Invalid texture operation");
963 break;
964 }
965
966 bool has_texture_deref = false, has_sampler_deref = false;
967 for (unsigned i = 0; i < instr->num_srcs; i++) {
968 if (i > 0) {
969 fprintf(fp, ", ");
970 }
971
972 print_src(&instr->src[i].src, state);
973 fprintf(fp, " ");
974
975 switch(instr->src[i].src_type) {
976 case nir_tex_src_coord:
977 fprintf(fp, "(coord)");
978 break;
979 case nir_tex_src_projector:
980 fprintf(fp, "(projector)");
981 break;
982 case nir_tex_src_comparator:
983 fprintf(fp, "(comparator)");
984 break;
985 case nir_tex_src_offset:
986 fprintf(fp, "(offset)");
987 break;
988 case nir_tex_src_bias:
989 fprintf(fp, "(bias)");
990 break;
991 case nir_tex_src_lod:
992 fprintf(fp, "(lod)");
993 break;
994 case nir_tex_src_min_lod:
995 fprintf(fp, "(min_lod)");
996 break;
997 case nir_tex_src_ms_index:
998 fprintf(fp, "(ms_index)");
999 break;
1000 case nir_tex_src_ms_mcs:
1001 fprintf(fp, "(ms_mcs)");
1002 break;
1003 case nir_tex_src_ddx:
1004 fprintf(fp, "(ddx)");
1005 break;
1006 case nir_tex_src_ddy:
1007 fprintf(fp, "(ddy)");
1008 break;
1009 case nir_tex_src_texture_deref:
1010 has_texture_deref = true;
1011 fprintf(fp, "(texture_deref)");
1012 break;
1013 case nir_tex_src_sampler_deref:
1014 has_sampler_deref = true;
1015 fprintf(fp, "(sampler_deref)");
1016 break;
1017 case nir_tex_src_texture_offset:
1018 fprintf(fp, "(texture_offset)");
1019 break;
1020 case nir_tex_src_sampler_offset:
1021 fprintf(fp, "(sampler_offset)");
1022 break;
1023 case nir_tex_src_texture_handle:
1024 fprintf(fp, "(texture_handle)");
1025 break;
1026 case nir_tex_src_sampler_handle:
1027 fprintf(fp, "(sampler_handle)");
1028 break;
1029 case nir_tex_src_plane:
1030 fprintf(fp, "(plane)");
1031 break;
1032
1033 default:
1034 unreachable("Invalid texture source type");
1035 break;
1036 }
1037 }
1038
1039 if (instr->op == nir_texop_tg4) {
1040 fprintf(fp, ", %u (gather_component)", instr->component);
1041 }
1042
1043 if (nir_tex_instr_has_explicit_tg4_offsets(instr)) {
1044 fprintf(fp, ", { (%i, %i)", instr->tg4_offsets[0][0], instr->tg4_offsets[0][1]);
1045 for (unsigned i = 1; i < 4; ++i)
1046 fprintf(fp, ", (%i, %i)", instr->tg4_offsets[i][0],
1047 instr->tg4_offsets[i][1]);
1048 fprintf(fp, " } (offsets)");
1049 }
1050
1051 if (instr->op != nir_texop_txf_ms_fb) {
1052 if (!has_texture_deref) {
1053 fprintf(fp, ", %u (texture)", instr->texture_index);
1054 }
1055
1056 if (!has_sampler_deref) {
1057 fprintf(fp, ", %u (sampler)", instr->sampler_index);
1058 }
1059 }
1060 }
1061
1062 static void
1063 print_call_instr(nir_call_instr *instr, print_state *state)
1064 {
1065 FILE *fp = state->fp;
1066
1067 fprintf(fp, "call %s ", instr->callee->name);
1068
1069 for (unsigned i = 0; i < instr->num_params; i++) {
1070 if (i != 0)
1071 fprintf(fp, ", ");
1072
1073 print_src(&instr->params[i], state);
1074 }
1075 }
1076
1077 static void
1078 print_load_const_instr(nir_load_const_instr *instr, print_state *state)
1079 {
1080 FILE *fp = state->fp;
1081
1082 print_ssa_def(&instr->def, state);
1083
1084 fprintf(fp, " = load_const (");
1085
1086 for (unsigned i = 0; i < instr->def.num_components; i++) {
1087 if (i != 0)
1088 fprintf(fp, ", ");
1089
1090 /*
1091 * we don't really know the type of the constant (if it will be used as a
1092 * float or an int), so just print the raw constant in hex for fidelity
1093 * and then print the float in a comment for readability.
1094 */
1095
1096 switch (instr->def.bit_size) {
1097 case 64:
1098 fprintf(fp, "0x%16" PRIx64 " /* %f */", instr->value[i].u64,
1099 instr->value[i].f64);
1100 break;
1101 case 32:
1102 fprintf(fp, "0x%08x /* %f */", instr->value[i].u32, instr->value[i].f32);
1103 break;
1104 case 16:
1105 fprintf(fp, "0x%04x /* %f */", instr->value[i].u16,
1106 _mesa_half_to_float(instr->value[i].u16));
1107 break;
1108 case 8:
1109 fprintf(fp, "0x%02x", instr->value[i].u8);
1110 break;
1111 case 1:
1112 fprintf(fp, "%s", instr->value[i].b ? "true" : "false");
1113 break;
1114 }
1115 }
1116
1117 fprintf(fp, ")");
1118 }
1119
1120 static void
1121 print_jump_instr(nir_jump_instr *instr, print_state *state)
1122 {
1123 FILE *fp = state->fp;
1124
1125 switch (instr->type) {
1126 case nir_jump_break:
1127 fprintf(fp, "break");
1128 break;
1129
1130 case nir_jump_continue:
1131 fprintf(fp, "continue");
1132 break;
1133
1134 case nir_jump_return:
1135 fprintf(fp, "return");
1136 break;
1137 }
1138 }
1139
1140 static void
1141 print_ssa_undef_instr(nir_ssa_undef_instr* instr, print_state *state)
1142 {
1143 FILE *fp = state->fp;
1144 print_ssa_def(&instr->def, state);
1145 fprintf(fp, " = undefined");
1146 }
1147
1148 static void
1149 print_phi_instr(nir_phi_instr *instr, print_state *state)
1150 {
1151 FILE *fp = state->fp;
1152 print_dest(&instr->dest, state);
1153 fprintf(fp, " = phi ");
1154 nir_foreach_phi_src(src, instr) {
1155 if (&src->node != exec_list_get_head(&instr->srcs))
1156 fprintf(fp, ", ");
1157
1158 fprintf(fp, "block_%u: ", src->pred->index);
1159 print_src(&src->src, state);
1160 }
1161 }
1162
1163 static void
1164 print_parallel_copy_instr(nir_parallel_copy_instr *instr, print_state *state)
1165 {
1166 FILE *fp = state->fp;
1167 nir_foreach_parallel_copy_entry(entry, instr) {
1168 if (&entry->node != exec_list_get_head(&instr->entries))
1169 fprintf(fp, "; ");
1170
1171 print_dest(&entry->dest, state);
1172 fprintf(fp, " = ");
1173 print_src(&entry->src, state);
1174 }
1175 }
1176
1177 static void
1178 print_instr(const nir_instr *instr, print_state *state, unsigned tabs)
1179 {
1180 FILE *fp = state->fp;
1181 print_tabs(tabs, fp);
1182
1183 switch (instr->type) {
1184 case nir_instr_type_alu:
1185 print_alu_instr(nir_instr_as_alu(instr), state);
1186 break;
1187
1188 case nir_instr_type_deref:
1189 print_deref_instr(nir_instr_as_deref(instr), state);
1190 break;
1191
1192 case nir_instr_type_call:
1193 print_call_instr(nir_instr_as_call(instr), state);
1194 break;
1195
1196 case nir_instr_type_intrinsic:
1197 print_intrinsic_instr(nir_instr_as_intrinsic(instr), state);
1198 break;
1199
1200 case nir_instr_type_tex:
1201 print_tex_instr(nir_instr_as_tex(instr), state);
1202 break;
1203
1204 case nir_instr_type_load_const:
1205 print_load_const_instr(nir_instr_as_load_const(instr), state);
1206 break;
1207
1208 case nir_instr_type_jump:
1209 print_jump_instr(nir_instr_as_jump(instr), state);
1210 break;
1211
1212 case nir_instr_type_ssa_undef:
1213 print_ssa_undef_instr(nir_instr_as_ssa_undef(instr), state);
1214 break;
1215
1216 case nir_instr_type_phi:
1217 print_phi_instr(nir_instr_as_phi(instr), state);
1218 break;
1219
1220 case nir_instr_type_parallel_copy:
1221 print_parallel_copy_instr(nir_instr_as_parallel_copy(instr), state);
1222 break;
1223
1224 default:
1225 unreachable("Invalid instruction type");
1226 break;
1227 }
1228 }
1229
1230 static int
1231 compare_block_index(const void *p1, const void *p2)
1232 {
1233 const nir_block *block1 = *((const nir_block **) p1);
1234 const nir_block *block2 = *((const nir_block **) p2);
1235
1236 return (int) block1->index - (int) block2->index;
1237 }
1238
1239 static void print_cf_node(nir_cf_node *node, print_state *state,
1240 unsigned tabs);
1241
1242 static void
1243 print_block(nir_block *block, print_state *state, unsigned tabs)
1244 {
1245 FILE *fp = state->fp;
1246
1247 print_tabs(tabs, fp);
1248 fprintf(fp, "block block_%u:\n", block->index);
1249
1250 /* sort the predecessors by index so we consistently print the same thing */
1251
1252 nir_block **preds =
1253 malloc(block->predecessors->entries * sizeof(nir_block *));
1254
1255 unsigned i = 0;
1256 set_foreach(block->predecessors, entry) {
1257 preds[i++] = (nir_block *) entry->key;
1258 }
1259
1260 qsort(preds, block->predecessors->entries, sizeof(nir_block *),
1261 compare_block_index);
1262
1263 print_tabs(tabs, fp);
1264 fprintf(fp, "/* preds: ");
1265 for (unsigned i = 0; i < block->predecessors->entries; i++) {
1266 fprintf(fp, "block_%u ", preds[i]->index);
1267 }
1268 fprintf(fp, "*/\n");
1269
1270 free(preds);
1271
1272 nir_foreach_instr(instr, block) {
1273 print_instr(instr, state, tabs);
1274 fprintf(fp, "\n");
1275 print_annotation(state, instr);
1276 }
1277
1278 print_tabs(tabs, fp);
1279 fprintf(fp, "/* succs: ");
1280 for (unsigned i = 0; i < 2; i++)
1281 if (block->successors[i]) {
1282 fprintf(fp, "block_%u ", block->successors[i]->index);
1283 }
1284 fprintf(fp, "*/\n");
1285 }
1286
1287 static void
1288 print_if(nir_if *if_stmt, print_state *state, unsigned tabs)
1289 {
1290 FILE *fp = state->fp;
1291
1292 print_tabs(tabs, fp);
1293 fprintf(fp, "if ");
1294 print_src(&if_stmt->condition, state);
1295 fprintf(fp, " {\n");
1296 foreach_list_typed(nir_cf_node, node, node, &if_stmt->then_list) {
1297 print_cf_node(node, state, tabs + 1);
1298 }
1299 print_tabs(tabs, fp);
1300 fprintf(fp, "} else {\n");
1301 foreach_list_typed(nir_cf_node, node, node, &if_stmt->else_list) {
1302 print_cf_node(node, state, tabs + 1);
1303 }
1304 print_tabs(tabs, fp);
1305 fprintf(fp, "}\n");
1306 }
1307
1308 static void
1309 print_loop(nir_loop *loop, print_state *state, unsigned tabs)
1310 {
1311 FILE *fp = state->fp;
1312
1313 print_tabs(tabs, fp);
1314 fprintf(fp, "loop {\n");
1315 foreach_list_typed(nir_cf_node, node, node, &loop->body) {
1316 print_cf_node(node, state, tabs + 1);
1317 }
1318 print_tabs(tabs, fp);
1319 fprintf(fp, "}\n");
1320 }
1321
1322 static void
1323 print_cf_node(nir_cf_node *node, print_state *state, unsigned int tabs)
1324 {
1325 switch (node->type) {
1326 case nir_cf_node_block:
1327 print_block(nir_cf_node_as_block(node), state, tabs);
1328 break;
1329
1330 case nir_cf_node_if:
1331 print_if(nir_cf_node_as_if(node), state, tabs);
1332 break;
1333
1334 case nir_cf_node_loop:
1335 print_loop(nir_cf_node_as_loop(node), state, tabs);
1336 break;
1337
1338 default:
1339 unreachable("Invalid CFG node type");
1340 }
1341 }
1342
1343 static void
1344 print_function_impl(nir_function_impl *impl, print_state *state)
1345 {
1346 FILE *fp = state->fp;
1347
1348 fprintf(fp, "\nimpl %s ", impl->function->name);
1349
1350 fprintf(fp, "{\n");
1351
1352 nir_foreach_variable(var, &impl->locals) {
1353 fprintf(fp, "\t");
1354 print_var_decl(var, state);
1355 }
1356
1357 foreach_list_typed(nir_register, reg, node, &impl->registers) {
1358 fprintf(fp, "\t");
1359 print_register_decl(reg, state);
1360 }
1361
1362 nir_index_blocks(impl);
1363
1364 foreach_list_typed(nir_cf_node, node, node, &impl->body) {
1365 print_cf_node(node, state, 1);
1366 }
1367
1368 fprintf(fp, "\tblock block_%u:\n}\n\n", impl->end_block->index);
1369 }
1370
1371 static void
1372 print_function(nir_function *function, print_state *state)
1373 {
1374 FILE *fp = state->fp;
1375
1376 fprintf(fp, "decl_function %s (%d params)", function->name,
1377 function->num_params);
1378
1379 fprintf(fp, "\n");
1380
1381 if (function->impl != NULL) {
1382 print_function_impl(function->impl, state);
1383 return;
1384 }
1385 }
1386
1387 static void
1388 init_print_state(print_state *state, nir_shader *shader, FILE *fp)
1389 {
1390 state->fp = fp;
1391 state->shader = shader;
1392 state->ht = _mesa_pointer_hash_table_create(NULL);
1393 state->syms = _mesa_set_create(NULL, _mesa_key_hash_string,
1394 _mesa_key_string_equal);
1395 state->index = 0;
1396 }
1397
1398 static void
1399 destroy_print_state(print_state *state)
1400 {
1401 _mesa_hash_table_destroy(state->ht, NULL);
1402 _mesa_set_destroy(state->syms, NULL);
1403 }
1404
1405 void
1406 nir_print_shader_annotated(nir_shader *shader, FILE *fp,
1407 struct hash_table *annotations)
1408 {
1409 print_state state;
1410 init_print_state(&state, shader, fp);
1411
1412 state.annotations = annotations;
1413
1414 fprintf(fp, "shader: %s\n", gl_shader_stage_name(shader->info.stage));
1415
1416 if (shader->info.name)
1417 fprintf(fp, "name: %s\n", shader->info.name);
1418
1419 if (shader->info.label)
1420 fprintf(fp, "label: %s\n", shader->info.label);
1421
1422 if (gl_shader_stage_is_compute(shader->info.stage)) {
1423 fprintf(fp, "local-size: %u, %u, %u%s\n",
1424 shader->info.cs.local_size[0],
1425 shader->info.cs.local_size[1],
1426 shader->info.cs.local_size[2],
1427 shader->info.cs.local_size_variable ? " (variable)" : "");
1428 fprintf(fp, "shared-size: %u\n", shader->info.cs.shared_size);
1429 }
1430
1431 fprintf(fp, "inputs: %u\n", shader->num_inputs);
1432 fprintf(fp, "outputs: %u\n", shader->num_outputs);
1433 fprintf(fp, "uniforms: %u\n", shader->num_uniforms);
1434 fprintf(fp, "shared: %u\n", shader->num_shared);
1435 if (shader->scratch_size)
1436 fprintf(fp, "scratch: %u\n", shader->scratch_size);
1437
1438 nir_foreach_variable(var, &shader->uniforms) {
1439 print_var_decl(var, &state);
1440 }
1441
1442 nir_foreach_variable(var, &shader->inputs) {
1443 print_var_decl(var, &state);
1444 }
1445
1446 nir_foreach_variable(var, &shader->outputs) {
1447 print_var_decl(var, &state);
1448 }
1449
1450 nir_foreach_variable(var, &shader->shared) {
1451 print_var_decl(var, &state);
1452 }
1453
1454 nir_foreach_variable(var, &shader->globals) {
1455 print_var_decl(var, &state);
1456 }
1457
1458 nir_foreach_variable(var, &shader->system_values) {
1459 print_var_decl(var, &state);
1460 }
1461
1462 foreach_list_typed(nir_function, func, node, &shader->functions) {
1463 print_function(func, &state);
1464 }
1465
1466 destroy_print_state(&state);
1467 }
1468
1469 void
1470 nir_print_shader(nir_shader *shader, FILE *fp)
1471 {
1472 nir_print_shader_annotated(shader, fp, NULL);
1473 fflush(fp);
1474 }
1475
1476 void
1477 nir_print_instr(const nir_instr *instr, FILE *fp)
1478 {
1479 print_state state = {
1480 .fp = fp,
1481 };
1482 print_instr(instr, &state, 0);
1483
1484 }
1485
1486 void
1487 nir_print_deref(const nir_deref_instr *deref, FILE *fp)
1488 {
1489 print_state state = {
1490 .fp = fp,
1491 };
1492 print_deref_link(deref, true, &state);
1493 }