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