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
)
246 for (unsigned i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++) {
250 print_alu_src(instr
, i
, state
);
255 get_var_name(nir_variable
*var
, print_state
*state
)
257 if (state
->ht
== NULL
)
258 return var
->name
? var
->name
: "unnamed";
262 struct hash_entry
*entry
= _mesa_hash_table_search(state
->ht
, var
);
267 if (var
->name
== NULL
) {
268 name
= ralloc_asprintf(state
->syms
, "@%u", state
->index
++);
270 struct set_entry
*set_entry
= _mesa_set_search(state
->syms
, var
->name
);
271 if (set_entry
!= NULL
) {
272 /* we have a collision with another name, append an @ + a unique
274 name
= ralloc_asprintf(state
->syms
, "%s@%u", var
->name
,
277 /* Mark this one as seen */
278 _mesa_set_add(state
->syms
, var
->name
);
283 _mesa_hash_table_insert(state
->ht
, var
, name
);
289 print_constant(nir_constant
*c
, const struct glsl_type
*type
, print_state
*state
)
291 FILE *fp
= state
->fp
;
292 const unsigned rows
= glsl_get_vector_elements(type
);
293 const unsigned cols
= glsl_get_matrix_columns(type
);
296 switch (glsl_get_base_type(type
)) {
298 /* Only float base types can be matrices. */
301 for (i
= 0; i
< rows
; i
++) {
302 if (i
> 0) fprintf(fp
, ", ");
303 fprintf(fp
, "%s", c
->values
[0].b
[i
] ? "true" : "false");
307 case GLSL_TYPE_UINT8
:
309 /* Only float base types can be matrices. */
312 for (i
= 0; i
< rows
; i
++) {
313 if (i
> 0) fprintf(fp
, ", ");
314 fprintf(fp
, "0x%02x", c
->values
[0].u8
[i
]);
318 case GLSL_TYPE_UINT16
:
319 case GLSL_TYPE_INT16
:
320 /* Only float base types can be matrices. */
323 for (i
= 0; i
< rows
; i
++) {
324 if (i
> 0) fprintf(fp
, ", ");
325 fprintf(fp
, "0x%04x", c
->values
[0].u16
[i
]);
331 /* Only float base types can be matrices. */
334 for (i
= 0; i
< rows
; i
++) {
335 if (i
> 0) fprintf(fp
, ", ");
336 fprintf(fp
, "0x%08x", c
->values
[0].u32
[i
]);
340 case GLSL_TYPE_FLOAT16
:
341 for (i
= 0; i
< cols
; i
++) {
342 for (j
= 0; j
< rows
; j
++) {
343 if (i
+ j
> 0) fprintf(fp
, ", ");
344 fprintf(fp
, "%f", _mesa_half_to_float(c
->values
[i
].u16
[j
]));
349 case GLSL_TYPE_FLOAT
:
350 for (i
= 0; i
< cols
; i
++) {
351 for (j
= 0; j
< rows
; j
++) {
352 if (i
+ j
> 0) fprintf(fp
, ", ");
353 fprintf(fp
, "%f", c
->values
[i
].f32
[j
]);
358 case GLSL_TYPE_DOUBLE
:
359 for (i
= 0; i
< cols
; i
++) {
360 for (j
= 0; j
< rows
; j
++) {
361 if (i
+ j
> 0) fprintf(fp
, ", ");
362 fprintf(fp
, "%f", c
->values
[i
].f64
[j
]);
367 case GLSL_TYPE_UINT64
:
368 case GLSL_TYPE_INT64
:
369 /* Only float base types can be matrices. */
372 for (i
= 0; i
< cols
; i
++) {
373 if (i
> 0) fprintf(fp
, ", ");
374 fprintf(fp
, "0x%08" PRIx64
, c
->values
[0].u64
[i
]);
378 case GLSL_TYPE_STRUCT
:
379 for (i
= 0; i
< c
->num_elements
; i
++) {
380 if (i
> 0) fprintf(fp
, ", ");
382 print_constant(c
->elements
[i
], glsl_get_struct_field(type
, i
), state
);
387 case GLSL_TYPE_ARRAY
:
388 for (i
= 0; i
< c
->num_elements
; i
++) {
389 if (i
> 0) fprintf(fp
, ", ");
391 print_constant(c
->elements
[i
], glsl_get_array_element(type
), state
);
397 unreachable("not reached");
402 get_variable_mode_str(nir_variable_mode mode
, bool want_local_global_mode
)
405 case nir_var_shader_in
:
407 case nir_var_shader_out
:
409 case nir_var_uniform
:
411 case nir_var_mem_ubo
:
413 case nir_var_system_value
:
415 case nir_var_mem_ssbo
:
417 case nir_var_mem_shared
:
419 case nir_var_mem_global
:
421 case nir_var_shader_temp
:
422 return want_local_global_mode
? "shader_temp" : "";
423 case nir_var_function_temp
:
424 return want_local_global_mode
? "function_temp" : "";
431 print_var_decl(nir_variable
*var
, print_state
*state
)
433 FILE *fp
= state
->fp
;
435 fprintf(fp
, "decl_var ");
437 const char *const cent
= (var
->data
.centroid
) ? "centroid " : "";
438 const char *const samp
= (var
->data
.sample
) ? "sample " : "";
439 const char *const patch
= (var
->data
.patch
) ? "patch " : "";
440 const char *const inv
= (var
->data
.invariant
) ? "invariant " : "";
441 fprintf(fp
, "%s%s%s%s%s %s ",
442 cent
, samp
, patch
, inv
, get_variable_mode_str(var
->data
.mode
, false),
443 glsl_interp_mode_name(var
->data
.interpolation
));
445 enum gl_access_qualifier access
= var
->data
.image
.access
;
446 const char *const coher
= (access
& ACCESS_COHERENT
) ? "coherent " : "";
447 const char *const volat
= (access
& ACCESS_VOLATILE
) ? "volatile " : "";
448 const char *const restr
= (access
& ACCESS_RESTRICT
) ? "restrict " : "";
449 const char *const ronly
= (access
& ACCESS_NON_WRITEABLE
) ? "readonly " : "";
450 const char *const wonly
= (access
& ACCESS_NON_READABLE
) ? "writeonly " : "";
451 fprintf(fp
, "%s%s%s%s%s", coher
, volat
, restr
, ronly
, wonly
);
453 #define FORMAT_CASE(x) case x: fprintf(stderr, #x " "); break
454 switch (var
->data
.image
.format
) {
455 FORMAT_CASE(GL_RGBA32F
);
456 FORMAT_CASE(GL_RGBA32UI
);
457 FORMAT_CASE(GL_RGBA32I
);
458 FORMAT_CASE(GL_R32F
);
459 FORMAT_CASE(GL_R32UI
);
460 FORMAT_CASE(GL_R32I
);
461 FORMAT_CASE(GL_RG32F
);
462 FORMAT_CASE(GL_RG32UI
);
463 FORMAT_CASE(GL_RG32I
);
466 FORMAT_CASE(GL_RGBA8
);
467 FORMAT_CASE(GL_R8_SNORM
);
468 FORMAT_CASE(GL_RG8_SNORM
);
469 FORMAT_CASE(GL_RGBA8_SNORM
);
471 FORMAT_CASE(GL_RG16
);
472 FORMAT_CASE(GL_RGBA16
);
473 FORMAT_CASE(GL_R16_SNORM
);
474 FORMAT_CASE(GL_RG16_SNORM
);
475 FORMAT_CASE(GL_RGBA16_SNORM
);
476 FORMAT_CASE(GL_R16F
);
477 FORMAT_CASE(GL_RG16F
);
478 FORMAT_CASE(GL_RGBA16F
);
479 FORMAT_CASE(GL_R8UI
);
481 FORMAT_CASE(GL_RG8UI
);
482 FORMAT_CASE(GL_RG8I
);
483 FORMAT_CASE(GL_RGBA8UI
);
484 FORMAT_CASE(GL_RGBA8I
);
485 FORMAT_CASE(GL_R16UI
);
486 FORMAT_CASE(GL_R16I
);
487 FORMAT_CASE(GL_RG16UI
);
488 FORMAT_CASE(GL_RG16I
);
489 FORMAT_CASE(GL_RGBA16UI
);
490 FORMAT_CASE(GL_RGBA16I
);
491 FORMAT_CASE(GL_R11F_G11F_B10F
);
492 FORMAT_CASE(GL_RGB9_E5
);
493 FORMAT_CASE(GL_RGB10_A2
);
494 FORMAT_CASE(GL_RGB10_A2UI
);
495 default: /* Including the normal GL_NONE */
500 fprintf(fp
, "%s %s", glsl_get_type_name(var
->type
),
501 get_var_name(var
, state
));
503 if (var
->data
.mode
== nir_var_shader_in
||
504 var
->data
.mode
== nir_var_shader_out
||
505 var
->data
.mode
== nir_var_uniform
||
506 var
->data
.mode
== nir_var_mem_ubo
||
507 var
->data
.mode
== nir_var_mem_ssbo
) {
508 const char *loc
= NULL
;
511 switch (state
->shader
->info
.stage
) {
512 case MESA_SHADER_VERTEX
:
513 if (var
->data
.mode
== nir_var_shader_in
)
514 loc
= gl_vert_attrib_name(var
->data
.location
);
515 else if (var
->data
.mode
== nir_var_shader_out
)
516 loc
= gl_varying_slot_name(var
->data
.location
);
518 case MESA_SHADER_GEOMETRY
:
519 if ((var
->data
.mode
== nir_var_shader_in
) ||
520 (var
->data
.mode
== nir_var_shader_out
))
521 loc
= gl_varying_slot_name(var
->data
.location
);
523 case MESA_SHADER_FRAGMENT
:
524 if (var
->data
.mode
== nir_var_shader_in
)
525 loc
= gl_varying_slot_name(var
->data
.location
);
526 else if (var
->data
.mode
== nir_var_shader_out
)
527 loc
= gl_frag_result_name(var
->data
.location
);
529 case MESA_SHADER_TESS_CTRL
:
530 case MESA_SHADER_TESS_EVAL
:
531 case MESA_SHADER_COMPUTE
:
532 case MESA_SHADER_KERNEL
:
539 snprintf(buf
, sizeof(buf
), "%u", var
->data
.location
);
543 /* For shader I/O vars that have been split to components or packed,
544 * print the fractional location within the input/output.
546 unsigned int num_components
=
547 glsl_get_components(glsl_without_array(var
->type
));
548 const char *components
= NULL
;
549 char components_local
[6] = {'.' /* the rest is 0-filled */};
550 switch (var
->data
.mode
) {
551 case nir_var_shader_in
:
552 case nir_var_shader_out
:
553 if (num_components
< 4 && num_components
!= 0) {
554 const char *xyzw
= "xyzw";
555 for (int i
= 0; i
< num_components
; i
++)
556 components_local
[i
+ 1] = xyzw
[i
+ var
->data
.location_frac
];
558 components
= components_local
;
565 fprintf(fp
, " (%s%s, %u, %u)%s", loc
,
566 components
? components
: "",
567 var
->data
.driver_location
, var
->data
.binding
,
568 var
->data
.compact
? " compact" : "");
571 if (var
->constant_initializer
) {
572 fprintf(fp
, " = { ");
573 print_constant(var
->constant_initializer
, var
->type
, state
);
578 print_annotation(state
, var
);
582 print_deref_link(const nir_deref_instr
*instr
, bool whole_chain
, print_state
*state
)
584 FILE *fp
= state
->fp
;
586 if (instr
->deref_type
== nir_deref_type_var
) {
587 fprintf(fp
, "%s", get_var_name(instr
->var
, state
));
589 } else if (instr
->deref_type
== nir_deref_type_cast
) {
590 fprintf(fp
, "(%s *)", glsl_get_type_name(instr
->type
));
591 print_src(&instr
->parent
, state
);
595 assert(instr
->parent
.is_ssa
);
596 nir_deref_instr
*parent
=
597 nir_instr_as_deref(instr
->parent
.ssa
->parent_instr
);
599 /* Is the parent we're going to print a bare cast? */
600 const bool is_parent_cast
=
601 whole_chain
&& parent
->deref_type
== nir_deref_type_cast
;
603 /* If we're not printing the whole chain, the parent we print will be a SSA
604 * value that represents a pointer. The only deref type that naturally
605 * gives a pointer is a cast.
607 const bool is_parent_pointer
=
608 !whole_chain
|| parent
->deref_type
== nir_deref_type_cast
;
610 /* Struct derefs have a nice syntax that works on pointers, arrays derefs
613 const bool need_deref
=
614 is_parent_pointer
&& instr
->deref_type
!= nir_deref_type_struct
;
616 /* Cast need extra parens and so * dereferences */
617 if (is_parent_cast
|| need_deref
)
624 print_deref_link(parent
, whole_chain
, state
);
626 print_src(&instr
->parent
, state
);
629 if (is_parent_cast
|| need_deref
)
632 switch (instr
->deref_type
) {
633 case nir_deref_type_struct
:
634 fprintf(fp
, "%s%s", is_parent_pointer
? "->" : ".",
635 glsl_get_struct_elem_name(parent
->type
, instr
->strct
.index
));
638 case nir_deref_type_array
:
639 case nir_deref_type_ptr_as_array
: {
640 if (nir_src_is_const(instr
->arr
.index
)) {
641 fprintf(fp
, "[%"PRIx64
"]", nir_src_as_int(instr
->arr
.index
));
644 print_src(&instr
->arr
.index
, state
);
650 case nir_deref_type_array_wildcard
:
655 unreachable("Invalid deref instruction type");
660 print_deref_instr(nir_deref_instr
*instr
, print_state
*state
)
662 FILE *fp
= state
->fp
;
664 print_dest(&instr
->dest
, state
);
666 switch (instr
->deref_type
) {
667 case nir_deref_type_var
:
668 fprintf(fp
, " = deref_var ");
670 case nir_deref_type_array
:
671 case nir_deref_type_array_wildcard
:
672 fprintf(fp
, " = deref_array ");
674 case nir_deref_type_struct
:
675 fprintf(fp
, " = deref_struct ");
677 case nir_deref_type_cast
:
678 fprintf(fp
, " = deref_cast ");
680 case nir_deref_type_ptr_as_array
:
681 fprintf(fp
, " = deref_ptr_as_array ");
684 unreachable("Invalid deref instruction type");
687 /* Only casts naturally return a pointer type */
688 if (instr
->deref_type
!= nir_deref_type_cast
)
691 print_deref_link(instr
, false, state
);
693 fprintf(fp
, " (%s %s) ",
694 get_variable_mode_str(instr
->mode
, true),
695 glsl_get_type_name(instr
->type
));
697 if (instr
->deref_type
!= nir_deref_type_var
&&
698 instr
->deref_type
!= nir_deref_type_cast
) {
699 /* Print the entire chain as a comment */
701 print_deref_link(instr
, true, state
);
707 vulkan_descriptor_type_name(VkDescriptorType type
)
710 case VK_DESCRIPTOR_TYPE_SAMPLER
: return "sampler";
711 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
: return "texture+sampler";
712 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE
: return "texture";
713 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE
: return "image";
714 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER
: return "texture-buffer";
715 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER
: return "image-buffer";
716 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER
: return "UBO";
717 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER
: return "SSBO";
718 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
: return "UBO";
719 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
: return "SSBO";
720 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT
: return "input-att";
721 case VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT
: return "inline-UBO";
722 default: return "unknown";
727 print_intrinsic_instr(nir_intrinsic_instr
*instr
, print_state
*state
)
729 const nir_intrinsic_info
*info
= &nir_intrinsic_infos
[instr
->intrinsic
];
730 unsigned num_srcs
= info
->num_srcs
;
731 FILE *fp
= state
->fp
;
733 if (info
->has_dest
) {
734 print_dest(&instr
->dest
, state
);
738 fprintf(fp
, "intrinsic %s (", info
->name
);
740 for (unsigned i
= 0; i
< num_srcs
; i
++) {
744 print_src(&instr
->src
[i
], state
);
749 for (unsigned i
= 0; i
< info
->num_indices
; i
++) {
753 fprintf(fp
, "%d", instr
->const_index
[i
]);
758 static const char *index_name
[NIR_INTRINSIC_NUM_INDEX_FLAGS
] = {
759 [NIR_INTRINSIC_BASE
] = "base",
760 [NIR_INTRINSIC_WRMASK
] = "wrmask",
761 [NIR_INTRINSIC_STREAM_ID
] = "stream-id",
762 [NIR_INTRINSIC_UCP_ID
] = "ucp-id",
763 [NIR_INTRINSIC_RANGE
] = "range",
764 [NIR_INTRINSIC_DESC_SET
] = "desc-set",
765 [NIR_INTRINSIC_BINDING
] = "binding",
766 [NIR_INTRINSIC_COMPONENT
] = "component",
767 [NIR_INTRINSIC_INTERP_MODE
] = "interp_mode",
768 [NIR_INTRINSIC_REDUCTION_OP
] = "reduction_op",
769 [NIR_INTRINSIC_CLUSTER_SIZE
] = "cluster_size",
770 [NIR_INTRINSIC_PARAM_IDX
] = "param_idx",
771 [NIR_INTRINSIC_IMAGE_DIM
] = "image_dim",
772 [NIR_INTRINSIC_IMAGE_ARRAY
] = "image_array",
773 [NIR_INTRINSIC_ACCESS
] = "access",
774 [NIR_INTRINSIC_FORMAT
] = "format",
775 [NIR_INTRINSIC_ALIGN_MUL
] = "align_mul",
776 [NIR_INTRINSIC_ALIGN_OFFSET
] = "align_offset",
777 [NIR_INTRINSIC_DESC_TYPE
] = "desc_type",
779 for (unsigned idx
= 1; idx
< NIR_INTRINSIC_NUM_INDEX_FLAGS
; idx
++) {
780 if (!info
->index_map
[idx
])
783 if (idx
== NIR_INTRINSIC_WRMASK
) {
784 /* special case wrmask to show it as a writemask.. */
785 unsigned wrmask
= nir_intrinsic_write_mask(instr
);
786 fprintf(fp
, " wrmask=");
787 for (unsigned i
= 0; i
< 4; i
++)
788 if ((wrmask
>> i
) & 1)
789 fprintf(fp
, "%c", "xyzw"[i
]);
790 } else if (idx
== NIR_INTRINSIC_REDUCTION_OP
) {
791 nir_op reduction_op
= nir_intrinsic_reduction_op(instr
);
792 fprintf(fp
, " reduction_op=%s", nir_op_infos
[reduction_op
].name
);
793 } else if (idx
== NIR_INTRINSIC_IMAGE_DIM
) {
794 static const char *dim_name
[] = {
795 [GLSL_SAMPLER_DIM_1D
] = "1D",
796 [GLSL_SAMPLER_DIM_2D
] = "2D",
797 [GLSL_SAMPLER_DIM_3D
] = "3D",
798 [GLSL_SAMPLER_DIM_CUBE
] = "Cube",
799 [GLSL_SAMPLER_DIM_RECT
] = "Rect",
800 [GLSL_SAMPLER_DIM_BUF
] = "Buf",
801 [GLSL_SAMPLER_DIM_MS
] = "2D-MSAA",
802 [GLSL_SAMPLER_DIM_SUBPASS
] = "Subpass",
803 [GLSL_SAMPLER_DIM_SUBPASS_MS
] = "Subpass-MSAA",
805 enum glsl_sampler_dim dim
= nir_intrinsic_image_dim(instr
);
806 assert(dim
< ARRAY_SIZE(dim_name
) && dim_name
[dim
]);
807 fprintf(fp
, " image_dim=%s", dim_name
[dim
]);
808 } else if (idx
== NIR_INTRINSIC_IMAGE_ARRAY
) {
809 bool array
= nir_intrinsic_image_array(instr
);
810 fprintf(fp
, " image_array=%s", array
? "true" : "false");
811 } else if (idx
== NIR_INTRINSIC_DESC_TYPE
) {
812 VkDescriptorType desc_type
= nir_intrinsic_desc_type(instr
);
813 fprintf(fp
, " desc_type=%s", vulkan_descriptor_type_name(desc_type
));
815 unsigned off
= info
->index_map
[idx
] - 1;
816 assert(index_name
[idx
]); /* forgot to update index_name table? */
817 fprintf(fp
, " %s=%d", index_name
[idx
], instr
->const_index
[off
]);
825 struct exec_list
*var_list
= NULL
;
827 switch (instr
->intrinsic
) {
828 case nir_intrinsic_load_uniform
:
829 var_list
= &state
->shader
->uniforms
;
831 case nir_intrinsic_load_input
:
832 case nir_intrinsic_load_interpolated_input
:
833 case nir_intrinsic_load_per_vertex_input
:
834 var_list
= &state
->shader
->inputs
;
836 case nir_intrinsic_load_output
:
837 case nir_intrinsic_store_output
:
838 case nir_intrinsic_store_per_vertex_output
:
839 var_list
= &state
->shader
->outputs
;
845 nir_foreach_variable(var
, var_list
) {
846 if ((var
->data
.driver_location
== nir_intrinsic_base(instr
)) &&
847 (instr
->intrinsic
== nir_intrinsic_load_uniform
||
848 (nir_intrinsic_component(instr
) >= var
->data
.location_frac
&&
849 nir_intrinsic_component(instr
) <
850 (var
->data
.location_frac
+ glsl_get_components(var
->type
)))) &&
852 fprintf(fp
, "\t/* %s */", var
->name
);
859 print_tex_instr(nir_tex_instr
*instr
, print_state
*state
)
861 FILE *fp
= state
->fp
;
863 print_dest(&instr
->dest
, state
);
883 case nir_texop_txf_ms
:
884 fprintf(fp
, "txf_ms ");
886 case nir_texop_txf_ms_mcs
:
887 fprintf(fp
, "txf_ms_mcs ");
898 case nir_texop_query_levels
:
899 fprintf(fp
, "query_levels ");
901 case nir_texop_texture_samples
:
902 fprintf(fp
, "texture_samples ");
904 case nir_texop_samples_identical
:
905 fprintf(fp
, "samples_identical ");
908 unreachable("Invalid texture operation");
912 bool has_texture_deref
= false, has_sampler_deref
= false;
913 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++) {
914 print_src(&instr
->src
[i
].src
, state
);
918 switch(instr
->src
[i
].src_type
) {
919 case nir_tex_src_coord
:
920 fprintf(fp
, "(coord)");
922 case nir_tex_src_projector
:
923 fprintf(fp
, "(projector)");
925 case nir_tex_src_comparator
:
926 fprintf(fp
, "(comparator)");
928 case nir_tex_src_offset
:
929 fprintf(fp
, "(offset)");
931 case nir_tex_src_bias
:
932 fprintf(fp
, "(bias)");
934 case nir_tex_src_lod
:
935 fprintf(fp
, "(lod)");
937 case nir_tex_src_min_lod
:
938 fprintf(fp
, "(min_lod)");
940 case nir_tex_src_ms_index
:
941 fprintf(fp
, "(ms_index)");
943 case nir_tex_src_ms_mcs
:
944 fprintf(fp
, "(ms_mcs)");
946 case nir_tex_src_ddx
:
947 fprintf(fp
, "(ddx)");
949 case nir_tex_src_ddy
:
950 fprintf(fp
, "(ddy)");
952 case nir_tex_src_texture_deref
:
953 has_texture_deref
= true;
954 fprintf(fp
, "(texture_deref)");
956 case nir_tex_src_sampler_deref
:
957 has_sampler_deref
= true;
958 fprintf(fp
, "(sampler_deref)");
960 case nir_tex_src_texture_offset
:
961 fprintf(fp
, "(texture_offset)");
963 case nir_tex_src_sampler_offset
:
964 fprintf(fp
, "(sampler_offset)");
966 case nir_tex_src_texture_handle
:
967 fprintf(fp
, "(texture_handle)");
969 case nir_tex_src_sampler_handle
:
970 fprintf(fp
, "(sampler_handle)");
972 case nir_tex_src_plane
:
973 fprintf(fp
, "(plane)");
977 unreachable("Invalid texture source type");
984 if (instr
->op
== nir_texop_tg4
) {
985 fprintf(fp
, "%u (gather_component), ", instr
->component
);
988 if (nir_tex_instr_has_explicit_tg4_offsets(instr
)) {
989 fprintf(fp
, "{ (%i, %i)", instr
->tg4_offsets
[0][0], instr
->tg4_offsets
[0][1]);
990 for (unsigned i
= 1; i
< 4; ++i
)
991 fprintf(fp
, ", (%i, %i)", instr
->tg4_offsets
[i
][0],
992 instr
->tg4_offsets
[i
][1]);
993 fprintf(fp
, " } (offsets), ");
996 if (!has_texture_deref
) {
997 fprintf(fp
, "%u (texture), ", instr
->texture_index
);
1000 if (!has_sampler_deref
) {
1001 fprintf(fp
, "%u (sampler), ", instr
->sampler_index
);
1006 print_call_instr(nir_call_instr
*instr
, print_state
*state
)
1008 FILE *fp
= state
->fp
;
1010 fprintf(fp
, "call %s ", instr
->callee
->name
);
1012 for (unsigned i
= 0; i
< instr
->num_params
; i
++) {
1016 print_src(&instr
->params
[i
], state
);
1021 print_load_const_instr(nir_load_const_instr
*instr
, print_state
*state
)
1023 FILE *fp
= state
->fp
;
1025 print_ssa_def(&instr
->def
, state
);
1027 fprintf(fp
, " = load_const (");
1029 for (unsigned i
= 0; i
< instr
->def
.num_components
; i
++) {
1034 * we don't really know the type of the constant (if it will be used as a
1035 * float or an int), so just print the raw constant in hex for fidelity
1036 * and then print the float in a comment for readability.
1039 switch (instr
->def
.bit_size
) {
1041 fprintf(fp
, "0x%16" PRIx64
" /* %f */", instr
->value
.u64
[i
],
1042 instr
->value
.f64
[i
]);
1045 fprintf(fp
, "0x%08x /* %f */", instr
->value
.u32
[i
], instr
->value
.f32
[i
]);
1048 fprintf(fp
, "0x%04x /* %f */", instr
->value
.u16
[i
],
1049 _mesa_half_to_float(instr
->value
.u16
[i
]));
1052 fprintf(fp
, "0x%02x", instr
->value
.u8
[i
]);
1055 fprintf(fp
, "%s", instr
->value
.b
[i
] ? "true" : "false");
1064 print_jump_instr(nir_jump_instr
*instr
, print_state
*state
)
1066 FILE *fp
= state
->fp
;
1068 switch (instr
->type
) {
1069 case nir_jump_break
:
1070 fprintf(fp
, "break");
1073 case nir_jump_continue
:
1074 fprintf(fp
, "continue");
1077 case nir_jump_return
:
1078 fprintf(fp
, "return");
1084 print_ssa_undef_instr(nir_ssa_undef_instr
* instr
, print_state
*state
)
1086 FILE *fp
= state
->fp
;
1087 print_ssa_def(&instr
->def
, state
);
1088 fprintf(fp
, " = undefined");
1092 print_phi_instr(nir_phi_instr
*instr
, print_state
*state
)
1094 FILE *fp
= state
->fp
;
1095 print_dest(&instr
->dest
, state
);
1096 fprintf(fp
, " = phi ");
1097 nir_foreach_phi_src(src
, instr
) {
1098 if (&src
->node
!= exec_list_get_head(&instr
->srcs
))
1101 fprintf(fp
, "block_%u: ", src
->pred
->index
);
1102 print_src(&src
->src
, state
);
1107 print_parallel_copy_instr(nir_parallel_copy_instr
*instr
, print_state
*state
)
1109 FILE *fp
= state
->fp
;
1110 nir_foreach_parallel_copy_entry(entry
, instr
) {
1111 if (&entry
->node
!= exec_list_get_head(&instr
->entries
))
1114 print_dest(&entry
->dest
, state
);
1116 print_src(&entry
->src
, state
);
1121 print_instr(const nir_instr
*instr
, print_state
*state
, unsigned tabs
)
1123 FILE *fp
= state
->fp
;
1124 print_tabs(tabs
, fp
);
1126 switch (instr
->type
) {
1127 case nir_instr_type_alu
:
1128 print_alu_instr(nir_instr_as_alu(instr
), state
);
1131 case nir_instr_type_deref
:
1132 print_deref_instr(nir_instr_as_deref(instr
), state
);
1135 case nir_instr_type_call
:
1136 print_call_instr(nir_instr_as_call(instr
), state
);
1139 case nir_instr_type_intrinsic
:
1140 print_intrinsic_instr(nir_instr_as_intrinsic(instr
), state
);
1143 case nir_instr_type_tex
:
1144 print_tex_instr(nir_instr_as_tex(instr
), state
);
1147 case nir_instr_type_load_const
:
1148 print_load_const_instr(nir_instr_as_load_const(instr
), state
);
1151 case nir_instr_type_jump
:
1152 print_jump_instr(nir_instr_as_jump(instr
), state
);
1155 case nir_instr_type_ssa_undef
:
1156 print_ssa_undef_instr(nir_instr_as_ssa_undef(instr
), state
);
1159 case nir_instr_type_phi
:
1160 print_phi_instr(nir_instr_as_phi(instr
), state
);
1163 case nir_instr_type_parallel_copy
:
1164 print_parallel_copy_instr(nir_instr_as_parallel_copy(instr
), state
);
1168 unreachable("Invalid instruction type");
1174 compare_block_index(const void *p1
, const void *p2
)
1176 const nir_block
*block1
= *((const nir_block
**) p1
);
1177 const nir_block
*block2
= *((const nir_block
**) p2
);
1179 return (int) block1
->index
- (int) block2
->index
;
1182 static void print_cf_node(nir_cf_node
*node
, print_state
*state
,
1186 print_block(nir_block
*block
, print_state
*state
, unsigned tabs
)
1188 FILE *fp
= state
->fp
;
1190 print_tabs(tabs
, fp
);
1191 fprintf(fp
, "block block_%u:\n", block
->index
);
1193 /* sort the predecessors by index so we consistently print the same thing */
1196 malloc(block
->predecessors
->entries
* sizeof(nir_block
*));
1199 set_foreach(block
->predecessors
, entry
) {
1200 preds
[i
++] = (nir_block
*) entry
->key
;
1203 qsort(preds
, block
->predecessors
->entries
, sizeof(nir_block
*),
1204 compare_block_index
);
1206 print_tabs(tabs
, fp
);
1207 fprintf(fp
, "/* preds: ");
1208 for (unsigned i
= 0; i
< block
->predecessors
->entries
; i
++) {
1209 fprintf(fp
, "block_%u ", preds
[i
]->index
);
1211 fprintf(fp
, "*/\n");
1215 nir_foreach_instr(instr
, block
) {
1216 print_instr(instr
, state
, tabs
);
1218 print_annotation(state
, instr
);
1221 print_tabs(tabs
, fp
);
1222 fprintf(fp
, "/* succs: ");
1223 for (unsigned i
= 0; i
< 2; i
++)
1224 if (block
->successors
[i
]) {
1225 fprintf(fp
, "block_%u ", block
->successors
[i
]->index
);
1227 fprintf(fp
, "*/\n");
1231 print_if(nir_if
*if_stmt
, print_state
*state
, unsigned tabs
)
1233 FILE *fp
= state
->fp
;
1235 print_tabs(tabs
, fp
);
1237 print_src(&if_stmt
->condition
, state
);
1238 fprintf(fp
, " {\n");
1239 foreach_list_typed(nir_cf_node
, node
, node
, &if_stmt
->then_list
) {
1240 print_cf_node(node
, state
, tabs
+ 1);
1242 print_tabs(tabs
, fp
);
1243 fprintf(fp
, "} else {\n");
1244 foreach_list_typed(nir_cf_node
, node
, node
, &if_stmt
->else_list
) {
1245 print_cf_node(node
, state
, tabs
+ 1);
1247 print_tabs(tabs
, fp
);
1252 print_loop(nir_loop
*loop
, print_state
*state
, unsigned tabs
)
1254 FILE *fp
= state
->fp
;
1256 print_tabs(tabs
, fp
);
1257 fprintf(fp
, "loop {\n");
1258 foreach_list_typed(nir_cf_node
, node
, node
, &loop
->body
) {
1259 print_cf_node(node
, state
, tabs
+ 1);
1261 print_tabs(tabs
, fp
);
1266 print_cf_node(nir_cf_node
*node
, print_state
*state
, unsigned int tabs
)
1268 switch (node
->type
) {
1269 case nir_cf_node_block
:
1270 print_block(nir_cf_node_as_block(node
), state
, tabs
);
1273 case nir_cf_node_if
:
1274 print_if(nir_cf_node_as_if(node
), state
, tabs
);
1277 case nir_cf_node_loop
:
1278 print_loop(nir_cf_node_as_loop(node
), state
, tabs
);
1282 unreachable("Invalid CFG node type");
1287 print_function_impl(nir_function_impl
*impl
, print_state
*state
)
1289 FILE *fp
= state
->fp
;
1291 fprintf(fp
, "\nimpl %s ", impl
->function
->name
);
1295 nir_foreach_variable(var
, &impl
->locals
) {
1297 print_var_decl(var
, state
);
1300 foreach_list_typed(nir_register
, reg
, node
, &impl
->registers
) {
1302 print_register_decl(reg
, state
);
1305 nir_index_blocks(impl
);
1307 foreach_list_typed(nir_cf_node
, node
, node
, &impl
->body
) {
1308 print_cf_node(node
, state
, 1);
1311 fprintf(fp
, "\tblock block_%u:\n}\n\n", impl
->end_block
->index
);
1315 print_function(nir_function
*function
, print_state
*state
)
1317 FILE *fp
= state
->fp
;
1319 fprintf(fp
, "decl_function %s (%d params)", function
->name
,
1320 function
->num_params
);
1324 if (function
->impl
!= NULL
) {
1325 print_function_impl(function
->impl
, state
);
1331 init_print_state(print_state
*state
, nir_shader
*shader
, FILE *fp
)
1334 state
->shader
= shader
;
1335 state
->ht
= _mesa_pointer_hash_table_create(NULL
);
1336 state
->syms
= _mesa_set_create(NULL
, _mesa_key_hash_string
,
1337 _mesa_key_string_equal
);
1342 destroy_print_state(print_state
*state
)
1344 _mesa_hash_table_destroy(state
->ht
, NULL
);
1345 _mesa_set_destroy(state
->syms
, NULL
);
1349 nir_print_shader_annotated(nir_shader
*shader
, FILE *fp
,
1350 struct hash_table
*annotations
)
1353 init_print_state(&state
, shader
, fp
);
1355 state
.annotations
= annotations
;
1357 fprintf(fp
, "shader: %s\n", gl_shader_stage_name(shader
->info
.stage
));
1359 if (shader
->info
.name
)
1360 fprintf(fp
, "name: %s\n", shader
->info
.name
);
1362 if (shader
->info
.label
)
1363 fprintf(fp
, "label: %s\n", shader
->info
.label
);
1365 if (gl_shader_stage_is_compute(shader
->info
.stage
)) {
1366 fprintf(fp
, "local-size: %u, %u, %u%s\n",
1367 shader
->info
.cs
.local_size
[0],
1368 shader
->info
.cs
.local_size
[1],
1369 shader
->info
.cs
.local_size
[2],
1370 shader
->info
.cs
.local_size_variable
? " (variable)" : "");
1371 fprintf(fp
, "shared-size: %u\n", shader
->info
.cs
.shared_size
);
1374 fprintf(fp
, "inputs: %u\n", shader
->num_inputs
);
1375 fprintf(fp
, "outputs: %u\n", shader
->num_outputs
);
1376 fprintf(fp
, "uniforms: %u\n", shader
->num_uniforms
);
1377 fprintf(fp
, "shared: %u\n", shader
->num_shared
);
1378 if (shader
->scratch_size
)
1379 fprintf(fp
, "scratch: %u\n", shader
->scratch_size
);
1381 nir_foreach_variable(var
, &shader
->uniforms
) {
1382 print_var_decl(var
, &state
);
1385 nir_foreach_variable(var
, &shader
->inputs
) {
1386 print_var_decl(var
, &state
);
1389 nir_foreach_variable(var
, &shader
->outputs
) {
1390 print_var_decl(var
, &state
);
1393 nir_foreach_variable(var
, &shader
->shared
) {
1394 print_var_decl(var
, &state
);
1397 nir_foreach_variable(var
, &shader
->globals
) {
1398 print_var_decl(var
, &state
);
1401 nir_foreach_variable(var
, &shader
->system_values
) {
1402 print_var_decl(var
, &state
);
1405 foreach_list_typed(nir_function
, func
, node
, &shader
->functions
) {
1406 print_function(func
, &state
);
1409 destroy_print_state(&state
);
1413 nir_print_shader(nir_shader
*shader
, FILE *fp
)
1415 nir_print_shader_annotated(shader
, fp
, NULL
);
1420 nir_print_instr(const nir_instr
*instr
, FILE *fp
)
1422 print_state state
= {
1425 print_instr(instr
, &state
, 0);
1430 nir_print_deref(const nir_deref_instr
*deref
, FILE *fp
)
1432 print_state state
= {
1435 print_deref_link(deref
, true, &state
);