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