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 for (unsigned i
= 0; i
< 4; i
++) {
190 if (!nir_alu_instr_channel_used(instr
, src
, i
))
193 if (instr
->src
[src
].swizzle
[i
] != i
) {
194 print_swizzle
= true;
201 for (unsigned i
= 0; i
< 4; i
++) {
202 if (!nir_alu_instr_channel_used(instr
, src
, i
))
205 fprintf(fp
, "%c", "xyzw"[instr
->src
[src
].swizzle
[i
]]);
209 if (instr
->src
[src
].abs
)
214 print_alu_dest(nir_alu_dest
*dest
, print_state
*state
)
216 FILE *fp
= state
->fp
;
217 /* we're going to print the saturate modifier later, after the opcode */
219 print_dest(&dest
->dest
, state
);
221 if (!dest
->dest
.is_ssa
&&
222 dest
->write_mask
!= (1 << dest
->dest
.reg
.reg
->num_components
) - 1) {
224 for (unsigned i
= 0; i
< 4; i
++)
225 if ((dest
->write_mask
>> i
) & 1)
226 fprintf(fp
, "%c", "xyzw"[i
]);
231 print_alu_instr(nir_alu_instr
*instr
, print_state
*state
)
233 FILE *fp
= state
->fp
;
235 print_alu_dest(&instr
->dest
, state
);
237 fprintf(fp
, " = %s", nir_op_infos
[instr
->op
].name
);
240 if (instr
->dest
.saturate
)
244 for (unsigned i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++) {
248 print_alu_src(instr
, i
, state
);
253 get_var_name(nir_variable
*var
, print_state
*state
)
255 if (state
->ht
== NULL
)
260 struct hash_entry
*entry
= _mesa_hash_table_search(state
->ht
, var
);
265 if (var
->name
== NULL
) {
266 name
= ralloc_asprintf(state
->syms
, "@%u", state
->index
++);
268 struct set_entry
*set_entry
= _mesa_set_search(state
->syms
, var
->name
);
269 if (set_entry
!= NULL
) {
270 /* we have a collision with another name, append an @ + a unique
272 name
= ralloc_asprintf(state
->syms
, "%s@%u", var
->name
,
275 /* Mark this one as seen */
276 _mesa_set_add(state
->syms
, var
->name
);
281 _mesa_hash_table_insert(state
->ht
, var
, name
);
287 print_constant(nir_constant
*c
, const struct glsl_type
*type
, print_state
*state
)
289 FILE *fp
= state
->fp
;
290 unsigned total_elems
= glsl_get_components(type
);
293 switch (glsl_get_base_type(type
)) {
297 for (i
= 0; i
< total_elems
; i
++) {
298 if (i
> 0) fprintf(fp
, ", ");
299 fprintf(fp
, "0x%08x", c
->value
.u
[i
]);
303 case GLSL_TYPE_FLOAT
:
304 for (i
= 0; i
< total_elems
; i
++) {
305 if (i
> 0) fprintf(fp
, ", ");
306 fprintf(fp
, "%f", c
->value
.f
[i
]);
310 case GLSL_TYPE_DOUBLE
:
311 for (i
= 0; i
< total_elems
; i
++) {
312 if (i
> 0) fprintf(fp
, ", ");
313 fprintf(fp
, "%f", c
->value
.d
[i
]);
317 case GLSL_TYPE_STRUCT
:
318 for (i
= 0; i
< c
->num_elements
; i
++) {
319 if (i
> 0) fprintf(fp
, ", ");
321 print_constant(c
->elements
[i
], glsl_get_struct_field(type
, i
), state
);
326 case GLSL_TYPE_ARRAY
:
327 for (i
= 0; i
< c
->num_elements
; i
++) {
328 if (i
> 0) fprintf(fp
, ", ");
330 print_constant(c
->elements
[i
], glsl_get_array_element(type
), state
);
336 unreachable("not reached");
341 get_variable_mode_str(nir_variable_mode mode
)
344 case nir_var_shader_in
:
346 case nir_var_shader_out
:
348 case nir_var_uniform
:
350 case nir_var_shader_storage
:
351 return "shader_storage";
352 case nir_var_system_value
:
365 print_var_decl(nir_variable
*var
, print_state
*state
)
367 FILE *fp
= state
->fp
;
369 fprintf(fp
, "decl_var ");
371 const char *const cent
= (var
->data
.centroid
) ? "centroid " : "";
372 const char *const samp
= (var
->data
.sample
) ? "sample " : "";
373 const char *const patch
= (var
->data
.patch
) ? "patch " : "";
374 const char *const inv
= (var
->data
.invariant
) ? "invariant " : "";
375 fprintf(fp
, "%s%s%s%s%s %s ",
376 cent
, samp
, patch
, inv
, get_variable_mode_str(var
->data
.mode
),
377 glsl_interp_qualifier_name(var
->data
.interpolation
));
379 const char *const coher
= (var
->data
.image
.coherent
) ? "coherent " : "";
380 const char *const volat
= (var
->data
.image
._volatile
) ? "volatile " : "";
381 const char *const restr
= (var
->data
.image
.restrict_flag
) ? "restrict " : "";
382 const char *const ronly
= (var
->data
.image
.read_only
) ? "readonly " : "";
383 const char *const wonly
= (var
->data
.image
.write_only
) ? "writeonly " : "";
384 fprintf(fp
, "%s%s%s%s%s", coher
, volat
, restr
, ronly
, wonly
);
386 glsl_print_type(var
->type
, fp
);
388 fprintf(fp
, " %s", get_var_name(var
, state
));
390 if (var
->data
.mode
== nir_var_shader_in
||
391 var
->data
.mode
== nir_var_shader_out
||
392 var
->data
.mode
== nir_var_uniform
||
393 var
->data
.mode
== nir_var_shader_storage
) {
394 const char *loc
= NULL
;
397 switch (state
->shader
->stage
) {
398 case MESA_SHADER_VERTEX
:
399 if (var
->data
.mode
== nir_var_shader_in
)
400 loc
= gl_vert_attrib_name(var
->data
.location
);
401 else if (var
->data
.mode
== nir_var_shader_out
)
402 loc
= gl_varying_slot_name(var
->data
.location
);
404 case MESA_SHADER_GEOMETRY
:
405 if ((var
->data
.mode
== nir_var_shader_in
) ||
406 (var
->data
.mode
== nir_var_shader_out
))
407 loc
= gl_varying_slot_name(var
->data
.location
);
409 case MESA_SHADER_FRAGMENT
:
410 if (var
->data
.mode
== nir_var_shader_in
)
411 loc
= gl_varying_slot_name(var
->data
.location
);
412 else if (var
->data
.mode
== nir_var_shader_out
)
413 loc
= gl_frag_result_name(var
->data
.location
);
415 case MESA_SHADER_TESS_CTRL
:
416 case MESA_SHADER_TESS_EVAL
:
417 case MESA_SHADER_COMPUTE
:
424 snprintf(buf
, sizeof(buf
), "%u", var
->data
.location
);
428 fprintf(fp
, " (%s, %u)", loc
, var
->data
.driver_location
);
431 if (var
->constant_initializer
) {
432 fprintf(fp
, " = { ");
433 print_constant(var
->constant_initializer
, var
->type
, state
);
438 print_annotation(state
, var
);
442 print_var(nir_variable
*var
, print_state
*state
)
444 FILE *fp
= state
->fp
;
445 fprintf(fp
, "%s", get_var_name(var
, state
));
449 print_arg(nir_variable
*var
, print_state
*state
)
451 FILE *fp
= state
->fp
;
452 glsl_print_type(var
->type
, fp
);
453 fprintf(fp
, " %s", get_var_name(var
, state
));
457 print_deref_var(nir_deref_var
*deref
, print_state
*state
)
459 print_var(deref
->var
, state
);
463 print_deref_array(nir_deref_array
*deref
, print_state
*state
)
465 FILE *fp
= state
->fp
;
467 switch (deref
->deref_array_type
) {
468 case nir_deref_array_type_direct
:
469 fprintf(fp
, "%u", deref
->base_offset
);
471 case nir_deref_array_type_indirect
:
472 if (deref
->base_offset
!= 0)
473 fprintf(fp
, "%u + ", deref
->base_offset
);
474 print_src(&deref
->indirect
, state
);
476 case nir_deref_array_type_wildcard
:
484 print_deref_struct(nir_deref_struct
*deref
, const struct glsl_type
*parent_type
,
487 FILE *fp
= state
->fp
;
488 fprintf(fp
, ".%s", glsl_get_struct_elem_name(parent_type
, deref
->index
));
492 print_deref(nir_deref_var
*deref
, print_state
*state
)
494 nir_deref
*tail
= &deref
->deref
;
495 nir_deref
*pretail
= NULL
;
496 while (tail
!= NULL
) {
497 switch (tail
->deref_type
) {
498 case nir_deref_type_var
:
499 assert(pretail
== NULL
);
500 assert(tail
== &deref
->deref
);
501 print_deref_var(deref
, state
);
504 case nir_deref_type_array
:
505 assert(pretail
!= NULL
);
506 print_deref_array(nir_deref_as_array(tail
), state
);
509 case nir_deref_type_struct
:
510 assert(pretail
!= NULL
);
511 print_deref_struct(nir_deref_as_struct(tail
),
512 pretail
->type
, state
);
516 unreachable("Invalid deref type");
520 tail
= pretail
->child
;
525 print_intrinsic_instr(nir_intrinsic_instr
*instr
, print_state
*state
)
527 const nir_intrinsic_info
*info
= &nir_intrinsic_infos
[instr
->intrinsic
];
528 unsigned num_srcs
= info
->num_srcs
;
529 FILE *fp
= state
->fp
;
531 if (info
->has_dest
) {
532 print_dest(&instr
->dest
, state
);
536 fprintf(fp
, "intrinsic %s (", info
->name
);
538 for (unsigned i
= 0; i
< num_srcs
; i
++) {
542 print_src(&instr
->src
[i
], state
);
547 for (unsigned i
= 0; i
< info
->num_variables
; i
++) {
551 print_deref(instr
->variables
[i
], state
);
556 for (unsigned i
= 0; i
< info
->num_indices
; i
++) {
560 fprintf(fp
, "%d", instr
->const_index
[i
]);
565 static const char *index_name
[NIR_INTRINSIC_NUM_INDEX_FLAGS
] = {
566 [NIR_INTRINSIC_BASE
] = "base",
567 [NIR_INTRINSIC_WRMASK
] = "wrmask",
568 [NIR_INTRINSIC_STREAM_ID
] = "stream-id",
569 [NIR_INTRINSIC_UCP_ID
] = "ucp-id",
570 [NIR_INTRINSIC_RANGE
] = "range",
571 [NIR_INTRINSIC_DESC_SET
] = "desc-set",
572 [NIR_INTRINSIC_BINDING
] = "binding",
574 for (unsigned idx
= 1; idx
< NIR_INTRINSIC_NUM_INDEX_FLAGS
; idx
++) {
575 if (!info
->index_map
[idx
])
578 if (idx
== NIR_INTRINSIC_WRMASK
) {
579 /* special case wrmask to show it as a writemask.. */
580 unsigned wrmask
= nir_intrinsic_write_mask(instr
);
581 fprintf(fp
, " wrmask=");
582 for (unsigned i
= 0; i
< 4; i
++)
583 if ((wrmask
>> i
) & 1)
584 fprintf(fp
, "%c", "xyzw"[i
]);
586 unsigned off
= info
->index_map
[idx
] - 1;
587 assert(index_name
[idx
]); /* forgot to update index_name table? */
588 fprintf(fp
, " %s=%d", index_name
[idx
], instr
->const_index
[off
]);
596 struct exec_list
*var_list
= NULL
;
598 switch (instr
->intrinsic
) {
599 case nir_intrinsic_load_uniform
:
600 var_list
= &state
->shader
->uniforms
;
602 case nir_intrinsic_load_input
:
603 case nir_intrinsic_load_per_vertex_input
:
604 var_list
= &state
->shader
->inputs
;
606 case nir_intrinsic_load_output
:
607 case nir_intrinsic_store_output
:
608 case nir_intrinsic_store_per_vertex_output
:
609 var_list
= &state
->shader
->outputs
;
615 nir_foreach_variable(var
, var_list
) {
616 if ((var
->data
.driver_location
== nir_intrinsic_base(instr
)) &&
618 fprintf(fp
, "\t/* %s */", var
->name
);
625 print_tex_instr(nir_tex_instr
*instr
, print_state
*state
)
627 FILE *fp
= state
->fp
;
629 print_dest(&instr
->dest
, state
);
649 case nir_texop_txf_ms
:
650 fprintf(fp
, "txf_ms ");
652 case nir_texop_txf_ms_mcs
:
653 fprintf(fp
, "txf_ms_mcs ");
664 case nir_texop_query_levels
:
665 fprintf(fp
, "query_levels ");
667 case nir_texop_texture_samples
:
668 fprintf(fp
, "texture_samples ");
670 case nir_texop_samples_identical
:
671 fprintf(fp
, "samples_identical ");
674 unreachable("Invalid texture operation");
678 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++) {
679 print_src(&instr
->src
[i
].src
, state
);
683 switch(instr
->src
[i
].src_type
) {
684 case nir_tex_src_coord
:
685 fprintf(fp
, "(coord)");
687 case nir_tex_src_projector
:
688 fprintf(fp
, "(projector)");
690 case nir_tex_src_comparitor
:
691 fprintf(fp
, "(comparitor)");
693 case nir_tex_src_offset
:
694 fprintf(fp
, "(offset)");
696 case nir_tex_src_bias
:
697 fprintf(fp
, "(bias)");
699 case nir_tex_src_lod
:
700 fprintf(fp
, "(lod)");
702 case nir_tex_src_ms_index
:
703 fprintf(fp
, "(ms_index)");
705 case nir_tex_src_ms_mcs
:
706 fprintf(fp
, "(ms_mcs)");
708 case nir_tex_src_ddx
:
709 fprintf(fp
, "(ddx)");
711 case nir_tex_src_ddy
:
712 fprintf(fp
, "(ddy)");
714 case nir_tex_src_texture_offset
:
715 fprintf(fp
, "(texture_offset)");
717 case nir_tex_src_sampler_offset
:
718 fprintf(fp
, "(sampler_offset)");
720 case nir_tex_src_plane
:
721 fprintf(fp
, "(plane)");
725 unreachable("Invalid texture source type");
732 if (instr
->op
== nir_texop_tg4
) {
733 fprintf(fp
, "%u (gather_component), ", instr
->component
);
736 if (instr
->texture
) {
737 print_deref(instr
->texture
, state
);
738 fprintf(fp
, " (texture)");
739 if (instr
->sampler
) {
740 print_deref(instr
->sampler
, state
);
741 fprintf(fp
, " (sampler)");
744 assert(instr
->sampler
== NULL
);
745 fprintf(fp
, "%u (texture) %u (sampler)",
746 instr
->texture_index
, instr
->sampler_index
);
751 print_call_instr(nir_call_instr
*instr
, print_state
*state
)
753 FILE *fp
= state
->fp
;
755 fprintf(fp
, "call %s ", instr
->callee
->name
);
757 for (unsigned i
= 0; i
< instr
->num_params
; i
++) {
761 print_deref(instr
->params
[i
], state
);
764 if (instr
->return_deref
!= NULL
) {
765 if (instr
->num_params
!= 0)
767 fprintf(fp
, "returning ");
768 print_deref(instr
->return_deref
, state
);
773 print_load_const_instr(nir_load_const_instr
*instr
, print_state
*state
)
775 FILE *fp
= state
->fp
;
777 print_ssa_def(&instr
->def
, state
);
779 fprintf(fp
, " = load_const (");
781 for (unsigned i
= 0; i
< instr
->def
.num_components
; i
++) {
786 * we don't really know the type of the constant (if it will be used as a
787 * float or an int), so just print the raw constant in hex for fidelity
788 * and then print the float in a comment for readability.
791 if (instr
->def
.bit_size
== 64)
792 fprintf(fp
, "0x%16" PRIx64
" /* %f */", instr
->value
.u64
[i
],
793 instr
->value
.f64
[i
]);
795 fprintf(fp
, "0x%08x /* %f */", instr
->value
.u32
[i
], instr
->value
.f32
[i
]);
802 print_jump_instr(nir_jump_instr
*instr
, print_state
*state
)
804 FILE *fp
= state
->fp
;
806 switch (instr
->type
) {
808 fprintf(fp
, "break");
811 case nir_jump_continue
:
812 fprintf(fp
, "continue");
815 case nir_jump_return
:
816 fprintf(fp
, "return");
822 print_ssa_undef_instr(nir_ssa_undef_instr
* instr
, print_state
*state
)
824 FILE *fp
= state
->fp
;
825 print_ssa_def(&instr
->def
, state
);
826 fprintf(fp
, " = undefined");
830 print_phi_instr(nir_phi_instr
*instr
, print_state
*state
)
832 FILE *fp
= state
->fp
;
833 print_dest(&instr
->dest
, state
);
834 fprintf(fp
, " = phi ");
835 nir_foreach_phi_src(src
, instr
) {
836 if (&src
->node
!= exec_list_get_head(&instr
->srcs
))
839 fprintf(fp
, "block_%u: ", src
->pred
->index
);
840 print_src(&src
->src
, state
);
845 print_parallel_copy_instr(nir_parallel_copy_instr
*instr
, print_state
*state
)
847 FILE *fp
= state
->fp
;
848 nir_foreach_parallel_copy_entry(entry
, instr
) {
849 if (&entry
->node
!= exec_list_get_head(&instr
->entries
))
852 print_dest(&entry
->dest
, state
);
854 print_src(&entry
->src
, state
);
859 print_instr(const nir_instr
*instr
, print_state
*state
, unsigned tabs
)
861 FILE *fp
= state
->fp
;
862 print_tabs(tabs
, fp
);
864 switch (instr
->type
) {
865 case nir_instr_type_alu
:
866 print_alu_instr(nir_instr_as_alu(instr
), state
);
869 case nir_instr_type_call
:
870 print_call_instr(nir_instr_as_call(instr
), state
);
873 case nir_instr_type_intrinsic
:
874 print_intrinsic_instr(nir_instr_as_intrinsic(instr
), state
);
877 case nir_instr_type_tex
:
878 print_tex_instr(nir_instr_as_tex(instr
), state
);
881 case nir_instr_type_load_const
:
882 print_load_const_instr(nir_instr_as_load_const(instr
), state
);
885 case nir_instr_type_jump
:
886 print_jump_instr(nir_instr_as_jump(instr
), state
);
889 case nir_instr_type_ssa_undef
:
890 print_ssa_undef_instr(nir_instr_as_ssa_undef(instr
), state
);
893 case nir_instr_type_phi
:
894 print_phi_instr(nir_instr_as_phi(instr
), state
);
897 case nir_instr_type_parallel_copy
:
898 print_parallel_copy_instr(nir_instr_as_parallel_copy(instr
), state
);
902 unreachable("Invalid instruction type");
908 compare_block_index(const void *p1
, const void *p2
)
910 const nir_block
*block1
= *((const nir_block
**) p1
);
911 const nir_block
*block2
= *((const nir_block
**) p2
);
913 return (int) block1
->index
- (int) block2
->index
;
916 static void print_cf_node(nir_cf_node
*node
, print_state
*state
,
920 print_block(nir_block
*block
, print_state
*state
, unsigned tabs
)
922 FILE *fp
= state
->fp
;
924 print_tabs(tabs
, fp
);
925 fprintf(fp
, "block block_%u:\n", block
->index
);
927 /* sort the predecessors by index so we consistently print the same thing */
930 malloc(block
->predecessors
->entries
* sizeof(nir_block
*));
932 struct set_entry
*entry
;
934 set_foreach(block
->predecessors
, entry
) {
935 preds
[i
++] = (nir_block
*) entry
->key
;
938 qsort(preds
, block
->predecessors
->entries
, sizeof(nir_block
*),
939 compare_block_index
);
941 print_tabs(tabs
, fp
);
942 fprintf(fp
, "/* preds: ");
943 for (unsigned i
= 0; i
< block
->predecessors
->entries
; i
++) {
944 fprintf(fp
, "block_%u ", preds
[i
]->index
);
950 nir_foreach_instr(instr
, block
) {
951 print_instr(instr
, state
, tabs
);
953 print_annotation(state
, instr
);
956 print_tabs(tabs
, fp
);
957 fprintf(fp
, "/* succs: ");
958 for (unsigned i
= 0; i
< 2; i
++)
959 if (block
->successors
[i
]) {
960 fprintf(fp
, "block_%u ", block
->successors
[i
]->index
);
966 print_if(nir_if
*if_stmt
, print_state
*state
, unsigned tabs
)
968 FILE *fp
= state
->fp
;
970 print_tabs(tabs
, fp
);
972 print_src(&if_stmt
->condition
, state
);
974 foreach_list_typed(nir_cf_node
, node
, node
, &if_stmt
->then_list
) {
975 print_cf_node(node
, state
, tabs
+ 1);
977 print_tabs(tabs
, fp
);
978 fprintf(fp
, "} else {\n");
979 foreach_list_typed(nir_cf_node
, node
, node
, &if_stmt
->else_list
) {
980 print_cf_node(node
, state
, tabs
+ 1);
982 print_tabs(tabs
, fp
);
987 print_loop(nir_loop
*loop
, print_state
*state
, unsigned tabs
)
989 FILE *fp
= state
->fp
;
991 print_tabs(tabs
, fp
);
992 fprintf(fp
, "loop {\n");
993 foreach_list_typed(nir_cf_node
, node
, node
, &loop
->body
) {
994 print_cf_node(node
, state
, tabs
+ 1);
996 print_tabs(tabs
, fp
);
1001 print_cf_node(nir_cf_node
*node
, print_state
*state
, unsigned int tabs
)
1003 switch (node
->type
) {
1004 case nir_cf_node_block
:
1005 print_block(nir_cf_node_as_block(node
), state
, tabs
);
1008 case nir_cf_node_if
:
1009 print_if(nir_cf_node_as_if(node
), state
, tabs
);
1012 case nir_cf_node_loop
:
1013 print_loop(nir_cf_node_as_loop(node
), state
, tabs
);
1017 unreachable("Invalid CFG node type");
1022 print_function_impl(nir_function_impl
*impl
, print_state
*state
)
1024 FILE *fp
= state
->fp
;
1026 fprintf(fp
, "\nimpl %s ", impl
->function
->name
);
1028 for (unsigned i
= 0; i
< impl
->num_params
; i
++) {
1032 print_arg(impl
->params
[i
], state
);
1035 if (impl
->return_var
!= NULL
) {
1036 if (impl
->num_params
!= 0)
1038 fprintf(fp
, "returning ");
1039 print_arg(impl
->return_var
, state
);
1044 nir_foreach_variable(var
, &impl
->locals
) {
1046 print_var_decl(var
, state
);
1049 foreach_list_typed(nir_register
, reg
, node
, &impl
->registers
) {
1051 print_register_decl(reg
, state
);
1054 nir_index_blocks(impl
);
1056 foreach_list_typed(nir_cf_node
, node
, node
, &impl
->body
) {
1057 print_cf_node(node
, state
, 1);
1060 fprintf(fp
, "\tblock block_%u:\n}\n\n", impl
->end_block
->index
);
1064 print_function(nir_function
*function
, print_state
*state
)
1066 FILE *fp
= state
->fp
;
1068 fprintf(fp
, "decl_function %s ", function
->name
);
1070 for (unsigned i
= 0; i
< function
->num_params
; i
++) {
1074 switch (function
->params
[i
].param_type
) {
1075 case nir_parameter_in
:
1078 case nir_parameter_out
:
1079 fprintf(fp
, "out ");
1081 case nir_parameter_inout
:
1082 fprintf(fp
, "inout ");
1085 unreachable("Invalid parameter type");
1088 glsl_print_type(function
->params
[i
].type
, fp
);
1091 if (function
->return_type
!= NULL
) {
1092 if (function
->num_params
!= 0)
1094 fprintf(fp
, "returning ");
1095 glsl_print_type(function
->return_type
, fp
);
1100 if (function
->impl
!= NULL
) {
1101 print_function_impl(function
->impl
, state
);
1107 init_print_state(print_state
*state
, nir_shader
*shader
, FILE *fp
)
1110 state
->shader
= shader
;
1111 state
->ht
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
1112 _mesa_key_pointer_equal
);
1113 state
->syms
= _mesa_set_create(NULL
, _mesa_key_hash_string
,
1114 _mesa_key_string_equal
);
1119 destroy_print_state(print_state
*state
)
1121 _mesa_hash_table_destroy(state
->ht
, NULL
);
1122 _mesa_set_destroy(state
->syms
, NULL
);
1126 nir_print_shader_annotated(nir_shader
*shader
, FILE *fp
,
1127 struct hash_table
*annotations
)
1130 init_print_state(&state
, shader
, fp
);
1132 state
.annotations
= annotations
;
1134 fprintf(fp
, "shader: %s\n", gl_shader_stage_name(shader
->stage
));
1136 if (shader
->info
.name
)
1137 fprintf(fp
, "name: %s\n", shader
->info
.name
);
1139 if (shader
->info
.label
)
1140 fprintf(fp
, "label: %s\n", shader
->info
.label
);
1142 fprintf(fp
, "inputs: %u\n", shader
->num_inputs
);
1143 fprintf(fp
, "outputs: %u\n", shader
->num_outputs
);
1144 fprintf(fp
, "uniforms: %u\n", shader
->num_uniforms
);
1145 fprintf(fp
, "shared: %u\n", shader
->num_shared
);
1147 nir_foreach_variable(var
, &shader
->uniforms
) {
1148 print_var_decl(var
, &state
);
1151 nir_foreach_variable(var
, &shader
->inputs
) {
1152 print_var_decl(var
, &state
);
1155 nir_foreach_variable(var
, &shader
->outputs
) {
1156 print_var_decl(var
, &state
);
1159 nir_foreach_variable(var
, &shader
->shared
) {
1160 print_var_decl(var
, &state
);
1163 nir_foreach_variable(var
, &shader
->globals
) {
1164 print_var_decl(var
, &state
);
1167 nir_foreach_variable(var
, &shader
->system_values
) {
1168 print_var_decl(var
, &state
);
1171 foreach_list_typed(nir_register
, reg
, node
, &shader
->registers
) {
1172 print_register_decl(reg
, &state
);
1175 foreach_list_typed(nir_function
, func
, node
, &shader
->functions
) {
1176 print_function(func
, &state
);
1179 destroy_print_state(&state
);
1183 nir_print_shader(nir_shader
*shader
, FILE *fp
)
1185 nir_print_shader_annotated(shader
, fp
, NULL
);
1189 nir_print_instr(const nir_instr
*instr
, FILE *fp
)
1191 print_state state
= {
1194 print_instr(instr
, &state
, 0);