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