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