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