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",
89 "error", "error", "error", "vec8",
90 "error", "error", "error", "vec16"};
93 print_register_decl(nir_register
*reg
, print_state
*state
)
96 fprintf(fp
, "decl_reg %s %u ", sizes
[reg
->num_components
], reg
->bit_size
);
98 fprintf(fp
, "(packed) ");
99 print_register(reg
, state
);
100 if (reg
->num_array_elems
!= 0)
101 fprintf(fp
, "[%u]", reg
->num_array_elems
);
106 print_ssa_def(nir_ssa_def
*def
, print_state
*state
)
108 FILE *fp
= state
->fp
;
109 if (def
->name
!= NULL
)
110 fprintf(fp
, "/* %s */ ", def
->name
);
111 fprintf(fp
, "%s %u ssa_%u", sizes
[def
->num_components
], def
->bit_size
,
116 print_ssa_use(nir_ssa_def
*def
, print_state
*state
)
118 FILE *fp
= state
->fp
;
119 if (def
->name
!= NULL
)
120 fprintf(fp
, "/* %s */ ", def
->name
);
121 fprintf(fp
, "ssa_%u", def
->index
);
124 static void print_src(nir_src
*src
, print_state
*state
);
127 print_reg_src(nir_reg_src
*src
, print_state
*state
)
129 FILE *fp
= state
->fp
;
130 print_register(src
->reg
, state
);
131 if (src
->reg
->num_array_elems
!= 0) {
132 fprintf(fp
, "[%u", src
->base_offset
);
133 if (src
->indirect
!= NULL
) {
135 print_src(src
->indirect
, state
);
142 print_reg_dest(nir_reg_dest
*dest
, print_state
*state
)
144 FILE *fp
= state
->fp
;
145 print_register(dest
->reg
, state
);
146 if (dest
->reg
->num_array_elems
!= 0) {
147 fprintf(fp
, "[%u", dest
->base_offset
);
148 if (dest
->indirect
!= NULL
) {
150 print_src(dest
->indirect
, state
);
157 print_src(nir_src
*src
, print_state
*state
)
160 print_ssa_use(src
->ssa
, state
);
162 print_reg_src(&src
->reg
, state
);
166 print_dest(nir_dest
*dest
, print_state
*state
)
169 print_ssa_def(&dest
->ssa
, state
);
171 print_reg_dest(&dest
->reg
, state
);
175 print_alu_src(nir_alu_instr
*instr
, unsigned src
, print_state
*state
)
177 FILE *fp
= state
->fp
;
179 if (instr
->src
[src
].negate
)
181 if (instr
->src
[src
].abs
)
184 print_src(&instr
->src
[src
].src
, state
);
186 bool print_swizzle
= false;
187 unsigned used_channels
= 0;
189 for (unsigned i
= 0; i
< 4; i
++) {
190 if (!nir_alu_instr_channel_used(instr
, src
, i
))
195 if (instr
->src
[src
].swizzle
[i
] != i
) {
196 print_swizzle
= true;
201 unsigned live_channels
= instr
->src
[src
].src
.is_ssa
202 ? instr
->src
[src
].src
.ssa
->num_components
203 : instr
->src
[src
].src
.reg
.reg
->num_components
;
205 if (print_swizzle
|| used_channels
!= live_channels
) {
207 for (unsigned i
= 0; i
< 4; i
++) {
208 if (!nir_alu_instr_channel_used(instr
, src
, i
))
211 fprintf(fp
, "%c", "xyzw"[instr
->src
[src
].swizzle
[i
]]);
215 if (instr
->src
[src
].abs
)
220 print_alu_dest(nir_alu_dest
*dest
, print_state
*state
)
222 FILE *fp
= state
->fp
;
223 /* we're going to print the saturate modifier later, after the opcode */
225 print_dest(&dest
->dest
, state
);
227 if (!dest
->dest
.is_ssa
&&
228 dest
->write_mask
!= (1 << dest
->dest
.reg
.reg
->num_components
) - 1) {
230 for (unsigned i
= 0; i
< 4; i
++)
231 if ((dest
->write_mask
>> i
) & 1)
232 fprintf(fp
, "%c", "xyzw"[i
]);
237 print_alu_instr(nir_alu_instr
*instr
, print_state
*state
)
239 FILE *fp
= state
->fp
;
241 print_alu_dest(&instr
->dest
, state
);
243 fprintf(fp
, " = %s", nir_op_infos
[instr
->op
].name
);
246 if (instr
->dest
.saturate
)
250 for (unsigned i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++) {
254 print_alu_src(instr
, i
, state
);
259 get_var_name(nir_variable
*var
, print_state
*state
)
261 if (state
->ht
== NULL
)
262 return var
->name
? var
->name
: "unnamed";
266 struct hash_entry
*entry
= _mesa_hash_table_search(state
->ht
, var
);
271 if (var
->name
== NULL
) {
272 name
= ralloc_asprintf(state
->syms
, "@%u", state
->index
++);
274 struct set_entry
*set_entry
= _mesa_set_search(state
->syms
, var
->name
);
275 if (set_entry
!= NULL
) {
276 /* we have a collision with another name, append an @ + a unique
278 name
= ralloc_asprintf(state
->syms
, "%s@%u", var
->name
,
281 /* Mark this one as seen */
282 _mesa_set_add(state
->syms
, var
->name
);
287 _mesa_hash_table_insert(state
->ht
, var
, name
);
293 print_constant(nir_constant
*c
, const struct glsl_type
*type
, print_state
*state
)
295 FILE *fp
= state
->fp
;
296 const unsigned rows
= glsl_get_vector_elements(type
);
297 const unsigned cols
= glsl_get_matrix_columns(type
);
300 switch (glsl_get_base_type(type
)) {
304 /* Only float base types can be matrices. */
307 for (i
= 0; i
< rows
; i
++) {
308 if (i
> 0) fprintf(fp
, ", ");
309 fprintf(fp
, "0x%08x", c
->values
[0].u32
[i
]);
313 case GLSL_TYPE_FLOAT
:
314 for (i
= 0; i
< cols
; i
++) {
315 for (j
= 0; j
< rows
; j
++) {
316 if (i
+ j
> 0) fprintf(fp
, ", ");
317 fprintf(fp
, "%f", c
->values
[i
].f32
[j
]);
322 case GLSL_TYPE_DOUBLE
:
323 for (i
= 0; i
< cols
; i
++) {
324 for (j
= 0; j
< rows
; j
++) {
325 if (i
+ j
> 0) fprintf(fp
, ", ");
326 fprintf(fp
, "%f", c
->values
[i
].f64
[j
]);
331 case GLSL_TYPE_UINT64
:
332 case GLSL_TYPE_INT64
:
333 /* Only float base types can be matrices. */
336 for (i
= 0; i
< cols
; i
++) {
337 if (i
> 0) fprintf(fp
, ", ");
338 fprintf(fp
, "0x%08" PRIx64
, c
->values
[0].u64
[i
]);
342 case GLSL_TYPE_STRUCT
:
343 for (i
= 0; i
< c
->num_elements
; i
++) {
344 if (i
> 0) fprintf(fp
, ", ");
346 print_constant(c
->elements
[i
], glsl_get_struct_field(type
, i
), state
);
351 case GLSL_TYPE_ARRAY
:
352 for (i
= 0; i
< c
->num_elements
; i
++) {
353 if (i
> 0) fprintf(fp
, ", ");
355 print_constant(c
->elements
[i
], glsl_get_array_element(type
), state
);
361 unreachable("not reached");
366 get_variable_mode_str(nir_variable_mode mode
)
369 case nir_var_shader_in
:
371 case nir_var_shader_out
:
373 case nir_var_uniform
:
375 case nir_var_shader_storage
:
376 return "shader_storage";
377 case nir_var_system_value
:
390 print_var_decl(nir_variable
*var
, print_state
*state
)
392 FILE *fp
= state
->fp
;
394 fprintf(fp
, "decl_var ");
396 const char *const cent
= (var
->data
.centroid
) ? "centroid " : "";
397 const char *const samp
= (var
->data
.sample
) ? "sample " : "";
398 const char *const patch
= (var
->data
.patch
) ? "patch " : "";
399 const char *const inv
= (var
->data
.invariant
) ? "invariant " : "";
400 fprintf(fp
, "%s%s%s%s%s %s ",
401 cent
, samp
, patch
, inv
, get_variable_mode_str(var
->data
.mode
),
402 glsl_interp_mode_name(var
->data
.interpolation
));
404 const char *const coher
= (var
->data
.image
.coherent
) ? "coherent " : "";
405 const char *const volat
= (var
->data
.image
._volatile
) ? "volatile " : "";
406 const char *const restr
= (var
->data
.image
.restrict_flag
) ? "restrict " : "";
407 const char *const ronly
= (var
->data
.image
.read_only
) ? "readonly " : "";
408 const char *const wonly
= (var
->data
.image
.write_only
) ? "writeonly " : "";
409 fprintf(fp
, "%s%s%s%s%s", coher
, volat
, restr
, ronly
, wonly
);
411 fprintf(fp
, "%s %s", glsl_get_type_name(var
->type
),
412 get_var_name(var
, state
));
414 if (var
->data
.mode
== nir_var_shader_in
||
415 var
->data
.mode
== nir_var_shader_out
||
416 var
->data
.mode
== nir_var_uniform
||
417 var
->data
.mode
== nir_var_shader_storage
) {
418 const char *loc
= NULL
;
421 switch (state
->shader
->info
.stage
) {
422 case MESA_SHADER_VERTEX
:
423 if (var
->data
.mode
== nir_var_shader_in
)
424 loc
= gl_vert_attrib_name(var
->data
.location
);
425 else if (var
->data
.mode
== nir_var_shader_out
)
426 loc
= gl_varying_slot_name(var
->data
.location
);
428 case MESA_SHADER_GEOMETRY
:
429 if ((var
->data
.mode
== nir_var_shader_in
) ||
430 (var
->data
.mode
== nir_var_shader_out
))
431 loc
= gl_varying_slot_name(var
->data
.location
);
433 case MESA_SHADER_FRAGMENT
:
434 if (var
->data
.mode
== nir_var_shader_in
)
435 loc
= gl_varying_slot_name(var
->data
.location
);
436 else if (var
->data
.mode
== nir_var_shader_out
)
437 loc
= gl_frag_result_name(var
->data
.location
);
439 case MESA_SHADER_TESS_CTRL
:
440 case MESA_SHADER_TESS_EVAL
:
441 case MESA_SHADER_COMPUTE
:
448 snprintf(buf
, sizeof(buf
), "%u", var
->data
.location
);
452 /* For shader I/O vars that have been split to components or packed,
453 * print the fractional location within the input/output.
455 unsigned int num_components
=
456 glsl_get_components(glsl_without_array(var
->type
));
457 const char *components
= NULL
;
458 char components_local
[6] = {'.' /* the rest is 0-filled */};
459 switch (var
->data
.mode
) {
460 case nir_var_shader_in
:
461 case nir_var_shader_out
:
462 if (num_components
< 4 && num_components
!= 0) {
463 const char *xyzw
= "xyzw";
464 for (int i
= 0; i
< num_components
; i
++)
465 components_local
[i
+ 1] = xyzw
[i
+ var
->data
.location_frac
];
467 components
= components_local
;
474 fprintf(fp
, " (%s%s, %u, %u)%s", loc
,
475 components
? components
: "",
476 var
->data
.driver_location
, var
->data
.binding
,
477 var
->data
.compact
? " compact" : "");
480 if (var
->constant_initializer
) {
481 fprintf(fp
, " = { ");
482 print_constant(var
->constant_initializer
, var
->type
, state
);
487 print_annotation(state
, var
);
491 print_var(nir_variable
*var
, print_state
*state
)
493 FILE *fp
= state
->fp
;
494 fprintf(fp
, "%s", get_var_name(var
, state
));
498 print_arg(nir_variable
*var
, print_state
*state
)
500 FILE *fp
= state
->fp
;
501 fprintf(fp
, "%s %s", glsl_get_type_name(var
->type
),
502 get_var_name(var
, state
));
506 print_deref_var(nir_deref_var
*deref
, print_state
*state
)
508 print_var(deref
->var
, state
);
512 print_deref_array(nir_deref_array
*deref
, print_state
*state
)
514 FILE *fp
= state
->fp
;
516 switch (deref
->deref_array_type
) {
517 case nir_deref_array_type_direct
:
518 fprintf(fp
, "%u", deref
->base_offset
);
520 case nir_deref_array_type_indirect
:
521 if (deref
->base_offset
!= 0)
522 fprintf(fp
, "%u + ", deref
->base_offset
);
523 print_src(&deref
->indirect
, state
);
525 case nir_deref_array_type_wildcard
:
533 print_deref_struct(nir_deref_struct
*deref
, const struct glsl_type
*parent_type
,
536 FILE *fp
= state
->fp
;
537 fprintf(fp
, ".%s", glsl_get_struct_elem_name(parent_type
, deref
->index
));
541 print_deref(nir_deref_var
*deref
, print_state
*state
)
543 nir_deref
*tail
= &deref
->deref
;
544 nir_deref
*pretail
= NULL
;
545 while (tail
!= NULL
) {
546 switch (tail
->deref_type
) {
547 case nir_deref_type_var
:
548 assert(pretail
== NULL
);
549 assert(tail
== &deref
->deref
);
550 print_deref_var(deref
, state
);
553 case nir_deref_type_array
:
554 assert(pretail
!= NULL
);
555 print_deref_array(nir_deref_as_array(tail
), state
);
558 case nir_deref_type_struct
:
559 assert(pretail
!= NULL
);
560 print_deref_struct(nir_deref_as_struct(tail
),
561 pretail
->type
, state
);
565 unreachable("Invalid deref type");
569 tail
= pretail
->child
;
574 print_intrinsic_instr(nir_intrinsic_instr
*instr
, print_state
*state
)
576 const nir_intrinsic_info
*info
= &nir_intrinsic_infos
[instr
->intrinsic
];
577 unsigned num_srcs
= info
->num_srcs
;
578 FILE *fp
= state
->fp
;
580 if (info
->has_dest
) {
581 print_dest(&instr
->dest
, state
);
585 fprintf(fp
, "intrinsic %s (", info
->name
);
587 for (unsigned i
= 0; i
< num_srcs
; i
++) {
591 print_src(&instr
->src
[i
], state
);
596 for (unsigned i
= 0; i
< info
->num_variables
; i
++) {
600 print_deref(instr
->variables
[i
], state
);
605 for (unsigned i
= 0; i
< info
->num_indices
; i
++) {
609 fprintf(fp
, "%d", instr
->const_index
[i
]);
614 static const char *index_name
[NIR_INTRINSIC_NUM_INDEX_FLAGS
] = {
615 [NIR_INTRINSIC_BASE
] = "base",
616 [NIR_INTRINSIC_WRMASK
] = "wrmask",
617 [NIR_INTRINSIC_STREAM_ID
] = "stream-id",
618 [NIR_INTRINSIC_UCP_ID
] = "ucp-id",
619 [NIR_INTRINSIC_RANGE
] = "range",
620 [NIR_INTRINSIC_DESC_SET
] = "desc-set",
621 [NIR_INTRINSIC_BINDING
] = "binding",
622 [NIR_INTRINSIC_COMPONENT
] = "component",
623 [NIR_INTRINSIC_INTERP_MODE
] = "interp_mode",
624 [NIR_INTRINSIC_REDUCTION_OP
] = "reduction_op",
625 [NIR_INTRINSIC_CLUSTER_SIZE
] = "cluster_size",
627 for (unsigned idx
= 1; idx
< NIR_INTRINSIC_NUM_INDEX_FLAGS
; idx
++) {
628 if (!info
->index_map
[idx
])
631 if (idx
== NIR_INTRINSIC_WRMASK
) {
632 /* special case wrmask to show it as a writemask.. */
633 unsigned wrmask
= nir_intrinsic_write_mask(instr
);
634 fprintf(fp
, " wrmask=");
635 for (unsigned i
= 0; i
< 4; i
++)
636 if ((wrmask
>> i
) & 1)
637 fprintf(fp
, "%c", "xyzw"[i
]);
638 } else if (idx
== NIR_INTRINSIC_REDUCTION_OP
) {
639 nir_op reduction_op
= nir_intrinsic_reduction_op(instr
);
640 fprintf(fp
, " reduction_op=%s", nir_op_infos
[reduction_op
].name
);
642 unsigned off
= info
->index_map
[idx
] - 1;
643 assert(index_name
[idx
]); /* forgot to update index_name table? */
644 fprintf(fp
, " %s=%d", index_name
[idx
], instr
->const_index
[off
]);
652 struct exec_list
*var_list
= NULL
;
654 switch (instr
->intrinsic
) {
655 case nir_intrinsic_load_uniform
:
656 var_list
= &state
->shader
->uniforms
;
658 case nir_intrinsic_load_input
:
659 case nir_intrinsic_load_per_vertex_input
:
660 var_list
= &state
->shader
->inputs
;
662 case nir_intrinsic_load_output
:
663 case nir_intrinsic_store_output
:
664 case nir_intrinsic_store_per_vertex_output
:
665 var_list
= &state
->shader
->outputs
;
671 nir_foreach_variable(var
, var_list
) {
672 if ((var
->data
.driver_location
== nir_intrinsic_base(instr
)) &&
673 (instr
->intrinsic
== nir_intrinsic_load_uniform
||
674 var
->data
.location_frac
== nir_intrinsic_component(instr
)) &&
676 fprintf(fp
, "\t/* %s */", var
->name
);
683 print_tex_instr(nir_tex_instr
*instr
, print_state
*state
)
685 FILE *fp
= state
->fp
;
687 print_dest(&instr
->dest
, state
);
707 case nir_texop_txf_ms
:
708 fprintf(fp
, "txf_ms ");
710 case nir_texop_txf_ms_mcs
:
711 fprintf(fp
, "txf_ms_mcs ");
722 case nir_texop_query_levels
:
723 fprintf(fp
, "query_levels ");
725 case nir_texop_texture_samples
:
726 fprintf(fp
, "texture_samples ");
728 case nir_texop_samples_identical
:
729 fprintf(fp
, "samples_identical ");
732 unreachable("Invalid texture operation");
736 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++) {
737 print_src(&instr
->src
[i
].src
, state
);
741 switch(instr
->src
[i
].src_type
) {
742 case nir_tex_src_coord
:
743 fprintf(fp
, "(coord)");
745 case nir_tex_src_projector
:
746 fprintf(fp
, "(projector)");
748 case nir_tex_src_comparator
:
749 fprintf(fp
, "(comparator)");
751 case nir_tex_src_offset
:
752 fprintf(fp
, "(offset)");
754 case nir_tex_src_bias
:
755 fprintf(fp
, "(bias)");
757 case nir_tex_src_lod
:
758 fprintf(fp
, "(lod)");
760 case nir_tex_src_ms_index
:
761 fprintf(fp
, "(ms_index)");
763 case nir_tex_src_ms_mcs
:
764 fprintf(fp
, "(ms_mcs)");
766 case nir_tex_src_ddx
:
767 fprintf(fp
, "(ddx)");
769 case nir_tex_src_ddy
:
770 fprintf(fp
, "(ddy)");
772 case nir_tex_src_texture_offset
:
773 fprintf(fp
, "(texture_offset)");
775 case nir_tex_src_sampler_offset
:
776 fprintf(fp
, "(sampler_offset)");
778 case nir_tex_src_plane
:
779 fprintf(fp
, "(plane)");
783 unreachable("Invalid texture source type");
790 if (instr
->op
== nir_texop_tg4
) {
791 fprintf(fp
, "%u (gather_component), ", instr
->component
);
794 if (instr
->texture
) {
795 print_deref(instr
->texture
, state
);
796 fprintf(fp
, " (texture)");
797 if (instr
->sampler
) {
798 print_deref(instr
->sampler
, state
);
799 fprintf(fp
, " (sampler)");
802 assert(instr
->sampler
== NULL
);
803 fprintf(fp
, "%u (texture) %u (sampler)",
804 instr
->texture_index
, instr
->sampler_index
);
809 print_call_instr(nir_call_instr
*instr
, print_state
*state
)
811 FILE *fp
= state
->fp
;
813 fprintf(fp
, "call %s ", instr
->callee
->name
);
815 for (unsigned i
= 0; i
< instr
->num_params
; i
++) {
819 print_deref(instr
->params
[i
], state
);
822 if (instr
->return_deref
!= NULL
) {
823 if (instr
->num_params
!= 0)
825 fprintf(fp
, "returning ");
826 print_deref(instr
->return_deref
, state
);
831 print_load_const_instr(nir_load_const_instr
*instr
, print_state
*state
)
833 FILE *fp
= state
->fp
;
835 print_ssa_def(&instr
->def
, state
);
837 fprintf(fp
, " = load_const (");
839 for (unsigned i
= 0; i
< instr
->def
.num_components
; i
++) {
844 * we don't really know the type of the constant (if it will be used as a
845 * float or an int), so just print the raw constant in hex for fidelity
846 * and then print the float in a comment for readability.
849 if (instr
->def
.bit_size
== 64)
850 fprintf(fp
, "0x%16" PRIx64
" /* %f */", instr
->value
.u64
[i
],
851 instr
->value
.f64
[i
]);
853 fprintf(fp
, "0x%08x /* %f */", instr
->value
.u32
[i
], instr
->value
.f32
[i
]);
860 print_jump_instr(nir_jump_instr
*instr
, print_state
*state
)
862 FILE *fp
= state
->fp
;
864 switch (instr
->type
) {
866 fprintf(fp
, "break");
869 case nir_jump_continue
:
870 fprintf(fp
, "continue");
873 case nir_jump_return
:
874 fprintf(fp
, "return");
880 print_ssa_undef_instr(nir_ssa_undef_instr
* instr
, print_state
*state
)
882 FILE *fp
= state
->fp
;
883 print_ssa_def(&instr
->def
, state
);
884 fprintf(fp
, " = undefined");
888 print_phi_instr(nir_phi_instr
*instr
, print_state
*state
)
890 FILE *fp
= state
->fp
;
891 print_dest(&instr
->dest
, state
);
892 fprintf(fp
, " = phi ");
893 nir_foreach_phi_src(src
, instr
) {
894 if (&src
->node
!= exec_list_get_head(&instr
->srcs
))
897 fprintf(fp
, "block_%u: ", src
->pred
->index
);
898 print_src(&src
->src
, state
);
903 print_parallel_copy_instr(nir_parallel_copy_instr
*instr
, print_state
*state
)
905 FILE *fp
= state
->fp
;
906 nir_foreach_parallel_copy_entry(entry
, instr
) {
907 if (&entry
->node
!= exec_list_get_head(&instr
->entries
))
910 print_dest(&entry
->dest
, state
);
912 print_src(&entry
->src
, state
);
917 print_instr(const nir_instr
*instr
, print_state
*state
, unsigned tabs
)
919 FILE *fp
= state
->fp
;
920 print_tabs(tabs
, fp
);
922 switch (instr
->type
) {
923 case nir_instr_type_alu
:
924 print_alu_instr(nir_instr_as_alu(instr
), state
);
927 case nir_instr_type_call
:
928 print_call_instr(nir_instr_as_call(instr
), state
);
931 case nir_instr_type_intrinsic
:
932 print_intrinsic_instr(nir_instr_as_intrinsic(instr
), state
);
935 case nir_instr_type_tex
:
936 print_tex_instr(nir_instr_as_tex(instr
), state
);
939 case nir_instr_type_load_const
:
940 print_load_const_instr(nir_instr_as_load_const(instr
), state
);
943 case nir_instr_type_jump
:
944 print_jump_instr(nir_instr_as_jump(instr
), state
);
947 case nir_instr_type_ssa_undef
:
948 print_ssa_undef_instr(nir_instr_as_ssa_undef(instr
), state
);
951 case nir_instr_type_phi
:
952 print_phi_instr(nir_instr_as_phi(instr
), state
);
955 case nir_instr_type_parallel_copy
:
956 print_parallel_copy_instr(nir_instr_as_parallel_copy(instr
), state
);
960 unreachable("Invalid instruction type");
966 compare_block_index(const void *p1
, const void *p2
)
968 const nir_block
*block1
= *((const nir_block
**) p1
);
969 const nir_block
*block2
= *((const nir_block
**) p2
);
971 return (int) block1
->index
- (int) block2
->index
;
974 static void print_cf_node(nir_cf_node
*node
, print_state
*state
,
978 print_block(nir_block
*block
, print_state
*state
, unsigned tabs
)
980 FILE *fp
= state
->fp
;
982 print_tabs(tabs
, fp
);
983 fprintf(fp
, "block block_%u:\n", block
->index
);
985 /* sort the predecessors by index so we consistently print the same thing */
988 malloc(block
->predecessors
->entries
* sizeof(nir_block
*));
990 struct set_entry
*entry
;
992 set_foreach(block
->predecessors
, entry
) {
993 preds
[i
++] = (nir_block
*) entry
->key
;
996 qsort(preds
, block
->predecessors
->entries
, sizeof(nir_block
*),
997 compare_block_index
);
999 print_tabs(tabs
, fp
);
1000 fprintf(fp
, "/* preds: ");
1001 for (unsigned i
= 0; i
< block
->predecessors
->entries
; i
++) {
1002 fprintf(fp
, "block_%u ", preds
[i
]->index
);
1004 fprintf(fp
, "*/\n");
1008 nir_foreach_instr(instr
, block
) {
1009 print_instr(instr
, state
, tabs
);
1011 print_annotation(state
, instr
);
1014 print_tabs(tabs
, fp
);
1015 fprintf(fp
, "/* succs: ");
1016 for (unsigned i
= 0; i
< 2; i
++)
1017 if (block
->successors
[i
]) {
1018 fprintf(fp
, "block_%u ", block
->successors
[i
]->index
);
1020 fprintf(fp
, "*/\n");
1024 print_if(nir_if
*if_stmt
, print_state
*state
, unsigned tabs
)
1026 FILE *fp
= state
->fp
;
1028 print_tabs(tabs
, fp
);
1030 print_src(&if_stmt
->condition
, state
);
1031 fprintf(fp
, " {\n");
1032 foreach_list_typed(nir_cf_node
, node
, node
, &if_stmt
->then_list
) {
1033 print_cf_node(node
, state
, tabs
+ 1);
1035 print_tabs(tabs
, fp
);
1036 fprintf(fp
, "} else {\n");
1037 foreach_list_typed(nir_cf_node
, node
, node
, &if_stmt
->else_list
) {
1038 print_cf_node(node
, state
, tabs
+ 1);
1040 print_tabs(tabs
, fp
);
1045 print_loop(nir_loop
*loop
, print_state
*state
, unsigned tabs
)
1047 FILE *fp
= state
->fp
;
1049 print_tabs(tabs
, fp
);
1050 fprintf(fp
, "loop {\n");
1051 foreach_list_typed(nir_cf_node
, node
, node
, &loop
->body
) {
1052 print_cf_node(node
, state
, tabs
+ 1);
1054 print_tabs(tabs
, fp
);
1059 print_cf_node(nir_cf_node
*node
, print_state
*state
, unsigned int tabs
)
1061 switch (node
->type
) {
1062 case nir_cf_node_block
:
1063 print_block(nir_cf_node_as_block(node
), state
, tabs
);
1066 case nir_cf_node_if
:
1067 print_if(nir_cf_node_as_if(node
), state
, tabs
);
1070 case nir_cf_node_loop
:
1071 print_loop(nir_cf_node_as_loop(node
), state
, tabs
);
1075 unreachable("Invalid CFG node type");
1080 print_function_impl(nir_function_impl
*impl
, print_state
*state
)
1082 FILE *fp
= state
->fp
;
1084 fprintf(fp
, "\nimpl %s ", impl
->function
->name
);
1086 for (unsigned i
= 0; i
< impl
->num_params
; i
++) {
1090 print_arg(impl
->params
[i
], state
);
1093 if (impl
->return_var
!= NULL
) {
1094 if (impl
->num_params
!= 0)
1096 fprintf(fp
, "returning ");
1097 print_arg(impl
->return_var
, state
);
1102 nir_foreach_variable(var
, &impl
->locals
) {
1104 print_var_decl(var
, state
);
1107 foreach_list_typed(nir_register
, reg
, node
, &impl
->registers
) {
1109 print_register_decl(reg
, state
);
1112 nir_index_blocks(impl
);
1114 foreach_list_typed(nir_cf_node
, node
, node
, &impl
->body
) {
1115 print_cf_node(node
, state
, 1);
1118 fprintf(fp
, "\tblock block_%u:\n}\n\n", impl
->end_block
->index
);
1122 print_function(nir_function
*function
, print_state
*state
)
1124 FILE *fp
= state
->fp
;
1126 fprintf(fp
, "decl_function %s ", function
->name
);
1128 for (unsigned i
= 0; i
< function
->num_params
; i
++) {
1132 switch (function
->params
[i
].param_type
) {
1133 case nir_parameter_in
:
1136 case nir_parameter_out
:
1137 fprintf(fp
, "out ");
1139 case nir_parameter_inout
:
1140 fprintf(fp
, "inout ");
1143 unreachable("Invalid parameter type");
1146 fprintf(fp
, "%s", glsl_get_type_name(function
->params
[i
].type
));
1149 if (function
->return_type
!= NULL
) {
1150 if (function
->num_params
!= 0)
1152 fprintf(fp
, "returning %s", glsl_get_type_name(function
->return_type
));
1157 if (function
->impl
!= NULL
) {
1158 print_function_impl(function
->impl
, state
);
1164 init_print_state(print_state
*state
, nir_shader
*shader
, FILE *fp
)
1167 state
->shader
= shader
;
1168 state
->ht
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
1169 _mesa_key_pointer_equal
);
1170 state
->syms
= _mesa_set_create(NULL
, _mesa_key_hash_string
,
1171 _mesa_key_string_equal
);
1176 destroy_print_state(print_state
*state
)
1178 _mesa_hash_table_destroy(state
->ht
, NULL
);
1179 _mesa_set_destroy(state
->syms
, NULL
);
1183 nir_print_shader_annotated(nir_shader
*shader
, FILE *fp
,
1184 struct hash_table
*annotations
)
1187 init_print_state(&state
, shader
, fp
);
1189 state
.annotations
= annotations
;
1191 fprintf(fp
, "shader: %s\n", gl_shader_stage_name(shader
->info
.stage
));
1193 if (shader
->info
.name
)
1194 fprintf(fp
, "name: %s\n", shader
->info
.name
);
1196 if (shader
->info
.label
)
1197 fprintf(fp
, "label: %s\n", shader
->info
.label
);
1199 switch (shader
->info
.stage
) {
1200 case MESA_SHADER_COMPUTE
:
1201 fprintf(fp
, "local-size: %u, %u, %u%s\n",
1202 shader
->info
.cs
.local_size
[0],
1203 shader
->info
.cs
.local_size
[1],
1204 shader
->info
.cs
.local_size
[2],
1205 shader
->info
.cs
.local_size_variable
? " (variable)" : "");
1206 fprintf(fp
, "shared-size: %u\n", shader
->info
.cs
.shared_size
);
1212 fprintf(fp
, "inputs: %u\n", shader
->num_inputs
);
1213 fprintf(fp
, "outputs: %u\n", shader
->num_outputs
);
1214 fprintf(fp
, "uniforms: %u\n", shader
->num_uniforms
);
1215 fprintf(fp
, "shared: %u\n", shader
->num_shared
);
1217 nir_foreach_variable(var
, &shader
->uniforms
) {
1218 print_var_decl(var
, &state
);
1221 nir_foreach_variable(var
, &shader
->inputs
) {
1222 print_var_decl(var
, &state
);
1225 nir_foreach_variable(var
, &shader
->outputs
) {
1226 print_var_decl(var
, &state
);
1229 nir_foreach_variable(var
, &shader
->shared
) {
1230 print_var_decl(var
, &state
);
1233 nir_foreach_variable(var
, &shader
->globals
) {
1234 print_var_decl(var
, &state
);
1237 nir_foreach_variable(var
, &shader
->system_values
) {
1238 print_var_decl(var
, &state
);
1241 foreach_list_typed(nir_register
, reg
, node
, &shader
->registers
) {
1242 print_register_decl(reg
, &state
);
1245 foreach_list_typed(nir_function
, func
, node
, &shader
->functions
) {
1246 print_function(func
, &state
);
1249 destroy_print_state(&state
);
1253 nir_print_shader(nir_shader
*shader
, FILE *fp
)
1255 nir_print_shader_annotated(shader
, fp
, NULL
);
1259 nir_print_instr(const nir_instr
*instr
, FILE *fp
)
1261 print_state state
= {
1264 print_instr(instr
, &state
, 0);