nir: Remove old-school deref chain support
[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_global:
412 return want_local_global_mode ? "global" : "";
413 case nir_var_local:
414 return want_local_global_mode ? "local" : "";
415 default:
416 return "";
417 }
418 }
419
420 static void
421 print_var_decl(nir_variable *var, print_state *state)
422 {
423 FILE *fp = state->fp;
424
425 fprintf(fp, "decl_var ");
426
427 const char *const cent = (var->data.centroid) ? "centroid " : "";
428 const char *const samp = (var->data.sample) ? "sample " : "";
429 const char *const patch = (var->data.patch) ? "patch " : "";
430 const char *const inv = (var->data.invariant) ? "invariant " : "";
431 fprintf(fp, "%s%s%s%s%s %s ",
432 cent, samp, patch, inv, get_variable_mode_str(var->data.mode, false),
433 glsl_interp_mode_name(var->data.interpolation));
434
435 const char *const coher = (var->data.image.coherent) ? "coherent " : "";
436 const char *const volat = (var->data.image._volatile) ? "volatile " : "";
437 const char *const restr = (var->data.image.restrict_flag) ? "restrict " : "";
438 const char *const ronly = (var->data.image.read_only) ? "readonly " : "";
439 const char *const wonly = (var->data.image.write_only) ? "writeonly " : "";
440 fprintf(fp, "%s%s%s%s%s", coher, volat, restr, ronly, wonly);
441
442 fprintf(fp, "%s %s", glsl_get_type_name(var->type),
443 get_var_name(var, state));
444
445 if (var->data.mode == nir_var_shader_in ||
446 var->data.mode == nir_var_shader_out ||
447 var->data.mode == nir_var_uniform ||
448 var->data.mode == nir_var_shader_storage) {
449 const char *loc = NULL;
450 char buf[4];
451
452 switch (state->shader->info.stage) {
453 case MESA_SHADER_VERTEX:
454 if (var->data.mode == nir_var_shader_in)
455 loc = gl_vert_attrib_name(var->data.location);
456 else if (var->data.mode == nir_var_shader_out)
457 loc = gl_varying_slot_name(var->data.location);
458 break;
459 case MESA_SHADER_GEOMETRY:
460 if ((var->data.mode == nir_var_shader_in) ||
461 (var->data.mode == nir_var_shader_out))
462 loc = gl_varying_slot_name(var->data.location);
463 break;
464 case MESA_SHADER_FRAGMENT:
465 if (var->data.mode == nir_var_shader_in)
466 loc = gl_varying_slot_name(var->data.location);
467 else if (var->data.mode == nir_var_shader_out)
468 loc = gl_frag_result_name(var->data.location);
469 break;
470 case MESA_SHADER_TESS_CTRL:
471 case MESA_SHADER_TESS_EVAL:
472 case MESA_SHADER_COMPUTE:
473 default:
474 /* TODO */
475 break;
476 }
477
478 if (!loc) {
479 snprintf(buf, sizeof(buf), "%u", var->data.location);
480 loc = buf;
481 }
482
483 /* For shader I/O vars that have been split to components or packed,
484 * print the fractional location within the input/output.
485 */
486 unsigned int num_components =
487 glsl_get_components(glsl_without_array(var->type));
488 const char *components = NULL;
489 char components_local[6] = {'.' /* the rest is 0-filled */};
490 switch (var->data.mode) {
491 case nir_var_shader_in:
492 case nir_var_shader_out:
493 if (num_components < 4 && num_components != 0) {
494 const char *xyzw = "xyzw";
495 for (int i = 0; i < num_components; i++)
496 components_local[i + 1] = xyzw[i + var->data.location_frac];
497
498 components = components_local;
499 }
500 break;
501 default:
502 break;
503 }
504
505 fprintf(fp, " (%s%s, %u, %u)%s", loc,
506 components ? components : "",
507 var->data.driver_location, var->data.binding,
508 var->data.compact ? " compact" : "");
509 }
510
511 if (var->constant_initializer) {
512 fprintf(fp, " = { ");
513 print_constant(var->constant_initializer, var->type, state);
514 fprintf(fp, " }");
515 }
516
517 fprintf(fp, "\n");
518 print_annotation(state, var);
519 }
520
521 static void
522 print_deref_link(nir_deref_instr *instr, bool whole_chain, print_state *state)
523 {
524 FILE *fp = state->fp;
525
526 if (instr->deref_type == nir_deref_type_var) {
527 fprintf(fp, "%s", get_var_name(instr->var, state));
528 return;
529 } else if (instr->deref_type == nir_deref_type_cast) {
530 fprintf(fp, "(%s *)", glsl_get_type_name(instr->type));
531 print_src(&instr->parent, state);
532 return;
533 }
534
535 assert(instr->parent.is_ssa);
536 nir_deref_instr *parent =
537 nir_instr_as_deref(instr->parent.ssa->parent_instr);
538
539 /* Is the parent we're going to print a bare cast? */
540 const bool is_parent_cast =
541 whole_chain && parent->deref_type == nir_deref_type_cast;
542
543 /* If we're not printing the whole chain, the parent we print will be a SSA
544 * value that represents a pointer. The only deref type that naturally
545 * gives a pointer is a cast.
546 */
547 const bool is_parent_pointer =
548 !whole_chain || parent->deref_type == nir_deref_type_cast;
549
550 /* Struct derefs have a nice syntax that works on pointers, arrays derefs
551 * do not.
552 */
553 const bool need_deref =
554 is_parent_pointer && instr->deref_type != nir_deref_type_struct;
555
556 /* Cast need extra parens and so * dereferences */
557 if (is_parent_cast || need_deref)
558 fprintf(fp, "(");
559
560 if (need_deref)
561 fprintf(fp, "*");
562
563 if (whole_chain) {
564 print_deref_link(parent, whole_chain, state);
565 } else {
566 print_src(&instr->parent, state);
567 }
568
569 if (is_parent_cast || need_deref)
570 fprintf(fp, ")");
571
572 switch (instr->deref_type) {
573 case nir_deref_type_struct:
574 fprintf(fp, "%s%s", is_parent_pointer ? "->" : ".",
575 glsl_get_struct_elem_name(parent->type, instr->strct.index));
576 break;
577
578 case nir_deref_type_array: {
579 nir_const_value *const_index = nir_src_as_const_value(instr->arr.index);
580 if (const_index) {
581 fprintf(fp, "[%u]", const_index->u32[0]);
582 } else {
583 fprintf(fp, "[");
584 print_src(&instr->arr.index, state);
585 fprintf(fp, "]");
586 }
587 break;
588 }
589
590 case nir_deref_type_array_wildcard:
591 fprintf(fp, "[*]");
592 break;
593
594 default:
595 unreachable("Invalid deref instruction type");
596 }
597 }
598
599 static void
600 print_deref_instr(nir_deref_instr *instr, print_state *state)
601 {
602 FILE *fp = state->fp;
603
604 print_dest(&instr->dest, state);
605
606 switch (instr->deref_type) {
607 case nir_deref_type_var:
608 fprintf(fp, " = deref_var ");
609 break;
610 case nir_deref_type_array:
611 case nir_deref_type_array_wildcard:
612 fprintf(fp, " = deref_array ");
613 break;
614 case nir_deref_type_struct:
615 fprintf(fp, " = deref_struct ");
616 break;
617 case nir_deref_type_cast:
618 fprintf(fp, " = deref_cast ");
619 break;
620 default:
621 unreachable("Invalid deref instruction type");
622 }
623
624 /* Only casts naturally return a pointer type */
625 if (instr->deref_type != nir_deref_type_cast)
626 fprintf(fp, "&");
627
628 print_deref_link(instr, false, state);
629
630 fprintf(fp, " (%s %s) ",
631 get_variable_mode_str(instr->mode, true),
632 glsl_get_type_name(instr->type));
633
634 if (instr->deref_type != nir_deref_type_var &&
635 instr->deref_type != nir_deref_type_cast) {
636 /* Print the entire chain as a comment */
637 fprintf(fp, "/* &");
638 print_deref_link(instr, true, state);
639 fprintf(fp, " */");
640 }
641 }
642
643 static void
644 print_intrinsic_instr(nir_intrinsic_instr *instr, print_state *state)
645 {
646 const nir_intrinsic_info *info = &nir_intrinsic_infos[instr->intrinsic];
647 unsigned num_srcs = info->num_srcs;
648 FILE *fp = state->fp;
649
650 if (info->has_dest) {
651 print_dest(&instr->dest, state);
652 fprintf(fp, " = ");
653 }
654
655 fprintf(fp, "intrinsic %s (", info->name);
656
657 for (unsigned i = 0; i < num_srcs; i++) {
658 if (i != 0)
659 fprintf(fp, ", ");
660
661 print_src(&instr->src[i], state);
662 }
663
664 fprintf(fp, ") (");
665
666 for (unsigned i = 0; i < info->num_indices; i++) {
667 if (i != 0)
668 fprintf(fp, ", ");
669
670 fprintf(fp, "%d", instr->const_index[i]);
671 }
672
673 fprintf(fp, ")");
674
675 static const char *index_name[NIR_INTRINSIC_NUM_INDEX_FLAGS] = {
676 [NIR_INTRINSIC_BASE] = "base",
677 [NIR_INTRINSIC_WRMASK] = "wrmask",
678 [NIR_INTRINSIC_STREAM_ID] = "stream-id",
679 [NIR_INTRINSIC_UCP_ID] = "ucp-id",
680 [NIR_INTRINSIC_RANGE] = "range",
681 [NIR_INTRINSIC_DESC_SET] = "desc-set",
682 [NIR_INTRINSIC_BINDING] = "binding",
683 [NIR_INTRINSIC_COMPONENT] = "component",
684 [NIR_INTRINSIC_INTERP_MODE] = "interp_mode",
685 [NIR_INTRINSIC_REDUCTION_OP] = "reduction_op",
686 [NIR_INTRINSIC_CLUSTER_SIZE] = "cluster_size",
687 [NIR_INTRINSIC_PARAM_IDX] = "param_idx",
688 };
689 for (unsigned idx = 1; idx < NIR_INTRINSIC_NUM_INDEX_FLAGS; idx++) {
690 if (!info->index_map[idx])
691 continue;
692 fprintf(fp, " /*");
693 if (idx == NIR_INTRINSIC_WRMASK) {
694 /* special case wrmask to show it as a writemask.. */
695 unsigned wrmask = nir_intrinsic_write_mask(instr);
696 fprintf(fp, " wrmask=");
697 for (unsigned i = 0; i < 4; i++)
698 if ((wrmask >> i) & 1)
699 fprintf(fp, "%c", "xyzw"[i]);
700 } else if (idx == NIR_INTRINSIC_REDUCTION_OP) {
701 nir_op reduction_op = nir_intrinsic_reduction_op(instr);
702 fprintf(fp, " reduction_op=%s", nir_op_infos[reduction_op].name);
703 } else {
704 unsigned off = info->index_map[idx] - 1;
705 assert(index_name[idx]); /* forgot to update index_name table? */
706 fprintf(fp, " %s=%d", index_name[idx], instr->const_index[off]);
707 }
708 fprintf(fp, " */");
709 }
710
711 if (!state->shader)
712 return;
713
714 struct exec_list *var_list = NULL;
715
716 switch (instr->intrinsic) {
717 case nir_intrinsic_load_uniform:
718 var_list = &state->shader->uniforms;
719 break;
720 case nir_intrinsic_load_input:
721 case nir_intrinsic_load_per_vertex_input:
722 var_list = &state->shader->inputs;
723 break;
724 case nir_intrinsic_load_output:
725 case nir_intrinsic_store_output:
726 case nir_intrinsic_store_per_vertex_output:
727 var_list = &state->shader->outputs;
728 break;
729 default:
730 return;
731 }
732
733 nir_foreach_variable(var, var_list) {
734 if ((var->data.driver_location == nir_intrinsic_base(instr)) &&
735 (instr->intrinsic == nir_intrinsic_load_uniform ||
736 var->data.location_frac == nir_intrinsic_component(instr)) &&
737 var->name) {
738 fprintf(fp, "\t/* %s */", var->name);
739 break;
740 }
741 }
742 }
743
744 static void
745 print_tex_instr(nir_tex_instr *instr, print_state *state)
746 {
747 FILE *fp = state->fp;
748
749 print_dest(&instr->dest, state);
750
751 fprintf(fp, " = ");
752
753 switch (instr->op) {
754 case nir_texop_tex:
755 fprintf(fp, "tex ");
756 break;
757 case nir_texop_txb:
758 fprintf(fp, "txb ");
759 break;
760 case nir_texop_txl:
761 fprintf(fp, "txl ");
762 break;
763 case nir_texop_txd:
764 fprintf(fp, "txd ");
765 break;
766 case nir_texop_txf:
767 fprintf(fp, "txf ");
768 break;
769 case nir_texop_txf_ms:
770 fprintf(fp, "txf_ms ");
771 break;
772 case nir_texop_txf_ms_mcs:
773 fprintf(fp, "txf_ms_mcs ");
774 break;
775 case nir_texop_txs:
776 fprintf(fp, "txs ");
777 break;
778 case nir_texop_lod:
779 fprintf(fp, "lod ");
780 break;
781 case nir_texop_tg4:
782 fprintf(fp, "tg4 ");
783 break;
784 case nir_texop_query_levels:
785 fprintf(fp, "query_levels ");
786 break;
787 case nir_texop_texture_samples:
788 fprintf(fp, "texture_samples ");
789 break;
790 case nir_texop_samples_identical:
791 fprintf(fp, "samples_identical ");
792 break;
793 default:
794 unreachable("Invalid texture operation");
795 break;
796 }
797
798 for (unsigned i = 0; i < instr->num_srcs; i++) {
799 print_src(&instr->src[i].src, state);
800
801 fprintf(fp, " ");
802
803 switch(instr->src[i].src_type) {
804 case nir_tex_src_coord:
805 fprintf(fp, "(coord)");
806 break;
807 case nir_tex_src_projector:
808 fprintf(fp, "(projector)");
809 break;
810 case nir_tex_src_comparator:
811 fprintf(fp, "(comparator)");
812 break;
813 case nir_tex_src_offset:
814 fprintf(fp, "(offset)");
815 break;
816 case nir_tex_src_bias:
817 fprintf(fp, "(bias)");
818 break;
819 case nir_tex_src_lod:
820 fprintf(fp, "(lod)");
821 break;
822 case nir_tex_src_ms_index:
823 fprintf(fp, "(ms_index)");
824 break;
825 case nir_tex_src_ms_mcs:
826 fprintf(fp, "(ms_mcs)");
827 break;
828 case nir_tex_src_ddx:
829 fprintf(fp, "(ddx)");
830 break;
831 case nir_tex_src_ddy:
832 fprintf(fp, "(ddy)");
833 break;
834 case nir_tex_src_texture_deref:
835 fprintf(fp, "(texture_deref)");
836 break;
837 case nir_tex_src_sampler_deref:
838 fprintf(fp, "(sampler_deref)");
839 break;
840 case nir_tex_src_texture_offset:
841 fprintf(fp, "(texture_offset)");
842 break;
843 case nir_tex_src_sampler_offset:
844 fprintf(fp, "(sampler_offset)");
845 break;
846 case nir_tex_src_plane:
847 fprintf(fp, "(plane)");
848 break;
849
850 default:
851 unreachable("Invalid texture source type");
852 break;
853 }
854
855 fprintf(fp, ", ");
856 }
857
858 if (instr->op == nir_texop_tg4) {
859 fprintf(fp, "%u (gather_component), ", instr->component);
860 }
861 }
862
863 static void
864 print_call_instr(nir_call_instr *instr, print_state *state)
865 {
866 FILE *fp = state->fp;
867
868 fprintf(fp, "call %s ", instr->callee->name);
869
870 for (unsigned i = 0; i < instr->num_params; i++) {
871 if (i != 0)
872 fprintf(fp, ", ");
873
874 print_src(&instr->params[i], state);
875 }
876 }
877
878 static void
879 print_load_const_instr(nir_load_const_instr *instr, print_state *state)
880 {
881 FILE *fp = state->fp;
882
883 print_ssa_def(&instr->def, state);
884
885 fprintf(fp, " = load_const (");
886
887 for (unsigned i = 0; i < instr->def.num_components; i++) {
888 if (i != 0)
889 fprintf(fp, ", ");
890
891 /*
892 * we don't really know the type of the constant (if it will be used as a
893 * float or an int), so just print the raw constant in hex for fidelity
894 * and then print the float in a comment for readability.
895 */
896
897 switch (instr->def.bit_size) {
898 case 64:
899 fprintf(fp, "0x%16" PRIx64 " /* %f */", instr->value.u64[i],
900 instr->value.f64[i]);
901 break;
902 case 32:
903 fprintf(fp, "0x%08x /* %f */", instr->value.u32[i], instr->value.f32[i]);
904 break;
905 case 16:
906 fprintf(fp, "0x%04x /* %f */", instr->value.u16[i],
907 _mesa_half_to_float(instr->value.u16[i]));
908 break;
909 case 8:
910 fprintf(fp, "0x%02x", instr->value.u8[i]);
911 break;
912 }
913 }
914
915 fprintf(fp, ")");
916 }
917
918 static void
919 print_jump_instr(nir_jump_instr *instr, print_state *state)
920 {
921 FILE *fp = state->fp;
922
923 switch (instr->type) {
924 case nir_jump_break:
925 fprintf(fp, "break");
926 break;
927
928 case nir_jump_continue:
929 fprintf(fp, "continue");
930 break;
931
932 case nir_jump_return:
933 fprintf(fp, "return");
934 break;
935 }
936 }
937
938 static void
939 print_ssa_undef_instr(nir_ssa_undef_instr* instr, print_state *state)
940 {
941 FILE *fp = state->fp;
942 print_ssa_def(&instr->def, state);
943 fprintf(fp, " = undefined");
944 }
945
946 static void
947 print_phi_instr(nir_phi_instr *instr, print_state *state)
948 {
949 FILE *fp = state->fp;
950 print_dest(&instr->dest, state);
951 fprintf(fp, " = phi ");
952 nir_foreach_phi_src(src, instr) {
953 if (&src->node != exec_list_get_head(&instr->srcs))
954 fprintf(fp, ", ");
955
956 fprintf(fp, "block_%u: ", src->pred->index);
957 print_src(&src->src, state);
958 }
959 }
960
961 static void
962 print_parallel_copy_instr(nir_parallel_copy_instr *instr, print_state *state)
963 {
964 FILE *fp = state->fp;
965 nir_foreach_parallel_copy_entry(entry, instr) {
966 if (&entry->node != exec_list_get_head(&instr->entries))
967 fprintf(fp, "; ");
968
969 print_dest(&entry->dest, state);
970 fprintf(fp, " = ");
971 print_src(&entry->src, state);
972 }
973 }
974
975 static void
976 print_instr(const nir_instr *instr, print_state *state, unsigned tabs)
977 {
978 FILE *fp = state->fp;
979 print_tabs(tabs, fp);
980
981 switch (instr->type) {
982 case nir_instr_type_alu:
983 print_alu_instr(nir_instr_as_alu(instr), state);
984 break;
985
986 case nir_instr_type_deref:
987 print_deref_instr(nir_instr_as_deref(instr), state);
988 break;
989
990 case nir_instr_type_call:
991 print_call_instr(nir_instr_as_call(instr), state);
992 break;
993
994 case nir_instr_type_intrinsic:
995 print_intrinsic_instr(nir_instr_as_intrinsic(instr), state);
996 break;
997
998 case nir_instr_type_tex:
999 print_tex_instr(nir_instr_as_tex(instr), state);
1000 break;
1001
1002 case nir_instr_type_load_const:
1003 print_load_const_instr(nir_instr_as_load_const(instr), state);
1004 break;
1005
1006 case nir_instr_type_jump:
1007 print_jump_instr(nir_instr_as_jump(instr), state);
1008 break;
1009
1010 case nir_instr_type_ssa_undef:
1011 print_ssa_undef_instr(nir_instr_as_ssa_undef(instr), state);
1012 break;
1013
1014 case nir_instr_type_phi:
1015 print_phi_instr(nir_instr_as_phi(instr), state);
1016 break;
1017
1018 case nir_instr_type_parallel_copy:
1019 print_parallel_copy_instr(nir_instr_as_parallel_copy(instr), state);
1020 break;
1021
1022 default:
1023 unreachable("Invalid instruction type");
1024 break;
1025 }
1026 }
1027
1028 static int
1029 compare_block_index(const void *p1, const void *p2)
1030 {
1031 const nir_block *block1 = *((const nir_block **) p1);
1032 const nir_block *block2 = *((const nir_block **) p2);
1033
1034 return (int) block1->index - (int) block2->index;
1035 }
1036
1037 static void print_cf_node(nir_cf_node *node, print_state *state,
1038 unsigned tabs);
1039
1040 static void
1041 print_block(nir_block *block, print_state *state, unsigned tabs)
1042 {
1043 FILE *fp = state->fp;
1044
1045 print_tabs(tabs, fp);
1046 fprintf(fp, "block block_%u:\n", block->index);
1047
1048 /* sort the predecessors by index so we consistently print the same thing */
1049
1050 nir_block **preds =
1051 malloc(block->predecessors->entries * sizeof(nir_block *));
1052
1053 struct set_entry *entry;
1054 unsigned i = 0;
1055 set_foreach(block->predecessors, entry) {
1056 preds[i++] = (nir_block *) entry->key;
1057 }
1058
1059 qsort(preds, block->predecessors->entries, sizeof(nir_block *),
1060 compare_block_index);
1061
1062 print_tabs(tabs, fp);
1063 fprintf(fp, "/* preds: ");
1064 for (unsigned i = 0; i < block->predecessors->entries; i++) {
1065 fprintf(fp, "block_%u ", preds[i]->index);
1066 }
1067 fprintf(fp, "*/\n");
1068
1069 free(preds);
1070
1071 nir_foreach_instr(instr, block) {
1072 print_instr(instr, state, tabs);
1073 fprintf(fp, "\n");
1074 print_annotation(state, instr);
1075 }
1076
1077 print_tabs(tabs, fp);
1078 fprintf(fp, "/* succs: ");
1079 for (unsigned i = 0; i < 2; i++)
1080 if (block->successors[i]) {
1081 fprintf(fp, "block_%u ", block->successors[i]->index);
1082 }
1083 fprintf(fp, "*/\n");
1084 }
1085
1086 static void
1087 print_if(nir_if *if_stmt, print_state *state, unsigned tabs)
1088 {
1089 FILE *fp = state->fp;
1090
1091 print_tabs(tabs, fp);
1092 fprintf(fp, "if ");
1093 print_src(&if_stmt->condition, state);
1094 fprintf(fp, " {\n");
1095 foreach_list_typed(nir_cf_node, node, node, &if_stmt->then_list) {
1096 print_cf_node(node, state, tabs + 1);
1097 }
1098 print_tabs(tabs, fp);
1099 fprintf(fp, "} else {\n");
1100 foreach_list_typed(nir_cf_node, node, node, &if_stmt->else_list) {
1101 print_cf_node(node, state, tabs + 1);
1102 }
1103 print_tabs(tabs, fp);
1104 fprintf(fp, "}\n");
1105 }
1106
1107 static void
1108 print_loop(nir_loop *loop, print_state *state, unsigned tabs)
1109 {
1110 FILE *fp = state->fp;
1111
1112 print_tabs(tabs, fp);
1113 fprintf(fp, "loop {\n");
1114 foreach_list_typed(nir_cf_node, node, node, &loop->body) {
1115 print_cf_node(node, state, tabs + 1);
1116 }
1117 print_tabs(tabs, fp);
1118 fprintf(fp, "}\n");
1119 }
1120
1121 static void
1122 print_cf_node(nir_cf_node *node, print_state *state, unsigned int tabs)
1123 {
1124 switch (node->type) {
1125 case nir_cf_node_block:
1126 print_block(nir_cf_node_as_block(node), state, tabs);
1127 break;
1128
1129 case nir_cf_node_if:
1130 print_if(nir_cf_node_as_if(node), state, tabs);
1131 break;
1132
1133 case nir_cf_node_loop:
1134 print_loop(nir_cf_node_as_loop(node), state, tabs);
1135 break;
1136
1137 default:
1138 unreachable("Invalid CFG node type");
1139 }
1140 }
1141
1142 static void
1143 print_function_impl(nir_function_impl *impl, print_state *state)
1144 {
1145 FILE *fp = state->fp;
1146
1147 fprintf(fp, "\nimpl %s ", impl->function->name);
1148
1149 fprintf(fp, "{\n");
1150
1151 nir_foreach_variable(var, &impl->locals) {
1152 fprintf(fp, "\t");
1153 print_var_decl(var, state);
1154 }
1155
1156 foreach_list_typed(nir_register, reg, node, &impl->registers) {
1157 fprintf(fp, "\t");
1158 print_register_decl(reg, state);
1159 }
1160
1161 nir_index_blocks(impl);
1162
1163 foreach_list_typed(nir_cf_node, node, node, &impl->body) {
1164 print_cf_node(node, state, 1);
1165 }
1166
1167 fprintf(fp, "\tblock block_%u:\n}\n\n", impl->end_block->index);
1168 }
1169
1170 static void
1171 print_function(nir_function *function, print_state *state)
1172 {
1173 FILE *fp = state->fp;
1174
1175 fprintf(fp, "decl_function %s (%d params)", function->name,
1176 function->num_params);
1177
1178 fprintf(fp, "\n");
1179
1180 if (function->impl != NULL) {
1181 print_function_impl(function->impl, state);
1182 return;
1183 }
1184 }
1185
1186 static void
1187 init_print_state(print_state *state, nir_shader *shader, FILE *fp)
1188 {
1189 state->fp = fp;
1190 state->shader = shader;
1191 state->ht = _mesa_hash_table_create(NULL, _mesa_hash_pointer,
1192 _mesa_key_pointer_equal);
1193 state->syms = _mesa_set_create(NULL, _mesa_key_hash_string,
1194 _mesa_key_string_equal);
1195 state->index = 0;
1196 }
1197
1198 static void
1199 destroy_print_state(print_state *state)
1200 {
1201 _mesa_hash_table_destroy(state->ht, NULL);
1202 _mesa_set_destroy(state->syms, NULL);
1203 }
1204
1205 void
1206 nir_print_shader_annotated(nir_shader *shader, FILE *fp,
1207 struct hash_table *annotations)
1208 {
1209 print_state state;
1210 init_print_state(&state, shader, fp);
1211
1212 state.annotations = annotations;
1213
1214 fprintf(fp, "shader: %s\n", gl_shader_stage_name(shader->info.stage));
1215
1216 if (shader->info.name)
1217 fprintf(fp, "name: %s\n", shader->info.name);
1218
1219 if (shader->info.label)
1220 fprintf(fp, "label: %s\n", shader->info.label);
1221
1222 switch (shader->info.stage) {
1223 case MESA_SHADER_COMPUTE:
1224 fprintf(fp, "local-size: %u, %u, %u%s\n",
1225 shader->info.cs.local_size[0],
1226 shader->info.cs.local_size[1],
1227 shader->info.cs.local_size[2],
1228 shader->info.cs.local_size_variable ? " (variable)" : "");
1229 fprintf(fp, "shared-size: %u\n", shader->info.cs.shared_size);
1230 break;
1231 default:
1232 break;
1233 }
1234
1235 fprintf(fp, "inputs: %u\n", shader->num_inputs);
1236 fprintf(fp, "outputs: %u\n", shader->num_outputs);
1237 fprintf(fp, "uniforms: %u\n", shader->num_uniforms);
1238 fprintf(fp, "shared: %u\n", shader->num_shared);
1239
1240 nir_foreach_variable(var, &shader->uniforms) {
1241 print_var_decl(var, &state);
1242 }
1243
1244 nir_foreach_variable(var, &shader->inputs) {
1245 print_var_decl(var, &state);
1246 }
1247
1248 nir_foreach_variable(var, &shader->outputs) {
1249 print_var_decl(var, &state);
1250 }
1251
1252 nir_foreach_variable(var, &shader->shared) {
1253 print_var_decl(var, &state);
1254 }
1255
1256 nir_foreach_variable(var, &shader->globals) {
1257 print_var_decl(var, &state);
1258 }
1259
1260 nir_foreach_variable(var, &shader->system_values) {
1261 print_var_decl(var, &state);
1262 }
1263
1264 foreach_list_typed(nir_register, reg, node, &shader->registers) {
1265 print_register_decl(reg, &state);
1266 }
1267
1268 foreach_list_typed(nir_function, func, node, &shader->functions) {
1269 print_function(func, &state);
1270 }
1271
1272 destroy_print_state(&state);
1273 }
1274
1275 void
1276 nir_print_shader(nir_shader *shader, FILE *fp)
1277 {
1278 nir_print_shader_annotated(shader, fp, NULL);
1279 }
1280
1281 void
1282 nir_print_instr(const nir_instr *instr, FILE *fp)
1283 {
1284 print_state state = {
1285 .fp = fp,
1286 };
1287 print_instr(instr, &state, 0);
1288
1289 }