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