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