2 * Copyright © 2014 Intel Corporation
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24 * Connor Abbott (cwabbott0@gmail.com)
29 #include "compiler/shader_enums.h"
30 #include "util/half_float.h"
33 #include <inttypes.h> /* for PRIx64 macro */
36 print_tabs(unsigned num_tabs
, FILE *fp
)
38 for (unsigned i
= 0; i
< num_tabs
; i
++)
45 /** map from nir_variable -> printable name */
46 struct hash_table
*ht
;
48 /** set of names used so far for nir_variables */
51 /* an index used to make new non-conflicting names */
55 * Optional table of annotations mapping nir object
56 * (such as instr or var) to message to print.
58 struct hash_table
*annotations
;
62 print_annotation(print_state
*state
, void *obj
)
64 if (!state
->annotations
)
67 struct hash_entry
*entry
= _mesa_hash_table_search(state
->annotations
, obj
);
71 const char *note
= entry
->data
;
72 _mesa_hash_table_remove(state
->annotations
, entry
);
74 fprintf(stderr
, "%s\n\n", note
);
78 print_register(nir_register
*reg
, print_state
*state
)
81 if (reg
->name
!= NULL
)
82 fprintf(fp
, "/* %s */ ", reg
->name
);
84 fprintf(fp
, "gr%u", reg
->index
);
86 fprintf(fp
, "r%u", reg
->index
);
89 static const char *sizes
[] = { "error", "vec1", "vec2", "vec3", "vec4",
90 "error", "error", "error", "vec8",
91 "error", "error", "error", "error",
92 "error", "error", "error", "vec16"};
95 print_register_decl(nir_register
*reg
, print_state
*state
)
98 fprintf(fp
, "decl_reg %s %u ", sizes
[reg
->num_components
], reg
->bit_size
);
100 fprintf(fp
, "(packed) ");
101 print_register(reg
, state
);
102 if (reg
->num_array_elems
!= 0)
103 fprintf(fp
, "[%u]", reg
->num_array_elems
);
108 print_ssa_def(nir_ssa_def
*def
, print_state
*state
)
110 FILE *fp
= state
->fp
;
111 if (def
->name
!= NULL
)
112 fprintf(fp
, "/* %s */ ", def
->name
);
113 fprintf(fp
, "%s %u ssa_%u", sizes
[def
->num_components
], def
->bit_size
,
118 print_ssa_use(nir_ssa_def
*def
, print_state
*state
)
120 FILE *fp
= state
->fp
;
121 if (def
->name
!= NULL
)
122 fprintf(fp
, "/* %s */ ", def
->name
);
123 fprintf(fp
, "ssa_%u", def
->index
);
126 static void print_src(nir_src
*src
, print_state
*state
);
129 print_reg_src(nir_reg_src
*src
, print_state
*state
)
131 FILE *fp
= state
->fp
;
132 print_register(src
->reg
, state
);
133 if (src
->reg
->num_array_elems
!= 0) {
134 fprintf(fp
, "[%u", src
->base_offset
);
135 if (src
->indirect
!= NULL
) {
137 print_src(src
->indirect
, state
);
144 print_reg_dest(nir_reg_dest
*dest
, print_state
*state
)
146 FILE *fp
= state
->fp
;
147 print_register(dest
->reg
, state
);
148 if (dest
->reg
->num_array_elems
!= 0) {
149 fprintf(fp
, "[%u", dest
->base_offset
);
150 if (dest
->indirect
!= NULL
) {
152 print_src(dest
->indirect
, state
);
159 print_src(nir_src
*src
, print_state
*state
)
162 print_ssa_use(src
->ssa
, state
);
164 print_reg_src(&src
->reg
, state
);
168 print_dest(nir_dest
*dest
, print_state
*state
)
171 print_ssa_def(&dest
->ssa
, state
);
173 print_reg_dest(&dest
->reg
, state
);
177 print_alu_src(nir_alu_instr
*instr
, unsigned src
, print_state
*state
)
179 FILE *fp
= state
->fp
;
181 if (instr
->src
[src
].negate
)
183 if (instr
->src
[src
].abs
)
186 print_src(&instr
->src
[src
].src
, state
);
188 bool print_swizzle
= false;
189 nir_component_mask_t used_channels
= 0;
191 for (unsigned i
= 0; i
< NIR_MAX_VEC_COMPONENTS
; i
++) {
192 if (!nir_alu_instr_channel_used(instr
, src
, i
))
197 if (instr
->src
[src
].swizzle
[i
] != i
) {
198 print_swizzle
= true;
203 unsigned live_channels
= nir_src_num_components(instr
->src
[src
].src
);
205 if (print_swizzle
|| used_channels
!= live_channels
) {
207 for (unsigned i
= 0; i
< NIR_MAX_VEC_COMPONENTS
; i
++) {
208 if (!nir_alu_instr_channel_used(instr
, src
, i
))
211 fprintf(fp
, "%c", "xyzw"[instr
->src
[src
].swizzle
[i
]]);
215 if (instr
->src
[src
].abs
)
220 print_alu_dest(nir_alu_dest
*dest
, print_state
*state
)
222 FILE *fp
= state
->fp
;
223 /* we're going to print the saturate modifier later, after the opcode */
225 print_dest(&dest
->dest
, state
);
227 if (!dest
->dest
.is_ssa
&&
228 dest
->write_mask
!= (1 << dest
->dest
.reg
.reg
->num_components
) - 1) {
230 for (unsigned i
= 0; i
< NIR_MAX_VEC_COMPONENTS
; i
++)
231 if ((dest
->write_mask
>> i
) & 1)
232 fprintf(fp
, "%c", "xyzw"[i
]);
237 print_alu_instr(nir_alu_instr
*instr
, print_state
*state
)
239 FILE *fp
= state
->fp
;
241 print_alu_dest(&instr
->dest
, state
);
243 fprintf(fp
, " = %s", nir_op_infos
[instr
->op
].name
);
246 if (instr
->dest
.saturate
)
250 for (unsigned i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++) {
254 print_alu_src(instr
, i
, state
);
259 get_var_name(nir_variable
*var
, print_state
*state
)
261 if (state
->ht
== NULL
)
262 return var
->name
? var
->name
: "unnamed";
266 struct hash_entry
*entry
= _mesa_hash_table_search(state
->ht
, var
);
271 if (var
->name
== NULL
) {
272 name
= ralloc_asprintf(state
->syms
, "@%u", state
->index
++);
274 struct set_entry
*set_entry
= _mesa_set_search(state
->syms
, var
->name
);
275 if (set_entry
!= NULL
) {
276 /* we have a collision with another name, append an @ + a unique
278 name
= ralloc_asprintf(state
->syms
, "%s@%u", var
->name
,
281 /* Mark this one as seen */
282 _mesa_set_add(state
->syms
, var
->name
);
287 _mesa_hash_table_insert(state
->ht
, var
, name
);
293 print_constant(nir_constant
*c
, const struct glsl_type
*type
, print_state
*state
)
295 FILE *fp
= state
->fp
;
296 const unsigned rows
= glsl_get_vector_elements(type
);
297 const unsigned cols
= glsl_get_matrix_columns(type
);
300 switch (glsl_get_base_type(type
)) {
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
[0].b
[i
] ? "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
[0].u8
[i
]);
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
[0].u16
[i
]);
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
[0].u32
[i
]);
344 case GLSL_TYPE_FLOAT16
:
345 for (i
= 0; i
< cols
; i
++) {
346 for (j
= 0; j
< rows
; j
++) {
347 if (i
+ j
> 0) fprintf(fp
, ", ");
348 fprintf(fp
, "%f", _mesa_half_to_float(c
->values
[i
].u16
[j
]));
353 case GLSL_TYPE_FLOAT
:
354 for (i
= 0; i
< cols
; i
++) {
355 for (j
= 0; j
< rows
; j
++) {
356 if (i
+ j
> 0) fprintf(fp
, ", ");
357 fprintf(fp
, "%f", c
->values
[i
].f32
[j
]);
362 case GLSL_TYPE_DOUBLE
:
363 for (i
= 0; i
< cols
; i
++) {
364 for (j
= 0; j
< rows
; j
++) {
365 if (i
+ j
> 0) fprintf(fp
, ", ");
366 fprintf(fp
, "%f", c
->values
[i
].f64
[j
]);
371 case GLSL_TYPE_UINT64
:
372 case GLSL_TYPE_INT64
:
373 /* Only float base types can be matrices. */
376 for (i
= 0; i
< cols
; i
++) {
377 if (i
> 0) fprintf(fp
, ", ");
378 fprintf(fp
, "0x%08" PRIx64
, c
->values
[0].u64
[i
]);
382 case GLSL_TYPE_STRUCT
:
383 for (i
= 0; i
< c
->num_elements
; i
++) {
384 if (i
> 0) fprintf(fp
, ", ");
386 print_constant(c
->elements
[i
], glsl_get_struct_field(type
, i
), state
);
391 case GLSL_TYPE_ARRAY
:
392 for (i
= 0; i
< c
->num_elements
; i
++) {
393 if (i
> 0) fprintf(fp
, ", ");
395 print_constant(c
->elements
[i
], glsl_get_array_element(type
), state
);
401 unreachable("not reached");
406 get_variable_mode_str(nir_variable_mode mode
, bool want_local_global_mode
)
409 case nir_var_shader_in
:
411 case nir_var_shader_out
:
413 case nir_var_uniform
:
415 case nir_var_shader_storage
:
416 return "shader_storage";
417 case nir_var_system_value
:
422 return want_local_global_mode
? "global" : "";
424 return want_local_global_mode
? "local" : "";
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_shader_storage
) {
507 const char *loc
= NULL
;
510 switch (state
->shader
->info
.stage
) {
511 case MESA_SHADER_VERTEX
:
512 if (var
->data
.mode
== nir_var_shader_in
)
513 loc
= gl_vert_attrib_name(var
->data
.location
);
514 else if (var
->data
.mode
== nir_var_shader_out
)
515 loc
= gl_varying_slot_name(var
->data
.location
);
517 case MESA_SHADER_GEOMETRY
:
518 if ((var
->data
.mode
== nir_var_shader_in
) ||
519 (var
->data
.mode
== nir_var_shader_out
))
520 loc
= gl_varying_slot_name(var
->data
.location
);
522 case MESA_SHADER_FRAGMENT
:
523 if (var
->data
.mode
== nir_var_shader_in
)
524 loc
= gl_varying_slot_name(var
->data
.location
);
525 else if (var
->data
.mode
== nir_var_shader_out
)
526 loc
= gl_frag_result_name(var
->data
.location
);
528 case MESA_SHADER_TESS_CTRL
:
529 case MESA_SHADER_TESS_EVAL
:
530 case MESA_SHADER_COMPUTE
:
537 snprintf(buf
, sizeof(buf
), "%u", var
->data
.location
);
541 /* For shader I/O vars that have been split to components or packed,
542 * print the fractional location within the input/output.
544 unsigned int num_components
=
545 glsl_get_components(glsl_without_array(var
->type
));
546 const char *components
= NULL
;
547 char components_local
[6] = {'.' /* the rest is 0-filled */};
548 switch (var
->data
.mode
) {
549 case nir_var_shader_in
:
550 case nir_var_shader_out
:
551 if (num_components
< 4 && num_components
!= 0) {
552 const char *xyzw
= "xyzw";
553 for (int i
= 0; i
< num_components
; i
++)
554 components_local
[i
+ 1] = xyzw
[i
+ var
->data
.location_frac
];
556 components
= components_local
;
563 fprintf(fp
, " (%s%s, %u, %u)%s", loc
,
564 components
? components
: "",
565 var
->data
.driver_location
, var
->data
.binding
,
566 var
->data
.compact
? " compact" : "");
569 if (var
->constant_initializer
) {
570 fprintf(fp
, " = { ");
571 print_constant(var
->constant_initializer
, var
->type
, state
);
576 print_annotation(state
, var
);
580 print_deref_link(nir_deref_instr
*instr
, bool whole_chain
, print_state
*state
)
582 FILE *fp
= state
->fp
;
584 if (instr
->deref_type
== nir_deref_type_var
) {
585 fprintf(fp
, "%s", get_var_name(instr
->var
, state
));
587 } else if (instr
->deref_type
== nir_deref_type_cast
) {
588 fprintf(fp
, "(%s *)", glsl_get_type_name(instr
->type
));
589 print_src(&instr
->parent
, state
);
593 assert(instr
->parent
.is_ssa
);
594 nir_deref_instr
*parent
=
595 nir_instr_as_deref(instr
->parent
.ssa
->parent_instr
);
597 /* Is the parent we're going to print a bare cast? */
598 const bool is_parent_cast
=
599 whole_chain
&& parent
->deref_type
== nir_deref_type_cast
;
601 /* If we're not printing the whole chain, the parent we print will be a SSA
602 * value that represents a pointer. The only deref type that naturally
603 * gives a pointer is a cast.
605 const bool is_parent_pointer
=
606 !whole_chain
|| parent
->deref_type
== nir_deref_type_cast
;
608 /* Struct derefs have a nice syntax that works on pointers, arrays derefs
611 const bool need_deref
=
612 is_parent_pointer
&& instr
->deref_type
!= nir_deref_type_struct
;
614 /* Cast need extra parens and so * dereferences */
615 if (is_parent_cast
|| need_deref
)
622 print_deref_link(parent
, whole_chain
, state
);
624 print_src(&instr
->parent
, state
);
627 if (is_parent_cast
|| need_deref
)
630 switch (instr
->deref_type
) {
631 case nir_deref_type_struct
:
632 fprintf(fp
, "%s%s", is_parent_pointer
? "->" : ".",
633 glsl_get_struct_elem_name(parent
->type
, instr
->strct
.index
));
636 case nir_deref_type_array
: {
637 nir_const_value
*const_index
= nir_src_as_const_value(instr
->arr
.index
);
639 fprintf(fp
, "[%u]", const_index
->u32
[0]);
642 print_src(&instr
->arr
.index
, state
);
648 case nir_deref_type_array_wildcard
:
653 unreachable("Invalid deref instruction type");
658 print_deref_instr(nir_deref_instr
*instr
, print_state
*state
)
660 FILE *fp
= state
->fp
;
662 print_dest(&instr
->dest
, state
);
664 switch (instr
->deref_type
) {
665 case nir_deref_type_var
:
666 fprintf(fp
, " = deref_var ");
668 case nir_deref_type_array
:
669 case nir_deref_type_array_wildcard
:
670 fprintf(fp
, " = deref_array ");
672 case nir_deref_type_struct
:
673 fprintf(fp
, " = deref_struct ");
675 case nir_deref_type_cast
:
676 fprintf(fp
, " = deref_cast ");
679 unreachable("Invalid deref instruction type");
682 /* Only casts naturally return a pointer type */
683 if (instr
->deref_type
!= nir_deref_type_cast
)
686 print_deref_link(instr
, false, state
);
688 fprintf(fp
, " (%s %s) ",
689 get_variable_mode_str(instr
->mode
, true),
690 glsl_get_type_name(instr
->type
));
692 if (instr
->deref_type
!= nir_deref_type_var
&&
693 instr
->deref_type
!= nir_deref_type_cast
) {
694 /* Print the entire chain as a comment */
696 print_deref_link(instr
, true, state
);
702 print_intrinsic_instr(nir_intrinsic_instr
*instr
, print_state
*state
)
704 const nir_intrinsic_info
*info
= &nir_intrinsic_infos
[instr
->intrinsic
];
705 unsigned num_srcs
= info
->num_srcs
;
706 FILE *fp
= state
->fp
;
708 if (info
->has_dest
) {
709 print_dest(&instr
->dest
, state
);
713 fprintf(fp
, "intrinsic %s (", info
->name
);
715 for (unsigned i
= 0; i
< num_srcs
; i
++) {
719 print_src(&instr
->src
[i
], state
);
724 for (unsigned i
= 0; i
< info
->num_indices
; i
++) {
728 fprintf(fp
, "%d", instr
->const_index
[i
]);
733 static const char *index_name
[NIR_INTRINSIC_NUM_INDEX_FLAGS
] = {
734 [NIR_INTRINSIC_BASE
] = "base",
735 [NIR_INTRINSIC_WRMASK
] = "wrmask",
736 [NIR_INTRINSIC_STREAM_ID
] = "stream-id",
737 [NIR_INTRINSIC_UCP_ID
] = "ucp-id",
738 [NIR_INTRINSIC_RANGE
] = "range",
739 [NIR_INTRINSIC_DESC_SET
] = "desc-set",
740 [NIR_INTRINSIC_BINDING
] = "binding",
741 [NIR_INTRINSIC_COMPONENT
] = "component",
742 [NIR_INTRINSIC_INTERP_MODE
] = "interp_mode",
743 [NIR_INTRINSIC_REDUCTION_OP
] = "reduction_op",
744 [NIR_INTRINSIC_CLUSTER_SIZE
] = "cluster_size",
745 [NIR_INTRINSIC_PARAM_IDX
] = "param_idx",
746 [NIR_INTRINSIC_IMAGE_DIM
] = "image_dim",
747 [NIR_INTRINSIC_IMAGE_ARRAY
] = "image_array",
748 [NIR_INTRINSIC_ACCESS
] = "access",
749 [NIR_INTRINSIC_FORMAT
] = "format",
750 [NIR_INTRINSIC_ALIGN_MUL
] = "align_mul",
751 [NIR_INTRINSIC_ALIGN_OFFSET
] = "align_offset",
753 for (unsigned idx
= 1; idx
< NIR_INTRINSIC_NUM_INDEX_FLAGS
; idx
++) {
754 if (!info
->index_map
[idx
])
757 if (idx
== NIR_INTRINSIC_WRMASK
) {
758 /* special case wrmask to show it as a writemask.. */
759 unsigned wrmask
= nir_intrinsic_write_mask(instr
);
760 fprintf(fp
, " wrmask=");
761 for (unsigned i
= 0; i
< 4; i
++)
762 if ((wrmask
>> i
) & 1)
763 fprintf(fp
, "%c", "xyzw"[i
]);
764 } else if (idx
== NIR_INTRINSIC_REDUCTION_OP
) {
765 nir_op reduction_op
= nir_intrinsic_reduction_op(instr
);
766 fprintf(fp
, " reduction_op=%s", nir_op_infos
[reduction_op
].name
);
767 } else if (idx
== NIR_INTRINSIC_IMAGE_DIM
) {
768 static const char *dim_name
[] = {
769 [GLSL_SAMPLER_DIM_1D
] = "1D",
770 [GLSL_SAMPLER_DIM_2D
] = "2D",
771 [GLSL_SAMPLER_DIM_3D
] = "3D",
772 [GLSL_SAMPLER_DIM_CUBE
] = "Cube",
773 [GLSL_SAMPLER_DIM_RECT
] = "Rect",
774 [GLSL_SAMPLER_DIM_BUF
] = "Buf",
775 [GLSL_SAMPLER_DIM_MS
] = "2D-MSAA",
776 [GLSL_SAMPLER_DIM_SUBPASS
] = "Subpass",
777 [GLSL_SAMPLER_DIM_SUBPASS_MS
] = "Subpass-MSAA",
779 enum glsl_sampler_dim dim
= nir_intrinsic_image_dim(instr
);
780 assert(dim
< ARRAY_SIZE(dim_name
) && dim_name
[dim
]);
781 fprintf(fp
, " image_dim=%s", dim_name
[dim
]);
782 } else if (idx
== NIR_INTRINSIC_IMAGE_ARRAY
) {
783 bool array
= nir_intrinsic_image_dim(instr
);
784 fprintf(fp
, " image_dim=%s", array
? "true" : "false");
786 unsigned off
= info
->index_map
[idx
] - 1;
787 assert(index_name
[idx
]); /* forgot to update index_name table? */
788 fprintf(fp
, " %s=%d", index_name
[idx
], instr
->const_index
[off
]);
796 struct exec_list
*var_list
= NULL
;
798 switch (instr
->intrinsic
) {
799 case nir_intrinsic_load_uniform
:
800 var_list
= &state
->shader
->uniforms
;
802 case nir_intrinsic_load_input
:
803 case nir_intrinsic_load_per_vertex_input
:
804 var_list
= &state
->shader
->inputs
;
806 case nir_intrinsic_load_output
:
807 case nir_intrinsic_store_output
:
808 case nir_intrinsic_store_per_vertex_output
:
809 var_list
= &state
->shader
->outputs
;
815 nir_foreach_variable(var
, var_list
) {
816 if ((var
->data
.driver_location
== nir_intrinsic_base(instr
)) &&
817 (instr
->intrinsic
== nir_intrinsic_load_uniform
||
818 var
->data
.location_frac
== nir_intrinsic_component(instr
)) &&
820 fprintf(fp
, "\t/* %s */", var
->name
);
827 print_tex_instr(nir_tex_instr
*instr
, print_state
*state
)
829 FILE *fp
= state
->fp
;
831 print_dest(&instr
->dest
, state
);
851 case nir_texop_txf_ms
:
852 fprintf(fp
, "txf_ms ");
854 case nir_texop_txf_ms_mcs
:
855 fprintf(fp
, "txf_ms_mcs ");
866 case nir_texop_query_levels
:
867 fprintf(fp
, "query_levels ");
869 case nir_texop_texture_samples
:
870 fprintf(fp
, "texture_samples ");
872 case nir_texop_samples_identical
:
873 fprintf(fp
, "samples_identical ");
876 unreachable("Invalid texture operation");
880 bool has_texture_deref
= false, has_sampler_deref
= false;
881 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++) {
882 print_src(&instr
->src
[i
].src
, state
);
886 switch(instr
->src
[i
].src_type
) {
887 case nir_tex_src_coord
:
888 fprintf(fp
, "(coord)");
890 case nir_tex_src_projector
:
891 fprintf(fp
, "(projector)");
893 case nir_tex_src_comparator
:
894 fprintf(fp
, "(comparator)");
896 case nir_tex_src_offset
:
897 fprintf(fp
, "(offset)");
899 case nir_tex_src_bias
:
900 fprintf(fp
, "(bias)");
902 case nir_tex_src_lod
:
903 fprintf(fp
, "(lod)");
905 case nir_tex_src_min_lod
:
906 fprintf(fp
, "(min_lod)");
908 case nir_tex_src_ms_index
:
909 fprintf(fp
, "(ms_index)");
911 case nir_tex_src_ms_mcs
:
912 fprintf(fp
, "(ms_mcs)");
914 case nir_tex_src_ddx
:
915 fprintf(fp
, "(ddx)");
917 case nir_tex_src_ddy
:
918 fprintf(fp
, "(ddy)");
920 case nir_tex_src_texture_deref
:
921 has_texture_deref
= true;
922 fprintf(fp
, "(texture_deref)");
924 case nir_tex_src_sampler_deref
:
925 has_sampler_deref
= true;
926 fprintf(fp
, "(sampler_deref)");
928 case nir_tex_src_texture_offset
:
929 fprintf(fp
, "(texture_offset)");
931 case nir_tex_src_sampler_offset
:
932 fprintf(fp
, "(sampler_offset)");
934 case nir_tex_src_plane
:
935 fprintf(fp
, "(plane)");
939 unreachable("Invalid texture source type");
946 if (instr
->op
== nir_texop_tg4
) {
947 fprintf(fp
, "%u (gather_component), ", instr
->component
);
950 if (!has_texture_deref
) {
951 fprintf(fp
, "%u (texture), ", instr
->texture_index
);
954 if (!has_sampler_deref
) {
955 fprintf(fp
, "%u (sampler), ", instr
->sampler_index
);
960 print_call_instr(nir_call_instr
*instr
, print_state
*state
)
962 FILE *fp
= state
->fp
;
964 fprintf(fp
, "call %s ", instr
->callee
->name
);
966 for (unsigned i
= 0; i
< instr
->num_params
; i
++) {
970 print_src(&instr
->params
[i
], state
);
975 print_load_const_instr(nir_load_const_instr
*instr
, print_state
*state
)
977 FILE *fp
= state
->fp
;
979 print_ssa_def(&instr
->def
, state
);
981 fprintf(fp
, " = load_const (");
983 for (unsigned i
= 0; i
< instr
->def
.num_components
; i
++) {
988 * we don't really know the type of the constant (if it will be used as a
989 * float or an int), so just print the raw constant in hex for fidelity
990 * and then print the float in a comment for readability.
993 switch (instr
->def
.bit_size
) {
995 fprintf(fp
, "0x%16" PRIx64
" /* %f */", instr
->value
.u64
[i
],
996 instr
->value
.f64
[i
]);
999 fprintf(fp
, "0x%08x /* %f */", instr
->value
.u32
[i
], instr
->value
.f32
[i
]);
1002 fprintf(fp
, "0x%04x /* %f */", instr
->value
.u16
[i
],
1003 _mesa_half_to_float(instr
->value
.u16
[i
]));
1006 fprintf(fp
, "0x%02x", instr
->value
.u8
[i
]);
1009 fprintf(fp
, "%s", instr
->value
.b
[i
] ? "true" : "false");
1018 print_jump_instr(nir_jump_instr
*instr
, print_state
*state
)
1020 FILE *fp
= state
->fp
;
1022 switch (instr
->type
) {
1023 case nir_jump_break
:
1024 fprintf(fp
, "break");
1027 case nir_jump_continue
:
1028 fprintf(fp
, "continue");
1031 case nir_jump_return
:
1032 fprintf(fp
, "return");
1038 print_ssa_undef_instr(nir_ssa_undef_instr
* instr
, print_state
*state
)
1040 FILE *fp
= state
->fp
;
1041 print_ssa_def(&instr
->def
, state
);
1042 fprintf(fp
, " = undefined");
1046 print_phi_instr(nir_phi_instr
*instr
, print_state
*state
)
1048 FILE *fp
= state
->fp
;
1049 print_dest(&instr
->dest
, state
);
1050 fprintf(fp
, " = phi ");
1051 nir_foreach_phi_src(src
, instr
) {
1052 if (&src
->node
!= exec_list_get_head(&instr
->srcs
))
1055 fprintf(fp
, "block_%u: ", src
->pred
->index
);
1056 print_src(&src
->src
, state
);
1061 print_parallel_copy_instr(nir_parallel_copy_instr
*instr
, print_state
*state
)
1063 FILE *fp
= state
->fp
;
1064 nir_foreach_parallel_copy_entry(entry
, instr
) {
1065 if (&entry
->node
!= exec_list_get_head(&instr
->entries
))
1068 print_dest(&entry
->dest
, state
);
1070 print_src(&entry
->src
, state
);
1075 print_instr(const nir_instr
*instr
, print_state
*state
, unsigned tabs
)
1077 FILE *fp
= state
->fp
;
1078 print_tabs(tabs
, fp
);
1080 switch (instr
->type
) {
1081 case nir_instr_type_alu
:
1082 print_alu_instr(nir_instr_as_alu(instr
), state
);
1085 case nir_instr_type_deref
:
1086 print_deref_instr(nir_instr_as_deref(instr
), state
);
1089 case nir_instr_type_call
:
1090 print_call_instr(nir_instr_as_call(instr
), state
);
1093 case nir_instr_type_intrinsic
:
1094 print_intrinsic_instr(nir_instr_as_intrinsic(instr
), state
);
1097 case nir_instr_type_tex
:
1098 print_tex_instr(nir_instr_as_tex(instr
), state
);
1101 case nir_instr_type_load_const
:
1102 print_load_const_instr(nir_instr_as_load_const(instr
), state
);
1105 case nir_instr_type_jump
:
1106 print_jump_instr(nir_instr_as_jump(instr
), state
);
1109 case nir_instr_type_ssa_undef
:
1110 print_ssa_undef_instr(nir_instr_as_ssa_undef(instr
), state
);
1113 case nir_instr_type_phi
:
1114 print_phi_instr(nir_instr_as_phi(instr
), state
);
1117 case nir_instr_type_parallel_copy
:
1118 print_parallel_copy_instr(nir_instr_as_parallel_copy(instr
), state
);
1122 unreachable("Invalid instruction type");
1128 compare_block_index(const void *p1
, const void *p2
)
1130 const nir_block
*block1
= *((const nir_block
**) p1
);
1131 const nir_block
*block2
= *((const nir_block
**) p2
);
1133 return (int) block1
->index
- (int) block2
->index
;
1136 static void print_cf_node(nir_cf_node
*node
, print_state
*state
,
1140 print_block(nir_block
*block
, print_state
*state
, unsigned tabs
)
1142 FILE *fp
= state
->fp
;
1144 print_tabs(tabs
, fp
);
1145 fprintf(fp
, "block block_%u:\n", block
->index
);
1147 /* sort the predecessors by index so we consistently print the same thing */
1150 malloc(block
->predecessors
->entries
* sizeof(nir_block
*));
1153 set_foreach(block
->predecessors
, entry
) {
1154 preds
[i
++] = (nir_block
*) entry
->key
;
1157 qsort(preds
, block
->predecessors
->entries
, sizeof(nir_block
*),
1158 compare_block_index
);
1160 print_tabs(tabs
, fp
);
1161 fprintf(fp
, "/* preds: ");
1162 for (unsigned i
= 0; i
< block
->predecessors
->entries
; i
++) {
1163 fprintf(fp
, "block_%u ", preds
[i
]->index
);
1165 fprintf(fp
, "*/\n");
1169 nir_foreach_instr(instr
, block
) {
1170 print_instr(instr
, state
, tabs
);
1172 print_annotation(state
, instr
);
1175 print_tabs(tabs
, fp
);
1176 fprintf(fp
, "/* succs: ");
1177 for (unsigned i
= 0; i
< 2; i
++)
1178 if (block
->successors
[i
]) {
1179 fprintf(fp
, "block_%u ", block
->successors
[i
]->index
);
1181 fprintf(fp
, "*/\n");
1185 print_if(nir_if
*if_stmt
, print_state
*state
, unsigned tabs
)
1187 FILE *fp
= state
->fp
;
1189 print_tabs(tabs
, fp
);
1191 print_src(&if_stmt
->condition
, state
);
1192 fprintf(fp
, " {\n");
1193 foreach_list_typed(nir_cf_node
, node
, node
, &if_stmt
->then_list
) {
1194 print_cf_node(node
, state
, tabs
+ 1);
1196 print_tabs(tabs
, fp
);
1197 fprintf(fp
, "} else {\n");
1198 foreach_list_typed(nir_cf_node
, node
, node
, &if_stmt
->else_list
) {
1199 print_cf_node(node
, state
, tabs
+ 1);
1201 print_tabs(tabs
, fp
);
1206 print_loop(nir_loop
*loop
, print_state
*state
, unsigned tabs
)
1208 FILE *fp
= state
->fp
;
1210 print_tabs(tabs
, fp
);
1211 fprintf(fp
, "loop {\n");
1212 foreach_list_typed(nir_cf_node
, node
, node
, &loop
->body
) {
1213 print_cf_node(node
, state
, tabs
+ 1);
1215 print_tabs(tabs
, fp
);
1220 print_cf_node(nir_cf_node
*node
, print_state
*state
, unsigned int tabs
)
1222 switch (node
->type
) {
1223 case nir_cf_node_block
:
1224 print_block(nir_cf_node_as_block(node
), state
, tabs
);
1227 case nir_cf_node_if
:
1228 print_if(nir_cf_node_as_if(node
), state
, tabs
);
1231 case nir_cf_node_loop
:
1232 print_loop(nir_cf_node_as_loop(node
), state
, tabs
);
1236 unreachable("Invalid CFG node type");
1241 print_function_impl(nir_function_impl
*impl
, print_state
*state
)
1243 FILE *fp
= state
->fp
;
1245 fprintf(fp
, "\nimpl %s ", impl
->function
->name
);
1249 nir_foreach_variable(var
, &impl
->locals
) {
1251 print_var_decl(var
, state
);
1254 foreach_list_typed(nir_register
, reg
, node
, &impl
->registers
) {
1256 print_register_decl(reg
, state
);
1259 nir_index_blocks(impl
);
1261 foreach_list_typed(nir_cf_node
, node
, node
, &impl
->body
) {
1262 print_cf_node(node
, state
, 1);
1265 fprintf(fp
, "\tblock block_%u:\n}\n\n", impl
->end_block
->index
);
1269 print_function(nir_function
*function
, print_state
*state
)
1271 FILE *fp
= state
->fp
;
1273 fprintf(fp
, "decl_function %s (%d params)", function
->name
,
1274 function
->num_params
);
1278 if (function
->impl
!= NULL
) {
1279 print_function_impl(function
->impl
, state
);
1285 init_print_state(print_state
*state
, nir_shader
*shader
, FILE *fp
)
1288 state
->shader
= shader
;
1289 state
->ht
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
1290 _mesa_key_pointer_equal
);
1291 state
->syms
= _mesa_set_create(NULL
, _mesa_key_hash_string
,
1292 _mesa_key_string_equal
);
1297 destroy_print_state(print_state
*state
)
1299 _mesa_hash_table_destroy(state
->ht
, NULL
);
1300 _mesa_set_destroy(state
->syms
, NULL
);
1304 nir_print_shader_annotated(nir_shader
*shader
, FILE *fp
,
1305 struct hash_table
*annotations
)
1308 init_print_state(&state
, shader
, fp
);
1310 state
.annotations
= annotations
;
1312 fprintf(fp
, "shader: %s\n", gl_shader_stage_name(shader
->info
.stage
));
1314 if (shader
->info
.name
)
1315 fprintf(fp
, "name: %s\n", shader
->info
.name
);
1317 if (shader
->info
.label
)
1318 fprintf(fp
, "label: %s\n", shader
->info
.label
);
1320 switch (shader
->info
.stage
) {
1321 case MESA_SHADER_COMPUTE
:
1322 fprintf(fp
, "local-size: %u, %u, %u%s\n",
1323 shader
->info
.cs
.local_size
[0],
1324 shader
->info
.cs
.local_size
[1],
1325 shader
->info
.cs
.local_size
[2],
1326 shader
->info
.cs
.local_size_variable
? " (variable)" : "");
1327 fprintf(fp
, "shared-size: %u\n", shader
->info
.cs
.shared_size
);
1333 fprintf(fp
, "inputs: %u\n", shader
->num_inputs
);
1334 fprintf(fp
, "outputs: %u\n", shader
->num_outputs
);
1335 fprintf(fp
, "uniforms: %u\n", shader
->num_uniforms
);
1336 fprintf(fp
, "shared: %u\n", shader
->num_shared
);
1338 nir_foreach_variable(var
, &shader
->uniforms
) {
1339 print_var_decl(var
, &state
);
1342 nir_foreach_variable(var
, &shader
->inputs
) {
1343 print_var_decl(var
, &state
);
1346 nir_foreach_variable(var
, &shader
->outputs
) {
1347 print_var_decl(var
, &state
);
1350 nir_foreach_variable(var
, &shader
->shared
) {
1351 print_var_decl(var
, &state
);
1354 nir_foreach_variable(var
, &shader
->globals
) {
1355 print_var_decl(var
, &state
);
1358 nir_foreach_variable(var
, &shader
->system_values
) {
1359 print_var_decl(var
, &state
);
1362 foreach_list_typed(nir_register
, reg
, node
, &shader
->registers
) {
1363 print_register_decl(reg
, &state
);
1366 foreach_list_typed(nir_function
, func
, node
, &shader
->functions
) {
1367 print_function(func
, &state
);
1370 destroy_print_state(&state
);
1374 nir_print_shader(nir_shader
*shader
, FILE *fp
)
1376 nir_print_shader_annotated(shader
, fp
, NULL
);
1381 nir_print_instr(const nir_instr
*instr
, FILE *fp
)
1383 print_state state
= {
1386 print_instr(instr
, &state
, 0);