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