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