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