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