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", "vec16"};
94 print_register_decl(nir_register
*reg
, print_state
*state
)
97 fprintf(fp
, "decl_reg %s %u ", sizes
[reg
->num_components
], reg
->bit_size
);
99 fprintf(fp
, "(packed) ");
100 print_register(reg
, state
);
101 if (reg
->num_array_elems
!= 0)
102 fprintf(fp
, "[%u]", reg
->num_array_elems
);
107 print_ssa_def(nir_ssa_def
*def
, print_state
*state
)
109 FILE *fp
= state
->fp
;
110 if (def
->name
!= NULL
)
111 fprintf(fp
, "/* %s */ ", def
->name
);
112 fprintf(fp
, "%s %u ssa_%u", sizes
[def
->num_components
], def
->bit_size
,
117 print_ssa_use(nir_ssa_def
*def
, print_state
*state
)
119 FILE *fp
= state
->fp
;
120 if (def
->name
!= NULL
)
121 fprintf(fp
, "/* %s */ ", def
->name
);
122 fprintf(fp
, "ssa_%u", def
->index
);
125 static void print_src(nir_src
*src
, print_state
*state
);
128 print_reg_src(nir_reg_src
*src
, print_state
*state
)
130 FILE *fp
= state
->fp
;
131 print_register(src
->reg
, state
);
132 if (src
->reg
->num_array_elems
!= 0) {
133 fprintf(fp
, "[%u", src
->base_offset
);
134 if (src
->indirect
!= NULL
) {
136 print_src(src
->indirect
, state
);
143 print_reg_dest(nir_reg_dest
*dest
, print_state
*state
)
145 FILE *fp
= state
->fp
;
146 print_register(dest
->reg
, state
);
147 if (dest
->reg
->num_array_elems
!= 0) {
148 fprintf(fp
, "[%u", dest
->base_offset
);
149 if (dest
->indirect
!= NULL
) {
151 print_src(dest
->indirect
, state
);
158 print_src(nir_src
*src
, print_state
*state
)
161 print_ssa_use(src
->ssa
, state
);
163 print_reg_src(&src
->reg
, state
);
167 print_dest(nir_dest
*dest
, print_state
*state
)
170 print_ssa_def(&dest
->ssa
, state
);
172 print_reg_dest(&dest
->reg
, state
);
176 print_alu_src(nir_alu_instr
*instr
, unsigned src
, print_state
*state
)
178 FILE *fp
= state
->fp
;
180 if (instr
->src
[src
].negate
)
182 if (instr
->src
[src
].abs
)
185 print_src(&instr
->src
[src
].src
, state
);
187 bool print_swizzle
= false;
188 unsigned used_channels
= 0;
190 for (unsigned i
= 0; i
< 4; i
++) {
191 if (!nir_alu_instr_channel_used(instr
, src
, i
))
196 if (instr
->src
[src
].swizzle
[i
] != i
) {
197 print_swizzle
= true;
202 unsigned live_channels
= nir_src_num_components(instr
->src
[src
].src
);
204 if (print_swizzle
|| used_channels
!= live_channels
) {
206 for (unsigned i
= 0; i
< 4; i
++) {
207 if (!nir_alu_instr_channel_used(instr
, src
, i
))
210 fprintf(fp
, "%c", "xyzw"[instr
->src
[src
].swizzle
[i
]]);
214 if (instr
->src
[src
].abs
)
219 print_alu_dest(nir_alu_dest
*dest
, print_state
*state
)
221 FILE *fp
= state
->fp
;
222 /* we're going to print the saturate modifier later, after the opcode */
224 print_dest(&dest
->dest
, state
);
226 if (!dest
->dest
.is_ssa
&&
227 dest
->write_mask
!= (1 << dest
->dest
.reg
.reg
->num_components
) - 1) {
229 for (unsigned i
= 0; i
< 4; i
++)
230 if ((dest
->write_mask
>> i
) & 1)
231 fprintf(fp
, "%c", "xyzw"[i
]);
236 print_alu_instr(nir_alu_instr
*instr
, print_state
*state
)
238 FILE *fp
= state
->fp
;
240 print_alu_dest(&instr
->dest
, state
);
242 fprintf(fp
, " = %s", nir_op_infos
[instr
->op
].name
);
245 if (instr
->dest
.saturate
)
249 for (unsigned i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++) {
253 print_alu_src(instr
, i
, state
);
258 get_var_name(nir_variable
*var
, print_state
*state
)
260 if (state
->ht
== NULL
)
261 return var
->name
? var
->name
: "unnamed";
265 struct hash_entry
*entry
= _mesa_hash_table_search(state
->ht
, var
);
270 if (var
->name
== NULL
) {
271 name
= ralloc_asprintf(state
->syms
, "@%u", state
->index
++);
273 struct set_entry
*set_entry
= _mesa_set_search(state
->syms
, var
->name
);
274 if (set_entry
!= NULL
) {
275 /* we have a collision with another name, append an @ + a unique
277 name
= ralloc_asprintf(state
->syms
, "%s@%u", var
->name
,
280 /* Mark this one as seen */
281 _mesa_set_add(state
->syms
, var
->name
);
286 _mesa_hash_table_insert(state
->ht
, var
, name
);
292 print_constant(nir_constant
*c
, const struct glsl_type
*type
, print_state
*state
)
294 FILE *fp
= state
->fp
;
295 const unsigned rows
= glsl_get_vector_elements(type
);
296 const unsigned cols
= glsl_get_matrix_columns(type
);
299 switch (glsl_get_base_type(type
)) {
300 case GLSL_TYPE_UINT8
:
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%02x", c
->values
[0].u8
[i
]);
311 case GLSL_TYPE_UINT16
:
312 case GLSL_TYPE_INT16
:
313 /* Only float base types can be matrices. */
316 for (i
= 0; i
< rows
; i
++) {
317 if (i
> 0) fprintf(fp
, ", ");
318 fprintf(fp
, "0x%04x", c
->values
[0].u16
[i
]);
325 /* Only float base types can be matrices. */
328 for (i
= 0; i
< rows
; i
++) {
329 if (i
> 0) fprintf(fp
, ", ");
330 fprintf(fp
, "0x%08x", c
->values
[0].u32
[i
]);
334 case GLSL_TYPE_FLOAT16
:
335 for (i
= 0; i
< cols
; i
++) {
336 for (j
= 0; j
< rows
; j
++) {
337 if (i
+ j
> 0) fprintf(fp
, ", ");
338 fprintf(fp
, "%f", _mesa_half_to_float(c
->values
[i
].u16
[j
]));
343 case GLSL_TYPE_FLOAT
:
344 for (i
= 0; i
< cols
; i
++) {
345 for (j
= 0; j
< rows
; j
++) {
346 if (i
+ j
> 0) fprintf(fp
, ", ");
347 fprintf(fp
, "%f", c
->values
[i
].f32
[j
]);
352 case GLSL_TYPE_DOUBLE
:
353 for (i
= 0; i
< cols
; i
++) {
354 for (j
= 0; j
< rows
; j
++) {
355 if (i
+ j
> 0) fprintf(fp
, ", ");
356 fprintf(fp
, "%f", c
->values
[i
].f64
[j
]);
361 case GLSL_TYPE_UINT64
:
362 case GLSL_TYPE_INT64
:
363 /* Only float base types can be matrices. */
366 for (i
= 0; i
< cols
; i
++) {
367 if (i
> 0) fprintf(fp
, ", ");
368 fprintf(fp
, "0x%08" PRIx64
, c
->values
[0].u64
[i
]);
372 case GLSL_TYPE_STRUCT
:
373 for (i
= 0; i
< c
->num_elements
; i
++) {
374 if (i
> 0) fprintf(fp
, ", ");
376 print_constant(c
->elements
[i
], glsl_get_struct_field(type
, i
), state
);
381 case GLSL_TYPE_ARRAY
:
382 for (i
= 0; i
< c
->num_elements
; i
++) {
383 if (i
> 0) fprintf(fp
, ", ");
385 print_constant(c
->elements
[i
], glsl_get_array_element(type
), state
);
391 unreachable("not reached");
396 get_variable_mode_str(nir_variable_mode mode
, bool want_local_global_mode
)
399 case nir_var_shader_in
:
401 case nir_var_shader_out
:
403 case nir_var_uniform
:
405 case nir_var_shader_storage
:
406 return "shader_storage";
407 case nir_var_system_value
:
412 return want_local_global_mode
? "global" : "";
414 return want_local_global_mode
? "local" : "";
421 print_var_decl(nir_variable
*var
, print_state
*state
)
423 FILE *fp
= state
->fp
;
425 fprintf(fp
, "decl_var ");
427 const char *const cent
= (var
->data
.centroid
) ? "centroid " : "";
428 const char *const samp
= (var
->data
.sample
) ? "sample " : "";
429 const char *const patch
= (var
->data
.patch
) ? "patch " : "";
430 const char *const inv
= (var
->data
.invariant
) ? "invariant " : "";
431 fprintf(fp
, "%s%s%s%s%s %s ",
432 cent
, samp
, patch
, inv
, get_variable_mode_str(var
->data
.mode
, false),
433 glsl_interp_mode_name(var
->data
.interpolation
));
435 const char *const coher
= (var
->data
.image
.coherent
) ? "coherent " : "";
436 const char *const volat
= (var
->data
.image
._volatile
) ? "volatile " : "";
437 const char *const restr
= (var
->data
.image
.restrict_flag
) ? "restrict " : "";
438 const char *const ronly
= (var
->data
.image
.read_only
) ? "readonly " : "";
439 const char *const wonly
= (var
->data
.image
.write_only
) ? "writeonly " : "";
440 fprintf(fp
, "%s%s%s%s%s", coher
, volat
, restr
, ronly
, wonly
);
442 fprintf(fp
, "%s %s", glsl_get_type_name(var
->type
),
443 get_var_name(var
, state
));
445 if (var
->data
.mode
== nir_var_shader_in
||
446 var
->data
.mode
== nir_var_shader_out
||
447 var
->data
.mode
== nir_var_uniform
||
448 var
->data
.mode
== nir_var_shader_storage
) {
449 const char *loc
= NULL
;
452 switch (state
->shader
->info
.stage
) {
453 case MESA_SHADER_VERTEX
:
454 if (var
->data
.mode
== nir_var_shader_in
)
455 loc
= gl_vert_attrib_name(var
->data
.location
);
456 else if (var
->data
.mode
== nir_var_shader_out
)
457 loc
= gl_varying_slot_name(var
->data
.location
);
459 case MESA_SHADER_GEOMETRY
:
460 if ((var
->data
.mode
== nir_var_shader_in
) ||
461 (var
->data
.mode
== nir_var_shader_out
))
462 loc
= gl_varying_slot_name(var
->data
.location
);
464 case MESA_SHADER_FRAGMENT
:
465 if (var
->data
.mode
== nir_var_shader_in
)
466 loc
= gl_varying_slot_name(var
->data
.location
);
467 else if (var
->data
.mode
== nir_var_shader_out
)
468 loc
= gl_frag_result_name(var
->data
.location
);
470 case MESA_SHADER_TESS_CTRL
:
471 case MESA_SHADER_TESS_EVAL
:
472 case MESA_SHADER_COMPUTE
:
479 snprintf(buf
, sizeof(buf
), "%u", var
->data
.location
);
483 /* For shader I/O vars that have been split to components or packed,
484 * print the fractional location within the input/output.
486 unsigned int num_components
=
487 glsl_get_components(glsl_without_array(var
->type
));
488 const char *components
= NULL
;
489 char components_local
[6] = {'.' /* the rest is 0-filled */};
490 switch (var
->data
.mode
) {
491 case nir_var_shader_in
:
492 case nir_var_shader_out
:
493 if (num_components
< 4 && num_components
!= 0) {
494 const char *xyzw
= "xyzw";
495 for (int i
= 0; i
< num_components
; i
++)
496 components_local
[i
+ 1] = xyzw
[i
+ var
->data
.location_frac
];
498 components
= components_local
;
505 fprintf(fp
, " (%s%s, %u, %u)%s", loc
,
506 components
? components
: "",
507 var
->data
.driver_location
, var
->data
.binding
,
508 var
->data
.compact
? " compact" : "");
511 if (var
->constant_initializer
) {
512 fprintf(fp
, " = { ");
513 print_constant(var
->constant_initializer
, var
->type
, state
);
518 print_annotation(state
, var
);
522 print_deref_link(nir_deref_instr
*instr
, bool whole_chain
, print_state
*state
)
524 FILE *fp
= state
->fp
;
526 if (instr
->deref_type
== nir_deref_type_var
) {
527 fprintf(fp
, "%s", get_var_name(instr
->var
, state
));
529 } else if (instr
->deref_type
== nir_deref_type_cast
) {
530 fprintf(fp
, "(%s *)", glsl_get_type_name(instr
->type
));
531 print_src(&instr
->parent
, state
);
535 assert(instr
->parent
.is_ssa
);
536 nir_deref_instr
*parent
=
537 nir_instr_as_deref(instr
->parent
.ssa
->parent_instr
);
539 /* Is the parent we're going to print a bare cast? */
540 const bool is_parent_cast
=
541 whole_chain
&& parent
->deref_type
== nir_deref_type_cast
;
543 /* If we're not printing the whole chain, the parent we print will be a SSA
544 * value that represents a pointer. The only deref type that naturally
545 * gives a pointer is a cast.
547 const bool is_parent_pointer
=
548 !whole_chain
|| parent
->deref_type
== nir_deref_type_cast
;
550 /* Struct derefs have a nice syntax that works on pointers, arrays derefs
553 const bool need_deref
=
554 is_parent_pointer
&& instr
->deref_type
!= nir_deref_type_struct
;
556 /* Cast need extra parens and so * dereferences */
557 if (is_parent_cast
|| need_deref
)
564 print_deref_link(parent
, whole_chain
, state
);
566 print_src(&instr
->parent
, state
);
569 if (is_parent_cast
|| need_deref
)
572 switch (instr
->deref_type
) {
573 case nir_deref_type_struct
:
574 fprintf(fp
, "%s%s", is_parent_pointer
? "->" : ".",
575 glsl_get_struct_elem_name(parent
->type
, instr
->strct
.index
));
578 case nir_deref_type_array
: {
579 nir_const_value
*const_index
= nir_src_as_const_value(instr
->arr
.index
);
581 fprintf(fp
, "[%u]", const_index
->u32
[0]);
584 print_src(&instr
->arr
.index
, state
);
590 case nir_deref_type_array_wildcard
:
595 unreachable("Invalid deref instruction type");
600 print_deref_instr(nir_deref_instr
*instr
, print_state
*state
)
602 FILE *fp
= state
->fp
;
604 print_dest(&instr
->dest
, state
);
606 switch (instr
->deref_type
) {
607 case nir_deref_type_var
:
608 fprintf(fp
, " = deref_var ");
610 case nir_deref_type_array
:
611 case nir_deref_type_array_wildcard
:
612 fprintf(fp
, " = deref_array ");
614 case nir_deref_type_struct
:
615 fprintf(fp
, " = deref_struct ");
617 case nir_deref_type_cast
:
618 fprintf(fp
, " = deref_cast ");
621 unreachable("Invalid deref instruction type");
624 /* Only casts naturally return a pointer type */
625 if (instr
->deref_type
!= nir_deref_type_cast
)
628 print_deref_link(instr
, false, state
);
630 fprintf(fp
, " (%s %s) ",
631 get_variable_mode_str(instr
->mode
, true),
632 glsl_get_type_name(instr
->type
));
634 if (instr
->deref_type
!= nir_deref_type_var
&&
635 instr
->deref_type
!= nir_deref_type_cast
) {
636 /* Print the entire chain as a comment */
638 print_deref_link(instr
, true, state
);
644 print_intrinsic_instr(nir_intrinsic_instr
*instr
, print_state
*state
)
646 const nir_intrinsic_info
*info
= &nir_intrinsic_infos
[instr
->intrinsic
];
647 unsigned num_srcs
= info
->num_srcs
;
648 FILE *fp
= state
->fp
;
650 if (info
->has_dest
) {
651 print_dest(&instr
->dest
, state
);
655 fprintf(fp
, "intrinsic %s (", info
->name
);
657 for (unsigned i
= 0; i
< num_srcs
; i
++) {
661 print_src(&instr
->src
[i
], state
);
666 for (unsigned i
= 0; i
< info
->num_indices
; i
++) {
670 fprintf(fp
, "%d", instr
->const_index
[i
]);
675 static const char *index_name
[NIR_INTRINSIC_NUM_INDEX_FLAGS
] = {
676 [NIR_INTRINSIC_BASE
] = "base",
677 [NIR_INTRINSIC_WRMASK
] = "wrmask",
678 [NIR_INTRINSIC_STREAM_ID
] = "stream-id",
679 [NIR_INTRINSIC_UCP_ID
] = "ucp-id",
680 [NIR_INTRINSIC_RANGE
] = "range",
681 [NIR_INTRINSIC_DESC_SET
] = "desc-set",
682 [NIR_INTRINSIC_BINDING
] = "binding",
683 [NIR_INTRINSIC_COMPONENT
] = "component",
684 [NIR_INTRINSIC_INTERP_MODE
] = "interp_mode",
685 [NIR_INTRINSIC_REDUCTION_OP
] = "reduction_op",
686 [NIR_INTRINSIC_CLUSTER_SIZE
] = "cluster_size",
687 [NIR_INTRINSIC_PARAM_IDX
] = "param_idx",
689 for (unsigned idx
= 1; idx
< NIR_INTRINSIC_NUM_INDEX_FLAGS
; idx
++) {
690 if (!info
->index_map
[idx
])
693 if (idx
== NIR_INTRINSIC_WRMASK
) {
694 /* special case wrmask to show it as a writemask.. */
695 unsigned wrmask
= nir_intrinsic_write_mask(instr
);
696 fprintf(fp
, " wrmask=");
697 for (unsigned i
= 0; i
< 4; i
++)
698 if ((wrmask
>> i
) & 1)
699 fprintf(fp
, "%c", "xyzw"[i
]);
700 } else if (idx
== NIR_INTRINSIC_REDUCTION_OP
) {
701 nir_op reduction_op
= nir_intrinsic_reduction_op(instr
);
702 fprintf(fp
, " reduction_op=%s", nir_op_infos
[reduction_op
].name
);
704 unsigned off
= info
->index_map
[idx
] - 1;
705 assert(index_name
[idx
]); /* forgot to update index_name table? */
706 fprintf(fp
, " %s=%d", index_name
[idx
], instr
->const_index
[off
]);
714 struct exec_list
*var_list
= NULL
;
716 switch (instr
->intrinsic
) {
717 case nir_intrinsic_load_uniform
:
718 var_list
= &state
->shader
->uniforms
;
720 case nir_intrinsic_load_input
:
721 case nir_intrinsic_load_per_vertex_input
:
722 var_list
= &state
->shader
->inputs
;
724 case nir_intrinsic_load_output
:
725 case nir_intrinsic_store_output
:
726 case nir_intrinsic_store_per_vertex_output
:
727 var_list
= &state
->shader
->outputs
;
733 nir_foreach_variable(var
, var_list
) {
734 if ((var
->data
.driver_location
== nir_intrinsic_base(instr
)) &&
735 (instr
->intrinsic
== nir_intrinsic_load_uniform
||
736 var
->data
.location_frac
== nir_intrinsic_component(instr
)) &&
738 fprintf(fp
, "\t/* %s */", var
->name
);
745 print_tex_instr(nir_tex_instr
*instr
, print_state
*state
)
747 FILE *fp
= state
->fp
;
749 print_dest(&instr
->dest
, state
);
769 case nir_texop_txf_ms
:
770 fprintf(fp
, "txf_ms ");
772 case nir_texop_txf_ms_mcs
:
773 fprintf(fp
, "txf_ms_mcs ");
784 case nir_texop_query_levels
:
785 fprintf(fp
, "query_levels ");
787 case nir_texop_texture_samples
:
788 fprintf(fp
, "texture_samples ");
790 case nir_texop_samples_identical
:
791 fprintf(fp
, "samples_identical ");
794 unreachable("Invalid texture operation");
798 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++) {
799 print_src(&instr
->src
[i
].src
, state
);
803 switch(instr
->src
[i
].src_type
) {
804 case nir_tex_src_coord
:
805 fprintf(fp
, "(coord)");
807 case nir_tex_src_projector
:
808 fprintf(fp
, "(projector)");
810 case nir_tex_src_comparator
:
811 fprintf(fp
, "(comparator)");
813 case nir_tex_src_offset
:
814 fprintf(fp
, "(offset)");
816 case nir_tex_src_bias
:
817 fprintf(fp
, "(bias)");
819 case nir_tex_src_lod
:
820 fprintf(fp
, "(lod)");
822 case nir_tex_src_ms_index
:
823 fprintf(fp
, "(ms_index)");
825 case nir_tex_src_ms_mcs
:
826 fprintf(fp
, "(ms_mcs)");
828 case nir_tex_src_ddx
:
829 fprintf(fp
, "(ddx)");
831 case nir_tex_src_ddy
:
832 fprintf(fp
, "(ddy)");
834 case nir_tex_src_texture_deref
:
835 fprintf(fp
, "(texture_deref)");
837 case nir_tex_src_sampler_deref
:
838 fprintf(fp
, "(sampler_deref)");
840 case nir_tex_src_texture_offset
:
841 fprintf(fp
, "(texture_offset)");
843 case nir_tex_src_sampler_offset
:
844 fprintf(fp
, "(sampler_offset)");
846 case nir_tex_src_plane
:
847 fprintf(fp
, "(plane)");
851 unreachable("Invalid texture source type");
858 if (instr
->op
== nir_texop_tg4
) {
859 fprintf(fp
, "%u (gather_component), ", instr
->component
);
864 print_call_instr(nir_call_instr
*instr
, print_state
*state
)
866 FILE *fp
= state
->fp
;
868 fprintf(fp
, "call %s ", instr
->callee
->name
);
870 for (unsigned i
= 0; i
< instr
->num_params
; i
++) {
874 print_src(&instr
->params
[i
], state
);
879 print_load_const_instr(nir_load_const_instr
*instr
, print_state
*state
)
881 FILE *fp
= state
->fp
;
883 print_ssa_def(&instr
->def
, state
);
885 fprintf(fp
, " = load_const (");
887 for (unsigned i
= 0; i
< instr
->def
.num_components
; i
++) {
892 * we don't really know the type of the constant (if it will be used as a
893 * float or an int), so just print the raw constant in hex for fidelity
894 * and then print the float in a comment for readability.
897 switch (instr
->def
.bit_size
) {
899 fprintf(fp
, "0x%16" PRIx64
" /* %f */", instr
->value
.u64
[i
],
900 instr
->value
.f64
[i
]);
903 fprintf(fp
, "0x%08x /* %f */", instr
->value
.u32
[i
], instr
->value
.f32
[i
]);
906 fprintf(fp
, "0x%04x /* %f */", instr
->value
.u16
[i
],
907 _mesa_half_to_float(instr
->value
.u16
[i
]));
910 fprintf(fp
, "0x%02x", instr
->value
.u8
[i
]);
919 print_jump_instr(nir_jump_instr
*instr
, print_state
*state
)
921 FILE *fp
= state
->fp
;
923 switch (instr
->type
) {
925 fprintf(fp
, "break");
928 case nir_jump_continue
:
929 fprintf(fp
, "continue");
932 case nir_jump_return
:
933 fprintf(fp
, "return");
939 print_ssa_undef_instr(nir_ssa_undef_instr
* instr
, print_state
*state
)
941 FILE *fp
= state
->fp
;
942 print_ssa_def(&instr
->def
, state
);
943 fprintf(fp
, " = undefined");
947 print_phi_instr(nir_phi_instr
*instr
, print_state
*state
)
949 FILE *fp
= state
->fp
;
950 print_dest(&instr
->dest
, state
);
951 fprintf(fp
, " = phi ");
952 nir_foreach_phi_src(src
, instr
) {
953 if (&src
->node
!= exec_list_get_head(&instr
->srcs
))
956 fprintf(fp
, "block_%u: ", src
->pred
->index
);
957 print_src(&src
->src
, state
);
962 print_parallel_copy_instr(nir_parallel_copy_instr
*instr
, print_state
*state
)
964 FILE *fp
= state
->fp
;
965 nir_foreach_parallel_copy_entry(entry
, instr
) {
966 if (&entry
->node
!= exec_list_get_head(&instr
->entries
))
969 print_dest(&entry
->dest
, state
);
971 print_src(&entry
->src
, state
);
976 print_instr(const nir_instr
*instr
, print_state
*state
, unsigned tabs
)
978 FILE *fp
= state
->fp
;
979 print_tabs(tabs
, fp
);
981 switch (instr
->type
) {
982 case nir_instr_type_alu
:
983 print_alu_instr(nir_instr_as_alu(instr
), state
);
986 case nir_instr_type_deref
:
987 print_deref_instr(nir_instr_as_deref(instr
), state
);
990 case nir_instr_type_call
:
991 print_call_instr(nir_instr_as_call(instr
), state
);
994 case nir_instr_type_intrinsic
:
995 print_intrinsic_instr(nir_instr_as_intrinsic(instr
), state
);
998 case nir_instr_type_tex
:
999 print_tex_instr(nir_instr_as_tex(instr
), state
);
1002 case nir_instr_type_load_const
:
1003 print_load_const_instr(nir_instr_as_load_const(instr
), state
);
1006 case nir_instr_type_jump
:
1007 print_jump_instr(nir_instr_as_jump(instr
), state
);
1010 case nir_instr_type_ssa_undef
:
1011 print_ssa_undef_instr(nir_instr_as_ssa_undef(instr
), state
);
1014 case nir_instr_type_phi
:
1015 print_phi_instr(nir_instr_as_phi(instr
), state
);
1018 case nir_instr_type_parallel_copy
:
1019 print_parallel_copy_instr(nir_instr_as_parallel_copy(instr
), state
);
1023 unreachable("Invalid instruction type");
1029 compare_block_index(const void *p1
, const void *p2
)
1031 const nir_block
*block1
= *((const nir_block
**) p1
);
1032 const nir_block
*block2
= *((const nir_block
**) p2
);
1034 return (int) block1
->index
- (int) block2
->index
;
1037 static void print_cf_node(nir_cf_node
*node
, print_state
*state
,
1041 print_block(nir_block
*block
, print_state
*state
, unsigned tabs
)
1043 FILE *fp
= state
->fp
;
1045 print_tabs(tabs
, fp
);
1046 fprintf(fp
, "block block_%u:\n", block
->index
);
1048 /* sort the predecessors by index so we consistently print the same thing */
1051 malloc(block
->predecessors
->entries
* sizeof(nir_block
*));
1053 struct set_entry
*entry
;
1055 set_foreach(block
->predecessors
, entry
) {
1056 preds
[i
++] = (nir_block
*) entry
->key
;
1059 qsort(preds
, block
->predecessors
->entries
, sizeof(nir_block
*),
1060 compare_block_index
);
1062 print_tabs(tabs
, fp
);
1063 fprintf(fp
, "/* preds: ");
1064 for (unsigned i
= 0; i
< block
->predecessors
->entries
; i
++) {
1065 fprintf(fp
, "block_%u ", preds
[i
]->index
);
1067 fprintf(fp
, "*/\n");
1071 nir_foreach_instr(instr
, block
) {
1072 print_instr(instr
, state
, tabs
);
1074 print_annotation(state
, instr
);
1077 print_tabs(tabs
, fp
);
1078 fprintf(fp
, "/* succs: ");
1079 for (unsigned i
= 0; i
< 2; i
++)
1080 if (block
->successors
[i
]) {
1081 fprintf(fp
, "block_%u ", block
->successors
[i
]->index
);
1083 fprintf(fp
, "*/\n");
1087 print_if(nir_if
*if_stmt
, print_state
*state
, unsigned tabs
)
1089 FILE *fp
= state
->fp
;
1091 print_tabs(tabs
, fp
);
1093 print_src(&if_stmt
->condition
, state
);
1094 fprintf(fp
, " {\n");
1095 foreach_list_typed(nir_cf_node
, node
, node
, &if_stmt
->then_list
) {
1096 print_cf_node(node
, state
, tabs
+ 1);
1098 print_tabs(tabs
, fp
);
1099 fprintf(fp
, "} else {\n");
1100 foreach_list_typed(nir_cf_node
, node
, node
, &if_stmt
->else_list
) {
1101 print_cf_node(node
, state
, tabs
+ 1);
1103 print_tabs(tabs
, fp
);
1108 print_loop(nir_loop
*loop
, print_state
*state
, unsigned tabs
)
1110 FILE *fp
= state
->fp
;
1112 print_tabs(tabs
, fp
);
1113 fprintf(fp
, "loop {\n");
1114 foreach_list_typed(nir_cf_node
, node
, node
, &loop
->body
) {
1115 print_cf_node(node
, state
, tabs
+ 1);
1117 print_tabs(tabs
, fp
);
1122 print_cf_node(nir_cf_node
*node
, print_state
*state
, unsigned int tabs
)
1124 switch (node
->type
) {
1125 case nir_cf_node_block
:
1126 print_block(nir_cf_node_as_block(node
), state
, tabs
);
1129 case nir_cf_node_if
:
1130 print_if(nir_cf_node_as_if(node
), state
, tabs
);
1133 case nir_cf_node_loop
:
1134 print_loop(nir_cf_node_as_loop(node
), state
, tabs
);
1138 unreachable("Invalid CFG node type");
1143 print_function_impl(nir_function_impl
*impl
, print_state
*state
)
1145 FILE *fp
= state
->fp
;
1147 fprintf(fp
, "\nimpl %s ", impl
->function
->name
);
1151 nir_foreach_variable(var
, &impl
->locals
) {
1153 print_var_decl(var
, state
);
1156 foreach_list_typed(nir_register
, reg
, node
, &impl
->registers
) {
1158 print_register_decl(reg
, state
);
1161 nir_index_blocks(impl
);
1163 foreach_list_typed(nir_cf_node
, node
, node
, &impl
->body
) {
1164 print_cf_node(node
, state
, 1);
1167 fprintf(fp
, "\tblock block_%u:\n}\n\n", impl
->end_block
->index
);
1171 print_function(nir_function
*function
, print_state
*state
)
1173 FILE *fp
= state
->fp
;
1175 fprintf(fp
, "decl_function %s (%d params)", function
->name
,
1176 function
->num_params
);
1180 if (function
->impl
!= NULL
) {
1181 print_function_impl(function
->impl
, state
);
1187 init_print_state(print_state
*state
, nir_shader
*shader
, FILE *fp
)
1190 state
->shader
= shader
;
1191 state
->ht
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
1192 _mesa_key_pointer_equal
);
1193 state
->syms
= _mesa_set_create(NULL
, _mesa_key_hash_string
,
1194 _mesa_key_string_equal
);
1199 destroy_print_state(print_state
*state
)
1201 _mesa_hash_table_destroy(state
->ht
, NULL
);
1202 _mesa_set_destroy(state
->syms
, NULL
);
1206 nir_print_shader_annotated(nir_shader
*shader
, FILE *fp
,
1207 struct hash_table
*annotations
)
1210 init_print_state(&state
, shader
, fp
);
1212 state
.annotations
= annotations
;
1214 fprintf(fp
, "shader: %s\n", gl_shader_stage_name(shader
->info
.stage
));
1216 if (shader
->info
.name
)
1217 fprintf(fp
, "name: %s\n", shader
->info
.name
);
1219 if (shader
->info
.label
)
1220 fprintf(fp
, "label: %s\n", shader
->info
.label
);
1222 switch (shader
->info
.stage
) {
1223 case MESA_SHADER_COMPUTE
:
1224 fprintf(fp
, "local-size: %u, %u, %u%s\n",
1225 shader
->info
.cs
.local_size
[0],
1226 shader
->info
.cs
.local_size
[1],
1227 shader
->info
.cs
.local_size
[2],
1228 shader
->info
.cs
.local_size_variable
? " (variable)" : "");
1229 fprintf(fp
, "shared-size: %u\n", shader
->info
.cs
.shared_size
);
1235 fprintf(fp
, "inputs: %u\n", shader
->num_inputs
);
1236 fprintf(fp
, "outputs: %u\n", shader
->num_outputs
);
1237 fprintf(fp
, "uniforms: %u\n", shader
->num_uniforms
);
1238 fprintf(fp
, "shared: %u\n", shader
->num_shared
);
1240 nir_foreach_variable(var
, &shader
->uniforms
) {
1241 print_var_decl(var
, &state
);
1244 nir_foreach_variable(var
, &shader
->inputs
) {
1245 print_var_decl(var
, &state
);
1248 nir_foreach_variable(var
, &shader
->outputs
) {
1249 print_var_decl(var
, &state
);
1252 nir_foreach_variable(var
, &shader
->shared
) {
1253 print_var_decl(var
, &state
);
1256 nir_foreach_variable(var
, &shader
->globals
) {
1257 print_var_decl(var
, &state
);
1260 nir_foreach_variable(var
, &shader
->system_values
) {
1261 print_var_decl(var
, &state
);
1264 foreach_list_typed(nir_register
, reg
, node
, &shader
->registers
) {
1265 print_register_decl(reg
, &state
);
1268 foreach_list_typed(nir_function
, func
, node
, &shader
->functions
) {
1269 print_function(func
, &state
);
1272 destroy_print_state(&state
);
1276 nir_print_shader(nir_shader
*shader
, FILE *fp
)
1278 nir_print_shader_annotated(shader
, fp
, NULL
);
1282 nir_print_instr(const nir_instr
*instr
, FILE *fp
)
1284 print_state state
= {
1287 print_instr(instr
, &state
, 0);