nir: Fix printing of individual instructions with io semantics.
[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 case GLSL_TYPE_INTERFACE:
403 for (i = 0; i < c->num_elements; i++) {
404 if (i > 0) fprintf(fp, ", ");
405 fprintf(fp, "{ ");
406 print_constant(c->elements[i], glsl_get_struct_field(type, i), state);
407 fprintf(fp, " }");
408 }
409 break;
410
411 case GLSL_TYPE_ARRAY:
412 for (i = 0; i < c->num_elements; i++) {
413 if (i > 0) fprintf(fp, ", ");
414 fprintf(fp, "{ ");
415 print_constant(c->elements[i], glsl_get_array_element(type), state);
416 fprintf(fp, " }");
417 }
418 break;
419
420 default:
421 unreachable("not reached");
422 }
423 }
424
425 static const char *
426 get_variable_mode_str(nir_variable_mode mode, bool want_local_global_mode)
427 {
428 switch (mode) {
429 case nir_var_shader_in:
430 return "shader_in";
431 case nir_var_shader_out:
432 return "shader_out";
433 case nir_var_uniform:
434 return "uniform";
435 case nir_var_mem_ubo:
436 return "ubo";
437 case nir_var_system_value:
438 return "system";
439 case nir_var_mem_ssbo:
440 return "ssbo";
441 case nir_var_mem_shared:
442 return "shared";
443 case nir_var_mem_global:
444 return "global";
445 case nir_var_shader_temp:
446 return want_local_global_mode ? "shader_temp" : "";
447 case nir_var_function_temp:
448 return want_local_global_mode ? "function_temp" : "";
449 default:
450 return "";
451 }
452 }
453
454 static void
455 print_var_decl(nir_variable *var, print_state *state)
456 {
457 FILE *fp = state->fp;
458
459 fprintf(fp, "decl_var ");
460
461 const char *const cent = (var->data.centroid) ? "centroid " : "";
462 const char *const samp = (var->data.sample) ? "sample " : "";
463 const char *const patch = (var->data.patch) ? "patch " : "";
464 const char *const inv = (var->data.invariant) ? "invariant " : "";
465 const char *const per_view = (var->data.per_view) ? "per_view " : "";
466 fprintf(fp, "%s%s%s%s%s%s %s ",
467 cent, samp, patch, inv, per_view,
468 get_variable_mode_str(var->data.mode, false),
469 glsl_interp_mode_name(var->data.interpolation));
470
471 enum gl_access_qualifier access = var->data.access;
472 const char *const coher = (access & ACCESS_COHERENT) ? "coherent " : "";
473 const char *const volat = (access & ACCESS_VOLATILE) ? "volatile " : "";
474 const char *const restr = (access & ACCESS_RESTRICT) ? "restrict " : "";
475 const char *const ronly = (access & ACCESS_NON_WRITEABLE) ? "readonly " : "";
476 const char *const wonly = (access & ACCESS_NON_READABLE) ? "writeonly " : "";
477 const char *const reorder = (access & ACCESS_CAN_REORDER) ? "reorderable " : "";
478 fprintf(fp, "%s%s%s%s%s%s", coher, volat, restr, ronly, wonly, reorder);
479
480 if (glsl_get_base_type(glsl_without_array(var->type)) == GLSL_TYPE_IMAGE) {
481 fprintf(fp, "%s ", util_format_short_name(var->data.image.format));
482 }
483
484 if (var->data.precision) {
485 const char *precisions[] = {
486 "",
487 "highp",
488 "mediump",
489 "lowp",
490 };
491 fprintf(fp, "%s ", precisions[var->data.precision]);
492 }
493
494 fprintf(fp, "%s %s", glsl_get_type_name(var->type),
495 get_var_name(var, state));
496
497 if (var->data.mode == nir_var_shader_in ||
498 var->data.mode == nir_var_shader_out ||
499 var->data.mode == nir_var_uniform ||
500 var->data.mode == nir_var_mem_ubo ||
501 var->data.mode == nir_var_mem_ssbo) {
502 const char *loc = NULL;
503 char buf[4];
504
505 switch (state->shader->info.stage) {
506 case MESA_SHADER_VERTEX:
507 if (var->data.mode == nir_var_shader_in)
508 loc = gl_vert_attrib_name(var->data.location);
509 else if (var->data.mode == nir_var_shader_out)
510 loc = gl_varying_slot_name(var->data.location);
511 break;
512 case MESA_SHADER_GEOMETRY:
513 if ((var->data.mode == nir_var_shader_in) ||
514 (var->data.mode == nir_var_shader_out))
515 loc = gl_varying_slot_name(var->data.location);
516 break;
517 case MESA_SHADER_FRAGMENT:
518 if (var->data.mode == nir_var_shader_in)
519 loc = gl_varying_slot_name(var->data.location);
520 else if (var->data.mode == nir_var_shader_out)
521 loc = gl_frag_result_name(var->data.location);
522 break;
523 case MESA_SHADER_TESS_CTRL:
524 case MESA_SHADER_TESS_EVAL:
525 case MESA_SHADER_COMPUTE:
526 case MESA_SHADER_KERNEL:
527 default:
528 /* TODO */
529 break;
530 }
531
532 if (!loc) {
533 if (var->data.location == ~0) {
534 loc = "~0";
535 } else {
536 snprintf(buf, sizeof(buf), "%u", var->data.location);
537 loc = buf;
538 }
539 }
540
541 /* For shader I/O vars that have been split to components or packed,
542 * print the fractional location within the input/output.
543 */
544 unsigned int num_components =
545 glsl_get_components(glsl_without_array(var->type));
546 const char *components = NULL;
547 char components_local[18] = {'.' /* the rest is 0-filled */};
548 switch (var->data.mode) {
549 case nir_var_shader_in:
550 case nir_var_shader_out:
551 if (num_components < 16 && num_components != 0) {
552 const char *xyzw = comp_mask_string(num_components);
553 for (int i = 0; i < num_components; i++)
554 components_local[i + 1] = xyzw[i + var->data.location_frac];
555
556 components = components_local;
557 }
558 break;
559 default:
560 break;
561 }
562
563 fprintf(fp, " (%s%s, %u, %u)%s", loc,
564 components ? components : "",
565 var->data.driver_location, var->data.binding,
566 var->data.compact ? " compact" : "");
567 }
568
569 if (var->constant_initializer) {
570 fprintf(fp, " = { ");
571 print_constant(var->constant_initializer, var->type, state);
572 fprintf(fp, " }");
573 }
574 if (var->pointer_initializer)
575 fprintf(fp, " = &%s", get_var_name(var->pointer_initializer, state));
576
577 fprintf(fp, "\n");
578 print_annotation(state, var);
579 }
580
581 static void
582 print_deref_link(const nir_deref_instr *instr, bool whole_chain, print_state *state)
583 {
584 FILE *fp = state->fp;
585
586 if (instr->deref_type == nir_deref_type_var) {
587 fprintf(fp, "%s", get_var_name(instr->var, state));
588 return;
589 } else if (instr->deref_type == nir_deref_type_cast) {
590 fprintf(fp, "(%s *)", glsl_get_type_name(instr->type));
591 print_src(&instr->parent, state);
592 return;
593 }
594
595 assert(instr->parent.is_ssa);
596 nir_deref_instr *parent =
597 nir_instr_as_deref(instr->parent.ssa->parent_instr);
598
599 /* Is the parent we're going to print a bare cast? */
600 const bool is_parent_cast =
601 whole_chain && parent->deref_type == nir_deref_type_cast;
602
603 /* If we're not printing the whole chain, the parent we print will be a SSA
604 * value that represents a pointer. The only deref type that naturally
605 * gives a pointer is a cast.
606 */
607 const bool is_parent_pointer =
608 !whole_chain || parent->deref_type == nir_deref_type_cast;
609
610 /* Struct derefs have a nice syntax that works on pointers, arrays derefs
611 * do not.
612 */
613 const bool need_deref =
614 is_parent_pointer && instr->deref_type != nir_deref_type_struct;
615
616 /* Cast need extra parens and so * dereferences */
617 if (is_parent_cast || need_deref)
618 fprintf(fp, "(");
619
620 if (need_deref)
621 fprintf(fp, "*");
622
623 if (whole_chain) {
624 print_deref_link(parent, whole_chain, state);
625 } else {
626 print_src(&instr->parent, state);
627 }
628
629 if (is_parent_cast || need_deref)
630 fprintf(fp, ")");
631
632 switch (instr->deref_type) {
633 case nir_deref_type_struct:
634 fprintf(fp, "%s%s", is_parent_pointer ? "->" : ".",
635 glsl_get_struct_elem_name(parent->type, instr->strct.index));
636 break;
637
638 case nir_deref_type_array:
639 case nir_deref_type_ptr_as_array: {
640 if (nir_src_is_const(instr->arr.index)) {
641 fprintf(fp, "[%"PRId64"]", nir_src_as_int(instr->arr.index));
642 } else {
643 fprintf(fp, "[");
644 print_src(&instr->arr.index, state);
645 fprintf(fp, "]");
646 }
647 break;
648 }
649
650 case nir_deref_type_array_wildcard:
651 fprintf(fp, "[*]");
652 break;
653
654 default:
655 unreachable("Invalid deref instruction type");
656 }
657 }
658
659 static void
660 print_deref_instr(nir_deref_instr *instr, print_state *state)
661 {
662 FILE *fp = state->fp;
663
664 print_dest(&instr->dest, state);
665
666 switch (instr->deref_type) {
667 case nir_deref_type_var:
668 fprintf(fp, " = deref_var ");
669 break;
670 case nir_deref_type_array:
671 case nir_deref_type_array_wildcard:
672 fprintf(fp, " = deref_array ");
673 break;
674 case nir_deref_type_struct:
675 fprintf(fp, " = deref_struct ");
676 break;
677 case nir_deref_type_cast:
678 fprintf(fp, " = deref_cast ");
679 break;
680 case nir_deref_type_ptr_as_array:
681 fprintf(fp, " = deref_ptr_as_array ");
682 break;
683 default:
684 unreachable("Invalid deref instruction type");
685 }
686
687 /* Only casts naturally return a pointer type */
688 if (instr->deref_type != nir_deref_type_cast)
689 fprintf(fp, "&");
690
691 print_deref_link(instr, false, state);
692
693 fprintf(fp, " (%s %s) ",
694 get_variable_mode_str(instr->mode, true),
695 glsl_get_type_name(instr->type));
696
697 if (instr->deref_type != nir_deref_type_var &&
698 instr->deref_type != nir_deref_type_cast) {
699 /* Print the entire chain as a comment */
700 fprintf(fp, "/* &");
701 print_deref_link(instr, true, state);
702 fprintf(fp, " */");
703 }
704
705 if (instr->deref_type == nir_deref_type_cast) {
706 fprintf(fp, " /* ptr_stride=%u */", instr->cast.ptr_stride);
707 }
708 }
709
710 static const char *
711 vulkan_descriptor_type_name(VkDescriptorType type)
712 {
713 switch (type) {
714 case VK_DESCRIPTOR_TYPE_SAMPLER: return "sampler";
715 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER: return "texture+sampler";
716 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE: return "texture";
717 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE: return "image";
718 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER: return "texture-buffer";
719 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER: return "image-buffer";
720 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER: return "UBO";
721 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER: return "SSBO";
722 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC: return "UBO";
723 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC: return "SSBO";
724 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT: return "input-att";
725 case VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT: return "inline-UBO";
726 default: return "unknown";
727 }
728 }
729
730 static void
731 print_alu_type(nir_alu_type type, print_state *state)
732 {
733 FILE *fp = state->fp;
734 unsigned size = nir_alu_type_get_type_size(type);
735 const char *name;
736
737 switch (nir_alu_type_get_base_type(type)) {
738 case nir_type_int: name = "int"; break;
739 case nir_type_uint: name = "uint"; break;
740 case nir_type_bool: name = "bool"; break;
741 case nir_type_float: name = "float"; break;
742 default: name = "invalid";
743 }
744 if (size)
745 fprintf(fp, "%s%u", name, size);
746 else
747 fprintf(fp, "%s", name);
748 }
749
750 static void
751 print_intrinsic_instr(nir_intrinsic_instr *instr, print_state *state)
752 {
753 const nir_intrinsic_info *info = &nir_intrinsic_infos[instr->intrinsic];
754 unsigned num_srcs = info->num_srcs;
755 FILE *fp = state->fp;
756
757 if (info->has_dest) {
758 print_dest(&instr->dest, state);
759 fprintf(fp, " = ");
760 }
761
762 fprintf(fp, "intrinsic %s (", info->name);
763
764 for (unsigned i = 0; i < num_srcs; i++) {
765 if (i != 0)
766 fprintf(fp, ", ");
767
768 print_src(&instr->src[i], state);
769 }
770
771 fprintf(fp, ") (");
772
773 for (unsigned i = 0; i < info->num_indices; i++) {
774 if (i != 0)
775 fprintf(fp, ", ");
776
777 fprintf(fp, "%d", instr->const_index[i]);
778 }
779
780 fprintf(fp, ")");
781
782 static const char *index_name[NIR_INTRINSIC_NUM_INDEX_FLAGS] = {
783 [NIR_INTRINSIC_BASE] = "base",
784 [NIR_INTRINSIC_WRMASK] = "wrmask",
785 [NIR_INTRINSIC_STREAM_ID] = "stream-id",
786 [NIR_INTRINSIC_UCP_ID] = "ucp-id",
787 [NIR_INTRINSIC_RANGE] = "range",
788 [NIR_INTRINSIC_DESC_SET] = "desc-set",
789 [NIR_INTRINSIC_BINDING] = "binding",
790 [NIR_INTRINSIC_COMPONENT] = "component",
791 [NIR_INTRINSIC_INTERP_MODE] = "interp_mode",
792 [NIR_INTRINSIC_REDUCTION_OP] = "reduction_op",
793 [NIR_INTRINSIC_CLUSTER_SIZE] = "cluster_size",
794 [NIR_INTRINSIC_PARAM_IDX] = "param_idx",
795 [NIR_INTRINSIC_IMAGE_DIM] = "image_dim",
796 [NIR_INTRINSIC_IMAGE_ARRAY] = "image_array",
797 [NIR_INTRINSIC_ACCESS] = "access",
798 [NIR_INTRINSIC_SRC_ACCESS] = "src-access",
799 [NIR_INTRINSIC_DST_ACCESS] = "dst-access",
800 [NIR_INTRINSIC_FORMAT] = "format",
801 [NIR_INTRINSIC_ALIGN_MUL] = "align_mul",
802 [NIR_INTRINSIC_ALIGN_OFFSET] = "align_offset",
803 [NIR_INTRINSIC_DESC_TYPE] = "desc_type",
804 [NIR_INTRINSIC_TYPE] = "type",
805 [NIR_INTRINSIC_SWIZZLE_MASK] = "swizzle_mask",
806 [NIR_INTRINSIC_DRIVER_LOCATION] = "driver_location",
807 [NIR_INTRINSIC_MEMORY_SEMANTICS] = "mem_semantics",
808 [NIR_INTRINSIC_MEMORY_MODES] = "mem_modes",
809 [NIR_INTRINSIC_MEMORY_SCOPE] = "mem_scope",
810 [NIR_INTRINSIC_EXECUTION_SCOPE] = "exec_scope",
811 [NIR_INTRINSIC_IO_SEMANTICS] = "io_semantics",
812 };
813
814 for (unsigned idx = 1; idx < NIR_INTRINSIC_NUM_INDEX_FLAGS; idx++) {
815 if (!info->index_map[idx])
816 continue;
817 fprintf(fp, " /*");
818 switch (idx) {
819 case NIR_INTRINSIC_WRMASK: {
820 /* special case wrmask to show it as a writemask.. */
821 unsigned wrmask = nir_intrinsic_write_mask(instr);
822 fprintf(fp, " wrmask=");
823 for (unsigned i = 0; i < instr->num_components; i++)
824 if ((wrmask >> i) & 1)
825 fprintf(fp, "%c", comp_mask_string(instr->num_components)[i]);
826 break;
827 }
828
829 case NIR_INTRINSIC_REDUCTION_OP: {
830 nir_op reduction_op = nir_intrinsic_reduction_op(instr);
831 fprintf(fp, " reduction_op=%s", nir_op_infos[reduction_op].name);
832 break;
833 }
834
835 case NIR_INTRINSIC_IMAGE_DIM: {
836 static const char *dim_name[] = {
837 [GLSL_SAMPLER_DIM_1D] = "1D",
838 [GLSL_SAMPLER_DIM_2D] = "2D",
839 [GLSL_SAMPLER_DIM_3D] = "3D",
840 [GLSL_SAMPLER_DIM_CUBE] = "Cube",
841 [GLSL_SAMPLER_DIM_RECT] = "Rect",
842 [GLSL_SAMPLER_DIM_BUF] = "Buf",
843 [GLSL_SAMPLER_DIM_MS] = "2D-MSAA",
844 [GLSL_SAMPLER_DIM_SUBPASS] = "Subpass",
845 [GLSL_SAMPLER_DIM_SUBPASS_MS] = "Subpass-MSAA",
846 };
847 enum glsl_sampler_dim dim = nir_intrinsic_image_dim(instr);
848 assert(dim < ARRAY_SIZE(dim_name) && dim_name[dim]);
849 fprintf(fp, " image_dim=%s", dim_name[dim]);
850 break;
851 }
852
853 case NIR_INTRINSIC_IMAGE_ARRAY: {
854 bool array = nir_intrinsic_image_array(instr);
855 fprintf(fp, " image_array=%s", array ? "true" : "false");
856 break;
857 }
858
859 case NIR_INTRINSIC_DESC_TYPE: {
860 VkDescriptorType desc_type = nir_intrinsic_desc_type(instr);
861 fprintf(fp, " desc_type=%s", vulkan_descriptor_type_name(desc_type));
862 break;
863 }
864
865 case NIR_INTRINSIC_TYPE: {
866 fprintf(fp, " type=");
867 print_alu_type(nir_intrinsic_type(instr), state);
868 break;
869 }
870
871 case NIR_INTRINSIC_SWIZZLE_MASK: {
872 fprintf(fp, " swizzle_mask=");
873 unsigned mask = nir_intrinsic_swizzle_mask(instr);
874 if (instr->intrinsic == nir_intrinsic_quad_swizzle_amd) {
875 for (unsigned i = 0; i < 4; i++)
876 fprintf(fp, "%d", (mask >> (i * 2) & 3));
877 } else if (instr->intrinsic == nir_intrinsic_masked_swizzle_amd) {
878 fprintf(fp, "((id & %d) | %d) ^ %d", mask & 0x1F,
879 (mask >> 5) & 0x1F,
880 (mask >> 10) & 0x1F);
881 } else {
882 fprintf(fp, "%d", mask);
883 }
884 break;
885 }
886
887 case NIR_INTRINSIC_MEMORY_SEMANTICS: {
888 nir_memory_semantics semantics = nir_intrinsic_memory_semantics(instr);
889 fprintf(fp, " mem_semantics=");
890 switch (semantics & (NIR_MEMORY_ACQUIRE | NIR_MEMORY_RELEASE)) {
891 case 0: fprintf(fp, "NONE"); break;
892 case NIR_MEMORY_ACQUIRE: fprintf(fp, "ACQ"); break;
893 case NIR_MEMORY_RELEASE: fprintf(fp, "REL"); break;
894 default: fprintf(fp, "ACQ|REL"); break;
895 }
896 if (semantics & (NIR_MEMORY_MAKE_AVAILABLE)) fprintf(fp, "|AVAILABLE");
897 if (semantics & (NIR_MEMORY_MAKE_VISIBLE)) fprintf(fp, "|VISIBLE");
898 break;
899 }
900
901 case NIR_INTRINSIC_MEMORY_MODES: {
902 fprintf(fp, " mem_modes=");
903 unsigned int modes = nir_intrinsic_memory_modes(instr);
904 while (modes) {
905 nir_variable_mode m = u_bit_scan(&modes);
906 fprintf(fp, "%s%s", get_variable_mode_str(1 << m, true), modes ? "|" : "");
907 }
908 break;
909 }
910
911 case NIR_INTRINSIC_EXECUTION_SCOPE:
912 case NIR_INTRINSIC_MEMORY_SCOPE: {
913 fprintf(fp, " %s=", index_name[idx]);
914 nir_scope scope =
915 idx == NIR_INTRINSIC_MEMORY_SCOPE ? nir_intrinsic_memory_scope(instr)
916 : nir_intrinsic_execution_scope(instr);
917 switch (scope) {
918 case NIR_SCOPE_NONE: fprintf(fp, "NONE"); break;
919 case NIR_SCOPE_DEVICE: fprintf(fp, "DEVICE"); break;
920 case NIR_SCOPE_QUEUE_FAMILY: fprintf(fp, "QUEUE_FAMILY"); break;
921 case NIR_SCOPE_WORKGROUP: fprintf(fp, "WORKGROUP"); break;
922 case NIR_SCOPE_SUBGROUP: fprintf(fp, "SUBGROUP"); break;
923 case NIR_SCOPE_INVOCATION: fprintf(fp, "INVOCATION"); break;
924 }
925 break;
926 }
927
928 case NIR_INTRINSIC_IO_SEMANTICS:
929 fprintf(fp, " location=%u slots=%u",
930 nir_intrinsic_io_semantics(instr).location,
931 nir_intrinsic_io_semantics(instr).num_slots);
932 if (state->shader) {
933 if (state->shader->info.stage == MESA_SHADER_FRAGMENT &&
934 instr->intrinsic == nir_intrinsic_store_output &&
935 nir_intrinsic_io_semantics(instr).dual_source_blend_index) {
936 fprintf(fp, " dualsrc=1");
937 }
938 if (state->shader->info.stage == MESA_SHADER_FRAGMENT &&
939 instr->intrinsic == nir_intrinsic_load_output &&
940 nir_intrinsic_io_semantics(instr).fb_fetch_output) {
941 fprintf(fp, " fbfetch=1");
942 }
943 if (state->shader->info.stage == MESA_SHADER_GEOMETRY &&
944 instr->intrinsic == nir_intrinsic_store_output) {
945 unsigned gs_streams = nir_intrinsic_io_semantics(instr).gs_streams;
946 fprintf(fp, " gs_streams(");
947 for (unsigned i = 0; i < 4; i++) {
948 fprintf(fp, "%s%c=%u", i ? " " : "", "xyzw"[i],
949 (gs_streams >> (i * 2)) & 0x3);
950 }
951 fprintf(fp, ")");
952 }
953 }
954 break;
955
956 default: {
957 unsigned off = info->index_map[idx] - 1;
958 assert(index_name[idx]); /* forgot to update index_name table? */
959 fprintf(fp, " %s=%d", index_name[idx], instr->const_index[off]);
960 break;
961 }
962 }
963 fprintf(fp, " */");
964 }
965
966 if (!state->shader)
967 return;
968
969 nir_variable_mode var_mode;
970 switch (instr->intrinsic) {
971 case nir_intrinsic_load_uniform:
972 var_mode = nir_var_uniform;
973 break;
974 case nir_intrinsic_load_input:
975 case nir_intrinsic_load_interpolated_input:
976 case nir_intrinsic_load_per_vertex_input:
977 var_mode = nir_var_shader_in;
978 break;
979 case nir_intrinsic_load_output:
980 case nir_intrinsic_store_output:
981 case nir_intrinsic_store_per_vertex_output:
982 var_mode = nir_var_shader_out;
983 break;
984 default:
985 return;
986 }
987
988 nir_foreach_variable_with_modes(var, state->shader, var_mode) {
989 if ((var->data.driver_location == nir_intrinsic_base(instr)) &&
990 (instr->intrinsic == nir_intrinsic_load_uniform ||
991 (nir_intrinsic_component(instr) >= var->data.location_frac &&
992 nir_intrinsic_component(instr) <
993 (var->data.location_frac + glsl_get_components(var->type)))) &&
994 var->name) {
995 fprintf(fp, "\t/* %s */", var->name);
996 break;
997 }
998 }
999 }
1000
1001 static void
1002 print_tex_instr(nir_tex_instr *instr, print_state *state)
1003 {
1004 FILE *fp = state->fp;
1005
1006 print_dest(&instr->dest, state);
1007
1008 fprintf(fp, " = (");
1009 print_alu_type(instr->dest_type, state);
1010 fprintf(fp, ")");
1011
1012 switch (instr->op) {
1013 case nir_texop_tex:
1014 fprintf(fp, "tex ");
1015 break;
1016 case nir_texop_txb:
1017 fprintf(fp, "txb ");
1018 break;
1019 case nir_texop_txl:
1020 fprintf(fp, "txl ");
1021 break;
1022 case nir_texop_txd:
1023 fprintf(fp, "txd ");
1024 break;
1025 case nir_texop_txf:
1026 fprintf(fp, "txf ");
1027 break;
1028 case nir_texop_txf_ms:
1029 fprintf(fp, "txf_ms ");
1030 break;
1031 case nir_texop_txf_ms_fb:
1032 fprintf(fp, "txf_ms_fb ");
1033 break;
1034 case nir_texop_txf_ms_mcs:
1035 fprintf(fp, "txf_ms_mcs ");
1036 break;
1037 case nir_texop_txs:
1038 fprintf(fp, "txs ");
1039 break;
1040 case nir_texop_lod:
1041 fprintf(fp, "lod ");
1042 break;
1043 case nir_texop_tg4:
1044 fprintf(fp, "tg4 ");
1045 break;
1046 case nir_texop_query_levels:
1047 fprintf(fp, "query_levels ");
1048 break;
1049 case nir_texop_texture_samples:
1050 fprintf(fp, "texture_samples ");
1051 break;
1052 case nir_texop_samples_identical:
1053 fprintf(fp, "samples_identical ");
1054 break;
1055 case nir_texop_tex_prefetch:
1056 fprintf(fp, "tex (pre-dispatchable) ");
1057 break;
1058 case nir_texop_fragment_fetch:
1059 fprintf(fp, "fragment_fetch ");
1060 break;
1061 case nir_texop_fragment_mask_fetch:
1062 fprintf(fp, "fragment_mask_fetch ");
1063 break;
1064 default:
1065 unreachable("Invalid texture operation");
1066 break;
1067 }
1068
1069 bool has_texture_deref = false, has_sampler_deref = false;
1070 for (unsigned i = 0; i < instr->num_srcs; i++) {
1071 if (i > 0) {
1072 fprintf(fp, ", ");
1073 }
1074
1075 print_src(&instr->src[i].src, state);
1076 fprintf(fp, " ");
1077
1078 switch(instr->src[i].src_type) {
1079 case nir_tex_src_coord:
1080 fprintf(fp, "(coord)");
1081 break;
1082 case nir_tex_src_projector:
1083 fprintf(fp, "(projector)");
1084 break;
1085 case nir_tex_src_comparator:
1086 fprintf(fp, "(comparator)");
1087 break;
1088 case nir_tex_src_offset:
1089 fprintf(fp, "(offset)");
1090 break;
1091 case nir_tex_src_bias:
1092 fprintf(fp, "(bias)");
1093 break;
1094 case nir_tex_src_lod:
1095 fprintf(fp, "(lod)");
1096 break;
1097 case nir_tex_src_min_lod:
1098 fprintf(fp, "(min_lod)");
1099 break;
1100 case nir_tex_src_ms_index:
1101 fprintf(fp, "(ms_index)");
1102 break;
1103 case nir_tex_src_ms_mcs:
1104 fprintf(fp, "(ms_mcs)");
1105 break;
1106 case nir_tex_src_ddx:
1107 fprintf(fp, "(ddx)");
1108 break;
1109 case nir_tex_src_ddy:
1110 fprintf(fp, "(ddy)");
1111 break;
1112 case nir_tex_src_texture_deref:
1113 has_texture_deref = true;
1114 fprintf(fp, "(texture_deref)");
1115 break;
1116 case nir_tex_src_sampler_deref:
1117 has_sampler_deref = true;
1118 fprintf(fp, "(sampler_deref)");
1119 break;
1120 case nir_tex_src_texture_offset:
1121 fprintf(fp, "(texture_offset)");
1122 break;
1123 case nir_tex_src_sampler_offset:
1124 fprintf(fp, "(sampler_offset)");
1125 break;
1126 case nir_tex_src_texture_handle:
1127 fprintf(fp, "(texture_handle)");
1128 break;
1129 case nir_tex_src_sampler_handle:
1130 fprintf(fp, "(sampler_handle)");
1131 break;
1132 case nir_tex_src_plane:
1133 fprintf(fp, "(plane)");
1134 break;
1135
1136 default:
1137 unreachable("Invalid texture source type");
1138 break;
1139 }
1140 }
1141
1142 if (instr->op == nir_texop_tg4) {
1143 fprintf(fp, ", %u (gather_component)", instr->component);
1144 }
1145
1146 if (nir_tex_instr_has_explicit_tg4_offsets(instr)) {
1147 fprintf(fp, ", { (%i, %i)", instr->tg4_offsets[0][0], instr->tg4_offsets[0][1]);
1148 for (unsigned i = 1; i < 4; ++i)
1149 fprintf(fp, ", (%i, %i)", instr->tg4_offsets[i][0],
1150 instr->tg4_offsets[i][1]);
1151 fprintf(fp, " } (offsets)");
1152 }
1153
1154 if (instr->op != nir_texop_txf_ms_fb) {
1155 if (!has_texture_deref) {
1156 fprintf(fp, ", %u (texture)", instr->texture_index);
1157 }
1158
1159 if (!has_sampler_deref) {
1160 fprintf(fp, ", %u (sampler)", instr->sampler_index);
1161 }
1162 }
1163
1164 if (instr->texture_non_uniform) {
1165 fprintf(fp, ", texture non-uniform");
1166 }
1167
1168 if (instr->sampler_non_uniform) {
1169 fprintf(fp, ", sampler non-uniform");
1170 }
1171 }
1172
1173 static void
1174 print_call_instr(nir_call_instr *instr, print_state *state)
1175 {
1176 FILE *fp = state->fp;
1177
1178 fprintf(fp, "call %s ", instr->callee->name);
1179
1180 for (unsigned i = 0; i < instr->num_params; i++) {
1181 if (i != 0)
1182 fprintf(fp, ", ");
1183
1184 print_src(&instr->params[i], state);
1185 }
1186 }
1187
1188 static void
1189 print_load_const_instr(nir_load_const_instr *instr, print_state *state)
1190 {
1191 FILE *fp = state->fp;
1192
1193 print_ssa_def(&instr->def, state);
1194
1195 fprintf(fp, " = load_const (");
1196
1197 for (unsigned i = 0; i < instr->def.num_components; i++) {
1198 if (i != 0)
1199 fprintf(fp, ", ");
1200
1201 /*
1202 * we don't really know the type of the constant (if it will be used as a
1203 * float or an int), so just print the raw constant in hex for fidelity
1204 * and then print the float in a comment for readability.
1205 */
1206
1207 switch (instr->def.bit_size) {
1208 case 64:
1209 fprintf(fp, "0x%16" PRIx64 " /* %f */", instr->value[i].u64,
1210 instr->value[i].f64);
1211 break;
1212 case 32:
1213 fprintf(fp, "0x%08x /* %f */", instr->value[i].u32, instr->value[i].f32);
1214 break;
1215 case 16:
1216 fprintf(fp, "0x%04x /* %f */", instr->value[i].u16,
1217 _mesa_half_to_float(instr->value[i].u16));
1218 break;
1219 case 8:
1220 fprintf(fp, "0x%02x", instr->value[i].u8);
1221 break;
1222 case 1:
1223 fprintf(fp, "%s", instr->value[i].b ? "true" : "false");
1224 break;
1225 }
1226 }
1227
1228 fprintf(fp, ")");
1229 }
1230
1231 static void
1232 print_jump_instr(nir_jump_instr *instr, print_state *state)
1233 {
1234 FILE *fp = state->fp;
1235
1236 switch (instr->type) {
1237 case nir_jump_break:
1238 fprintf(fp, "break");
1239 break;
1240
1241 case nir_jump_continue:
1242 fprintf(fp, "continue");
1243 break;
1244
1245 case nir_jump_return:
1246 fprintf(fp, "return");
1247 break;
1248
1249 case nir_jump_goto:
1250 fprintf(fp, "goto block_%u",
1251 instr->target ? instr->target->index : -1);
1252 break;
1253
1254 case nir_jump_goto_if:
1255 fprintf(fp, "goto block_%u if ",
1256 instr->target ? instr->target->index : -1);
1257 print_src(&instr->condition, state);
1258 fprintf(fp, " else block_%u",
1259 instr->else_target ? instr->else_target->index : -1);
1260 break;
1261 }
1262 }
1263
1264 static void
1265 print_ssa_undef_instr(nir_ssa_undef_instr* instr, print_state *state)
1266 {
1267 FILE *fp = state->fp;
1268 print_ssa_def(&instr->def, state);
1269 fprintf(fp, " = undefined");
1270 }
1271
1272 static void
1273 print_phi_instr(nir_phi_instr *instr, print_state *state)
1274 {
1275 FILE *fp = state->fp;
1276 print_dest(&instr->dest, state);
1277 fprintf(fp, " = phi ");
1278 nir_foreach_phi_src(src, instr) {
1279 if (&src->node != exec_list_get_head(&instr->srcs))
1280 fprintf(fp, ", ");
1281
1282 fprintf(fp, "block_%u: ", src->pred->index);
1283 print_src(&src->src, state);
1284 }
1285 }
1286
1287 static void
1288 print_parallel_copy_instr(nir_parallel_copy_instr *instr, print_state *state)
1289 {
1290 FILE *fp = state->fp;
1291 nir_foreach_parallel_copy_entry(entry, instr) {
1292 if (&entry->node != exec_list_get_head(&instr->entries))
1293 fprintf(fp, "; ");
1294
1295 print_dest(&entry->dest, state);
1296 fprintf(fp, " = ");
1297 print_src(&entry->src, state);
1298 }
1299 }
1300
1301 static void
1302 print_instr(const nir_instr *instr, print_state *state, unsigned tabs)
1303 {
1304 FILE *fp = state->fp;
1305 print_tabs(tabs, fp);
1306
1307 switch (instr->type) {
1308 case nir_instr_type_alu:
1309 print_alu_instr(nir_instr_as_alu(instr), state);
1310 break;
1311
1312 case nir_instr_type_deref:
1313 print_deref_instr(nir_instr_as_deref(instr), state);
1314 break;
1315
1316 case nir_instr_type_call:
1317 print_call_instr(nir_instr_as_call(instr), state);
1318 break;
1319
1320 case nir_instr_type_intrinsic:
1321 print_intrinsic_instr(nir_instr_as_intrinsic(instr), state);
1322 break;
1323
1324 case nir_instr_type_tex:
1325 print_tex_instr(nir_instr_as_tex(instr), state);
1326 break;
1327
1328 case nir_instr_type_load_const:
1329 print_load_const_instr(nir_instr_as_load_const(instr), state);
1330 break;
1331
1332 case nir_instr_type_jump:
1333 print_jump_instr(nir_instr_as_jump(instr), state);
1334 break;
1335
1336 case nir_instr_type_ssa_undef:
1337 print_ssa_undef_instr(nir_instr_as_ssa_undef(instr), state);
1338 break;
1339
1340 case nir_instr_type_phi:
1341 print_phi_instr(nir_instr_as_phi(instr), state);
1342 break;
1343
1344 case nir_instr_type_parallel_copy:
1345 print_parallel_copy_instr(nir_instr_as_parallel_copy(instr), state);
1346 break;
1347
1348 default:
1349 unreachable("Invalid instruction type");
1350 break;
1351 }
1352 }
1353
1354 static int
1355 compare_block_index(const void *p1, const void *p2)
1356 {
1357 const nir_block *block1 = *((const nir_block **) p1);
1358 const nir_block *block2 = *((const nir_block **) p2);
1359
1360 return (int) block1->index - (int) block2->index;
1361 }
1362
1363 static void print_cf_node(nir_cf_node *node, print_state *state,
1364 unsigned tabs);
1365
1366 static void
1367 print_block(nir_block *block, print_state *state, unsigned tabs)
1368 {
1369 FILE *fp = state->fp;
1370
1371 print_tabs(tabs, fp);
1372 fprintf(fp, "block block_%u:\n", block->index);
1373
1374 /* sort the predecessors by index so we consistently print the same thing */
1375
1376 nir_block **preds =
1377 malloc(block->predecessors->entries * sizeof(nir_block *));
1378
1379 unsigned i = 0;
1380 set_foreach(block->predecessors, entry) {
1381 preds[i++] = (nir_block *) entry->key;
1382 }
1383
1384 qsort(preds, block->predecessors->entries, sizeof(nir_block *),
1385 compare_block_index);
1386
1387 print_tabs(tabs, fp);
1388 fprintf(fp, "/* preds: ");
1389 for (unsigned i = 0; i < block->predecessors->entries; i++) {
1390 fprintf(fp, "block_%u ", preds[i]->index);
1391 }
1392 fprintf(fp, "*/\n");
1393
1394 free(preds);
1395
1396 nir_foreach_instr(instr, block) {
1397 print_instr(instr, state, tabs);
1398 fprintf(fp, "\n");
1399 print_annotation(state, instr);
1400 }
1401
1402 print_tabs(tabs, fp);
1403 fprintf(fp, "/* succs: ");
1404 for (unsigned i = 0; i < 2; i++)
1405 if (block->successors[i]) {
1406 fprintf(fp, "block_%u ", block->successors[i]->index);
1407 }
1408 fprintf(fp, "*/\n");
1409 }
1410
1411 static void
1412 print_if(nir_if *if_stmt, print_state *state, unsigned tabs)
1413 {
1414 FILE *fp = state->fp;
1415
1416 print_tabs(tabs, fp);
1417 fprintf(fp, "if ");
1418 print_src(&if_stmt->condition, state);
1419 fprintf(fp, " {\n");
1420 foreach_list_typed(nir_cf_node, node, node, &if_stmt->then_list) {
1421 print_cf_node(node, state, tabs + 1);
1422 }
1423 print_tabs(tabs, fp);
1424 fprintf(fp, "} else {\n");
1425 foreach_list_typed(nir_cf_node, node, node, &if_stmt->else_list) {
1426 print_cf_node(node, state, tabs + 1);
1427 }
1428 print_tabs(tabs, fp);
1429 fprintf(fp, "}\n");
1430 }
1431
1432 static void
1433 print_loop(nir_loop *loop, print_state *state, unsigned tabs)
1434 {
1435 FILE *fp = state->fp;
1436
1437 print_tabs(tabs, fp);
1438 fprintf(fp, "loop {\n");
1439 foreach_list_typed(nir_cf_node, node, node, &loop->body) {
1440 print_cf_node(node, state, tabs + 1);
1441 }
1442 print_tabs(tabs, fp);
1443 fprintf(fp, "}\n");
1444 }
1445
1446 static void
1447 print_cf_node(nir_cf_node *node, print_state *state, unsigned int tabs)
1448 {
1449 switch (node->type) {
1450 case nir_cf_node_block:
1451 print_block(nir_cf_node_as_block(node), state, tabs);
1452 break;
1453
1454 case nir_cf_node_if:
1455 print_if(nir_cf_node_as_if(node), state, tabs);
1456 break;
1457
1458 case nir_cf_node_loop:
1459 print_loop(nir_cf_node_as_loop(node), state, tabs);
1460 break;
1461
1462 default:
1463 unreachable("Invalid CFG node type");
1464 }
1465 }
1466
1467 static void
1468 print_function_impl(nir_function_impl *impl, print_state *state)
1469 {
1470 FILE *fp = state->fp;
1471
1472 fprintf(fp, "\nimpl %s ", impl->function->name);
1473
1474 fprintf(fp, "{\n");
1475
1476 nir_foreach_function_temp_variable(var, impl) {
1477 fprintf(fp, "\t");
1478 print_var_decl(var, state);
1479 }
1480
1481 foreach_list_typed(nir_register, reg, node, &impl->registers) {
1482 fprintf(fp, "\t");
1483 print_register_decl(reg, state);
1484 }
1485
1486 nir_index_blocks(impl);
1487
1488 foreach_list_typed(nir_cf_node, node, node, &impl->body) {
1489 print_cf_node(node, state, 1);
1490 }
1491
1492 fprintf(fp, "\tblock block_%u:\n}\n\n", impl->end_block->index);
1493 }
1494
1495 static void
1496 print_function(nir_function *function, print_state *state)
1497 {
1498 FILE *fp = state->fp;
1499
1500 fprintf(fp, "decl_function %s (%d params)", function->name,
1501 function->num_params);
1502
1503 fprintf(fp, "\n");
1504
1505 if (function->impl != NULL) {
1506 print_function_impl(function->impl, state);
1507 return;
1508 }
1509 }
1510
1511 static void
1512 init_print_state(print_state *state, nir_shader *shader, FILE *fp)
1513 {
1514 state->fp = fp;
1515 state->shader = shader;
1516 state->ht = _mesa_pointer_hash_table_create(NULL);
1517 state->syms = _mesa_set_create(NULL, _mesa_hash_string,
1518 _mesa_key_string_equal);
1519 state->index = 0;
1520 }
1521
1522 static void
1523 destroy_print_state(print_state *state)
1524 {
1525 _mesa_hash_table_destroy(state->ht, NULL);
1526 _mesa_set_destroy(state->syms, NULL);
1527 }
1528
1529 void
1530 nir_print_shader_annotated(nir_shader *shader, FILE *fp,
1531 struct hash_table *annotations)
1532 {
1533 print_state state;
1534 init_print_state(&state, shader, fp);
1535
1536 state.annotations = annotations;
1537
1538 fprintf(fp, "shader: %s\n", gl_shader_stage_name(shader->info.stage));
1539
1540 if (shader->info.name)
1541 fprintf(fp, "name: %s\n", shader->info.name);
1542
1543 if (shader->info.label)
1544 fprintf(fp, "label: %s\n", shader->info.label);
1545
1546 if (gl_shader_stage_is_compute(shader->info.stage)) {
1547 fprintf(fp, "local-size: %u, %u, %u%s\n",
1548 shader->info.cs.local_size[0],
1549 shader->info.cs.local_size[1],
1550 shader->info.cs.local_size[2],
1551 shader->info.cs.local_size_variable ? " (variable)" : "");
1552 fprintf(fp, "shared-size: %u\n", shader->info.cs.shared_size);
1553 }
1554
1555 fprintf(fp, "inputs: %u\n", shader->num_inputs);
1556 fprintf(fp, "outputs: %u\n", shader->num_outputs);
1557 fprintf(fp, "uniforms: %u\n", shader->num_uniforms);
1558 if (shader->info.num_ubos)
1559 fprintf(fp, "ubos: %u\n", shader->info.num_ubos);
1560 fprintf(fp, "shared: %u\n", shader->num_shared);
1561 if (shader->scratch_size)
1562 fprintf(fp, "scratch: %u\n", shader->scratch_size);
1563 if (shader->constant_data_size)
1564 fprintf(fp, "constants: %u\n", shader->constant_data_size);
1565
1566 nir_foreach_variable_in_shader(var, shader)
1567 print_var_decl(var, &state);
1568
1569 foreach_list_typed(nir_function, func, node, &shader->functions) {
1570 print_function(func, &state);
1571 }
1572
1573 destroy_print_state(&state);
1574 }
1575
1576 void
1577 nir_print_shader(nir_shader *shader, FILE *fp)
1578 {
1579 nir_print_shader_annotated(shader, fp, NULL);
1580 fflush(fp);
1581 }
1582
1583 void
1584 nir_print_instr(const nir_instr *instr, FILE *fp)
1585 {
1586 print_state state = {
1587 .fp = fp,
1588 };
1589 print_instr(instr, &state, 0);
1590
1591 }
1592
1593 void
1594 nir_print_deref(const nir_deref_instr *deref, FILE *fp)
1595 {
1596 print_state state = {
1597 .fp = fp,
1598 };
1599 print_deref_link(deref, true, &state);
1600 }