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