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