nir: Add a ptr_as_array deref 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", "error",
92 "error", "error", "error", "vec16"};
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(const nir_src *src, print_state *state);
127
128 static void
129 print_reg_src(const 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(const 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 nir_component_mask_t used_channels = 0;
190
191 for (unsigned i = 0; i < NIR_MAX_VEC_COMPONENTS; i++) {
192 if (!nir_alu_instr_channel_used(instr, src, i))
193 continue;
194
195 used_channels++;
196
197 if (instr->src[src].swizzle[i] != i) {
198 print_swizzle = true;
199 break;
200 }
201 }
202
203 unsigned live_channels = nir_src_num_components(instr->src[src].src);
204
205 if (print_swizzle || used_channels != live_channels) {
206 fprintf(fp, ".");
207 for (unsigned i = 0; i < NIR_MAX_VEC_COMPONENTS; i++) {
208 if (!nir_alu_instr_channel_used(instr, src, i))
209 continue;
210
211 fprintf(fp, "%c", "xyzw"[instr->src[src].swizzle[i]]);
212 }
213 }
214
215 if (instr->src[src].abs)
216 fprintf(fp, ")");
217 }
218
219 static void
220 print_alu_dest(nir_alu_dest *dest, print_state *state)
221 {
222 FILE *fp = state->fp;
223 /* we're going to print the saturate modifier later, after the opcode */
224
225 print_dest(&dest->dest, state);
226
227 if (!dest->dest.is_ssa &&
228 dest->write_mask != (1 << dest->dest.reg.reg->num_components) - 1) {
229 fprintf(fp, ".");
230 for (unsigned i = 0; i < NIR_MAX_VEC_COMPONENTS; i++)
231 if ((dest->write_mask >> i) & 1)
232 fprintf(fp, "%c", "xyzw"[i]);
233 }
234 }
235
236 static void
237 print_alu_instr(nir_alu_instr *instr, print_state *state)
238 {
239 FILE *fp = state->fp;
240
241 print_alu_dest(&instr->dest, state);
242
243 fprintf(fp, " = %s", nir_op_infos[instr->op].name);
244 if (instr->exact)
245 fprintf(fp, "!");
246 if (instr->dest.saturate)
247 fprintf(fp, ".sat");
248 fprintf(fp, " ");
249
250 for (unsigned i = 0; i < nir_op_infos[instr->op].num_inputs; i++) {
251 if (i != 0)
252 fprintf(fp, ", ");
253
254 print_alu_src(instr, i, state);
255 }
256 }
257
258 static const char *
259 get_var_name(nir_variable *var, print_state *state)
260 {
261 if (state->ht == NULL)
262 return var->name ? var->name : "unnamed";
263
264 assert(state->syms);
265
266 struct hash_entry *entry = _mesa_hash_table_search(state->ht, var);
267 if (entry)
268 return entry->data;
269
270 char *name;
271 if (var->name == NULL) {
272 name = ralloc_asprintf(state->syms, "@%u", state->index++);
273 } else {
274 struct set_entry *set_entry = _mesa_set_search(state->syms, var->name);
275 if (set_entry != NULL) {
276 /* we have a collision with another name, append an @ + a unique
277 * index */
278 name = ralloc_asprintf(state->syms, "%s@%u", var->name,
279 state->index++);
280 } else {
281 /* Mark this one as seen */
282 _mesa_set_add(state->syms, var->name);
283 name = var->name;
284 }
285 }
286
287 _mesa_hash_table_insert(state->ht, var, name);
288
289 return name;
290 }
291
292 static void
293 print_constant(nir_constant *c, const struct glsl_type *type, print_state *state)
294 {
295 FILE *fp = state->fp;
296 const unsigned rows = glsl_get_vector_elements(type);
297 const unsigned cols = glsl_get_matrix_columns(type);
298 unsigned i, j;
299
300 switch (glsl_get_base_type(type)) {
301 case GLSL_TYPE_BOOL:
302 /* Only float base types can be matrices. */
303 assert(cols == 1);
304
305 for (i = 0; i < rows; i++) {
306 if (i > 0) fprintf(fp, ", ");
307 fprintf(fp, "%s", c->values[0].b[i] ? "true" : "false");
308 }
309 break;
310
311 case GLSL_TYPE_UINT8:
312 case GLSL_TYPE_INT8:
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%02x", c->values[0].u8[i]);
319 }
320 break;
321
322 case GLSL_TYPE_UINT16:
323 case GLSL_TYPE_INT16:
324 /* Only float base types can be matrices. */
325 assert(cols == 1);
326
327 for (i = 0; i < rows; i++) {
328 if (i > 0) fprintf(fp, ", ");
329 fprintf(fp, "0x%04x", c->values[0].u16[i]);
330 }
331 break;
332
333 case GLSL_TYPE_UINT:
334 case GLSL_TYPE_INT:
335 /* Only float base types can be matrices. */
336 assert(cols == 1);
337
338 for (i = 0; i < rows; i++) {
339 if (i > 0) fprintf(fp, ", ");
340 fprintf(fp, "0x%08x", c->values[0].u32[i]);
341 }
342 break;
343
344 case GLSL_TYPE_FLOAT16:
345 for (i = 0; i < cols; i++) {
346 for (j = 0; j < rows; j++) {
347 if (i + j > 0) fprintf(fp, ", ");
348 fprintf(fp, "%f", _mesa_half_to_float(c->values[i].u16[j]));
349 }
350 }
351 break;
352
353 case GLSL_TYPE_FLOAT:
354 for (i = 0; i < cols; i++) {
355 for (j = 0; j < rows; j++) {
356 if (i + j > 0) fprintf(fp, ", ");
357 fprintf(fp, "%f", c->values[i].f32[j]);
358 }
359 }
360 break;
361
362 case GLSL_TYPE_DOUBLE:
363 for (i = 0; i < cols; i++) {
364 for (j = 0; j < rows; j++) {
365 if (i + j > 0) fprintf(fp, ", ");
366 fprintf(fp, "%f", c->values[i].f64[j]);
367 }
368 }
369 break;
370
371 case GLSL_TYPE_UINT64:
372 case GLSL_TYPE_INT64:
373 /* Only float base types can be matrices. */
374 assert(cols == 1);
375
376 for (i = 0; i < cols; i++) {
377 if (i > 0) fprintf(fp, ", ");
378 fprintf(fp, "0x%08" PRIx64, c->values[0].u64[i]);
379 }
380 break;
381
382 case GLSL_TYPE_STRUCT:
383 for (i = 0; i < c->num_elements; i++) {
384 if (i > 0) fprintf(fp, ", ");
385 fprintf(fp, "{ ");
386 print_constant(c->elements[i], glsl_get_struct_field(type, i), state);
387 fprintf(fp, " }");
388 }
389 break;
390
391 case GLSL_TYPE_ARRAY:
392 for (i = 0; i < c->num_elements; i++) {
393 if (i > 0) fprintf(fp, ", ");
394 fprintf(fp, "{ ");
395 print_constant(c->elements[i], glsl_get_array_element(type), state);
396 fprintf(fp, " }");
397 }
398 break;
399
400 default:
401 unreachable("not reached");
402 }
403 }
404
405 static const char *
406 get_variable_mode_str(nir_variable_mode mode, bool want_local_global_mode)
407 {
408 switch (mode) {
409 case nir_var_shader_in:
410 return "shader_in";
411 case nir_var_shader_out:
412 return "shader_out";
413 case nir_var_uniform:
414 return "uniform";
415 case nir_var_ubo:
416 return "ubo";
417 case nir_var_system_value:
418 return "system";
419 case nir_var_ssbo:
420 return "ssbo";
421 case nir_var_shared:
422 return "shared";
423 case nir_var_global:
424 return want_local_global_mode ? "global" : "";
425 case nir_var_local:
426 return want_local_global_mode ? "local" : "";
427 default:
428 return "";
429 }
430 }
431
432 static void
433 print_var_decl(nir_variable *var, print_state *state)
434 {
435 FILE *fp = state->fp;
436
437 fprintf(fp, "decl_var ");
438
439 const char *const cent = (var->data.centroid) ? "centroid " : "";
440 const char *const samp = (var->data.sample) ? "sample " : "";
441 const char *const patch = (var->data.patch) ? "patch " : "";
442 const char *const inv = (var->data.invariant) ? "invariant " : "";
443 fprintf(fp, "%s%s%s%s%s %s ",
444 cent, samp, patch, inv, get_variable_mode_str(var->data.mode, false),
445 glsl_interp_mode_name(var->data.interpolation));
446
447 enum gl_access_qualifier access = var->data.image.access;
448 const char *const coher = (access & ACCESS_COHERENT) ? "coherent " : "";
449 const char *const volat = (access & ACCESS_VOLATILE) ? "volatile " : "";
450 const char *const restr = (access & ACCESS_RESTRICT) ? "restrict " : "";
451 const char *const ronly = (access & ACCESS_NON_WRITEABLE) ? "readonly " : "";
452 const char *const wonly = (access & ACCESS_NON_READABLE) ? "writeonly " : "";
453 fprintf(fp, "%s%s%s%s%s", coher, volat, restr, ronly, wonly);
454
455 #define FORMAT_CASE(x) case x: fprintf(stderr, #x " "); break
456 switch (var->data.image.format) {
457 FORMAT_CASE(GL_RGBA32F);
458 FORMAT_CASE(GL_RGBA32UI);
459 FORMAT_CASE(GL_RGBA32I);
460 FORMAT_CASE(GL_R32F);
461 FORMAT_CASE(GL_R32UI);
462 FORMAT_CASE(GL_R32I);
463 FORMAT_CASE(GL_RG32F);
464 FORMAT_CASE(GL_RG32UI);
465 FORMAT_CASE(GL_RG32I);
466 FORMAT_CASE(GL_R8);
467 FORMAT_CASE(GL_RG8);
468 FORMAT_CASE(GL_RGBA8);
469 FORMAT_CASE(GL_R8_SNORM);
470 FORMAT_CASE(GL_RG8_SNORM);
471 FORMAT_CASE(GL_RGBA8_SNORM);
472 FORMAT_CASE(GL_R16);
473 FORMAT_CASE(GL_RG16);
474 FORMAT_CASE(GL_RGBA16);
475 FORMAT_CASE(GL_R16_SNORM);
476 FORMAT_CASE(GL_RG16_SNORM);
477 FORMAT_CASE(GL_RGBA16_SNORM);
478 FORMAT_CASE(GL_R16F);
479 FORMAT_CASE(GL_RG16F);
480 FORMAT_CASE(GL_RGBA16F);
481 FORMAT_CASE(GL_R8UI);
482 FORMAT_CASE(GL_R8I);
483 FORMAT_CASE(GL_RG8UI);
484 FORMAT_CASE(GL_RG8I);
485 FORMAT_CASE(GL_RGBA8UI);
486 FORMAT_CASE(GL_RGBA8I);
487 FORMAT_CASE(GL_R16UI);
488 FORMAT_CASE(GL_R16I);
489 FORMAT_CASE(GL_RG16UI);
490 FORMAT_CASE(GL_RG16I);
491 FORMAT_CASE(GL_RGBA16UI);
492 FORMAT_CASE(GL_RGBA16I);
493 FORMAT_CASE(GL_R11F_G11F_B10F);
494 FORMAT_CASE(GL_RGB9_E5);
495 FORMAT_CASE(GL_RGB10_A2);
496 FORMAT_CASE(GL_RGB10_A2UI);
497 default: /* Including the normal GL_NONE */
498 break;
499 }
500 #undef FORMAT_CASE
501
502 fprintf(fp, "%s %s", glsl_get_type_name(var->type),
503 get_var_name(var, state));
504
505 if (var->data.mode == nir_var_shader_in ||
506 var->data.mode == nir_var_shader_out ||
507 var->data.mode == nir_var_uniform ||
508 var->data.mode == nir_var_ubo ||
509 var->data.mode == nir_var_ssbo) {
510 const char *loc = NULL;
511 char buf[4];
512
513 switch (state->shader->info.stage) {
514 case MESA_SHADER_VERTEX:
515 if (var->data.mode == nir_var_shader_in)
516 loc = gl_vert_attrib_name(var->data.location);
517 else if (var->data.mode == nir_var_shader_out)
518 loc = gl_varying_slot_name(var->data.location);
519 break;
520 case MESA_SHADER_GEOMETRY:
521 if ((var->data.mode == nir_var_shader_in) ||
522 (var->data.mode == nir_var_shader_out))
523 loc = gl_varying_slot_name(var->data.location);
524 break;
525 case MESA_SHADER_FRAGMENT:
526 if (var->data.mode == nir_var_shader_in)
527 loc = gl_varying_slot_name(var->data.location);
528 else if (var->data.mode == nir_var_shader_out)
529 loc = gl_frag_result_name(var->data.location);
530 break;
531 case MESA_SHADER_TESS_CTRL:
532 case MESA_SHADER_TESS_EVAL:
533 case MESA_SHADER_COMPUTE:
534 default:
535 /* TODO */
536 break;
537 }
538
539 if (!loc) {
540 snprintf(buf, sizeof(buf), "%u", var->data.location);
541 loc = buf;
542 }
543
544 /* For shader I/O vars that have been split to components or packed,
545 * print the fractional location within the input/output.
546 */
547 unsigned int num_components =
548 glsl_get_components(glsl_without_array(var->type));
549 const char *components = NULL;
550 char components_local[6] = {'.' /* the rest is 0-filled */};
551 switch (var->data.mode) {
552 case nir_var_shader_in:
553 case nir_var_shader_out:
554 if (num_components < 4 && num_components != 0) {
555 const char *xyzw = "xyzw";
556 for (int i = 0; i < num_components; i++)
557 components_local[i + 1] = xyzw[i + var->data.location_frac];
558
559 components = components_local;
560 }
561 break;
562 default:
563 break;
564 }
565
566 fprintf(fp, " (%s%s, %u, %u)%s", loc,
567 components ? components : "",
568 var->data.driver_location, var->data.binding,
569 var->data.compact ? " compact" : "");
570 }
571
572 if (var->constant_initializer) {
573 fprintf(fp, " = { ");
574 print_constant(var->constant_initializer, var->type, state);
575 fprintf(fp, " }");
576 }
577
578 fprintf(fp, "\n");
579 print_annotation(state, var);
580 }
581
582 static void
583 print_deref_link(const nir_deref_instr *instr, bool whole_chain, print_state *state)
584 {
585 FILE *fp = state->fp;
586
587 if (instr->deref_type == nir_deref_type_var) {
588 fprintf(fp, "%s", get_var_name(instr->var, state));
589 return;
590 } else if (instr->deref_type == nir_deref_type_cast) {
591 fprintf(fp, "(%s *)", glsl_get_type_name(instr->type));
592 print_src(&instr->parent, state);
593 return;
594 }
595
596 assert(instr->parent.is_ssa);
597 nir_deref_instr *parent =
598 nir_instr_as_deref(instr->parent.ssa->parent_instr);
599
600 /* Is the parent we're going to print a bare cast? */
601 const bool is_parent_cast =
602 whole_chain && parent->deref_type == nir_deref_type_cast;
603
604 /* If we're not printing the whole chain, the parent we print will be a SSA
605 * value that represents a pointer. The only deref type that naturally
606 * gives a pointer is a cast.
607 */
608 const bool is_parent_pointer =
609 !whole_chain || parent->deref_type == nir_deref_type_cast;
610
611 /* Struct derefs have a nice syntax that works on pointers, arrays derefs
612 * do not.
613 */
614 const bool need_deref =
615 is_parent_pointer && instr->deref_type != nir_deref_type_struct;
616
617 /* Cast need extra parens and so * dereferences */
618 if (is_parent_cast || need_deref)
619 fprintf(fp, "(");
620
621 if (need_deref)
622 fprintf(fp, "*");
623
624 if (whole_chain) {
625 print_deref_link(parent, whole_chain, state);
626 } else {
627 print_src(&instr->parent, state);
628 }
629
630 if (is_parent_cast || need_deref)
631 fprintf(fp, ")");
632
633 switch (instr->deref_type) {
634 case nir_deref_type_struct:
635 fprintf(fp, "%s%s", is_parent_pointer ? "->" : ".",
636 glsl_get_struct_elem_name(parent->type, instr->strct.index));
637 break;
638
639 case nir_deref_type_array:
640 case nir_deref_type_ptr_as_array: {
641 nir_const_value *const_index = nir_src_as_const_value(instr->arr.index);
642 if (const_index) {
643 fprintf(fp, "[%u]", const_index->u32[0]);
644 } else {
645 fprintf(fp, "[");
646 print_src(&instr->arr.index, state);
647 fprintf(fp, "]");
648 }
649 break;
650 }
651
652 case nir_deref_type_array_wildcard:
653 fprintf(fp, "[*]");
654 break;
655
656 default:
657 unreachable("Invalid deref instruction type");
658 }
659 }
660
661 static void
662 print_deref_instr(nir_deref_instr *instr, print_state *state)
663 {
664 FILE *fp = state->fp;
665
666 print_dest(&instr->dest, state);
667
668 switch (instr->deref_type) {
669 case nir_deref_type_var:
670 fprintf(fp, " = deref_var ");
671 break;
672 case nir_deref_type_array:
673 case nir_deref_type_array_wildcard:
674 fprintf(fp, " = deref_array ");
675 break;
676 case nir_deref_type_struct:
677 fprintf(fp, " = deref_struct ");
678 break;
679 case nir_deref_type_cast:
680 fprintf(fp, " = deref_cast ");
681 break;
682 case nir_deref_type_ptr_as_array:
683 fprintf(fp, " = deref_ptr_as_array ");
684 break;
685 default:
686 unreachable("Invalid deref instruction type");
687 }
688
689 /* Only casts naturally return a pointer type */
690 if (instr->deref_type != nir_deref_type_cast)
691 fprintf(fp, "&");
692
693 print_deref_link(instr, false, state);
694
695 fprintf(fp, " (%s %s) ",
696 get_variable_mode_str(instr->mode, true),
697 glsl_get_type_name(instr->type));
698
699 if (instr->deref_type != nir_deref_type_var &&
700 instr->deref_type != nir_deref_type_cast) {
701 /* Print the entire chain as a comment */
702 fprintf(fp, "/* &");
703 print_deref_link(instr, true, state);
704 fprintf(fp, " */");
705 }
706 }
707
708 static void
709 print_intrinsic_instr(nir_intrinsic_instr *instr, print_state *state)
710 {
711 const nir_intrinsic_info *info = &nir_intrinsic_infos[instr->intrinsic];
712 unsigned num_srcs = info->num_srcs;
713 FILE *fp = state->fp;
714
715 if (info->has_dest) {
716 print_dest(&instr->dest, state);
717 fprintf(fp, " = ");
718 }
719
720 fprintf(fp, "intrinsic %s (", info->name);
721
722 for (unsigned i = 0; i < num_srcs; i++) {
723 if (i != 0)
724 fprintf(fp, ", ");
725
726 print_src(&instr->src[i], state);
727 }
728
729 fprintf(fp, ") (");
730
731 for (unsigned i = 0; i < info->num_indices; i++) {
732 if (i != 0)
733 fprintf(fp, ", ");
734
735 fprintf(fp, "%d", instr->const_index[i]);
736 }
737
738 fprintf(fp, ")");
739
740 static const char *index_name[NIR_INTRINSIC_NUM_INDEX_FLAGS] = {
741 [NIR_INTRINSIC_BASE] = "base",
742 [NIR_INTRINSIC_WRMASK] = "wrmask",
743 [NIR_INTRINSIC_STREAM_ID] = "stream-id",
744 [NIR_INTRINSIC_UCP_ID] = "ucp-id",
745 [NIR_INTRINSIC_RANGE] = "range",
746 [NIR_INTRINSIC_DESC_SET] = "desc-set",
747 [NIR_INTRINSIC_BINDING] = "binding",
748 [NIR_INTRINSIC_COMPONENT] = "component",
749 [NIR_INTRINSIC_INTERP_MODE] = "interp_mode",
750 [NIR_INTRINSIC_REDUCTION_OP] = "reduction_op",
751 [NIR_INTRINSIC_CLUSTER_SIZE] = "cluster_size",
752 [NIR_INTRINSIC_PARAM_IDX] = "param_idx",
753 [NIR_INTRINSIC_IMAGE_DIM] = "image_dim",
754 [NIR_INTRINSIC_IMAGE_ARRAY] = "image_array",
755 [NIR_INTRINSIC_ACCESS] = "access",
756 [NIR_INTRINSIC_FORMAT] = "format",
757 [NIR_INTRINSIC_ALIGN_MUL] = "align_mul",
758 [NIR_INTRINSIC_ALIGN_OFFSET] = "align_offset",
759 };
760 for (unsigned idx = 1; idx < NIR_INTRINSIC_NUM_INDEX_FLAGS; idx++) {
761 if (!info->index_map[idx])
762 continue;
763 fprintf(fp, " /*");
764 if (idx == NIR_INTRINSIC_WRMASK) {
765 /* special case wrmask to show it as a writemask.. */
766 unsigned wrmask = nir_intrinsic_write_mask(instr);
767 fprintf(fp, " wrmask=");
768 for (unsigned i = 0; i < 4; i++)
769 if ((wrmask >> i) & 1)
770 fprintf(fp, "%c", "xyzw"[i]);
771 } else if (idx == NIR_INTRINSIC_REDUCTION_OP) {
772 nir_op reduction_op = nir_intrinsic_reduction_op(instr);
773 fprintf(fp, " reduction_op=%s", nir_op_infos[reduction_op].name);
774 } else if (idx == NIR_INTRINSIC_IMAGE_DIM) {
775 static const char *dim_name[] = {
776 [GLSL_SAMPLER_DIM_1D] = "1D",
777 [GLSL_SAMPLER_DIM_2D] = "2D",
778 [GLSL_SAMPLER_DIM_3D] = "3D",
779 [GLSL_SAMPLER_DIM_CUBE] = "Cube",
780 [GLSL_SAMPLER_DIM_RECT] = "Rect",
781 [GLSL_SAMPLER_DIM_BUF] = "Buf",
782 [GLSL_SAMPLER_DIM_MS] = "2D-MSAA",
783 [GLSL_SAMPLER_DIM_SUBPASS] = "Subpass",
784 [GLSL_SAMPLER_DIM_SUBPASS_MS] = "Subpass-MSAA",
785 };
786 enum glsl_sampler_dim dim = nir_intrinsic_image_dim(instr);
787 assert(dim < ARRAY_SIZE(dim_name) && dim_name[dim]);
788 fprintf(fp, " image_dim=%s", dim_name[dim]);
789 } else if (idx == NIR_INTRINSIC_IMAGE_ARRAY) {
790 bool array = nir_intrinsic_image_dim(instr);
791 fprintf(fp, " image_dim=%s", array ? "true" : "false");
792 } else {
793 unsigned off = info->index_map[idx] - 1;
794 assert(index_name[idx]); /* forgot to update index_name table? */
795 fprintf(fp, " %s=%d", index_name[idx], instr->const_index[off]);
796 }
797 fprintf(fp, " */");
798 }
799
800 if (!state->shader)
801 return;
802
803 struct exec_list *var_list = NULL;
804
805 switch (instr->intrinsic) {
806 case nir_intrinsic_load_uniform:
807 var_list = &state->shader->uniforms;
808 break;
809 case nir_intrinsic_load_input:
810 case nir_intrinsic_load_per_vertex_input:
811 var_list = &state->shader->inputs;
812 break;
813 case nir_intrinsic_load_output:
814 case nir_intrinsic_store_output:
815 case nir_intrinsic_store_per_vertex_output:
816 var_list = &state->shader->outputs;
817 break;
818 default:
819 return;
820 }
821
822 nir_foreach_variable(var, var_list) {
823 if ((var->data.driver_location == nir_intrinsic_base(instr)) &&
824 (instr->intrinsic == nir_intrinsic_load_uniform ||
825 var->data.location_frac == nir_intrinsic_component(instr)) &&
826 var->name) {
827 fprintf(fp, "\t/* %s */", var->name);
828 break;
829 }
830 }
831 }
832
833 static void
834 print_tex_instr(nir_tex_instr *instr, print_state *state)
835 {
836 FILE *fp = state->fp;
837
838 print_dest(&instr->dest, state);
839
840 fprintf(fp, " = ");
841
842 switch (instr->op) {
843 case nir_texop_tex:
844 fprintf(fp, "tex ");
845 break;
846 case nir_texop_txb:
847 fprintf(fp, "txb ");
848 break;
849 case nir_texop_txl:
850 fprintf(fp, "txl ");
851 break;
852 case nir_texop_txd:
853 fprintf(fp, "txd ");
854 break;
855 case nir_texop_txf:
856 fprintf(fp, "txf ");
857 break;
858 case nir_texop_txf_ms:
859 fprintf(fp, "txf_ms ");
860 break;
861 case nir_texop_txf_ms_mcs:
862 fprintf(fp, "txf_ms_mcs ");
863 break;
864 case nir_texop_txs:
865 fprintf(fp, "txs ");
866 break;
867 case nir_texop_lod:
868 fprintf(fp, "lod ");
869 break;
870 case nir_texop_tg4:
871 fprintf(fp, "tg4 ");
872 break;
873 case nir_texop_query_levels:
874 fprintf(fp, "query_levels ");
875 break;
876 case nir_texop_texture_samples:
877 fprintf(fp, "texture_samples ");
878 break;
879 case nir_texop_samples_identical:
880 fprintf(fp, "samples_identical ");
881 break;
882 default:
883 unreachable("Invalid texture operation");
884 break;
885 }
886
887 bool has_texture_deref = false, has_sampler_deref = false;
888 for (unsigned i = 0; i < instr->num_srcs; i++) {
889 print_src(&instr->src[i].src, state);
890
891 fprintf(fp, " ");
892
893 switch(instr->src[i].src_type) {
894 case nir_tex_src_coord:
895 fprintf(fp, "(coord)");
896 break;
897 case nir_tex_src_projector:
898 fprintf(fp, "(projector)");
899 break;
900 case nir_tex_src_comparator:
901 fprintf(fp, "(comparator)");
902 break;
903 case nir_tex_src_offset:
904 fprintf(fp, "(offset)");
905 break;
906 case nir_tex_src_bias:
907 fprintf(fp, "(bias)");
908 break;
909 case nir_tex_src_lod:
910 fprintf(fp, "(lod)");
911 break;
912 case nir_tex_src_min_lod:
913 fprintf(fp, "(min_lod)");
914 break;
915 case nir_tex_src_ms_index:
916 fprintf(fp, "(ms_index)");
917 break;
918 case nir_tex_src_ms_mcs:
919 fprintf(fp, "(ms_mcs)");
920 break;
921 case nir_tex_src_ddx:
922 fprintf(fp, "(ddx)");
923 break;
924 case nir_tex_src_ddy:
925 fprintf(fp, "(ddy)");
926 break;
927 case nir_tex_src_texture_deref:
928 has_texture_deref = true;
929 fprintf(fp, "(texture_deref)");
930 break;
931 case nir_tex_src_sampler_deref:
932 has_sampler_deref = true;
933 fprintf(fp, "(sampler_deref)");
934 break;
935 case nir_tex_src_texture_offset:
936 fprintf(fp, "(texture_offset)");
937 break;
938 case nir_tex_src_sampler_offset:
939 fprintf(fp, "(sampler_offset)");
940 break;
941 case nir_tex_src_plane:
942 fprintf(fp, "(plane)");
943 break;
944
945 default:
946 unreachable("Invalid texture source type");
947 break;
948 }
949
950 fprintf(fp, ", ");
951 }
952
953 if (instr->op == nir_texop_tg4) {
954 fprintf(fp, "%u (gather_component), ", instr->component);
955 }
956
957 if (!has_texture_deref) {
958 fprintf(fp, "%u (texture), ", instr->texture_index);
959 }
960
961 if (!has_sampler_deref) {
962 fprintf(fp, "%u (sampler), ", instr->sampler_index);
963 }
964 }
965
966 static void
967 print_call_instr(nir_call_instr *instr, print_state *state)
968 {
969 FILE *fp = state->fp;
970
971 fprintf(fp, "call %s ", instr->callee->name);
972
973 for (unsigned i = 0; i < instr->num_params; i++) {
974 if (i != 0)
975 fprintf(fp, ", ");
976
977 print_src(&instr->params[i], state);
978 }
979 }
980
981 static void
982 print_load_const_instr(nir_load_const_instr *instr, print_state *state)
983 {
984 FILE *fp = state->fp;
985
986 print_ssa_def(&instr->def, state);
987
988 fprintf(fp, " = load_const (");
989
990 for (unsigned i = 0; i < instr->def.num_components; i++) {
991 if (i != 0)
992 fprintf(fp, ", ");
993
994 /*
995 * we don't really know the type of the constant (if it will be used as a
996 * float or an int), so just print the raw constant in hex for fidelity
997 * and then print the float in a comment for readability.
998 */
999
1000 switch (instr->def.bit_size) {
1001 case 64:
1002 fprintf(fp, "0x%16" PRIx64 " /* %f */", instr->value.u64[i],
1003 instr->value.f64[i]);
1004 break;
1005 case 32:
1006 fprintf(fp, "0x%08x /* %f */", instr->value.u32[i], instr->value.f32[i]);
1007 break;
1008 case 16:
1009 fprintf(fp, "0x%04x /* %f */", instr->value.u16[i],
1010 _mesa_half_to_float(instr->value.u16[i]));
1011 break;
1012 case 8:
1013 fprintf(fp, "0x%02x", instr->value.u8[i]);
1014 break;
1015 case 1:
1016 fprintf(fp, "%s", instr->value.b[i] ? "true" : "false");
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 unsigned i = 0;
1160 set_foreach(block->predecessors, entry) {
1161 preds[i++] = (nir_block *) entry->key;
1162 }
1163
1164 qsort(preds, block->predecessors->entries, sizeof(nir_block *),
1165 compare_block_index);
1166
1167 print_tabs(tabs, fp);
1168 fprintf(fp, "/* preds: ");
1169 for (unsigned i = 0; i < block->predecessors->entries; i++) {
1170 fprintf(fp, "block_%u ", preds[i]->index);
1171 }
1172 fprintf(fp, "*/\n");
1173
1174 free(preds);
1175
1176 nir_foreach_instr(instr, block) {
1177 print_instr(instr, state, tabs);
1178 fprintf(fp, "\n");
1179 print_annotation(state, instr);
1180 }
1181
1182 print_tabs(tabs, fp);
1183 fprintf(fp, "/* succs: ");
1184 for (unsigned i = 0; i < 2; i++)
1185 if (block->successors[i]) {
1186 fprintf(fp, "block_%u ", block->successors[i]->index);
1187 }
1188 fprintf(fp, "*/\n");
1189 }
1190
1191 static void
1192 print_if(nir_if *if_stmt, print_state *state, unsigned tabs)
1193 {
1194 FILE *fp = state->fp;
1195
1196 print_tabs(tabs, fp);
1197 fprintf(fp, "if ");
1198 print_src(&if_stmt->condition, state);
1199 fprintf(fp, " {\n");
1200 foreach_list_typed(nir_cf_node, node, node, &if_stmt->then_list) {
1201 print_cf_node(node, state, tabs + 1);
1202 }
1203 print_tabs(tabs, fp);
1204 fprintf(fp, "} else {\n");
1205 foreach_list_typed(nir_cf_node, node, node, &if_stmt->else_list) {
1206 print_cf_node(node, state, tabs + 1);
1207 }
1208 print_tabs(tabs, fp);
1209 fprintf(fp, "}\n");
1210 }
1211
1212 static void
1213 print_loop(nir_loop *loop, print_state *state, unsigned tabs)
1214 {
1215 FILE *fp = state->fp;
1216
1217 print_tabs(tabs, fp);
1218 fprintf(fp, "loop {\n");
1219 foreach_list_typed(nir_cf_node, node, node, &loop->body) {
1220 print_cf_node(node, state, tabs + 1);
1221 }
1222 print_tabs(tabs, fp);
1223 fprintf(fp, "}\n");
1224 }
1225
1226 static void
1227 print_cf_node(nir_cf_node *node, print_state *state, unsigned int tabs)
1228 {
1229 switch (node->type) {
1230 case nir_cf_node_block:
1231 print_block(nir_cf_node_as_block(node), state, tabs);
1232 break;
1233
1234 case nir_cf_node_if:
1235 print_if(nir_cf_node_as_if(node), state, tabs);
1236 break;
1237
1238 case nir_cf_node_loop:
1239 print_loop(nir_cf_node_as_loop(node), state, tabs);
1240 break;
1241
1242 default:
1243 unreachable("Invalid CFG node type");
1244 }
1245 }
1246
1247 static void
1248 print_function_impl(nir_function_impl *impl, print_state *state)
1249 {
1250 FILE *fp = state->fp;
1251
1252 fprintf(fp, "\nimpl %s ", impl->function->name);
1253
1254 fprintf(fp, "{\n");
1255
1256 nir_foreach_variable(var, &impl->locals) {
1257 fprintf(fp, "\t");
1258 print_var_decl(var, state);
1259 }
1260
1261 foreach_list_typed(nir_register, reg, node, &impl->registers) {
1262 fprintf(fp, "\t");
1263 print_register_decl(reg, state);
1264 }
1265
1266 nir_index_blocks(impl);
1267
1268 foreach_list_typed(nir_cf_node, node, node, &impl->body) {
1269 print_cf_node(node, state, 1);
1270 }
1271
1272 fprintf(fp, "\tblock block_%u:\n}\n\n", impl->end_block->index);
1273 }
1274
1275 static void
1276 print_function(nir_function *function, print_state *state)
1277 {
1278 FILE *fp = state->fp;
1279
1280 fprintf(fp, "decl_function %s (%d params)", function->name,
1281 function->num_params);
1282
1283 fprintf(fp, "\n");
1284
1285 if (function->impl != NULL) {
1286 print_function_impl(function->impl, state);
1287 return;
1288 }
1289 }
1290
1291 static void
1292 init_print_state(print_state *state, nir_shader *shader, FILE *fp)
1293 {
1294 state->fp = fp;
1295 state->shader = shader;
1296 state->ht = _mesa_hash_table_create(NULL, _mesa_hash_pointer,
1297 _mesa_key_pointer_equal);
1298 state->syms = _mesa_set_create(NULL, _mesa_key_hash_string,
1299 _mesa_key_string_equal);
1300 state->index = 0;
1301 }
1302
1303 static void
1304 destroy_print_state(print_state *state)
1305 {
1306 _mesa_hash_table_destroy(state->ht, NULL);
1307 _mesa_set_destroy(state->syms, NULL);
1308 }
1309
1310 void
1311 nir_print_shader_annotated(nir_shader *shader, FILE *fp,
1312 struct hash_table *annotations)
1313 {
1314 print_state state;
1315 init_print_state(&state, shader, fp);
1316
1317 state.annotations = annotations;
1318
1319 fprintf(fp, "shader: %s\n", gl_shader_stage_name(shader->info.stage));
1320
1321 if (shader->info.name)
1322 fprintf(fp, "name: %s\n", shader->info.name);
1323
1324 if (shader->info.label)
1325 fprintf(fp, "label: %s\n", shader->info.label);
1326
1327 switch (shader->info.stage) {
1328 case MESA_SHADER_COMPUTE:
1329 fprintf(fp, "local-size: %u, %u, %u%s\n",
1330 shader->info.cs.local_size[0],
1331 shader->info.cs.local_size[1],
1332 shader->info.cs.local_size[2],
1333 shader->info.cs.local_size_variable ? " (variable)" : "");
1334 fprintf(fp, "shared-size: %u\n", shader->info.cs.shared_size);
1335 break;
1336 default:
1337 break;
1338 }
1339
1340 fprintf(fp, "inputs: %u\n", shader->num_inputs);
1341 fprintf(fp, "outputs: %u\n", shader->num_outputs);
1342 fprintf(fp, "uniforms: %u\n", shader->num_uniforms);
1343 fprintf(fp, "shared: %u\n", shader->num_shared);
1344
1345 nir_foreach_variable(var, &shader->uniforms) {
1346 print_var_decl(var, &state);
1347 }
1348
1349 nir_foreach_variable(var, &shader->inputs) {
1350 print_var_decl(var, &state);
1351 }
1352
1353 nir_foreach_variable(var, &shader->outputs) {
1354 print_var_decl(var, &state);
1355 }
1356
1357 nir_foreach_variable(var, &shader->shared) {
1358 print_var_decl(var, &state);
1359 }
1360
1361 nir_foreach_variable(var, &shader->globals) {
1362 print_var_decl(var, &state);
1363 }
1364
1365 nir_foreach_variable(var, &shader->system_values) {
1366 print_var_decl(var, &state);
1367 }
1368
1369 foreach_list_typed(nir_register, reg, node, &shader->registers) {
1370 print_register_decl(reg, &state);
1371 }
1372
1373 foreach_list_typed(nir_function, func, node, &shader->functions) {
1374 print_function(func, &state);
1375 }
1376
1377 destroy_print_state(&state);
1378 }
1379
1380 void
1381 nir_print_shader(nir_shader *shader, FILE *fp)
1382 {
1383 nir_print_shader_annotated(shader, fp, NULL);
1384 fflush(fp);
1385 }
1386
1387 void
1388 nir_print_instr(const nir_instr *instr, FILE *fp)
1389 {
1390 print_state state = {
1391 .fp = fp,
1392 };
1393 print_instr(instr, &state, 0);
1394
1395 }
1396
1397 void
1398 nir_print_deref(const nir_deref_instr *deref, FILE *fp)
1399 {
1400 print_state state = {
1401 .fp = fp,
1402 };
1403 print_deref_link(deref, true, &state);
1404 }