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