2 * Copyright © 2014 Intel Corporation
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24 * Connor Abbott (cwabbott0@gmail.com)
29 #include "compiler/shader_enums.h"
30 #include "util/half_float.h"
33 #include <inttypes.h> /* for PRIx64 macro */
36 print_tabs(unsigned num_tabs
, FILE *fp
)
38 for (unsigned i
= 0; i
< num_tabs
; i
++)
45 /** map from nir_variable -> printable name */
46 struct hash_table
*ht
;
48 /** set of names used so far for nir_variables */
51 /* an index used to make new non-conflicting names */
55 * Optional table of annotations mapping nir object
56 * (such as instr or var) to message to print.
58 struct hash_table
*annotations
;
62 print_annotation(print_state
*state
, void *obj
)
64 if (!state
->annotations
)
67 struct hash_entry
*entry
= _mesa_hash_table_search(state
->annotations
, obj
);
71 const char *note
= entry
->data
;
72 _mesa_hash_table_remove(state
->annotations
, entry
);
74 fprintf(stderr
, "%s\n\n", note
);
78 print_register(nir_register
*reg
, print_state
*state
)
81 if (reg
->name
!= NULL
)
82 fprintf(fp
, "/* %s */ ", reg
->name
);
84 fprintf(fp
, "gr%u", reg
->index
);
86 fprintf(fp
, "r%u", reg
->index
);
89 static const char *sizes
[] = { "error", "vec1", "vec2", "vec3", "vec4",
90 "error", "error", "error", "vec8",
91 "error", "error", "error", "vec16"};
94 print_register_decl(nir_register
*reg
, print_state
*state
)
97 fprintf(fp
, "decl_reg %s %u ", sizes
[reg
->num_components
], reg
->bit_size
);
99 fprintf(fp
, "(packed) ");
100 print_register(reg
, state
);
101 if (reg
->num_array_elems
!= 0)
102 fprintf(fp
, "[%u]", reg
->num_array_elems
);
107 print_ssa_def(nir_ssa_def
*def
, print_state
*state
)
109 FILE *fp
= state
->fp
;
110 if (def
->name
!= NULL
)
111 fprintf(fp
, "/* %s */ ", def
->name
);
112 fprintf(fp
, "%s %u ssa_%u", sizes
[def
->num_components
], def
->bit_size
,
117 print_ssa_use(nir_ssa_def
*def
, print_state
*state
)
119 FILE *fp
= state
->fp
;
120 if (def
->name
!= NULL
)
121 fprintf(fp
, "/* %s */ ", def
->name
);
122 fprintf(fp
, "ssa_%u", def
->index
);
125 static void print_src(nir_src
*src
, print_state
*state
);
128 print_reg_src(nir_reg_src
*src
, print_state
*state
)
130 FILE *fp
= state
->fp
;
131 print_register(src
->reg
, state
);
132 if (src
->reg
->num_array_elems
!= 0) {
133 fprintf(fp
, "[%u", src
->base_offset
);
134 if (src
->indirect
!= NULL
) {
136 print_src(src
->indirect
, state
);
143 print_reg_dest(nir_reg_dest
*dest
, print_state
*state
)
145 FILE *fp
= state
->fp
;
146 print_register(dest
->reg
, state
);
147 if (dest
->reg
->num_array_elems
!= 0) {
148 fprintf(fp
, "[%u", dest
->base_offset
);
149 if (dest
->indirect
!= NULL
) {
151 print_src(dest
->indirect
, state
);
158 print_src(nir_src
*src
, print_state
*state
)
161 print_ssa_use(src
->ssa
, state
);
163 print_reg_src(&src
->reg
, state
);
167 print_dest(nir_dest
*dest
, print_state
*state
)
170 print_ssa_def(&dest
->ssa
, state
);
172 print_reg_dest(&dest
->reg
, state
);
176 print_alu_src(nir_alu_instr
*instr
, unsigned src
, print_state
*state
)
178 FILE *fp
= state
->fp
;
180 if (instr
->src
[src
].negate
)
182 if (instr
->src
[src
].abs
)
185 print_src(&instr
->src
[src
].src
, state
);
187 bool print_swizzle
= false;
188 unsigned used_channels
= 0;
190 for (unsigned i
= 0; i
< 4; i
++) {
191 if (!nir_alu_instr_channel_used(instr
, src
, i
))
196 if (instr
->src
[src
].swizzle
[i
] != i
) {
197 print_swizzle
= true;
202 unsigned live_channels
= nir_src_num_components(instr
->src
[src
].src
);
204 if (print_swizzle
|| used_channels
!= live_channels
) {
206 for (unsigned i
= 0; i
< 4; i
++) {
207 if (!nir_alu_instr_channel_used(instr
, src
, i
))
210 fprintf(fp
, "%c", "xyzw"[instr
->src
[src
].swizzle
[i
]]);
214 if (instr
->src
[src
].abs
)
219 print_alu_dest(nir_alu_dest
*dest
, print_state
*state
)
221 FILE *fp
= state
->fp
;
222 /* we're going to print the saturate modifier later, after the opcode */
224 print_dest(&dest
->dest
, state
);
226 if (!dest
->dest
.is_ssa
&&
227 dest
->write_mask
!= (1 << dest
->dest
.reg
.reg
->num_components
) - 1) {
229 for (unsigned i
= 0; i
< 4; i
++)
230 if ((dest
->write_mask
>> i
) & 1)
231 fprintf(fp
, "%c", "xyzw"[i
]);
236 print_alu_instr(nir_alu_instr
*instr
, print_state
*state
)
238 FILE *fp
= state
->fp
;
240 print_alu_dest(&instr
->dest
, state
);
242 fprintf(fp
, " = %s", nir_op_infos
[instr
->op
].name
);
245 if (instr
->dest
.saturate
)
249 for (unsigned i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++) {
253 print_alu_src(instr
, i
, state
);
258 get_var_name(nir_variable
*var
, print_state
*state
)
260 if (state
->ht
== NULL
)
261 return var
->name
? var
->name
: "unnamed";
265 struct hash_entry
*entry
= _mesa_hash_table_search(state
->ht
, var
);
270 if (var
->name
== NULL
) {
271 name
= ralloc_asprintf(state
->syms
, "@%u", state
->index
++);
273 struct set_entry
*set_entry
= _mesa_set_search(state
->syms
, var
->name
);
274 if (set_entry
!= NULL
) {
275 /* we have a collision with another name, append an @ + a unique
277 name
= ralloc_asprintf(state
->syms
, "%s@%u", var
->name
,
280 /* Mark this one as seen */
281 _mesa_set_add(state
->syms
, var
->name
);
286 _mesa_hash_table_insert(state
->ht
, var
, name
);
292 print_constant(nir_constant
*c
, const struct glsl_type
*type
, print_state
*state
)
294 FILE *fp
= state
->fp
;
295 const unsigned rows
= glsl_get_vector_elements(type
);
296 const unsigned cols
= glsl_get_matrix_columns(type
);
299 switch (glsl_get_base_type(type
)) {
300 case GLSL_TYPE_UINT8
:
302 /* Only float base types can be matrices. */
305 for (i
= 0; i
< rows
; i
++) {
306 if (i
> 0) fprintf(fp
, ", ");
307 fprintf(fp
, "0x%02x", c
->values
[0].u8
[i
]);
311 case GLSL_TYPE_UINT16
:
312 case GLSL_TYPE_INT16
:
313 /* Only float base types can be matrices. */
316 for (i
= 0; i
< rows
; i
++) {
317 if (i
> 0) fprintf(fp
, ", ");
318 fprintf(fp
, "0x%04x", c
->values
[0].u16
[i
]);
325 /* Only float base types can be matrices. */
328 for (i
= 0; i
< rows
; i
++) {
329 if (i
> 0) fprintf(fp
, ", ");
330 fprintf(fp
, "0x%08x", c
->values
[0].u32
[i
]);
334 case GLSL_TYPE_FLOAT16
:
335 for (i
= 0; i
< cols
; i
++) {
336 for (j
= 0; j
< rows
; j
++) {
337 if (i
+ j
> 0) fprintf(fp
, ", ");
338 fprintf(fp
, "%f", _mesa_half_to_float(c
->values
[i
].u16
[j
]));
343 case GLSL_TYPE_FLOAT
:
344 for (i
= 0; i
< cols
; i
++) {
345 for (j
= 0; j
< rows
; j
++) {
346 if (i
+ j
> 0) fprintf(fp
, ", ");
347 fprintf(fp
, "%f", c
->values
[i
].f32
[j
]);
352 case GLSL_TYPE_DOUBLE
:
353 for (i
= 0; i
< cols
; i
++) {
354 for (j
= 0; j
< rows
; j
++) {
355 if (i
+ j
> 0) fprintf(fp
, ", ");
356 fprintf(fp
, "%f", c
->values
[i
].f64
[j
]);
361 case GLSL_TYPE_UINT64
:
362 case GLSL_TYPE_INT64
:
363 /* Only float base types can be matrices. */
366 for (i
= 0; i
< cols
; i
++) {
367 if (i
> 0) fprintf(fp
, ", ");
368 fprintf(fp
, "0x%08" PRIx64
, c
->values
[0].u64
[i
]);
372 case GLSL_TYPE_STRUCT
:
373 for (i
= 0; i
< c
->num_elements
; i
++) {
374 if (i
> 0) fprintf(fp
, ", ");
376 print_constant(c
->elements
[i
], glsl_get_struct_field(type
, i
), state
);
381 case GLSL_TYPE_ARRAY
:
382 for (i
= 0; i
< c
->num_elements
; i
++) {
383 if (i
> 0) fprintf(fp
, ", ");
385 print_constant(c
->elements
[i
], glsl_get_array_element(type
), state
);
391 unreachable("not reached");
396 get_variable_mode_str(nir_variable_mode mode
, bool want_local_global_mode
)
399 case nir_var_shader_in
:
401 case nir_var_shader_out
:
403 case nir_var_uniform
:
405 case nir_var_shader_storage
:
406 return "shader_storage";
407 case nir_var_system_value
:
413 return want_local_global_mode
? "global" : "";
415 return want_local_global_mode
? "local" : "";
422 print_var_decl(nir_variable
*var
, print_state
*state
)
424 FILE *fp
= state
->fp
;
426 fprintf(fp
, "decl_var ");
428 const char *const cent
= (var
->data
.centroid
) ? "centroid " : "";
429 const char *const samp
= (var
->data
.sample
) ? "sample " : "";
430 const char *const patch
= (var
->data
.patch
) ? "patch " : "";
431 const char *const inv
= (var
->data
.invariant
) ? "invariant " : "";
432 fprintf(fp
, "%s%s%s%s%s %s ",
433 cent
, samp
, patch
, inv
, get_variable_mode_str(var
->data
.mode
, false),
434 glsl_interp_mode_name(var
->data
.interpolation
));
436 const char *const coher
= (var
->data
.image
.coherent
) ? "coherent " : "";
437 const char *const volat
= (var
->data
.image
._volatile
) ? "volatile " : "";
438 const char *const restr
= (var
->data
.image
.restrict_flag
) ? "restrict " : "";
439 const char *const ronly
= (var
->data
.image
.read_only
) ? "readonly " : "";
440 const char *const wonly
= (var
->data
.image
.write_only
) ? "writeonly " : "";
441 fprintf(fp
, "%s%s%s%s%s", coher
, volat
, restr
, ronly
, wonly
);
443 fprintf(fp
, "%s %s", glsl_get_type_name(var
->type
),
444 get_var_name(var
, state
));
446 if (var
->data
.mode
== nir_var_shader_in
||
447 var
->data
.mode
== nir_var_shader_out
||
448 var
->data
.mode
== nir_var_uniform
||
449 var
->data
.mode
== nir_var_shader_storage
) {
450 const char *loc
= NULL
;
453 switch (state
->shader
->info
.stage
) {
454 case MESA_SHADER_VERTEX
:
455 if (var
->data
.mode
== nir_var_shader_in
)
456 loc
= gl_vert_attrib_name(var
->data
.location
);
457 else if (var
->data
.mode
== nir_var_shader_out
)
458 loc
= gl_varying_slot_name(var
->data
.location
);
460 case MESA_SHADER_GEOMETRY
:
461 if ((var
->data
.mode
== nir_var_shader_in
) ||
462 (var
->data
.mode
== nir_var_shader_out
))
463 loc
= gl_varying_slot_name(var
->data
.location
);
465 case MESA_SHADER_FRAGMENT
:
466 if (var
->data
.mode
== nir_var_shader_in
)
467 loc
= gl_varying_slot_name(var
->data
.location
);
468 else if (var
->data
.mode
== nir_var_shader_out
)
469 loc
= gl_frag_result_name(var
->data
.location
);
471 case MESA_SHADER_TESS_CTRL
:
472 case MESA_SHADER_TESS_EVAL
:
473 case MESA_SHADER_COMPUTE
:
480 snprintf(buf
, sizeof(buf
), "%u", var
->data
.location
);
484 /* For shader I/O vars that have been split to components or packed,
485 * print the fractional location within the input/output.
487 unsigned int num_components
=
488 glsl_get_components(glsl_without_array(var
->type
));
489 const char *components
= NULL
;
490 char components_local
[6] = {'.' /* the rest is 0-filled */};
491 switch (var
->data
.mode
) {
492 case nir_var_shader_in
:
493 case nir_var_shader_out
:
494 if (num_components
< 4 && num_components
!= 0) {
495 const char *xyzw
= "xyzw";
496 for (int i
= 0; i
< num_components
; i
++)
497 components_local
[i
+ 1] = xyzw
[i
+ var
->data
.location_frac
];
499 components
= components_local
;
506 fprintf(fp
, " (%s%s, %u, %u)%s", loc
,
507 components
? components
: "",
508 var
->data
.driver_location
, var
->data
.binding
,
509 var
->data
.compact
? " compact" : "");
512 if (var
->constant_initializer
) {
513 fprintf(fp
, " = { ");
514 print_constant(var
->constant_initializer
, var
->type
, state
);
519 print_annotation(state
, var
);
523 print_deref_link(nir_deref_instr
*instr
, bool whole_chain
, print_state
*state
)
525 FILE *fp
= state
->fp
;
527 if (instr
->deref_type
== nir_deref_type_var
) {
528 fprintf(fp
, "%s", get_var_name(instr
->var
, state
));
530 } else if (instr
->deref_type
== nir_deref_type_cast
) {
531 fprintf(fp
, "(%s *)", glsl_get_type_name(instr
->type
));
532 print_src(&instr
->parent
, state
);
536 assert(instr
->parent
.is_ssa
);
537 nir_deref_instr
*parent
=
538 nir_instr_as_deref(instr
->parent
.ssa
->parent_instr
);
540 /* Is the parent we're going to print a bare cast? */
541 const bool is_parent_cast
=
542 whole_chain
&& parent
->deref_type
== nir_deref_type_cast
;
544 /* If we're not printing the whole chain, the parent we print will be a SSA
545 * value that represents a pointer. The only deref type that naturally
546 * gives a pointer is a cast.
548 const bool is_parent_pointer
=
549 !whole_chain
|| parent
->deref_type
== nir_deref_type_cast
;
551 /* Struct derefs have a nice syntax that works on pointers, arrays derefs
554 const bool need_deref
=
555 is_parent_pointer
&& instr
->deref_type
!= nir_deref_type_struct
;
557 /* Cast need extra parens and so * dereferences */
558 if (is_parent_cast
|| need_deref
)
565 print_deref_link(parent
, whole_chain
, state
);
567 print_src(&instr
->parent
, state
);
570 if (is_parent_cast
|| need_deref
)
573 switch (instr
->deref_type
) {
574 case nir_deref_type_struct
:
575 fprintf(fp
, "%s%s", is_parent_pointer
? "->" : ".",
576 glsl_get_struct_elem_name(parent
->type
, instr
->strct
.index
));
579 case nir_deref_type_array
: {
580 nir_const_value
*const_index
= nir_src_as_const_value(instr
->arr
.index
);
582 fprintf(fp
, "[%u]", const_index
->u32
[0]);
585 print_src(&instr
->arr
.index
, state
);
591 case nir_deref_type_array_wildcard
:
596 unreachable("Invalid deref instruction type");
601 print_deref_instr(nir_deref_instr
*instr
, print_state
*state
)
603 FILE *fp
= state
->fp
;
605 print_dest(&instr
->dest
, state
);
607 switch (instr
->deref_type
) {
608 case nir_deref_type_var
:
609 fprintf(fp
, " = deref_var ");
611 case nir_deref_type_array
:
612 case nir_deref_type_array_wildcard
:
613 fprintf(fp
, " = deref_array ");
615 case nir_deref_type_struct
:
616 fprintf(fp
, " = deref_struct ");
618 case nir_deref_type_cast
:
619 fprintf(fp
, " = deref_cast ");
622 unreachable("Invalid deref instruction type");
625 /* Only casts naturally return a pointer type */
626 if (instr
->deref_type
!= nir_deref_type_cast
)
629 print_deref_link(instr
, false, state
);
631 fprintf(fp
, " (%s %s) ",
632 get_variable_mode_str(instr
->mode
, true),
633 glsl_get_type_name(instr
->type
));
635 if (instr
->deref_type
!= nir_deref_type_var
&&
636 instr
->deref_type
!= nir_deref_type_cast
) {
637 /* Print the entire chain as a comment */
639 print_deref_link(instr
, true, state
);
645 print_var(nir_variable
*var
, print_state
*state
)
647 FILE *fp
= state
->fp
;
648 fprintf(fp
, "%s", get_var_name(var
, state
));
652 print_arg(nir_variable
*var
, print_state
*state
)
654 FILE *fp
= state
->fp
;
655 fprintf(fp
, "%s %s", glsl_get_type_name(var
->type
),
656 get_var_name(var
, state
));
660 print_deref_var(nir_deref_var
*deref
, print_state
*state
)
662 print_var(deref
->var
, state
);
666 print_deref_array(nir_deref_array
*deref
, print_state
*state
)
668 FILE *fp
= state
->fp
;
670 switch (deref
->deref_array_type
) {
671 case nir_deref_array_type_direct
:
672 fprintf(fp
, "%u", deref
->base_offset
);
674 case nir_deref_array_type_indirect
:
675 if (deref
->base_offset
!= 0)
676 fprintf(fp
, "%u + ", deref
->base_offset
);
677 print_src(&deref
->indirect
, state
);
679 case nir_deref_array_type_wildcard
:
687 print_deref_struct(nir_deref_struct
*deref
, const struct glsl_type
*parent_type
,
690 FILE *fp
= state
->fp
;
691 fprintf(fp
, ".%s", glsl_get_struct_elem_name(parent_type
, deref
->index
));
695 print_deref(nir_deref_var
*deref
, print_state
*state
)
697 nir_deref
*tail
= &deref
->deref
;
698 nir_deref
*pretail
= NULL
;
699 while (tail
!= NULL
) {
700 switch (tail
->deref_type
) {
701 case nir_deref_type_var
:
702 assert(pretail
== NULL
);
703 assert(tail
== &deref
->deref
);
704 print_deref_var(deref
, state
);
707 case nir_deref_type_array
:
708 assert(pretail
!= NULL
);
709 print_deref_array(nir_deref_as_array(tail
), state
);
712 case nir_deref_type_struct
:
713 assert(pretail
!= NULL
);
714 print_deref_struct(nir_deref_as_struct(tail
),
715 pretail
->type
, state
);
719 unreachable("Invalid deref type");
723 tail
= pretail
->child
;
728 print_intrinsic_instr(nir_intrinsic_instr
*instr
, print_state
*state
)
730 const nir_intrinsic_info
*info
= &nir_intrinsic_infos
[instr
->intrinsic
];
731 unsigned num_srcs
= info
->num_srcs
;
732 FILE *fp
= state
->fp
;
734 if (info
->has_dest
) {
735 print_dest(&instr
->dest
, state
);
739 fprintf(fp
, "intrinsic %s (", info
->name
);
741 for (unsigned i
= 0; i
< num_srcs
; i
++) {
745 print_src(&instr
->src
[i
], state
);
750 for (unsigned i
= 0; i
< info
->num_variables
; i
++) {
754 print_deref(instr
->variables
[i
], state
);
759 for (unsigned i
= 0; i
< info
->num_indices
; i
++) {
763 fprintf(fp
, "%d", instr
->const_index
[i
]);
768 static const char *index_name
[NIR_INTRINSIC_NUM_INDEX_FLAGS
] = {
769 [NIR_INTRINSIC_BASE
] = "base",
770 [NIR_INTRINSIC_WRMASK
] = "wrmask",
771 [NIR_INTRINSIC_STREAM_ID
] = "stream-id",
772 [NIR_INTRINSIC_UCP_ID
] = "ucp-id",
773 [NIR_INTRINSIC_RANGE
] = "range",
774 [NIR_INTRINSIC_DESC_SET
] = "desc-set",
775 [NIR_INTRINSIC_BINDING
] = "binding",
776 [NIR_INTRINSIC_COMPONENT
] = "component",
777 [NIR_INTRINSIC_INTERP_MODE
] = "interp_mode",
778 [NIR_INTRINSIC_REDUCTION_OP
] = "reduction_op",
779 [NIR_INTRINSIC_CLUSTER_SIZE
] = "cluster_size",
781 for (unsigned idx
= 1; idx
< NIR_INTRINSIC_NUM_INDEX_FLAGS
; idx
++) {
782 if (!info
->index_map
[idx
])
785 if (idx
== NIR_INTRINSIC_WRMASK
) {
786 /* special case wrmask to show it as a writemask.. */
787 unsigned wrmask
= nir_intrinsic_write_mask(instr
);
788 fprintf(fp
, " wrmask=");
789 for (unsigned i
= 0; i
< 4; i
++)
790 if ((wrmask
>> i
) & 1)
791 fprintf(fp
, "%c", "xyzw"[i
]);
792 } else if (idx
== NIR_INTRINSIC_REDUCTION_OP
) {
793 nir_op reduction_op
= nir_intrinsic_reduction_op(instr
);
794 fprintf(fp
, " reduction_op=%s", nir_op_infos
[reduction_op
].name
);
796 unsigned off
= info
->index_map
[idx
] - 1;
797 assert(index_name
[idx
]); /* forgot to update index_name table? */
798 fprintf(fp
, " %s=%d", index_name
[idx
], instr
->const_index
[off
]);
806 struct exec_list
*var_list
= NULL
;
808 switch (instr
->intrinsic
) {
809 case nir_intrinsic_load_uniform
:
810 var_list
= &state
->shader
->uniforms
;
812 case nir_intrinsic_load_input
:
813 case nir_intrinsic_load_per_vertex_input
:
814 var_list
= &state
->shader
->inputs
;
816 case nir_intrinsic_load_output
:
817 case nir_intrinsic_store_output
:
818 case nir_intrinsic_store_per_vertex_output
:
819 var_list
= &state
->shader
->outputs
;
825 nir_foreach_variable(var
, var_list
) {
826 if ((var
->data
.driver_location
== nir_intrinsic_base(instr
)) &&
827 (instr
->intrinsic
== nir_intrinsic_load_uniform
||
828 var
->data
.location_frac
== nir_intrinsic_component(instr
)) &&
830 fprintf(fp
, "\t/* %s */", var
->name
);
837 print_tex_instr(nir_tex_instr
*instr
, print_state
*state
)
839 FILE *fp
= state
->fp
;
841 print_dest(&instr
->dest
, state
);
861 case nir_texop_txf_ms
:
862 fprintf(fp
, "txf_ms ");
864 case nir_texop_txf_ms_mcs
:
865 fprintf(fp
, "txf_ms_mcs ");
876 case nir_texop_query_levels
:
877 fprintf(fp
, "query_levels ");
879 case nir_texop_texture_samples
:
880 fprintf(fp
, "texture_samples ");
882 case nir_texop_samples_identical
:
883 fprintf(fp
, "samples_identical ");
886 unreachable("Invalid texture operation");
890 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++) {
891 print_src(&instr
->src
[i
].src
, state
);
895 switch(instr
->src
[i
].src_type
) {
896 case nir_tex_src_coord
:
897 fprintf(fp
, "(coord)");
899 case nir_tex_src_projector
:
900 fprintf(fp
, "(projector)");
902 case nir_tex_src_comparator
:
903 fprintf(fp
, "(comparator)");
905 case nir_tex_src_offset
:
906 fprintf(fp
, "(offset)");
908 case nir_tex_src_bias
:
909 fprintf(fp
, "(bias)");
911 case nir_tex_src_lod
:
912 fprintf(fp
, "(lod)");
914 case nir_tex_src_ms_index
:
915 fprintf(fp
, "(ms_index)");
917 case nir_tex_src_ms_mcs
:
918 fprintf(fp
, "(ms_mcs)");
920 case nir_tex_src_ddx
:
921 fprintf(fp
, "(ddx)");
923 case nir_tex_src_ddy
:
924 fprintf(fp
, "(ddy)");
926 case nir_tex_src_texture_offset
:
927 fprintf(fp
, "(texture_offset)");
929 case nir_tex_src_sampler_offset
:
930 fprintf(fp
, "(sampler_offset)");
932 case nir_tex_src_plane
:
933 fprintf(fp
, "(plane)");
937 unreachable("Invalid texture source type");
944 if (instr
->op
== nir_texop_tg4
) {
945 fprintf(fp
, "%u (gather_component), ", instr
->component
);
948 if (instr
->texture
) {
949 print_deref(instr
->texture
, state
);
950 fprintf(fp
, " (texture)");
951 if (instr
->sampler
) {
952 print_deref(instr
->sampler
, state
);
953 fprintf(fp
, " (sampler)");
956 assert(instr
->sampler
== NULL
);
957 fprintf(fp
, "%u (texture) %u (sampler)",
958 instr
->texture_index
, instr
->sampler_index
);
963 print_call_instr(nir_call_instr
*instr
, print_state
*state
)
965 FILE *fp
= state
->fp
;
967 fprintf(fp
, "call %s ", instr
->callee
->name
);
969 for (unsigned i
= 0; i
< instr
->num_params
; i
++) {
973 print_deref(instr
->params
[i
], state
);
976 if (instr
->return_deref
!= NULL
) {
977 if (instr
->num_params
!= 0)
979 fprintf(fp
, "returning ");
980 print_deref(instr
->return_deref
, state
);
985 print_load_const_instr(nir_load_const_instr
*instr
, print_state
*state
)
987 FILE *fp
= state
->fp
;
989 print_ssa_def(&instr
->def
, state
);
991 fprintf(fp
, " = load_const (");
993 for (unsigned i
= 0; i
< instr
->def
.num_components
; i
++) {
998 * we don't really know the type of the constant (if it will be used as a
999 * float or an int), so just print the raw constant in hex for fidelity
1000 * and then print the float in a comment for readability.
1003 switch (instr
->def
.bit_size
) {
1005 fprintf(fp
, "0x%16" PRIx64
" /* %f */", instr
->value
.u64
[i
],
1006 instr
->value
.f64
[i
]);
1009 fprintf(fp
, "0x%08x /* %f */", instr
->value
.u32
[i
], instr
->value
.f32
[i
]);
1012 fprintf(fp
, "0x%04x /* %f */", instr
->value
.u16
[i
],
1013 _mesa_half_to_float(instr
->value
.u16
[i
]));
1016 fprintf(fp
, "0x%02x", instr
->value
.u8
[i
]);
1025 print_jump_instr(nir_jump_instr
*instr
, print_state
*state
)
1027 FILE *fp
= state
->fp
;
1029 switch (instr
->type
) {
1030 case nir_jump_break
:
1031 fprintf(fp
, "break");
1034 case nir_jump_continue
:
1035 fprintf(fp
, "continue");
1038 case nir_jump_return
:
1039 fprintf(fp
, "return");
1045 print_ssa_undef_instr(nir_ssa_undef_instr
* instr
, print_state
*state
)
1047 FILE *fp
= state
->fp
;
1048 print_ssa_def(&instr
->def
, state
);
1049 fprintf(fp
, " = undefined");
1053 print_phi_instr(nir_phi_instr
*instr
, print_state
*state
)
1055 FILE *fp
= state
->fp
;
1056 print_dest(&instr
->dest
, state
);
1057 fprintf(fp
, " = phi ");
1058 nir_foreach_phi_src(src
, instr
) {
1059 if (&src
->node
!= exec_list_get_head(&instr
->srcs
))
1062 fprintf(fp
, "block_%u: ", src
->pred
->index
);
1063 print_src(&src
->src
, state
);
1068 print_parallel_copy_instr(nir_parallel_copy_instr
*instr
, print_state
*state
)
1070 FILE *fp
= state
->fp
;
1071 nir_foreach_parallel_copy_entry(entry
, instr
) {
1072 if (&entry
->node
!= exec_list_get_head(&instr
->entries
))
1075 print_dest(&entry
->dest
, state
);
1077 print_src(&entry
->src
, state
);
1082 print_instr(const nir_instr
*instr
, print_state
*state
, unsigned tabs
)
1084 FILE *fp
= state
->fp
;
1085 print_tabs(tabs
, fp
);
1087 switch (instr
->type
) {
1088 case nir_instr_type_alu
:
1089 print_alu_instr(nir_instr_as_alu(instr
), state
);
1092 case nir_instr_type_deref
:
1093 print_deref_instr(nir_instr_as_deref(instr
), state
);
1096 case nir_instr_type_call
:
1097 print_call_instr(nir_instr_as_call(instr
), state
);
1100 case nir_instr_type_intrinsic
:
1101 print_intrinsic_instr(nir_instr_as_intrinsic(instr
), state
);
1104 case nir_instr_type_tex
:
1105 print_tex_instr(nir_instr_as_tex(instr
), state
);
1108 case nir_instr_type_load_const
:
1109 print_load_const_instr(nir_instr_as_load_const(instr
), state
);
1112 case nir_instr_type_jump
:
1113 print_jump_instr(nir_instr_as_jump(instr
), state
);
1116 case nir_instr_type_ssa_undef
:
1117 print_ssa_undef_instr(nir_instr_as_ssa_undef(instr
), state
);
1120 case nir_instr_type_phi
:
1121 print_phi_instr(nir_instr_as_phi(instr
), state
);
1124 case nir_instr_type_parallel_copy
:
1125 print_parallel_copy_instr(nir_instr_as_parallel_copy(instr
), state
);
1129 unreachable("Invalid instruction type");
1135 compare_block_index(const void *p1
, const void *p2
)
1137 const nir_block
*block1
= *((const nir_block
**) p1
);
1138 const nir_block
*block2
= *((const nir_block
**) p2
);
1140 return (int) block1
->index
- (int) block2
->index
;
1143 static void print_cf_node(nir_cf_node
*node
, print_state
*state
,
1147 print_block(nir_block
*block
, print_state
*state
, unsigned tabs
)
1149 FILE *fp
= state
->fp
;
1151 print_tabs(tabs
, fp
);
1152 fprintf(fp
, "block block_%u:\n", block
->index
);
1154 /* sort the predecessors by index so we consistently print the same thing */
1157 malloc(block
->predecessors
->entries
* sizeof(nir_block
*));
1159 struct set_entry
*entry
;
1161 set_foreach(block
->predecessors
, entry
) {
1162 preds
[i
++] = (nir_block
*) entry
->key
;
1165 qsort(preds
, block
->predecessors
->entries
, sizeof(nir_block
*),
1166 compare_block_index
);
1168 print_tabs(tabs
, fp
);
1169 fprintf(fp
, "/* preds: ");
1170 for (unsigned i
= 0; i
< block
->predecessors
->entries
; i
++) {
1171 fprintf(fp
, "block_%u ", preds
[i
]->index
);
1173 fprintf(fp
, "*/\n");
1177 nir_foreach_instr(instr
, block
) {
1178 print_instr(instr
, state
, tabs
);
1180 print_annotation(state
, instr
);
1183 print_tabs(tabs
, fp
);
1184 fprintf(fp
, "/* succs: ");
1185 for (unsigned i
= 0; i
< 2; i
++)
1186 if (block
->successors
[i
]) {
1187 fprintf(fp
, "block_%u ", block
->successors
[i
]->index
);
1189 fprintf(fp
, "*/\n");
1193 print_if(nir_if
*if_stmt
, print_state
*state
, unsigned tabs
)
1195 FILE *fp
= state
->fp
;
1197 print_tabs(tabs
, fp
);
1199 print_src(&if_stmt
->condition
, state
);
1200 fprintf(fp
, " {\n");
1201 foreach_list_typed(nir_cf_node
, node
, node
, &if_stmt
->then_list
) {
1202 print_cf_node(node
, state
, tabs
+ 1);
1204 print_tabs(tabs
, fp
);
1205 fprintf(fp
, "} else {\n");
1206 foreach_list_typed(nir_cf_node
, node
, node
, &if_stmt
->else_list
) {
1207 print_cf_node(node
, state
, tabs
+ 1);
1209 print_tabs(tabs
, fp
);
1214 print_loop(nir_loop
*loop
, print_state
*state
, unsigned tabs
)
1216 FILE *fp
= state
->fp
;
1218 print_tabs(tabs
, fp
);
1219 fprintf(fp
, "loop {\n");
1220 foreach_list_typed(nir_cf_node
, node
, node
, &loop
->body
) {
1221 print_cf_node(node
, state
, tabs
+ 1);
1223 print_tabs(tabs
, fp
);
1228 print_cf_node(nir_cf_node
*node
, print_state
*state
, unsigned int tabs
)
1230 switch (node
->type
) {
1231 case nir_cf_node_block
:
1232 print_block(nir_cf_node_as_block(node
), state
, tabs
);
1235 case nir_cf_node_if
:
1236 print_if(nir_cf_node_as_if(node
), state
, tabs
);
1239 case nir_cf_node_loop
:
1240 print_loop(nir_cf_node_as_loop(node
), state
, tabs
);
1244 unreachable("Invalid CFG node type");
1249 print_function_impl(nir_function_impl
*impl
, print_state
*state
)
1251 FILE *fp
= state
->fp
;
1253 fprintf(fp
, "\nimpl %s ", impl
->function
->name
);
1255 for (unsigned i
= 0; i
< impl
->num_params
; i
++) {
1259 print_arg(impl
->params
[i
], state
);
1262 if (impl
->return_var
!= NULL
) {
1263 if (impl
->num_params
!= 0)
1265 fprintf(fp
, "returning ");
1266 print_arg(impl
->return_var
, state
);
1271 nir_foreach_variable(var
, &impl
->locals
) {
1273 print_var_decl(var
, state
);
1276 foreach_list_typed(nir_register
, reg
, node
, &impl
->registers
) {
1278 print_register_decl(reg
, state
);
1281 nir_index_blocks(impl
);
1283 foreach_list_typed(nir_cf_node
, node
, node
, &impl
->body
) {
1284 print_cf_node(node
, state
, 1);
1287 fprintf(fp
, "\tblock block_%u:\n}\n\n", impl
->end_block
->index
);
1291 print_function(nir_function
*function
, print_state
*state
)
1293 FILE *fp
= state
->fp
;
1295 fprintf(fp
, "decl_function %s ", function
->name
);
1297 for (unsigned i
= 0; i
< function
->num_params
; i
++) {
1301 switch (function
->params
[i
].param_type
) {
1302 case nir_parameter_in
:
1305 case nir_parameter_out
:
1306 fprintf(fp
, "out ");
1308 case nir_parameter_inout
:
1309 fprintf(fp
, "inout ");
1312 unreachable("Invalid parameter type");
1315 fprintf(fp
, "%s", glsl_get_type_name(function
->params
[i
].type
));
1318 if (function
->return_type
!= NULL
) {
1319 if (function
->num_params
!= 0)
1321 fprintf(fp
, "returning %s", glsl_get_type_name(function
->return_type
));
1326 if (function
->impl
!= NULL
) {
1327 print_function_impl(function
->impl
, state
);
1333 init_print_state(print_state
*state
, nir_shader
*shader
, FILE *fp
)
1336 state
->shader
= shader
;
1337 state
->ht
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
1338 _mesa_key_pointer_equal
);
1339 state
->syms
= _mesa_set_create(NULL
, _mesa_key_hash_string
,
1340 _mesa_key_string_equal
);
1345 destroy_print_state(print_state
*state
)
1347 _mesa_hash_table_destroy(state
->ht
, NULL
);
1348 _mesa_set_destroy(state
->syms
, NULL
);
1352 nir_print_shader_annotated(nir_shader
*shader
, FILE *fp
,
1353 struct hash_table
*annotations
)
1356 init_print_state(&state
, shader
, fp
);
1358 state
.annotations
= annotations
;
1360 fprintf(fp
, "shader: %s\n", gl_shader_stage_name(shader
->info
.stage
));
1362 if (shader
->info
.name
)
1363 fprintf(fp
, "name: %s\n", shader
->info
.name
);
1365 if (shader
->info
.label
)
1366 fprintf(fp
, "label: %s\n", shader
->info
.label
);
1368 switch (shader
->info
.stage
) {
1369 case MESA_SHADER_COMPUTE
:
1370 fprintf(fp
, "local-size: %u, %u, %u%s\n",
1371 shader
->info
.cs
.local_size
[0],
1372 shader
->info
.cs
.local_size
[1],
1373 shader
->info
.cs
.local_size
[2],
1374 shader
->info
.cs
.local_size_variable
? " (variable)" : "");
1375 fprintf(fp
, "shared-size: %u\n", shader
->info
.cs
.shared_size
);
1381 fprintf(fp
, "inputs: %u\n", shader
->num_inputs
);
1382 fprintf(fp
, "outputs: %u\n", shader
->num_outputs
);
1383 fprintf(fp
, "uniforms: %u\n", shader
->num_uniforms
);
1384 fprintf(fp
, "shared: %u\n", shader
->num_shared
);
1386 nir_foreach_variable(var
, &shader
->uniforms
) {
1387 print_var_decl(var
, &state
);
1390 nir_foreach_variable(var
, &shader
->inputs
) {
1391 print_var_decl(var
, &state
);
1394 nir_foreach_variable(var
, &shader
->outputs
) {
1395 print_var_decl(var
, &state
);
1398 nir_foreach_variable(var
, &shader
->shared
) {
1399 print_var_decl(var
, &state
);
1402 nir_foreach_variable(var
, &shader
->globals
) {
1403 print_var_decl(var
, &state
);
1406 nir_foreach_variable(var
, &shader
->system_values
) {
1407 print_var_decl(var
, &state
);
1410 foreach_list_typed(nir_register
, reg
, node
, &shader
->registers
) {
1411 print_register_decl(reg
, &state
);
1414 foreach_list_typed(nir_function
, func
, node
, &shader
->functions
) {
1415 print_function(func
, &state
);
1418 destroy_print_state(&state
);
1422 nir_print_shader(nir_shader
*shader
, FILE *fp
)
1424 nir_print_shader_annotated(shader
, fp
, NULL
);
1428 nir_print_instr(const nir_instr
*instr
, FILE *fp
)
1430 print_state state
= {
1433 print_instr(instr
, &state
, 0);