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