nir: print var binding in dumps.
[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 <stdio.h>
31 #include <stdlib.h>
32 #include <inttypes.h> /* for PRIx64 macro */
33
34 #if defined(_WIN32) && !defined(snprintf)
35 #define snprintf _snprintf
36 #endif
37
38 static void
39 print_tabs(unsigned num_tabs, FILE *fp)
40 {
41 for (unsigned i = 0; i < num_tabs; i++)
42 fprintf(fp, "\t");
43 }
44
45 typedef struct {
46 FILE *fp;
47 nir_shader *shader;
48 /** map from nir_variable -> printable name */
49 struct hash_table *ht;
50
51 /** set of names used so far for nir_variables */
52 struct set *syms;
53
54 /* an index used to make new non-conflicting names */
55 unsigned index;
56
57 /**
58 * Optional table of annotations mapping nir object
59 * (such as instr or var) to message to print.
60 */
61 struct hash_table *annotations;
62 } print_state;
63
64 static void
65 print_annotation(print_state *state, void *obj)
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(stderr, "%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 if (reg->is_global)
87 fprintf(fp, "gr%u", reg->index);
88 else
89 fprintf(fp, "r%u", reg->index);
90 }
91
92 static const char *sizes[] = { "error", "vec1", "vec2", "vec3", "vec4" };
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 if (reg->is_packed)
100 fprintf(fp, "(packed) ");
101 print_register(reg, state);
102 if (reg->num_array_elems != 0)
103 fprintf(fp, "[%u]", reg->num_array_elems);
104 fprintf(fp, "\n");
105 }
106
107 static void
108 print_ssa_def(nir_ssa_def *def, print_state *state)
109 {
110 FILE *fp = state->fp;
111 if (def->name != NULL)
112 fprintf(fp, "/* %s */ ", def->name);
113 fprintf(fp, "%s %u ssa_%u", sizes[def->num_components], def->bit_size,
114 def->index);
115 }
116
117 static void
118 print_ssa_use(nir_ssa_def *def, print_state *state)
119 {
120 FILE *fp = state->fp;
121 if (def->name != NULL)
122 fprintf(fp, "/* %s */ ", def->name);
123 fprintf(fp, "ssa_%u", def->index);
124 }
125
126 static void print_src(nir_src *src, print_state *state);
127
128 static void
129 print_reg_src(nir_reg_src *src, print_state *state)
130 {
131 FILE *fp = state->fp;
132 print_register(src->reg, state);
133 if (src->reg->num_array_elems != 0) {
134 fprintf(fp, "[%u", src->base_offset);
135 if (src->indirect != NULL) {
136 fprintf(fp, " + ");
137 print_src(src->indirect, state);
138 }
139 fprintf(fp, "]");
140 }
141 }
142
143 static void
144 print_reg_dest(nir_reg_dest *dest, print_state *state)
145 {
146 FILE *fp = state->fp;
147 print_register(dest->reg, state);
148 if (dest->reg->num_array_elems != 0) {
149 fprintf(fp, "[%u", dest->base_offset);
150 if (dest->indirect != NULL) {
151 fprintf(fp, " + ");
152 print_src(dest->indirect, state);
153 }
154 fprintf(fp, "]");
155 }
156 }
157
158 static void
159 print_src(nir_src *src, print_state *state)
160 {
161 if (src->is_ssa)
162 print_ssa_use(src->ssa, state);
163 else
164 print_reg_src(&src->reg, state);
165 }
166
167 static void
168 print_dest(nir_dest *dest, print_state *state)
169 {
170 if (dest->is_ssa)
171 print_ssa_def(&dest->ssa, state);
172 else
173 print_reg_dest(&dest->reg, state);
174 }
175
176 static void
177 print_alu_src(nir_alu_instr *instr, unsigned src, print_state *state)
178 {
179 FILE *fp = state->fp;
180
181 if (instr->src[src].negate)
182 fprintf(fp, "-");
183 if (instr->src[src].abs)
184 fprintf(fp, "abs(");
185
186 print_src(&instr->src[src].src, state);
187
188 bool print_swizzle = false;
189 unsigned used_channels = 0;
190
191 for (unsigned i = 0; i < 4; i++) {
192 if (!nir_alu_instr_channel_used(instr, src, i))
193 continue;
194
195 used_channels++;
196
197 if (instr->src[src].swizzle[i] != i) {
198 print_swizzle = true;
199 break;
200 }
201 }
202
203 unsigned live_channels = instr->src[src].src.is_ssa
204 ? instr->src[src].src.ssa->num_components
205 : instr->src[src].src.reg.reg->num_components;
206
207 if (print_swizzle || used_channels != live_channels) {
208 fprintf(fp, ".");
209 for (unsigned i = 0; i < 4; i++) {
210 if (!nir_alu_instr_channel_used(instr, src, i))
211 continue;
212
213 fprintf(fp, "%c", "xyzw"[instr->src[src].swizzle[i]]);
214 }
215 }
216
217 if (instr->src[src].abs)
218 fprintf(fp, ")");
219 }
220
221 static void
222 print_alu_dest(nir_alu_dest *dest, print_state *state)
223 {
224 FILE *fp = state->fp;
225 /* we're going to print the saturate modifier later, after the opcode */
226
227 print_dest(&dest->dest, state);
228
229 if (!dest->dest.is_ssa &&
230 dest->write_mask != (1 << dest->dest.reg.reg->num_components) - 1) {
231 fprintf(fp, ".");
232 for (unsigned i = 0; i < 4; i++)
233 if ((dest->write_mask >> i) & 1)
234 fprintf(fp, "%c", "xyzw"[i]);
235 }
236 }
237
238 static void
239 print_alu_instr(nir_alu_instr *instr, print_state *state)
240 {
241 FILE *fp = state->fp;
242
243 print_alu_dest(&instr->dest, state);
244
245 fprintf(fp, " = %s", nir_op_infos[instr->op].name);
246 if (instr->exact)
247 fprintf(fp, "!");
248 if (instr->dest.saturate)
249 fprintf(fp, ".sat");
250 fprintf(fp, " ");
251
252 for (unsigned i = 0; i < nir_op_infos[instr->op].num_inputs; i++) {
253 if (i != 0)
254 fprintf(fp, ", ");
255
256 print_alu_src(instr, i, state);
257 }
258 }
259
260 static const char *
261 get_var_name(nir_variable *var, print_state *state)
262 {
263 if (state->ht == NULL)
264 return var->name;
265
266 assert(state->syms);
267
268 struct hash_entry *entry = _mesa_hash_table_search(state->ht, var);
269 if (entry)
270 return entry->data;
271
272 char *name;
273 if (var->name == NULL) {
274 name = ralloc_asprintf(state->syms, "@%u", state->index++);
275 } else {
276 struct set_entry *set_entry = _mesa_set_search(state->syms, var->name);
277 if (set_entry != NULL) {
278 /* we have a collision with another name, append an @ + a unique
279 * index */
280 name = ralloc_asprintf(state->syms, "%s@%u", var->name,
281 state->index++);
282 } else {
283 /* Mark this one as seen */
284 _mesa_set_add(state->syms, var->name);
285 name = var->name;
286 }
287 }
288
289 _mesa_hash_table_insert(state->ht, var, name);
290
291 return name;
292 }
293
294 static void
295 print_constant(nir_constant *c, const struct glsl_type *type, print_state *state)
296 {
297 FILE *fp = state->fp;
298 unsigned total_elems = glsl_get_components(type);
299 unsigned i;
300
301 switch (glsl_get_base_type(type)) {
302 case GLSL_TYPE_UINT:
303 case GLSL_TYPE_INT:
304 case GLSL_TYPE_BOOL:
305 for (i = 0; i < total_elems; i++) {
306 if (i > 0) fprintf(fp, ", ");
307 fprintf(fp, "0x%08x", c->value.u[i]);
308 }
309 break;
310
311 case GLSL_TYPE_FLOAT:
312 for (i = 0; i < total_elems; i++) {
313 if (i > 0) fprintf(fp, ", ");
314 fprintf(fp, "%f", c->value.f[i]);
315 }
316 break;
317
318 case GLSL_TYPE_DOUBLE:
319 for (i = 0; i < total_elems; i++) {
320 if (i > 0) fprintf(fp, ", ");
321 fprintf(fp, "%f", c->value.d[i]);
322 }
323 break;
324
325 case GLSL_TYPE_STRUCT:
326 for (i = 0; i < c->num_elements; i++) {
327 if (i > 0) fprintf(fp, ", ");
328 fprintf(fp, "{ ");
329 print_constant(c->elements[i], glsl_get_struct_field(type, i), state);
330 fprintf(fp, " }");
331 }
332 break;
333
334 case GLSL_TYPE_ARRAY:
335 for (i = 0; i < c->num_elements; i++) {
336 if (i > 0) fprintf(fp, ", ");
337 fprintf(fp, "{ ");
338 print_constant(c->elements[i], glsl_get_array_element(type), state);
339 fprintf(fp, " }");
340 }
341 break;
342
343 default:
344 unreachable("not reached");
345 }
346 }
347
348 static const char *
349 get_variable_mode_str(nir_variable_mode mode)
350 {
351 switch (mode) {
352 case nir_var_shader_in:
353 return "shader_in";
354 case nir_var_shader_out:
355 return "shader_out";
356 case nir_var_uniform:
357 return "uniform";
358 case nir_var_shader_storage:
359 return "shader_storage";
360 case nir_var_system_value:
361 return "system";
362 case nir_var_shared:
363 return "shared";
364 case nir_var_param:
365 case nir_var_global:
366 case nir_var_local:
367 default:
368 return "";
369 }
370 }
371
372 static void
373 print_var_decl(nir_variable *var, print_state *state)
374 {
375 FILE *fp = state->fp;
376
377 fprintf(fp, "decl_var ");
378
379 const char *const cent = (var->data.centroid) ? "centroid " : "";
380 const char *const samp = (var->data.sample) ? "sample " : "";
381 const char *const patch = (var->data.patch) ? "patch " : "";
382 const char *const inv = (var->data.invariant) ? "invariant " : "";
383 fprintf(fp, "%s%s%s%s%s %s ",
384 cent, samp, patch, inv, get_variable_mode_str(var->data.mode),
385 glsl_interp_mode_name(var->data.interpolation));
386
387 const char *const coher = (var->data.image.coherent) ? "coherent " : "";
388 const char *const volat = (var->data.image._volatile) ? "volatile " : "";
389 const char *const restr = (var->data.image.restrict_flag) ? "restrict " : "";
390 const char *const ronly = (var->data.image.read_only) ? "readonly " : "";
391 const char *const wonly = (var->data.image.write_only) ? "writeonly " : "";
392 fprintf(fp, "%s%s%s%s%s", coher, volat, restr, ronly, wonly);
393
394 fprintf(fp, "%s %s", glsl_get_type_name(var->type),
395 get_var_name(var, state));
396
397 if (var->data.mode == nir_var_shader_in ||
398 var->data.mode == nir_var_shader_out ||
399 var->data.mode == nir_var_uniform ||
400 var->data.mode == nir_var_shader_storage) {
401 const char *loc = NULL;
402 char buf[4];
403
404 switch (state->shader->stage) {
405 case MESA_SHADER_VERTEX:
406 if (var->data.mode == nir_var_shader_in)
407 loc = gl_vert_attrib_name(var->data.location);
408 else if (var->data.mode == nir_var_shader_out)
409 loc = gl_varying_slot_name(var->data.location);
410 break;
411 case MESA_SHADER_GEOMETRY:
412 if ((var->data.mode == nir_var_shader_in) ||
413 (var->data.mode == nir_var_shader_out))
414 loc = gl_varying_slot_name(var->data.location);
415 break;
416 case MESA_SHADER_FRAGMENT:
417 if (var->data.mode == nir_var_shader_in)
418 loc = gl_varying_slot_name(var->data.location);
419 else if (var->data.mode == nir_var_shader_out)
420 loc = gl_frag_result_name(var->data.location);
421 break;
422 case MESA_SHADER_TESS_CTRL:
423 case MESA_SHADER_TESS_EVAL:
424 case MESA_SHADER_COMPUTE:
425 default:
426 /* TODO */
427 break;
428 }
429
430 if (!loc) {
431 snprintf(buf, sizeof(buf), "%u", var->data.location);
432 loc = buf;
433 }
434
435 fprintf(fp, " (%s, %u, %u)%s", loc, var->data.driver_location, var->data.binding,
436 var->data.compact ? " compact" : "");
437 }
438
439 if (var->constant_initializer) {
440 fprintf(fp, " = { ");
441 print_constant(var->constant_initializer, var->type, state);
442 fprintf(fp, " }");
443 }
444
445 fprintf(fp, "\n");
446 print_annotation(state, var);
447 }
448
449 static void
450 print_var(nir_variable *var, print_state *state)
451 {
452 FILE *fp = state->fp;
453 fprintf(fp, "%s", get_var_name(var, state));
454 }
455
456 static void
457 print_arg(nir_variable *var, print_state *state)
458 {
459 FILE *fp = state->fp;
460 fprintf(fp, "%s %s", glsl_get_type_name(var->type),
461 get_var_name(var, state));
462 }
463
464 static void
465 print_deref_var(nir_deref_var *deref, print_state *state)
466 {
467 print_var(deref->var, state);
468 }
469
470 static void
471 print_deref_array(nir_deref_array *deref, print_state *state)
472 {
473 FILE *fp = state->fp;
474 fprintf(fp, "[");
475 switch (deref->deref_array_type) {
476 case nir_deref_array_type_direct:
477 fprintf(fp, "%u", deref->base_offset);
478 break;
479 case nir_deref_array_type_indirect:
480 if (deref->base_offset != 0)
481 fprintf(fp, "%u + ", deref->base_offset);
482 print_src(&deref->indirect, state);
483 break;
484 case nir_deref_array_type_wildcard:
485 fprintf(fp, "*");
486 break;
487 }
488 fprintf(fp, "]");
489 }
490
491 static void
492 print_deref_struct(nir_deref_struct *deref, const struct glsl_type *parent_type,
493 print_state *state)
494 {
495 FILE *fp = state->fp;
496 fprintf(fp, ".%s", glsl_get_struct_elem_name(parent_type, deref->index));
497 }
498
499 static void
500 print_deref(nir_deref_var *deref, print_state *state)
501 {
502 nir_deref *tail = &deref->deref;
503 nir_deref *pretail = NULL;
504 while (tail != NULL) {
505 switch (tail->deref_type) {
506 case nir_deref_type_var:
507 assert(pretail == NULL);
508 assert(tail == &deref->deref);
509 print_deref_var(deref, state);
510 break;
511
512 case nir_deref_type_array:
513 assert(pretail != NULL);
514 print_deref_array(nir_deref_as_array(tail), state);
515 break;
516
517 case nir_deref_type_struct:
518 assert(pretail != NULL);
519 print_deref_struct(nir_deref_as_struct(tail),
520 pretail->type, state);
521 break;
522
523 default:
524 unreachable("Invalid deref type");
525 }
526
527 pretail = tail;
528 tail = pretail->child;
529 }
530 }
531
532 static void
533 print_intrinsic_instr(nir_intrinsic_instr *instr, print_state *state)
534 {
535 const nir_intrinsic_info *info = &nir_intrinsic_infos[instr->intrinsic];
536 unsigned num_srcs = info->num_srcs;
537 FILE *fp = state->fp;
538
539 if (info->has_dest) {
540 print_dest(&instr->dest, state);
541 fprintf(fp, " = ");
542 }
543
544 fprintf(fp, "intrinsic %s (", info->name);
545
546 for (unsigned i = 0; i < num_srcs; i++) {
547 if (i != 0)
548 fprintf(fp, ", ");
549
550 print_src(&instr->src[i], state);
551 }
552
553 fprintf(fp, ") (");
554
555 for (unsigned i = 0; i < info->num_variables; i++) {
556 if (i != 0)
557 fprintf(fp, ", ");
558
559 print_deref(instr->variables[i], state);
560 }
561
562 fprintf(fp, ") (");
563
564 for (unsigned i = 0; i < info->num_indices; i++) {
565 if (i != 0)
566 fprintf(fp, ", ");
567
568 fprintf(fp, "%d", instr->const_index[i]);
569 }
570
571 fprintf(fp, ")");
572
573 static const char *index_name[NIR_INTRINSIC_NUM_INDEX_FLAGS] = {
574 [NIR_INTRINSIC_BASE] = "base",
575 [NIR_INTRINSIC_WRMASK] = "wrmask",
576 [NIR_INTRINSIC_STREAM_ID] = "stream-id",
577 [NIR_INTRINSIC_UCP_ID] = "ucp-id",
578 [NIR_INTRINSIC_RANGE] = "range",
579 [NIR_INTRINSIC_DESC_SET] = "desc-set",
580 [NIR_INTRINSIC_BINDING] = "binding",
581 [NIR_INTRINSIC_COMPONENT] = "component",
582 [NIR_INTRINSIC_INTERP_MODE] = "interp_mode",
583 };
584 for (unsigned idx = 1; idx < NIR_INTRINSIC_NUM_INDEX_FLAGS; idx++) {
585 if (!info->index_map[idx])
586 continue;
587 fprintf(fp, " /*");
588 if (idx == NIR_INTRINSIC_WRMASK) {
589 /* special case wrmask to show it as a writemask.. */
590 unsigned wrmask = nir_intrinsic_write_mask(instr);
591 fprintf(fp, " wrmask=");
592 for (unsigned i = 0; i < 4; i++)
593 if ((wrmask >> i) & 1)
594 fprintf(fp, "%c", "xyzw"[i]);
595 } else {
596 unsigned off = info->index_map[idx] - 1;
597 assert(index_name[idx]); /* forgot to update index_name table? */
598 fprintf(fp, " %s=%d", index_name[idx], instr->const_index[off]);
599 }
600 fprintf(fp, " */");
601 }
602
603 if (!state->shader)
604 return;
605
606 struct exec_list *var_list = NULL;
607
608 switch (instr->intrinsic) {
609 case nir_intrinsic_load_uniform:
610 var_list = &state->shader->uniforms;
611 break;
612 case nir_intrinsic_load_input:
613 case nir_intrinsic_load_per_vertex_input:
614 var_list = &state->shader->inputs;
615 break;
616 case nir_intrinsic_load_output:
617 case nir_intrinsic_store_output:
618 case nir_intrinsic_store_per_vertex_output:
619 var_list = &state->shader->outputs;
620 break;
621 default:
622 return;
623 }
624
625 nir_foreach_variable(var, var_list) {
626 if ((var->data.driver_location == nir_intrinsic_base(instr)) &&
627 (instr->intrinsic == nir_intrinsic_load_uniform ||
628 var->data.location_frac == nir_intrinsic_component(instr)) &&
629 var->name) {
630 fprintf(fp, "\t/* %s */", var->name);
631 break;
632 }
633 }
634 }
635
636 static void
637 print_tex_instr(nir_tex_instr *instr, print_state *state)
638 {
639 FILE *fp = state->fp;
640
641 print_dest(&instr->dest, state);
642
643 fprintf(fp, " = ");
644
645 switch (instr->op) {
646 case nir_texop_tex:
647 fprintf(fp, "tex ");
648 break;
649 case nir_texop_txb:
650 fprintf(fp, "txb ");
651 break;
652 case nir_texop_txl:
653 fprintf(fp, "txl ");
654 break;
655 case nir_texop_txd:
656 fprintf(fp, "txd ");
657 break;
658 case nir_texop_txf:
659 fprintf(fp, "txf ");
660 break;
661 case nir_texop_txf_ms:
662 fprintf(fp, "txf_ms ");
663 break;
664 case nir_texop_txf_ms_mcs:
665 fprintf(fp, "txf_ms_mcs ");
666 break;
667 case nir_texop_txs:
668 fprintf(fp, "txs ");
669 break;
670 case nir_texop_lod:
671 fprintf(fp, "lod ");
672 break;
673 case nir_texop_tg4:
674 fprintf(fp, "tg4 ");
675 break;
676 case nir_texop_query_levels:
677 fprintf(fp, "query_levels ");
678 break;
679 case nir_texop_texture_samples:
680 fprintf(fp, "texture_samples ");
681 break;
682 case nir_texop_samples_identical:
683 fprintf(fp, "samples_identical ");
684 break;
685 default:
686 unreachable("Invalid texture operation");
687 break;
688 }
689
690 for (unsigned i = 0; i < instr->num_srcs; i++) {
691 print_src(&instr->src[i].src, state);
692
693 fprintf(fp, " ");
694
695 switch(instr->src[i].src_type) {
696 case nir_tex_src_coord:
697 fprintf(fp, "(coord)");
698 break;
699 case nir_tex_src_projector:
700 fprintf(fp, "(projector)");
701 break;
702 case nir_tex_src_comparitor:
703 fprintf(fp, "(comparitor)");
704 break;
705 case nir_tex_src_offset:
706 fprintf(fp, "(offset)");
707 break;
708 case nir_tex_src_bias:
709 fprintf(fp, "(bias)");
710 break;
711 case nir_tex_src_lod:
712 fprintf(fp, "(lod)");
713 break;
714 case nir_tex_src_ms_index:
715 fprintf(fp, "(ms_index)");
716 break;
717 case nir_tex_src_ms_mcs:
718 fprintf(fp, "(ms_mcs)");
719 break;
720 case nir_tex_src_ddx:
721 fprintf(fp, "(ddx)");
722 break;
723 case nir_tex_src_ddy:
724 fprintf(fp, "(ddy)");
725 break;
726 case nir_tex_src_texture_offset:
727 fprintf(fp, "(texture_offset)");
728 break;
729 case nir_tex_src_sampler_offset:
730 fprintf(fp, "(sampler_offset)");
731 break;
732 case nir_tex_src_plane:
733 fprintf(fp, "(plane)");
734 break;
735
736 default:
737 unreachable("Invalid texture source type");
738 break;
739 }
740
741 fprintf(fp, ", ");
742 }
743
744 if (instr->op == nir_texop_tg4) {
745 fprintf(fp, "%u (gather_component), ", instr->component);
746 }
747
748 if (instr->texture) {
749 print_deref(instr->texture, state);
750 fprintf(fp, " (texture)");
751 if (instr->sampler) {
752 print_deref(instr->sampler, state);
753 fprintf(fp, " (sampler)");
754 }
755 } else {
756 assert(instr->sampler == NULL);
757 fprintf(fp, "%u (texture) %u (sampler)",
758 instr->texture_index, instr->sampler_index);
759 }
760 }
761
762 static void
763 print_call_instr(nir_call_instr *instr, print_state *state)
764 {
765 FILE *fp = state->fp;
766
767 fprintf(fp, "call %s ", instr->callee->name);
768
769 for (unsigned i = 0; i < instr->num_params; i++) {
770 if (i != 0)
771 fprintf(fp, ", ");
772
773 print_deref(instr->params[i], state);
774 }
775
776 if (instr->return_deref != NULL) {
777 if (instr->num_params != 0)
778 fprintf(fp, ", ");
779 fprintf(fp, "returning ");
780 print_deref(instr->return_deref, state);
781 }
782 }
783
784 static void
785 print_load_const_instr(nir_load_const_instr *instr, print_state *state)
786 {
787 FILE *fp = state->fp;
788
789 print_ssa_def(&instr->def, state);
790
791 fprintf(fp, " = load_const (");
792
793 for (unsigned i = 0; i < instr->def.num_components; i++) {
794 if (i != 0)
795 fprintf(fp, ", ");
796
797 /*
798 * we don't really know the type of the constant (if it will be used as a
799 * float or an int), so just print the raw constant in hex for fidelity
800 * and then print the float in a comment for readability.
801 */
802
803 if (instr->def.bit_size == 64)
804 fprintf(fp, "0x%16" PRIx64 " /* %f */", instr->value.u64[i],
805 instr->value.f64[i]);
806 else
807 fprintf(fp, "0x%08x /* %f */", instr->value.u32[i], instr->value.f32[i]);
808 }
809
810 fprintf(fp, ")");
811 }
812
813 static void
814 print_jump_instr(nir_jump_instr *instr, print_state *state)
815 {
816 FILE *fp = state->fp;
817
818 switch (instr->type) {
819 case nir_jump_break:
820 fprintf(fp, "break");
821 break;
822
823 case nir_jump_continue:
824 fprintf(fp, "continue");
825 break;
826
827 case nir_jump_return:
828 fprintf(fp, "return");
829 break;
830 }
831 }
832
833 static void
834 print_ssa_undef_instr(nir_ssa_undef_instr* instr, print_state *state)
835 {
836 FILE *fp = state->fp;
837 print_ssa_def(&instr->def, state);
838 fprintf(fp, " = undefined");
839 }
840
841 static void
842 print_phi_instr(nir_phi_instr *instr, print_state *state)
843 {
844 FILE *fp = state->fp;
845 print_dest(&instr->dest, state);
846 fprintf(fp, " = phi ");
847 nir_foreach_phi_src(src, instr) {
848 if (&src->node != exec_list_get_head(&instr->srcs))
849 fprintf(fp, ", ");
850
851 fprintf(fp, "block_%u: ", src->pred->index);
852 print_src(&src->src, state);
853 }
854 }
855
856 static void
857 print_parallel_copy_instr(nir_parallel_copy_instr *instr, print_state *state)
858 {
859 FILE *fp = state->fp;
860 nir_foreach_parallel_copy_entry(entry, instr) {
861 if (&entry->node != exec_list_get_head(&instr->entries))
862 fprintf(fp, "; ");
863
864 print_dest(&entry->dest, state);
865 fprintf(fp, " = ");
866 print_src(&entry->src, state);
867 }
868 }
869
870 static void
871 print_instr(const nir_instr *instr, print_state *state, unsigned tabs)
872 {
873 FILE *fp = state->fp;
874 print_tabs(tabs, fp);
875
876 switch (instr->type) {
877 case nir_instr_type_alu:
878 print_alu_instr(nir_instr_as_alu(instr), state);
879 break;
880
881 case nir_instr_type_call:
882 print_call_instr(nir_instr_as_call(instr), state);
883 break;
884
885 case nir_instr_type_intrinsic:
886 print_intrinsic_instr(nir_instr_as_intrinsic(instr), state);
887 break;
888
889 case nir_instr_type_tex:
890 print_tex_instr(nir_instr_as_tex(instr), state);
891 break;
892
893 case nir_instr_type_load_const:
894 print_load_const_instr(nir_instr_as_load_const(instr), state);
895 break;
896
897 case nir_instr_type_jump:
898 print_jump_instr(nir_instr_as_jump(instr), state);
899 break;
900
901 case nir_instr_type_ssa_undef:
902 print_ssa_undef_instr(nir_instr_as_ssa_undef(instr), state);
903 break;
904
905 case nir_instr_type_phi:
906 print_phi_instr(nir_instr_as_phi(instr), state);
907 break;
908
909 case nir_instr_type_parallel_copy:
910 print_parallel_copy_instr(nir_instr_as_parallel_copy(instr), state);
911 break;
912
913 default:
914 unreachable("Invalid instruction type");
915 break;
916 }
917 }
918
919 static int
920 compare_block_index(const void *p1, const void *p2)
921 {
922 const nir_block *block1 = *((const nir_block **) p1);
923 const nir_block *block2 = *((const nir_block **) p2);
924
925 return (int) block1->index - (int) block2->index;
926 }
927
928 static void print_cf_node(nir_cf_node *node, print_state *state,
929 unsigned tabs);
930
931 static void
932 print_block(nir_block *block, print_state *state, unsigned tabs)
933 {
934 FILE *fp = state->fp;
935
936 print_tabs(tabs, fp);
937 fprintf(fp, "block block_%u:\n", block->index);
938
939 /* sort the predecessors by index so we consistently print the same thing */
940
941 nir_block **preds =
942 malloc(block->predecessors->entries * sizeof(nir_block *));
943
944 struct set_entry *entry;
945 unsigned i = 0;
946 set_foreach(block->predecessors, entry) {
947 preds[i++] = (nir_block *) entry->key;
948 }
949
950 qsort(preds, block->predecessors->entries, sizeof(nir_block *),
951 compare_block_index);
952
953 print_tabs(tabs, fp);
954 fprintf(fp, "/* preds: ");
955 for (unsigned i = 0; i < block->predecessors->entries; i++) {
956 fprintf(fp, "block_%u ", preds[i]->index);
957 }
958 fprintf(fp, "*/\n");
959
960 free(preds);
961
962 nir_foreach_instr(instr, block) {
963 print_instr(instr, state, tabs);
964 fprintf(fp, "\n");
965 print_annotation(state, instr);
966 }
967
968 print_tabs(tabs, fp);
969 fprintf(fp, "/* succs: ");
970 for (unsigned i = 0; i < 2; i++)
971 if (block->successors[i]) {
972 fprintf(fp, "block_%u ", block->successors[i]->index);
973 }
974 fprintf(fp, "*/\n");
975 }
976
977 static void
978 print_if(nir_if *if_stmt, print_state *state, unsigned tabs)
979 {
980 FILE *fp = state->fp;
981
982 print_tabs(tabs, fp);
983 fprintf(fp, "if ");
984 print_src(&if_stmt->condition, state);
985 fprintf(fp, " {\n");
986 foreach_list_typed(nir_cf_node, node, node, &if_stmt->then_list) {
987 print_cf_node(node, state, tabs + 1);
988 }
989 print_tabs(tabs, fp);
990 fprintf(fp, "} else {\n");
991 foreach_list_typed(nir_cf_node, node, node, &if_stmt->else_list) {
992 print_cf_node(node, state, tabs + 1);
993 }
994 print_tabs(tabs, fp);
995 fprintf(fp, "}\n");
996 }
997
998 static void
999 print_loop(nir_loop *loop, print_state *state, unsigned tabs)
1000 {
1001 FILE *fp = state->fp;
1002
1003 print_tabs(tabs, fp);
1004 fprintf(fp, "loop {\n");
1005 foreach_list_typed(nir_cf_node, node, node, &loop->body) {
1006 print_cf_node(node, state, tabs + 1);
1007 }
1008 print_tabs(tabs, fp);
1009 fprintf(fp, "}\n");
1010 }
1011
1012 static void
1013 print_cf_node(nir_cf_node *node, print_state *state, unsigned int tabs)
1014 {
1015 switch (node->type) {
1016 case nir_cf_node_block:
1017 print_block(nir_cf_node_as_block(node), state, tabs);
1018 break;
1019
1020 case nir_cf_node_if:
1021 print_if(nir_cf_node_as_if(node), state, tabs);
1022 break;
1023
1024 case nir_cf_node_loop:
1025 print_loop(nir_cf_node_as_loop(node), state, tabs);
1026 break;
1027
1028 default:
1029 unreachable("Invalid CFG node type");
1030 }
1031 }
1032
1033 static void
1034 print_function_impl(nir_function_impl *impl, print_state *state)
1035 {
1036 FILE *fp = state->fp;
1037
1038 fprintf(fp, "\nimpl %s ", impl->function->name);
1039
1040 for (unsigned i = 0; i < impl->num_params; i++) {
1041 if (i != 0)
1042 fprintf(fp, ", ");
1043
1044 print_arg(impl->params[i], state);
1045 }
1046
1047 if (impl->return_var != NULL) {
1048 if (impl->num_params != 0)
1049 fprintf(fp, ", ");
1050 fprintf(fp, "returning ");
1051 print_arg(impl->return_var, state);
1052 }
1053
1054 fprintf(fp, "{\n");
1055
1056 nir_foreach_variable(var, &impl->locals) {
1057 fprintf(fp, "\t");
1058 print_var_decl(var, state);
1059 }
1060
1061 foreach_list_typed(nir_register, reg, node, &impl->registers) {
1062 fprintf(fp, "\t");
1063 print_register_decl(reg, state);
1064 }
1065
1066 nir_index_blocks(impl);
1067
1068 foreach_list_typed(nir_cf_node, node, node, &impl->body) {
1069 print_cf_node(node, state, 1);
1070 }
1071
1072 fprintf(fp, "\tblock block_%u:\n}\n\n", impl->end_block->index);
1073 }
1074
1075 static void
1076 print_function(nir_function *function, print_state *state)
1077 {
1078 FILE *fp = state->fp;
1079
1080 fprintf(fp, "decl_function %s ", function->name);
1081
1082 for (unsigned i = 0; i < function->num_params; i++) {
1083 if (i != 0)
1084 fprintf(fp, ", ");
1085
1086 switch (function->params[i].param_type) {
1087 case nir_parameter_in:
1088 fprintf(fp, "in ");
1089 break;
1090 case nir_parameter_out:
1091 fprintf(fp, "out ");
1092 break;
1093 case nir_parameter_inout:
1094 fprintf(fp, "inout ");
1095 break;
1096 default:
1097 unreachable("Invalid parameter type");
1098 }
1099
1100 fprintf(fp, "%s", glsl_get_type_name(function->params[i].type));
1101 }
1102
1103 if (function->return_type != NULL) {
1104 if (function->num_params != 0)
1105 fprintf(fp, ", ");
1106 fprintf(fp, "returning %s", glsl_get_type_name(function->return_type));
1107 }
1108
1109 fprintf(fp, "\n");
1110
1111 if (function->impl != NULL) {
1112 print_function_impl(function->impl, state);
1113 return;
1114 }
1115 }
1116
1117 static void
1118 init_print_state(print_state *state, nir_shader *shader, FILE *fp)
1119 {
1120 state->fp = fp;
1121 state->shader = shader;
1122 state->ht = _mesa_hash_table_create(NULL, _mesa_hash_pointer,
1123 _mesa_key_pointer_equal);
1124 state->syms = _mesa_set_create(NULL, _mesa_key_hash_string,
1125 _mesa_key_string_equal);
1126 state->index = 0;
1127 }
1128
1129 static void
1130 destroy_print_state(print_state *state)
1131 {
1132 _mesa_hash_table_destroy(state->ht, NULL);
1133 _mesa_set_destroy(state->syms, NULL);
1134 }
1135
1136 void
1137 nir_print_shader_annotated(nir_shader *shader, FILE *fp,
1138 struct hash_table *annotations)
1139 {
1140 print_state state;
1141 init_print_state(&state, shader, fp);
1142
1143 state.annotations = annotations;
1144
1145 fprintf(fp, "shader: %s\n", gl_shader_stage_name(shader->stage));
1146
1147 if (shader->info->name)
1148 fprintf(fp, "name: %s\n", shader->info->name);
1149
1150 if (shader->info->label)
1151 fprintf(fp, "label: %s\n", shader->info->label);
1152
1153 fprintf(fp, "inputs: %u\n", shader->num_inputs);
1154 fprintf(fp, "outputs: %u\n", shader->num_outputs);
1155 fprintf(fp, "uniforms: %u\n", shader->num_uniforms);
1156 fprintf(fp, "shared: %u\n", shader->num_shared);
1157
1158 nir_foreach_variable(var, &shader->uniforms) {
1159 print_var_decl(var, &state);
1160 }
1161
1162 nir_foreach_variable(var, &shader->inputs) {
1163 print_var_decl(var, &state);
1164 }
1165
1166 nir_foreach_variable(var, &shader->outputs) {
1167 print_var_decl(var, &state);
1168 }
1169
1170 nir_foreach_variable(var, &shader->shared) {
1171 print_var_decl(var, &state);
1172 }
1173
1174 nir_foreach_variable(var, &shader->globals) {
1175 print_var_decl(var, &state);
1176 }
1177
1178 nir_foreach_variable(var, &shader->system_values) {
1179 print_var_decl(var, &state);
1180 }
1181
1182 foreach_list_typed(nir_register, reg, node, &shader->registers) {
1183 print_register_decl(reg, &state);
1184 }
1185
1186 foreach_list_typed(nir_function, func, node, &shader->functions) {
1187 print_function(func, &state);
1188 }
1189
1190 destroy_print_state(&state);
1191 }
1192
1193 void
1194 nir_print_shader(nir_shader *shader, FILE *fp)
1195 {
1196 nir_print_shader_annotated(shader, fp, NULL);
1197 }
1198
1199 void
1200 nir_print_instr(const nir_instr *instr, FILE *fp)
1201 {
1202 print_state state = {
1203 .fp = fp,
1204 };
1205 print_instr(instr, &state, 0);
1206
1207 }