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
->info
.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 /* For shader I/O vars that have been split to components or packed,
451 * print the fractional location within the input/output.
453 unsigned int num_components
=
454 glsl_get_components(glsl_without_array(var
->type
));
455 const char *components
= NULL
;
456 char components_local
[6] = {'.' /* the rest is 0-filled */};
457 switch (var
->data
.mode
) {
458 case nir_var_shader_in
:
459 case nir_var_shader_out
:
460 if (num_components
< 4 && num_components
!= 0) {
461 const char *xyzw
= "xyzw";
462 for (int i
= 0; i
< num_components
; i
++)
463 components_local
[i
+ 1] = xyzw
[i
+ var
->data
.location_frac
];
465 components
= components_local
;
472 fprintf(fp
, " (%s%s, %u, %u)%s", loc
,
473 components
? components
: "",
474 var
->data
.driver_location
, var
->data
.binding
,
475 var
->data
.compact
? " compact" : "");
478 if (var
->constant_initializer
) {
479 fprintf(fp
, " = { ");
480 print_constant(var
->constant_initializer
, var
->type
, state
);
485 print_annotation(state
, var
);
489 print_var(nir_variable
*var
, print_state
*state
)
491 FILE *fp
= state
->fp
;
492 fprintf(fp
, "%s", get_var_name(var
, state
));
496 print_arg(nir_variable
*var
, print_state
*state
)
498 FILE *fp
= state
->fp
;
499 fprintf(fp
, "%s %s", glsl_get_type_name(var
->type
),
500 get_var_name(var
, state
));
504 print_deref_var(nir_deref_var
*deref
, print_state
*state
)
506 print_var(deref
->var
, state
);
510 print_deref_array(nir_deref_array
*deref
, print_state
*state
)
512 FILE *fp
= state
->fp
;
514 switch (deref
->deref_array_type
) {
515 case nir_deref_array_type_direct
:
516 fprintf(fp
, "%u", deref
->base_offset
);
518 case nir_deref_array_type_indirect
:
519 if (deref
->base_offset
!= 0)
520 fprintf(fp
, "%u + ", deref
->base_offset
);
521 print_src(&deref
->indirect
, state
);
523 case nir_deref_array_type_wildcard
:
531 print_deref_struct(nir_deref_struct
*deref
, const struct glsl_type
*parent_type
,
534 FILE *fp
= state
->fp
;
535 fprintf(fp
, ".%s", glsl_get_struct_elem_name(parent_type
, deref
->index
));
539 print_deref(nir_deref_var
*deref
, print_state
*state
)
541 nir_deref
*tail
= &deref
->deref
;
542 nir_deref
*pretail
= NULL
;
543 while (tail
!= NULL
) {
544 switch (tail
->deref_type
) {
545 case nir_deref_type_var
:
546 assert(pretail
== NULL
);
547 assert(tail
== &deref
->deref
);
548 print_deref_var(deref
, state
);
551 case nir_deref_type_array
:
552 assert(pretail
!= NULL
);
553 print_deref_array(nir_deref_as_array(tail
), state
);
556 case nir_deref_type_struct
:
557 assert(pretail
!= NULL
);
558 print_deref_struct(nir_deref_as_struct(tail
),
559 pretail
->type
, state
);
563 unreachable("Invalid deref type");
567 tail
= pretail
->child
;
572 print_intrinsic_instr(nir_intrinsic_instr
*instr
, print_state
*state
)
574 const nir_intrinsic_info
*info
= &nir_intrinsic_infos
[instr
->intrinsic
];
575 unsigned num_srcs
= info
->num_srcs
;
576 FILE *fp
= state
->fp
;
578 if (info
->has_dest
) {
579 print_dest(&instr
->dest
, state
);
583 fprintf(fp
, "intrinsic %s (", info
->name
);
585 for (unsigned i
= 0; i
< num_srcs
; i
++) {
589 print_src(&instr
->src
[i
], state
);
594 for (unsigned i
= 0; i
< info
->num_variables
; i
++) {
598 print_deref(instr
->variables
[i
], state
);
603 for (unsigned i
= 0; i
< info
->num_indices
; i
++) {
607 fprintf(fp
, "%d", instr
->const_index
[i
]);
612 static const char *index_name
[NIR_INTRINSIC_NUM_INDEX_FLAGS
] = {
613 [NIR_INTRINSIC_BASE
] = "base",
614 [NIR_INTRINSIC_WRMASK
] = "wrmask",
615 [NIR_INTRINSIC_STREAM_ID
] = "stream-id",
616 [NIR_INTRINSIC_UCP_ID
] = "ucp-id",
617 [NIR_INTRINSIC_RANGE
] = "range",
618 [NIR_INTRINSIC_DESC_SET
] = "desc-set",
619 [NIR_INTRINSIC_BINDING
] = "binding",
620 [NIR_INTRINSIC_COMPONENT
] = "component",
621 [NIR_INTRINSIC_INTERP_MODE
] = "interp_mode",
623 for (unsigned idx
= 1; idx
< NIR_INTRINSIC_NUM_INDEX_FLAGS
; idx
++) {
624 if (!info
->index_map
[idx
])
627 if (idx
== NIR_INTRINSIC_WRMASK
) {
628 /* special case wrmask to show it as a writemask.. */
629 unsigned wrmask
= nir_intrinsic_write_mask(instr
);
630 fprintf(fp
, " wrmask=");
631 for (unsigned i
= 0; i
< 4; i
++)
632 if ((wrmask
>> i
) & 1)
633 fprintf(fp
, "%c", "xyzw"[i
]);
635 unsigned off
= info
->index_map
[idx
] - 1;
636 assert(index_name
[idx
]); /* forgot to update index_name table? */
637 fprintf(fp
, " %s=%d", index_name
[idx
], instr
->const_index
[off
]);
645 struct exec_list
*var_list
= NULL
;
647 switch (instr
->intrinsic
) {
648 case nir_intrinsic_load_uniform
:
649 var_list
= &state
->shader
->uniforms
;
651 case nir_intrinsic_load_input
:
652 case nir_intrinsic_load_per_vertex_input
:
653 var_list
= &state
->shader
->inputs
;
655 case nir_intrinsic_load_output
:
656 case nir_intrinsic_store_output
:
657 case nir_intrinsic_store_per_vertex_output
:
658 var_list
= &state
->shader
->outputs
;
664 nir_foreach_variable(var
, var_list
) {
665 if ((var
->data
.driver_location
== nir_intrinsic_base(instr
)) &&
666 (instr
->intrinsic
== nir_intrinsic_load_uniform
||
667 var
->data
.location_frac
== nir_intrinsic_component(instr
)) &&
669 fprintf(fp
, "\t/* %s */", var
->name
);
676 print_tex_instr(nir_tex_instr
*instr
, print_state
*state
)
678 FILE *fp
= state
->fp
;
680 print_dest(&instr
->dest
, state
);
700 case nir_texop_txf_ms
:
701 fprintf(fp
, "txf_ms ");
703 case nir_texop_txf_ms_mcs
:
704 fprintf(fp
, "txf_ms_mcs ");
715 case nir_texop_query_levels
:
716 fprintf(fp
, "query_levels ");
718 case nir_texop_texture_samples
:
719 fprintf(fp
, "texture_samples ");
721 case nir_texop_samples_identical
:
722 fprintf(fp
, "samples_identical ");
725 unreachable("Invalid texture operation");
729 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++) {
730 print_src(&instr
->src
[i
].src
, state
);
734 switch(instr
->src
[i
].src_type
) {
735 case nir_tex_src_coord
:
736 fprintf(fp
, "(coord)");
738 case nir_tex_src_projector
:
739 fprintf(fp
, "(projector)");
741 case nir_tex_src_comparator
:
742 fprintf(fp
, "(comparator)");
744 case nir_tex_src_offset
:
745 fprintf(fp
, "(offset)");
747 case nir_tex_src_bias
:
748 fprintf(fp
, "(bias)");
750 case nir_tex_src_lod
:
751 fprintf(fp
, "(lod)");
753 case nir_tex_src_ms_index
:
754 fprintf(fp
, "(ms_index)");
756 case nir_tex_src_ms_mcs
:
757 fprintf(fp
, "(ms_mcs)");
759 case nir_tex_src_ddx
:
760 fprintf(fp
, "(ddx)");
762 case nir_tex_src_ddy
:
763 fprintf(fp
, "(ddy)");
765 case nir_tex_src_texture_offset
:
766 fprintf(fp
, "(texture_offset)");
768 case nir_tex_src_sampler_offset
:
769 fprintf(fp
, "(sampler_offset)");
771 case nir_tex_src_plane
:
772 fprintf(fp
, "(plane)");
776 unreachable("Invalid texture source type");
783 if (instr
->op
== nir_texop_tg4
) {
784 fprintf(fp
, "%u (gather_component), ", instr
->component
);
787 if (instr
->texture
) {
788 print_deref(instr
->texture
, state
);
789 fprintf(fp
, " (texture)");
790 if (instr
->sampler
) {
791 print_deref(instr
->sampler
, state
);
792 fprintf(fp
, " (sampler)");
795 assert(instr
->sampler
== NULL
);
796 fprintf(fp
, "%u (texture) %u (sampler)",
797 instr
->texture_index
, instr
->sampler_index
);
802 print_call_instr(nir_call_instr
*instr
, print_state
*state
)
804 FILE *fp
= state
->fp
;
806 fprintf(fp
, "call %s ", instr
->callee
->name
);
808 for (unsigned i
= 0; i
< instr
->num_params
; i
++) {
812 print_deref(instr
->params
[i
], state
);
815 if (instr
->return_deref
!= NULL
) {
816 if (instr
->num_params
!= 0)
818 fprintf(fp
, "returning ");
819 print_deref(instr
->return_deref
, state
);
824 print_load_const_instr(nir_load_const_instr
*instr
, print_state
*state
)
826 FILE *fp
= state
->fp
;
828 print_ssa_def(&instr
->def
, state
);
830 fprintf(fp
, " = load_const (");
832 for (unsigned i
= 0; i
< instr
->def
.num_components
; i
++) {
837 * we don't really know the type of the constant (if it will be used as a
838 * float or an int), so just print the raw constant in hex for fidelity
839 * and then print the float in a comment for readability.
842 if (instr
->def
.bit_size
== 64)
843 fprintf(fp
, "0x%16" PRIx64
" /* %f */", instr
->value
.u64
[i
],
844 instr
->value
.f64
[i
]);
846 fprintf(fp
, "0x%08x /* %f */", instr
->value
.u32
[i
], instr
->value
.f32
[i
]);
853 print_jump_instr(nir_jump_instr
*instr
, print_state
*state
)
855 FILE *fp
= state
->fp
;
857 switch (instr
->type
) {
859 fprintf(fp
, "break");
862 case nir_jump_continue
:
863 fprintf(fp
, "continue");
866 case nir_jump_return
:
867 fprintf(fp
, "return");
873 print_ssa_undef_instr(nir_ssa_undef_instr
* instr
, print_state
*state
)
875 FILE *fp
= state
->fp
;
876 print_ssa_def(&instr
->def
, state
);
877 fprintf(fp
, " = undefined");
881 print_phi_instr(nir_phi_instr
*instr
, print_state
*state
)
883 FILE *fp
= state
->fp
;
884 print_dest(&instr
->dest
, state
);
885 fprintf(fp
, " = phi ");
886 nir_foreach_phi_src(src
, instr
) {
887 if (&src
->node
!= exec_list_get_head(&instr
->srcs
))
890 fprintf(fp
, "block_%u: ", src
->pred
->index
);
891 print_src(&src
->src
, state
);
896 print_parallel_copy_instr(nir_parallel_copy_instr
*instr
, print_state
*state
)
898 FILE *fp
= state
->fp
;
899 nir_foreach_parallel_copy_entry(entry
, instr
) {
900 if (&entry
->node
!= exec_list_get_head(&instr
->entries
))
903 print_dest(&entry
->dest
, state
);
905 print_src(&entry
->src
, state
);
910 print_instr(const nir_instr
*instr
, print_state
*state
, unsigned tabs
)
912 FILE *fp
= state
->fp
;
913 print_tabs(tabs
, fp
);
915 switch (instr
->type
) {
916 case nir_instr_type_alu
:
917 print_alu_instr(nir_instr_as_alu(instr
), state
);
920 case nir_instr_type_call
:
921 print_call_instr(nir_instr_as_call(instr
), state
);
924 case nir_instr_type_intrinsic
:
925 print_intrinsic_instr(nir_instr_as_intrinsic(instr
), state
);
928 case nir_instr_type_tex
:
929 print_tex_instr(nir_instr_as_tex(instr
), state
);
932 case nir_instr_type_load_const
:
933 print_load_const_instr(nir_instr_as_load_const(instr
), state
);
936 case nir_instr_type_jump
:
937 print_jump_instr(nir_instr_as_jump(instr
), state
);
940 case nir_instr_type_ssa_undef
:
941 print_ssa_undef_instr(nir_instr_as_ssa_undef(instr
), state
);
944 case nir_instr_type_phi
:
945 print_phi_instr(nir_instr_as_phi(instr
), state
);
948 case nir_instr_type_parallel_copy
:
949 print_parallel_copy_instr(nir_instr_as_parallel_copy(instr
), state
);
953 unreachable("Invalid instruction type");
959 compare_block_index(const void *p1
, const void *p2
)
961 const nir_block
*block1
= *((const nir_block
**) p1
);
962 const nir_block
*block2
= *((const nir_block
**) p2
);
964 return (int) block1
->index
- (int) block2
->index
;
967 static void print_cf_node(nir_cf_node
*node
, print_state
*state
,
971 print_block(nir_block
*block
, print_state
*state
, unsigned tabs
)
973 FILE *fp
= state
->fp
;
975 print_tabs(tabs
, fp
);
976 fprintf(fp
, "block block_%u:\n", block
->index
);
978 /* sort the predecessors by index so we consistently print the same thing */
981 malloc(block
->predecessors
->entries
* sizeof(nir_block
*));
983 struct set_entry
*entry
;
985 set_foreach(block
->predecessors
, entry
) {
986 preds
[i
++] = (nir_block
*) entry
->key
;
989 qsort(preds
, block
->predecessors
->entries
, sizeof(nir_block
*),
990 compare_block_index
);
992 print_tabs(tabs
, fp
);
993 fprintf(fp
, "/* preds: ");
994 for (unsigned i
= 0; i
< block
->predecessors
->entries
; i
++) {
995 fprintf(fp
, "block_%u ", preds
[i
]->index
);
1001 nir_foreach_instr(instr
, block
) {
1002 print_instr(instr
, state
, tabs
);
1004 print_annotation(state
, instr
);
1007 print_tabs(tabs
, fp
);
1008 fprintf(fp
, "/* succs: ");
1009 for (unsigned i
= 0; i
< 2; i
++)
1010 if (block
->successors
[i
]) {
1011 fprintf(fp
, "block_%u ", block
->successors
[i
]->index
);
1013 fprintf(fp
, "*/\n");
1017 print_if(nir_if
*if_stmt
, print_state
*state
, unsigned tabs
)
1019 FILE *fp
= state
->fp
;
1021 print_tabs(tabs
, fp
);
1023 print_src(&if_stmt
->condition
, state
);
1024 fprintf(fp
, " {\n");
1025 foreach_list_typed(nir_cf_node
, node
, node
, &if_stmt
->then_list
) {
1026 print_cf_node(node
, state
, tabs
+ 1);
1028 print_tabs(tabs
, fp
);
1029 fprintf(fp
, "} else {\n");
1030 foreach_list_typed(nir_cf_node
, node
, node
, &if_stmt
->else_list
) {
1031 print_cf_node(node
, state
, tabs
+ 1);
1033 print_tabs(tabs
, fp
);
1038 print_loop(nir_loop
*loop
, print_state
*state
, unsigned tabs
)
1040 FILE *fp
= state
->fp
;
1042 print_tabs(tabs
, fp
);
1043 fprintf(fp
, "loop {\n");
1044 foreach_list_typed(nir_cf_node
, node
, node
, &loop
->body
) {
1045 print_cf_node(node
, state
, tabs
+ 1);
1047 print_tabs(tabs
, fp
);
1052 print_cf_node(nir_cf_node
*node
, print_state
*state
, unsigned int tabs
)
1054 switch (node
->type
) {
1055 case nir_cf_node_block
:
1056 print_block(nir_cf_node_as_block(node
), state
, tabs
);
1059 case nir_cf_node_if
:
1060 print_if(nir_cf_node_as_if(node
), state
, tabs
);
1063 case nir_cf_node_loop
:
1064 print_loop(nir_cf_node_as_loop(node
), state
, tabs
);
1068 unreachable("Invalid CFG node type");
1073 print_function_impl(nir_function_impl
*impl
, print_state
*state
)
1075 FILE *fp
= state
->fp
;
1077 fprintf(fp
, "\nimpl %s ", impl
->function
->name
);
1079 for (unsigned i
= 0; i
< impl
->num_params
; i
++) {
1083 print_arg(impl
->params
[i
], state
);
1086 if (impl
->return_var
!= NULL
) {
1087 if (impl
->num_params
!= 0)
1089 fprintf(fp
, "returning ");
1090 print_arg(impl
->return_var
, state
);
1095 nir_foreach_variable(var
, &impl
->locals
) {
1097 print_var_decl(var
, state
);
1100 foreach_list_typed(nir_register
, reg
, node
, &impl
->registers
) {
1102 print_register_decl(reg
, state
);
1105 nir_index_blocks(impl
);
1107 foreach_list_typed(nir_cf_node
, node
, node
, &impl
->body
) {
1108 print_cf_node(node
, state
, 1);
1111 fprintf(fp
, "\tblock block_%u:\n}\n\n", impl
->end_block
->index
);
1115 print_function(nir_function
*function
, print_state
*state
)
1117 FILE *fp
= state
->fp
;
1119 fprintf(fp
, "decl_function %s ", function
->name
);
1121 for (unsigned i
= 0; i
< function
->num_params
; i
++) {
1125 switch (function
->params
[i
].param_type
) {
1126 case nir_parameter_in
:
1129 case nir_parameter_out
:
1130 fprintf(fp
, "out ");
1132 case nir_parameter_inout
:
1133 fprintf(fp
, "inout ");
1136 unreachable("Invalid parameter type");
1139 fprintf(fp
, "%s", glsl_get_type_name(function
->params
[i
].type
));
1142 if (function
->return_type
!= NULL
) {
1143 if (function
->num_params
!= 0)
1145 fprintf(fp
, "returning %s", glsl_get_type_name(function
->return_type
));
1150 if (function
->impl
!= NULL
) {
1151 print_function_impl(function
->impl
, state
);
1157 init_print_state(print_state
*state
, nir_shader
*shader
, FILE *fp
)
1160 state
->shader
= shader
;
1161 state
->ht
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
1162 _mesa_key_pointer_equal
);
1163 state
->syms
= _mesa_set_create(NULL
, _mesa_key_hash_string
,
1164 _mesa_key_string_equal
);
1169 destroy_print_state(print_state
*state
)
1171 _mesa_hash_table_destroy(state
->ht
, NULL
);
1172 _mesa_set_destroy(state
->syms
, NULL
);
1176 nir_print_shader_annotated(nir_shader
*shader
, FILE *fp
,
1177 struct hash_table
*annotations
)
1180 init_print_state(&state
, shader
, fp
);
1182 state
.annotations
= annotations
;
1184 fprintf(fp
, "shader: %s\n", gl_shader_stage_name(shader
->info
.stage
));
1186 if (shader
->info
.name
)
1187 fprintf(fp
, "name: %s\n", shader
->info
.name
);
1189 if (shader
->info
.label
)
1190 fprintf(fp
, "label: %s\n", shader
->info
.label
);
1192 switch (shader
->info
.stage
) {
1193 case MESA_SHADER_COMPUTE
:
1194 fprintf(fp
, "local-size: %u, %u, %u%s\n",
1195 shader
->info
.cs
.local_size
[0],
1196 shader
->info
.cs
.local_size
[1],
1197 shader
->info
.cs
.local_size
[2],
1198 shader
->info
.cs
.local_size_variable
? " (variable)" : "");
1199 fprintf(fp
, "shared-size: %u\n", shader
->info
.cs
.shared_size
);
1205 fprintf(fp
, "inputs: %u\n", shader
->num_inputs
);
1206 fprintf(fp
, "outputs: %u\n", shader
->num_outputs
);
1207 fprintf(fp
, "uniforms: %u\n", shader
->num_uniforms
);
1208 fprintf(fp
, "shared: %u\n", shader
->num_shared
);
1210 nir_foreach_variable(var
, &shader
->uniforms
) {
1211 print_var_decl(var
, &state
);
1214 nir_foreach_variable(var
, &shader
->inputs
) {
1215 print_var_decl(var
, &state
);
1218 nir_foreach_variable(var
, &shader
->outputs
) {
1219 print_var_decl(var
, &state
);
1222 nir_foreach_variable(var
, &shader
->shared
) {
1223 print_var_decl(var
, &state
);
1226 nir_foreach_variable(var
, &shader
->globals
) {
1227 print_var_decl(var
, &state
);
1230 nir_foreach_variable(var
, &shader
->system_values
) {
1231 print_var_decl(var
, &state
);
1234 foreach_list_typed(nir_register
, reg
, node
, &shader
->registers
) {
1235 print_register_decl(reg
, &state
);
1238 foreach_list_typed(nir_function
, func
, node
, &shader
->functions
) {
1239 print_function(func
, &state
);
1242 destroy_print_state(&state
);
1246 nir_print_shader(nir_shader
*shader
, FILE *fp
)
1248 nir_print_shader_annotated(shader
, fp
, NULL
);
1252 nir_print_instr(const nir_instr
*instr
, FILE *fp
)
1254 print_state state
= {
1257 print_instr(instr
, &state
, 0);