glsl: avoid hitting assert for arrays of arrays
[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 inv = (var->data.invariant) ? "invariant " : "";
232 const char *const mode[] = { "shader_in ", "shader_out ", "", "",
233 "uniform ", "shader_storage", "system " };
234
235 fprintf(fp, "%s%s%s%s%s ",
236 cent, samp, inv, mode[var->data.mode],
237 glsl_interp_qualifier_name(var->data.interpolation));
238
239 glsl_print_type(var->type, fp);
240
241 struct set_entry *entry = NULL;
242 if (state->syms)
243 entry = _mesa_set_search(state->syms, var->name);
244
245 char *name;
246
247 if (entry != NULL) {
248 /* we have a collision with another name, append an @ + a unique index */
249 name = ralloc_asprintf(state->syms, "%s@%u", var->name, state->index++);
250 } else {
251 name = var->name;
252 }
253
254 fprintf(fp, " %s", name);
255
256 if (var->data.mode == nir_var_shader_in ||
257 var->data.mode == nir_var_shader_out ||
258 var->data.mode == nir_var_uniform ||
259 var->data.mode == nir_var_shader_storage) {
260 const char *loc = NULL;
261 char buf[4];
262
263 switch (state->shader->stage) {
264 case MESA_SHADER_VERTEX:
265 if (var->data.mode == nir_var_shader_in)
266 loc = gl_vert_attrib_name(var->data.location);
267 else if (var->data.mode == nir_var_shader_out)
268 loc = gl_varying_slot_name(var->data.location);
269 break;
270 case MESA_SHADER_GEOMETRY:
271 if ((var->data.mode == nir_var_shader_in) ||
272 (var->data.mode == nir_var_shader_out))
273 loc = gl_varying_slot_name(var->data.location);
274 break;
275 case MESA_SHADER_FRAGMENT:
276 if (var->data.mode == nir_var_shader_in)
277 loc = gl_varying_slot_name(var->data.location);
278 else if (var->data.mode == nir_var_shader_out)
279 loc = gl_frag_result_name(var->data.location);
280 break;
281 case MESA_SHADER_TESS_CTRL:
282 case MESA_SHADER_TESS_EVAL:
283 case MESA_SHADER_COMPUTE:
284 default:
285 /* TODO */
286 break;
287 }
288
289 if (!loc) {
290 snprintf(buf, sizeof(buf), "%u", var->data.location);
291 loc = buf;
292 }
293
294 fprintf(fp, " (%s, %u)", loc, var->data.driver_location);
295 }
296
297 fprintf(fp, "\n");
298
299 if (state->syms) {
300 _mesa_set_add(state->syms, name);
301 _mesa_hash_table_insert(state->ht, var, name);
302 }
303 }
304
305 static void
306 print_var(nir_variable *var, print_state *state)
307 {
308 FILE *fp = state->fp;
309 const char *name;
310 if (state->ht) {
311 struct hash_entry *entry = _mesa_hash_table_search(state->ht, var);
312
313 assert(entry != NULL);
314 name = entry->data;
315 } else {
316 name = var->name;
317 }
318
319 fprintf(fp, "%s", name);
320 }
321
322 static void
323 print_deref_var(nir_deref_var *deref, print_state *state)
324 {
325 print_var(deref->var, state);
326 }
327
328 static void
329 print_deref_array(nir_deref_array *deref, print_state *state)
330 {
331 FILE *fp = state->fp;
332 fprintf(fp, "[");
333 switch (deref->deref_array_type) {
334 case nir_deref_array_type_direct:
335 fprintf(fp, "%u", deref->base_offset);
336 break;
337 case nir_deref_array_type_indirect:
338 if (deref->base_offset != 0)
339 fprintf(fp, "%u + ", deref->base_offset);
340 print_src(&deref->indirect, state);
341 break;
342 case nir_deref_array_type_wildcard:
343 fprintf(fp, "*");
344 break;
345 }
346 fprintf(fp, "]");
347 }
348
349 static void
350 print_deref_struct(nir_deref_struct *deref, const struct glsl_type *parent_type,
351 print_state *state)
352 {
353 FILE *fp = state->fp;
354 fprintf(fp, ".%s", glsl_get_struct_elem_name(parent_type, deref->index));
355 }
356
357 static void
358 print_deref(nir_deref_var *deref, print_state *state)
359 {
360 nir_deref *tail = &deref->deref;
361 nir_deref *pretail = NULL;
362 while (tail != NULL) {
363 switch (tail->deref_type) {
364 case nir_deref_type_var:
365 assert(pretail == NULL);
366 assert(tail == &deref->deref);
367 print_deref_var(deref, state);
368 break;
369
370 case nir_deref_type_array:
371 assert(pretail != NULL);
372 print_deref_array(nir_deref_as_array(tail), state);
373 break;
374
375 case nir_deref_type_struct:
376 assert(pretail != NULL);
377 print_deref_struct(nir_deref_as_struct(tail),
378 pretail->type, state);
379 break;
380
381 default:
382 unreachable("Invalid deref type");
383 }
384
385 pretail = tail;
386 tail = pretail->child;
387 }
388 }
389
390 static void
391 print_intrinsic_instr(nir_intrinsic_instr *instr, print_state *state)
392 {
393 unsigned num_srcs = nir_intrinsic_infos[instr->intrinsic].num_srcs;
394 FILE *fp = state->fp;
395
396 if (nir_intrinsic_infos[instr->intrinsic].has_dest) {
397 print_dest(&instr->dest, state);
398 fprintf(fp, " = ");
399 }
400
401 fprintf(fp, "intrinsic %s (", nir_intrinsic_infos[instr->intrinsic].name);
402
403 for (unsigned i = 0; i < num_srcs; i++) {
404 if (i != 0)
405 fprintf(fp, ", ");
406
407 print_src(&instr->src[i], state);
408 }
409
410 fprintf(fp, ") (");
411
412 unsigned num_vars = nir_intrinsic_infos[instr->intrinsic].num_variables;
413
414 for (unsigned i = 0; i < num_vars; i++) {
415 if (i != 0)
416 fprintf(fp, ", ");
417
418 print_deref(instr->variables[i], state);
419 }
420
421 fprintf(fp, ") (");
422
423 unsigned num_indices = nir_intrinsic_infos[instr->intrinsic].num_indices;
424
425 for (unsigned i = 0; i < num_indices; i++) {
426 if (i != 0)
427 fprintf(fp, ", ");
428
429 fprintf(fp, "%u", instr->const_index[i]);
430 }
431
432 fprintf(fp, ")");
433
434 if (!state->shader)
435 return;
436
437 struct exec_list *var_list = NULL;
438
439 switch (instr->intrinsic) {
440 case nir_intrinsic_load_uniform:
441 case nir_intrinsic_load_uniform_indirect:
442 var_list = &state->shader->uniforms;
443 break;
444 case nir_intrinsic_load_input:
445 case nir_intrinsic_load_input_indirect:
446 case nir_intrinsic_load_per_vertex_input:
447 case nir_intrinsic_load_per_vertex_input_indirect:
448 var_list = &state->shader->inputs;
449 break;
450 case nir_intrinsic_store_output:
451 case nir_intrinsic_store_output_indirect:
452 var_list = &state->shader->outputs;
453 break;
454 default:
455 return;
456 }
457
458 nir_foreach_variable(var, var_list) {
459 if ((var->data.driver_location == instr->const_index[0]) &&
460 var->name) {
461 fprintf(fp, "\t/* %s */", var->name);
462 break;
463 }
464 }
465 }
466
467 static void
468 print_tex_instr(nir_tex_instr *instr, print_state *state)
469 {
470 FILE *fp = state->fp;
471
472 print_dest(&instr->dest, state);
473
474 fprintf(fp, " = ");
475
476 switch (instr->op) {
477 case nir_texop_tex:
478 fprintf(fp, "tex ");
479 break;
480 case nir_texop_txb:
481 fprintf(fp, "txb ");
482 break;
483 case nir_texop_txl:
484 fprintf(fp, "txl ");
485 break;
486 case nir_texop_txd:
487 fprintf(fp, "txd ");
488 break;
489 case nir_texop_txf:
490 fprintf(fp, "txf ");
491 break;
492 case nir_texop_txf_ms:
493 fprintf(fp, "txf_ms ");
494 break;
495 case nir_texop_txs:
496 fprintf(fp, "txs ");
497 break;
498 case nir_texop_lod:
499 fprintf(fp, "lod ");
500 break;
501 case nir_texop_tg4:
502 fprintf(fp, "tg4 ");
503 break;
504 case nir_texop_query_levels:
505 fprintf(fp, "query_levels ");
506 break;
507 case nir_texop_texture_samples:
508 fprintf(fp, "texture_samples ");
509 break;
510
511 default:
512 unreachable("Invalid texture operation");
513 break;
514 }
515
516 for (unsigned i = 0; i < instr->num_srcs; i++) {
517 print_src(&instr->src[i].src, state);
518
519 fprintf(fp, " ");
520
521 switch(instr->src[i].src_type) {
522 case nir_tex_src_coord:
523 fprintf(fp, "(coord)");
524 break;
525 case nir_tex_src_projector:
526 fprintf(fp, "(projector)");
527 break;
528 case nir_tex_src_comparitor:
529 fprintf(fp, "(comparitor)");
530 break;
531 case nir_tex_src_offset:
532 fprintf(fp, "(offset)");
533 break;
534 case nir_tex_src_bias:
535 fprintf(fp, "(bias)");
536 break;
537 case nir_tex_src_lod:
538 fprintf(fp, "(lod)");
539 break;
540 case nir_tex_src_ms_index:
541 fprintf(fp, "(ms_index)");
542 break;
543 case nir_tex_src_ddx:
544 fprintf(fp, "(ddx)");
545 break;
546 case nir_tex_src_ddy:
547 fprintf(fp, "(ddy)");
548 break;
549 case nir_tex_src_sampler_offset:
550 fprintf(fp, "(sampler_offset)");
551 break;
552
553 default:
554 unreachable("Invalid texture source type");
555 break;
556 }
557
558 fprintf(fp, ", ");
559 }
560
561 bool has_nonzero_offset = false;
562 for (unsigned i = 0; i < 4; i++) {
563 if (instr->const_offset[i] != 0) {
564 has_nonzero_offset = true;
565 break;
566 }
567 }
568
569 if (has_nonzero_offset) {
570 fprintf(fp, "[%i %i %i %i] (offset), ",
571 instr->const_offset[0], instr->const_offset[1],
572 instr->const_offset[2], instr->const_offset[3]);
573 }
574
575 if (instr->op == nir_texop_tg4) {
576 fprintf(fp, "%u (gather_component), ", instr->component);
577 }
578
579 if (instr->sampler) {
580 print_deref(instr->sampler, state);
581 } else {
582 fprintf(fp, "%u", instr->sampler_index);
583 }
584
585 fprintf(fp, " (sampler)");
586 }
587
588 static void
589 print_call_instr(nir_call_instr *instr, print_state *state)
590 {
591 FILE *fp = state->fp;
592
593 fprintf(fp, "call %s ", instr->callee->function->name);
594
595 for (unsigned i = 0; i < instr->num_params; i++) {
596 if (i != 0)
597 fprintf(fp, ", ");
598
599 print_deref(instr->params[i], state);
600 }
601
602 if (instr->return_deref != NULL) {
603 if (instr->num_params != 0)
604 fprintf(fp, ", ");
605 fprintf(fp, "returning ");
606 print_deref(instr->return_deref, state);
607 }
608 }
609
610 static void
611 print_load_const_instr(nir_load_const_instr *instr, print_state *state)
612 {
613 FILE *fp = state->fp;
614
615 print_ssa_def(&instr->def, state);
616
617 fprintf(fp, " = load_const (");
618
619 for (unsigned i = 0; i < instr->def.num_components; i++) {
620 if (i != 0)
621 fprintf(fp, ", ");
622
623 /*
624 * we don't really know the type of the constant (if it will be used as a
625 * float or an int), so just print the raw constant in hex for fidelity
626 * and then print the float in a comment for readability.
627 */
628
629 fprintf(fp, "0x%08x /* %f */", instr->value.u[i], instr->value.f[i]);
630 }
631
632 fprintf(fp, ")");
633 }
634
635 static void
636 print_jump_instr(nir_jump_instr *instr, print_state *state)
637 {
638 FILE *fp = state->fp;
639
640 switch (instr->type) {
641 case nir_jump_break:
642 fprintf(fp, "break");
643 break;
644
645 case nir_jump_continue:
646 fprintf(fp, "continue");
647 break;
648
649 case nir_jump_return:
650 fprintf(fp, "return");
651 break;
652 }
653 }
654
655 static void
656 print_ssa_undef_instr(nir_ssa_undef_instr* instr, print_state *state)
657 {
658 FILE *fp = state->fp;
659 print_ssa_def(&instr->def, state);
660 fprintf(fp, " = undefined");
661 }
662
663 static void
664 print_phi_instr(nir_phi_instr *instr, print_state *state)
665 {
666 FILE *fp = state->fp;
667 print_dest(&instr->dest, state);
668 fprintf(fp, " = phi ");
669 nir_foreach_phi_src(instr, src) {
670 if (&src->node != exec_list_get_head(&instr->srcs))
671 fprintf(fp, ", ");
672
673 fprintf(fp, "block_%u: ", src->pred->index);
674 print_src(&src->src, state);
675 }
676 }
677
678 static void
679 print_parallel_copy_instr(nir_parallel_copy_instr *instr, print_state *state)
680 {
681 FILE *fp = state->fp;
682 nir_foreach_parallel_copy_entry(instr, entry) {
683 if (&entry->node != exec_list_get_head(&instr->entries))
684 fprintf(fp, "; ");
685
686 print_dest(&entry->dest, state);
687 fprintf(fp, " = ");
688 print_src(&entry->src, state);
689 }
690 }
691
692 static void
693 print_instr(const nir_instr *instr, print_state *state, unsigned tabs)
694 {
695 FILE *fp = state->fp;
696 print_tabs(tabs, fp);
697
698 switch (instr->type) {
699 case nir_instr_type_alu:
700 print_alu_instr(nir_instr_as_alu(instr), state);
701 break;
702
703 case nir_instr_type_call:
704 print_call_instr(nir_instr_as_call(instr), state);
705 break;
706
707 case nir_instr_type_intrinsic:
708 print_intrinsic_instr(nir_instr_as_intrinsic(instr), state);
709 break;
710
711 case nir_instr_type_tex:
712 print_tex_instr(nir_instr_as_tex(instr), state);
713 break;
714
715 case nir_instr_type_load_const:
716 print_load_const_instr(nir_instr_as_load_const(instr), state);
717 break;
718
719 case nir_instr_type_jump:
720 print_jump_instr(nir_instr_as_jump(instr), state);
721 break;
722
723 case nir_instr_type_ssa_undef:
724 print_ssa_undef_instr(nir_instr_as_ssa_undef(instr), state);
725 break;
726
727 case nir_instr_type_phi:
728 print_phi_instr(nir_instr_as_phi(instr), state);
729 break;
730
731 case nir_instr_type_parallel_copy:
732 print_parallel_copy_instr(nir_instr_as_parallel_copy(instr), state);
733 break;
734
735 default:
736 unreachable("Invalid instruction type");
737 break;
738 }
739 }
740
741 static int
742 compare_block_index(const void *p1, const void *p2)
743 {
744 const nir_block *block1 = *((const nir_block **) p1);
745 const nir_block *block2 = *((const nir_block **) p2);
746
747 return (int) block1->index - (int) block2->index;
748 }
749
750 static void print_cf_node(nir_cf_node *node, print_state *state,
751 unsigned tabs);
752
753 static void
754 print_block(nir_block *block, print_state *state, unsigned tabs)
755 {
756 FILE *fp = state->fp;
757
758 print_tabs(tabs, fp);
759 fprintf(fp, "block block_%u:\n", block->index);
760
761 /* sort the predecessors by index so we consistently print the same thing */
762
763 nir_block **preds =
764 malloc(block->predecessors->entries * sizeof(nir_block *));
765
766 struct set_entry *entry;
767 unsigned i = 0;
768 set_foreach(block->predecessors, entry) {
769 preds[i++] = (nir_block *) entry->key;
770 }
771
772 qsort(preds, block->predecessors->entries, sizeof(nir_block *),
773 compare_block_index);
774
775 print_tabs(tabs, fp);
776 fprintf(fp, "/* preds: ");
777 for (unsigned i = 0; i < block->predecessors->entries; i++) {
778 fprintf(fp, "block_%u ", preds[i]->index);
779 }
780 fprintf(fp, "*/\n");
781
782 free(preds);
783
784 nir_foreach_instr(block, instr) {
785 print_instr(instr, state, tabs);
786 fprintf(fp, "\n");
787 }
788
789 print_tabs(tabs, fp);
790 fprintf(fp, "/* succs: ");
791 for (unsigned i = 0; i < 2; i++)
792 if (block->successors[i]) {
793 fprintf(fp, "block_%u ", block->successors[i]->index);
794 }
795 fprintf(fp, "*/\n");
796 }
797
798 static void
799 print_if(nir_if *if_stmt, print_state *state, unsigned tabs)
800 {
801 FILE *fp = state->fp;
802
803 print_tabs(tabs, fp);
804 fprintf(fp, "if ");
805 print_src(&if_stmt->condition, state);
806 fprintf(fp, " {\n");
807 foreach_list_typed(nir_cf_node, node, node, &if_stmt->then_list) {
808 print_cf_node(node, state, tabs + 1);
809 }
810 print_tabs(tabs, fp);
811 fprintf(fp, "} else {\n");
812 foreach_list_typed(nir_cf_node, node, node, &if_stmt->else_list) {
813 print_cf_node(node, state, tabs + 1);
814 }
815 print_tabs(tabs, fp);
816 fprintf(fp, "}\n");
817 }
818
819 static void
820 print_loop(nir_loop *loop, print_state *state, unsigned tabs)
821 {
822 FILE *fp = state->fp;
823
824 print_tabs(tabs, fp);
825 fprintf(fp, "loop {\n");
826 foreach_list_typed(nir_cf_node, node, node, &loop->body) {
827 print_cf_node(node, state, tabs + 1);
828 }
829 print_tabs(tabs, fp);
830 fprintf(fp, "}\n");
831 }
832
833 static void
834 print_cf_node(nir_cf_node *node, print_state *state, unsigned int tabs)
835 {
836 switch (node->type) {
837 case nir_cf_node_block:
838 print_block(nir_cf_node_as_block(node), state, tabs);
839 break;
840
841 case nir_cf_node_if:
842 print_if(nir_cf_node_as_if(node), state, tabs);
843 break;
844
845 case nir_cf_node_loop:
846 print_loop(nir_cf_node_as_loop(node), state, tabs);
847 break;
848
849 default:
850 unreachable("Invalid CFG node type");
851 }
852 }
853
854 static void
855 print_function_impl(nir_function_impl *impl, print_state *state)
856 {
857 FILE *fp = state->fp;
858
859 fprintf(fp, "\nimpl %s ", impl->overload->function->name);
860
861 for (unsigned i = 0; i < impl->num_params; i++) {
862 if (i != 0)
863 fprintf(fp, ", ");
864
865 print_var(impl->params[i], state);
866 }
867
868 if (impl->return_var != NULL) {
869 if (impl->num_params != 0)
870 fprintf(fp, ", ");
871 fprintf(fp, "returning ");
872 print_var(impl->return_var, state);
873 }
874
875 fprintf(fp, "{\n");
876
877 nir_foreach_variable(var, &impl->locals) {
878 fprintf(fp, "\t");
879 print_var_decl(var, state);
880 }
881
882 foreach_list_typed(nir_register, reg, node, &impl->registers) {
883 fprintf(fp, "\t");
884 print_register_decl(reg, state);
885 }
886
887 nir_index_blocks(impl);
888
889 foreach_list_typed(nir_cf_node, node, node, &impl->body) {
890 print_cf_node(node, state, 1);
891 }
892
893 fprintf(fp, "\tblock block_%u:\n}\n\n", impl->end_block->index);
894 }
895
896 static void
897 print_function_overload(nir_function_overload *overload,
898 print_state *state)
899 {
900 FILE *fp = state->fp;
901
902 fprintf(fp, "decl_overload %s ", overload->function->name);
903
904 for (unsigned i = 0; i < overload->num_params; i++) {
905 if (i != 0)
906 fprintf(fp, ", ");
907
908 switch (overload->params[i].param_type) {
909 case nir_parameter_in:
910 fprintf(fp, "in ");
911 break;
912 case nir_parameter_out:
913 fprintf(fp, "out ");
914 break;
915 case nir_parameter_inout:
916 fprintf(fp, "inout ");
917 break;
918 default:
919 unreachable("Invalid parameter type");
920 }
921
922 glsl_print_type(overload->params[i].type, fp);
923 }
924
925 if (overload->return_type != NULL) {
926 if (overload->num_params != 0)
927 fprintf(fp, ", ");
928 fprintf(fp, "returning ");
929 glsl_print_type(overload->return_type, fp);
930 }
931
932 fprintf(fp, "\n");
933
934 if (overload->impl != NULL) {
935 print_function_impl(overload->impl, state);
936 return;
937 }
938 }
939
940 static void
941 print_function(nir_function *func, print_state *state)
942 {
943 foreach_list_typed(nir_function_overload, overload, node, &func->overload_list) {
944 print_function_overload(overload, state);
945 }
946 }
947
948 static void
949 init_print_state(print_state *state, nir_shader *shader, FILE *fp)
950 {
951 state->fp = fp;
952 state->shader = shader;
953 state->ht = _mesa_hash_table_create(NULL, _mesa_hash_pointer,
954 _mesa_key_pointer_equal);
955 state->syms = _mesa_set_create(NULL, _mesa_key_hash_string,
956 _mesa_key_string_equal);
957 state->index = 0;
958 }
959
960 static void
961 destroy_print_state(print_state *state)
962 {
963 _mesa_hash_table_destroy(state->ht, NULL);
964 _mesa_set_destroy(state->syms, NULL);
965 }
966
967 void
968 nir_print_shader(nir_shader *shader, FILE *fp)
969 {
970 print_state state;
971 init_print_state(&state, shader, fp);
972
973 fprintf(fp, "shader: %s\n", gl_shader_stage_name(shader->stage));
974
975 nir_foreach_variable(var, &shader->uniforms) {
976 print_var_decl(var, &state);
977 }
978
979 nir_foreach_variable(var, &shader->inputs) {
980 print_var_decl(var, &state);
981 }
982
983 nir_foreach_variable(var, &shader->outputs) {
984 print_var_decl(var, &state);
985 }
986
987 nir_foreach_variable(var, &shader->globals) {
988 print_var_decl(var, &state);
989 }
990
991 nir_foreach_variable(var, &shader->system_values) {
992 print_var_decl(var, &state);
993 }
994
995 foreach_list_typed(nir_register, reg, node, &shader->registers) {
996 print_register_decl(reg, &state);
997 }
998
999 foreach_list_typed(nir_function, func, node, &shader->functions) {
1000 print_function(func, &state);
1001 }
1002
1003 destroy_print_state(&state);
1004 }
1005
1006 void
1007 nir_print_instr(const nir_instr *instr, FILE *fp)
1008 {
1009 print_state state = {
1010 .fp = fp,
1011 };
1012 print_instr(instr, &state, 0);
1013
1014 }