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