2 * Copyright © 2014 Intel Corporation
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:
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
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
24 * Connor Abbott (cwabbott0@gmail.com)
29 #include "compiler/shader_enums.h"
30 #include "util/half_float.h"
33 #include <inttypes.h> /* for PRIx64 macro */
36 print_tabs(unsigned num_tabs
, FILE *fp
)
38 for (unsigned i
= 0; i
< num_tabs
; i
++)
45 /** map from nir_variable -> printable name */
46 struct hash_table
*ht
;
48 /** set of names used so far for nir_variables */
51 /* an index used to make new non-conflicting names */
55 * Optional table of annotations mapping nir object
56 * (such as instr or var) to message to print.
58 struct hash_table
*annotations
;
62 print_annotation(print_state
*state
, void *obj
)
64 if (!state
->annotations
)
67 struct hash_entry
*entry
= _mesa_hash_table_search(state
->annotations
, obj
);
71 const char *note
= entry
->data
;
72 _mesa_hash_table_remove(state
->annotations
, entry
);
74 fprintf(stderr
, "%s\n\n", note
);
78 print_register(nir_register
*reg
, print_state
*state
)
81 if (reg
->name
!= NULL
)
82 fprintf(fp
, "/* %s */ ", reg
->name
);
84 fprintf(fp
, "gr%u", reg
->index
);
86 fprintf(fp
, "r%u", reg
->index
);
89 static const char *sizes
[] = { "error", "vec1", "vec2", "vec3", "vec4",
90 "error", "error", "error", "vec8",
91 "error", "error", "error", "vec16"};
94 print_register_decl(nir_register
*reg
, print_state
*state
)
97 fprintf(fp
, "decl_reg %s %u ", sizes
[reg
->num_components
], reg
->bit_size
);
99 fprintf(fp
, "(packed) ");
100 print_register(reg
, state
);
101 if (reg
->num_array_elems
!= 0)
102 fprintf(fp
, "[%u]", reg
->num_array_elems
);
107 print_ssa_def(nir_ssa_def
*def
, print_state
*state
)
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
,
117 print_ssa_use(nir_ssa_def
*def
, print_state
*state
)
119 FILE *fp
= state
->fp
;
120 if (def
->name
!= NULL
)
121 fprintf(fp
, "/* %s */ ", def
->name
);
122 fprintf(fp
, "ssa_%u", def
->index
);
125 static void print_src(nir_src
*src
, print_state
*state
);
128 print_reg_src(nir_reg_src
*src
, print_state
*state
)
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
) {
136 print_src(src
->indirect
, state
);
143 print_reg_dest(nir_reg_dest
*dest
, print_state
*state
)
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
) {
151 print_src(dest
->indirect
, state
);
158 print_src(nir_src
*src
, print_state
*state
)
161 print_ssa_use(src
->ssa
, state
);
163 print_reg_src(&src
->reg
, state
);
167 print_dest(nir_dest
*dest
, print_state
*state
)
170 print_ssa_def(&dest
->ssa
, state
);
172 print_reg_dest(&dest
->reg
, state
);
176 print_alu_src(nir_alu_instr
*instr
, unsigned src
, print_state
*state
)
178 FILE *fp
= state
->fp
;
180 if (instr
->src
[src
].negate
)
182 if (instr
->src
[src
].abs
)
185 print_src(&instr
->src
[src
].src
, state
);
187 bool print_swizzle
= false;
188 unsigned used_channels
= 0;
190 for (unsigned i
= 0; i
< 4; i
++) {
191 if (!nir_alu_instr_channel_used(instr
, src
, i
))
196 if (instr
->src
[src
].swizzle
[i
] != i
) {
197 print_swizzle
= true;
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
;
206 if (print_swizzle
|| used_channels
!= live_channels
) {
208 for (unsigned i
= 0; i
< 4; i
++) {
209 if (!nir_alu_instr_channel_used(instr
, src
, i
))
212 fprintf(fp
, "%c", "xyzw"[instr
->src
[src
].swizzle
[i
]]);
216 if (instr
->src
[src
].abs
)
221 print_alu_dest(nir_alu_dest
*dest
, print_state
*state
)
223 FILE *fp
= state
->fp
;
224 /* we're going to print the saturate modifier later, after the opcode */
226 print_dest(&dest
->dest
, state
);
228 if (!dest
->dest
.is_ssa
&&
229 dest
->write_mask
!= (1 << dest
->dest
.reg
.reg
->num_components
) - 1) {
231 for (unsigned i
= 0; i
< 4; i
++)
232 if ((dest
->write_mask
>> i
) & 1)
233 fprintf(fp
, "%c", "xyzw"[i
]);
238 print_alu_instr(nir_alu_instr
*instr
, print_state
*state
)
240 FILE *fp
= state
->fp
;
242 print_alu_dest(&instr
->dest
, state
);
244 fprintf(fp
, " = %s", nir_op_infos
[instr
->op
].name
);
247 if (instr
->dest
.saturate
)
251 for (unsigned i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++) {
255 print_alu_src(instr
, i
, state
);
260 get_var_name(nir_variable
*var
, print_state
*state
)
262 if (state
->ht
== NULL
)
263 return var
->name
? var
->name
: "unnamed";
267 struct hash_entry
*entry
= _mesa_hash_table_search(state
->ht
, var
);
272 if (var
->name
== NULL
) {
273 name
= ralloc_asprintf(state
->syms
, "@%u", state
->index
++);
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
279 name
= ralloc_asprintf(state
->syms
, "%s@%u", var
->name
,
282 /* Mark this one as seen */
283 _mesa_set_add(state
->syms
, var
->name
);
288 _mesa_hash_table_insert(state
->ht
, var
, name
);
294 print_constant(nir_constant
*c
, const struct glsl_type
*type
, print_state
*state
)
296 FILE *fp
= state
->fp
;
297 const unsigned rows
= glsl_get_vector_elements(type
);
298 const unsigned cols
= glsl_get_matrix_columns(type
);
301 switch (glsl_get_base_type(type
)) {
305 /* Only float base types can be matrices. */
308 for (i
= 0; i
< rows
; i
++) {
309 if (i
> 0) fprintf(fp
, ", ");
310 fprintf(fp
, "0x%08x", c
->values
[0].u32
[i
]);
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
]);
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
]);
332 case GLSL_TYPE_UINT64
:
333 case GLSL_TYPE_INT64
:
334 /* Only float base types can be matrices. */
337 for (i
= 0; i
< cols
; i
++) {
338 if (i
> 0) fprintf(fp
, ", ");
339 fprintf(fp
, "0x%08" PRIx64
, c
->values
[0].u64
[i
]);
343 case GLSL_TYPE_STRUCT
:
344 for (i
= 0; i
< c
->num_elements
; i
++) {
345 if (i
> 0) fprintf(fp
, ", ");
347 print_constant(c
->elements
[i
], glsl_get_struct_field(type
, i
), state
);
352 case GLSL_TYPE_ARRAY
:
353 for (i
= 0; i
< c
->num_elements
; i
++) {
354 if (i
> 0) fprintf(fp
, ", ");
356 print_constant(c
->elements
[i
], glsl_get_array_element(type
), state
);
362 unreachable("not reached");
367 get_variable_mode_str(nir_variable_mode mode
)
370 case nir_var_shader_in
:
372 case nir_var_shader_out
:
374 case nir_var_uniform
:
376 case nir_var_shader_storage
:
377 return "shader_storage";
378 case nir_var_system_value
:
391 print_var_decl(nir_variable
*var
, print_state
*state
)
393 FILE *fp
= state
->fp
;
395 fprintf(fp
, "decl_var ");
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
));
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
);
412 fprintf(fp
, "%s %s", glsl_get_type_name(var
->type
),
413 get_var_name(var
, state
));
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
;
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
);
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
);
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
);
440 case MESA_SHADER_TESS_CTRL
:
441 case MESA_SHADER_TESS_EVAL
:
442 case MESA_SHADER_COMPUTE
:
449 snprintf(buf
, sizeof(buf
), "%u", var
->data
.location
);
453 /* For shader I/O vars that have been split to components or packed,
454 * print the fractional location within the input/output.
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
];
468 components
= components_local
;
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" : "");
481 if (var
->constant_initializer
) {
482 fprintf(fp
, " = { ");
483 print_constant(var
->constant_initializer
, var
->type
, state
);
488 print_annotation(state
, var
);
492 print_var(nir_variable
*var
, print_state
*state
)
494 FILE *fp
= state
->fp
;
495 fprintf(fp
, "%s", get_var_name(var
, state
));
499 print_arg(nir_variable
*var
, print_state
*state
)
501 FILE *fp
= state
->fp
;
502 fprintf(fp
, "%s %s", glsl_get_type_name(var
->type
),
503 get_var_name(var
, state
));
507 print_deref_var(nir_deref_var
*deref
, print_state
*state
)
509 print_var(deref
->var
, state
);
513 print_deref_array(nir_deref_array
*deref
, print_state
*state
)
515 FILE *fp
= state
->fp
;
517 switch (deref
->deref_array_type
) {
518 case nir_deref_array_type_direct
:
519 fprintf(fp
, "%u", deref
->base_offset
);
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
);
526 case nir_deref_array_type_wildcard
:
534 print_deref_struct(nir_deref_struct
*deref
, const struct glsl_type
*parent_type
,
537 FILE *fp
= state
->fp
;
538 fprintf(fp
, ".%s", glsl_get_struct_elem_name(parent_type
, deref
->index
));
542 print_deref(nir_deref_var
*deref
, print_state
*state
)
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
);
554 case nir_deref_type_array
:
555 assert(pretail
!= NULL
);
556 print_deref_array(nir_deref_as_array(tail
), state
);
559 case nir_deref_type_struct
:
560 assert(pretail
!= NULL
);
561 print_deref_struct(nir_deref_as_struct(tail
),
562 pretail
->type
, state
);
566 unreachable("Invalid deref type");
570 tail
= pretail
->child
;
575 print_intrinsic_instr(nir_intrinsic_instr
*instr
, print_state
*state
)
577 const nir_intrinsic_info
*info
= &nir_intrinsic_infos
[instr
->intrinsic
];
578 unsigned num_srcs
= info
->num_srcs
;
579 FILE *fp
= state
->fp
;
581 if (info
->has_dest
) {
582 print_dest(&instr
->dest
, state
);
586 fprintf(fp
, "intrinsic %s (", info
->name
);
588 for (unsigned i
= 0; i
< num_srcs
; i
++) {
592 print_src(&instr
->src
[i
], state
);
597 for (unsigned i
= 0; i
< info
->num_variables
; i
++) {
601 print_deref(instr
->variables
[i
], state
);
606 for (unsigned i
= 0; i
< info
->num_indices
; i
++) {
610 fprintf(fp
, "%d", instr
->const_index
[i
]);
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",
628 for (unsigned idx
= 1; idx
< NIR_INTRINSIC_NUM_INDEX_FLAGS
; idx
++) {
629 if (!info
->index_map
[idx
])
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
);
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
]);
653 struct exec_list
*var_list
= NULL
;
655 switch (instr
->intrinsic
) {
656 case nir_intrinsic_load_uniform
:
657 var_list
= &state
->shader
->uniforms
;
659 case nir_intrinsic_load_input
:
660 case nir_intrinsic_load_per_vertex_input
:
661 var_list
= &state
->shader
->inputs
;
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
;
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
)) &&
677 fprintf(fp
, "\t/* %s */", var
->name
);
684 print_tex_instr(nir_tex_instr
*instr
, print_state
*state
)
686 FILE *fp
= state
->fp
;
688 print_dest(&instr
->dest
, state
);
708 case nir_texop_txf_ms
:
709 fprintf(fp
, "txf_ms ");
711 case nir_texop_txf_ms_mcs
:
712 fprintf(fp
, "txf_ms_mcs ");
723 case nir_texop_query_levels
:
724 fprintf(fp
, "query_levels ");
726 case nir_texop_texture_samples
:
727 fprintf(fp
, "texture_samples ");
729 case nir_texop_samples_identical
:
730 fprintf(fp
, "samples_identical ");
733 unreachable("Invalid texture operation");
737 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++) {
738 print_src(&instr
->src
[i
].src
, state
);
742 switch(instr
->src
[i
].src_type
) {
743 case nir_tex_src_coord
:
744 fprintf(fp
, "(coord)");
746 case nir_tex_src_projector
:
747 fprintf(fp
, "(projector)");
749 case nir_tex_src_comparator
:
750 fprintf(fp
, "(comparator)");
752 case nir_tex_src_offset
:
753 fprintf(fp
, "(offset)");
755 case nir_tex_src_bias
:
756 fprintf(fp
, "(bias)");
758 case nir_tex_src_lod
:
759 fprintf(fp
, "(lod)");
761 case nir_tex_src_ms_index
:
762 fprintf(fp
, "(ms_index)");
764 case nir_tex_src_ms_mcs
:
765 fprintf(fp
, "(ms_mcs)");
767 case nir_tex_src_ddx
:
768 fprintf(fp
, "(ddx)");
770 case nir_tex_src_ddy
:
771 fprintf(fp
, "(ddy)");
773 case nir_tex_src_texture_offset
:
774 fprintf(fp
, "(texture_offset)");
776 case nir_tex_src_sampler_offset
:
777 fprintf(fp
, "(sampler_offset)");
779 case nir_tex_src_plane
:
780 fprintf(fp
, "(plane)");
784 unreachable("Invalid texture source type");
791 if (instr
->op
== nir_texop_tg4
) {
792 fprintf(fp
, "%u (gather_component), ", instr
->component
);
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)");
803 assert(instr
->sampler
== NULL
);
804 fprintf(fp
, "%u (texture) %u (sampler)",
805 instr
->texture_index
, instr
->sampler_index
);
810 print_call_instr(nir_call_instr
*instr
, print_state
*state
)
812 FILE *fp
= state
->fp
;
814 fprintf(fp
, "call %s ", instr
->callee
->name
);
816 for (unsigned i
= 0; i
< instr
->num_params
; i
++) {
820 print_deref(instr
->params
[i
], state
);
823 if (instr
->return_deref
!= NULL
) {
824 if (instr
->num_params
!= 0)
826 fprintf(fp
, "returning ");
827 print_deref(instr
->return_deref
, state
);
832 print_load_const_instr(nir_load_const_instr
*instr
, print_state
*state
)
834 FILE *fp
= state
->fp
;
836 print_ssa_def(&instr
->def
, state
);
838 fprintf(fp
, " = load_const (");
840 for (unsigned i
= 0; i
< instr
->def
.num_components
; i
++) {
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.
850 switch (instr
->def
.bit_size
) {
852 fprintf(fp
, "0x%16" PRIx64
" /* %f */", instr
->value
.u64
[i
],
853 instr
->value
.f64
[i
]);
856 fprintf(fp
, "0x%08x /* %f */", instr
->value
.u32
[i
], instr
->value
.f32
[i
]);
859 fprintf(fp
, "0x%04x /* %f */", instr
->value
.u16
[i
],
860 _mesa_half_to_float(instr
->value
.u16
[i
]));
863 fprintf(fp
, "0x%02x", instr
->value
.u8
[i
]);
872 print_jump_instr(nir_jump_instr
*instr
, print_state
*state
)
874 FILE *fp
= state
->fp
;
876 switch (instr
->type
) {
878 fprintf(fp
, "break");
881 case nir_jump_continue
:
882 fprintf(fp
, "continue");
885 case nir_jump_return
:
886 fprintf(fp
, "return");
892 print_ssa_undef_instr(nir_ssa_undef_instr
* instr
, print_state
*state
)
894 FILE *fp
= state
->fp
;
895 print_ssa_def(&instr
->def
, state
);
896 fprintf(fp
, " = undefined");
900 print_phi_instr(nir_phi_instr
*instr
, print_state
*state
)
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
))
909 fprintf(fp
, "block_%u: ", src
->pred
->index
);
910 print_src(&src
->src
, state
);
915 print_parallel_copy_instr(nir_parallel_copy_instr
*instr
, print_state
*state
)
917 FILE *fp
= state
->fp
;
918 nir_foreach_parallel_copy_entry(entry
, instr
) {
919 if (&entry
->node
!= exec_list_get_head(&instr
->entries
))
922 print_dest(&entry
->dest
, state
);
924 print_src(&entry
->src
, state
);
929 print_instr(const nir_instr
*instr
, print_state
*state
, unsigned tabs
)
931 FILE *fp
= state
->fp
;
932 print_tabs(tabs
, fp
);
934 switch (instr
->type
) {
935 case nir_instr_type_alu
:
936 print_alu_instr(nir_instr_as_alu(instr
), state
);
939 case nir_instr_type_call
:
940 print_call_instr(nir_instr_as_call(instr
), state
);
943 case nir_instr_type_intrinsic
:
944 print_intrinsic_instr(nir_instr_as_intrinsic(instr
), state
);
947 case nir_instr_type_tex
:
948 print_tex_instr(nir_instr_as_tex(instr
), state
);
951 case nir_instr_type_load_const
:
952 print_load_const_instr(nir_instr_as_load_const(instr
), state
);
955 case nir_instr_type_jump
:
956 print_jump_instr(nir_instr_as_jump(instr
), state
);
959 case nir_instr_type_ssa_undef
:
960 print_ssa_undef_instr(nir_instr_as_ssa_undef(instr
), state
);
963 case nir_instr_type_phi
:
964 print_phi_instr(nir_instr_as_phi(instr
), state
);
967 case nir_instr_type_parallel_copy
:
968 print_parallel_copy_instr(nir_instr_as_parallel_copy(instr
), state
);
972 unreachable("Invalid instruction type");
978 compare_block_index(const void *p1
, const void *p2
)
980 const nir_block
*block1
= *((const nir_block
**) p1
);
981 const nir_block
*block2
= *((const nir_block
**) p2
);
983 return (int) block1
->index
- (int) block2
->index
;
986 static void print_cf_node(nir_cf_node
*node
, print_state
*state
,
990 print_block(nir_block
*block
, print_state
*state
, unsigned tabs
)
992 FILE *fp
= state
->fp
;
994 print_tabs(tabs
, fp
);
995 fprintf(fp
, "block block_%u:\n", block
->index
);
997 /* sort the predecessors by index so we consistently print the same thing */
1000 malloc(block
->predecessors
->entries
* sizeof(nir_block
*));
1002 struct set_entry
*entry
;
1004 set_foreach(block
->predecessors
, entry
) {
1005 preds
[i
++] = (nir_block
*) entry
->key
;
1008 qsort(preds
, block
->predecessors
->entries
, sizeof(nir_block
*),
1009 compare_block_index
);
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
);
1016 fprintf(fp
, "*/\n");
1020 nir_foreach_instr(instr
, block
) {
1021 print_instr(instr
, state
, tabs
);
1023 print_annotation(state
, instr
);
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
);
1032 fprintf(fp
, "*/\n");
1036 print_if(nir_if
*if_stmt
, print_state
*state
, unsigned tabs
)
1038 FILE *fp
= state
->fp
;
1040 print_tabs(tabs
, fp
);
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);
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);
1052 print_tabs(tabs
, fp
);
1057 print_loop(nir_loop
*loop
, print_state
*state
, unsigned tabs
)
1059 FILE *fp
= state
->fp
;
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);
1066 print_tabs(tabs
, fp
);
1071 print_cf_node(nir_cf_node
*node
, print_state
*state
, unsigned int tabs
)
1073 switch (node
->type
) {
1074 case nir_cf_node_block
:
1075 print_block(nir_cf_node_as_block(node
), state
, tabs
);
1078 case nir_cf_node_if
:
1079 print_if(nir_cf_node_as_if(node
), state
, tabs
);
1082 case nir_cf_node_loop
:
1083 print_loop(nir_cf_node_as_loop(node
), state
, tabs
);
1087 unreachable("Invalid CFG node type");
1092 print_function_impl(nir_function_impl
*impl
, print_state
*state
)
1094 FILE *fp
= state
->fp
;
1096 fprintf(fp
, "\nimpl %s ", impl
->function
->name
);
1098 for (unsigned i
= 0; i
< impl
->num_params
; i
++) {
1102 print_arg(impl
->params
[i
], state
);
1105 if (impl
->return_var
!= NULL
) {
1106 if (impl
->num_params
!= 0)
1108 fprintf(fp
, "returning ");
1109 print_arg(impl
->return_var
, state
);
1114 nir_foreach_variable(var
, &impl
->locals
) {
1116 print_var_decl(var
, state
);
1119 foreach_list_typed(nir_register
, reg
, node
, &impl
->registers
) {
1121 print_register_decl(reg
, state
);
1124 nir_index_blocks(impl
);
1126 foreach_list_typed(nir_cf_node
, node
, node
, &impl
->body
) {
1127 print_cf_node(node
, state
, 1);
1130 fprintf(fp
, "\tblock block_%u:\n}\n\n", impl
->end_block
->index
);
1134 print_function(nir_function
*function
, print_state
*state
)
1136 FILE *fp
= state
->fp
;
1138 fprintf(fp
, "decl_function %s ", function
->name
);
1140 for (unsigned i
= 0; i
< function
->num_params
; i
++) {
1144 switch (function
->params
[i
].param_type
) {
1145 case nir_parameter_in
:
1148 case nir_parameter_out
:
1149 fprintf(fp
, "out ");
1151 case nir_parameter_inout
:
1152 fprintf(fp
, "inout ");
1155 unreachable("Invalid parameter type");
1158 fprintf(fp
, "%s", glsl_get_type_name(function
->params
[i
].type
));
1161 if (function
->return_type
!= NULL
) {
1162 if (function
->num_params
!= 0)
1164 fprintf(fp
, "returning %s", glsl_get_type_name(function
->return_type
));
1169 if (function
->impl
!= NULL
) {
1170 print_function_impl(function
->impl
, state
);
1176 init_print_state(print_state
*state
, nir_shader
*shader
, FILE *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
);
1188 destroy_print_state(print_state
*state
)
1190 _mesa_hash_table_destroy(state
->ht
, NULL
);
1191 _mesa_set_destroy(state
->syms
, NULL
);
1195 nir_print_shader_annotated(nir_shader
*shader
, FILE *fp
,
1196 struct hash_table
*annotations
)
1199 init_print_state(&state
, shader
, fp
);
1201 state
.annotations
= annotations
;
1203 fprintf(fp
, "shader: %s\n", gl_shader_stage_name(shader
->info
.stage
));
1205 if (shader
->info
.name
)
1206 fprintf(fp
, "name: %s\n", shader
->info
.name
);
1208 if (shader
->info
.label
)
1209 fprintf(fp
, "label: %s\n", shader
->info
.label
);
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
);
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
);
1229 nir_foreach_variable(var
, &shader
->uniforms
) {
1230 print_var_decl(var
, &state
);
1233 nir_foreach_variable(var
, &shader
->inputs
) {
1234 print_var_decl(var
, &state
);
1237 nir_foreach_variable(var
, &shader
->outputs
) {
1238 print_var_decl(var
, &state
);
1241 nir_foreach_variable(var
, &shader
->shared
) {
1242 print_var_decl(var
, &state
);
1245 nir_foreach_variable(var
, &shader
->globals
) {
1246 print_var_decl(var
, &state
);
1249 nir_foreach_variable(var
, &shader
->system_values
) {
1250 print_var_decl(var
, &state
);
1253 foreach_list_typed(nir_register
, reg
, node
, &shader
->registers
) {
1254 print_register_decl(reg
, &state
);
1257 foreach_list_typed(nir_function
, func
, node
, &shader
->functions
) {
1258 print_function(func
, &state
);
1261 destroy_print_state(&state
);
1265 nir_print_shader(nir_shader
*shader
, FILE *fp
)
1267 nir_print_shader_annotated(shader
, fp
, NULL
);
1271 nir_print_instr(const nir_instr
*instr
, FILE *fp
)
1273 print_state state
= {
1276 print_instr(instr
, &state
, 0);