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 */
35 print_tabs(unsigned num_tabs
, FILE *fp
)
37 for (unsigned i
= 0; i
< num_tabs
; i
++)
44 /** map from nir_variable -> printable name */
45 struct hash_table
*ht
;
47 /** set of names used so far for nir_variables */
50 /* an index used to make new non-conflicting names */
54 * Optional table of annotations mapping nir object
55 * (such as instr or var) to message to print.
57 struct hash_table
*annotations
;
61 print_annotation(print_state
*state
, void *obj
)
63 if (!state
->annotations
)
66 struct hash_entry
*entry
= _mesa_hash_table_search(state
->annotations
, obj
);
70 const char *note
= entry
->data
;
71 _mesa_hash_table_remove(state
->annotations
, entry
);
73 fprintf(stderr
, "%s\n\n", note
);
77 print_register(nir_register
*reg
, print_state
*state
)
80 if (reg
->name
!= NULL
)
81 fprintf(fp
, "/* %s */ ", reg
->name
);
83 fprintf(fp
, "gr%u", reg
->index
);
85 fprintf(fp
, "r%u", reg
->index
);
88 static const char *sizes
[] = { "error", "vec1", "vec2", "vec3", "vec4" };
91 print_register_decl(nir_register
*reg
, print_state
*state
)
94 fprintf(fp
, "decl_reg %s %u ", sizes
[reg
->num_components
], reg
->bit_size
);
96 fprintf(fp
, "(packed) ");
97 print_register(reg
, state
);
98 if (reg
->num_array_elems
!= 0)
99 fprintf(fp
, "[%u]", reg
->num_array_elems
);
104 print_ssa_def(nir_ssa_def
*def
, print_state
*state
)
106 FILE *fp
= state
->fp
;
107 if (def
->name
!= NULL
)
108 fprintf(fp
, "/* %s */ ", def
->name
);
109 fprintf(fp
, "%s %u ssa_%u", sizes
[def
->num_components
], def
->bit_size
,
114 print_ssa_use(nir_ssa_def
*def
, print_state
*state
)
116 FILE *fp
= state
->fp
;
117 if (def
->name
!= NULL
)
118 fprintf(fp
, "/* %s */ ", def
->name
);
119 fprintf(fp
, "ssa_%u", def
->index
);
122 static void print_src(nir_src
*src
, print_state
*state
);
125 print_reg_src(nir_reg_src
*src
, print_state
*state
)
127 FILE *fp
= state
->fp
;
128 print_register(src
->reg
, state
);
129 if (src
->reg
->num_array_elems
!= 0) {
130 fprintf(fp
, "[%u", src
->base_offset
);
131 if (src
->indirect
!= NULL
) {
133 print_src(src
->indirect
, state
);
140 print_reg_dest(nir_reg_dest
*dest
, print_state
*state
)
142 FILE *fp
= state
->fp
;
143 print_register(dest
->reg
, state
);
144 if (dest
->reg
->num_array_elems
!= 0) {
145 fprintf(fp
, "[%u", dest
->base_offset
);
146 if (dest
->indirect
!= NULL
) {
148 print_src(dest
->indirect
, state
);
155 print_src(nir_src
*src
, print_state
*state
)
158 print_ssa_use(src
->ssa
, state
);
160 print_reg_src(&src
->reg
, state
);
164 print_dest(nir_dest
*dest
, print_state
*state
)
167 print_ssa_def(&dest
->ssa
, state
);
169 print_reg_dest(&dest
->reg
, state
);
173 print_alu_src(nir_alu_instr
*instr
, unsigned src
, print_state
*state
)
175 FILE *fp
= state
->fp
;
177 if (instr
->src
[src
].negate
)
179 if (instr
->src
[src
].abs
)
182 print_src(&instr
->src
[src
].src
, state
);
184 bool print_swizzle
= false;
185 unsigned used_channels
= 0;
187 for (unsigned i
= 0; i
< 4; i
++) {
188 if (!nir_alu_instr_channel_used(instr
, src
, i
))
193 if (instr
->src
[src
].swizzle
[i
] != i
) {
194 print_swizzle
= true;
199 unsigned live_channels
= instr
->src
[src
].src
.is_ssa
200 ? instr
->src
[src
].src
.ssa
->num_components
201 : instr
->src
[src
].src
.reg
.reg
->num_components
;
203 if (print_swizzle
|| used_channels
!= live_channels
) {
205 for (unsigned i
= 0; i
< 4; i
++) {
206 if (!nir_alu_instr_channel_used(instr
, src
, i
))
209 fprintf(fp
, "%c", "xyzw"[instr
->src
[src
].swizzle
[i
]]);
213 if (instr
->src
[src
].abs
)
218 print_alu_dest(nir_alu_dest
*dest
, print_state
*state
)
220 FILE *fp
= state
->fp
;
221 /* we're going to print the saturate modifier later, after the opcode */
223 print_dest(&dest
->dest
, state
);
225 if (!dest
->dest
.is_ssa
&&
226 dest
->write_mask
!= (1 << dest
->dest
.reg
.reg
->num_components
) - 1) {
228 for (unsigned i
= 0; i
< 4; i
++)
229 if ((dest
->write_mask
>> i
) & 1)
230 fprintf(fp
, "%c", "xyzw"[i
]);
235 print_alu_instr(nir_alu_instr
*instr
, print_state
*state
)
237 FILE *fp
= state
->fp
;
239 print_alu_dest(&instr
->dest
, state
);
241 fprintf(fp
, " = %s", nir_op_infos
[instr
->op
].name
);
244 if (instr
->dest
.saturate
)
248 for (unsigned i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++) {
252 print_alu_src(instr
, i
, state
);
257 get_var_name(nir_variable
*var
, print_state
*state
)
259 if (state
->ht
== NULL
)
260 return var
->name
? var
->name
: "unnamed";
264 struct hash_entry
*entry
= _mesa_hash_table_search(state
->ht
, var
);
269 if (var
->name
== NULL
) {
270 name
= ralloc_asprintf(state
->syms
, "@%u", state
->index
++);
272 struct set_entry
*set_entry
= _mesa_set_search(state
->syms
, var
->name
);
273 if (set_entry
!= NULL
) {
274 /* we have a collision with another name, append an @ + a unique
276 name
= ralloc_asprintf(state
->syms
, "%s@%u", var
->name
,
279 /* Mark this one as seen */
280 _mesa_set_add(state
->syms
, var
->name
);
285 _mesa_hash_table_insert(state
->ht
, var
, name
);
291 print_constant(nir_constant
*c
, const struct glsl_type
*type
, print_state
*state
)
293 FILE *fp
= state
->fp
;
294 const unsigned rows
= glsl_get_vector_elements(type
);
295 const unsigned cols
= glsl_get_matrix_columns(type
);
298 switch (glsl_get_base_type(type
)) {
302 /* Only float base types can be matrices. */
305 for (i
= 0; i
< rows
; i
++) {
306 if (i
> 0) fprintf(fp
, ", ");
307 fprintf(fp
, "0x%08x", c
->values
[0].u32
[i
]);
311 case GLSL_TYPE_FLOAT
:
312 for (i
= 0; i
< cols
; i
++) {
313 for (j
= 0; j
< rows
; j
++) {
314 if (i
+ j
> 0) fprintf(fp
, ", ");
315 fprintf(fp
, "%f", c
->values
[i
].f32
[j
]);
320 case GLSL_TYPE_DOUBLE
:
321 for (i
= 0; i
< cols
; i
++) {
322 for (j
= 0; j
< rows
; j
++) {
323 if (i
+ j
> 0) fprintf(fp
, ", ");
324 fprintf(fp
, "%f", c
->values
[i
].f64
[j
]);
329 case GLSL_TYPE_UINT64
:
330 case GLSL_TYPE_INT64
:
331 /* Only float base types can be matrices. */
334 for (i
= 0; i
< cols
; i
++) {
335 if (i
> 0) fprintf(fp
, ", ");
336 fprintf(fp
, "0x%08" PRIx64
, c
->values
[0].u64
[i
]);
340 case GLSL_TYPE_STRUCT
:
341 for (i
= 0; i
< c
->num_elements
; i
++) {
342 if (i
> 0) fprintf(fp
, ", ");
344 print_constant(c
->elements
[i
], glsl_get_struct_field(type
, i
), state
);
349 case GLSL_TYPE_ARRAY
:
350 for (i
= 0; i
< c
->num_elements
; i
++) {
351 if (i
> 0) fprintf(fp
, ", ");
353 print_constant(c
->elements
[i
], glsl_get_array_element(type
), state
);
359 unreachable("not reached");
364 get_variable_mode_str(nir_variable_mode mode
)
367 case nir_var_shader_in
:
369 case nir_var_shader_out
:
371 case nir_var_uniform
:
373 case nir_var_shader_storage
:
374 return "shader_storage";
375 case nir_var_system_value
:
388 print_var_decl(nir_variable
*var
, print_state
*state
)
390 FILE *fp
= state
->fp
;
392 fprintf(fp
, "decl_var ");
394 const char *const cent
= (var
->data
.centroid
) ? "centroid " : "";
395 const char *const samp
= (var
->data
.sample
) ? "sample " : "";
396 const char *const patch
= (var
->data
.patch
) ? "patch " : "";
397 const char *const inv
= (var
->data
.invariant
) ? "invariant " : "";
398 fprintf(fp
, "%s%s%s%s%s %s ",
399 cent
, samp
, patch
, inv
, get_variable_mode_str(var
->data
.mode
),
400 glsl_interp_mode_name(var
->data
.interpolation
));
402 const char *const coher
= (var
->data
.image
.coherent
) ? "coherent " : "";
403 const char *const volat
= (var
->data
.image
._volatile
) ? "volatile " : "";
404 const char *const restr
= (var
->data
.image
.restrict_flag
) ? "restrict " : "";
405 const char *const ronly
= (var
->data
.image
.read_only
) ? "readonly " : "";
406 const char *const wonly
= (var
->data
.image
.write_only
) ? "writeonly " : "";
407 fprintf(fp
, "%s%s%s%s%s", coher
, volat
, restr
, ronly
, wonly
);
409 fprintf(fp
, "%s %s", glsl_get_type_name(var
->type
),
410 get_var_name(var
, state
));
412 if (var
->data
.mode
== nir_var_shader_in
||
413 var
->data
.mode
== nir_var_shader_out
||
414 var
->data
.mode
== nir_var_uniform
||
415 var
->data
.mode
== nir_var_shader_storage
) {
416 const char *loc
= NULL
;
419 switch (state
->shader
->stage
) {
420 case MESA_SHADER_VERTEX
:
421 if (var
->data
.mode
== nir_var_shader_in
)
422 loc
= gl_vert_attrib_name(var
->data
.location
);
423 else if (var
->data
.mode
== nir_var_shader_out
)
424 loc
= gl_varying_slot_name(var
->data
.location
);
426 case MESA_SHADER_GEOMETRY
:
427 if ((var
->data
.mode
== nir_var_shader_in
) ||
428 (var
->data
.mode
== nir_var_shader_out
))
429 loc
= gl_varying_slot_name(var
->data
.location
);
431 case MESA_SHADER_FRAGMENT
:
432 if (var
->data
.mode
== nir_var_shader_in
)
433 loc
= gl_varying_slot_name(var
->data
.location
);
434 else if (var
->data
.mode
== nir_var_shader_out
)
435 loc
= gl_frag_result_name(var
->data
.location
);
437 case MESA_SHADER_TESS_CTRL
:
438 case MESA_SHADER_TESS_EVAL
:
439 case MESA_SHADER_COMPUTE
:
446 snprintf(buf
, sizeof(buf
), "%u", var
->data
.location
);
450 fprintf(fp
, " (%s, %u, %u)%s", loc
, var
->data
.driver_location
, var
->data
.binding
,
451 var
->data
.compact
? " compact" : "");
454 if (var
->constant_initializer
) {
455 fprintf(fp
, " = { ");
456 print_constant(var
->constant_initializer
, var
->type
, state
);
461 print_annotation(state
, var
);
465 print_var(nir_variable
*var
, print_state
*state
)
467 FILE *fp
= state
->fp
;
468 fprintf(fp
, "%s", get_var_name(var
, state
));
472 print_arg(nir_variable
*var
, print_state
*state
)
474 FILE *fp
= state
->fp
;
475 fprintf(fp
, "%s %s", glsl_get_type_name(var
->type
),
476 get_var_name(var
, state
));
480 print_deref_var(nir_deref_var
*deref
, print_state
*state
)
482 print_var(deref
->var
, state
);
486 print_deref_array(nir_deref_array
*deref
, print_state
*state
)
488 FILE *fp
= state
->fp
;
490 switch (deref
->deref_array_type
) {
491 case nir_deref_array_type_direct
:
492 fprintf(fp
, "%u", deref
->base_offset
);
494 case nir_deref_array_type_indirect
:
495 if (deref
->base_offset
!= 0)
496 fprintf(fp
, "%u + ", deref
->base_offset
);
497 print_src(&deref
->indirect
, state
);
499 case nir_deref_array_type_wildcard
:
507 print_deref_struct(nir_deref_struct
*deref
, const struct glsl_type
*parent_type
,
510 FILE *fp
= state
->fp
;
511 fprintf(fp
, ".%s", glsl_get_struct_elem_name(parent_type
, deref
->index
));
515 print_deref(nir_deref_var
*deref
, print_state
*state
)
517 nir_deref
*tail
= &deref
->deref
;
518 nir_deref
*pretail
= NULL
;
519 while (tail
!= NULL
) {
520 switch (tail
->deref_type
) {
521 case nir_deref_type_var
:
522 assert(pretail
== NULL
);
523 assert(tail
== &deref
->deref
);
524 print_deref_var(deref
, state
);
527 case nir_deref_type_array
:
528 assert(pretail
!= NULL
);
529 print_deref_array(nir_deref_as_array(tail
), state
);
532 case nir_deref_type_struct
:
533 assert(pretail
!= NULL
);
534 print_deref_struct(nir_deref_as_struct(tail
),
535 pretail
->type
, state
);
539 unreachable("Invalid deref type");
543 tail
= pretail
->child
;
548 print_intrinsic_instr(nir_intrinsic_instr
*instr
, print_state
*state
)
550 const nir_intrinsic_info
*info
= &nir_intrinsic_infos
[instr
->intrinsic
];
551 unsigned num_srcs
= info
->num_srcs
;
552 FILE *fp
= state
->fp
;
554 if (info
->has_dest
) {
555 print_dest(&instr
->dest
, state
);
559 fprintf(fp
, "intrinsic %s (", info
->name
);
561 for (unsigned i
= 0; i
< num_srcs
; i
++) {
565 print_src(&instr
->src
[i
], state
);
570 for (unsigned i
= 0; i
< info
->num_variables
; i
++) {
574 print_deref(instr
->variables
[i
], state
);
579 for (unsigned i
= 0; i
< info
->num_indices
; i
++) {
583 fprintf(fp
, "%d", instr
->const_index
[i
]);
588 static const char *index_name
[NIR_INTRINSIC_NUM_INDEX_FLAGS
] = {
589 [NIR_INTRINSIC_BASE
] = "base",
590 [NIR_INTRINSIC_WRMASK
] = "wrmask",
591 [NIR_INTRINSIC_STREAM_ID
] = "stream-id",
592 [NIR_INTRINSIC_UCP_ID
] = "ucp-id",
593 [NIR_INTRINSIC_RANGE
] = "range",
594 [NIR_INTRINSIC_DESC_SET
] = "desc-set",
595 [NIR_INTRINSIC_BINDING
] = "binding",
596 [NIR_INTRINSIC_COMPONENT
] = "component",
597 [NIR_INTRINSIC_INTERP_MODE
] = "interp_mode",
599 for (unsigned idx
= 1; idx
< NIR_INTRINSIC_NUM_INDEX_FLAGS
; idx
++) {
600 if (!info
->index_map
[idx
])
603 if (idx
== NIR_INTRINSIC_WRMASK
) {
604 /* special case wrmask to show it as a writemask.. */
605 unsigned wrmask
= nir_intrinsic_write_mask(instr
);
606 fprintf(fp
, " wrmask=");
607 for (unsigned i
= 0; i
< 4; i
++)
608 if ((wrmask
>> i
) & 1)
609 fprintf(fp
, "%c", "xyzw"[i
]);
611 unsigned off
= info
->index_map
[idx
] - 1;
612 assert(index_name
[idx
]); /* forgot to update index_name table? */
613 fprintf(fp
, " %s=%d", index_name
[idx
], instr
->const_index
[off
]);
621 struct exec_list
*var_list
= NULL
;
623 switch (instr
->intrinsic
) {
624 case nir_intrinsic_load_uniform
:
625 var_list
= &state
->shader
->uniforms
;
627 case nir_intrinsic_load_input
:
628 case nir_intrinsic_load_per_vertex_input
:
629 var_list
= &state
->shader
->inputs
;
631 case nir_intrinsic_load_output
:
632 case nir_intrinsic_store_output
:
633 case nir_intrinsic_store_per_vertex_output
:
634 var_list
= &state
->shader
->outputs
;
640 nir_foreach_variable(var
, var_list
) {
641 if ((var
->data
.driver_location
== nir_intrinsic_base(instr
)) &&
642 (instr
->intrinsic
== nir_intrinsic_load_uniform
||
643 var
->data
.location_frac
== nir_intrinsic_component(instr
)) &&
645 fprintf(fp
, "\t/* %s */", var
->name
);
652 print_tex_instr(nir_tex_instr
*instr
, print_state
*state
)
654 FILE *fp
= state
->fp
;
656 print_dest(&instr
->dest
, state
);
676 case nir_texop_txf_ms
:
677 fprintf(fp
, "txf_ms ");
679 case nir_texop_txf_ms_mcs
:
680 fprintf(fp
, "txf_ms_mcs ");
691 case nir_texop_query_levels
:
692 fprintf(fp
, "query_levels ");
694 case nir_texop_texture_samples
:
695 fprintf(fp
, "texture_samples ");
697 case nir_texop_samples_identical
:
698 fprintf(fp
, "samples_identical ");
701 unreachable("Invalid texture operation");
705 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++) {
706 print_src(&instr
->src
[i
].src
, state
);
710 switch(instr
->src
[i
].src_type
) {
711 case nir_tex_src_coord
:
712 fprintf(fp
, "(coord)");
714 case nir_tex_src_projector
:
715 fprintf(fp
, "(projector)");
717 case nir_tex_src_comparator
:
718 fprintf(fp
, "(comparator)");
720 case nir_tex_src_offset
:
721 fprintf(fp
, "(offset)");
723 case nir_tex_src_bias
:
724 fprintf(fp
, "(bias)");
726 case nir_tex_src_lod
:
727 fprintf(fp
, "(lod)");
729 case nir_tex_src_ms_index
:
730 fprintf(fp
, "(ms_index)");
732 case nir_tex_src_ms_mcs
:
733 fprintf(fp
, "(ms_mcs)");
735 case nir_tex_src_ddx
:
736 fprintf(fp
, "(ddx)");
738 case nir_tex_src_ddy
:
739 fprintf(fp
, "(ddy)");
741 case nir_tex_src_texture_offset
:
742 fprintf(fp
, "(texture_offset)");
744 case nir_tex_src_sampler_offset
:
745 fprintf(fp
, "(sampler_offset)");
747 case nir_tex_src_plane
:
748 fprintf(fp
, "(plane)");
752 unreachable("Invalid texture source type");
759 if (instr
->op
== nir_texop_tg4
) {
760 fprintf(fp
, "%u (gather_component), ", instr
->component
);
763 if (instr
->texture
) {
764 print_deref(instr
->texture
, state
);
765 fprintf(fp
, " (texture)");
766 if (instr
->sampler
) {
767 print_deref(instr
->sampler
, state
);
768 fprintf(fp
, " (sampler)");
771 assert(instr
->sampler
== NULL
);
772 fprintf(fp
, "%u (texture) %u (sampler)",
773 instr
->texture_index
, instr
->sampler_index
);
778 print_call_instr(nir_call_instr
*instr
, print_state
*state
)
780 FILE *fp
= state
->fp
;
782 fprintf(fp
, "call %s ", instr
->callee
->name
);
784 for (unsigned i
= 0; i
< instr
->num_params
; i
++) {
788 print_deref(instr
->params
[i
], state
);
791 if (instr
->return_deref
!= NULL
) {
792 if (instr
->num_params
!= 0)
794 fprintf(fp
, "returning ");
795 print_deref(instr
->return_deref
, state
);
800 print_load_const_instr(nir_load_const_instr
*instr
, print_state
*state
)
802 FILE *fp
= state
->fp
;
804 print_ssa_def(&instr
->def
, state
);
806 fprintf(fp
, " = load_const (");
808 for (unsigned i
= 0; i
< instr
->def
.num_components
; i
++) {
813 * we don't really know the type of the constant (if it will be used as a
814 * float or an int), so just print the raw constant in hex for fidelity
815 * and then print the float in a comment for readability.
818 if (instr
->def
.bit_size
== 64)
819 fprintf(fp
, "0x%16" PRIx64
" /* %f */", instr
->value
.u64
[i
],
820 instr
->value
.f64
[i
]);
822 fprintf(fp
, "0x%08x /* %f */", instr
->value
.u32
[i
], instr
->value
.f32
[i
]);
829 print_jump_instr(nir_jump_instr
*instr
, print_state
*state
)
831 FILE *fp
= state
->fp
;
833 switch (instr
->type
) {
835 fprintf(fp
, "break");
838 case nir_jump_continue
:
839 fprintf(fp
, "continue");
842 case nir_jump_return
:
843 fprintf(fp
, "return");
849 print_ssa_undef_instr(nir_ssa_undef_instr
* instr
, print_state
*state
)
851 FILE *fp
= state
->fp
;
852 print_ssa_def(&instr
->def
, state
);
853 fprintf(fp
, " = undefined");
857 print_phi_instr(nir_phi_instr
*instr
, print_state
*state
)
859 FILE *fp
= state
->fp
;
860 print_dest(&instr
->dest
, state
);
861 fprintf(fp
, " = phi ");
862 nir_foreach_phi_src(src
, instr
) {
863 if (&src
->node
!= exec_list_get_head(&instr
->srcs
))
866 fprintf(fp
, "block_%u: ", src
->pred
->index
);
867 print_src(&src
->src
, state
);
872 print_parallel_copy_instr(nir_parallel_copy_instr
*instr
, print_state
*state
)
874 FILE *fp
= state
->fp
;
875 nir_foreach_parallel_copy_entry(entry
, instr
) {
876 if (&entry
->node
!= exec_list_get_head(&instr
->entries
))
879 print_dest(&entry
->dest
, state
);
881 print_src(&entry
->src
, state
);
886 print_instr(const nir_instr
*instr
, print_state
*state
, unsigned tabs
)
888 FILE *fp
= state
->fp
;
889 print_tabs(tabs
, fp
);
891 switch (instr
->type
) {
892 case nir_instr_type_alu
:
893 print_alu_instr(nir_instr_as_alu(instr
), state
);
896 case nir_instr_type_call
:
897 print_call_instr(nir_instr_as_call(instr
), state
);
900 case nir_instr_type_intrinsic
:
901 print_intrinsic_instr(nir_instr_as_intrinsic(instr
), state
);
904 case nir_instr_type_tex
:
905 print_tex_instr(nir_instr_as_tex(instr
), state
);
908 case nir_instr_type_load_const
:
909 print_load_const_instr(nir_instr_as_load_const(instr
), state
);
912 case nir_instr_type_jump
:
913 print_jump_instr(nir_instr_as_jump(instr
), state
);
916 case nir_instr_type_ssa_undef
:
917 print_ssa_undef_instr(nir_instr_as_ssa_undef(instr
), state
);
920 case nir_instr_type_phi
:
921 print_phi_instr(nir_instr_as_phi(instr
), state
);
924 case nir_instr_type_parallel_copy
:
925 print_parallel_copy_instr(nir_instr_as_parallel_copy(instr
), state
);
929 unreachable("Invalid instruction type");
935 compare_block_index(const void *p1
, const void *p2
)
937 const nir_block
*block1
= *((const nir_block
**) p1
);
938 const nir_block
*block2
= *((const nir_block
**) p2
);
940 return (int) block1
->index
- (int) block2
->index
;
943 static void print_cf_node(nir_cf_node
*node
, print_state
*state
,
947 print_block(nir_block
*block
, print_state
*state
, unsigned tabs
)
949 FILE *fp
= state
->fp
;
951 print_tabs(tabs
, fp
);
952 fprintf(fp
, "block block_%u:\n", block
->index
);
954 /* sort the predecessors by index so we consistently print the same thing */
957 malloc(block
->predecessors
->entries
* sizeof(nir_block
*));
959 struct set_entry
*entry
;
961 set_foreach(block
->predecessors
, entry
) {
962 preds
[i
++] = (nir_block
*) entry
->key
;
965 qsort(preds
, block
->predecessors
->entries
, sizeof(nir_block
*),
966 compare_block_index
);
968 print_tabs(tabs
, fp
);
969 fprintf(fp
, "/* preds: ");
970 for (unsigned i
= 0; i
< block
->predecessors
->entries
; i
++) {
971 fprintf(fp
, "block_%u ", preds
[i
]->index
);
977 nir_foreach_instr(instr
, block
) {
978 print_instr(instr
, state
, tabs
);
980 print_annotation(state
, instr
);
983 print_tabs(tabs
, fp
);
984 fprintf(fp
, "/* succs: ");
985 for (unsigned i
= 0; i
< 2; i
++)
986 if (block
->successors
[i
]) {
987 fprintf(fp
, "block_%u ", block
->successors
[i
]->index
);
993 print_if(nir_if
*if_stmt
, print_state
*state
, unsigned tabs
)
995 FILE *fp
= state
->fp
;
997 print_tabs(tabs
, fp
);
999 print_src(&if_stmt
->condition
, state
);
1000 fprintf(fp
, " {\n");
1001 foreach_list_typed(nir_cf_node
, node
, node
, &if_stmt
->then_list
) {
1002 print_cf_node(node
, state
, tabs
+ 1);
1004 print_tabs(tabs
, fp
);
1005 fprintf(fp
, "} else {\n");
1006 foreach_list_typed(nir_cf_node
, node
, node
, &if_stmt
->else_list
) {
1007 print_cf_node(node
, state
, tabs
+ 1);
1009 print_tabs(tabs
, fp
);
1014 print_loop(nir_loop
*loop
, print_state
*state
, unsigned tabs
)
1016 FILE *fp
= state
->fp
;
1018 print_tabs(tabs
, fp
);
1019 fprintf(fp
, "loop {\n");
1020 foreach_list_typed(nir_cf_node
, node
, node
, &loop
->body
) {
1021 print_cf_node(node
, state
, tabs
+ 1);
1023 print_tabs(tabs
, fp
);
1028 print_cf_node(nir_cf_node
*node
, print_state
*state
, unsigned int tabs
)
1030 switch (node
->type
) {
1031 case nir_cf_node_block
:
1032 print_block(nir_cf_node_as_block(node
), state
, tabs
);
1035 case nir_cf_node_if
:
1036 print_if(nir_cf_node_as_if(node
), state
, tabs
);
1039 case nir_cf_node_loop
:
1040 print_loop(nir_cf_node_as_loop(node
), state
, tabs
);
1044 unreachable("Invalid CFG node type");
1049 print_function_impl(nir_function_impl
*impl
, print_state
*state
)
1051 FILE *fp
= state
->fp
;
1053 fprintf(fp
, "\nimpl %s ", impl
->function
->name
);
1055 for (unsigned i
= 0; i
< impl
->num_params
; i
++) {
1059 print_arg(impl
->params
[i
], state
);
1062 if (impl
->return_var
!= NULL
) {
1063 if (impl
->num_params
!= 0)
1065 fprintf(fp
, "returning ");
1066 print_arg(impl
->return_var
, state
);
1071 nir_foreach_variable(var
, &impl
->locals
) {
1073 print_var_decl(var
, state
);
1076 foreach_list_typed(nir_register
, reg
, node
, &impl
->registers
) {
1078 print_register_decl(reg
, state
);
1081 nir_index_blocks(impl
);
1083 foreach_list_typed(nir_cf_node
, node
, node
, &impl
->body
) {
1084 print_cf_node(node
, state
, 1);
1087 fprintf(fp
, "\tblock block_%u:\n}\n\n", impl
->end_block
->index
);
1091 print_function(nir_function
*function
, print_state
*state
)
1093 FILE *fp
= state
->fp
;
1095 fprintf(fp
, "decl_function %s ", function
->name
);
1097 for (unsigned i
= 0; i
< function
->num_params
; i
++) {
1101 switch (function
->params
[i
].param_type
) {
1102 case nir_parameter_in
:
1105 case nir_parameter_out
:
1106 fprintf(fp
, "out ");
1108 case nir_parameter_inout
:
1109 fprintf(fp
, "inout ");
1112 unreachable("Invalid parameter type");
1115 fprintf(fp
, "%s", glsl_get_type_name(function
->params
[i
].type
));
1118 if (function
->return_type
!= NULL
) {
1119 if (function
->num_params
!= 0)
1121 fprintf(fp
, "returning %s", glsl_get_type_name(function
->return_type
));
1126 if (function
->impl
!= NULL
) {
1127 print_function_impl(function
->impl
, state
);
1133 init_print_state(print_state
*state
, nir_shader
*shader
, FILE *fp
)
1136 state
->shader
= shader
;
1137 state
->ht
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
1138 _mesa_key_pointer_equal
);
1139 state
->syms
= _mesa_set_create(NULL
, _mesa_key_hash_string
,
1140 _mesa_key_string_equal
);
1145 destroy_print_state(print_state
*state
)
1147 _mesa_hash_table_destroy(state
->ht
, NULL
);
1148 _mesa_set_destroy(state
->syms
, NULL
);
1152 nir_print_shader_annotated(nir_shader
*shader
, FILE *fp
,
1153 struct hash_table
*annotations
)
1156 init_print_state(&state
, shader
, fp
);
1158 state
.annotations
= annotations
;
1160 fprintf(fp
, "shader: %s\n", gl_shader_stage_name(shader
->stage
));
1162 if (shader
->info
.name
)
1163 fprintf(fp
, "name: %s\n", shader
->info
.name
);
1165 if (shader
->info
.label
)
1166 fprintf(fp
, "label: %s\n", shader
->info
.label
);
1168 switch (shader
->stage
) {
1169 case MESA_SHADER_COMPUTE
:
1170 fprintf(fp
, "local-size: %u, %u, %u%s\n",
1171 shader
->info
.cs
.local_size
[0],
1172 shader
->info
.cs
.local_size
[1],
1173 shader
->info
.cs
.local_size
[2],
1174 shader
->info
.cs
.local_size_variable
? " (variable)" : "");
1175 fprintf(fp
, "shared-size: %u\n", shader
->info
.cs
.shared_size
);
1181 fprintf(fp
, "inputs: %u\n", shader
->num_inputs
);
1182 fprintf(fp
, "outputs: %u\n", shader
->num_outputs
);
1183 fprintf(fp
, "uniforms: %u\n", shader
->num_uniforms
);
1184 fprintf(fp
, "shared: %u\n", shader
->num_shared
);
1186 nir_foreach_variable(var
, &shader
->uniforms
) {
1187 print_var_decl(var
, &state
);
1190 nir_foreach_variable(var
, &shader
->inputs
) {
1191 print_var_decl(var
, &state
);
1194 nir_foreach_variable(var
, &shader
->outputs
) {
1195 print_var_decl(var
, &state
);
1198 nir_foreach_variable(var
, &shader
->shared
) {
1199 print_var_decl(var
, &state
);
1202 nir_foreach_variable(var
, &shader
->globals
) {
1203 print_var_decl(var
, &state
);
1206 nir_foreach_variable(var
, &shader
->system_values
) {
1207 print_var_decl(var
, &state
);
1210 foreach_list_typed(nir_register
, reg
, node
, &shader
->registers
) {
1211 print_register_decl(reg
, &state
);
1214 foreach_list_typed(nir_function
, func
, node
, &shader
->functions
) {
1215 print_function(func
, &state
);
1218 destroy_print_state(&state
);
1222 nir_print_shader(nir_shader
*shader
, FILE *fp
)
1224 nir_print_shader_annotated(shader
, fp
, NULL
);
1228 nir_print_instr(const nir_instr
*instr
, FILE *fp
)
1230 print_state state
= {
1233 print_instr(instr
, &state
, 0);