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