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