nir: Add support for 1-bit data types
[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(nir_src *src, print_state *state);
127
128 static void
129 print_reg_src(nir_reg_src *src, print_state *state)
130 {
131 FILE *fp = state->fp;
132 print_register(src->reg, state);
133 if (src->reg->num_array_elems != 0) {
134 fprintf(fp, "[%u", src->base_offset);
135 if (src->indirect != NULL) {
136 fprintf(fp, " + ");
137 print_src(src->indirect, state);
138 }
139 fprintf(fp, "]");
140 }
141 }
142
143 static void
144 print_reg_dest(nir_reg_dest *dest, print_state *state)
145 {
146 FILE *fp = state->fp;
147 print_register(dest->reg, state);
148 if (dest->reg->num_array_elems != 0) {
149 fprintf(fp, "[%u", dest->base_offset);
150 if (dest->indirect != NULL) {
151 fprintf(fp, " + ");
152 print_src(dest->indirect, state);
153 }
154 fprintf(fp, "]");
155 }
156 }
157
158 static void
159 print_src(nir_src *src, print_state *state)
160 {
161 if (src->is_ssa)
162 print_ssa_use(src->ssa, state);
163 else
164 print_reg_src(&src->reg, state);
165 }
166
167 static void
168 print_dest(nir_dest *dest, print_state *state)
169 {
170 if (dest->is_ssa)
171 print_ssa_def(&dest->ssa, state);
172 else
173 print_reg_dest(&dest->reg, state);
174 }
175
176 static void
177 print_alu_src(nir_alu_instr *instr, unsigned src, print_state *state)
178 {
179 FILE *fp = state->fp;
180
181 if (instr->src[src].negate)
182 fprintf(fp, "-");
183 if (instr->src[src].abs)
184 fprintf(fp, "abs(");
185
186 print_src(&instr->src[src].src, state);
187
188 bool print_swizzle = false;
189 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_UINT8:
302 case GLSL_TYPE_INT8:
303 /* Only float base types can be matrices. */
304 assert(cols == 1);
305
306 for (i = 0; i < rows; i++) {
307 if (i > 0) fprintf(fp, ", ");
308 fprintf(fp, "0x%02x", c->values[0].u8[i]);
309 }
310 break;
311
312 case GLSL_TYPE_UINT16:
313 case GLSL_TYPE_INT16:
314 /* Only float base types can be matrices. */
315 assert(cols == 1);
316
317 for (i = 0; i < rows; i++) {
318 if (i > 0) fprintf(fp, ", ");
319 fprintf(fp, "0x%04x", c->values[0].u16[i]);
320 }
321 break;
322
323 case GLSL_TYPE_UINT:
324 case GLSL_TYPE_INT:
325 case GLSL_TYPE_BOOL:
326 /* Only float base types can be matrices. */
327 assert(cols == 1);
328
329 for (i = 0; i < rows; i++) {
330 if (i > 0) fprintf(fp, ", ");
331 fprintf(fp, "0x%08x", c->values[0].u32[i]);
332 }
333 break;
334
335 case GLSL_TYPE_FLOAT16:
336 for (i = 0; i < cols; i++) {
337 for (j = 0; j < rows; j++) {
338 if (i + j > 0) fprintf(fp, ", ");
339 fprintf(fp, "%f", _mesa_half_to_float(c->values[i].u16[j]));
340 }
341 }
342 break;
343
344 case GLSL_TYPE_FLOAT:
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", c->values[i].f32[j]);
349 }
350 }
351 break;
352
353 case GLSL_TYPE_DOUBLE:
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].f64[j]);
358 }
359 }
360 break;
361
362 case GLSL_TYPE_UINT64:
363 case GLSL_TYPE_INT64:
364 /* Only float base types can be matrices. */
365 assert(cols == 1);
366
367 for (i = 0; i < cols; i++) {
368 if (i > 0) fprintf(fp, ", ");
369 fprintf(fp, "0x%08" PRIx64, c->values[0].u64[i]);
370 }
371 break;
372
373 case GLSL_TYPE_STRUCT:
374 for (i = 0; i < c->num_elements; i++) {
375 if (i > 0) fprintf(fp, ", ");
376 fprintf(fp, "{ ");
377 print_constant(c->elements[i], glsl_get_struct_field(type, i), state);
378 fprintf(fp, " }");
379 }
380 break;
381
382 case GLSL_TYPE_ARRAY:
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_array_element(type), state);
387 fprintf(fp, " }");
388 }
389 break;
390
391 default:
392 unreachable("not reached");
393 }
394 }
395
396 static const char *
397 get_variable_mode_str(nir_variable_mode mode, bool want_local_global_mode)
398 {
399 switch (mode) {
400 case nir_var_shader_in:
401 return "shader_in";
402 case nir_var_shader_out:
403 return "shader_out";
404 case nir_var_uniform:
405 return "uniform";
406 case nir_var_shader_storage:
407 return "shader_storage";
408 case nir_var_system_value:
409 return "system";
410 case nir_var_shared:
411 return "shared";
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 enum gl_access_qualifier access = var->data.image.access;
437 const char *const coher = (access & ACCESS_COHERENT) ? "coherent " : "";
438 const char *const volat = (access & ACCESS_VOLATILE) ? "volatile " : "";
439 const char *const restr = (access & ACCESS_RESTRICT) ? "restrict " : "";
440 const char *const ronly = (access & ACCESS_NON_WRITEABLE) ? "readonly " : "";
441 const char *const wonly = (access & ACCESS_NON_READABLE) ? "writeonly " : "";
442 fprintf(fp, "%s%s%s%s%s", coher, volat, restr, ronly, wonly);
443
444 #define FORMAT_CASE(x) case x: fprintf(stderr, #x " "); break
445 switch (var->data.image.format) {
446 FORMAT_CASE(GL_RGBA32F);
447 FORMAT_CASE(GL_RGBA32UI);
448 FORMAT_CASE(GL_RGBA32I);
449 FORMAT_CASE(GL_R32F);
450 FORMAT_CASE(GL_R32UI);
451 FORMAT_CASE(GL_R32I);
452 FORMAT_CASE(GL_RG32F);
453 FORMAT_CASE(GL_RG32UI);
454 FORMAT_CASE(GL_RG32I);
455 FORMAT_CASE(GL_R8);
456 FORMAT_CASE(GL_RG8);
457 FORMAT_CASE(GL_RGBA8);
458 FORMAT_CASE(GL_R8_SNORM);
459 FORMAT_CASE(GL_RG8_SNORM);
460 FORMAT_CASE(GL_RGBA8_SNORM);
461 FORMAT_CASE(GL_R16);
462 FORMAT_CASE(GL_RG16);
463 FORMAT_CASE(GL_RGBA16);
464 FORMAT_CASE(GL_R16_SNORM);
465 FORMAT_CASE(GL_RG16_SNORM);
466 FORMAT_CASE(GL_RGBA16_SNORM);
467 FORMAT_CASE(GL_R16F);
468 FORMAT_CASE(GL_RG16F);
469 FORMAT_CASE(GL_RGBA16F);
470 FORMAT_CASE(GL_R8UI);
471 FORMAT_CASE(GL_R8I);
472 FORMAT_CASE(GL_RG8UI);
473 FORMAT_CASE(GL_RG8I);
474 FORMAT_CASE(GL_RGBA8UI);
475 FORMAT_CASE(GL_RGBA8I);
476 FORMAT_CASE(GL_R16UI);
477 FORMAT_CASE(GL_R16I);
478 FORMAT_CASE(GL_RG16UI);
479 FORMAT_CASE(GL_RG16I);
480 FORMAT_CASE(GL_RGBA16UI);
481 FORMAT_CASE(GL_RGBA16I);
482 FORMAT_CASE(GL_R11F_G11F_B10F);
483 FORMAT_CASE(GL_RGB9_E5);
484 FORMAT_CASE(GL_RGB10_A2);
485 FORMAT_CASE(GL_RGB10_A2UI);
486 default: /* Including the normal GL_NONE */
487 break;
488 }
489 #undef FORMAT_CASE
490
491 fprintf(fp, "%s %s", glsl_get_type_name(var->type),
492 get_var_name(var, state));
493
494 if (var->data.mode == nir_var_shader_in ||
495 var->data.mode == nir_var_shader_out ||
496 var->data.mode == nir_var_uniform ||
497 var->data.mode == nir_var_shader_storage) {
498 const char *loc = NULL;
499 char buf[4];
500
501 switch (state->shader->info.stage) {
502 case MESA_SHADER_VERTEX:
503 if (var->data.mode == nir_var_shader_in)
504 loc = gl_vert_attrib_name(var->data.location);
505 else if (var->data.mode == nir_var_shader_out)
506 loc = gl_varying_slot_name(var->data.location);
507 break;
508 case MESA_SHADER_GEOMETRY:
509 if ((var->data.mode == nir_var_shader_in) ||
510 (var->data.mode == nir_var_shader_out))
511 loc = gl_varying_slot_name(var->data.location);
512 break;
513 case MESA_SHADER_FRAGMENT:
514 if (var->data.mode == nir_var_shader_in)
515 loc = gl_varying_slot_name(var->data.location);
516 else if (var->data.mode == nir_var_shader_out)
517 loc = gl_frag_result_name(var->data.location);
518 break;
519 case MESA_SHADER_TESS_CTRL:
520 case MESA_SHADER_TESS_EVAL:
521 case MESA_SHADER_COMPUTE:
522 default:
523 /* TODO */
524 break;
525 }
526
527 if (!loc) {
528 snprintf(buf, sizeof(buf), "%u", var->data.location);
529 loc = buf;
530 }
531
532 /* For shader I/O vars that have been split to components or packed,
533 * print the fractional location within the input/output.
534 */
535 unsigned int num_components =
536 glsl_get_components(glsl_without_array(var->type));
537 const char *components = NULL;
538 char components_local[6] = {'.' /* the rest is 0-filled */};
539 switch (var->data.mode) {
540 case nir_var_shader_in:
541 case nir_var_shader_out:
542 if (num_components < 4 && num_components != 0) {
543 const char *xyzw = "xyzw";
544 for (int i = 0; i < num_components; i++)
545 components_local[i + 1] = xyzw[i + var->data.location_frac];
546
547 components = components_local;
548 }
549 break;
550 default:
551 break;
552 }
553
554 fprintf(fp, " (%s%s, %u, %u)%s", loc,
555 components ? components : "",
556 var->data.driver_location, var->data.binding,
557 var->data.compact ? " compact" : "");
558 }
559
560 if (var->constant_initializer) {
561 fprintf(fp, " = { ");
562 print_constant(var->constant_initializer, var->type, state);
563 fprintf(fp, " }");
564 }
565
566 fprintf(fp, "\n");
567 print_annotation(state, var);
568 }
569
570 static void
571 print_deref_link(nir_deref_instr *instr, bool whole_chain, print_state *state)
572 {
573 FILE *fp = state->fp;
574
575 if (instr->deref_type == nir_deref_type_var) {
576 fprintf(fp, "%s", get_var_name(instr->var, state));
577 return;
578 } else if (instr->deref_type == nir_deref_type_cast) {
579 fprintf(fp, "(%s *)", glsl_get_type_name(instr->type));
580 print_src(&instr->parent, state);
581 return;
582 }
583
584 assert(instr->parent.is_ssa);
585 nir_deref_instr *parent =
586 nir_instr_as_deref(instr->parent.ssa->parent_instr);
587
588 /* Is the parent we're going to print a bare cast? */
589 const bool is_parent_cast =
590 whole_chain && parent->deref_type == nir_deref_type_cast;
591
592 /* If we're not printing the whole chain, the parent we print will be a SSA
593 * value that represents a pointer. The only deref type that naturally
594 * gives a pointer is a cast.
595 */
596 const bool is_parent_pointer =
597 !whole_chain || parent->deref_type == nir_deref_type_cast;
598
599 /* Struct derefs have a nice syntax that works on pointers, arrays derefs
600 * do not.
601 */
602 const bool need_deref =
603 is_parent_pointer && instr->deref_type != nir_deref_type_struct;
604
605 /* Cast need extra parens and so * dereferences */
606 if (is_parent_cast || need_deref)
607 fprintf(fp, "(");
608
609 if (need_deref)
610 fprintf(fp, "*");
611
612 if (whole_chain) {
613 print_deref_link(parent, whole_chain, state);
614 } else {
615 print_src(&instr->parent, state);
616 }
617
618 if (is_parent_cast || need_deref)
619 fprintf(fp, ")");
620
621 switch (instr->deref_type) {
622 case nir_deref_type_struct:
623 fprintf(fp, "%s%s", is_parent_pointer ? "->" : ".",
624 glsl_get_struct_elem_name(parent->type, instr->strct.index));
625 break;
626
627 case nir_deref_type_array: {
628 nir_const_value *const_index = nir_src_as_const_value(instr->arr.index);
629 if (const_index) {
630 fprintf(fp, "[%u]", const_index->u32[0]);
631 } else {
632 fprintf(fp, "[");
633 print_src(&instr->arr.index, state);
634 fprintf(fp, "]");
635 }
636 break;
637 }
638
639 case nir_deref_type_array_wildcard:
640 fprintf(fp, "[*]");
641 break;
642
643 default:
644 unreachable("Invalid deref instruction type");
645 }
646 }
647
648 static void
649 print_deref_instr(nir_deref_instr *instr, print_state *state)
650 {
651 FILE *fp = state->fp;
652
653 print_dest(&instr->dest, state);
654
655 switch (instr->deref_type) {
656 case nir_deref_type_var:
657 fprintf(fp, " = deref_var ");
658 break;
659 case nir_deref_type_array:
660 case nir_deref_type_array_wildcard:
661 fprintf(fp, " = deref_array ");
662 break;
663 case nir_deref_type_struct:
664 fprintf(fp, " = deref_struct ");
665 break;
666 case nir_deref_type_cast:
667 fprintf(fp, " = deref_cast ");
668 break;
669 default:
670 unreachable("Invalid deref instruction type");
671 }
672
673 /* Only casts naturally return a pointer type */
674 if (instr->deref_type != nir_deref_type_cast)
675 fprintf(fp, "&");
676
677 print_deref_link(instr, false, state);
678
679 fprintf(fp, " (%s %s) ",
680 get_variable_mode_str(instr->mode, true),
681 glsl_get_type_name(instr->type));
682
683 if (instr->deref_type != nir_deref_type_var &&
684 instr->deref_type != nir_deref_type_cast) {
685 /* Print the entire chain as a comment */
686 fprintf(fp, "/* &");
687 print_deref_link(instr, true, state);
688 fprintf(fp, " */");
689 }
690 }
691
692 static void
693 print_intrinsic_instr(nir_intrinsic_instr *instr, print_state *state)
694 {
695 const nir_intrinsic_info *info = &nir_intrinsic_infos[instr->intrinsic];
696 unsigned num_srcs = info->num_srcs;
697 FILE *fp = state->fp;
698
699 if (info->has_dest) {
700 print_dest(&instr->dest, state);
701 fprintf(fp, " = ");
702 }
703
704 fprintf(fp, "intrinsic %s (", info->name);
705
706 for (unsigned i = 0; i < num_srcs; i++) {
707 if (i != 0)
708 fprintf(fp, ", ");
709
710 print_src(&instr->src[i], state);
711 }
712
713 fprintf(fp, ") (");
714
715 for (unsigned i = 0; i < info->num_indices; i++) {
716 if (i != 0)
717 fprintf(fp, ", ");
718
719 fprintf(fp, "%d", instr->const_index[i]);
720 }
721
722 fprintf(fp, ")");
723
724 static const char *index_name[NIR_INTRINSIC_NUM_INDEX_FLAGS] = {
725 [NIR_INTRINSIC_BASE] = "base",
726 [NIR_INTRINSIC_WRMASK] = "wrmask",
727 [NIR_INTRINSIC_STREAM_ID] = "stream-id",
728 [NIR_INTRINSIC_UCP_ID] = "ucp-id",
729 [NIR_INTRINSIC_RANGE] = "range",
730 [NIR_INTRINSIC_DESC_SET] = "desc-set",
731 [NIR_INTRINSIC_BINDING] = "binding",
732 [NIR_INTRINSIC_COMPONENT] = "component",
733 [NIR_INTRINSIC_INTERP_MODE] = "interp_mode",
734 [NIR_INTRINSIC_REDUCTION_OP] = "reduction_op",
735 [NIR_INTRINSIC_CLUSTER_SIZE] = "cluster_size",
736 [NIR_INTRINSIC_PARAM_IDX] = "param_idx",
737 [NIR_INTRINSIC_IMAGE_DIM] = "image_dim",
738 [NIR_INTRINSIC_IMAGE_ARRAY] = "image_array",
739 [NIR_INTRINSIC_ACCESS] = "access",
740 [NIR_INTRINSIC_FORMAT] = "format",
741 [NIR_INTRINSIC_ALIGN_MUL] = "align_mul",
742 [NIR_INTRINSIC_ALIGN_OFFSET] = "align_offset",
743 };
744 for (unsigned idx = 1; idx < NIR_INTRINSIC_NUM_INDEX_FLAGS; idx++) {
745 if (!info->index_map[idx])
746 continue;
747 fprintf(fp, " /*");
748 if (idx == NIR_INTRINSIC_WRMASK) {
749 /* special case wrmask to show it as a writemask.. */
750 unsigned wrmask = nir_intrinsic_write_mask(instr);
751 fprintf(fp, " wrmask=");
752 for (unsigned i = 0; i < 4; i++)
753 if ((wrmask >> i) & 1)
754 fprintf(fp, "%c", "xyzw"[i]);
755 } else if (idx == NIR_INTRINSIC_REDUCTION_OP) {
756 nir_op reduction_op = nir_intrinsic_reduction_op(instr);
757 fprintf(fp, " reduction_op=%s", nir_op_infos[reduction_op].name);
758 } else if (idx == NIR_INTRINSIC_IMAGE_DIM) {
759 static const char *dim_name[] = {
760 [GLSL_SAMPLER_DIM_1D] = "1D",
761 [GLSL_SAMPLER_DIM_2D] = "2D",
762 [GLSL_SAMPLER_DIM_3D] = "3D",
763 [GLSL_SAMPLER_DIM_CUBE] = "Cube",
764 [GLSL_SAMPLER_DIM_RECT] = "Rect",
765 [GLSL_SAMPLER_DIM_BUF] = "Buf",
766 [GLSL_SAMPLER_DIM_MS] = "2D-MSAA",
767 [GLSL_SAMPLER_DIM_SUBPASS] = "Subpass",
768 [GLSL_SAMPLER_DIM_SUBPASS_MS] = "Subpass-MSAA",
769 };
770 enum glsl_sampler_dim dim = nir_intrinsic_image_dim(instr);
771 assert(dim < ARRAY_SIZE(dim_name) && dim_name[dim]);
772 fprintf(fp, " image_dim=%s", dim_name[dim]);
773 } else if (idx == NIR_INTRINSIC_IMAGE_ARRAY) {
774 bool array = nir_intrinsic_image_dim(instr);
775 fprintf(fp, " image_dim=%s", array ? "true" : "false");
776 } else {
777 unsigned off = info->index_map[idx] - 1;
778 assert(index_name[idx]); /* forgot to update index_name table? */
779 fprintf(fp, " %s=%d", index_name[idx], instr->const_index[off]);
780 }
781 fprintf(fp, " */");
782 }
783
784 if (!state->shader)
785 return;
786
787 struct exec_list *var_list = NULL;
788
789 switch (instr->intrinsic) {
790 case nir_intrinsic_load_uniform:
791 var_list = &state->shader->uniforms;
792 break;
793 case nir_intrinsic_load_input:
794 case nir_intrinsic_load_per_vertex_input:
795 var_list = &state->shader->inputs;
796 break;
797 case nir_intrinsic_load_output:
798 case nir_intrinsic_store_output:
799 case nir_intrinsic_store_per_vertex_output:
800 var_list = &state->shader->outputs;
801 break;
802 default:
803 return;
804 }
805
806 nir_foreach_variable(var, var_list) {
807 if ((var->data.driver_location == nir_intrinsic_base(instr)) &&
808 (instr->intrinsic == nir_intrinsic_load_uniform ||
809 var->data.location_frac == nir_intrinsic_component(instr)) &&
810 var->name) {
811 fprintf(fp, "\t/* %s */", var->name);
812 break;
813 }
814 }
815 }
816
817 static void
818 print_tex_instr(nir_tex_instr *instr, print_state *state)
819 {
820 FILE *fp = state->fp;
821
822 print_dest(&instr->dest, state);
823
824 fprintf(fp, " = ");
825
826 switch (instr->op) {
827 case nir_texop_tex:
828 fprintf(fp, "tex ");
829 break;
830 case nir_texop_txb:
831 fprintf(fp, "txb ");
832 break;
833 case nir_texop_txl:
834 fprintf(fp, "txl ");
835 break;
836 case nir_texop_txd:
837 fprintf(fp, "txd ");
838 break;
839 case nir_texop_txf:
840 fprintf(fp, "txf ");
841 break;
842 case nir_texop_txf_ms:
843 fprintf(fp, "txf_ms ");
844 break;
845 case nir_texop_txf_ms_mcs:
846 fprintf(fp, "txf_ms_mcs ");
847 break;
848 case nir_texop_txs:
849 fprintf(fp, "txs ");
850 break;
851 case nir_texop_lod:
852 fprintf(fp, "lod ");
853 break;
854 case nir_texop_tg4:
855 fprintf(fp, "tg4 ");
856 break;
857 case nir_texop_query_levels:
858 fprintf(fp, "query_levels ");
859 break;
860 case nir_texop_texture_samples:
861 fprintf(fp, "texture_samples ");
862 break;
863 case nir_texop_samples_identical:
864 fprintf(fp, "samples_identical ");
865 break;
866 default:
867 unreachable("Invalid texture operation");
868 break;
869 }
870
871 bool has_texture_deref = false, has_sampler_deref = false;
872 for (unsigned i = 0; i < instr->num_srcs; i++) {
873 print_src(&instr->src[i].src, state);
874
875 fprintf(fp, " ");
876
877 switch(instr->src[i].src_type) {
878 case nir_tex_src_coord:
879 fprintf(fp, "(coord)");
880 break;
881 case nir_tex_src_projector:
882 fprintf(fp, "(projector)");
883 break;
884 case nir_tex_src_comparator:
885 fprintf(fp, "(comparator)");
886 break;
887 case nir_tex_src_offset:
888 fprintf(fp, "(offset)");
889 break;
890 case nir_tex_src_bias:
891 fprintf(fp, "(bias)");
892 break;
893 case nir_tex_src_lod:
894 fprintf(fp, "(lod)");
895 break;
896 case nir_tex_src_min_lod:
897 fprintf(fp, "(min_lod)");
898 break;
899 case nir_tex_src_ms_index:
900 fprintf(fp, "(ms_index)");
901 break;
902 case nir_tex_src_ms_mcs:
903 fprintf(fp, "(ms_mcs)");
904 break;
905 case nir_tex_src_ddx:
906 fprintf(fp, "(ddx)");
907 break;
908 case nir_tex_src_ddy:
909 fprintf(fp, "(ddy)");
910 break;
911 case nir_tex_src_texture_deref:
912 has_texture_deref = true;
913 fprintf(fp, "(texture_deref)");
914 break;
915 case nir_tex_src_sampler_deref:
916 has_sampler_deref = true;
917 fprintf(fp, "(sampler_deref)");
918 break;
919 case nir_tex_src_texture_offset:
920 fprintf(fp, "(texture_offset)");
921 break;
922 case nir_tex_src_sampler_offset:
923 fprintf(fp, "(sampler_offset)");
924 break;
925 case nir_tex_src_plane:
926 fprintf(fp, "(plane)");
927 break;
928
929 default:
930 unreachable("Invalid texture source type");
931 break;
932 }
933
934 fprintf(fp, ", ");
935 }
936
937 if (instr->op == nir_texop_tg4) {
938 fprintf(fp, "%u (gather_component), ", instr->component);
939 }
940
941 if (!has_texture_deref) {
942 fprintf(fp, "%u (texture), ", instr->texture_index);
943 }
944
945 if (!has_sampler_deref) {
946 fprintf(fp, "%u (sampler), ", instr->sampler_index);
947 }
948 }
949
950 static void
951 print_call_instr(nir_call_instr *instr, print_state *state)
952 {
953 FILE *fp = state->fp;
954
955 fprintf(fp, "call %s ", instr->callee->name);
956
957 for (unsigned i = 0; i < instr->num_params; i++) {
958 if (i != 0)
959 fprintf(fp, ", ");
960
961 print_src(&instr->params[i], state);
962 }
963 }
964
965 static void
966 print_load_const_instr(nir_load_const_instr *instr, print_state *state)
967 {
968 FILE *fp = state->fp;
969
970 print_ssa_def(&instr->def, state);
971
972 fprintf(fp, " = load_const (");
973
974 for (unsigned i = 0; i < instr->def.num_components; i++) {
975 if (i != 0)
976 fprintf(fp, ", ");
977
978 /*
979 * we don't really know the type of the constant (if it will be used as a
980 * float or an int), so just print the raw constant in hex for fidelity
981 * and then print the float in a comment for readability.
982 */
983
984 switch (instr->def.bit_size) {
985 case 64:
986 fprintf(fp, "0x%16" PRIx64 " /* %f */", instr->value.u64[i],
987 instr->value.f64[i]);
988 break;
989 case 32:
990 fprintf(fp, "0x%08x /* %f */", instr->value.u32[i], instr->value.f32[i]);
991 break;
992 case 16:
993 fprintf(fp, "0x%04x /* %f */", instr->value.u16[i],
994 _mesa_half_to_float(instr->value.u16[i]));
995 break;
996 case 8:
997 fprintf(fp, "0x%02x", instr->value.u8[i]);
998 break;
999 case 1:
1000 fprintf(fp, "%s", instr->value.b[i] ? "true" : "false");
1001 break;
1002 }
1003 }
1004
1005 fprintf(fp, ")");
1006 }
1007
1008 static void
1009 print_jump_instr(nir_jump_instr *instr, print_state *state)
1010 {
1011 FILE *fp = state->fp;
1012
1013 switch (instr->type) {
1014 case nir_jump_break:
1015 fprintf(fp, "break");
1016 break;
1017
1018 case nir_jump_continue:
1019 fprintf(fp, "continue");
1020 break;
1021
1022 case nir_jump_return:
1023 fprintf(fp, "return");
1024 break;
1025 }
1026 }
1027
1028 static void
1029 print_ssa_undef_instr(nir_ssa_undef_instr* instr, print_state *state)
1030 {
1031 FILE *fp = state->fp;
1032 print_ssa_def(&instr->def, state);
1033 fprintf(fp, " = undefined");
1034 }
1035
1036 static void
1037 print_phi_instr(nir_phi_instr *instr, print_state *state)
1038 {
1039 FILE *fp = state->fp;
1040 print_dest(&instr->dest, state);
1041 fprintf(fp, " = phi ");
1042 nir_foreach_phi_src(src, instr) {
1043 if (&src->node != exec_list_get_head(&instr->srcs))
1044 fprintf(fp, ", ");
1045
1046 fprintf(fp, "block_%u: ", src->pred->index);
1047 print_src(&src->src, state);
1048 }
1049 }
1050
1051 static void
1052 print_parallel_copy_instr(nir_parallel_copy_instr *instr, print_state *state)
1053 {
1054 FILE *fp = state->fp;
1055 nir_foreach_parallel_copy_entry(entry, instr) {
1056 if (&entry->node != exec_list_get_head(&instr->entries))
1057 fprintf(fp, "; ");
1058
1059 print_dest(&entry->dest, state);
1060 fprintf(fp, " = ");
1061 print_src(&entry->src, state);
1062 }
1063 }
1064
1065 static void
1066 print_instr(const nir_instr *instr, print_state *state, unsigned tabs)
1067 {
1068 FILE *fp = state->fp;
1069 print_tabs(tabs, fp);
1070
1071 switch (instr->type) {
1072 case nir_instr_type_alu:
1073 print_alu_instr(nir_instr_as_alu(instr), state);
1074 break;
1075
1076 case nir_instr_type_deref:
1077 print_deref_instr(nir_instr_as_deref(instr), state);
1078 break;
1079
1080 case nir_instr_type_call:
1081 print_call_instr(nir_instr_as_call(instr), state);
1082 break;
1083
1084 case nir_instr_type_intrinsic:
1085 print_intrinsic_instr(nir_instr_as_intrinsic(instr), state);
1086 break;
1087
1088 case nir_instr_type_tex:
1089 print_tex_instr(nir_instr_as_tex(instr), state);
1090 break;
1091
1092 case nir_instr_type_load_const:
1093 print_load_const_instr(nir_instr_as_load_const(instr), state);
1094 break;
1095
1096 case nir_instr_type_jump:
1097 print_jump_instr(nir_instr_as_jump(instr), state);
1098 break;
1099
1100 case nir_instr_type_ssa_undef:
1101 print_ssa_undef_instr(nir_instr_as_ssa_undef(instr), state);
1102 break;
1103
1104 case nir_instr_type_phi:
1105 print_phi_instr(nir_instr_as_phi(instr), state);
1106 break;
1107
1108 case nir_instr_type_parallel_copy:
1109 print_parallel_copy_instr(nir_instr_as_parallel_copy(instr), state);
1110 break;
1111
1112 default:
1113 unreachable("Invalid instruction type");
1114 break;
1115 }
1116 }
1117
1118 static int
1119 compare_block_index(const void *p1, const void *p2)
1120 {
1121 const nir_block *block1 = *((const nir_block **) p1);
1122 const nir_block *block2 = *((const nir_block **) p2);
1123
1124 return (int) block1->index - (int) block2->index;
1125 }
1126
1127 static void print_cf_node(nir_cf_node *node, print_state *state,
1128 unsigned tabs);
1129
1130 static void
1131 print_block(nir_block *block, print_state *state, unsigned tabs)
1132 {
1133 FILE *fp = state->fp;
1134
1135 print_tabs(tabs, fp);
1136 fprintf(fp, "block block_%u:\n", block->index);
1137
1138 /* sort the predecessors by index so we consistently print the same thing */
1139
1140 nir_block **preds =
1141 malloc(block->predecessors->entries * sizeof(nir_block *));
1142
1143 unsigned i = 0;
1144 set_foreach(block->predecessors, entry) {
1145 preds[i++] = (nir_block *) entry->key;
1146 }
1147
1148 qsort(preds, block->predecessors->entries, sizeof(nir_block *),
1149 compare_block_index);
1150
1151 print_tabs(tabs, fp);
1152 fprintf(fp, "/* preds: ");
1153 for (unsigned i = 0; i < block->predecessors->entries; i++) {
1154 fprintf(fp, "block_%u ", preds[i]->index);
1155 }
1156 fprintf(fp, "*/\n");
1157
1158 free(preds);
1159
1160 nir_foreach_instr(instr, block) {
1161 print_instr(instr, state, tabs);
1162 fprintf(fp, "\n");
1163 print_annotation(state, instr);
1164 }
1165
1166 print_tabs(tabs, fp);
1167 fprintf(fp, "/* succs: ");
1168 for (unsigned i = 0; i < 2; i++)
1169 if (block->successors[i]) {
1170 fprintf(fp, "block_%u ", block->successors[i]->index);
1171 }
1172 fprintf(fp, "*/\n");
1173 }
1174
1175 static void
1176 print_if(nir_if *if_stmt, print_state *state, unsigned tabs)
1177 {
1178 FILE *fp = state->fp;
1179
1180 print_tabs(tabs, fp);
1181 fprintf(fp, "if ");
1182 print_src(&if_stmt->condition, state);
1183 fprintf(fp, " {\n");
1184 foreach_list_typed(nir_cf_node, node, node, &if_stmt->then_list) {
1185 print_cf_node(node, state, tabs + 1);
1186 }
1187 print_tabs(tabs, fp);
1188 fprintf(fp, "} else {\n");
1189 foreach_list_typed(nir_cf_node, node, node, &if_stmt->else_list) {
1190 print_cf_node(node, state, tabs + 1);
1191 }
1192 print_tabs(tabs, fp);
1193 fprintf(fp, "}\n");
1194 }
1195
1196 static void
1197 print_loop(nir_loop *loop, print_state *state, unsigned tabs)
1198 {
1199 FILE *fp = state->fp;
1200
1201 print_tabs(tabs, fp);
1202 fprintf(fp, "loop {\n");
1203 foreach_list_typed(nir_cf_node, node, node, &loop->body) {
1204 print_cf_node(node, state, tabs + 1);
1205 }
1206 print_tabs(tabs, fp);
1207 fprintf(fp, "}\n");
1208 }
1209
1210 static void
1211 print_cf_node(nir_cf_node *node, print_state *state, unsigned int tabs)
1212 {
1213 switch (node->type) {
1214 case nir_cf_node_block:
1215 print_block(nir_cf_node_as_block(node), state, tabs);
1216 break;
1217
1218 case nir_cf_node_if:
1219 print_if(nir_cf_node_as_if(node), state, tabs);
1220 break;
1221
1222 case nir_cf_node_loop:
1223 print_loop(nir_cf_node_as_loop(node), state, tabs);
1224 break;
1225
1226 default:
1227 unreachable("Invalid CFG node type");
1228 }
1229 }
1230
1231 static void
1232 print_function_impl(nir_function_impl *impl, print_state *state)
1233 {
1234 FILE *fp = state->fp;
1235
1236 fprintf(fp, "\nimpl %s ", impl->function->name);
1237
1238 fprintf(fp, "{\n");
1239
1240 nir_foreach_variable(var, &impl->locals) {
1241 fprintf(fp, "\t");
1242 print_var_decl(var, state);
1243 }
1244
1245 foreach_list_typed(nir_register, reg, node, &impl->registers) {
1246 fprintf(fp, "\t");
1247 print_register_decl(reg, state);
1248 }
1249
1250 nir_index_blocks(impl);
1251
1252 foreach_list_typed(nir_cf_node, node, node, &impl->body) {
1253 print_cf_node(node, state, 1);
1254 }
1255
1256 fprintf(fp, "\tblock block_%u:\n}\n\n", impl->end_block->index);
1257 }
1258
1259 static void
1260 print_function(nir_function *function, print_state *state)
1261 {
1262 FILE *fp = state->fp;
1263
1264 fprintf(fp, "decl_function %s (%d params)", function->name,
1265 function->num_params);
1266
1267 fprintf(fp, "\n");
1268
1269 if (function->impl != NULL) {
1270 print_function_impl(function->impl, state);
1271 return;
1272 }
1273 }
1274
1275 static void
1276 init_print_state(print_state *state, nir_shader *shader, FILE *fp)
1277 {
1278 state->fp = fp;
1279 state->shader = shader;
1280 state->ht = _mesa_hash_table_create(NULL, _mesa_hash_pointer,
1281 _mesa_key_pointer_equal);
1282 state->syms = _mesa_set_create(NULL, _mesa_key_hash_string,
1283 _mesa_key_string_equal);
1284 state->index = 0;
1285 }
1286
1287 static void
1288 destroy_print_state(print_state *state)
1289 {
1290 _mesa_hash_table_destroy(state->ht, NULL);
1291 _mesa_set_destroy(state->syms, NULL);
1292 }
1293
1294 void
1295 nir_print_shader_annotated(nir_shader *shader, FILE *fp,
1296 struct hash_table *annotations)
1297 {
1298 print_state state;
1299 init_print_state(&state, shader, fp);
1300
1301 state.annotations = annotations;
1302
1303 fprintf(fp, "shader: %s\n", gl_shader_stage_name(shader->info.stage));
1304
1305 if (shader->info.name)
1306 fprintf(fp, "name: %s\n", shader->info.name);
1307
1308 if (shader->info.label)
1309 fprintf(fp, "label: %s\n", shader->info.label);
1310
1311 switch (shader->info.stage) {
1312 case MESA_SHADER_COMPUTE:
1313 fprintf(fp, "local-size: %u, %u, %u%s\n",
1314 shader->info.cs.local_size[0],
1315 shader->info.cs.local_size[1],
1316 shader->info.cs.local_size[2],
1317 shader->info.cs.local_size_variable ? " (variable)" : "");
1318 fprintf(fp, "shared-size: %u\n", shader->info.cs.shared_size);
1319 break;
1320 default:
1321 break;
1322 }
1323
1324 fprintf(fp, "inputs: %u\n", shader->num_inputs);
1325 fprintf(fp, "outputs: %u\n", shader->num_outputs);
1326 fprintf(fp, "uniforms: %u\n", shader->num_uniforms);
1327 fprintf(fp, "shared: %u\n", shader->num_shared);
1328
1329 nir_foreach_variable(var, &shader->uniforms) {
1330 print_var_decl(var, &state);
1331 }
1332
1333 nir_foreach_variable(var, &shader->inputs) {
1334 print_var_decl(var, &state);
1335 }
1336
1337 nir_foreach_variable(var, &shader->outputs) {
1338 print_var_decl(var, &state);
1339 }
1340
1341 nir_foreach_variable(var, &shader->shared) {
1342 print_var_decl(var, &state);
1343 }
1344
1345 nir_foreach_variable(var, &shader->globals) {
1346 print_var_decl(var, &state);
1347 }
1348
1349 nir_foreach_variable(var, &shader->system_values) {
1350 print_var_decl(var, &state);
1351 }
1352
1353 foreach_list_typed(nir_register, reg, node, &shader->registers) {
1354 print_register_decl(reg, &state);
1355 }
1356
1357 foreach_list_typed(nir_function, func, node, &shader->functions) {
1358 print_function(func, &state);
1359 }
1360
1361 destroy_print_state(&state);
1362 }
1363
1364 void
1365 nir_print_shader(nir_shader *shader, FILE *fp)
1366 {
1367 nir_print_shader_annotated(shader, fp, NULL);
1368 fflush(fp);
1369 }
1370
1371 void
1372 nir_print_instr(const nir_instr *instr, FILE *fp)
1373 {
1374 print_state state = {
1375 .fp = fp,
1376 };
1377 print_instr(instr, &state, 0);
1378
1379 }