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