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 glsl_print_type(var
->type
, fp
);
359 fprintf(fp
, " %s", get_var_name(var
, state
));
361 if (var
->data
.mode
== nir_var_shader_in
||
362 var
->data
.mode
== nir_var_shader_out
||
363 var
->data
.mode
== nir_var_uniform
||
364 var
->data
.mode
== nir_var_shader_storage
) {
365 const char *loc
= NULL
;
368 switch (state
->shader
->stage
) {
369 case MESA_SHADER_VERTEX
:
370 if (var
->data
.mode
== nir_var_shader_in
)
371 loc
= gl_vert_attrib_name(var
->data
.location
);
372 else if (var
->data
.mode
== nir_var_shader_out
)
373 loc
= gl_varying_slot_name(var
->data
.location
);
375 case MESA_SHADER_GEOMETRY
:
376 if ((var
->data
.mode
== nir_var_shader_in
) ||
377 (var
->data
.mode
== nir_var_shader_out
))
378 loc
= gl_varying_slot_name(var
->data
.location
);
380 case MESA_SHADER_FRAGMENT
:
381 if (var
->data
.mode
== nir_var_shader_in
)
382 loc
= gl_varying_slot_name(var
->data
.location
);
383 else if (var
->data
.mode
== nir_var_shader_out
)
384 loc
= gl_frag_result_name(var
->data
.location
);
386 case MESA_SHADER_TESS_CTRL
:
387 case MESA_SHADER_TESS_EVAL
:
388 case MESA_SHADER_COMPUTE
:
395 snprintf(buf
, sizeof(buf
), "%u", var
->data
.location
);
399 fprintf(fp
, " (%s, %u)", loc
, var
->data
.driver_location
);
402 if (var
->constant_initializer
) {
403 fprintf(fp
, " = { ");
404 print_constant(var
->constant_initializer
, var
->type
, state
);
412 print_var(nir_variable
*var
, print_state
*state
)
414 FILE *fp
= state
->fp
;
415 fprintf(fp
, "%s", get_var_name(var
, state
));
419 print_arg(nir_variable
*var
, print_state
*state
)
421 FILE *fp
= state
->fp
;
422 glsl_print_type(var
->type
, fp
);
423 fprintf(fp
, " %s", get_var_name(var
, state
));
427 print_deref_var(nir_deref_var
*deref
, print_state
*state
)
429 print_var(deref
->var
, state
);
433 print_deref_array(nir_deref_array
*deref
, print_state
*state
)
435 FILE *fp
= state
->fp
;
437 switch (deref
->deref_array_type
) {
438 case nir_deref_array_type_direct
:
439 fprintf(fp
, "%u", deref
->base_offset
);
441 case nir_deref_array_type_indirect
:
442 if (deref
->base_offset
!= 0)
443 fprintf(fp
, "%u + ", deref
->base_offset
);
444 print_src(&deref
->indirect
, state
);
446 case nir_deref_array_type_wildcard
:
454 print_deref_struct(nir_deref_struct
*deref
, const struct glsl_type
*parent_type
,
457 FILE *fp
= state
->fp
;
458 fprintf(fp
, ".%s", glsl_get_struct_elem_name(parent_type
, deref
->index
));
462 print_deref(nir_deref_var
*deref
, print_state
*state
)
464 nir_deref
*tail
= &deref
->deref
;
465 nir_deref
*pretail
= NULL
;
466 while (tail
!= NULL
) {
467 switch (tail
->deref_type
) {
468 case nir_deref_type_var
:
469 assert(pretail
== NULL
);
470 assert(tail
== &deref
->deref
);
471 print_deref_var(deref
, state
);
474 case nir_deref_type_array
:
475 assert(pretail
!= NULL
);
476 print_deref_array(nir_deref_as_array(tail
), state
);
479 case nir_deref_type_struct
:
480 assert(pretail
!= NULL
);
481 print_deref_struct(nir_deref_as_struct(tail
),
482 pretail
->type
, state
);
486 unreachable("Invalid deref type");
490 tail
= pretail
->child
;
495 print_intrinsic_instr(nir_intrinsic_instr
*instr
, print_state
*state
)
497 const nir_intrinsic_info
*info
= &nir_intrinsic_infos
[instr
->intrinsic
];
498 unsigned num_srcs
= info
->num_srcs
;
499 FILE *fp
= state
->fp
;
501 if (info
->has_dest
) {
502 print_dest(&instr
->dest
, state
);
506 fprintf(fp
, "intrinsic %s (", info
->name
);
508 for (unsigned i
= 0; i
< num_srcs
; i
++) {
512 print_src(&instr
->src
[i
], state
);
517 for (unsigned i
= 0; i
< info
->num_variables
; i
++) {
521 print_deref(instr
->variables
[i
], state
);
526 for (unsigned i
= 0; i
< info
->num_indices
; i
++) {
530 fprintf(fp
, "%d", instr
->const_index
[i
]);
535 static const char *index_name
[NIR_INTRINSIC_NUM_INDEX_FLAGS
] = {
536 [NIR_INTRINSIC_BASE
] = "base",
537 [NIR_INTRINSIC_WRMASK
] = "wrmask",
538 [NIR_INTRINSIC_STREAM_ID
] = "stream-id",
539 [NIR_INTRINSIC_UCP_ID
] = "ucp-id",
540 [NIR_INTRINSIC_RANGE
] = "range",
541 [NIR_INTRINSIC_DESC_SET
] = "desc-set",
542 [NIR_INTRINSIC_BINDING
] = "binding",
544 for (unsigned idx
= 1; idx
< NIR_INTRINSIC_NUM_INDEX_FLAGS
; idx
++) {
545 if (!info
->index_map
[idx
])
548 if (idx
== NIR_INTRINSIC_WRMASK
) {
549 /* special case wrmask to show it as a writemask.. */
550 unsigned wrmask
= nir_intrinsic_write_mask(instr
);
551 fprintf(fp
, " wrmask=");
552 for (unsigned i
= 0; i
< 4; i
++)
553 if ((wrmask
>> i
) & 1)
554 fprintf(fp
, "%c", "xyzw"[i
]);
556 unsigned off
= info
->index_map
[idx
] - 1;
557 assert(index_name
[idx
]); /* forgot to update index_name table? */
558 fprintf(fp
, " %s=%d", index_name
[idx
], instr
->const_index
[off
]);
566 struct exec_list
*var_list
= NULL
;
568 switch (instr
->intrinsic
) {
569 case nir_intrinsic_load_uniform
:
570 var_list
= &state
->shader
->uniforms
;
572 case nir_intrinsic_load_input
:
573 case nir_intrinsic_load_per_vertex_input
:
574 var_list
= &state
->shader
->inputs
;
576 case nir_intrinsic_load_output
:
577 case nir_intrinsic_store_output
:
578 case nir_intrinsic_store_per_vertex_output
:
579 var_list
= &state
->shader
->outputs
;
585 nir_foreach_variable(var
, var_list
) {
586 if ((var
->data
.driver_location
== nir_intrinsic_base(instr
)) &&
588 fprintf(fp
, "\t/* %s */", var
->name
);
595 print_tex_instr(nir_tex_instr
*instr
, print_state
*state
)
597 FILE *fp
= state
->fp
;
599 print_dest(&instr
->dest
, state
);
619 case nir_texop_txf_ms
:
620 fprintf(fp
, "txf_ms ");
631 case nir_texop_query_levels
:
632 fprintf(fp
, "query_levels ");
634 case nir_texop_texture_samples
:
635 fprintf(fp
, "texture_samples ");
637 case nir_texop_samples_identical
:
638 fprintf(fp
, "samples_identical ");
641 unreachable("Invalid texture operation");
645 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++) {
646 print_src(&instr
->src
[i
].src
, state
);
650 switch(instr
->src
[i
].src_type
) {
651 case nir_tex_src_coord
:
652 fprintf(fp
, "(coord)");
654 case nir_tex_src_projector
:
655 fprintf(fp
, "(projector)");
657 case nir_tex_src_comparitor
:
658 fprintf(fp
, "(comparitor)");
660 case nir_tex_src_offset
:
661 fprintf(fp
, "(offset)");
663 case nir_tex_src_bias
:
664 fprintf(fp
, "(bias)");
666 case nir_tex_src_lod
:
667 fprintf(fp
, "(lod)");
669 case nir_tex_src_ms_index
:
670 fprintf(fp
, "(ms_index)");
672 case nir_tex_src_ddx
:
673 fprintf(fp
, "(ddx)");
675 case nir_tex_src_ddy
:
676 fprintf(fp
, "(ddy)");
678 case nir_tex_src_texture_offset
:
679 fprintf(fp
, "(texture_offset)");
681 case nir_tex_src_sampler_offset
:
682 fprintf(fp
, "(sampler_offset)");
686 unreachable("Invalid texture source type");
693 if (instr
->op
== nir_texop_tg4
) {
694 fprintf(fp
, "%u (gather_component), ", instr
->component
);
697 if (instr
->texture
) {
698 print_deref(instr
->texture
, state
);
699 fprintf(fp
, " (texture)");
700 if (instr
->sampler
) {
701 print_deref(instr
->sampler
, state
);
702 fprintf(fp
, " (sampler)");
705 assert(instr
->sampler
== NULL
);
706 fprintf(fp
, "%u (texture) %u (sampler)",
707 instr
->texture_index
, instr
->sampler_index
);
712 print_call_instr(nir_call_instr
*instr
, print_state
*state
)
714 FILE *fp
= state
->fp
;
716 fprintf(fp
, "call %s ", instr
->callee
->name
);
718 for (unsigned i
= 0; i
< instr
->num_params
; i
++) {
722 print_deref(instr
->params
[i
], state
);
725 if (instr
->return_deref
!= NULL
) {
726 if (instr
->num_params
!= 0)
728 fprintf(fp
, "returning ");
729 print_deref(instr
->return_deref
, state
);
734 print_load_const_instr(nir_load_const_instr
*instr
, print_state
*state
)
736 FILE *fp
= state
->fp
;
738 print_ssa_def(&instr
->def
, state
);
740 fprintf(fp
, " = load_const (");
742 for (unsigned i
= 0; i
< instr
->def
.num_components
; i
++) {
747 * we don't really know the type of the constant (if it will be used as a
748 * float or an int), so just print the raw constant in hex for fidelity
749 * and then print the float in a comment for readability.
752 if (instr
->def
.bit_size
== 64)
753 fprintf(fp
, "0x%16" PRIx64
" /* %f */", instr
->value
.u64
[i
],
754 instr
->value
.f64
[i
]);
756 fprintf(fp
, "0x%08x /* %f */", instr
->value
.u32
[i
], instr
->value
.f32
[i
]);
763 print_jump_instr(nir_jump_instr
*instr
, print_state
*state
)
765 FILE *fp
= state
->fp
;
767 switch (instr
->type
) {
769 fprintf(fp
, "break");
772 case nir_jump_continue
:
773 fprintf(fp
, "continue");
776 case nir_jump_return
:
777 fprintf(fp
, "return");
783 print_ssa_undef_instr(nir_ssa_undef_instr
* instr
, print_state
*state
)
785 FILE *fp
= state
->fp
;
786 print_ssa_def(&instr
->def
, state
);
787 fprintf(fp
, " = undefined");
791 print_phi_instr(nir_phi_instr
*instr
, print_state
*state
)
793 FILE *fp
= state
->fp
;
794 print_dest(&instr
->dest
, state
);
795 fprintf(fp
, " = phi ");
796 nir_foreach_phi_src(instr
, src
) {
797 if (&src
->node
!= exec_list_get_head(&instr
->srcs
))
800 fprintf(fp
, "block_%u: ", src
->pred
->index
);
801 print_src(&src
->src
, state
);
806 print_parallel_copy_instr(nir_parallel_copy_instr
*instr
, print_state
*state
)
808 FILE *fp
= state
->fp
;
809 nir_foreach_parallel_copy_entry(instr
, entry
) {
810 if (&entry
->node
!= exec_list_get_head(&instr
->entries
))
813 print_dest(&entry
->dest
, state
);
815 print_src(&entry
->src
, state
);
820 print_instr(const nir_instr
*instr
, print_state
*state
, unsigned tabs
)
822 FILE *fp
= state
->fp
;
823 print_tabs(tabs
, fp
);
825 switch (instr
->type
) {
826 case nir_instr_type_alu
:
827 print_alu_instr(nir_instr_as_alu(instr
), state
);
830 case nir_instr_type_call
:
831 print_call_instr(nir_instr_as_call(instr
), state
);
834 case nir_instr_type_intrinsic
:
835 print_intrinsic_instr(nir_instr_as_intrinsic(instr
), state
);
838 case nir_instr_type_tex
:
839 print_tex_instr(nir_instr_as_tex(instr
), state
);
842 case nir_instr_type_load_const
:
843 print_load_const_instr(nir_instr_as_load_const(instr
), state
);
846 case nir_instr_type_jump
:
847 print_jump_instr(nir_instr_as_jump(instr
), state
);
850 case nir_instr_type_ssa_undef
:
851 print_ssa_undef_instr(nir_instr_as_ssa_undef(instr
), state
);
854 case nir_instr_type_phi
:
855 print_phi_instr(nir_instr_as_phi(instr
), state
);
858 case nir_instr_type_parallel_copy
:
859 print_parallel_copy_instr(nir_instr_as_parallel_copy(instr
), state
);
863 unreachable("Invalid instruction type");
869 compare_block_index(const void *p1
, const void *p2
)
871 const nir_block
*block1
= *((const nir_block
**) p1
);
872 const nir_block
*block2
= *((const nir_block
**) p2
);
874 return (int) block1
->index
- (int) block2
->index
;
877 static void print_cf_node(nir_cf_node
*node
, print_state
*state
,
881 print_block(nir_block
*block
, print_state
*state
, unsigned tabs
)
883 FILE *fp
= state
->fp
;
885 print_tabs(tabs
, fp
);
886 fprintf(fp
, "block block_%u:\n", block
->index
);
888 /* sort the predecessors by index so we consistently print the same thing */
891 malloc(block
->predecessors
->entries
* sizeof(nir_block
*));
893 struct set_entry
*entry
;
895 set_foreach(block
->predecessors
, entry
) {
896 preds
[i
++] = (nir_block
*) entry
->key
;
899 qsort(preds
, block
->predecessors
->entries
, sizeof(nir_block
*),
900 compare_block_index
);
902 print_tabs(tabs
, fp
);
903 fprintf(fp
, "/* preds: ");
904 for (unsigned i
= 0; i
< block
->predecessors
->entries
; i
++) {
905 fprintf(fp
, "block_%u ", preds
[i
]->index
);
911 nir_foreach_instr(block
, instr
) {
912 print_instr(instr
, state
, tabs
);
916 print_tabs(tabs
, fp
);
917 fprintf(fp
, "/* succs: ");
918 for (unsigned i
= 0; i
< 2; i
++)
919 if (block
->successors
[i
]) {
920 fprintf(fp
, "block_%u ", block
->successors
[i
]->index
);
926 print_if(nir_if
*if_stmt
, print_state
*state
, unsigned tabs
)
928 FILE *fp
= state
->fp
;
930 print_tabs(tabs
, fp
);
932 print_src(&if_stmt
->condition
, state
);
934 foreach_list_typed(nir_cf_node
, node
, node
, &if_stmt
->then_list
) {
935 print_cf_node(node
, state
, tabs
+ 1);
937 print_tabs(tabs
, fp
);
938 fprintf(fp
, "} else {\n");
939 foreach_list_typed(nir_cf_node
, node
, node
, &if_stmt
->else_list
) {
940 print_cf_node(node
, state
, tabs
+ 1);
942 print_tabs(tabs
, fp
);
947 print_loop(nir_loop
*loop
, print_state
*state
, unsigned tabs
)
949 FILE *fp
= state
->fp
;
951 print_tabs(tabs
, fp
);
952 fprintf(fp
, "loop {\n");
953 foreach_list_typed(nir_cf_node
, node
, node
, &loop
->body
) {
954 print_cf_node(node
, state
, tabs
+ 1);
956 print_tabs(tabs
, fp
);
961 print_cf_node(nir_cf_node
*node
, print_state
*state
, unsigned int tabs
)
963 switch (node
->type
) {
964 case nir_cf_node_block
:
965 print_block(nir_cf_node_as_block(node
), state
, tabs
);
969 print_if(nir_cf_node_as_if(node
), state
, tabs
);
972 case nir_cf_node_loop
:
973 print_loop(nir_cf_node_as_loop(node
), state
, tabs
);
977 unreachable("Invalid CFG node type");
982 print_function_impl(nir_function_impl
*impl
, print_state
*state
)
984 FILE *fp
= state
->fp
;
986 fprintf(fp
, "\nimpl %s ", impl
->function
->name
);
988 for (unsigned i
= 0; i
< impl
->num_params
; i
++) {
992 print_arg(impl
->params
[i
], state
);
995 if (impl
->return_var
!= NULL
) {
996 if (impl
->num_params
!= 0)
998 fprintf(fp
, "returning ");
999 print_arg(impl
->return_var
, state
);
1004 nir_foreach_variable(var
, &impl
->locals
) {
1006 print_var_decl(var
, state
);
1009 foreach_list_typed(nir_register
, reg
, node
, &impl
->registers
) {
1011 print_register_decl(reg
, state
);
1014 nir_index_blocks(impl
);
1016 foreach_list_typed(nir_cf_node
, node
, node
, &impl
->body
) {
1017 print_cf_node(node
, state
, 1);
1020 fprintf(fp
, "\tblock block_%u:\n}\n\n", impl
->end_block
->index
);
1024 print_function(nir_function
*function
, print_state
*state
)
1026 FILE *fp
= state
->fp
;
1028 fprintf(fp
, "decl_function %s ", function
->name
);
1030 for (unsigned i
= 0; i
< function
->num_params
; i
++) {
1034 switch (function
->params
[i
].param_type
) {
1035 case nir_parameter_in
:
1038 case nir_parameter_out
:
1039 fprintf(fp
, "out ");
1041 case nir_parameter_inout
:
1042 fprintf(fp
, "inout ");
1045 unreachable("Invalid parameter type");
1048 glsl_print_type(function
->params
[i
].type
, fp
);
1051 if (function
->return_type
!= NULL
) {
1052 if (function
->num_params
!= 0)
1054 fprintf(fp
, "returning ");
1055 glsl_print_type(function
->return_type
, fp
);
1060 if (function
->impl
!= NULL
) {
1061 print_function_impl(function
->impl
, state
);
1067 init_print_state(print_state
*state
, nir_shader
*shader
, FILE *fp
)
1070 state
->shader
= shader
;
1071 state
->ht
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
1072 _mesa_key_pointer_equal
);
1073 state
->syms
= _mesa_set_create(NULL
, _mesa_key_hash_string
,
1074 _mesa_key_string_equal
);
1079 destroy_print_state(print_state
*state
)
1081 _mesa_hash_table_destroy(state
->ht
, NULL
);
1082 _mesa_set_destroy(state
->syms
, NULL
);
1086 nir_print_shader(nir_shader
*shader
, FILE *fp
)
1089 init_print_state(&state
, shader
, fp
);
1091 fprintf(fp
, "shader: %s\n", gl_shader_stage_name(shader
->stage
));
1093 if (shader
->info
.name
)
1094 fprintf(fp
, "name: %s\n", shader
->info
.name
);
1096 if (shader
->info
.label
)
1097 fprintf(fp
, "label: %s\n", shader
->info
.label
);
1099 fprintf(fp
, "inputs: %u\n", shader
->num_inputs
);
1100 fprintf(fp
, "outputs: %u\n", shader
->num_outputs
);
1101 fprintf(fp
, "uniforms: %u\n", shader
->num_uniforms
);
1102 fprintf(fp
, "shared: %u\n", shader
->num_shared
);
1104 nir_foreach_variable(var
, &shader
->uniforms
) {
1105 print_var_decl(var
, &state
);
1108 nir_foreach_variable(var
, &shader
->inputs
) {
1109 print_var_decl(var
, &state
);
1112 nir_foreach_variable(var
, &shader
->outputs
) {
1113 print_var_decl(var
, &state
);
1116 nir_foreach_variable(var
, &shader
->shared
) {
1117 print_var_decl(var
, &state
);
1120 nir_foreach_variable(var
, &shader
->globals
) {
1121 print_var_decl(var
, &state
);
1124 nir_foreach_variable(var
, &shader
->system_values
) {
1125 print_var_decl(var
, &state
);
1128 foreach_list_typed(nir_register
, reg
, node
, &shader
->registers
) {
1129 print_register_decl(reg
, &state
);
1132 foreach_list_typed(nir_function
, func
, node
, &shader
->functions
) {
1133 print_function(func
, &state
);
1136 destroy_print_state(&state
);
1140 nir_print_instr(const nir_instr
*instr
, FILE *fp
)
1142 print_state state
= {
1145 print_instr(instr
, &state
, 0);