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
, "[%"PRIx64
"]", 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
])
806 if (idx
== NIR_INTRINSIC_WRMASK
) {
807 /* special case wrmask to show it as a writemask.. */
808 unsigned wrmask
= nir_intrinsic_write_mask(instr
);
809 fprintf(fp
, " wrmask=");
810 for (unsigned i
= 0; i
< 4; i
++)
811 if ((wrmask
>> i
) & 1)
812 fprintf(fp
, "%c", "xyzw"[i
]);
813 } else if (idx
== NIR_INTRINSIC_REDUCTION_OP
) {
814 nir_op reduction_op
= nir_intrinsic_reduction_op(instr
);
815 fprintf(fp
, " reduction_op=%s", nir_op_infos
[reduction_op
].name
);
816 } else if (idx
== NIR_INTRINSIC_IMAGE_DIM
) {
817 static const char *dim_name
[] = {
818 [GLSL_SAMPLER_DIM_1D
] = "1D",
819 [GLSL_SAMPLER_DIM_2D
] = "2D",
820 [GLSL_SAMPLER_DIM_3D
] = "3D",
821 [GLSL_SAMPLER_DIM_CUBE
] = "Cube",
822 [GLSL_SAMPLER_DIM_RECT
] = "Rect",
823 [GLSL_SAMPLER_DIM_BUF
] = "Buf",
824 [GLSL_SAMPLER_DIM_MS
] = "2D-MSAA",
825 [GLSL_SAMPLER_DIM_SUBPASS
] = "Subpass",
826 [GLSL_SAMPLER_DIM_SUBPASS_MS
] = "Subpass-MSAA",
828 enum glsl_sampler_dim dim
= nir_intrinsic_image_dim(instr
);
829 assert(dim
< ARRAY_SIZE(dim_name
) && dim_name
[dim
]);
830 fprintf(fp
, " image_dim=%s", dim_name
[dim
]);
831 } else if (idx
== NIR_INTRINSIC_IMAGE_ARRAY
) {
832 bool array
= nir_intrinsic_image_array(instr
);
833 fprintf(fp
, " image_array=%s", array
? "true" : "false");
834 } else if (idx
== NIR_INTRINSIC_DESC_TYPE
) {
835 VkDescriptorType desc_type
= nir_intrinsic_desc_type(instr
);
836 fprintf(fp
, " desc_type=%s", vulkan_descriptor_type_name(desc_type
));
837 } else if (idx
== NIR_INTRINSIC_TYPE
) {
838 nir_alu_type type
= nir_intrinsic_type(instr
);
839 unsigned size
= nir_alu_type_get_type_size(type
);
841 switch (nir_alu_type_get_base_type(type
)) {
842 case nir_type_int
: name
= "int"; break;
843 case nir_type_uint
: name
= "uint"; break;
844 case nir_type_bool
: name
= "bool"; break;
845 case nir_type_float
: name
= "float"; break;
846 default: name
= "invalid";
849 fprintf(fp
, " type=%s%u", name
, size
);
851 fprintf(fp
, " type=%s", name
);
852 } else if (idx
== NIR_INTRINSIC_SWIZZLE_MASK
) {
853 fprintf(fp
, " swizzle_mask=");
854 unsigned mask
= nir_intrinsic_swizzle_mask(instr
);
855 if (instr
->intrinsic
== nir_intrinsic_quad_swizzle_amd
) {
856 for (unsigned i
= 0; i
< 4; i
++)
857 fprintf(fp
, "%d", (mask
>> (i
* 2) & 3));
858 } else if (instr
->intrinsic
== nir_intrinsic_masked_swizzle_amd
) {
859 fprintf(fp
, "((id & %d) | %d) ^ %d", mask
& 0x1F,
861 (mask
>> 10) & 0x1F);
863 fprintf(fp
, "%d", mask
);
866 unsigned off
= info
->index_map
[idx
] - 1;
867 assert(index_name
[idx
]); /* forgot to update index_name table? */
868 fprintf(fp
, " %s=%d", index_name
[idx
], instr
->const_index
[off
]);
876 struct exec_list
*var_list
= NULL
;
878 switch (instr
->intrinsic
) {
879 case nir_intrinsic_load_uniform
:
880 var_list
= &state
->shader
->uniforms
;
882 case nir_intrinsic_load_input
:
883 case nir_intrinsic_load_interpolated_input
:
884 case nir_intrinsic_load_per_vertex_input
:
885 var_list
= &state
->shader
->inputs
;
887 case nir_intrinsic_load_output
:
888 case nir_intrinsic_store_output
:
889 case nir_intrinsic_store_per_vertex_output
:
890 var_list
= &state
->shader
->outputs
;
896 nir_foreach_variable(var
, var_list
) {
897 if ((var
->data
.driver_location
== nir_intrinsic_base(instr
)) &&
898 (instr
->intrinsic
== nir_intrinsic_load_uniform
||
899 (nir_intrinsic_component(instr
) >= var
->data
.location_frac
&&
900 nir_intrinsic_component(instr
) <
901 (var
->data
.location_frac
+ glsl_get_components(var
->type
)))) &&
903 fprintf(fp
, "\t/* %s */", var
->name
);
910 print_tex_instr(nir_tex_instr
*instr
, print_state
*state
)
912 FILE *fp
= state
->fp
;
914 print_dest(&instr
->dest
, state
);
934 case nir_texop_txf_ms
:
935 fprintf(fp
, "txf_ms ");
937 case nir_texop_txf_ms_fb
:
938 fprintf(fp
, "txf_ms_fb ");
940 case nir_texop_txf_ms_mcs
:
941 fprintf(fp
, "txf_ms_mcs ");
952 case nir_texop_query_levels
:
953 fprintf(fp
, "query_levels ");
955 case nir_texop_texture_samples
:
956 fprintf(fp
, "texture_samples ");
958 case nir_texop_samples_identical
:
959 fprintf(fp
, "samples_identical ");
962 unreachable("Invalid texture operation");
966 bool has_texture_deref
= false, has_sampler_deref
= false;
967 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++) {
972 print_src(&instr
->src
[i
].src
, state
);
975 switch(instr
->src
[i
].src_type
) {
976 case nir_tex_src_coord
:
977 fprintf(fp
, "(coord)");
979 case nir_tex_src_projector
:
980 fprintf(fp
, "(projector)");
982 case nir_tex_src_comparator
:
983 fprintf(fp
, "(comparator)");
985 case nir_tex_src_offset
:
986 fprintf(fp
, "(offset)");
988 case nir_tex_src_bias
:
989 fprintf(fp
, "(bias)");
991 case nir_tex_src_lod
:
992 fprintf(fp
, "(lod)");
994 case nir_tex_src_min_lod
:
995 fprintf(fp
, "(min_lod)");
997 case nir_tex_src_ms_index
:
998 fprintf(fp
, "(ms_index)");
1000 case nir_tex_src_ms_mcs
:
1001 fprintf(fp
, "(ms_mcs)");
1003 case nir_tex_src_ddx
:
1004 fprintf(fp
, "(ddx)");
1006 case nir_tex_src_ddy
:
1007 fprintf(fp
, "(ddy)");
1009 case nir_tex_src_texture_deref
:
1010 has_texture_deref
= true;
1011 fprintf(fp
, "(texture_deref)");
1013 case nir_tex_src_sampler_deref
:
1014 has_sampler_deref
= true;
1015 fprintf(fp
, "(sampler_deref)");
1017 case nir_tex_src_texture_offset
:
1018 fprintf(fp
, "(texture_offset)");
1020 case nir_tex_src_sampler_offset
:
1021 fprintf(fp
, "(sampler_offset)");
1023 case nir_tex_src_texture_handle
:
1024 fprintf(fp
, "(texture_handle)");
1026 case nir_tex_src_sampler_handle
:
1027 fprintf(fp
, "(sampler_handle)");
1029 case nir_tex_src_plane
:
1030 fprintf(fp
, "(plane)");
1034 unreachable("Invalid texture source type");
1039 if (instr
->op
== nir_texop_tg4
) {
1040 fprintf(fp
, ", %u (gather_component)", instr
->component
);
1043 if (nir_tex_instr_has_explicit_tg4_offsets(instr
)) {
1044 fprintf(fp
, ", { (%i, %i)", instr
->tg4_offsets
[0][0], instr
->tg4_offsets
[0][1]);
1045 for (unsigned i
= 1; i
< 4; ++i
)
1046 fprintf(fp
, ", (%i, %i)", instr
->tg4_offsets
[i
][0],
1047 instr
->tg4_offsets
[i
][1]);
1048 fprintf(fp
, " } (offsets)");
1051 if (instr
->op
!= nir_texop_txf_ms_fb
) {
1052 if (!has_texture_deref
) {
1053 fprintf(fp
, ", %u (texture)", instr
->texture_index
);
1056 if (!has_sampler_deref
) {
1057 fprintf(fp
, ", %u (sampler)", instr
->sampler_index
);
1063 print_call_instr(nir_call_instr
*instr
, print_state
*state
)
1065 FILE *fp
= state
->fp
;
1067 fprintf(fp
, "call %s ", instr
->callee
->name
);
1069 for (unsigned i
= 0; i
< instr
->num_params
; i
++) {
1073 print_src(&instr
->params
[i
], state
);
1078 print_load_const_instr(nir_load_const_instr
*instr
, print_state
*state
)
1080 FILE *fp
= state
->fp
;
1082 print_ssa_def(&instr
->def
, state
);
1084 fprintf(fp
, " = load_const (");
1086 for (unsigned i
= 0; i
< instr
->def
.num_components
; i
++) {
1091 * we don't really know the type of the constant (if it will be used as a
1092 * float or an int), so just print the raw constant in hex for fidelity
1093 * and then print the float in a comment for readability.
1096 switch (instr
->def
.bit_size
) {
1098 fprintf(fp
, "0x%16" PRIx64
" /* %f */", instr
->value
[i
].u64
,
1099 instr
->value
[i
].f64
);
1102 fprintf(fp
, "0x%08x /* %f */", instr
->value
[i
].u32
, instr
->value
[i
].f32
);
1105 fprintf(fp
, "0x%04x /* %f */", instr
->value
[i
].u16
,
1106 _mesa_half_to_float(instr
->value
[i
].u16
));
1109 fprintf(fp
, "0x%02x", instr
->value
[i
].u8
);
1112 fprintf(fp
, "%s", instr
->value
[i
].b
? "true" : "false");
1121 print_jump_instr(nir_jump_instr
*instr
, print_state
*state
)
1123 FILE *fp
= state
->fp
;
1125 switch (instr
->type
) {
1126 case nir_jump_break
:
1127 fprintf(fp
, "break");
1130 case nir_jump_continue
:
1131 fprintf(fp
, "continue");
1134 case nir_jump_return
:
1135 fprintf(fp
, "return");
1141 print_ssa_undef_instr(nir_ssa_undef_instr
* instr
, print_state
*state
)
1143 FILE *fp
= state
->fp
;
1144 print_ssa_def(&instr
->def
, state
);
1145 fprintf(fp
, " = undefined");
1149 print_phi_instr(nir_phi_instr
*instr
, print_state
*state
)
1151 FILE *fp
= state
->fp
;
1152 print_dest(&instr
->dest
, state
);
1153 fprintf(fp
, " = phi ");
1154 nir_foreach_phi_src(src
, instr
) {
1155 if (&src
->node
!= exec_list_get_head(&instr
->srcs
))
1158 fprintf(fp
, "block_%u: ", src
->pred
->index
);
1159 print_src(&src
->src
, state
);
1164 print_parallel_copy_instr(nir_parallel_copy_instr
*instr
, print_state
*state
)
1166 FILE *fp
= state
->fp
;
1167 nir_foreach_parallel_copy_entry(entry
, instr
) {
1168 if (&entry
->node
!= exec_list_get_head(&instr
->entries
))
1171 print_dest(&entry
->dest
, state
);
1173 print_src(&entry
->src
, state
);
1178 print_instr(const nir_instr
*instr
, print_state
*state
, unsigned tabs
)
1180 FILE *fp
= state
->fp
;
1181 print_tabs(tabs
, fp
);
1183 switch (instr
->type
) {
1184 case nir_instr_type_alu
:
1185 print_alu_instr(nir_instr_as_alu(instr
), state
);
1188 case nir_instr_type_deref
:
1189 print_deref_instr(nir_instr_as_deref(instr
), state
);
1192 case nir_instr_type_call
:
1193 print_call_instr(nir_instr_as_call(instr
), state
);
1196 case nir_instr_type_intrinsic
:
1197 print_intrinsic_instr(nir_instr_as_intrinsic(instr
), state
);
1200 case nir_instr_type_tex
:
1201 print_tex_instr(nir_instr_as_tex(instr
), state
);
1204 case nir_instr_type_load_const
:
1205 print_load_const_instr(nir_instr_as_load_const(instr
), state
);
1208 case nir_instr_type_jump
:
1209 print_jump_instr(nir_instr_as_jump(instr
), state
);
1212 case nir_instr_type_ssa_undef
:
1213 print_ssa_undef_instr(nir_instr_as_ssa_undef(instr
), state
);
1216 case nir_instr_type_phi
:
1217 print_phi_instr(nir_instr_as_phi(instr
), state
);
1220 case nir_instr_type_parallel_copy
:
1221 print_parallel_copy_instr(nir_instr_as_parallel_copy(instr
), state
);
1225 unreachable("Invalid instruction type");
1231 compare_block_index(const void *p1
, const void *p2
)
1233 const nir_block
*block1
= *((const nir_block
**) p1
);
1234 const nir_block
*block2
= *((const nir_block
**) p2
);
1236 return (int) block1
->index
- (int) block2
->index
;
1239 static void print_cf_node(nir_cf_node
*node
, print_state
*state
,
1243 print_block(nir_block
*block
, print_state
*state
, unsigned tabs
)
1245 FILE *fp
= state
->fp
;
1247 print_tabs(tabs
, fp
);
1248 fprintf(fp
, "block block_%u:\n", block
->index
);
1250 /* sort the predecessors by index so we consistently print the same thing */
1253 malloc(block
->predecessors
->entries
* sizeof(nir_block
*));
1256 set_foreach(block
->predecessors
, entry
) {
1257 preds
[i
++] = (nir_block
*) entry
->key
;
1260 qsort(preds
, block
->predecessors
->entries
, sizeof(nir_block
*),
1261 compare_block_index
);
1263 print_tabs(tabs
, fp
);
1264 fprintf(fp
, "/* preds: ");
1265 for (unsigned i
= 0; i
< block
->predecessors
->entries
; i
++) {
1266 fprintf(fp
, "block_%u ", preds
[i
]->index
);
1268 fprintf(fp
, "*/\n");
1272 nir_foreach_instr(instr
, block
) {
1273 print_instr(instr
, state
, tabs
);
1275 print_annotation(state
, instr
);
1278 print_tabs(tabs
, fp
);
1279 fprintf(fp
, "/* succs: ");
1280 for (unsigned i
= 0; i
< 2; i
++)
1281 if (block
->successors
[i
]) {
1282 fprintf(fp
, "block_%u ", block
->successors
[i
]->index
);
1284 fprintf(fp
, "*/\n");
1288 print_if(nir_if
*if_stmt
, print_state
*state
, unsigned tabs
)
1290 FILE *fp
= state
->fp
;
1292 print_tabs(tabs
, fp
);
1294 print_src(&if_stmt
->condition
, state
);
1295 fprintf(fp
, " {\n");
1296 foreach_list_typed(nir_cf_node
, node
, node
, &if_stmt
->then_list
) {
1297 print_cf_node(node
, state
, tabs
+ 1);
1299 print_tabs(tabs
, fp
);
1300 fprintf(fp
, "} else {\n");
1301 foreach_list_typed(nir_cf_node
, node
, node
, &if_stmt
->else_list
) {
1302 print_cf_node(node
, state
, tabs
+ 1);
1304 print_tabs(tabs
, fp
);
1309 print_loop(nir_loop
*loop
, print_state
*state
, unsigned tabs
)
1311 FILE *fp
= state
->fp
;
1313 print_tabs(tabs
, fp
);
1314 fprintf(fp
, "loop {\n");
1315 foreach_list_typed(nir_cf_node
, node
, node
, &loop
->body
) {
1316 print_cf_node(node
, state
, tabs
+ 1);
1318 print_tabs(tabs
, fp
);
1323 print_cf_node(nir_cf_node
*node
, print_state
*state
, unsigned int tabs
)
1325 switch (node
->type
) {
1326 case nir_cf_node_block
:
1327 print_block(nir_cf_node_as_block(node
), state
, tabs
);
1330 case nir_cf_node_if
:
1331 print_if(nir_cf_node_as_if(node
), state
, tabs
);
1334 case nir_cf_node_loop
:
1335 print_loop(nir_cf_node_as_loop(node
), state
, tabs
);
1339 unreachable("Invalid CFG node type");
1344 print_function_impl(nir_function_impl
*impl
, print_state
*state
)
1346 FILE *fp
= state
->fp
;
1348 fprintf(fp
, "\nimpl %s ", impl
->function
->name
);
1352 nir_foreach_variable(var
, &impl
->locals
) {
1354 print_var_decl(var
, state
);
1357 foreach_list_typed(nir_register
, reg
, node
, &impl
->registers
) {
1359 print_register_decl(reg
, state
);
1362 nir_index_blocks(impl
);
1364 foreach_list_typed(nir_cf_node
, node
, node
, &impl
->body
) {
1365 print_cf_node(node
, state
, 1);
1368 fprintf(fp
, "\tblock block_%u:\n}\n\n", impl
->end_block
->index
);
1372 print_function(nir_function
*function
, print_state
*state
)
1374 FILE *fp
= state
->fp
;
1376 fprintf(fp
, "decl_function %s (%d params)", function
->name
,
1377 function
->num_params
);
1381 if (function
->impl
!= NULL
) {
1382 print_function_impl(function
->impl
, state
);
1388 init_print_state(print_state
*state
, nir_shader
*shader
, FILE *fp
)
1391 state
->shader
= shader
;
1392 state
->ht
= _mesa_pointer_hash_table_create(NULL
);
1393 state
->syms
= _mesa_set_create(NULL
, _mesa_key_hash_string
,
1394 _mesa_key_string_equal
);
1399 destroy_print_state(print_state
*state
)
1401 _mesa_hash_table_destroy(state
->ht
, NULL
);
1402 _mesa_set_destroy(state
->syms
, NULL
);
1406 nir_print_shader_annotated(nir_shader
*shader
, FILE *fp
,
1407 struct hash_table
*annotations
)
1410 init_print_state(&state
, shader
, fp
);
1412 state
.annotations
= annotations
;
1414 fprintf(fp
, "shader: %s\n", gl_shader_stage_name(shader
->info
.stage
));
1416 if (shader
->info
.name
)
1417 fprintf(fp
, "name: %s\n", shader
->info
.name
);
1419 if (shader
->info
.label
)
1420 fprintf(fp
, "label: %s\n", shader
->info
.label
);
1422 if (gl_shader_stage_is_compute(shader
->info
.stage
)) {
1423 fprintf(fp
, "local-size: %u, %u, %u%s\n",
1424 shader
->info
.cs
.local_size
[0],
1425 shader
->info
.cs
.local_size
[1],
1426 shader
->info
.cs
.local_size
[2],
1427 shader
->info
.cs
.local_size_variable
? " (variable)" : "");
1428 fprintf(fp
, "shared-size: %u\n", shader
->info
.cs
.shared_size
);
1431 fprintf(fp
, "inputs: %u\n", shader
->num_inputs
);
1432 fprintf(fp
, "outputs: %u\n", shader
->num_outputs
);
1433 fprintf(fp
, "uniforms: %u\n", shader
->num_uniforms
);
1434 fprintf(fp
, "shared: %u\n", shader
->num_shared
);
1435 if (shader
->scratch_size
)
1436 fprintf(fp
, "scratch: %u\n", shader
->scratch_size
);
1438 nir_foreach_variable(var
, &shader
->uniforms
) {
1439 print_var_decl(var
, &state
);
1442 nir_foreach_variable(var
, &shader
->inputs
) {
1443 print_var_decl(var
, &state
);
1446 nir_foreach_variable(var
, &shader
->outputs
) {
1447 print_var_decl(var
, &state
);
1450 nir_foreach_variable(var
, &shader
->shared
) {
1451 print_var_decl(var
, &state
);
1454 nir_foreach_variable(var
, &shader
->globals
) {
1455 print_var_decl(var
, &state
);
1458 nir_foreach_variable(var
, &shader
->system_values
) {
1459 print_var_decl(var
, &state
);
1462 foreach_list_typed(nir_function
, func
, node
, &shader
->functions
) {
1463 print_function(func
, &state
);
1466 destroy_print_state(&state
);
1470 nir_print_shader(nir_shader
*shader
, FILE *fp
)
1472 nir_print_shader_annotated(shader
, fp
, NULL
);
1477 nir_print_instr(const nir_instr
*instr
, FILE *fp
)
1479 print_state state
= {
1482 print_instr(instr
, &state
, 0);
1487 nir_print_deref(const nir_deref_instr
*deref
, FILE *fp
)
1489 print_state state
= {
1492 print_deref_link(deref
, true, &state
);