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