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