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"
30 #include "util/half_float.h"
33 #include <inttypes.h> /* for PRIx64 macro */
36 print_tabs(unsigned num_tabs
, FILE *fp
)
38 for (unsigned i
= 0; i
< num_tabs
; i
++)
45 /** map from nir_variable -> printable name */
46 struct hash_table
*ht
;
48 /** set of names used so far for nir_variables */
51 /* an index used to make new non-conflicting names */
55 * Optional table of annotations mapping nir object
56 * (such as instr or var) to message to print.
58 struct hash_table
*annotations
;
62 print_annotation(print_state
*state
, void *obj
)
64 if (!state
->annotations
)
67 struct hash_entry
*entry
= _mesa_hash_table_search(state
->annotations
, obj
);
71 const char *note
= entry
->data
;
72 _mesa_hash_table_remove(state
->annotations
, entry
);
74 fprintf(stderr
, "%s\n\n", note
);
78 print_register(nir_register
*reg
, print_state
*state
)
81 if (reg
->name
!= NULL
)
82 fprintf(fp
, "/* %s */ ", reg
->name
);
84 fprintf(fp
, "gr%u", reg
->index
);
86 fprintf(fp
, "r%u", reg
->index
);
89 static const char *sizes
[] = { "error", "vec1", "vec2", "vec3", "vec4",
90 "error", "error", "error", "vec8",
91 "error", "error", "error", "error",
92 "error", "error", "error", "vec16"};
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 nir_component_mask_t used_channels
= 0;
191 for (unsigned i
= 0; i
< NIR_MAX_VEC_COMPONENTS
; i
++) {
192 if (!nir_alu_instr_channel_used(instr
, src
, i
))
197 if (instr
->src
[src
].swizzle
[i
] != i
) {
198 print_swizzle
= true;
203 unsigned live_channels
= nir_src_num_components(instr
->src
[src
].src
);
205 if (print_swizzle
|| used_channels
!= live_channels
) {
207 for (unsigned i
= 0; i
< NIR_MAX_VEC_COMPONENTS
; 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
< NIR_MAX_VEC_COMPONENTS
; 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
)) {
301 case GLSL_TYPE_UINT8
:
303 /* Only float base types can be matrices. */
306 for (i
= 0; i
< rows
; i
++) {
307 if (i
> 0) fprintf(fp
, ", ");
308 fprintf(fp
, "0x%02x", c
->values
[0].u8
[i
]);
312 case GLSL_TYPE_UINT16
:
313 case GLSL_TYPE_INT16
:
314 /* Only float base types can be matrices. */
317 for (i
= 0; i
< rows
; i
++) {
318 if (i
> 0) fprintf(fp
, ", ");
319 fprintf(fp
, "0x%04x", c
->values
[0].u16
[i
]);
326 /* Only float base types can be matrices. */
329 for (i
= 0; i
< rows
; i
++) {
330 if (i
> 0) fprintf(fp
, ", ");
331 fprintf(fp
, "0x%08x", c
->values
[0].u32
[i
]);
335 case GLSL_TYPE_FLOAT16
:
336 for (i
= 0; i
< cols
; i
++) {
337 for (j
= 0; j
< rows
; j
++) {
338 if (i
+ j
> 0) fprintf(fp
, ", ");
339 fprintf(fp
, "%f", _mesa_half_to_float(c
->values
[i
].u16
[j
]));
344 case GLSL_TYPE_FLOAT
:
345 for (i
= 0; i
< cols
; i
++) {
346 for (j
= 0; j
< rows
; j
++) {
347 if (i
+ j
> 0) fprintf(fp
, ", ");
348 fprintf(fp
, "%f", c
->values
[i
].f32
[j
]);
353 case GLSL_TYPE_DOUBLE
:
354 for (i
= 0; i
< cols
; i
++) {
355 for (j
= 0; j
< rows
; j
++) {
356 if (i
+ j
> 0) fprintf(fp
, ", ");
357 fprintf(fp
, "%f", c
->values
[i
].f64
[j
]);
362 case GLSL_TYPE_UINT64
:
363 case GLSL_TYPE_INT64
:
364 /* Only float base types can be matrices. */
367 for (i
= 0; i
< cols
; i
++) {
368 if (i
> 0) fprintf(fp
, ", ");
369 fprintf(fp
, "0x%08" PRIx64
, c
->values
[0].u64
[i
]);
373 case GLSL_TYPE_STRUCT
:
374 for (i
= 0; i
< c
->num_elements
; i
++) {
375 if (i
> 0) fprintf(fp
, ", ");
377 print_constant(c
->elements
[i
], glsl_get_struct_field(type
, i
), state
);
382 case GLSL_TYPE_ARRAY
:
383 for (i
= 0; i
< c
->num_elements
; i
++) {
384 if (i
> 0) fprintf(fp
, ", ");
386 print_constant(c
->elements
[i
], glsl_get_array_element(type
), state
);
392 unreachable("not reached");
397 get_variable_mode_str(nir_variable_mode mode
, bool want_local_global_mode
)
400 case nir_var_shader_in
:
402 case nir_var_shader_out
:
404 case nir_var_uniform
:
406 case nir_var_shader_storage
:
407 return "shader_storage";
408 case nir_var_system_value
:
413 return want_local_global_mode
? "global" : "";
415 return want_local_global_mode
? "local" : "";
422 print_var_decl(nir_variable
*var
, print_state
*state
)
424 FILE *fp
= state
->fp
;
426 fprintf(fp
, "decl_var ");
428 const char *const cent
= (var
->data
.centroid
) ? "centroid " : "";
429 const char *const samp
= (var
->data
.sample
) ? "sample " : "";
430 const char *const patch
= (var
->data
.patch
) ? "patch " : "";
431 const char *const inv
= (var
->data
.invariant
) ? "invariant " : "";
432 fprintf(fp
, "%s%s%s%s%s %s ",
433 cent
, samp
, patch
, inv
, get_variable_mode_str(var
->data
.mode
, false),
434 glsl_interp_mode_name(var
->data
.interpolation
));
436 enum gl_access_qualifier access
= var
->data
.image
.access
;
437 const char *const coher
= (access
& ACCESS_COHERENT
) ? "coherent " : "";
438 const char *const volat
= (access
& ACCESS_VOLATILE
) ? "volatile " : "";
439 const char *const restr
= (access
& ACCESS_RESTRICT
) ? "restrict " : "";
440 const char *const ronly
= (access
& ACCESS_NON_WRITEABLE
) ? "readonly " : "";
441 const char *const wonly
= (access
& ACCESS_NON_READABLE
) ? "writeonly " : "";
442 fprintf(fp
, "%s%s%s%s%s", coher
, volat
, restr
, ronly
, wonly
);
444 fprintf(fp
, "%s %s", glsl_get_type_name(var
->type
),
445 get_var_name(var
, state
));
447 if (var
->data
.mode
== nir_var_shader_in
||
448 var
->data
.mode
== nir_var_shader_out
||
449 var
->data
.mode
== nir_var_uniform
||
450 var
->data
.mode
== nir_var_shader_storage
) {
451 const char *loc
= NULL
;
454 switch (state
->shader
->info
.stage
) {
455 case MESA_SHADER_VERTEX
:
456 if (var
->data
.mode
== nir_var_shader_in
)
457 loc
= gl_vert_attrib_name(var
->data
.location
);
458 else if (var
->data
.mode
== nir_var_shader_out
)
459 loc
= gl_varying_slot_name(var
->data
.location
);
461 case MESA_SHADER_GEOMETRY
:
462 if ((var
->data
.mode
== nir_var_shader_in
) ||
463 (var
->data
.mode
== nir_var_shader_out
))
464 loc
= gl_varying_slot_name(var
->data
.location
);
466 case MESA_SHADER_FRAGMENT
:
467 if (var
->data
.mode
== nir_var_shader_in
)
468 loc
= gl_varying_slot_name(var
->data
.location
);
469 else if (var
->data
.mode
== nir_var_shader_out
)
470 loc
= gl_frag_result_name(var
->data
.location
);
472 case MESA_SHADER_TESS_CTRL
:
473 case MESA_SHADER_TESS_EVAL
:
474 case MESA_SHADER_COMPUTE
:
481 snprintf(buf
, sizeof(buf
), "%u", var
->data
.location
);
485 /* For shader I/O vars that have been split to components or packed,
486 * print the fractional location within the input/output.
488 unsigned int num_components
=
489 glsl_get_components(glsl_without_array(var
->type
));
490 const char *components
= NULL
;
491 char components_local
[6] = {'.' /* the rest is 0-filled */};
492 switch (var
->data
.mode
) {
493 case nir_var_shader_in
:
494 case nir_var_shader_out
:
495 if (num_components
< 4 && num_components
!= 0) {
496 const char *xyzw
= "xyzw";
497 for (int i
= 0; i
< num_components
; i
++)
498 components_local
[i
+ 1] = xyzw
[i
+ var
->data
.location_frac
];
500 components
= components_local
;
507 fprintf(fp
, " (%s%s, %u, %u)%s", loc
,
508 components
? components
: "",
509 var
->data
.driver_location
, var
->data
.binding
,
510 var
->data
.compact
? " compact" : "");
513 if (var
->constant_initializer
) {
514 fprintf(fp
, " = { ");
515 print_constant(var
->constant_initializer
, var
->type
, state
);
520 print_annotation(state
, var
);
524 print_deref_link(nir_deref_instr
*instr
, bool whole_chain
, print_state
*state
)
526 FILE *fp
= state
->fp
;
528 if (instr
->deref_type
== nir_deref_type_var
) {
529 fprintf(fp
, "%s", get_var_name(instr
->var
, state
));
531 } else if (instr
->deref_type
== nir_deref_type_cast
) {
532 fprintf(fp
, "(%s *)", glsl_get_type_name(instr
->type
));
533 print_src(&instr
->parent
, state
);
537 assert(instr
->parent
.is_ssa
);
538 nir_deref_instr
*parent
=
539 nir_instr_as_deref(instr
->parent
.ssa
->parent_instr
);
541 /* Is the parent we're going to print a bare cast? */
542 const bool is_parent_cast
=
543 whole_chain
&& parent
->deref_type
== nir_deref_type_cast
;
545 /* If we're not printing the whole chain, the parent we print will be a SSA
546 * value that represents a pointer. The only deref type that naturally
547 * gives a pointer is a cast.
549 const bool is_parent_pointer
=
550 !whole_chain
|| parent
->deref_type
== nir_deref_type_cast
;
552 /* Struct derefs have a nice syntax that works on pointers, arrays derefs
555 const bool need_deref
=
556 is_parent_pointer
&& instr
->deref_type
!= nir_deref_type_struct
;
558 /* Cast need extra parens and so * dereferences */
559 if (is_parent_cast
|| need_deref
)
566 print_deref_link(parent
, whole_chain
, state
);
568 print_src(&instr
->parent
, state
);
571 if (is_parent_cast
|| need_deref
)
574 switch (instr
->deref_type
) {
575 case nir_deref_type_struct
:
576 fprintf(fp
, "%s%s", is_parent_pointer
? "->" : ".",
577 glsl_get_struct_elem_name(parent
->type
, instr
->strct
.index
));
580 case nir_deref_type_array
: {
581 nir_const_value
*const_index
= nir_src_as_const_value(instr
->arr
.index
);
583 fprintf(fp
, "[%u]", const_index
->u32
[0]);
586 print_src(&instr
->arr
.index
, state
);
592 case nir_deref_type_array_wildcard
:
597 unreachable("Invalid deref instruction type");
602 print_deref_instr(nir_deref_instr
*instr
, print_state
*state
)
604 FILE *fp
= state
->fp
;
606 print_dest(&instr
->dest
, state
);
608 switch (instr
->deref_type
) {
609 case nir_deref_type_var
:
610 fprintf(fp
, " = deref_var ");
612 case nir_deref_type_array
:
613 case nir_deref_type_array_wildcard
:
614 fprintf(fp
, " = deref_array ");
616 case nir_deref_type_struct
:
617 fprintf(fp
, " = deref_struct ");
619 case nir_deref_type_cast
:
620 fprintf(fp
, " = deref_cast ");
623 unreachable("Invalid deref instruction type");
626 /* Only casts naturally return a pointer type */
627 if (instr
->deref_type
!= nir_deref_type_cast
)
630 print_deref_link(instr
, false, state
);
632 fprintf(fp
, " (%s %s) ",
633 get_variable_mode_str(instr
->mode
, true),
634 glsl_get_type_name(instr
->type
));
636 if (instr
->deref_type
!= nir_deref_type_var
&&
637 instr
->deref_type
!= nir_deref_type_cast
) {
638 /* Print the entire chain as a comment */
640 print_deref_link(instr
, true, state
);
646 print_intrinsic_instr(nir_intrinsic_instr
*instr
, print_state
*state
)
648 const nir_intrinsic_info
*info
= &nir_intrinsic_infos
[instr
->intrinsic
];
649 unsigned num_srcs
= info
->num_srcs
;
650 FILE *fp
= state
->fp
;
652 if (info
->has_dest
) {
653 print_dest(&instr
->dest
, state
);
657 fprintf(fp
, "intrinsic %s (", info
->name
);
659 for (unsigned i
= 0; i
< num_srcs
; i
++) {
663 print_src(&instr
->src
[i
], state
);
668 for (unsigned i
= 0; i
< info
->num_indices
; i
++) {
672 fprintf(fp
, "%d", instr
->const_index
[i
]);
677 static const char *index_name
[NIR_INTRINSIC_NUM_INDEX_FLAGS
] = {
678 [NIR_INTRINSIC_BASE
] = "base",
679 [NIR_INTRINSIC_WRMASK
] = "wrmask",
680 [NIR_INTRINSIC_STREAM_ID
] = "stream-id",
681 [NIR_INTRINSIC_UCP_ID
] = "ucp-id",
682 [NIR_INTRINSIC_RANGE
] = "range",
683 [NIR_INTRINSIC_DESC_SET
] = "desc-set",
684 [NIR_INTRINSIC_BINDING
] = "binding",
685 [NIR_INTRINSIC_COMPONENT
] = "component",
686 [NIR_INTRINSIC_INTERP_MODE
] = "interp_mode",
687 [NIR_INTRINSIC_REDUCTION_OP
] = "reduction_op",
688 [NIR_INTRINSIC_CLUSTER_SIZE
] = "cluster_size",
689 [NIR_INTRINSIC_PARAM_IDX
] = "param_idx",
691 for (unsigned idx
= 1; idx
< NIR_INTRINSIC_NUM_INDEX_FLAGS
; idx
++) {
692 if (!info
->index_map
[idx
])
695 if (idx
== NIR_INTRINSIC_WRMASK
) {
696 /* special case wrmask to show it as a writemask.. */
697 unsigned wrmask
= nir_intrinsic_write_mask(instr
);
698 fprintf(fp
, " wrmask=");
699 for (unsigned i
= 0; i
< 4; i
++)
700 if ((wrmask
>> i
) & 1)
701 fprintf(fp
, "%c", "xyzw"[i
]);
702 } else if (idx
== NIR_INTRINSIC_REDUCTION_OP
) {
703 nir_op reduction_op
= nir_intrinsic_reduction_op(instr
);
704 fprintf(fp
, " reduction_op=%s", nir_op_infos
[reduction_op
].name
);
706 unsigned off
= info
->index_map
[idx
] - 1;
707 assert(index_name
[idx
]); /* forgot to update index_name table? */
708 fprintf(fp
, " %s=%d", index_name
[idx
], instr
->const_index
[off
]);
716 struct exec_list
*var_list
= NULL
;
718 switch (instr
->intrinsic
) {
719 case nir_intrinsic_load_uniform
:
720 var_list
= &state
->shader
->uniforms
;
722 case nir_intrinsic_load_input
:
723 case nir_intrinsic_load_per_vertex_input
:
724 var_list
= &state
->shader
->inputs
;
726 case nir_intrinsic_load_output
:
727 case nir_intrinsic_store_output
:
728 case nir_intrinsic_store_per_vertex_output
:
729 var_list
= &state
->shader
->outputs
;
735 nir_foreach_variable(var
, var_list
) {
736 if ((var
->data
.driver_location
== nir_intrinsic_base(instr
)) &&
737 (instr
->intrinsic
== nir_intrinsic_load_uniform
||
738 var
->data
.location_frac
== nir_intrinsic_component(instr
)) &&
740 fprintf(fp
, "\t/* %s */", var
->name
);
747 print_tex_instr(nir_tex_instr
*instr
, print_state
*state
)
749 FILE *fp
= state
->fp
;
751 print_dest(&instr
->dest
, state
);
771 case nir_texop_txf_ms
:
772 fprintf(fp
, "txf_ms ");
774 case nir_texop_txf_ms_mcs
:
775 fprintf(fp
, "txf_ms_mcs ");
786 case nir_texop_query_levels
:
787 fprintf(fp
, "query_levels ");
789 case nir_texop_texture_samples
:
790 fprintf(fp
, "texture_samples ");
792 case nir_texop_samples_identical
:
793 fprintf(fp
, "samples_identical ");
796 unreachable("Invalid texture operation");
800 bool has_texture_deref
= false, has_sampler_deref
= false;
801 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++) {
802 print_src(&instr
->src
[i
].src
, state
);
806 switch(instr
->src
[i
].src_type
) {
807 case nir_tex_src_coord
:
808 fprintf(fp
, "(coord)");
810 case nir_tex_src_projector
:
811 fprintf(fp
, "(projector)");
813 case nir_tex_src_comparator
:
814 fprintf(fp
, "(comparator)");
816 case nir_tex_src_offset
:
817 fprintf(fp
, "(offset)");
819 case nir_tex_src_bias
:
820 fprintf(fp
, "(bias)");
822 case nir_tex_src_lod
:
823 fprintf(fp
, "(lod)");
825 case nir_tex_src_ms_index
:
826 fprintf(fp
, "(ms_index)");
828 case nir_tex_src_ms_mcs
:
829 fprintf(fp
, "(ms_mcs)");
831 case nir_tex_src_ddx
:
832 fprintf(fp
, "(ddx)");
834 case nir_tex_src_ddy
:
835 fprintf(fp
, "(ddy)");
837 case nir_tex_src_texture_deref
:
838 has_texture_deref
= true;
839 fprintf(fp
, "(texture_deref)");
841 case nir_tex_src_sampler_deref
:
842 has_sampler_deref
= true;
843 fprintf(fp
, "(sampler_deref)");
845 case nir_tex_src_texture_offset
:
846 fprintf(fp
, "(texture_offset)");
848 case nir_tex_src_sampler_offset
:
849 fprintf(fp
, "(sampler_offset)");
851 case nir_tex_src_plane
:
852 fprintf(fp
, "(plane)");
856 unreachable("Invalid texture source type");
863 if (instr
->op
== nir_texop_tg4
) {
864 fprintf(fp
, "%u (gather_component), ", instr
->component
);
867 if (!has_texture_deref
) {
868 fprintf(fp
, "%u (texture), ", instr
->texture_index
);
871 if (!has_sampler_deref
) {
872 fprintf(fp
, "%u (sampler), ", instr
->sampler_index
);
877 print_call_instr(nir_call_instr
*instr
, print_state
*state
)
879 FILE *fp
= state
->fp
;
881 fprintf(fp
, "call %s ", instr
->callee
->name
);
883 for (unsigned i
= 0; i
< instr
->num_params
; i
++) {
887 print_src(&instr
->params
[i
], state
);
892 print_load_const_instr(nir_load_const_instr
*instr
, print_state
*state
)
894 FILE *fp
= state
->fp
;
896 print_ssa_def(&instr
->def
, state
);
898 fprintf(fp
, " = load_const (");
900 for (unsigned i
= 0; i
< instr
->def
.num_components
; i
++) {
905 * we don't really know the type of the constant (if it will be used as a
906 * float or an int), so just print the raw constant in hex for fidelity
907 * and then print the float in a comment for readability.
910 switch (instr
->def
.bit_size
) {
912 fprintf(fp
, "0x%16" PRIx64
" /* %f */", instr
->value
.u64
[i
],
913 instr
->value
.f64
[i
]);
916 fprintf(fp
, "0x%08x /* %f */", instr
->value
.u32
[i
], instr
->value
.f32
[i
]);
919 fprintf(fp
, "0x%04x /* %f */", instr
->value
.u16
[i
],
920 _mesa_half_to_float(instr
->value
.u16
[i
]));
923 fprintf(fp
, "0x%02x", instr
->value
.u8
[i
]);
932 print_jump_instr(nir_jump_instr
*instr
, print_state
*state
)
934 FILE *fp
= state
->fp
;
936 switch (instr
->type
) {
938 fprintf(fp
, "break");
941 case nir_jump_continue
:
942 fprintf(fp
, "continue");
945 case nir_jump_return
:
946 fprintf(fp
, "return");
952 print_ssa_undef_instr(nir_ssa_undef_instr
* instr
, print_state
*state
)
954 FILE *fp
= state
->fp
;
955 print_ssa_def(&instr
->def
, state
);
956 fprintf(fp
, " = undefined");
960 print_phi_instr(nir_phi_instr
*instr
, print_state
*state
)
962 FILE *fp
= state
->fp
;
963 print_dest(&instr
->dest
, state
);
964 fprintf(fp
, " = phi ");
965 nir_foreach_phi_src(src
, instr
) {
966 if (&src
->node
!= exec_list_get_head(&instr
->srcs
))
969 fprintf(fp
, "block_%u: ", src
->pred
->index
);
970 print_src(&src
->src
, state
);
975 print_parallel_copy_instr(nir_parallel_copy_instr
*instr
, print_state
*state
)
977 FILE *fp
= state
->fp
;
978 nir_foreach_parallel_copy_entry(entry
, instr
) {
979 if (&entry
->node
!= exec_list_get_head(&instr
->entries
))
982 print_dest(&entry
->dest
, state
);
984 print_src(&entry
->src
, state
);
989 print_instr(const nir_instr
*instr
, print_state
*state
, unsigned tabs
)
991 FILE *fp
= state
->fp
;
992 print_tabs(tabs
, fp
);
994 switch (instr
->type
) {
995 case nir_instr_type_alu
:
996 print_alu_instr(nir_instr_as_alu(instr
), state
);
999 case nir_instr_type_deref
:
1000 print_deref_instr(nir_instr_as_deref(instr
), state
);
1003 case nir_instr_type_call
:
1004 print_call_instr(nir_instr_as_call(instr
), state
);
1007 case nir_instr_type_intrinsic
:
1008 print_intrinsic_instr(nir_instr_as_intrinsic(instr
), state
);
1011 case nir_instr_type_tex
:
1012 print_tex_instr(nir_instr_as_tex(instr
), state
);
1015 case nir_instr_type_load_const
:
1016 print_load_const_instr(nir_instr_as_load_const(instr
), state
);
1019 case nir_instr_type_jump
:
1020 print_jump_instr(nir_instr_as_jump(instr
), state
);
1023 case nir_instr_type_ssa_undef
:
1024 print_ssa_undef_instr(nir_instr_as_ssa_undef(instr
), state
);
1027 case nir_instr_type_phi
:
1028 print_phi_instr(nir_instr_as_phi(instr
), state
);
1031 case nir_instr_type_parallel_copy
:
1032 print_parallel_copy_instr(nir_instr_as_parallel_copy(instr
), state
);
1036 unreachable("Invalid instruction type");
1042 compare_block_index(const void *p1
, const void *p2
)
1044 const nir_block
*block1
= *((const nir_block
**) p1
);
1045 const nir_block
*block2
= *((const nir_block
**) p2
);
1047 return (int) block1
->index
- (int) block2
->index
;
1050 static void print_cf_node(nir_cf_node
*node
, print_state
*state
,
1054 print_block(nir_block
*block
, print_state
*state
, unsigned tabs
)
1056 FILE *fp
= state
->fp
;
1058 print_tabs(tabs
, fp
);
1059 fprintf(fp
, "block block_%u:\n", block
->index
);
1061 /* sort the predecessors by index so we consistently print the same thing */
1064 malloc(block
->predecessors
->entries
* sizeof(nir_block
*));
1066 struct set_entry
*entry
;
1068 set_foreach(block
->predecessors
, entry
) {
1069 preds
[i
++] = (nir_block
*) entry
->key
;
1072 qsort(preds
, block
->predecessors
->entries
, sizeof(nir_block
*),
1073 compare_block_index
);
1075 print_tabs(tabs
, fp
);
1076 fprintf(fp
, "/* preds: ");
1077 for (unsigned i
= 0; i
< block
->predecessors
->entries
; i
++) {
1078 fprintf(fp
, "block_%u ", preds
[i
]->index
);
1080 fprintf(fp
, "*/\n");
1084 nir_foreach_instr(instr
, block
) {
1085 print_instr(instr
, state
, tabs
);
1087 print_annotation(state
, instr
);
1090 print_tabs(tabs
, fp
);
1091 fprintf(fp
, "/* succs: ");
1092 for (unsigned i
= 0; i
< 2; i
++)
1093 if (block
->successors
[i
]) {
1094 fprintf(fp
, "block_%u ", block
->successors
[i
]->index
);
1096 fprintf(fp
, "*/\n");
1100 print_if(nir_if
*if_stmt
, print_state
*state
, unsigned tabs
)
1102 FILE *fp
= state
->fp
;
1104 print_tabs(tabs
, fp
);
1106 print_src(&if_stmt
->condition
, state
);
1107 fprintf(fp
, " {\n");
1108 foreach_list_typed(nir_cf_node
, node
, node
, &if_stmt
->then_list
) {
1109 print_cf_node(node
, state
, tabs
+ 1);
1111 print_tabs(tabs
, fp
);
1112 fprintf(fp
, "} else {\n");
1113 foreach_list_typed(nir_cf_node
, node
, node
, &if_stmt
->else_list
) {
1114 print_cf_node(node
, state
, tabs
+ 1);
1116 print_tabs(tabs
, fp
);
1121 print_loop(nir_loop
*loop
, print_state
*state
, unsigned tabs
)
1123 FILE *fp
= state
->fp
;
1125 print_tabs(tabs
, fp
);
1126 fprintf(fp
, "loop {\n");
1127 foreach_list_typed(nir_cf_node
, node
, node
, &loop
->body
) {
1128 print_cf_node(node
, state
, tabs
+ 1);
1130 print_tabs(tabs
, fp
);
1135 print_cf_node(nir_cf_node
*node
, print_state
*state
, unsigned int tabs
)
1137 switch (node
->type
) {
1138 case nir_cf_node_block
:
1139 print_block(nir_cf_node_as_block(node
), state
, tabs
);
1142 case nir_cf_node_if
:
1143 print_if(nir_cf_node_as_if(node
), state
, tabs
);
1146 case nir_cf_node_loop
:
1147 print_loop(nir_cf_node_as_loop(node
), state
, tabs
);
1151 unreachable("Invalid CFG node type");
1156 print_function_impl(nir_function_impl
*impl
, print_state
*state
)
1158 FILE *fp
= state
->fp
;
1160 fprintf(fp
, "\nimpl %s ", impl
->function
->name
);
1164 nir_foreach_variable(var
, &impl
->locals
) {
1166 print_var_decl(var
, state
);
1169 foreach_list_typed(nir_register
, reg
, node
, &impl
->registers
) {
1171 print_register_decl(reg
, state
);
1174 nir_index_blocks(impl
);
1176 foreach_list_typed(nir_cf_node
, node
, node
, &impl
->body
) {
1177 print_cf_node(node
, state
, 1);
1180 fprintf(fp
, "\tblock block_%u:\n}\n\n", impl
->end_block
->index
);
1184 print_function(nir_function
*function
, print_state
*state
)
1186 FILE *fp
= state
->fp
;
1188 fprintf(fp
, "decl_function %s (%d params)", function
->name
,
1189 function
->num_params
);
1193 if (function
->impl
!= NULL
) {
1194 print_function_impl(function
->impl
, state
);
1200 init_print_state(print_state
*state
, nir_shader
*shader
, FILE *fp
)
1203 state
->shader
= shader
;
1204 state
->ht
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
1205 _mesa_key_pointer_equal
);
1206 state
->syms
= _mesa_set_create(NULL
, _mesa_key_hash_string
,
1207 _mesa_key_string_equal
);
1212 destroy_print_state(print_state
*state
)
1214 _mesa_hash_table_destroy(state
->ht
, NULL
);
1215 _mesa_set_destroy(state
->syms
, NULL
);
1219 nir_print_shader_annotated(nir_shader
*shader
, FILE *fp
,
1220 struct hash_table
*annotations
)
1223 init_print_state(&state
, shader
, fp
);
1225 state
.annotations
= annotations
;
1227 fprintf(fp
, "shader: %s\n", gl_shader_stage_name(shader
->info
.stage
));
1229 if (shader
->info
.name
)
1230 fprintf(fp
, "name: %s\n", shader
->info
.name
);
1232 if (shader
->info
.label
)
1233 fprintf(fp
, "label: %s\n", shader
->info
.label
);
1235 switch (shader
->info
.stage
) {
1236 case MESA_SHADER_COMPUTE
:
1237 fprintf(fp
, "local-size: %u, %u, %u%s\n",
1238 shader
->info
.cs
.local_size
[0],
1239 shader
->info
.cs
.local_size
[1],
1240 shader
->info
.cs
.local_size
[2],
1241 shader
->info
.cs
.local_size_variable
? " (variable)" : "");
1242 fprintf(fp
, "shared-size: %u\n", shader
->info
.cs
.shared_size
);
1248 fprintf(fp
, "inputs: %u\n", shader
->num_inputs
);
1249 fprintf(fp
, "outputs: %u\n", shader
->num_outputs
);
1250 fprintf(fp
, "uniforms: %u\n", shader
->num_uniforms
);
1251 fprintf(fp
, "shared: %u\n", shader
->num_shared
);
1253 nir_foreach_variable(var
, &shader
->uniforms
) {
1254 print_var_decl(var
, &state
);
1257 nir_foreach_variable(var
, &shader
->inputs
) {
1258 print_var_decl(var
, &state
);
1261 nir_foreach_variable(var
, &shader
->outputs
) {
1262 print_var_decl(var
, &state
);
1265 nir_foreach_variable(var
, &shader
->shared
) {
1266 print_var_decl(var
, &state
);
1269 nir_foreach_variable(var
, &shader
->globals
) {
1270 print_var_decl(var
, &state
);
1273 nir_foreach_variable(var
, &shader
->system_values
) {
1274 print_var_decl(var
, &state
);
1277 foreach_list_typed(nir_register
, reg
, node
, &shader
->registers
) {
1278 print_register_decl(reg
, &state
);
1281 foreach_list_typed(nir_function
, func
, node
, &shader
->functions
) {
1282 print_function(func
, &state
);
1285 destroy_print_state(&state
);
1289 nir_print_shader(nir_shader
*shader
, FILE *fp
)
1291 nir_print_shader_annotated(shader
, fp
, NULL
);
1295 nir_print_instr(const nir_instr
*instr
, FILE *fp
)
1297 print_state state
= {
1300 print_instr(instr
, &state
, 0);