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"
32 #include <inttypes.h> /* for PRIx64 macro */
34 #if defined(_WIN32) && !defined(snprintf)
35 #define snprintf _snprintf
39 print_tabs(unsigned num_tabs
, FILE *fp
)
41 for (unsigned i
= 0; i
< num_tabs
; i
++)
48 /** map from nir_variable -> printable name */
49 struct hash_table
*ht
;
51 /** set of names used so far for nir_variables */
54 /* an index used to make new non-conflicting names */
59 print_register(nir_register
*reg
, print_state
*state
)
62 if (reg
->name
!= NULL
)
63 fprintf(fp
, "/* %s */ ", reg
->name
);
65 fprintf(fp
, "gr%u", reg
->index
);
67 fprintf(fp
, "r%u", reg
->index
);
70 static const char *sizes
[] = { "error", "vec1", "vec2", "vec3", "vec4" };
73 print_register_decl(nir_register
*reg
, print_state
*state
)
76 fprintf(fp
, "decl_reg %s %u ", sizes
[reg
->num_components
], reg
->bit_size
);
78 fprintf(fp
, "(packed) ");
79 print_register(reg
, state
);
80 if (reg
->num_array_elems
!= 0)
81 fprintf(fp
, "[%u]", reg
->num_array_elems
);
86 print_ssa_def(nir_ssa_def
*def
, print_state
*state
)
89 if (def
->name
!= NULL
)
90 fprintf(fp
, "/* %s */ ", def
->name
);
91 fprintf(fp
, "%s %u ssa_%u", sizes
[def
->num_components
], def
->bit_size
,
96 print_ssa_use(nir_ssa_def
*def
, print_state
*state
)
99 if (def
->name
!= NULL
)
100 fprintf(fp
, "/* %s */ ", def
->name
);
101 fprintf(fp
, "ssa_%u", def
->index
);
104 static void print_src(nir_src
*src
, print_state
*state
);
107 print_reg_src(nir_reg_src
*src
, print_state
*state
)
109 FILE *fp
= state
->fp
;
110 print_register(src
->reg
, state
);
111 if (src
->reg
->num_array_elems
!= 0) {
112 fprintf(fp
, "[%u", src
->base_offset
);
113 if (src
->indirect
!= NULL
) {
115 print_src(src
->indirect
, state
);
122 print_reg_dest(nir_reg_dest
*dest
, print_state
*state
)
124 FILE *fp
= state
->fp
;
125 print_register(dest
->reg
, state
);
126 if (dest
->reg
->num_array_elems
!= 0) {
127 fprintf(fp
, "[%u", dest
->base_offset
);
128 if (dest
->indirect
!= NULL
) {
130 print_src(dest
->indirect
, state
);
137 print_src(nir_src
*src
, print_state
*state
)
140 print_ssa_use(src
->ssa
, state
);
142 print_reg_src(&src
->reg
, state
);
146 print_dest(nir_dest
*dest
, print_state
*state
)
149 print_ssa_def(&dest
->ssa
, state
);
151 print_reg_dest(&dest
->reg
, state
);
155 print_alu_src(nir_alu_instr
*instr
, unsigned src
, print_state
*state
)
157 FILE *fp
= state
->fp
;
159 if (instr
->src
[src
].negate
)
161 if (instr
->src
[src
].abs
)
164 print_src(&instr
->src
[src
].src
, state
);
166 bool print_swizzle
= false;
167 for (unsigned i
= 0; i
< 4; i
++) {
168 if (!nir_alu_instr_channel_used(instr
, src
, i
))
171 if (instr
->src
[src
].swizzle
[i
] != i
) {
172 print_swizzle
= true;
179 for (unsigned i
= 0; i
< 4; i
++) {
180 if (!nir_alu_instr_channel_used(instr
, src
, i
))
183 fprintf(fp
, "%c", "xyzw"[instr
->src
[src
].swizzle
[i
]]);
187 if (instr
->src
[src
].abs
)
192 print_alu_dest(nir_alu_dest
*dest
, print_state
*state
)
194 FILE *fp
= state
->fp
;
195 /* we're going to print the saturate modifier later, after the opcode */
197 print_dest(&dest
->dest
, state
);
199 if (!dest
->dest
.is_ssa
&&
200 dest
->write_mask
!= (1 << dest
->dest
.reg
.reg
->num_components
) - 1) {
202 for (unsigned i
= 0; i
< 4; i
++)
203 if ((dest
->write_mask
>> i
) & 1)
204 fprintf(fp
, "%c", "xyzw"[i
]);
209 print_alu_instr(nir_alu_instr
*instr
, print_state
*state
)
211 FILE *fp
= state
->fp
;
213 print_alu_dest(&instr
->dest
, state
);
215 fprintf(fp
, " = %s", nir_op_infos
[instr
->op
].name
);
218 if (instr
->dest
.saturate
)
222 for (unsigned i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++) {
226 print_alu_src(instr
, i
, state
);
231 get_var_name(nir_variable
*var
, print_state
*state
)
233 if (state
->ht
== NULL
)
238 struct hash_entry
*entry
= _mesa_hash_table_search(state
->ht
, var
);
243 if (var
->name
== NULL
) {
244 name
= ralloc_asprintf(state
->syms
, "@%u", state
->index
++);
246 struct set_entry
*set_entry
= _mesa_set_search(state
->syms
, var
->name
);
247 if (set_entry
!= NULL
) {
248 /* we have a collision with another name, append an @ + a unique
250 name
= ralloc_asprintf(state
->syms
, "%s@%u", var
->name
,
253 /* Mark this one as seen */
254 _mesa_set_add(state
->syms
, var
->name
);
259 _mesa_hash_table_insert(state
->ht
, var
, name
);
265 print_constant(nir_constant
*c
, const struct glsl_type
*type
, print_state
*state
)
267 FILE *fp
= state
->fp
;
268 unsigned total_elems
= glsl_get_components(type
);
271 switch (glsl_get_base_type(type
)) {
275 for (i
= 0; i
< total_elems
; i
++) {
276 if (i
> 0) fprintf(fp
, ", ");
277 fprintf(fp
, "0x%08x", c
->value
.u
[i
]);
281 case GLSL_TYPE_FLOAT
:
282 for (i
= 0; i
< total_elems
; i
++) {
283 if (i
> 0) fprintf(fp
, ", ");
284 fprintf(fp
, "%f", c
->value
.f
[i
]);
288 case GLSL_TYPE_DOUBLE
:
289 for (i
= 0; i
< total_elems
; i
++) {
290 if (i
> 0) fprintf(fp
, ", ");
291 fprintf(fp
, "%f", c
->value
.d
[i
]);
295 case GLSL_TYPE_STRUCT
:
296 for (i
= 0; i
< c
->num_elements
; i
++) {
297 if (i
> 0) fprintf(fp
, ", ");
299 print_constant(c
->elements
[i
], glsl_get_struct_field(type
, i
), state
);
304 case GLSL_TYPE_ARRAY
:
305 for (i
= 0; i
< c
->num_elements
; i
++) {
306 if (i
> 0) fprintf(fp
, ", ");
308 print_constant(c
->elements
[i
], glsl_get_array_element(type
), state
);
314 unreachable("not reached");
319 get_variable_mode_str(nir_variable_mode mode
)
322 case nir_var_shader_in
:
324 case nir_var_shader_out
:
326 case nir_var_uniform
:
328 case nir_var_shader_storage
:
329 return "shader_storage";
330 case nir_var_system_value
:
343 print_var_decl(nir_variable
*var
, print_state
*state
)
345 FILE *fp
= state
->fp
;
347 fprintf(fp
, "decl_var ");
349 const char *const cent
= (var
->data
.centroid
) ? "centroid " : "";
350 const char *const samp
= (var
->data
.sample
) ? "sample " : "";
351 const char *const patch
= (var
->data
.patch
) ? "patch " : "";
352 const char *const inv
= (var
->data
.invariant
) ? "invariant " : "";
353 fprintf(fp
, "%s%s%s%s%s %s ",
354 cent
, samp
, patch
, inv
, get_variable_mode_str(var
->data
.mode
),
355 glsl_interp_qualifier_name(var
->data
.interpolation
));
357 const char *const coher
= (var
->data
.image
.coherent
) ? "coherent " : "";
358 const char *const volat
= (var
->data
.image
._volatile
) ? "volatile " : "";
359 const char *const restr
= (var
->data
.image
.restrict_flag
) ? "restrict " : "";
360 const char *const ronly
= (var
->data
.image
.read_only
) ? "readonly " : "";
361 const char *const wonly
= (var
->data
.image
.write_only
) ? "writeonly " : "";
362 fprintf(fp
, "%s%s%s%s%s", coher
, volat
, restr
, ronly
, wonly
);
364 glsl_print_type(var
->type
, fp
);
366 fprintf(fp
, " %s", get_var_name(var
, state
));
368 if (var
->data
.mode
== nir_var_shader_in
||
369 var
->data
.mode
== nir_var_shader_out
||
370 var
->data
.mode
== nir_var_uniform
||
371 var
->data
.mode
== nir_var_shader_storage
) {
372 const char *loc
= NULL
;
375 switch (state
->shader
->stage
) {
376 case MESA_SHADER_VERTEX
:
377 if (var
->data
.mode
== nir_var_shader_in
)
378 loc
= gl_vert_attrib_name(var
->data
.location
);
379 else if (var
->data
.mode
== nir_var_shader_out
)
380 loc
= gl_varying_slot_name(var
->data
.location
);
382 case MESA_SHADER_GEOMETRY
:
383 if ((var
->data
.mode
== nir_var_shader_in
) ||
384 (var
->data
.mode
== nir_var_shader_out
))
385 loc
= gl_varying_slot_name(var
->data
.location
);
387 case MESA_SHADER_FRAGMENT
:
388 if (var
->data
.mode
== nir_var_shader_in
)
389 loc
= gl_varying_slot_name(var
->data
.location
);
390 else if (var
->data
.mode
== nir_var_shader_out
)
391 loc
= gl_frag_result_name(var
->data
.location
);
393 case MESA_SHADER_TESS_CTRL
:
394 case MESA_SHADER_TESS_EVAL
:
395 case MESA_SHADER_COMPUTE
:
402 snprintf(buf
, sizeof(buf
), "%u", var
->data
.location
);
406 fprintf(fp
, " (%s, %u)", loc
, var
->data
.driver_location
);
409 if (var
->constant_initializer
) {
410 fprintf(fp
, " = { ");
411 print_constant(var
->constant_initializer
, var
->type
, state
);
419 print_var(nir_variable
*var
, print_state
*state
)
421 FILE *fp
= state
->fp
;
422 fprintf(fp
, "%s", get_var_name(var
, state
));
426 print_arg(nir_variable
*var
, print_state
*state
)
428 FILE *fp
= state
->fp
;
429 glsl_print_type(var
->type
, fp
);
430 fprintf(fp
, " %s", get_var_name(var
, state
));
434 print_deref_var(nir_deref_var
*deref
, print_state
*state
)
436 print_var(deref
->var
, state
);
440 print_deref_array(nir_deref_array
*deref
, print_state
*state
)
442 FILE *fp
= state
->fp
;
444 switch (deref
->deref_array_type
) {
445 case nir_deref_array_type_direct
:
446 fprintf(fp
, "%u", deref
->base_offset
);
448 case nir_deref_array_type_indirect
:
449 if (deref
->base_offset
!= 0)
450 fprintf(fp
, "%u + ", deref
->base_offset
);
451 print_src(&deref
->indirect
, state
);
453 case nir_deref_array_type_wildcard
:
461 print_deref_struct(nir_deref_struct
*deref
, const struct glsl_type
*parent_type
,
464 FILE *fp
= state
->fp
;
465 fprintf(fp
, ".%s", glsl_get_struct_elem_name(parent_type
, deref
->index
));
469 print_deref(nir_deref_var
*deref
, print_state
*state
)
471 nir_deref
*tail
= &deref
->deref
;
472 nir_deref
*pretail
= NULL
;
473 while (tail
!= NULL
) {
474 switch (tail
->deref_type
) {
475 case nir_deref_type_var
:
476 assert(pretail
== NULL
);
477 assert(tail
== &deref
->deref
);
478 print_deref_var(deref
, state
);
481 case nir_deref_type_array
:
482 assert(pretail
!= NULL
);
483 print_deref_array(nir_deref_as_array(tail
), state
);
486 case nir_deref_type_struct
:
487 assert(pretail
!= NULL
);
488 print_deref_struct(nir_deref_as_struct(tail
),
489 pretail
->type
, state
);
493 unreachable("Invalid deref type");
497 tail
= pretail
->child
;
502 print_intrinsic_instr(nir_intrinsic_instr
*instr
, print_state
*state
)
504 const nir_intrinsic_info
*info
= &nir_intrinsic_infos
[instr
->intrinsic
];
505 unsigned num_srcs
= info
->num_srcs
;
506 FILE *fp
= state
->fp
;
508 if (info
->has_dest
) {
509 print_dest(&instr
->dest
, state
);
513 fprintf(fp
, "intrinsic %s (", info
->name
);
515 for (unsigned i
= 0; i
< num_srcs
; i
++) {
519 print_src(&instr
->src
[i
], state
);
524 for (unsigned i
= 0; i
< info
->num_variables
; i
++) {
528 print_deref(instr
->variables
[i
], state
);
533 for (unsigned i
= 0; i
< info
->num_indices
; i
++) {
537 fprintf(fp
, "%d", instr
->const_index
[i
]);
542 static const char *index_name
[NIR_INTRINSIC_NUM_INDEX_FLAGS
] = {
543 [NIR_INTRINSIC_BASE
] = "base",
544 [NIR_INTRINSIC_WRMASK
] = "wrmask",
545 [NIR_INTRINSIC_STREAM_ID
] = "stream-id",
546 [NIR_INTRINSIC_UCP_ID
] = "ucp-id",
547 [NIR_INTRINSIC_RANGE
] = "range",
548 [NIR_INTRINSIC_DESC_SET
] = "desc-set",
549 [NIR_INTRINSIC_BINDING
] = "binding",
551 for (unsigned idx
= 1; idx
< NIR_INTRINSIC_NUM_INDEX_FLAGS
; idx
++) {
552 if (!info
->index_map
[idx
])
555 if (idx
== NIR_INTRINSIC_WRMASK
) {
556 /* special case wrmask to show it as a writemask.. */
557 unsigned wrmask
= nir_intrinsic_write_mask(instr
);
558 fprintf(fp
, " wrmask=");
559 for (unsigned i
= 0; i
< 4; i
++)
560 if ((wrmask
>> i
) & 1)
561 fprintf(fp
, "%c", "xyzw"[i
]);
563 unsigned off
= info
->index_map
[idx
] - 1;
564 assert(index_name
[idx
]); /* forgot to update index_name table? */
565 fprintf(fp
, " %s=%d", index_name
[idx
], instr
->const_index
[off
]);
573 struct exec_list
*var_list
= NULL
;
575 switch (instr
->intrinsic
) {
576 case nir_intrinsic_load_uniform
:
577 var_list
= &state
->shader
->uniforms
;
579 case nir_intrinsic_load_input
:
580 case nir_intrinsic_load_per_vertex_input
:
581 var_list
= &state
->shader
->inputs
;
583 case nir_intrinsic_load_output
:
584 case nir_intrinsic_store_output
:
585 case nir_intrinsic_store_per_vertex_output
:
586 var_list
= &state
->shader
->outputs
;
592 nir_foreach_variable(var
, var_list
) {
593 if ((var
->data
.driver_location
== nir_intrinsic_base(instr
)) &&
595 fprintf(fp
, "\t/* %s */", var
->name
);
602 print_tex_instr(nir_tex_instr
*instr
, print_state
*state
)
604 FILE *fp
= state
->fp
;
606 print_dest(&instr
->dest
, state
);
626 case nir_texop_txf_ms
:
627 fprintf(fp
, "txf_ms ");
629 case nir_texop_txf_ms_mcs
:
630 fprintf(fp
, "txf_ms_mcs ");
641 case nir_texop_query_levels
:
642 fprintf(fp
, "query_levels ");
644 case nir_texop_texture_samples
:
645 fprintf(fp
, "texture_samples ");
647 case nir_texop_samples_identical
:
648 fprintf(fp
, "samples_identical ");
651 unreachable("Invalid texture operation");
655 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++) {
656 print_src(&instr
->src
[i
].src
, state
);
660 switch(instr
->src
[i
].src_type
) {
661 case nir_tex_src_coord
:
662 fprintf(fp
, "(coord)");
664 case nir_tex_src_projector
:
665 fprintf(fp
, "(projector)");
667 case nir_tex_src_comparitor
:
668 fprintf(fp
, "(comparitor)");
670 case nir_tex_src_offset
:
671 fprintf(fp
, "(offset)");
673 case nir_tex_src_bias
:
674 fprintf(fp
, "(bias)");
676 case nir_tex_src_lod
:
677 fprintf(fp
, "(lod)");
679 case nir_tex_src_ms_index
:
680 fprintf(fp
, "(ms_index)");
682 case nir_tex_src_ms_mcs
:
683 fprintf(fp
, "(ms_mcs)");
685 case nir_tex_src_ddx
:
686 fprintf(fp
, "(ddx)");
688 case nir_tex_src_ddy
:
689 fprintf(fp
, "(ddy)");
691 case nir_tex_src_texture_offset
:
692 fprintf(fp
, "(texture_offset)");
694 case nir_tex_src_sampler_offset
:
695 fprintf(fp
, "(sampler_offset)");
699 unreachable("Invalid texture source type");
706 if (instr
->op
== nir_texop_tg4
) {
707 fprintf(fp
, "%u (gather_component), ", instr
->component
);
710 if (instr
->texture
) {
711 print_deref(instr
->texture
, state
);
712 fprintf(fp
, " (texture)");
713 if (instr
->sampler
) {
714 print_deref(instr
->sampler
, state
);
715 fprintf(fp
, " (sampler)");
718 assert(instr
->sampler
== NULL
);
719 fprintf(fp
, "%u (texture) %u (sampler)",
720 instr
->texture_index
, instr
->sampler_index
);
725 print_call_instr(nir_call_instr
*instr
, print_state
*state
)
727 FILE *fp
= state
->fp
;
729 fprintf(fp
, "call %s ", instr
->callee
->name
);
731 for (unsigned i
= 0; i
< instr
->num_params
; i
++) {
735 print_deref(instr
->params
[i
], state
);
738 if (instr
->return_deref
!= NULL
) {
739 if (instr
->num_params
!= 0)
741 fprintf(fp
, "returning ");
742 print_deref(instr
->return_deref
, state
);
747 print_load_const_instr(nir_load_const_instr
*instr
, print_state
*state
)
749 FILE *fp
= state
->fp
;
751 print_ssa_def(&instr
->def
, state
);
753 fprintf(fp
, " = load_const (");
755 for (unsigned i
= 0; i
< instr
->def
.num_components
; i
++) {
760 * we don't really know the type of the constant (if it will be used as a
761 * float or an int), so just print the raw constant in hex for fidelity
762 * and then print the float in a comment for readability.
765 if (instr
->def
.bit_size
== 64)
766 fprintf(fp
, "0x%16" PRIx64
" /* %f */", instr
->value
.u64
[i
],
767 instr
->value
.f64
[i
]);
769 fprintf(fp
, "0x%08x /* %f */", instr
->value
.u32
[i
], instr
->value
.f32
[i
]);
776 print_jump_instr(nir_jump_instr
*instr
, print_state
*state
)
778 FILE *fp
= state
->fp
;
780 switch (instr
->type
) {
782 fprintf(fp
, "break");
785 case nir_jump_continue
:
786 fprintf(fp
, "continue");
789 case nir_jump_return
:
790 fprintf(fp
, "return");
796 print_ssa_undef_instr(nir_ssa_undef_instr
* instr
, print_state
*state
)
798 FILE *fp
= state
->fp
;
799 print_ssa_def(&instr
->def
, state
);
800 fprintf(fp
, " = undefined");
804 print_phi_instr(nir_phi_instr
*instr
, print_state
*state
)
806 FILE *fp
= state
->fp
;
807 print_dest(&instr
->dest
, state
);
808 fprintf(fp
, " = phi ");
809 nir_foreach_phi_src(src
, instr
) {
810 if (&src
->node
!= exec_list_get_head(&instr
->srcs
))
813 fprintf(fp
, "block_%u: ", src
->pred
->index
);
814 print_src(&src
->src
, state
);
819 print_parallel_copy_instr(nir_parallel_copy_instr
*instr
, print_state
*state
)
821 FILE *fp
= state
->fp
;
822 nir_foreach_parallel_copy_entry(entry
, instr
) {
823 if (&entry
->node
!= exec_list_get_head(&instr
->entries
))
826 print_dest(&entry
->dest
, state
);
828 print_src(&entry
->src
, state
);
833 print_instr(const nir_instr
*instr
, print_state
*state
, unsigned tabs
)
835 FILE *fp
= state
->fp
;
836 print_tabs(tabs
, fp
);
838 switch (instr
->type
) {
839 case nir_instr_type_alu
:
840 print_alu_instr(nir_instr_as_alu(instr
), state
);
843 case nir_instr_type_call
:
844 print_call_instr(nir_instr_as_call(instr
), state
);
847 case nir_instr_type_intrinsic
:
848 print_intrinsic_instr(nir_instr_as_intrinsic(instr
), state
);
851 case nir_instr_type_tex
:
852 print_tex_instr(nir_instr_as_tex(instr
), state
);
855 case nir_instr_type_load_const
:
856 print_load_const_instr(nir_instr_as_load_const(instr
), state
);
859 case nir_instr_type_jump
:
860 print_jump_instr(nir_instr_as_jump(instr
), state
);
863 case nir_instr_type_ssa_undef
:
864 print_ssa_undef_instr(nir_instr_as_ssa_undef(instr
), state
);
867 case nir_instr_type_phi
:
868 print_phi_instr(nir_instr_as_phi(instr
), state
);
871 case nir_instr_type_parallel_copy
:
872 print_parallel_copy_instr(nir_instr_as_parallel_copy(instr
), state
);
876 unreachable("Invalid instruction type");
882 compare_block_index(const void *p1
, const void *p2
)
884 const nir_block
*block1
= *((const nir_block
**) p1
);
885 const nir_block
*block2
= *((const nir_block
**) p2
);
887 return (int) block1
->index
- (int) block2
->index
;
890 static void print_cf_node(nir_cf_node
*node
, print_state
*state
,
894 print_block(nir_block
*block
, print_state
*state
, unsigned tabs
)
896 FILE *fp
= state
->fp
;
898 print_tabs(tabs
, fp
);
899 fprintf(fp
, "block block_%u:\n", block
->index
);
901 /* sort the predecessors by index so we consistently print the same thing */
904 malloc(block
->predecessors
->entries
* sizeof(nir_block
*));
906 struct set_entry
*entry
;
908 set_foreach(block
->predecessors
, entry
) {
909 preds
[i
++] = (nir_block
*) entry
->key
;
912 qsort(preds
, block
->predecessors
->entries
, sizeof(nir_block
*),
913 compare_block_index
);
915 print_tabs(tabs
, fp
);
916 fprintf(fp
, "/* preds: ");
917 for (unsigned i
= 0; i
< block
->predecessors
->entries
; i
++) {
918 fprintf(fp
, "block_%u ", preds
[i
]->index
);
924 nir_foreach_instr(instr
, block
) {
925 print_instr(instr
, state
, tabs
);
929 print_tabs(tabs
, fp
);
930 fprintf(fp
, "/* succs: ");
931 for (unsigned i
= 0; i
< 2; i
++)
932 if (block
->successors
[i
]) {
933 fprintf(fp
, "block_%u ", block
->successors
[i
]->index
);
939 print_if(nir_if
*if_stmt
, print_state
*state
, unsigned tabs
)
941 FILE *fp
= state
->fp
;
943 print_tabs(tabs
, fp
);
945 print_src(&if_stmt
->condition
, state
);
947 foreach_list_typed(nir_cf_node
, node
, node
, &if_stmt
->then_list
) {
948 print_cf_node(node
, state
, tabs
+ 1);
950 print_tabs(tabs
, fp
);
951 fprintf(fp
, "} else {\n");
952 foreach_list_typed(nir_cf_node
, node
, node
, &if_stmt
->else_list
) {
953 print_cf_node(node
, state
, tabs
+ 1);
955 print_tabs(tabs
, fp
);
960 print_loop(nir_loop
*loop
, print_state
*state
, unsigned tabs
)
962 FILE *fp
= state
->fp
;
964 print_tabs(tabs
, fp
);
965 fprintf(fp
, "loop {\n");
966 foreach_list_typed(nir_cf_node
, node
, node
, &loop
->body
) {
967 print_cf_node(node
, state
, tabs
+ 1);
969 print_tabs(tabs
, fp
);
974 print_cf_node(nir_cf_node
*node
, print_state
*state
, unsigned int tabs
)
976 switch (node
->type
) {
977 case nir_cf_node_block
:
978 print_block(nir_cf_node_as_block(node
), state
, tabs
);
982 print_if(nir_cf_node_as_if(node
), state
, tabs
);
985 case nir_cf_node_loop
:
986 print_loop(nir_cf_node_as_loop(node
), state
, tabs
);
990 unreachable("Invalid CFG node type");
995 print_function_impl(nir_function_impl
*impl
, print_state
*state
)
997 FILE *fp
= state
->fp
;
999 fprintf(fp
, "\nimpl %s ", impl
->function
->name
);
1001 for (unsigned i
= 0; i
< impl
->num_params
; i
++) {
1005 print_arg(impl
->params
[i
], state
);
1008 if (impl
->return_var
!= NULL
) {
1009 if (impl
->num_params
!= 0)
1011 fprintf(fp
, "returning ");
1012 print_arg(impl
->return_var
, state
);
1017 nir_foreach_variable(var
, &impl
->locals
) {
1019 print_var_decl(var
, state
);
1022 foreach_list_typed(nir_register
, reg
, node
, &impl
->registers
) {
1024 print_register_decl(reg
, state
);
1027 nir_index_blocks(impl
);
1029 foreach_list_typed(nir_cf_node
, node
, node
, &impl
->body
) {
1030 print_cf_node(node
, state
, 1);
1033 fprintf(fp
, "\tblock block_%u:\n}\n\n", impl
->end_block
->index
);
1037 print_function(nir_function
*function
, print_state
*state
)
1039 FILE *fp
= state
->fp
;
1041 fprintf(fp
, "decl_function %s ", function
->name
);
1043 for (unsigned i
= 0; i
< function
->num_params
; i
++) {
1047 switch (function
->params
[i
].param_type
) {
1048 case nir_parameter_in
:
1051 case nir_parameter_out
:
1052 fprintf(fp
, "out ");
1054 case nir_parameter_inout
:
1055 fprintf(fp
, "inout ");
1058 unreachable("Invalid parameter type");
1061 glsl_print_type(function
->params
[i
].type
, fp
);
1064 if (function
->return_type
!= NULL
) {
1065 if (function
->num_params
!= 0)
1067 fprintf(fp
, "returning ");
1068 glsl_print_type(function
->return_type
, fp
);
1073 if (function
->impl
!= NULL
) {
1074 print_function_impl(function
->impl
, state
);
1080 init_print_state(print_state
*state
, nir_shader
*shader
, FILE *fp
)
1083 state
->shader
= shader
;
1084 state
->ht
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
1085 _mesa_key_pointer_equal
);
1086 state
->syms
= _mesa_set_create(NULL
, _mesa_key_hash_string
,
1087 _mesa_key_string_equal
);
1092 destroy_print_state(print_state
*state
)
1094 _mesa_hash_table_destroy(state
->ht
, NULL
);
1095 _mesa_set_destroy(state
->syms
, NULL
);
1099 nir_print_shader(nir_shader
*shader
, FILE *fp
)
1102 init_print_state(&state
, shader
, fp
);
1104 fprintf(fp
, "shader: %s\n", gl_shader_stage_name(shader
->stage
));
1106 if (shader
->info
.name
)
1107 fprintf(fp
, "name: %s\n", shader
->info
.name
);
1109 if (shader
->info
.label
)
1110 fprintf(fp
, "label: %s\n", shader
->info
.label
);
1112 fprintf(fp
, "inputs: %u\n", shader
->num_inputs
);
1113 fprintf(fp
, "outputs: %u\n", shader
->num_outputs
);
1114 fprintf(fp
, "uniforms: %u\n", shader
->num_uniforms
);
1115 fprintf(fp
, "shared: %u\n", shader
->num_shared
);
1117 nir_foreach_variable(var
, &shader
->uniforms
) {
1118 print_var_decl(var
, &state
);
1121 nir_foreach_variable(var
, &shader
->inputs
) {
1122 print_var_decl(var
, &state
);
1125 nir_foreach_variable(var
, &shader
->outputs
) {
1126 print_var_decl(var
, &state
);
1129 nir_foreach_variable(var
, &shader
->shared
) {
1130 print_var_decl(var
, &state
);
1133 nir_foreach_variable(var
, &shader
->globals
) {
1134 print_var_decl(var
, &state
);
1137 nir_foreach_variable(var
, &shader
->system_values
) {
1138 print_var_decl(var
, &state
);
1141 foreach_list_typed(nir_register
, reg
, node
, &shader
->registers
) {
1142 print_register_decl(reg
, &state
);
1145 foreach_list_typed(nir_function
, func
, node
, &shader
->functions
) {
1146 print_function(func
, &state
);
1149 destroy_print_state(&state
);
1153 nir_print_instr(const nir_instr
*instr
, FILE *fp
)
1155 print_state state
= {
1158 print_instr(instr
, &state
, 0);