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"
32 #include <inttypes.h> /* for PRIx64 macro */
34 #if defined(_WIN32) && !defined(snprintf)
35 #define snprintf _snprintf
39 print_tabs(unsigned num_tabs
, FILE *fp
)
41 for (unsigned i
= 0; i
< num_tabs
; i
++)
48 /** map from nir_variable -> printable name */
49 struct hash_table
*ht
;
51 /** set of names used so far for nir_variables */
54 /* an index used to make new non-conflicting names */
58 * Optional table of annotations mapping nir object
59 * (such as instr or var) to message to print.
61 struct hash_table
*annotations
;
65 print_annotation(print_state
*state
, void *obj
)
67 if (!state
->annotations
)
70 struct hash_entry
*entry
= _mesa_hash_table_search(state
->annotations
, obj
);
74 const char *note
= entry
->data
;
75 _mesa_hash_table_remove(state
->annotations
, entry
);
77 fprintf(stderr
, "%s\n\n", note
);
81 print_register(nir_register
*reg
, print_state
*state
)
84 if (reg
->name
!= NULL
)
85 fprintf(fp
, "/* %s */ ", reg
->name
);
87 fprintf(fp
, "gr%u", reg
->index
);
89 fprintf(fp
, "r%u", reg
->index
);
92 static const char *sizes
[] = { "error", "vec1", "vec2", "vec3", "vec4" };
95 print_register_decl(nir_register
*reg
, print_state
*state
)
98 fprintf(fp
, "decl_reg %s %u ", sizes
[reg
->num_components
], reg
->bit_size
);
100 fprintf(fp
, "(packed) ");
101 print_register(reg
, state
);
102 if (reg
->num_array_elems
!= 0)
103 fprintf(fp
, "[%u]", reg
->num_array_elems
);
108 print_ssa_def(nir_ssa_def
*def
, print_state
*state
)
110 FILE *fp
= state
->fp
;
111 if (def
->name
!= NULL
)
112 fprintf(fp
, "/* %s */ ", def
->name
);
113 fprintf(fp
, "%s %u ssa_%u", sizes
[def
->num_components
], def
->bit_size
,
118 print_ssa_use(nir_ssa_def
*def
, print_state
*state
)
120 FILE *fp
= state
->fp
;
121 if (def
->name
!= NULL
)
122 fprintf(fp
, "/* %s */ ", def
->name
);
123 fprintf(fp
, "ssa_%u", def
->index
);
126 static void print_src(nir_src
*src
, print_state
*state
);
129 print_reg_src(nir_reg_src
*src
, print_state
*state
)
131 FILE *fp
= state
->fp
;
132 print_register(src
->reg
, state
);
133 if (src
->reg
->num_array_elems
!= 0) {
134 fprintf(fp
, "[%u", src
->base_offset
);
135 if (src
->indirect
!= NULL
) {
137 print_src(src
->indirect
, state
);
144 print_reg_dest(nir_reg_dest
*dest
, print_state
*state
)
146 FILE *fp
= state
->fp
;
147 print_register(dest
->reg
, state
);
148 if (dest
->reg
->num_array_elems
!= 0) {
149 fprintf(fp
, "[%u", dest
->base_offset
);
150 if (dest
->indirect
!= NULL
) {
152 print_src(dest
->indirect
, state
);
159 print_src(nir_src
*src
, print_state
*state
)
162 print_ssa_use(src
->ssa
, state
);
164 print_reg_src(&src
->reg
, state
);
168 print_dest(nir_dest
*dest
, print_state
*state
)
171 print_ssa_def(&dest
->ssa
, state
);
173 print_reg_dest(&dest
->reg
, state
);
177 print_alu_src(nir_alu_instr
*instr
, unsigned src
, print_state
*state
)
179 FILE *fp
= state
->fp
;
181 if (instr
->src
[src
].negate
)
183 if (instr
->src
[src
].abs
)
186 print_src(&instr
->src
[src
].src
, state
);
188 bool print_swizzle
= false;
189 unsigned used_channels
= 0;
191 for (unsigned i
= 0; i
< 4; i
++) {
192 if (!nir_alu_instr_channel_used(instr
, src
, i
))
197 if (instr
->src
[src
].swizzle
[i
] != i
) {
198 print_swizzle
= true;
203 unsigned live_channels
= instr
->src
[src
].src
.is_ssa
204 ? instr
->src
[src
].src
.ssa
->num_components
205 : instr
->src
[src
].src
.reg
.reg
->num_components
;
207 if (print_swizzle
|| used_channels
!= live_channels
) {
209 for (unsigned i
= 0; i
< 4; i
++) {
210 if (!nir_alu_instr_channel_used(instr
, src
, i
))
213 fprintf(fp
, "%c", "xyzw"[instr
->src
[src
].swizzle
[i
]]);
217 if (instr
->src
[src
].abs
)
222 print_alu_dest(nir_alu_dest
*dest
, print_state
*state
)
224 FILE *fp
= state
->fp
;
225 /* we're going to print the saturate modifier later, after the opcode */
227 print_dest(&dest
->dest
, state
);
229 if (!dest
->dest
.is_ssa
&&
230 dest
->write_mask
!= (1 << dest
->dest
.reg
.reg
->num_components
) - 1) {
232 for (unsigned i
= 0; i
< 4; i
++)
233 if ((dest
->write_mask
>> i
) & 1)
234 fprintf(fp
, "%c", "xyzw"[i
]);
239 print_alu_instr(nir_alu_instr
*instr
, print_state
*state
)
241 FILE *fp
= state
->fp
;
243 print_alu_dest(&instr
->dest
, state
);
245 fprintf(fp
, " = %s", nir_op_infos
[instr
->op
].name
);
248 if (instr
->dest
.saturate
)
252 for (unsigned i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++) {
256 print_alu_src(instr
, i
, state
);
261 get_var_name(nir_variable
*var
, print_state
*state
)
263 if (state
->ht
== NULL
)
268 struct hash_entry
*entry
= _mesa_hash_table_search(state
->ht
, var
);
273 if (var
->name
== NULL
) {
274 name
= ralloc_asprintf(state
->syms
, "@%u", state
->index
++);
276 struct set_entry
*set_entry
= _mesa_set_search(state
->syms
, var
->name
);
277 if (set_entry
!= NULL
) {
278 /* we have a collision with another name, append an @ + a unique
280 name
= ralloc_asprintf(state
->syms
, "%s@%u", var
->name
,
283 /* Mark this one as seen */
284 _mesa_set_add(state
->syms
, var
->name
);
289 _mesa_hash_table_insert(state
->ht
, var
, name
);
295 print_constant(nir_constant
*c
, const struct glsl_type
*type
, print_state
*state
)
297 FILE *fp
= state
->fp
;
298 const unsigned rows
= glsl_get_vector_elements(type
);
299 const unsigned cols
= glsl_get_matrix_columns(type
);
302 switch (glsl_get_base_type(type
)) {
306 /* Only float base types can be matrices. */
309 for (i
= 0; i
< rows
; i
++) {
310 if (i
> 0) fprintf(fp
, ", ");
311 fprintf(fp
, "0x%08x", c
->values
[0].u32
[i
]);
315 case GLSL_TYPE_FLOAT
:
316 for (i
= 0; i
< cols
; i
++) {
317 for (j
= 0; j
< rows
; j
++) {
318 if (i
+ j
> 0) fprintf(fp
, ", ");
319 fprintf(fp
, "%f", c
->values
[i
].f32
[j
]);
324 case GLSL_TYPE_DOUBLE
:
325 for (i
= 0; i
< cols
; i
++) {
326 for (j
= 0; j
< rows
; j
++) {
327 if (i
+ j
> 0) fprintf(fp
, ", ");
328 fprintf(fp
, "%f", c
->values
[i
].f64
[j
]);
333 case GLSL_TYPE_UINT64
:
334 case GLSL_TYPE_INT64
:
335 /* Only float base types can be matrices. */
338 for (i
= 0; i
< cols
; i
++) {
339 if (i
> 0) fprintf(fp
, ", ");
340 fprintf(fp
, "0x%08" PRIx64
, c
->values
[0].u64
[i
]);
344 case GLSL_TYPE_STRUCT
:
345 for (i
= 0; i
< c
->num_elements
; i
++) {
346 if (i
> 0) fprintf(fp
, ", ");
348 print_constant(c
->elements
[i
], glsl_get_struct_field(type
, i
), state
);
353 case GLSL_TYPE_ARRAY
:
354 for (i
= 0; i
< c
->num_elements
; i
++) {
355 if (i
> 0) fprintf(fp
, ", ");
357 print_constant(c
->elements
[i
], glsl_get_array_element(type
), state
);
363 unreachable("not reached");
368 get_variable_mode_str(nir_variable_mode mode
)
371 case nir_var_shader_in
:
373 case nir_var_shader_out
:
375 case nir_var_uniform
:
377 case nir_var_shader_storage
:
378 return "shader_storage";
379 case nir_var_system_value
:
392 print_var_decl(nir_variable
*var
, print_state
*state
)
394 FILE *fp
= state
->fp
;
396 fprintf(fp
, "decl_var ");
398 const char *const cent
= (var
->data
.centroid
) ? "centroid " : "";
399 const char *const samp
= (var
->data
.sample
) ? "sample " : "";
400 const char *const patch
= (var
->data
.patch
) ? "patch " : "";
401 const char *const inv
= (var
->data
.invariant
) ? "invariant " : "";
402 fprintf(fp
, "%s%s%s%s%s %s ",
403 cent
, samp
, patch
, inv
, get_variable_mode_str(var
->data
.mode
),
404 glsl_interp_mode_name(var
->data
.interpolation
));
406 const char *const coher
= (var
->data
.image
.coherent
) ? "coherent " : "";
407 const char *const volat
= (var
->data
.image
._volatile
) ? "volatile " : "";
408 const char *const restr
= (var
->data
.image
.restrict_flag
) ? "restrict " : "";
409 const char *const ronly
= (var
->data
.image
.read_only
) ? "readonly " : "";
410 const char *const wonly
= (var
->data
.image
.write_only
) ? "writeonly " : "";
411 fprintf(fp
, "%s%s%s%s%s", coher
, volat
, restr
, ronly
, wonly
);
413 fprintf(fp
, "%s %s", glsl_get_type_name(var
->type
),
414 get_var_name(var
, state
));
416 if (var
->data
.mode
== nir_var_shader_in
||
417 var
->data
.mode
== nir_var_shader_out
||
418 var
->data
.mode
== nir_var_uniform
||
419 var
->data
.mode
== nir_var_shader_storage
) {
420 const char *loc
= NULL
;
423 switch (state
->shader
->stage
) {
424 case MESA_SHADER_VERTEX
:
425 if (var
->data
.mode
== nir_var_shader_in
)
426 loc
= gl_vert_attrib_name(var
->data
.location
);
427 else if (var
->data
.mode
== nir_var_shader_out
)
428 loc
= gl_varying_slot_name(var
->data
.location
);
430 case MESA_SHADER_GEOMETRY
:
431 if ((var
->data
.mode
== nir_var_shader_in
) ||
432 (var
->data
.mode
== nir_var_shader_out
))
433 loc
= gl_varying_slot_name(var
->data
.location
);
435 case MESA_SHADER_FRAGMENT
:
436 if (var
->data
.mode
== nir_var_shader_in
)
437 loc
= gl_varying_slot_name(var
->data
.location
);
438 else if (var
->data
.mode
== nir_var_shader_out
)
439 loc
= gl_frag_result_name(var
->data
.location
);
441 case MESA_SHADER_TESS_CTRL
:
442 case MESA_SHADER_TESS_EVAL
:
443 case MESA_SHADER_COMPUTE
:
450 snprintf(buf
, sizeof(buf
), "%u", var
->data
.location
);
454 fprintf(fp
, " (%s, %u, %u)%s", loc
, var
->data
.driver_location
, var
->data
.binding
,
455 var
->data
.compact
? " compact" : "");
458 if (var
->constant_initializer
) {
459 fprintf(fp
, " = { ");
460 print_constant(var
->constant_initializer
, var
->type
, state
);
465 print_annotation(state
, var
);
469 print_var(nir_variable
*var
, print_state
*state
)
471 FILE *fp
= state
->fp
;
472 fprintf(fp
, "%s", get_var_name(var
, state
));
476 print_arg(nir_variable
*var
, print_state
*state
)
478 FILE *fp
= state
->fp
;
479 fprintf(fp
, "%s %s", glsl_get_type_name(var
->type
),
480 get_var_name(var
, state
));
484 print_deref_var(nir_deref_var
*deref
, print_state
*state
)
486 print_var(deref
->var
, state
);
490 print_deref_array(nir_deref_array
*deref
, print_state
*state
)
492 FILE *fp
= state
->fp
;
494 switch (deref
->deref_array_type
) {
495 case nir_deref_array_type_direct
:
496 fprintf(fp
, "%u", deref
->base_offset
);
498 case nir_deref_array_type_indirect
:
499 if (deref
->base_offset
!= 0)
500 fprintf(fp
, "%u + ", deref
->base_offset
);
501 print_src(&deref
->indirect
, state
);
503 case nir_deref_array_type_wildcard
:
511 print_deref_struct(nir_deref_struct
*deref
, const struct glsl_type
*parent_type
,
514 FILE *fp
= state
->fp
;
515 fprintf(fp
, ".%s", glsl_get_struct_elem_name(parent_type
, deref
->index
));
519 print_deref(nir_deref_var
*deref
, print_state
*state
)
521 nir_deref
*tail
= &deref
->deref
;
522 nir_deref
*pretail
= NULL
;
523 while (tail
!= NULL
) {
524 switch (tail
->deref_type
) {
525 case nir_deref_type_var
:
526 assert(pretail
== NULL
);
527 assert(tail
== &deref
->deref
);
528 print_deref_var(deref
, state
);
531 case nir_deref_type_array
:
532 assert(pretail
!= NULL
);
533 print_deref_array(nir_deref_as_array(tail
), state
);
536 case nir_deref_type_struct
:
537 assert(pretail
!= NULL
);
538 print_deref_struct(nir_deref_as_struct(tail
),
539 pretail
->type
, state
);
543 unreachable("Invalid deref type");
547 tail
= pretail
->child
;
552 print_intrinsic_instr(nir_intrinsic_instr
*instr
, print_state
*state
)
554 const nir_intrinsic_info
*info
= &nir_intrinsic_infos
[instr
->intrinsic
];
555 unsigned num_srcs
= info
->num_srcs
;
556 FILE *fp
= state
->fp
;
558 if (info
->has_dest
) {
559 print_dest(&instr
->dest
, state
);
563 fprintf(fp
, "intrinsic %s (", info
->name
);
565 for (unsigned i
= 0; i
< num_srcs
; i
++) {
569 print_src(&instr
->src
[i
], state
);
574 for (unsigned i
= 0; i
< info
->num_variables
; i
++) {
578 print_deref(instr
->variables
[i
], state
);
583 for (unsigned i
= 0; i
< info
->num_indices
; i
++) {
587 fprintf(fp
, "%d", instr
->const_index
[i
]);
592 static const char *index_name
[NIR_INTRINSIC_NUM_INDEX_FLAGS
] = {
593 [NIR_INTRINSIC_BASE
] = "base",
594 [NIR_INTRINSIC_WRMASK
] = "wrmask",
595 [NIR_INTRINSIC_STREAM_ID
] = "stream-id",
596 [NIR_INTRINSIC_UCP_ID
] = "ucp-id",
597 [NIR_INTRINSIC_RANGE
] = "range",
598 [NIR_INTRINSIC_DESC_SET
] = "desc-set",
599 [NIR_INTRINSIC_BINDING
] = "binding",
600 [NIR_INTRINSIC_COMPONENT
] = "component",
601 [NIR_INTRINSIC_INTERP_MODE
] = "interp_mode",
603 for (unsigned idx
= 1; idx
< NIR_INTRINSIC_NUM_INDEX_FLAGS
; idx
++) {
604 if (!info
->index_map
[idx
])
607 if (idx
== NIR_INTRINSIC_WRMASK
) {
608 /* special case wrmask to show it as a writemask.. */
609 unsigned wrmask
= nir_intrinsic_write_mask(instr
);
610 fprintf(fp
, " wrmask=");
611 for (unsigned i
= 0; i
< 4; i
++)
612 if ((wrmask
>> i
) & 1)
613 fprintf(fp
, "%c", "xyzw"[i
]);
615 unsigned off
= info
->index_map
[idx
] - 1;
616 assert(index_name
[idx
]); /* forgot to update index_name table? */
617 fprintf(fp
, " %s=%d", index_name
[idx
], instr
->const_index
[off
]);
625 struct exec_list
*var_list
= NULL
;
627 switch (instr
->intrinsic
) {
628 case nir_intrinsic_load_uniform
:
629 var_list
= &state
->shader
->uniforms
;
631 case nir_intrinsic_load_input
:
632 case nir_intrinsic_load_per_vertex_input
:
633 var_list
= &state
->shader
->inputs
;
635 case nir_intrinsic_load_output
:
636 case nir_intrinsic_store_output
:
637 case nir_intrinsic_store_per_vertex_output
:
638 var_list
= &state
->shader
->outputs
;
644 nir_foreach_variable(var
, var_list
) {
645 if ((var
->data
.driver_location
== nir_intrinsic_base(instr
)) &&
646 (instr
->intrinsic
== nir_intrinsic_load_uniform
||
647 var
->data
.location_frac
== nir_intrinsic_component(instr
)) &&
649 fprintf(fp
, "\t/* %s */", var
->name
);
656 print_tex_instr(nir_tex_instr
*instr
, print_state
*state
)
658 FILE *fp
= state
->fp
;
660 print_dest(&instr
->dest
, state
);
680 case nir_texop_txf_ms
:
681 fprintf(fp
, "txf_ms ");
683 case nir_texop_txf_ms_mcs
:
684 fprintf(fp
, "txf_ms_mcs ");
695 case nir_texop_query_levels
:
696 fprintf(fp
, "query_levels ");
698 case nir_texop_texture_samples
:
699 fprintf(fp
, "texture_samples ");
701 case nir_texop_samples_identical
:
702 fprintf(fp
, "samples_identical ");
705 unreachable("Invalid texture operation");
709 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++) {
710 print_src(&instr
->src
[i
].src
, state
);
714 switch(instr
->src
[i
].src_type
) {
715 case nir_tex_src_coord
:
716 fprintf(fp
, "(coord)");
718 case nir_tex_src_projector
:
719 fprintf(fp
, "(projector)");
721 case nir_tex_src_comparator
:
722 fprintf(fp
, "(comparator)");
724 case nir_tex_src_offset
:
725 fprintf(fp
, "(offset)");
727 case nir_tex_src_bias
:
728 fprintf(fp
, "(bias)");
730 case nir_tex_src_lod
:
731 fprintf(fp
, "(lod)");
733 case nir_tex_src_ms_index
:
734 fprintf(fp
, "(ms_index)");
736 case nir_tex_src_ms_mcs
:
737 fprintf(fp
, "(ms_mcs)");
739 case nir_tex_src_ddx
:
740 fprintf(fp
, "(ddx)");
742 case nir_tex_src_ddy
:
743 fprintf(fp
, "(ddy)");
745 case nir_tex_src_texture_offset
:
746 fprintf(fp
, "(texture_offset)");
748 case nir_tex_src_sampler_offset
:
749 fprintf(fp
, "(sampler_offset)");
751 case nir_tex_src_plane
:
752 fprintf(fp
, "(plane)");
756 unreachable("Invalid texture source type");
763 if (instr
->op
== nir_texop_tg4
) {
764 fprintf(fp
, "%u (gather_component), ", instr
->component
);
767 if (instr
->texture
) {
768 print_deref(instr
->texture
, state
);
769 fprintf(fp
, " (texture)");
770 if (instr
->sampler
) {
771 print_deref(instr
->sampler
, state
);
772 fprintf(fp
, " (sampler)");
775 assert(instr
->sampler
== NULL
);
776 fprintf(fp
, "%u (texture) %u (sampler)",
777 instr
->texture_index
, instr
->sampler_index
);
782 print_call_instr(nir_call_instr
*instr
, print_state
*state
)
784 FILE *fp
= state
->fp
;
786 fprintf(fp
, "call %s ", instr
->callee
->name
);
788 for (unsigned i
= 0; i
< instr
->num_params
; i
++) {
792 print_deref(instr
->params
[i
], state
);
795 if (instr
->return_deref
!= NULL
) {
796 if (instr
->num_params
!= 0)
798 fprintf(fp
, "returning ");
799 print_deref(instr
->return_deref
, state
);
804 print_load_const_instr(nir_load_const_instr
*instr
, print_state
*state
)
806 FILE *fp
= state
->fp
;
808 print_ssa_def(&instr
->def
, state
);
810 fprintf(fp
, " = load_const (");
812 for (unsigned i
= 0; i
< instr
->def
.num_components
; i
++) {
817 * we don't really know the type of the constant (if it will be used as a
818 * float or an int), so just print the raw constant in hex for fidelity
819 * and then print the float in a comment for readability.
822 if (instr
->def
.bit_size
== 64)
823 fprintf(fp
, "0x%16" PRIx64
" /* %f */", instr
->value
.u64
[i
],
824 instr
->value
.f64
[i
]);
826 fprintf(fp
, "0x%08x /* %f */", instr
->value
.u32
[i
], instr
->value
.f32
[i
]);
833 print_jump_instr(nir_jump_instr
*instr
, print_state
*state
)
835 FILE *fp
= state
->fp
;
837 switch (instr
->type
) {
839 fprintf(fp
, "break");
842 case nir_jump_continue
:
843 fprintf(fp
, "continue");
846 case nir_jump_return
:
847 fprintf(fp
, "return");
853 print_ssa_undef_instr(nir_ssa_undef_instr
* instr
, print_state
*state
)
855 FILE *fp
= state
->fp
;
856 print_ssa_def(&instr
->def
, state
);
857 fprintf(fp
, " = undefined");
861 print_phi_instr(nir_phi_instr
*instr
, print_state
*state
)
863 FILE *fp
= state
->fp
;
864 print_dest(&instr
->dest
, state
);
865 fprintf(fp
, " = phi ");
866 nir_foreach_phi_src(src
, instr
) {
867 if (&src
->node
!= exec_list_get_head(&instr
->srcs
))
870 fprintf(fp
, "block_%u: ", src
->pred
->index
);
871 print_src(&src
->src
, state
);
876 print_parallel_copy_instr(nir_parallel_copy_instr
*instr
, print_state
*state
)
878 FILE *fp
= state
->fp
;
879 nir_foreach_parallel_copy_entry(entry
, instr
) {
880 if (&entry
->node
!= exec_list_get_head(&instr
->entries
))
883 print_dest(&entry
->dest
, state
);
885 print_src(&entry
->src
, state
);
890 print_instr(const nir_instr
*instr
, print_state
*state
, unsigned tabs
)
892 FILE *fp
= state
->fp
;
893 print_tabs(tabs
, fp
);
895 switch (instr
->type
) {
896 case nir_instr_type_alu
:
897 print_alu_instr(nir_instr_as_alu(instr
), state
);
900 case nir_instr_type_call
:
901 print_call_instr(nir_instr_as_call(instr
), state
);
904 case nir_instr_type_intrinsic
:
905 print_intrinsic_instr(nir_instr_as_intrinsic(instr
), state
);
908 case nir_instr_type_tex
:
909 print_tex_instr(nir_instr_as_tex(instr
), state
);
912 case nir_instr_type_load_const
:
913 print_load_const_instr(nir_instr_as_load_const(instr
), state
);
916 case nir_instr_type_jump
:
917 print_jump_instr(nir_instr_as_jump(instr
), state
);
920 case nir_instr_type_ssa_undef
:
921 print_ssa_undef_instr(nir_instr_as_ssa_undef(instr
), state
);
924 case nir_instr_type_phi
:
925 print_phi_instr(nir_instr_as_phi(instr
), state
);
928 case nir_instr_type_parallel_copy
:
929 print_parallel_copy_instr(nir_instr_as_parallel_copy(instr
), state
);
933 unreachable("Invalid instruction type");
939 compare_block_index(const void *p1
, const void *p2
)
941 const nir_block
*block1
= *((const nir_block
**) p1
);
942 const nir_block
*block2
= *((const nir_block
**) p2
);
944 return (int) block1
->index
- (int) block2
->index
;
947 static void print_cf_node(nir_cf_node
*node
, print_state
*state
,
951 print_block(nir_block
*block
, print_state
*state
, unsigned tabs
)
953 FILE *fp
= state
->fp
;
955 print_tabs(tabs
, fp
);
956 fprintf(fp
, "block block_%u:\n", block
->index
);
958 /* sort the predecessors by index so we consistently print the same thing */
961 malloc(block
->predecessors
->entries
* sizeof(nir_block
*));
963 struct set_entry
*entry
;
965 set_foreach(block
->predecessors
, entry
) {
966 preds
[i
++] = (nir_block
*) entry
->key
;
969 qsort(preds
, block
->predecessors
->entries
, sizeof(nir_block
*),
970 compare_block_index
);
972 print_tabs(tabs
, fp
);
973 fprintf(fp
, "/* preds: ");
974 for (unsigned i
= 0; i
< block
->predecessors
->entries
; i
++) {
975 fprintf(fp
, "block_%u ", preds
[i
]->index
);
981 nir_foreach_instr(instr
, block
) {
982 print_instr(instr
, state
, tabs
);
984 print_annotation(state
, instr
);
987 print_tabs(tabs
, fp
);
988 fprintf(fp
, "/* succs: ");
989 for (unsigned i
= 0; i
< 2; i
++)
990 if (block
->successors
[i
]) {
991 fprintf(fp
, "block_%u ", block
->successors
[i
]->index
);
997 print_if(nir_if
*if_stmt
, print_state
*state
, unsigned tabs
)
999 FILE *fp
= state
->fp
;
1001 print_tabs(tabs
, fp
);
1003 print_src(&if_stmt
->condition
, state
);
1004 fprintf(fp
, " {\n");
1005 foreach_list_typed(nir_cf_node
, node
, node
, &if_stmt
->then_list
) {
1006 print_cf_node(node
, state
, tabs
+ 1);
1008 print_tabs(tabs
, fp
);
1009 fprintf(fp
, "} else {\n");
1010 foreach_list_typed(nir_cf_node
, node
, node
, &if_stmt
->else_list
) {
1011 print_cf_node(node
, state
, tabs
+ 1);
1013 print_tabs(tabs
, fp
);
1018 print_loop(nir_loop
*loop
, print_state
*state
, unsigned tabs
)
1020 FILE *fp
= state
->fp
;
1022 print_tabs(tabs
, fp
);
1023 fprintf(fp
, "loop {\n");
1024 foreach_list_typed(nir_cf_node
, node
, node
, &loop
->body
) {
1025 print_cf_node(node
, state
, tabs
+ 1);
1027 print_tabs(tabs
, fp
);
1032 print_cf_node(nir_cf_node
*node
, print_state
*state
, unsigned int tabs
)
1034 switch (node
->type
) {
1035 case nir_cf_node_block
:
1036 print_block(nir_cf_node_as_block(node
), state
, tabs
);
1039 case nir_cf_node_if
:
1040 print_if(nir_cf_node_as_if(node
), state
, tabs
);
1043 case nir_cf_node_loop
:
1044 print_loop(nir_cf_node_as_loop(node
), state
, tabs
);
1048 unreachable("Invalid CFG node type");
1053 print_function_impl(nir_function_impl
*impl
, print_state
*state
)
1055 FILE *fp
= state
->fp
;
1057 fprintf(fp
, "\nimpl %s ", impl
->function
->name
);
1059 for (unsigned i
= 0; i
< impl
->num_params
; i
++) {
1063 print_arg(impl
->params
[i
], state
);
1066 if (impl
->return_var
!= NULL
) {
1067 if (impl
->num_params
!= 0)
1069 fprintf(fp
, "returning ");
1070 print_arg(impl
->return_var
, state
);
1075 nir_foreach_variable(var
, &impl
->locals
) {
1077 print_var_decl(var
, state
);
1080 foreach_list_typed(nir_register
, reg
, node
, &impl
->registers
) {
1082 print_register_decl(reg
, state
);
1085 nir_index_blocks(impl
);
1087 foreach_list_typed(nir_cf_node
, node
, node
, &impl
->body
) {
1088 print_cf_node(node
, state
, 1);
1091 fprintf(fp
, "\tblock block_%u:\n}\n\n", impl
->end_block
->index
);
1095 print_function(nir_function
*function
, print_state
*state
)
1097 FILE *fp
= state
->fp
;
1099 fprintf(fp
, "decl_function %s ", function
->name
);
1101 for (unsigned i
= 0; i
< function
->num_params
; i
++) {
1105 switch (function
->params
[i
].param_type
) {
1106 case nir_parameter_in
:
1109 case nir_parameter_out
:
1110 fprintf(fp
, "out ");
1112 case nir_parameter_inout
:
1113 fprintf(fp
, "inout ");
1116 unreachable("Invalid parameter type");
1119 fprintf(fp
, "%s", glsl_get_type_name(function
->params
[i
].type
));
1122 if (function
->return_type
!= NULL
) {
1123 if (function
->num_params
!= 0)
1125 fprintf(fp
, "returning %s", glsl_get_type_name(function
->return_type
));
1130 if (function
->impl
!= NULL
) {
1131 print_function_impl(function
->impl
, state
);
1137 init_print_state(print_state
*state
, nir_shader
*shader
, FILE *fp
)
1140 state
->shader
= shader
;
1141 state
->ht
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
1142 _mesa_key_pointer_equal
);
1143 state
->syms
= _mesa_set_create(NULL
, _mesa_key_hash_string
,
1144 _mesa_key_string_equal
);
1149 destroy_print_state(print_state
*state
)
1151 _mesa_hash_table_destroy(state
->ht
, NULL
);
1152 _mesa_set_destroy(state
->syms
, NULL
);
1156 nir_print_shader_annotated(nir_shader
*shader
, FILE *fp
,
1157 struct hash_table
*annotations
)
1160 init_print_state(&state
, shader
, fp
);
1162 state
.annotations
= annotations
;
1164 fprintf(fp
, "shader: %s\n", gl_shader_stage_name(shader
->stage
));
1166 if (shader
->info
->name
)
1167 fprintf(fp
, "name: %s\n", shader
->info
->name
);
1169 if (shader
->info
->label
)
1170 fprintf(fp
, "label: %s\n", shader
->info
->label
);
1172 fprintf(fp
, "inputs: %u\n", shader
->num_inputs
);
1173 fprintf(fp
, "outputs: %u\n", shader
->num_outputs
);
1174 fprintf(fp
, "uniforms: %u\n", shader
->num_uniforms
);
1175 fprintf(fp
, "shared: %u\n", shader
->num_shared
);
1177 nir_foreach_variable(var
, &shader
->uniforms
) {
1178 print_var_decl(var
, &state
);
1181 nir_foreach_variable(var
, &shader
->inputs
) {
1182 print_var_decl(var
, &state
);
1185 nir_foreach_variable(var
, &shader
->outputs
) {
1186 print_var_decl(var
, &state
);
1189 nir_foreach_variable(var
, &shader
->shared
) {
1190 print_var_decl(var
, &state
);
1193 nir_foreach_variable(var
, &shader
->globals
) {
1194 print_var_decl(var
, &state
);
1197 nir_foreach_variable(var
, &shader
->system_values
) {
1198 print_var_decl(var
, &state
);
1201 foreach_list_typed(nir_register
, reg
, node
, &shader
->registers
) {
1202 print_register_decl(reg
, &state
);
1205 foreach_list_typed(nir_function
, func
, node
, &shader
->functions
) {
1206 print_function(func
, &state
);
1209 destroy_print_state(&state
);
1213 nir_print_shader(nir_shader
*shader
, FILE *fp
)
1215 nir_print_shader_annotated(shader
, fp
, NULL
);
1219 nir_print_instr(const nir_instr
*instr
, FILE *fp
)
1221 print_state state
= {
1224 print_instr(instr
, &state
, 0);