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"
31 #include "vulkan/vulkan_core.h"
34 #include <inttypes.h> /* for PRIx64 macro */
37 print_tabs(unsigned num_tabs
, FILE *fp
)
39 for (unsigned i
= 0; i
< num_tabs
; i
++)
46 /** map from nir_variable -> printable name */
47 struct hash_table
*ht
;
49 /** set of names used so far for nir_variables */
52 /* an index used to make new non-conflicting names */
56 * Optional table of annotations mapping nir object
57 * (such as instr or var) to message to print.
59 struct hash_table
*annotations
;
63 print_annotation(print_state
*state
, void *obj
)
65 if (!state
->annotations
)
68 struct hash_entry
*entry
= _mesa_hash_table_search(state
->annotations
, obj
);
72 const char *note
= entry
->data
;
73 _mesa_hash_table_remove(state
->annotations
, entry
);
75 fprintf(stderr
, "%s\n\n", note
);
79 print_register(nir_register
*reg
, print_state
*state
)
82 if (reg
->name
!= NULL
)
83 fprintf(fp
, "/* %s */ ", reg
->name
);
84 fprintf(fp
, "r%u", reg
->index
);
87 static const char *sizes
[] = { "error", "vec1", "vec2", "vec3", "vec4",
88 "error", "error", "error", "vec8",
89 "error", "error", "error", "error",
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
);
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(const nir_src
*src
, print_state
*state
);
125 print_reg_src(const 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(const 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 nir_component_mask_t used_channels
= 0;
187 for (unsigned i
= 0; i
< NIR_MAX_VEC_COMPONENTS
; 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
= nir_src_num_components(instr
->src
[src
].src
);
201 if (print_swizzle
|| used_channels
!= live_channels
) {
203 for (unsigned i
= 0; i
< NIR_MAX_VEC_COMPONENTS
; i
++) {
204 if (!nir_alu_instr_channel_used(instr
, src
, i
))
207 fprintf(fp
, "%c", "xyzw"[instr
->src
[src
].swizzle
[i
]]);
211 if (instr
->src
[src
].abs
)
216 print_alu_dest(nir_alu_dest
*dest
, print_state
*state
)
218 FILE *fp
= state
->fp
;
219 /* we're going to print the saturate modifier later, after the opcode */
221 print_dest(&dest
->dest
, state
);
223 if (!dest
->dest
.is_ssa
&&
224 dest
->write_mask
!= (1 << dest
->dest
.reg
.reg
->num_components
) - 1) {
226 for (unsigned i
= 0; i
< NIR_MAX_VEC_COMPONENTS
; i
++)
227 if ((dest
->write_mask
>> i
) & 1)
228 fprintf(fp
, "%c", "xyzw"[i
]);
233 print_alu_instr(nir_alu_instr
*instr
, print_state
*state
)
235 FILE *fp
= state
->fp
;
237 print_alu_dest(&instr
->dest
, state
);
239 fprintf(fp
, " = %s", nir_op_infos
[instr
->op
].name
);
242 if (instr
->dest
.saturate
)
244 if (instr
->no_signed_wrap
)
246 if (instr
->no_unsigned_wrap
)
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
)) {
302 /* Only float base types can be matrices. */
305 for (i
= 0; i
< rows
; i
++) {
306 if (i
> 0) fprintf(fp
, ", ");
307 fprintf(fp
, "%s", c
->values
[i
].b
? "true" : "false");
311 case GLSL_TYPE_UINT8
:
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%02x", c
->values
[i
].u8
);
322 case GLSL_TYPE_UINT16
:
323 case GLSL_TYPE_INT16
:
324 /* Only float base types can be matrices. */
327 for (i
= 0; i
< rows
; i
++) {
328 if (i
> 0) fprintf(fp
, ", ");
329 fprintf(fp
, "0x%04x", c
->values
[i
].u16
);
335 /* Only float base types can be matrices. */
338 for (i
= 0; i
< rows
; i
++) {
339 if (i
> 0) fprintf(fp
, ", ");
340 fprintf(fp
, "0x%08x", c
->values
[i
].u32
);
344 case GLSL_TYPE_FLOAT16
:
345 case GLSL_TYPE_FLOAT
:
346 case GLSL_TYPE_DOUBLE
:
348 for (i
= 0; i
< cols
; i
++) {
349 if (i
> 0) fprintf(fp
, ", ");
350 print_constant(c
->elements
[i
], glsl_get_column_type(type
), state
);
353 switch (glsl_get_base_type(type
)) {
354 case GLSL_TYPE_FLOAT16
:
355 for (i
= 0; i
< rows
; i
++) {
356 if (i
> 0) fprintf(fp
, ", ");
357 fprintf(fp
, "%f", _mesa_half_to_float(c
->values
[i
].u16
));
361 case GLSL_TYPE_FLOAT
:
362 for (i
= 0; i
< rows
; i
++) {
363 if (i
> 0) fprintf(fp
, ", ");
364 fprintf(fp
, "%f", c
->values
[i
].f32
);
368 case GLSL_TYPE_DOUBLE
:
369 for (i
= 0; i
< rows
; i
++) {
370 if (i
> 0) fprintf(fp
, ", ");
371 fprintf(fp
, "%f", c
->values
[i
].f64
);
376 unreachable("Cannot get here from the first level switch");
381 case GLSL_TYPE_UINT64
:
382 case GLSL_TYPE_INT64
:
383 /* Only float base types can be matrices. */
386 for (i
= 0; i
< cols
; i
++) {
387 if (i
> 0) fprintf(fp
, ", ");
388 fprintf(fp
, "0x%08" PRIx64
, c
->values
[i
].u64
);
392 case GLSL_TYPE_STRUCT
:
393 for (i
= 0; i
< c
->num_elements
; i
++) {
394 if (i
> 0) fprintf(fp
, ", ");
396 print_constant(c
->elements
[i
], glsl_get_struct_field(type
, i
), state
);
401 case GLSL_TYPE_ARRAY
:
402 for (i
= 0; i
< c
->num_elements
; i
++) {
403 if (i
> 0) fprintf(fp
, ", ");
405 print_constant(c
->elements
[i
], glsl_get_array_element(type
), state
);
411 unreachable("not reached");
416 get_variable_mode_str(nir_variable_mode mode
, bool want_local_global_mode
)
419 case nir_var_shader_in
:
421 case nir_var_shader_out
:
423 case nir_var_uniform
:
425 case nir_var_mem_ubo
:
427 case nir_var_system_value
:
429 case nir_var_mem_ssbo
:
431 case nir_var_mem_shared
:
433 case nir_var_mem_global
:
435 case nir_var_shader_temp
:
436 return want_local_global_mode
? "shader_temp" : "";
437 case nir_var_function_temp
:
438 return want_local_global_mode
? "function_temp" : "";
445 print_var_decl(nir_variable
*var
, print_state
*state
)
447 FILE *fp
= state
->fp
;
449 fprintf(fp
, "decl_var ");
451 const char *const cent
= (var
->data
.centroid
) ? "centroid " : "";
452 const char *const samp
= (var
->data
.sample
) ? "sample " : "";
453 const char *const patch
= (var
->data
.patch
) ? "patch " : "";
454 const char *const inv
= (var
->data
.invariant
) ? "invariant " : "";
455 fprintf(fp
, "%s%s%s%s%s %s ",
456 cent
, samp
, patch
, inv
, get_variable_mode_str(var
->data
.mode
, false),
457 glsl_interp_mode_name(var
->data
.interpolation
));
459 enum gl_access_qualifier access
= var
->data
.image
.access
;
460 const char *const coher
= (access
& ACCESS_COHERENT
) ? "coherent " : "";
461 const char *const volat
= (access
& ACCESS_VOLATILE
) ? "volatile " : "";
462 const char *const restr
= (access
& ACCESS_RESTRICT
) ? "restrict " : "";
463 const char *const ronly
= (access
& ACCESS_NON_WRITEABLE
) ? "readonly " : "";
464 const char *const wonly
= (access
& ACCESS_NON_READABLE
) ? "writeonly " : "";
465 const char *const reorder
= (access
& ACCESS_CAN_REORDER
) ? "reorderable " : "";
466 fprintf(fp
, "%s%s%s%s%s%s", coher
, volat
, restr
, ronly
, wonly
, reorder
);
468 #define FORMAT_CASE(x) case x: fprintf(stderr, #x " "); break
469 switch (var
->data
.image
.format
) {
470 FORMAT_CASE(GL_RGBA32F
);
471 FORMAT_CASE(GL_RGBA32UI
);
472 FORMAT_CASE(GL_RGBA32I
);
473 FORMAT_CASE(GL_R32F
);
474 FORMAT_CASE(GL_R32UI
);
475 FORMAT_CASE(GL_R32I
);
476 FORMAT_CASE(GL_RG32F
);
477 FORMAT_CASE(GL_RG32UI
);
478 FORMAT_CASE(GL_RG32I
);
481 FORMAT_CASE(GL_RGBA8
);
482 FORMAT_CASE(GL_R8_SNORM
);
483 FORMAT_CASE(GL_RG8_SNORM
);
484 FORMAT_CASE(GL_RGBA8_SNORM
);
486 FORMAT_CASE(GL_RG16
);
487 FORMAT_CASE(GL_RGBA16
);
488 FORMAT_CASE(GL_R16_SNORM
);
489 FORMAT_CASE(GL_RG16_SNORM
);
490 FORMAT_CASE(GL_RGBA16_SNORM
);
491 FORMAT_CASE(GL_R16F
);
492 FORMAT_CASE(GL_RG16F
);
493 FORMAT_CASE(GL_RGBA16F
);
494 FORMAT_CASE(GL_R8UI
);
496 FORMAT_CASE(GL_RG8UI
);
497 FORMAT_CASE(GL_RG8I
);
498 FORMAT_CASE(GL_RGBA8UI
);
499 FORMAT_CASE(GL_RGBA8I
);
500 FORMAT_CASE(GL_R16UI
);
501 FORMAT_CASE(GL_R16I
);
502 FORMAT_CASE(GL_RG16UI
);
503 FORMAT_CASE(GL_RG16I
);
504 FORMAT_CASE(GL_RGBA16UI
);
505 FORMAT_CASE(GL_RGBA16I
);
506 FORMAT_CASE(GL_R11F_G11F_B10F
);
507 FORMAT_CASE(GL_RGB9_E5
);
508 FORMAT_CASE(GL_RGB10_A2
);
509 FORMAT_CASE(GL_RGB10_A2UI
);
510 default: /* Including the normal GL_NONE */
515 fprintf(fp
, "%s %s", glsl_get_type_name(var
->type
),
516 get_var_name(var
, state
));
518 if (var
->data
.mode
== nir_var_shader_in
||
519 var
->data
.mode
== nir_var_shader_out
||
520 var
->data
.mode
== nir_var_uniform
||
521 var
->data
.mode
== nir_var_mem_ubo
||
522 var
->data
.mode
== nir_var_mem_ssbo
) {
523 const char *loc
= NULL
;
526 switch (state
->shader
->info
.stage
) {
527 case MESA_SHADER_VERTEX
:
528 if (var
->data
.mode
== nir_var_shader_in
)
529 loc
= gl_vert_attrib_name(var
->data
.location
);
530 else if (var
->data
.mode
== nir_var_shader_out
)
531 loc
= gl_varying_slot_name(var
->data
.location
);
533 case MESA_SHADER_GEOMETRY
:
534 if ((var
->data
.mode
== nir_var_shader_in
) ||
535 (var
->data
.mode
== nir_var_shader_out
))
536 loc
= gl_varying_slot_name(var
->data
.location
);
538 case MESA_SHADER_FRAGMENT
:
539 if (var
->data
.mode
== nir_var_shader_in
)
540 loc
= gl_varying_slot_name(var
->data
.location
);
541 else if (var
->data
.mode
== nir_var_shader_out
)
542 loc
= gl_frag_result_name(var
->data
.location
);
544 case MESA_SHADER_TESS_CTRL
:
545 case MESA_SHADER_TESS_EVAL
:
546 case MESA_SHADER_COMPUTE
:
547 case MESA_SHADER_KERNEL
:
554 snprintf(buf
, sizeof(buf
), "%u", var
->data
.location
);
558 /* For shader I/O vars that have been split to components or packed,
559 * print the fractional location within the input/output.
561 unsigned int num_components
=
562 glsl_get_components(glsl_without_array(var
->type
));
563 const char *components
= NULL
;
564 char components_local
[6] = {'.' /* the rest is 0-filled */};
565 switch (var
->data
.mode
) {
566 case nir_var_shader_in
:
567 case nir_var_shader_out
:
568 if (num_components
< 4 && num_components
!= 0) {
569 const char *xyzw
= "xyzw";
570 for (int i
= 0; i
< num_components
; i
++)
571 components_local
[i
+ 1] = xyzw
[i
+ var
->data
.location_frac
];
573 components
= components_local
;
580 fprintf(fp
, " (%s%s, %u, %u)%s", loc
,
581 components
? components
: "",
582 var
->data
.driver_location
, var
->data
.binding
,
583 var
->data
.compact
? " compact" : "");
586 if (var
->constant_initializer
) {
587 fprintf(fp
, " = { ");
588 print_constant(var
->constant_initializer
, var
->type
, state
);
593 print_annotation(state
, var
);
597 print_deref_link(const nir_deref_instr
*instr
, bool whole_chain
, print_state
*state
)
599 FILE *fp
= state
->fp
;
601 if (instr
->deref_type
== nir_deref_type_var
) {
602 fprintf(fp
, "%s", get_var_name(instr
->var
, state
));
604 } else if (instr
->deref_type
== nir_deref_type_cast
) {
605 fprintf(fp
, "(%s *)", glsl_get_type_name(instr
->type
));
606 print_src(&instr
->parent
, state
);
610 assert(instr
->parent
.is_ssa
);
611 nir_deref_instr
*parent
=
612 nir_instr_as_deref(instr
->parent
.ssa
->parent_instr
);
614 /* Is the parent we're going to print a bare cast? */
615 const bool is_parent_cast
=
616 whole_chain
&& parent
->deref_type
== nir_deref_type_cast
;
618 /* If we're not printing the whole chain, the parent we print will be a SSA
619 * value that represents a pointer. The only deref type that naturally
620 * gives a pointer is a cast.
622 const bool is_parent_pointer
=
623 !whole_chain
|| parent
->deref_type
== nir_deref_type_cast
;
625 /* Struct derefs have a nice syntax that works on pointers, arrays derefs
628 const bool need_deref
=
629 is_parent_pointer
&& instr
->deref_type
!= nir_deref_type_struct
;
631 /* Cast need extra parens and so * dereferences */
632 if (is_parent_cast
|| need_deref
)
639 print_deref_link(parent
, whole_chain
, state
);
641 print_src(&instr
->parent
, state
);
644 if (is_parent_cast
|| need_deref
)
647 switch (instr
->deref_type
) {
648 case nir_deref_type_struct
:
649 fprintf(fp
, "%s%s", is_parent_pointer
? "->" : ".",
650 glsl_get_struct_elem_name(parent
->type
, instr
->strct
.index
));
653 case nir_deref_type_array
:
654 case nir_deref_type_ptr_as_array
: {
655 if (nir_src_is_const(instr
->arr
.index
)) {
656 fprintf(fp
, "[%"PRId64
"]", nir_src_as_int(instr
->arr
.index
));
659 print_src(&instr
->arr
.index
, state
);
665 case nir_deref_type_array_wildcard
:
670 unreachable("Invalid deref instruction type");
675 print_deref_instr(nir_deref_instr
*instr
, print_state
*state
)
677 FILE *fp
= state
->fp
;
679 print_dest(&instr
->dest
, state
);
681 switch (instr
->deref_type
) {
682 case nir_deref_type_var
:
683 fprintf(fp
, " = deref_var ");
685 case nir_deref_type_array
:
686 case nir_deref_type_array_wildcard
:
687 fprintf(fp
, " = deref_array ");
689 case nir_deref_type_struct
:
690 fprintf(fp
, " = deref_struct ");
692 case nir_deref_type_cast
:
693 fprintf(fp
, " = deref_cast ");
695 case nir_deref_type_ptr_as_array
:
696 fprintf(fp
, " = deref_ptr_as_array ");
699 unreachable("Invalid deref instruction type");
702 /* Only casts naturally return a pointer type */
703 if (instr
->deref_type
!= nir_deref_type_cast
)
706 print_deref_link(instr
, false, state
);
708 fprintf(fp
, " (%s %s) ",
709 get_variable_mode_str(instr
->mode
, true),
710 glsl_get_type_name(instr
->type
));
712 if (instr
->deref_type
!= nir_deref_type_var
&&
713 instr
->deref_type
!= nir_deref_type_cast
) {
714 /* Print the entire chain as a comment */
716 print_deref_link(instr
, true, state
);
720 if (instr
->deref_type
== nir_deref_type_cast
) {
721 fprintf(fp
, " /* ptr_stride=%u */", instr
->cast
.ptr_stride
);
726 vulkan_descriptor_type_name(VkDescriptorType type
)
729 case VK_DESCRIPTOR_TYPE_SAMPLER
: return "sampler";
730 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
: return "texture+sampler";
731 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE
: return "texture";
732 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE
: return "image";
733 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER
: return "texture-buffer";
734 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER
: return "image-buffer";
735 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER
: return "UBO";
736 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER
: return "SSBO";
737 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
: return "UBO";
738 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
: return "SSBO";
739 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT
: return "input-att";
740 case VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT
: return "inline-UBO";
741 default: return "unknown";
746 print_intrinsic_instr(nir_intrinsic_instr
*instr
, print_state
*state
)
748 const nir_intrinsic_info
*info
= &nir_intrinsic_infos
[instr
->intrinsic
];
749 unsigned num_srcs
= info
->num_srcs
;
750 FILE *fp
= state
->fp
;
752 if (info
->has_dest
) {
753 print_dest(&instr
->dest
, state
);
757 fprintf(fp
, "intrinsic %s (", info
->name
);
759 for (unsigned i
= 0; i
< num_srcs
; i
++) {
763 print_src(&instr
->src
[i
], state
);
768 for (unsigned i
= 0; i
< info
->num_indices
; i
++) {
772 fprintf(fp
, "%d", instr
->const_index
[i
]);
777 static const char *index_name
[NIR_INTRINSIC_NUM_INDEX_FLAGS
] = {
778 [NIR_INTRINSIC_BASE
] = "base",
779 [NIR_INTRINSIC_WRMASK
] = "wrmask",
780 [NIR_INTRINSIC_STREAM_ID
] = "stream-id",
781 [NIR_INTRINSIC_UCP_ID
] = "ucp-id",
782 [NIR_INTRINSIC_RANGE
] = "range",
783 [NIR_INTRINSIC_DESC_SET
] = "desc-set",
784 [NIR_INTRINSIC_BINDING
] = "binding",
785 [NIR_INTRINSIC_COMPONENT
] = "component",
786 [NIR_INTRINSIC_INTERP_MODE
] = "interp_mode",
787 [NIR_INTRINSIC_REDUCTION_OP
] = "reduction_op",
788 [NIR_INTRINSIC_CLUSTER_SIZE
] = "cluster_size",
789 [NIR_INTRINSIC_PARAM_IDX
] = "param_idx",
790 [NIR_INTRINSIC_IMAGE_DIM
] = "image_dim",
791 [NIR_INTRINSIC_IMAGE_ARRAY
] = "image_array",
792 [NIR_INTRINSIC_ACCESS
] = "access",
793 [NIR_INTRINSIC_SRC_ACCESS
] = "src-access",
794 [NIR_INTRINSIC_DST_ACCESS
] = "dst-access",
795 [NIR_INTRINSIC_FORMAT
] = "format",
796 [NIR_INTRINSIC_ALIGN_MUL
] = "align_mul",
797 [NIR_INTRINSIC_ALIGN_OFFSET
] = "align_offset",
798 [NIR_INTRINSIC_DESC_TYPE
] = "desc_type",
799 [NIR_INTRINSIC_TYPE
] = "type",
800 [NIR_INTRINSIC_SWIZZLE_MASK
] = "swizzle_mask",
802 for (unsigned idx
= 1; idx
< NIR_INTRINSIC_NUM_INDEX_FLAGS
; idx
++) {
803 if (!info
->index_map
[idx
])
807 case NIR_INTRINSIC_WRMASK
: {
808 /* special case wrmask to show it as a writemask.. */
809 unsigned wrmask
= nir_intrinsic_write_mask(instr
);
810 fprintf(fp
, " wrmask=");
811 for (unsigned i
= 0; i
< 4; i
++)
812 if ((wrmask
>> i
) & 1)
813 fprintf(fp
, "%c", "xyzw"[i
]);
817 case NIR_INTRINSIC_REDUCTION_OP
: {
818 nir_op reduction_op
= nir_intrinsic_reduction_op(instr
);
819 fprintf(fp
, " reduction_op=%s", nir_op_infos
[reduction_op
].name
);
823 case NIR_INTRINSIC_IMAGE_DIM
: {
824 static const char *dim_name
[] = {
825 [GLSL_SAMPLER_DIM_1D
] = "1D",
826 [GLSL_SAMPLER_DIM_2D
] = "2D",
827 [GLSL_SAMPLER_DIM_3D
] = "3D",
828 [GLSL_SAMPLER_DIM_CUBE
] = "Cube",
829 [GLSL_SAMPLER_DIM_RECT
] = "Rect",
830 [GLSL_SAMPLER_DIM_BUF
] = "Buf",
831 [GLSL_SAMPLER_DIM_MS
] = "2D-MSAA",
832 [GLSL_SAMPLER_DIM_SUBPASS
] = "Subpass",
833 [GLSL_SAMPLER_DIM_SUBPASS_MS
] = "Subpass-MSAA",
835 enum glsl_sampler_dim dim
= nir_intrinsic_image_dim(instr
);
836 assert(dim
< ARRAY_SIZE(dim_name
) && dim_name
[dim
]);
837 fprintf(fp
, " image_dim=%s", dim_name
[dim
]);
841 case NIR_INTRINSIC_IMAGE_ARRAY
: {
842 bool array
= nir_intrinsic_image_array(instr
);
843 fprintf(fp
, " image_array=%s", array
? "true" : "false");
847 case NIR_INTRINSIC_DESC_TYPE
: {
848 VkDescriptorType desc_type
= nir_intrinsic_desc_type(instr
);
849 fprintf(fp
, " desc_type=%s", vulkan_descriptor_type_name(desc_type
));
853 case NIR_INTRINSIC_TYPE
: {
854 nir_alu_type type
= nir_intrinsic_type(instr
);
855 unsigned size
= nir_alu_type_get_type_size(type
);
857 switch (nir_alu_type_get_base_type(type
)) {
858 case nir_type_int
: name
= "int"; break;
859 case nir_type_uint
: name
= "uint"; break;
860 case nir_type_bool
: name
= "bool"; break;
861 case nir_type_float
: name
= "float"; break;
862 default: name
= "invalid";
865 fprintf(fp
, " type=%s%u", name
, size
);
867 fprintf(fp
, " type=%s", name
);
871 case NIR_INTRINSIC_SWIZZLE_MASK
: {
872 fprintf(fp
, " swizzle_mask=");
873 unsigned mask
= nir_intrinsic_swizzle_mask(instr
);
874 if (instr
->intrinsic
== nir_intrinsic_quad_swizzle_amd
) {
875 for (unsigned i
= 0; i
< 4; i
++)
876 fprintf(fp
, "%d", (mask
>> (i
* 2) & 3));
877 } else if (instr
->intrinsic
== nir_intrinsic_masked_swizzle_amd
) {
878 fprintf(fp
, "((id & %d) | %d) ^ %d", mask
& 0x1F,
880 (mask
>> 10) & 0x1F);
882 fprintf(fp
, "%d", mask
);
888 unsigned off
= info
->index_map
[idx
] - 1;
889 assert(index_name
[idx
]); /* forgot to update index_name table? */
890 fprintf(fp
, " %s=%d", index_name
[idx
], instr
->const_index
[off
]);
900 struct exec_list
*var_list
= NULL
;
902 switch (instr
->intrinsic
) {
903 case nir_intrinsic_load_uniform
:
904 var_list
= &state
->shader
->uniforms
;
906 case nir_intrinsic_load_input
:
907 case nir_intrinsic_load_interpolated_input
:
908 case nir_intrinsic_load_per_vertex_input
:
909 var_list
= &state
->shader
->inputs
;
911 case nir_intrinsic_load_output
:
912 case nir_intrinsic_store_output
:
913 case nir_intrinsic_store_per_vertex_output
:
914 var_list
= &state
->shader
->outputs
;
920 nir_foreach_variable(var
, var_list
) {
921 if ((var
->data
.driver_location
== nir_intrinsic_base(instr
)) &&
922 (instr
->intrinsic
== nir_intrinsic_load_uniform
||
923 (nir_intrinsic_component(instr
) >= var
->data
.location_frac
&&
924 nir_intrinsic_component(instr
) <
925 (var
->data
.location_frac
+ glsl_get_components(var
->type
)))) &&
927 fprintf(fp
, "\t/* %s */", var
->name
);
934 print_tex_instr(nir_tex_instr
*instr
, print_state
*state
)
936 FILE *fp
= state
->fp
;
938 print_dest(&instr
->dest
, state
);
958 case nir_texop_txf_ms
:
959 fprintf(fp
, "txf_ms ");
961 case nir_texop_txf_ms_fb
:
962 fprintf(fp
, "txf_ms_fb ");
964 case nir_texop_txf_ms_mcs
:
965 fprintf(fp
, "txf_ms_mcs ");
976 case nir_texop_query_levels
:
977 fprintf(fp
, "query_levels ");
979 case nir_texop_texture_samples
:
980 fprintf(fp
, "texture_samples ");
982 case nir_texop_samples_identical
:
983 fprintf(fp
, "samples_identical ");
986 unreachable("Invalid texture operation");
990 bool has_texture_deref
= false, has_sampler_deref
= false;
991 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++) {
996 print_src(&instr
->src
[i
].src
, state
);
999 switch(instr
->src
[i
].src_type
) {
1000 case nir_tex_src_coord
:
1001 fprintf(fp
, "(coord)");
1003 case nir_tex_src_projector
:
1004 fprintf(fp
, "(projector)");
1006 case nir_tex_src_comparator
:
1007 fprintf(fp
, "(comparator)");
1009 case nir_tex_src_offset
:
1010 fprintf(fp
, "(offset)");
1012 case nir_tex_src_bias
:
1013 fprintf(fp
, "(bias)");
1015 case nir_tex_src_lod
:
1016 fprintf(fp
, "(lod)");
1018 case nir_tex_src_min_lod
:
1019 fprintf(fp
, "(min_lod)");
1021 case nir_tex_src_ms_index
:
1022 fprintf(fp
, "(ms_index)");
1024 case nir_tex_src_ms_mcs
:
1025 fprintf(fp
, "(ms_mcs)");
1027 case nir_tex_src_ddx
:
1028 fprintf(fp
, "(ddx)");
1030 case nir_tex_src_ddy
:
1031 fprintf(fp
, "(ddy)");
1033 case nir_tex_src_texture_deref
:
1034 has_texture_deref
= true;
1035 fprintf(fp
, "(texture_deref)");
1037 case nir_tex_src_sampler_deref
:
1038 has_sampler_deref
= true;
1039 fprintf(fp
, "(sampler_deref)");
1041 case nir_tex_src_texture_offset
:
1042 fprintf(fp
, "(texture_offset)");
1044 case nir_tex_src_sampler_offset
:
1045 fprintf(fp
, "(sampler_offset)");
1047 case nir_tex_src_texture_handle
:
1048 fprintf(fp
, "(texture_handle)");
1050 case nir_tex_src_sampler_handle
:
1051 fprintf(fp
, "(sampler_handle)");
1053 case nir_tex_src_plane
:
1054 fprintf(fp
, "(plane)");
1058 unreachable("Invalid texture source type");
1063 if (instr
->op
== nir_texop_tg4
) {
1064 fprintf(fp
, ", %u (gather_component)", instr
->component
);
1067 if (nir_tex_instr_has_explicit_tg4_offsets(instr
)) {
1068 fprintf(fp
, ", { (%i, %i)", instr
->tg4_offsets
[0][0], instr
->tg4_offsets
[0][1]);
1069 for (unsigned i
= 1; i
< 4; ++i
)
1070 fprintf(fp
, ", (%i, %i)", instr
->tg4_offsets
[i
][0],
1071 instr
->tg4_offsets
[i
][1]);
1072 fprintf(fp
, " } (offsets)");
1075 if (instr
->op
!= nir_texop_txf_ms_fb
) {
1076 if (!has_texture_deref
) {
1077 fprintf(fp
, ", %u (texture)", instr
->texture_index
);
1080 if (!has_sampler_deref
) {
1081 fprintf(fp
, ", %u (sampler)", instr
->sampler_index
);
1087 print_call_instr(nir_call_instr
*instr
, print_state
*state
)
1089 FILE *fp
= state
->fp
;
1091 fprintf(fp
, "call %s ", instr
->callee
->name
);
1093 for (unsigned i
= 0; i
< instr
->num_params
; i
++) {
1097 print_src(&instr
->params
[i
], state
);
1102 print_load_const_instr(nir_load_const_instr
*instr
, print_state
*state
)
1104 FILE *fp
= state
->fp
;
1106 print_ssa_def(&instr
->def
, state
);
1108 fprintf(fp
, " = load_const (");
1110 for (unsigned i
= 0; i
< instr
->def
.num_components
; i
++) {
1115 * we don't really know the type of the constant (if it will be used as a
1116 * float or an int), so just print the raw constant in hex for fidelity
1117 * and then print the float in a comment for readability.
1120 switch (instr
->def
.bit_size
) {
1122 fprintf(fp
, "0x%16" PRIx64
" /* %f */", instr
->value
[i
].u64
,
1123 instr
->value
[i
].f64
);
1126 fprintf(fp
, "0x%08x /* %f */", instr
->value
[i
].u32
, instr
->value
[i
].f32
);
1129 fprintf(fp
, "0x%04x /* %f */", instr
->value
[i
].u16
,
1130 _mesa_half_to_float(instr
->value
[i
].u16
));
1133 fprintf(fp
, "0x%02x", instr
->value
[i
].u8
);
1136 fprintf(fp
, "%s", instr
->value
[i
].b
? "true" : "false");
1145 print_jump_instr(nir_jump_instr
*instr
, print_state
*state
)
1147 FILE *fp
= state
->fp
;
1149 switch (instr
->type
) {
1150 case nir_jump_break
:
1151 fprintf(fp
, "break");
1154 case nir_jump_continue
:
1155 fprintf(fp
, "continue");
1158 case nir_jump_return
:
1159 fprintf(fp
, "return");
1165 print_ssa_undef_instr(nir_ssa_undef_instr
* instr
, print_state
*state
)
1167 FILE *fp
= state
->fp
;
1168 print_ssa_def(&instr
->def
, state
);
1169 fprintf(fp
, " = undefined");
1173 print_phi_instr(nir_phi_instr
*instr
, print_state
*state
)
1175 FILE *fp
= state
->fp
;
1176 print_dest(&instr
->dest
, state
);
1177 fprintf(fp
, " = phi ");
1178 nir_foreach_phi_src(src
, instr
) {
1179 if (&src
->node
!= exec_list_get_head(&instr
->srcs
))
1182 fprintf(fp
, "block_%u: ", src
->pred
->index
);
1183 print_src(&src
->src
, state
);
1188 print_parallel_copy_instr(nir_parallel_copy_instr
*instr
, print_state
*state
)
1190 FILE *fp
= state
->fp
;
1191 nir_foreach_parallel_copy_entry(entry
, instr
) {
1192 if (&entry
->node
!= exec_list_get_head(&instr
->entries
))
1195 print_dest(&entry
->dest
, state
);
1197 print_src(&entry
->src
, state
);
1202 print_instr(const nir_instr
*instr
, print_state
*state
, unsigned tabs
)
1204 FILE *fp
= state
->fp
;
1205 print_tabs(tabs
, fp
);
1207 switch (instr
->type
) {
1208 case nir_instr_type_alu
:
1209 print_alu_instr(nir_instr_as_alu(instr
), state
);
1212 case nir_instr_type_deref
:
1213 print_deref_instr(nir_instr_as_deref(instr
), state
);
1216 case nir_instr_type_call
:
1217 print_call_instr(nir_instr_as_call(instr
), state
);
1220 case nir_instr_type_intrinsic
:
1221 print_intrinsic_instr(nir_instr_as_intrinsic(instr
), state
);
1224 case nir_instr_type_tex
:
1225 print_tex_instr(nir_instr_as_tex(instr
), state
);
1228 case nir_instr_type_load_const
:
1229 print_load_const_instr(nir_instr_as_load_const(instr
), state
);
1232 case nir_instr_type_jump
:
1233 print_jump_instr(nir_instr_as_jump(instr
), state
);
1236 case nir_instr_type_ssa_undef
:
1237 print_ssa_undef_instr(nir_instr_as_ssa_undef(instr
), state
);
1240 case nir_instr_type_phi
:
1241 print_phi_instr(nir_instr_as_phi(instr
), state
);
1244 case nir_instr_type_parallel_copy
:
1245 print_parallel_copy_instr(nir_instr_as_parallel_copy(instr
), state
);
1249 unreachable("Invalid instruction type");
1255 compare_block_index(const void *p1
, const void *p2
)
1257 const nir_block
*block1
= *((const nir_block
**) p1
);
1258 const nir_block
*block2
= *((const nir_block
**) p2
);
1260 return (int) block1
->index
- (int) block2
->index
;
1263 static void print_cf_node(nir_cf_node
*node
, print_state
*state
,
1267 print_block(nir_block
*block
, print_state
*state
, unsigned tabs
)
1269 FILE *fp
= state
->fp
;
1271 print_tabs(tabs
, fp
);
1272 fprintf(fp
, "block block_%u:\n", block
->index
);
1274 /* sort the predecessors by index so we consistently print the same thing */
1277 malloc(block
->predecessors
->entries
* sizeof(nir_block
*));
1280 set_foreach(block
->predecessors
, entry
) {
1281 preds
[i
++] = (nir_block
*) entry
->key
;
1284 qsort(preds
, block
->predecessors
->entries
, sizeof(nir_block
*),
1285 compare_block_index
);
1287 print_tabs(tabs
, fp
);
1288 fprintf(fp
, "/* preds: ");
1289 for (unsigned i
= 0; i
< block
->predecessors
->entries
; i
++) {
1290 fprintf(fp
, "block_%u ", preds
[i
]->index
);
1292 fprintf(fp
, "*/\n");
1296 nir_foreach_instr(instr
, block
) {
1297 print_instr(instr
, state
, tabs
);
1299 print_annotation(state
, instr
);
1302 print_tabs(tabs
, fp
);
1303 fprintf(fp
, "/* succs: ");
1304 for (unsigned i
= 0; i
< 2; i
++)
1305 if (block
->successors
[i
]) {
1306 fprintf(fp
, "block_%u ", block
->successors
[i
]->index
);
1308 fprintf(fp
, "*/\n");
1312 print_if(nir_if
*if_stmt
, print_state
*state
, unsigned tabs
)
1314 FILE *fp
= state
->fp
;
1316 print_tabs(tabs
, fp
);
1318 print_src(&if_stmt
->condition
, state
);
1319 fprintf(fp
, " {\n");
1320 foreach_list_typed(nir_cf_node
, node
, node
, &if_stmt
->then_list
) {
1321 print_cf_node(node
, state
, tabs
+ 1);
1323 print_tabs(tabs
, fp
);
1324 fprintf(fp
, "} else {\n");
1325 foreach_list_typed(nir_cf_node
, node
, node
, &if_stmt
->else_list
) {
1326 print_cf_node(node
, state
, tabs
+ 1);
1328 print_tabs(tabs
, fp
);
1333 print_loop(nir_loop
*loop
, print_state
*state
, unsigned tabs
)
1335 FILE *fp
= state
->fp
;
1337 print_tabs(tabs
, fp
);
1338 fprintf(fp
, "loop {\n");
1339 foreach_list_typed(nir_cf_node
, node
, node
, &loop
->body
) {
1340 print_cf_node(node
, state
, tabs
+ 1);
1342 print_tabs(tabs
, fp
);
1347 print_cf_node(nir_cf_node
*node
, print_state
*state
, unsigned int tabs
)
1349 switch (node
->type
) {
1350 case nir_cf_node_block
:
1351 print_block(nir_cf_node_as_block(node
), state
, tabs
);
1354 case nir_cf_node_if
:
1355 print_if(nir_cf_node_as_if(node
), state
, tabs
);
1358 case nir_cf_node_loop
:
1359 print_loop(nir_cf_node_as_loop(node
), state
, tabs
);
1363 unreachable("Invalid CFG node type");
1368 print_function_impl(nir_function_impl
*impl
, print_state
*state
)
1370 FILE *fp
= state
->fp
;
1372 fprintf(fp
, "\nimpl %s ", impl
->function
->name
);
1376 nir_foreach_variable(var
, &impl
->locals
) {
1378 print_var_decl(var
, state
);
1381 foreach_list_typed(nir_register
, reg
, node
, &impl
->registers
) {
1383 print_register_decl(reg
, state
);
1386 nir_index_blocks(impl
);
1388 foreach_list_typed(nir_cf_node
, node
, node
, &impl
->body
) {
1389 print_cf_node(node
, state
, 1);
1392 fprintf(fp
, "\tblock block_%u:\n}\n\n", impl
->end_block
->index
);
1396 print_function(nir_function
*function
, print_state
*state
)
1398 FILE *fp
= state
->fp
;
1400 fprintf(fp
, "decl_function %s (%d params)", function
->name
,
1401 function
->num_params
);
1405 if (function
->impl
!= NULL
) {
1406 print_function_impl(function
->impl
, state
);
1412 init_print_state(print_state
*state
, nir_shader
*shader
, FILE *fp
)
1415 state
->shader
= shader
;
1416 state
->ht
= _mesa_pointer_hash_table_create(NULL
);
1417 state
->syms
= _mesa_set_create(NULL
, _mesa_key_hash_string
,
1418 _mesa_key_string_equal
);
1423 destroy_print_state(print_state
*state
)
1425 _mesa_hash_table_destroy(state
->ht
, NULL
);
1426 _mesa_set_destroy(state
->syms
, NULL
);
1430 nir_print_shader_annotated(nir_shader
*shader
, FILE *fp
,
1431 struct hash_table
*annotations
)
1434 init_print_state(&state
, shader
, fp
);
1436 state
.annotations
= annotations
;
1438 fprintf(fp
, "shader: %s\n", gl_shader_stage_name(shader
->info
.stage
));
1440 if (shader
->info
.name
)
1441 fprintf(fp
, "name: %s\n", shader
->info
.name
);
1443 if (shader
->info
.label
)
1444 fprintf(fp
, "label: %s\n", shader
->info
.label
);
1446 if (gl_shader_stage_is_compute(shader
->info
.stage
)) {
1447 fprintf(fp
, "local-size: %u, %u, %u%s\n",
1448 shader
->info
.cs
.local_size
[0],
1449 shader
->info
.cs
.local_size
[1],
1450 shader
->info
.cs
.local_size
[2],
1451 shader
->info
.cs
.local_size_variable
? " (variable)" : "");
1452 fprintf(fp
, "shared-size: %u\n", shader
->info
.cs
.shared_size
);
1455 fprintf(fp
, "inputs: %u\n", shader
->num_inputs
);
1456 fprintf(fp
, "outputs: %u\n", shader
->num_outputs
);
1457 fprintf(fp
, "uniforms: %u\n", shader
->num_uniforms
);
1458 fprintf(fp
, "shared: %u\n", shader
->num_shared
);
1459 if (shader
->scratch_size
)
1460 fprintf(fp
, "scratch: %u\n", shader
->scratch_size
);
1462 nir_foreach_variable(var
, &shader
->uniforms
) {
1463 print_var_decl(var
, &state
);
1466 nir_foreach_variable(var
, &shader
->inputs
) {
1467 print_var_decl(var
, &state
);
1470 nir_foreach_variable(var
, &shader
->outputs
) {
1471 print_var_decl(var
, &state
);
1474 nir_foreach_variable(var
, &shader
->shared
) {
1475 print_var_decl(var
, &state
);
1478 nir_foreach_variable(var
, &shader
->globals
) {
1479 print_var_decl(var
, &state
);
1482 nir_foreach_variable(var
, &shader
->system_values
) {
1483 print_var_decl(var
, &state
);
1486 foreach_list_typed(nir_function
, func
, node
, &shader
->functions
) {
1487 print_function(func
, &state
);
1490 destroy_print_state(&state
);
1494 nir_print_shader(nir_shader
*shader
, FILE *fp
)
1496 nir_print_shader_annotated(shader
, fp
, NULL
);
1501 nir_print_instr(const nir_instr
*instr
, FILE *fp
)
1503 print_state state
= {
1506 print_instr(instr
, &state
, 0);
1511 nir_print_deref(const nir_deref_instr
*deref
, FILE *fp
)
1513 print_state state
= {
1516 print_deref_link(deref
, true, &state
);