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
= instr
->src
[src
].src
.is_ssa
203 ? instr
->src
[src
].src
.ssa
->num_components
204 : instr
->src
[src
].src
.reg
.reg
->num_components
;
206 if (print_swizzle
|| used_channels
!= live_channels
) {
208 for (unsigned i
= 0; i
< 4; i
++) {
209 if (!nir_alu_instr_channel_used(instr
, src
, i
))
212 fprintf(fp
, "%c", "xyzw"[instr
->src
[src
].swizzle
[i
]]);
216 if (instr
->src
[src
].abs
)
221 print_alu_dest(nir_alu_dest
*dest
, print_state
*state
)
223 FILE *fp
= state
->fp
;
224 /* we're going to print the saturate modifier later, after the opcode */
226 print_dest(&dest
->dest
, state
);
228 if (!dest
->dest
.is_ssa
&&
229 dest
->write_mask
!= (1 << dest
->dest
.reg
.reg
->num_components
) - 1) {
231 for (unsigned i
= 0; i
< 4; i
++)
232 if ((dest
->write_mask
>> i
) & 1)
233 fprintf(fp
, "%c", "xyzw"[i
]);
238 print_alu_instr(nir_alu_instr
*instr
, print_state
*state
)
240 FILE *fp
= state
->fp
;
242 print_alu_dest(&instr
->dest
, state
);
244 fprintf(fp
, " = %s", nir_op_infos
[instr
->op
].name
);
247 if (instr
->dest
.saturate
)
251 for (unsigned i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++) {
255 print_alu_src(instr
, i
, state
);
260 get_var_name(nir_variable
*var
, print_state
*state
)
262 if (state
->ht
== NULL
)
263 return var
->name
? var
->name
: "unnamed";
267 struct hash_entry
*entry
= _mesa_hash_table_search(state
->ht
, var
);
272 if (var
->name
== NULL
) {
273 name
= ralloc_asprintf(state
->syms
, "@%u", state
->index
++);
275 struct set_entry
*set_entry
= _mesa_set_search(state
->syms
, var
->name
);
276 if (set_entry
!= NULL
) {
277 /* we have a collision with another name, append an @ + a unique
279 name
= ralloc_asprintf(state
->syms
, "%s@%u", var
->name
,
282 /* Mark this one as seen */
283 _mesa_set_add(state
->syms
, var
->name
);
288 _mesa_hash_table_insert(state
->ht
, var
, name
);
294 print_constant(nir_constant
*c
, const struct glsl_type
*type
, print_state
*state
)
296 FILE *fp
= state
->fp
;
297 const unsigned rows
= glsl_get_vector_elements(type
);
298 const unsigned cols
= glsl_get_matrix_columns(type
);
301 switch (glsl_get_base_type(type
)) {
302 case GLSL_TYPE_UINT8
:
304 /* Only float base types can be matrices. */
307 for (i
= 0; i
< rows
; i
++) {
308 if (i
> 0) fprintf(fp
, ", ");
309 fprintf(fp
, "0x%02x", c
->values
[0].u8
[i
]);
313 case GLSL_TYPE_UINT16
:
314 case GLSL_TYPE_INT16
:
315 /* Only float base types can be matrices. */
318 for (i
= 0; i
< rows
; i
++) {
319 if (i
> 0) fprintf(fp
, ", ");
320 fprintf(fp
, "0x%04x", c
->values
[0].u16
[i
]);
327 /* Only float base types can be matrices. */
330 for (i
= 0; i
< rows
; i
++) {
331 if (i
> 0) fprintf(fp
, ", ");
332 fprintf(fp
, "0x%08x", c
->values
[0].u32
[i
]);
336 case GLSL_TYPE_FLOAT16
:
337 for (i
= 0; i
< cols
; i
++) {
338 for (j
= 0; j
< rows
; j
++) {
339 if (i
+ j
> 0) fprintf(fp
, ", ");
340 fprintf(fp
, "%f", _mesa_half_to_float(c
->values
[i
].u16
[j
]));
345 case GLSL_TYPE_FLOAT
:
346 for (i
= 0; i
< cols
; i
++) {
347 for (j
= 0; j
< rows
; j
++) {
348 if (i
+ j
> 0) fprintf(fp
, ", ");
349 fprintf(fp
, "%f", c
->values
[i
].f32
[j
]);
354 case GLSL_TYPE_DOUBLE
:
355 for (i
= 0; i
< cols
; i
++) {
356 for (j
= 0; j
< rows
; j
++) {
357 if (i
+ j
> 0) fprintf(fp
, ", ");
358 fprintf(fp
, "%f", c
->values
[i
].f64
[j
]);
363 case GLSL_TYPE_UINT64
:
364 case GLSL_TYPE_INT64
:
365 /* Only float base types can be matrices. */
368 for (i
= 0; i
< cols
; i
++) {
369 if (i
> 0) fprintf(fp
, ", ");
370 fprintf(fp
, "0x%08" PRIx64
, c
->values
[0].u64
[i
]);
374 case GLSL_TYPE_STRUCT
:
375 for (i
= 0; i
< c
->num_elements
; i
++) {
376 if (i
> 0) fprintf(fp
, ", ");
378 print_constant(c
->elements
[i
], glsl_get_struct_field(type
, i
), state
);
383 case GLSL_TYPE_ARRAY
:
384 for (i
= 0; i
< c
->num_elements
; i
++) {
385 if (i
> 0) fprintf(fp
, ", ");
387 print_constant(c
->elements
[i
], glsl_get_array_element(type
), state
);
393 unreachable("not reached");
398 get_variable_mode_str(nir_variable_mode mode
)
401 case nir_var_shader_in
:
403 case nir_var_shader_out
:
405 case nir_var_uniform
:
407 case nir_var_shader_storage
:
408 return "shader_storage";
409 case nir_var_system_value
:
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
),
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_var(nir_variable
*var
, print_state
*state
)
525 FILE *fp
= state
->fp
;
526 fprintf(fp
, "%s", get_var_name(var
, state
));
530 print_arg(nir_variable
*var
, print_state
*state
)
532 FILE *fp
= state
->fp
;
533 fprintf(fp
, "%s %s", glsl_get_type_name(var
->type
),
534 get_var_name(var
, state
));
538 print_deref_var(nir_deref_var
*deref
, print_state
*state
)
540 print_var(deref
->var
, state
);
544 print_deref_array(nir_deref_array
*deref
, print_state
*state
)
546 FILE *fp
= state
->fp
;
548 switch (deref
->deref_array_type
) {
549 case nir_deref_array_type_direct
:
550 fprintf(fp
, "%u", deref
->base_offset
);
552 case nir_deref_array_type_indirect
:
553 if (deref
->base_offset
!= 0)
554 fprintf(fp
, "%u + ", deref
->base_offset
);
555 print_src(&deref
->indirect
, state
);
557 case nir_deref_array_type_wildcard
:
565 print_deref_struct(nir_deref_struct
*deref
, const struct glsl_type
*parent_type
,
568 FILE *fp
= state
->fp
;
569 fprintf(fp
, ".%s", glsl_get_struct_elem_name(parent_type
, deref
->index
));
573 print_deref(nir_deref_var
*deref
, print_state
*state
)
575 nir_deref
*tail
= &deref
->deref
;
576 nir_deref
*pretail
= NULL
;
577 while (tail
!= NULL
) {
578 switch (tail
->deref_type
) {
579 case nir_deref_type_var
:
580 assert(pretail
== NULL
);
581 assert(tail
== &deref
->deref
);
582 print_deref_var(deref
, state
);
585 case nir_deref_type_array
:
586 assert(pretail
!= NULL
);
587 print_deref_array(nir_deref_as_array(tail
), state
);
590 case nir_deref_type_struct
:
591 assert(pretail
!= NULL
);
592 print_deref_struct(nir_deref_as_struct(tail
),
593 pretail
->type
, state
);
597 unreachable("Invalid deref type");
601 tail
= pretail
->child
;
606 print_intrinsic_instr(nir_intrinsic_instr
*instr
, print_state
*state
)
608 const nir_intrinsic_info
*info
= &nir_intrinsic_infos
[instr
->intrinsic
];
609 unsigned num_srcs
= info
->num_srcs
;
610 FILE *fp
= state
->fp
;
612 if (info
->has_dest
) {
613 print_dest(&instr
->dest
, state
);
617 fprintf(fp
, "intrinsic %s (", info
->name
);
619 for (unsigned i
= 0; i
< num_srcs
; i
++) {
623 print_src(&instr
->src
[i
], state
);
628 for (unsigned i
= 0; i
< info
->num_variables
; i
++) {
632 print_deref(instr
->variables
[i
], state
);
637 for (unsigned i
= 0; i
< info
->num_indices
; i
++) {
641 fprintf(fp
, "%d", instr
->const_index
[i
]);
646 static const char *index_name
[NIR_INTRINSIC_NUM_INDEX_FLAGS
] = {
647 [NIR_INTRINSIC_BASE
] = "base",
648 [NIR_INTRINSIC_WRMASK
] = "wrmask",
649 [NIR_INTRINSIC_STREAM_ID
] = "stream-id",
650 [NIR_INTRINSIC_UCP_ID
] = "ucp-id",
651 [NIR_INTRINSIC_RANGE
] = "range",
652 [NIR_INTRINSIC_DESC_SET
] = "desc-set",
653 [NIR_INTRINSIC_BINDING
] = "binding",
654 [NIR_INTRINSIC_COMPONENT
] = "component",
655 [NIR_INTRINSIC_INTERP_MODE
] = "interp_mode",
656 [NIR_INTRINSIC_REDUCTION_OP
] = "reduction_op",
657 [NIR_INTRINSIC_CLUSTER_SIZE
] = "cluster_size",
659 for (unsigned idx
= 1; idx
< NIR_INTRINSIC_NUM_INDEX_FLAGS
; idx
++) {
660 if (!info
->index_map
[idx
])
663 if (idx
== NIR_INTRINSIC_WRMASK
) {
664 /* special case wrmask to show it as a writemask.. */
665 unsigned wrmask
= nir_intrinsic_write_mask(instr
);
666 fprintf(fp
, " wrmask=");
667 for (unsigned i
= 0; i
< 4; i
++)
668 if ((wrmask
>> i
) & 1)
669 fprintf(fp
, "%c", "xyzw"[i
]);
670 } else if (idx
== NIR_INTRINSIC_REDUCTION_OP
) {
671 nir_op reduction_op
= nir_intrinsic_reduction_op(instr
);
672 fprintf(fp
, " reduction_op=%s", nir_op_infos
[reduction_op
].name
);
674 unsigned off
= info
->index_map
[idx
] - 1;
675 assert(index_name
[idx
]); /* forgot to update index_name table? */
676 fprintf(fp
, " %s=%d", index_name
[idx
], instr
->const_index
[off
]);
684 struct exec_list
*var_list
= NULL
;
686 switch (instr
->intrinsic
) {
687 case nir_intrinsic_load_uniform
:
688 var_list
= &state
->shader
->uniforms
;
690 case nir_intrinsic_load_input
:
691 case nir_intrinsic_load_per_vertex_input
:
692 var_list
= &state
->shader
->inputs
;
694 case nir_intrinsic_load_output
:
695 case nir_intrinsic_store_output
:
696 case nir_intrinsic_store_per_vertex_output
:
697 var_list
= &state
->shader
->outputs
;
703 nir_foreach_variable(var
, var_list
) {
704 if ((var
->data
.driver_location
== nir_intrinsic_base(instr
)) &&
705 (instr
->intrinsic
== nir_intrinsic_load_uniform
||
706 var
->data
.location_frac
== nir_intrinsic_component(instr
)) &&
708 fprintf(fp
, "\t/* %s */", var
->name
);
715 print_tex_instr(nir_tex_instr
*instr
, print_state
*state
)
717 FILE *fp
= state
->fp
;
719 print_dest(&instr
->dest
, state
);
739 case nir_texop_txf_ms
:
740 fprintf(fp
, "txf_ms ");
742 case nir_texop_txf_ms_mcs
:
743 fprintf(fp
, "txf_ms_mcs ");
754 case nir_texop_query_levels
:
755 fprintf(fp
, "query_levels ");
757 case nir_texop_texture_samples
:
758 fprintf(fp
, "texture_samples ");
760 case nir_texop_samples_identical
:
761 fprintf(fp
, "samples_identical ");
764 unreachable("Invalid texture operation");
768 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++) {
769 print_src(&instr
->src
[i
].src
, state
);
773 switch(instr
->src
[i
].src_type
) {
774 case nir_tex_src_coord
:
775 fprintf(fp
, "(coord)");
777 case nir_tex_src_projector
:
778 fprintf(fp
, "(projector)");
780 case nir_tex_src_comparator
:
781 fprintf(fp
, "(comparator)");
783 case nir_tex_src_offset
:
784 fprintf(fp
, "(offset)");
786 case nir_tex_src_bias
:
787 fprintf(fp
, "(bias)");
789 case nir_tex_src_lod
:
790 fprintf(fp
, "(lod)");
792 case nir_tex_src_ms_index
:
793 fprintf(fp
, "(ms_index)");
795 case nir_tex_src_ms_mcs
:
796 fprintf(fp
, "(ms_mcs)");
798 case nir_tex_src_ddx
:
799 fprintf(fp
, "(ddx)");
801 case nir_tex_src_ddy
:
802 fprintf(fp
, "(ddy)");
804 case nir_tex_src_texture_offset
:
805 fprintf(fp
, "(texture_offset)");
807 case nir_tex_src_sampler_offset
:
808 fprintf(fp
, "(sampler_offset)");
810 case nir_tex_src_plane
:
811 fprintf(fp
, "(plane)");
815 unreachable("Invalid texture source type");
822 if (instr
->op
== nir_texop_tg4
) {
823 fprintf(fp
, "%u (gather_component), ", instr
->component
);
826 if (instr
->texture
) {
827 print_deref(instr
->texture
, state
);
828 fprintf(fp
, " (texture)");
829 if (instr
->sampler
) {
830 print_deref(instr
->sampler
, state
);
831 fprintf(fp
, " (sampler)");
834 assert(instr
->sampler
== NULL
);
835 fprintf(fp
, "%u (texture) %u (sampler)",
836 instr
->texture_index
, instr
->sampler_index
);
841 print_call_instr(nir_call_instr
*instr
, print_state
*state
)
843 FILE *fp
= state
->fp
;
845 fprintf(fp
, "call %s ", instr
->callee
->name
);
847 for (unsigned i
= 0; i
< instr
->num_params
; i
++) {
851 print_deref(instr
->params
[i
], state
);
854 if (instr
->return_deref
!= NULL
) {
855 if (instr
->num_params
!= 0)
857 fprintf(fp
, "returning ");
858 print_deref(instr
->return_deref
, state
);
863 print_load_const_instr(nir_load_const_instr
*instr
, print_state
*state
)
865 FILE *fp
= state
->fp
;
867 print_ssa_def(&instr
->def
, state
);
869 fprintf(fp
, " = load_const (");
871 for (unsigned i
= 0; i
< instr
->def
.num_components
; i
++) {
876 * we don't really know the type of the constant (if it will be used as a
877 * float or an int), so just print the raw constant in hex for fidelity
878 * and then print the float in a comment for readability.
881 switch (instr
->def
.bit_size
) {
883 fprintf(fp
, "0x%16" PRIx64
" /* %f */", instr
->value
.u64
[i
],
884 instr
->value
.f64
[i
]);
887 fprintf(fp
, "0x%08x /* %f */", instr
->value
.u32
[i
], instr
->value
.f32
[i
]);
890 fprintf(fp
, "0x%04x /* %f */", instr
->value
.u16
[i
],
891 _mesa_half_to_float(instr
->value
.u16
[i
]));
894 fprintf(fp
, "0x%02x", instr
->value
.u8
[i
]);
903 print_jump_instr(nir_jump_instr
*instr
, print_state
*state
)
905 FILE *fp
= state
->fp
;
907 switch (instr
->type
) {
909 fprintf(fp
, "break");
912 case nir_jump_continue
:
913 fprintf(fp
, "continue");
916 case nir_jump_return
:
917 fprintf(fp
, "return");
923 print_ssa_undef_instr(nir_ssa_undef_instr
* instr
, print_state
*state
)
925 FILE *fp
= state
->fp
;
926 print_ssa_def(&instr
->def
, state
);
927 fprintf(fp
, " = undefined");
931 print_phi_instr(nir_phi_instr
*instr
, print_state
*state
)
933 FILE *fp
= state
->fp
;
934 print_dest(&instr
->dest
, state
);
935 fprintf(fp
, " = phi ");
936 nir_foreach_phi_src(src
, instr
) {
937 if (&src
->node
!= exec_list_get_head(&instr
->srcs
))
940 fprintf(fp
, "block_%u: ", src
->pred
->index
);
941 print_src(&src
->src
, state
);
946 print_parallel_copy_instr(nir_parallel_copy_instr
*instr
, print_state
*state
)
948 FILE *fp
= state
->fp
;
949 nir_foreach_parallel_copy_entry(entry
, instr
) {
950 if (&entry
->node
!= exec_list_get_head(&instr
->entries
))
953 print_dest(&entry
->dest
, state
);
955 print_src(&entry
->src
, state
);
960 print_instr(const nir_instr
*instr
, print_state
*state
, unsigned tabs
)
962 FILE *fp
= state
->fp
;
963 print_tabs(tabs
, fp
);
965 switch (instr
->type
) {
966 case nir_instr_type_alu
:
967 print_alu_instr(nir_instr_as_alu(instr
), state
);
970 case nir_instr_type_call
:
971 print_call_instr(nir_instr_as_call(instr
), state
);
974 case nir_instr_type_intrinsic
:
975 print_intrinsic_instr(nir_instr_as_intrinsic(instr
), state
);
978 case nir_instr_type_tex
:
979 print_tex_instr(nir_instr_as_tex(instr
), state
);
982 case nir_instr_type_load_const
:
983 print_load_const_instr(nir_instr_as_load_const(instr
), state
);
986 case nir_instr_type_jump
:
987 print_jump_instr(nir_instr_as_jump(instr
), state
);
990 case nir_instr_type_ssa_undef
:
991 print_ssa_undef_instr(nir_instr_as_ssa_undef(instr
), state
);
994 case nir_instr_type_phi
:
995 print_phi_instr(nir_instr_as_phi(instr
), state
);
998 case nir_instr_type_parallel_copy
:
999 print_parallel_copy_instr(nir_instr_as_parallel_copy(instr
), state
);
1003 unreachable("Invalid instruction type");
1009 compare_block_index(const void *p1
, const void *p2
)
1011 const nir_block
*block1
= *((const nir_block
**) p1
);
1012 const nir_block
*block2
= *((const nir_block
**) p2
);
1014 return (int) block1
->index
- (int) block2
->index
;
1017 static void print_cf_node(nir_cf_node
*node
, print_state
*state
,
1021 print_block(nir_block
*block
, print_state
*state
, unsigned tabs
)
1023 FILE *fp
= state
->fp
;
1025 print_tabs(tabs
, fp
);
1026 fprintf(fp
, "block block_%u:\n", block
->index
);
1028 /* sort the predecessors by index so we consistently print the same thing */
1031 malloc(block
->predecessors
->entries
* sizeof(nir_block
*));
1033 struct set_entry
*entry
;
1035 set_foreach(block
->predecessors
, entry
) {
1036 preds
[i
++] = (nir_block
*) entry
->key
;
1039 qsort(preds
, block
->predecessors
->entries
, sizeof(nir_block
*),
1040 compare_block_index
);
1042 print_tabs(tabs
, fp
);
1043 fprintf(fp
, "/* preds: ");
1044 for (unsigned i
= 0; i
< block
->predecessors
->entries
; i
++) {
1045 fprintf(fp
, "block_%u ", preds
[i
]->index
);
1047 fprintf(fp
, "*/\n");
1051 nir_foreach_instr(instr
, block
) {
1052 print_instr(instr
, state
, tabs
);
1054 print_annotation(state
, instr
);
1057 print_tabs(tabs
, fp
);
1058 fprintf(fp
, "/* succs: ");
1059 for (unsigned i
= 0; i
< 2; i
++)
1060 if (block
->successors
[i
]) {
1061 fprintf(fp
, "block_%u ", block
->successors
[i
]->index
);
1063 fprintf(fp
, "*/\n");
1067 print_if(nir_if
*if_stmt
, print_state
*state
, unsigned tabs
)
1069 FILE *fp
= state
->fp
;
1071 print_tabs(tabs
, fp
);
1073 print_src(&if_stmt
->condition
, state
);
1074 fprintf(fp
, " {\n");
1075 foreach_list_typed(nir_cf_node
, node
, node
, &if_stmt
->then_list
) {
1076 print_cf_node(node
, state
, tabs
+ 1);
1078 print_tabs(tabs
, fp
);
1079 fprintf(fp
, "} else {\n");
1080 foreach_list_typed(nir_cf_node
, node
, node
, &if_stmt
->else_list
) {
1081 print_cf_node(node
, state
, tabs
+ 1);
1083 print_tabs(tabs
, fp
);
1088 print_loop(nir_loop
*loop
, print_state
*state
, unsigned tabs
)
1090 FILE *fp
= state
->fp
;
1092 print_tabs(tabs
, fp
);
1093 fprintf(fp
, "loop {\n");
1094 foreach_list_typed(nir_cf_node
, node
, node
, &loop
->body
) {
1095 print_cf_node(node
, state
, tabs
+ 1);
1097 print_tabs(tabs
, fp
);
1102 print_cf_node(nir_cf_node
*node
, print_state
*state
, unsigned int tabs
)
1104 switch (node
->type
) {
1105 case nir_cf_node_block
:
1106 print_block(nir_cf_node_as_block(node
), state
, tabs
);
1109 case nir_cf_node_if
:
1110 print_if(nir_cf_node_as_if(node
), state
, tabs
);
1113 case nir_cf_node_loop
:
1114 print_loop(nir_cf_node_as_loop(node
), state
, tabs
);
1118 unreachable("Invalid CFG node type");
1123 print_function_impl(nir_function_impl
*impl
, print_state
*state
)
1125 FILE *fp
= state
->fp
;
1127 fprintf(fp
, "\nimpl %s ", impl
->function
->name
);
1129 for (unsigned i
= 0; i
< impl
->num_params
; i
++) {
1133 print_arg(impl
->params
[i
], state
);
1136 if (impl
->return_var
!= NULL
) {
1137 if (impl
->num_params
!= 0)
1139 fprintf(fp
, "returning ");
1140 print_arg(impl
->return_var
, state
);
1145 nir_foreach_variable(var
, &impl
->locals
) {
1147 print_var_decl(var
, state
);
1150 foreach_list_typed(nir_register
, reg
, node
, &impl
->registers
) {
1152 print_register_decl(reg
, state
);
1155 nir_index_blocks(impl
);
1157 foreach_list_typed(nir_cf_node
, node
, node
, &impl
->body
) {
1158 print_cf_node(node
, state
, 1);
1161 fprintf(fp
, "\tblock block_%u:\n}\n\n", impl
->end_block
->index
);
1165 print_function(nir_function
*function
, print_state
*state
)
1167 FILE *fp
= state
->fp
;
1169 fprintf(fp
, "decl_function %s ", function
->name
);
1171 for (unsigned i
= 0; i
< function
->num_params
; i
++) {
1175 switch (function
->params
[i
].param_type
) {
1176 case nir_parameter_in
:
1179 case nir_parameter_out
:
1180 fprintf(fp
, "out ");
1182 case nir_parameter_inout
:
1183 fprintf(fp
, "inout ");
1186 unreachable("Invalid parameter type");
1189 fprintf(fp
, "%s", glsl_get_type_name(function
->params
[i
].type
));
1192 if (function
->return_type
!= NULL
) {
1193 if (function
->num_params
!= 0)
1195 fprintf(fp
, "returning %s", glsl_get_type_name(function
->return_type
));
1200 if (function
->impl
!= NULL
) {
1201 print_function_impl(function
->impl
, state
);
1207 init_print_state(print_state
*state
, nir_shader
*shader
, FILE *fp
)
1210 state
->shader
= shader
;
1211 state
->ht
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
1212 _mesa_key_pointer_equal
);
1213 state
->syms
= _mesa_set_create(NULL
, _mesa_key_hash_string
,
1214 _mesa_key_string_equal
);
1219 destroy_print_state(print_state
*state
)
1221 _mesa_hash_table_destroy(state
->ht
, NULL
);
1222 _mesa_set_destroy(state
->syms
, NULL
);
1226 nir_print_shader_annotated(nir_shader
*shader
, FILE *fp
,
1227 struct hash_table
*annotations
)
1230 init_print_state(&state
, shader
, fp
);
1232 state
.annotations
= annotations
;
1234 fprintf(fp
, "shader: %s\n", gl_shader_stage_name(shader
->info
.stage
));
1236 if (shader
->info
.name
)
1237 fprintf(fp
, "name: %s\n", shader
->info
.name
);
1239 if (shader
->info
.label
)
1240 fprintf(fp
, "label: %s\n", shader
->info
.label
);
1242 switch (shader
->info
.stage
) {
1243 case MESA_SHADER_COMPUTE
:
1244 fprintf(fp
, "local-size: %u, %u, %u%s\n",
1245 shader
->info
.cs
.local_size
[0],
1246 shader
->info
.cs
.local_size
[1],
1247 shader
->info
.cs
.local_size
[2],
1248 shader
->info
.cs
.local_size_variable
? " (variable)" : "");
1249 fprintf(fp
, "shared-size: %u\n", shader
->info
.cs
.shared_size
);
1255 fprintf(fp
, "inputs: %u\n", shader
->num_inputs
);
1256 fprintf(fp
, "outputs: %u\n", shader
->num_outputs
);
1257 fprintf(fp
, "uniforms: %u\n", shader
->num_uniforms
);
1258 fprintf(fp
, "shared: %u\n", shader
->num_shared
);
1260 nir_foreach_variable(var
, &shader
->uniforms
) {
1261 print_var_decl(var
, &state
);
1264 nir_foreach_variable(var
, &shader
->inputs
) {
1265 print_var_decl(var
, &state
);
1268 nir_foreach_variable(var
, &shader
->outputs
) {
1269 print_var_decl(var
, &state
);
1272 nir_foreach_variable(var
, &shader
->shared
) {
1273 print_var_decl(var
, &state
);
1276 nir_foreach_variable(var
, &shader
->globals
) {
1277 print_var_decl(var
, &state
);
1280 nir_foreach_variable(var
, &shader
->system_values
) {
1281 print_var_decl(var
, &state
);
1284 foreach_list_typed(nir_register
, reg
, node
, &shader
->registers
) {
1285 print_register_decl(reg
, &state
);
1288 foreach_list_typed(nir_function
, func
, node
, &shader
->functions
) {
1289 print_function(func
, &state
);
1292 destroy_print_state(&state
);
1296 nir_print_shader(nir_shader
*shader
, FILE *fp
)
1298 nir_print_shader_annotated(shader
, fp
, NULL
);
1302 nir_print_instr(const nir_instr
*instr
, FILE *fp
)
1304 print_state state
= {
1307 print_instr(instr
, &state
, 0);