glsl_types: vec8/vec16 support
[mesa.git] / src / compiler / nir / nir_print.c
1 /*
2 * Copyright © 2014 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 *
23 * Authors:
24 * Connor Abbott (cwabbott0@gmail.com)
25 *
26 */
27
28 #include "nir.h"
29 #include "compiler/shader_enums.h"
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <inttypes.h> /* for PRIx64 macro */
33
34 static void
35 print_tabs(unsigned num_tabs, FILE *fp)
36 {
37 for (unsigned i = 0; i < num_tabs; i++)
38 fprintf(fp, "\t");
39 }
40
41 typedef struct {
42 FILE *fp;
43 nir_shader *shader;
44 /** map from nir_variable -> printable name */
45 struct hash_table *ht;
46
47 /** set of names used so far for nir_variables */
48 struct set *syms;
49
50 /* an index used to make new non-conflicting names */
51 unsigned index;
52
53 /**
54 * Optional table of annotations mapping nir object
55 * (such as instr or var) to message to print.
56 */
57 struct hash_table *annotations;
58 } print_state;
59
60 static void
61 print_annotation(print_state *state, void *obj)
62 {
63 if (!state->annotations)
64 return;
65
66 struct hash_entry *entry = _mesa_hash_table_search(state->annotations, obj);
67 if (!entry)
68 return;
69
70 const char *note = entry->data;
71 _mesa_hash_table_remove(state->annotations, entry);
72
73 fprintf(stderr, "%s\n\n", note);
74 }
75
76 static void
77 print_register(nir_register *reg, print_state *state)
78 {
79 FILE *fp = state->fp;
80 if (reg->name != NULL)
81 fprintf(fp, "/* %s */ ", reg->name);
82 if (reg->is_global)
83 fprintf(fp, "gr%u", reg->index);
84 else
85 fprintf(fp, "r%u", reg->index);
86 }
87
88 static const char *sizes[] = { "error", "vec1", "vec2", "vec3", "vec4",
89 "error", "error", "error", "vec8",
90 "error", "error", "error", "vec16"};
91
92 static void
93 print_register_decl(nir_register *reg, print_state *state)
94 {
95 FILE *fp = state->fp;
96 fprintf(fp, "decl_reg %s %u ", sizes[reg->num_components], reg->bit_size);
97 if (reg->is_packed)
98 fprintf(fp, "(packed) ");
99 print_register(reg, state);
100 if (reg->num_array_elems != 0)
101 fprintf(fp, "[%u]", reg->num_array_elems);
102 fprintf(fp, "\n");
103 }
104
105 static void
106 print_ssa_def(nir_ssa_def *def, print_state *state)
107 {
108 FILE *fp = state->fp;
109 if (def->name != NULL)
110 fprintf(fp, "/* %s */ ", def->name);
111 fprintf(fp, "%s %u ssa_%u", sizes[def->num_components], def->bit_size,
112 def->index);
113 }
114
115 static void
116 print_ssa_use(nir_ssa_def *def, print_state *state)
117 {
118 FILE *fp = state->fp;
119 if (def->name != NULL)
120 fprintf(fp, "/* %s */ ", def->name);
121 fprintf(fp, "ssa_%u", def->index);
122 }
123
124 static void print_src(nir_src *src, print_state *state);
125
126 static void
127 print_reg_src(nir_reg_src *src, print_state *state)
128 {
129 FILE *fp = state->fp;
130 print_register(src->reg, state);
131 if (src->reg->num_array_elems != 0) {
132 fprintf(fp, "[%u", src->base_offset);
133 if (src->indirect != NULL) {
134 fprintf(fp, " + ");
135 print_src(src->indirect, state);
136 }
137 fprintf(fp, "]");
138 }
139 }
140
141 static void
142 print_reg_dest(nir_reg_dest *dest, print_state *state)
143 {
144 FILE *fp = state->fp;
145 print_register(dest->reg, state);
146 if (dest->reg->num_array_elems != 0) {
147 fprintf(fp, "[%u", dest->base_offset);
148 if (dest->indirect != NULL) {
149 fprintf(fp, " + ");
150 print_src(dest->indirect, state);
151 }
152 fprintf(fp, "]");
153 }
154 }
155
156 static void
157 print_src(nir_src *src, print_state *state)
158 {
159 if (src->is_ssa)
160 print_ssa_use(src->ssa, state);
161 else
162 print_reg_src(&src->reg, state);
163 }
164
165 static void
166 print_dest(nir_dest *dest, print_state *state)
167 {
168 if (dest->is_ssa)
169 print_ssa_def(&dest->ssa, state);
170 else
171 print_reg_dest(&dest->reg, state);
172 }
173
174 static void
175 print_alu_src(nir_alu_instr *instr, unsigned src, print_state *state)
176 {
177 FILE *fp = state->fp;
178
179 if (instr->src[src].negate)
180 fprintf(fp, "-");
181 if (instr->src[src].abs)
182 fprintf(fp, "abs(");
183
184 print_src(&instr->src[src].src, state);
185
186 bool print_swizzle = false;
187 unsigned used_channels = 0;
188
189 for (unsigned i = 0; i < 4; i++) {
190 if (!nir_alu_instr_channel_used(instr, src, i))
191 continue;
192
193 used_channels++;
194
195 if (instr->src[src].swizzle[i] != i) {
196 print_swizzle = true;
197 break;
198 }
199 }
200
201 unsigned live_channels = instr->src[src].src.is_ssa
202 ? instr->src[src].src.ssa->num_components
203 : instr->src[src].src.reg.reg->num_components;
204
205 if (print_swizzle || used_channels != live_channels) {
206 fprintf(fp, ".");
207 for (unsigned i = 0; i < 4; 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 < 4; 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_UINT:
302 case GLSL_TYPE_INT:
303 case GLSL_TYPE_BOOL:
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%08x", c->values[0].u32[i]);
310 }
311 break;
312
313 case GLSL_TYPE_FLOAT:
314 for (i = 0; i < cols; i++) {
315 for (j = 0; j < rows; j++) {
316 if (i + j > 0) fprintf(fp, ", ");
317 fprintf(fp, "%f", c->values[i].f32[j]);
318 }
319 }
320 break;
321
322 case GLSL_TYPE_DOUBLE:
323 for (i = 0; i < cols; i++) {
324 for (j = 0; j < rows; j++) {
325 if (i + j > 0) fprintf(fp, ", ");
326 fprintf(fp, "%f", c->values[i].f64[j]);
327 }
328 }
329 break;
330
331 case GLSL_TYPE_UINT64:
332 case GLSL_TYPE_INT64:
333 /* Only float base types can be matrices. */
334 assert(cols == 1);
335
336 for (i = 0; i < cols; i++) {
337 if (i > 0) fprintf(fp, ", ");
338 fprintf(fp, "0x%08" PRIx64, c->values[0].u64[i]);
339 }
340 break;
341
342 case GLSL_TYPE_STRUCT:
343 for (i = 0; i < c->num_elements; i++) {
344 if (i > 0) fprintf(fp, ", ");
345 fprintf(fp, "{ ");
346 print_constant(c->elements[i], glsl_get_struct_field(type, i), state);
347 fprintf(fp, " }");
348 }
349 break;
350
351 case GLSL_TYPE_ARRAY:
352 for (i = 0; i < c->num_elements; i++) {
353 if (i > 0) fprintf(fp, ", ");
354 fprintf(fp, "{ ");
355 print_constant(c->elements[i], glsl_get_array_element(type), state);
356 fprintf(fp, " }");
357 }
358 break;
359
360 default:
361 unreachable("not reached");
362 }
363 }
364
365 static const char *
366 get_variable_mode_str(nir_variable_mode mode)
367 {
368 switch (mode) {
369 case nir_var_shader_in:
370 return "shader_in";
371 case nir_var_shader_out:
372 return "shader_out";
373 case nir_var_uniform:
374 return "uniform";
375 case nir_var_shader_storage:
376 return "shader_storage";
377 case nir_var_system_value:
378 return "system";
379 case nir_var_shared:
380 return "shared";
381 case nir_var_param:
382 case nir_var_global:
383 case nir_var_local:
384 default:
385 return "";
386 }
387 }
388
389 static void
390 print_var_decl(nir_variable *var, print_state *state)
391 {
392 FILE *fp = state->fp;
393
394 fprintf(fp, "decl_var ");
395
396 const char *const cent = (var->data.centroid) ? "centroid " : "";
397 const char *const samp = (var->data.sample) ? "sample " : "";
398 const char *const patch = (var->data.patch) ? "patch " : "";
399 const char *const inv = (var->data.invariant) ? "invariant " : "";
400 fprintf(fp, "%s%s%s%s%s %s ",
401 cent, samp, patch, inv, get_variable_mode_str(var->data.mode),
402 glsl_interp_mode_name(var->data.interpolation));
403
404 const char *const coher = (var->data.image.coherent) ? "coherent " : "";
405 const char *const volat = (var->data.image._volatile) ? "volatile " : "";
406 const char *const restr = (var->data.image.restrict_flag) ? "restrict " : "";
407 const char *const ronly = (var->data.image.read_only) ? "readonly " : "";
408 const char *const wonly = (var->data.image.write_only) ? "writeonly " : "";
409 fprintf(fp, "%s%s%s%s%s", coher, volat, restr, ronly, wonly);
410
411 fprintf(fp, "%s %s", glsl_get_type_name(var->type),
412 get_var_name(var, state));
413
414 if (var->data.mode == nir_var_shader_in ||
415 var->data.mode == nir_var_shader_out ||
416 var->data.mode == nir_var_uniform ||
417 var->data.mode == nir_var_shader_storage) {
418 const char *loc = NULL;
419 char buf[4];
420
421 switch (state->shader->info.stage) {
422 case MESA_SHADER_VERTEX:
423 if (var->data.mode == nir_var_shader_in)
424 loc = gl_vert_attrib_name(var->data.location);
425 else if (var->data.mode == nir_var_shader_out)
426 loc = gl_varying_slot_name(var->data.location);
427 break;
428 case MESA_SHADER_GEOMETRY:
429 if ((var->data.mode == nir_var_shader_in) ||
430 (var->data.mode == nir_var_shader_out))
431 loc = gl_varying_slot_name(var->data.location);
432 break;
433 case MESA_SHADER_FRAGMENT:
434 if (var->data.mode == nir_var_shader_in)
435 loc = gl_varying_slot_name(var->data.location);
436 else if (var->data.mode == nir_var_shader_out)
437 loc = gl_frag_result_name(var->data.location);
438 break;
439 case MESA_SHADER_TESS_CTRL:
440 case MESA_SHADER_TESS_EVAL:
441 case MESA_SHADER_COMPUTE:
442 default:
443 /* TODO */
444 break;
445 }
446
447 if (!loc) {
448 snprintf(buf, sizeof(buf), "%u", var->data.location);
449 loc = buf;
450 }
451
452 /* For shader I/O vars that have been split to components or packed,
453 * print the fractional location within the input/output.
454 */
455 unsigned int num_components =
456 glsl_get_components(glsl_without_array(var->type));
457 const char *components = NULL;
458 char components_local[6] = {'.' /* the rest is 0-filled */};
459 switch (var->data.mode) {
460 case nir_var_shader_in:
461 case nir_var_shader_out:
462 if (num_components < 4 && num_components != 0) {
463 const char *xyzw = "xyzw";
464 for (int i = 0; i < num_components; i++)
465 components_local[i + 1] = xyzw[i + var->data.location_frac];
466
467 components = components_local;
468 }
469 break;
470 default:
471 break;
472 }
473
474 fprintf(fp, " (%s%s, %u, %u)%s", loc,
475 components ? components : "",
476 var->data.driver_location, var->data.binding,
477 var->data.compact ? " compact" : "");
478 }
479
480 if (var->constant_initializer) {
481 fprintf(fp, " = { ");
482 print_constant(var->constant_initializer, var->type, state);
483 fprintf(fp, " }");
484 }
485
486 fprintf(fp, "\n");
487 print_annotation(state, var);
488 }
489
490 static void
491 print_var(nir_variable *var, print_state *state)
492 {
493 FILE *fp = state->fp;
494 fprintf(fp, "%s", get_var_name(var, state));
495 }
496
497 static void
498 print_arg(nir_variable *var, print_state *state)
499 {
500 FILE *fp = state->fp;
501 fprintf(fp, "%s %s", glsl_get_type_name(var->type),
502 get_var_name(var, state));
503 }
504
505 static void
506 print_deref_var(nir_deref_var *deref, print_state *state)
507 {
508 print_var(deref->var, state);
509 }
510
511 static void
512 print_deref_array(nir_deref_array *deref, print_state *state)
513 {
514 FILE *fp = state->fp;
515 fprintf(fp, "[");
516 switch (deref->deref_array_type) {
517 case nir_deref_array_type_direct:
518 fprintf(fp, "%u", deref->base_offset);
519 break;
520 case nir_deref_array_type_indirect:
521 if (deref->base_offset != 0)
522 fprintf(fp, "%u + ", deref->base_offset);
523 print_src(&deref->indirect, state);
524 break;
525 case nir_deref_array_type_wildcard:
526 fprintf(fp, "*");
527 break;
528 }
529 fprintf(fp, "]");
530 }
531
532 static void
533 print_deref_struct(nir_deref_struct *deref, const struct glsl_type *parent_type,
534 print_state *state)
535 {
536 FILE *fp = state->fp;
537 fprintf(fp, ".%s", glsl_get_struct_elem_name(parent_type, deref->index));
538 }
539
540 static void
541 print_deref(nir_deref_var *deref, print_state *state)
542 {
543 nir_deref *tail = &deref->deref;
544 nir_deref *pretail = NULL;
545 while (tail != NULL) {
546 switch (tail->deref_type) {
547 case nir_deref_type_var:
548 assert(pretail == NULL);
549 assert(tail == &deref->deref);
550 print_deref_var(deref, state);
551 break;
552
553 case nir_deref_type_array:
554 assert(pretail != NULL);
555 print_deref_array(nir_deref_as_array(tail), state);
556 break;
557
558 case nir_deref_type_struct:
559 assert(pretail != NULL);
560 print_deref_struct(nir_deref_as_struct(tail),
561 pretail->type, state);
562 break;
563
564 default:
565 unreachable("Invalid deref type");
566 }
567
568 pretail = tail;
569 tail = pretail->child;
570 }
571 }
572
573 static void
574 print_intrinsic_instr(nir_intrinsic_instr *instr, print_state *state)
575 {
576 const nir_intrinsic_info *info = &nir_intrinsic_infos[instr->intrinsic];
577 unsigned num_srcs = info->num_srcs;
578 FILE *fp = state->fp;
579
580 if (info->has_dest) {
581 print_dest(&instr->dest, state);
582 fprintf(fp, " = ");
583 }
584
585 fprintf(fp, "intrinsic %s (", info->name);
586
587 for (unsigned i = 0; i < num_srcs; i++) {
588 if (i != 0)
589 fprintf(fp, ", ");
590
591 print_src(&instr->src[i], state);
592 }
593
594 fprintf(fp, ") (");
595
596 for (unsigned i = 0; i < info->num_variables; i++) {
597 if (i != 0)
598 fprintf(fp, ", ");
599
600 print_deref(instr->variables[i], state);
601 }
602
603 fprintf(fp, ") (");
604
605 for (unsigned i = 0; i < info->num_indices; i++) {
606 if (i != 0)
607 fprintf(fp, ", ");
608
609 fprintf(fp, "%d", instr->const_index[i]);
610 }
611
612 fprintf(fp, ")");
613
614 static const char *index_name[NIR_INTRINSIC_NUM_INDEX_FLAGS] = {
615 [NIR_INTRINSIC_BASE] = "base",
616 [NIR_INTRINSIC_WRMASK] = "wrmask",
617 [NIR_INTRINSIC_STREAM_ID] = "stream-id",
618 [NIR_INTRINSIC_UCP_ID] = "ucp-id",
619 [NIR_INTRINSIC_RANGE] = "range",
620 [NIR_INTRINSIC_DESC_SET] = "desc-set",
621 [NIR_INTRINSIC_BINDING] = "binding",
622 [NIR_INTRINSIC_COMPONENT] = "component",
623 [NIR_INTRINSIC_INTERP_MODE] = "interp_mode",
624 [NIR_INTRINSIC_REDUCTION_OP] = "reduction_op",
625 [NIR_INTRINSIC_CLUSTER_SIZE] = "cluster_size",
626 };
627 for (unsigned idx = 1; idx < NIR_INTRINSIC_NUM_INDEX_FLAGS; idx++) {
628 if (!info->index_map[idx])
629 continue;
630 fprintf(fp, " /*");
631 if (idx == NIR_INTRINSIC_WRMASK) {
632 /* special case wrmask to show it as a writemask.. */
633 unsigned wrmask = nir_intrinsic_write_mask(instr);
634 fprintf(fp, " wrmask=");
635 for (unsigned i = 0; i < 4; i++)
636 if ((wrmask >> i) & 1)
637 fprintf(fp, "%c", "xyzw"[i]);
638 } else if (idx == NIR_INTRINSIC_REDUCTION_OP) {
639 nir_op reduction_op = nir_intrinsic_reduction_op(instr);
640 fprintf(fp, " reduction_op=%s", nir_op_infos[reduction_op].name);
641 } else {
642 unsigned off = info->index_map[idx] - 1;
643 assert(index_name[idx]); /* forgot to update index_name table? */
644 fprintf(fp, " %s=%d", index_name[idx], instr->const_index[off]);
645 }
646 fprintf(fp, " */");
647 }
648
649 if (!state->shader)
650 return;
651
652 struct exec_list *var_list = NULL;
653
654 switch (instr->intrinsic) {
655 case nir_intrinsic_load_uniform:
656 var_list = &state->shader->uniforms;
657 break;
658 case nir_intrinsic_load_input:
659 case nir_intrinsic_load_per_vertex_input:
660 var_list = &state->shader->inputs;
661 break;
662 case nir_intrinsic_load_output:
663 case nir_intrinsic_store_output:
664 case nir_intrinsic_store_per_vertex_output:
665 var_list = &state->shader->outputs;
666 break;
667 default:
668 return;
669 }
670
671 nir_foreach_variable(var, var_list) {
672 if ((var->data.driver_location == nir_intrinsic_base(instr)) &&
673 (instr->intrinsic == nir_intrinsic_load_uniform ||
674 var->data.location_frac == nir_intrinsic_component(instr)) &&
675 var->name) {
676 fprintf(fp, "\t/* %s */", var->name);
677 break;
678 }
679 }
680 }
681
682 static void
683 print_tex_instr(nir_tex_instr *instr, print_state *state)
684 {
685 FILE *fp = state->fp;
686
687 print_dest(&instr->dest, state);
688
689 fprintf(fp, " = ");
690
691 switch (instr->op) {
692 case nir_texop_tex:
693 fprintf(fp, "tex ");
694 break;
695 case nir_texop_txb:
696 fprintf(fp, "txb ");
697 break;
698 case nir_texop_txl:
699 fprintf(fp, "txl ");
700 break;
701 case nir_texop_txd:
702 fprintf(fp, "txd ");
703 break;
704 case nir_texop_txf:
705 fprintf(fp, "txf ");
706 break;
707 case nir_texop_txf_ms:
708 fprintf(fp, "txf_ms ");
709 break;
710 case nir_texop_txf_ms_mcs:
711 fprintf(fp, "txf_ms_mcs ");
712 break;
713 case nir_texop_txs:
714 fprintf(fp, "txs ");
715 break;
716 case nir_texop_lod:
717 fprintf(fp, "lod ");
718 break;
719 case nir_texop_tg4:
720 fprintf(fp, "tg4 ");
721 break;
722 case nir_texop_query_levels:
723 fprintf(fp, "query_levels ");
724 break;
725 case nir_texop_texture_samples:
726 fprintf(fp, "texture_samples ");
727 break;
728 case nir_texop_samples_identical:
729 fprintf(fp, "samples_identical ");
730 break;
731 default:
732 unreachable("Invalid texture operation");
733 break;
734 }
735
736 for (unsigned i = 0; i < instr->num_srcs; i++) {
737 print_src(&instr->src[i].src, state);
738
739 fprintf(fp, " ");
740
741 switch(instr->src[i].src_type) {
742 case nir_tex_src_coord:
743 fprintf(fp, "(coord)");
744 break;
745 case nir_tex_src_projector:
746 fprintf(fp, "(projector)");
747 break;
748 case nir_tex_src_comparator:
749 fprintf(fp, "(comparator)");
750 break;
751 case nir_tex_src_offset:
752 fprintf(fp, "(offset)");
753 break;
754 case nir_tex_src_bias:
755 fprintf(fp, "(bias)");
756 break;
757 case nir_tex_src_lod:
758 fprintf(fp, "(lod)");
759 break;
760 case nir_tex_src_ms_index:
761 fprintf(fp, "(ms_index)");
762 break;
763 case nir_tex_src_ms_mcs:
764 fprintf(fp, "(ms_mcs)");
765 break;
766 case nir_tex_src_ddx:
767 fprintf(fp, "(ddx)");
768 break;
769 case nir_tex_src_ddy:
770 fprintf(fp, "(ddy)");
771 break;
772 case nir_tex_src_texture_offset:
773 fprintf(fp, "(texture_offset)");
774 break;
775 case nir_tex_src_sampler_offset:
776 fprintf(fp, "(sampler_offset)");
777 break;
778 case nir_tex_src_plane:
779 fprintf(fp, "(plane)");
780 break;
781
782 default:
783 unreachable("Invalid texture source type");
784 break;
785 }
786
787 fprintf(fp, ", ");
788 }
789
790 if (instr->op == nir_texop_tg4) {
791 fprintf(fp, "%u (gather_component), ", instr->component);
792 }
793
794 if (instr->texture) {
795 print_deref(instr->texture, state);
796 fprintf(fp, " (texture)");
797 if (instr->sampler) {
798 print_deref(instr->sampler, state);
799 fprintf(fp, " (sampler)");
800 }
801 } else {
802 assert(instr->sampler == NULL);
803 fprintf(fp, "%u (texture) %u (sampler)",
804 instr->texture_index, instr->sampler_index);
805 }
806 }
807
808 static void
809 print_call_instr(nir_call_instr *instr, print_state *state)
810 {
811 FILE *fp = state->fp;
812
813 fprintf(fp, "call %s ", instr->callee->name);
814
815 for (unsigned i = 0; i < instr->num_params; i++) {
816 if (i != 0)
817 fprintf(fp, ", ");
818
819 print_deref(instr->params[i], state);
820 }
821
822 if (instr->return_deref != NULL) {
823 if (instr->num_params != 0)
824 fprintf(fp, ", ");
825 fprintf(fp, "returning ");
826 print_deref(instr->return_deref, state);
827 }
828 }
829
830 static void
831 print_load_const_instr(nir_load_const_instr *instr, print_state *state)
832 {
833 FILE *fp = state->fp;
834
835 print_ssa_def(&instr->def, state);
836
837 fprintf(fp, " = load_const (");
838
839 for (unsigned i = 0; i < instr->def.num_components; i++) {
840 if (i != 0)
841 fprintf(fp, ", ");
842
843 /*
844 * we don't really know the type of the constant (if it will be used as a
845 * float or an int), so just print the raw constant in hex for fidelity
846 * and then print the float in a comment for readability.
847 */
848
849 if (instr->def.bit_size == 64)
850 fprintf(fp, "0x%16" PRIx64 " /* %f */", instr->value.u64[i],
851 instr->value.f64[i]);
852 else
853 fprintf(fp, "0x%08x /* %f */", instr->value.u32[i], instr->value.f32[i]);
854 }
855
856 fprintf(fp, ")");
857 }
858
859 static void
860 print_jump_instr(nir_jump_instr *instr, print_state *state)
861 {
862 FILE *fp = state->fp;
863
864 switch (instr->type) {
865 case nir_jump_break:
866 fprintf(fp, "break");
867 break;
868
869 case nir_jump_continue:
870 fprintf(fp, "continue");
871 break;
872
873 case nir_jump_return:
874 fprintf(fp, "return");
875 break;
876 }
877 }
878
879 static void
880 print_ssa_undef_instr(nir_ssa_undef_instr* instr, print_state *state)
881 {
882 FILE *fp = state->fp;
883 print_ssa_def(&instr->def, state);
884 fprintf(fp, " = undefined");
885 }
886
887 static void
888 print_phi_instr(nir_phi_instr *instr, print_state *state)
889 {
890 FILE *fp = state->fp;
891 print_dest(&instr->dest, state);
892 fprintf(fp, " = phi ");
893 nir_foreach_phi_src(src, instr) {
894 if (&src->node != exec_list_get_head(&instr->srcs))
895 fprintf(fp, ", ");
896
897 fprintf(fp, "block_%u: ", src->pred->index);
898 print_src(&src->src, state);
899 }
900 }
901
902 static void
903 print_parallel_copy_instr(nir_parallel_copy_instr *instr, print_state *state)
904 {
905 FILE *fp = state->fp;
906 nir_foreach_parallel_copy_entry(entry, instr) {
907 if (&entry->node != exec_list_get_head(&instr->entries))
908 fprintf(fp, "; ");
909
910 print_dest(&entry->dest, state);
911 fprintf(fp, " = ");
912 print_src(&entry->src, state);
913 }
914 }
915
916 static void
917 print_instr(const nir_instr *instr, print_state *state, unsigned tabs)
918 {
919 FILE *fp = state->fp;
920 print_tabs(tabs, fp);
921
922 switch (instr->type) {
923 case nir_instr_type_alu:
924 print_alu_instr(nir_instr_as_alu(instr), state);
925 break;
926
927 case nir_instr_type_call:
928 print_call_instr(nir_instr_as_call(instr), state);
929 break;
930
931 case nir_instr_type_intrinsic:
932 print_intrinsic_instr(nir_instr_as_intrinsic(instr), state);
933 break;
934
935 case nir_instr_type_tex:
936 print_tex_instr(nir_instr_as_tex(instr), state);
937 break;
938
939 case nir_instr_type_load_const:
940 print_load_const_instr(nir_instr_as_load_const(instr), state);
941 break;
942
943 case nir_instr_type_jump:
944 print_jump_instr(nir_instr_as_jump(instr), state);
945 break;
946
947 case nir_instr_type_ssa_undef:
948 print_ssa_undef_instr(nir_instr_as_ssa_undef(instr), state);
949 break;
950
951 case nir_instr_type_phi:
952 print_phi_instr(nir_instr_as_phi(instr), state);
953 break;
954
955 case nir_instr_type_parallel_copy:
956 print_parallel_copy_instr(nir_instr_as_parallel_copy(instr), state);
957 break;
958
959 default:
960 unreachable("Invalid instruction type");
961 break;
962 }
963 }
964
965 static int
966 compare_block_index(const void *p1, const void *p2)
967 {
968 const nir_block *block1 = *((const nir_block **) p1);
969 const nir_block *block2 = *((const nir_block **) p2);
970
971 return (int) block1->index - (int) block2->index;
972 }
973
974 static void print_cf_node(nir_cf_node *node, print_state *state,
975 unsigned tabs);
976
977 static void
978 print_block(nir_block *block, print_state *state, unsigned tabs)
979 {
980 FILE *fp = state->fp;
981
982 print_tabs(tabs, fp);
983 fprintf(fp, "block block_%u:\n", block->index);
984
985 /* sort the predecessors by index so we consistently print the same thing */
986
987 nir_block **preds =
988 malloc(block->predecessors->entries * sizeof(nir_block *));
989
990 struct set_entry *entry;
991 unsigned i = 0;
992 set_foreach(block->predecessors, entry) {
993 preds[i++] = (nir_block *) entry->key;
994 }
995
996 qsort(preds, block->predecessors->entries, sizeof(nir_block *),
997 compare_block_index);
998
999 print_tabs(tabs, fp);
1000 fprintf(fp, "/* preds: ");
1001 for (unsigned i = 0; i < block->predecessors->entries; i++) {
1002 fprintf(fp, "block_%u ", preds[i]->index);
1003 }
1004 fprintf(fp, "*/\n");
1005
1006 free(preds);
1007
1008 nir_foreach_instr(instr, block) {
1009 print_instr(instr, state, tabs);
1010 fprintf(fp, "\n");
1011 print_annotation(state, instr);
1012 }
1013
1014 print_tabs(tabs, fp);
1015 fprintf(fp, "/* succs: ");
1016 for (unsigned i = 0; i < 2; i++)
1017 if (block->successors[i]) {
1018 fprintf(fp, "block_%u ", block->successors[i]->index);
1019 }
1020 fprintf(fp, "*/\n");
1021 }
1022
1023 static void
1024 print_if(nir_if *if_stmt, print_state *state, unsigned tabs)
1025 {
1026 FILE *fp = state->fp;
1027
1028 print_tabs(tabs, fp);
1029 fprintf(fp, "if ");
1030 print_src(&if_stmt->condition, state);
1031 fprintf(fp, " {\n");
1032 foreach_list_typed(nir_cf_node, node, node, &if_stmt->then_list) {
1033 print_cf_node(node, state, tabs + 1);
1034 }
1035 print_tabs(tabs, fp);
1036 fprintf(fp, "} else {\n");
1037 foreach_list_typed(nir_cf_node, node, node, &if_stmt->else_list) {
1038 print_cf_node(node, state, tabs + 1);
1039 }
1040 print_tabs(tabs, fp);
1041 fprintf(fp, "}\n");
1042 }
1043
1044 static void
1045 print_loop(nir_loop *loop, print_state *state, unsigned tabs)
1046 {
1047 FILE *fp = state->fp;
1048
1049 print_tabs(tabs, fp);
1050 fprintf(fp, "loop {\n");
1051 foreach_list_typed(nir_cf_node, node, node, &loop->body) {
1052 print_cf_node(node, state, tabs + 1);
1053 }
1054 print_tabs(tabs, fp);
1055 fprintf(fp, "}\n");
1056 }
1057
1058 static void
1059 print_cf_node(nir_cf_node *node, print_state *state, unsigned int tabs)
1060 {
1061 switch (node->type) {
1062 case nir_cf_node_block:
1063 print_block(nir_cf_node_as_block(node), state, tabs);
1064 break;
1065
1066 case nir_cf_node_if:
1067 print_if(nir_cf_node_as_if(node), state, tabs);
1068 break;
1069
1070 case nir_cf_node_loop:
1071 print_loop(nir_cf_node_as_loop(node), state, tabs);
1072 break;
1073
1074 default:
1075 unreachable("Invalid CFG node type");
1076 }
1077 }
1078
1079 static void
1080 print_function_impl(nir_function_impl *impl, print_state *state)
1081 {
1082 FILE *fp = state->fp;
1083
1084 fprintf(fp, "\nimpl %s ", impl->function->name);
1085
1086 for (unsigned i = 0; i < impl->num_params; i++) {
1087 if (i != 0)
1088 fprintf(fp, ", ");
1089
1090 print_arg(impl->params[i], state);
1091 }
1092
1093 if (impl->return_var != NULL) {
1094 if (impl->num_params != 0)
1095 fprintf(fp, ", ");
1096 fprintf(fp, "returning ");
1097 print_arg(impl->return_var, state);
1098 }
1099
1100 fprintf(fp, "{\n");
1101
1102 nir_foreach_variable(var, &impl->locals) {
1103 fprintf(fp, "\t");
1104 print_var_decl(var, state);
1105 }
1106
1107 foreach_list_typed(nir_register, reg, node, &impl->registers) {
1108 fprintf(fp, "\t");
1109 print_register_decl(reg, state);
1110 }
1111
1112 nir_index_blocks(impl);
1113
1114 foreach_list_typed(nir_cf_node, node, node, &impl->body) {
1115 print_cf_node(node, state, 1);
1116 }
1117
1118 fprintf(fp, "\tblock block_%u:\n}\n\n", impl->end_block->index);
1119 }
1120
1121 static void
1122 print_function(nir_function *function, print_state *state)
1123 {
1124 FILE *fp = state->fp;
1125
1126 fprintf(fp, "decl_function %s ", function->name);
1127
1128 for (unsigned i = 0; i < function->num_params; i++) {
1129 if (i != 0)
1130 fprintf(fp, ", ");
1131
1132 switch (function->params[i].param_type) {
1133 case nir_parameter_in:
1134 fprintf(fp, "in ");
1135 break;
1136 case nir_parameter_out:
1137 fprintf(fp, "out ");
1138 break;
1139 case nir_parameter_inout:
1140 fprintf(fp, "inout ");
1141 break;
1142 default:
1143 unreachable("Invalid parameter type");
1144 }
1145
1146 fprintf(fp, "%s", glsl_get_type_name(function->params[i].type));
1147 }
1148
1149 if (function->return_type != NULL) {
1150 if (function->num_params != 0)
1151 fprintf(fp, ", ");
1152 fprintf(fp, "returning %s", glsl_get_type_name(function->return_type));
1153 }
1154
1155 fprintf(fp, "\n");
1156
1157 if (function->impl != NULL) {
1158 print_function_impl(function->impl, state);
1159 return;
1160 }
1161 }
1162
1163 static void
1164 init_print_state(print_state *state, nir_shader *shader, FILE *fp)
1165 {
1166 state->fp = fp;
1167 state->shader = shader;
1168 state->ht = _mesa_hash_table_create(NULL, _mesa_hash_pointer,
1169 _mesa_key_pointer_equal);
1170 state->syms = _mesa_set_create(NULL, _mesa_key_hash_string,
1171 _mesa_key_string_equal);
1172 state->index = 0;
1173 }
1174
1175 static void
1176 destroy_print_state(print_state *state)
1177 {
1178 _mesa_hash_table_destroy(state->ht, NULL);
1179 _mesa_set_destroy(state->syms, NULL);
1180 }
1181
1182 void
1183 nir_print_shader_annotated(nir_shader *shader, FILE *fp,
1184 struct hash_table *annotations)
1185 {
1186 print_state state;
1187 init_print_state(&state, shader, fp);
1188
1189 state.annotations = annotations;
1190
1191 fprintf(fp, "shader: %s\n", gl_shader_stage_name(shader->info.stage));
1192
1193 if (shader->info.name)
1194 fprintf(fp, "name: %s\n", shader->info.name);
1195
1196 if (shader->info.label)
1197 fprintf(fp, "label: %s\n", shader->info.label);
1198
1199 switch (shader->info.stage) {
1200 case MESA_SHADER_COMPUTE:
1201 fprintf(fp, "local-size: %u, %u, %u%s\n",
1202 shader->info.cs.local_size[0],
1203 shader->info.cs.local_size[1],
1204 shader->info.cs.local_size[2],
1205 shader->info.cs.local_size_variable ? " (variable)" : "");
1206 fprintf(fp, "shared-size: %u\n", shader->info.cs.shared_size);
1207 break;
1208 default:
1209 break;
1210 }
1211
1212 fprintf(fp, "inputs: %u\n", shader->num_inputs);
1213 fprintf(fp, "outputs: %u\n", shader->num_outputs);
1214 fprintf(fp, "uniforms: %u\n", shader->num_uniforms);
1215 fprintf(fp, "shared: %u\n", shader->num_shared);
1216
1217 nir_foreach_variable(var, &shader->uniforms) {
1218 print_var_decl(var, &state);
1219 }
1220
1221 nir_foreach_variable(var, &shader->inputs) {
1222 print_var_decl(var, &state);
1223 }
1224
1225 nir_foreach_variable(var, &shader->outputs) {
1226 print_var_decl(var, &state);
1227 }
1228
1229 nir_foreach_variable(var, &shader->shared) {
1230 print_var_decl(var, &state);
1231 }
1232
1233 nir_foreach_variable(var, &shader->globals) {
1234 print_var_decl(var, &state);
1235 }
1236
1237 nir_foreach_variable(var, &shader->system_values) {
1238 print_var_decl(var, &state);
1239 }
1240
1241 foreach_list_typed(nir_register, reg, node, &shader->registers) {
1242 print_register_decl(reg, &state);
1243 }
1244
1245 foreach_list_typed(nir_function, func, node, &shader->functions) {
1246 print_function(func, &state);
1247 }
1248
1249 destroy_print_state(&state);
1250 }
1251
1252 void
1253 nir_print_shader(nir_shader *shader, FILE *fp)
1254 {
1255 nir_print_shader_annotated(shader, fp, NULL);
1256 }
1257
1258 void
1259 nir_print_instr(const nir_instr *instr, FILE *fp)
1260 {
1261 print_state state = {
1262 .fp = fp,
1263 };
1264 print_instr(instr, &state, 0);
1265
1266 }