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 print_var_decl(nir_variable
*var
, print_state
*state
)
321 FILE *fp
= state
->fp
;
323 fprintf(fp
, "decl_var ");
325 const char *const cent
= (var
->data
.centroid
) ? "centroid " : "";
326 const char *const samp
= (var
->data
.sample
) ? "sample " : "";
327 const char *const patch
= (var
->data
.patch
) ? "patch " : "";
328 const char *const inv
= (var
->data
.invariant
) ? "invariant " : "";
329 const char *const mode
[] = { "shader_in ", "shader_out ", "", "",
330 "uniform ", "shader_storage ", "shared ",
333 fprintf(fp
, "%s%s%s%s%s%s ",
334 cent
, samp
, patch
, inv
, mode
[var
->data
.mode
],
335 glsl_interp_qualifier_name(var
->data
.interpolation
));
337 glsl_print_type(var
->type
, fp
);
339 fprintf(fp
, " %s", get_var_name(var
, state
));
341 if (var
->data
.mode
== nir_var_shader_in
||
342 var
->data
.mode
== nir_var_shader_out
||
343 var
->data
.mode
== nir_var_uniform
||
344 var
->data
.mode
== nir_var_shader_storage
) {
345 const char *loc
= NULL
;
348 switch (state
->shader
->stage
) {
349 case MESA_SHADER_VERTEX
:
350 if (var
->data
.mode
== nir_var_shader_in
)
351 loc
= gl_vert_attrib_name(var
->data
.location
);
352 else if (var
->data
.mode
== nir_var_shader_out
)
353 loc
= gl_varying_slot_name(var
->data
.location
);
355 case MESA_SHADER_GEOMETRY
:
356 if ((var
->data
.mode
== nir_var_shader_in
) ||
357 (var
->data
.mode
== nir_var_shader_out
))
358 loc
= gl_varying_slot_name(var
->data
.location
);
360 case MESA_SHADER_FRAGMENT
:
361 if (var
->data
.mode
== nir_var_shader_in
)
362 loc
= gl_varying_slot_name(var
->data
.location
);
363 else if (var
->data
.mode
== nir_var_shader_out
)
364 loc
= gl_frag_result_name(var
->data
.location
);
366 case MESA_SHADER_TESS_CTRL
:
367 case MESA_SHADER_TESS_EVAL
:
368 case MESA_SHADER_COMPUTE
:
375 snprintf(buf
, sizeof(buf
), "%u", var
->data
.location
);
379 fprintf(fp
, " (%s, %u)", loc
, var
->data
.driver_location
);
382 if (var
->constant_initializer
) {
383 fprintf(fp
, " = { ");
384 print_constant(var
->constant_initializer
, var
->type
, state
);
392 print_var(nir_variable
*var
, print_state
*state
)
394 FILE *fp
= state
->fp
;
395 fprintf(fp
, "%s", get_var_name(var
, state
));
399 print_arg(nir_variable
*var
, print_state
*state
)
401 FILE *fp
= state
->fp
;
402 glsl_print_type(var
->type
, fp
);
403 fprintf(fp
, " %s", get_var_name(var
, state
));
407 print_deref_var(nir_deref_var
*deref
, print_state
*state
)
409 print_var(deref
->var
, state
);
413 print_deref_array(nir_deref_array
*deref
, print_state
*state
)
415 FILE *fp
= state
->fp
;
417 switch (deref
->deref_array_type
) {
418 case nir_deref_array_type_direct
:
419 fprintf(fp
, "%u", deref
->base_offset
);
421 case nir_deref_array_type_indirect
:
422 if (deref
->base_offset
!= 0)
423 fprintf(fp
, "%u + ", deref
->base_offset
);
424 print_src(&deref
->indirect
, state
);
426 case nir_deref_array_type_wildcard
:
434 print_deref_struct(nir_deref_struct
*deref
, const struct glsl_type
*parent_type
,
437 FILE *fp
= state
->fp
;
438 fprintf(fp
, ".%s", glsl_get_struct_elem_name(parent_type
, deref
->index
));
442 print_deref(nir_deref_var
*deref
, print_state
*state
)
444 nir_deref
*tail
= &deref
->deref
;
445 nir_deref
*pretail
= NULL
;
446 while (tail
!= NULL
) {
447 switch (tail
->deref_type
) {
448 case nir_deref_type_var
:
449 assert(pretail
== NULL
);
450 assert(tail
== &deref
->deref
);
451 print_deref_var(deref
, state
);
454 case nir_deref_type_array
:
455 assert(pretail
!= NULL
);
456 print_deref_array(nir_deref_as_array(tail
), state
);
459 case nir_deref_type_struct
:
460 assert(pretail
!= NULL
);
461 print_deref_struct(nir_deref_as_struct(tail
),
462 pretail
->type
, state
);
466 unreachable("Invalid deref type");
470 tail
= pretail
->child
;
475 print_intrinsic_instr(nir_intrinsic_instr
*instr
, print_state
*state
)
477 const nir_intrinsic_info
*info
= &nir_intrinsic_infos
[instr
->intrinsic
];
478 unsigned num_srcs
= info
->num_srcs
;
479 FILE *fp
= state
->fp
;
481 if (info
->has_dest
) {
482 print_dest(&instr
->dest
, state
);
486 fprintf(fp
, "intrinsic %s (", info
->name
);
488 for (unsigned i
= 0; i
< num_srcs
; i
++) {
492 print_src(&instr
->src
[i
], state
);
497 for (unsigned i
= 0; i
< info
->num_variables
; i
++) {
501 print_deref(instr
->variables
[i
], state
);
506 for (unsigned i
= 0; i
< info
->num_indices
; i
++) {
510 fprintf(fp
, "%d", instr
->const_index
[i
]);
515 static const char *index_name
[NIR_INTRINSIC_NUM_INDEX_FLAGS
] = {
516 [NIR_INTRINSIC_BASE
] = "base",
517 [NIR_INTRINSIC_WRMASK
] = "wrmask",
518 [NIR_INTRINSIC_STREAM_ID
] = "stream-id",
519 [NIR_INTRINSIC_UCP_ID
] = "ucp-id",
521 for (unsigned idx
= 1; idx
< NIR_INTRINSIC_NUM_INDEX_FLAGS
; idx
++) {
522 if (!info
->index_map
[idx
])
525 if (idx
== NIR_INTRINSIC_WRMASK
) {
526 /* special case wrmask to show it as a writemask.. */
527 unsigned wrmask
= nir_intrinsic_write_mask(instr
);
528 fprintf(fp
, " wrmask=");
529 for (unsigned i
= 0; i
< 4; i
++)
530 if ((wrmask
>> i
) & 1)
531 fprintf(fp
, "%c", "xyzw"[i
]);
533 unsigned off
= info
->index_map
[idx
] - 1;
534 assert(index_name
[idx
]); /* forgot to update index_name table? */
535 fprintf(fp
, " %s=%d", index_name
[idx
], instr
->const_index
[off
]);
543 struct exec_list
*var_list
= NULL
;
545 switch (instr
->intrinsic
) {
546 case nir_intrinsic_load_uniform
:
547 var_list
= &state
->shader
->uniforms
;
549 case nir_intrinsic_load_input
:
550 case nir_intrinsic_load_per_vertex_input
:
551 var_list
= &state
->shader
->inputs
;
553 case nir_intrinsic_load_output
:
554 case nir_intrinsic_store_output
:
555 case nir_intrinsic_store_per_vertex_output
:
556 var_list
= &state
->shader
->outputs
;
562 nir_foreach_variable(var
, var_list
) {
563 if ((var
->data
.driver_location
== nir_intrinsic_base(instr
)) &&
565 fprintf(fp
, "\t/* %s */", var
->name
);
572 print_tex_instr(nir_tex_instr
*instr
, print_state
*state
)
574 FILE *fp
= state
->fp
;
576 print_dest(&instr
->dest
, state
);
596 case nir_texop_txf_ms
:
597 fprintf(fp
, "txf_ms ");
608 case nir_texop_query_levels
:
609 fprintf(fp
, "query_levels ");
611 case nir_texop_texture_samples
:
612 fprintf(fp
, "texture_samples ");
614 case nir_texop_samples_identical
:
615 fprintf(fp
, "samples_identical ");
618 unreachable("Invalid texture operation");
622 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++) {
623 print_src(&instr
->src
[i
].src
, state
);
627 switch(instr
->src
[i
].src_type
) {
628 case nir_tex_src_coord
:
629 fprintf(fp
, "(coord)");
631 case nir_tex_src_projector
:
632 fprintf(fp
, "(projector)");
634 case nir_tex_src_comparitor
:
635 fprintf(fp
, "(comparitor)");
637 case nir_tex_src_offset
:
638 fprintf(fp
, "(offset)");
640 case nir_tex_src_bias
:
641 fprintf(fp
, "(bias)");
643 case nir_tex_src_lod
:
644 fprintf(fp
, "(lod)");
646 case nir_tex_src_ms_index
:
647 fprintf(fp
, "(ms_index)");
649 case nir_tex_src_ddx
:
650 fprintf(fp
, "(ddx)");
652 case nir_tex_src_ddy
:
653 fprintf(fp
, "(ddy)");
655 case nir_tex_src_texture_offset
:
656 fprintf(fp
, "(texture_offset)");
658 case nir_tex_src_sampler_offset
:
659 fprintf(fp
, "(sampler_offset)");
663 unreachable("Invalid texture source type");
670 if (instr
->op
== nir_texop_tg4
) {
671 fprintf(fp
, "%u (gather_component), ", instr
->component
);
674 if (instr
->texture
) {
675 print_deref(instr
->texture
, state
);
676 fprintf(fp
, " (texture)");
677 if (instr
->sampler
) {
678 print_deref(instr
->sampler
, state
);
679 fprintf(fp
, " (sampler)");
682 assert(instr
->sampler
== NULL
);
683 fprintf(fp
, "%u (texture) %u (sampler)",
684 instr
->texture_index
, instr
->sampler_index
);
689 print_call_instr(nir_call_instr
*instr
, print_state
*state
)
691 FILE *fp
= state
->fp
;
693 fprintf(fp
, "call %s ", instr
->callee
->name
);
695 for (unsigned i
= 0; i
< instr
->num_params
; i
++) {
699 print_deref(instr
->params
[i
], state
);
702 if (instr
->return_deref
!= NULL
) {
703 if (instr
->num_params
!= 0)
705 fprintf(fp
, "returning ");
706 print_deref(instr
->return_deref
, state
);
711 print_load_const_instr(nir_load_const_instr
*instr
, print_state
*state
)
713 FILE *fp
= state
->fp
;
715 print_ssa_def(&instr
->def
, state
);
717 fprintf(fp
, " = load_const (");
719 for (unsigned i
= 0; i
< instr
->def
.num_components
; i
++) {
724 * we don't really know the type of the constant (if it will be used as a
725 * float or an int), so just print the raw constant in hex for fidelity
726 * and then print the float in a comment for readability.
729 if (instr
->def
.bit_size
== 64)
730 fprintf(fp
, "0x%16" PRIx64
" /* %f */", instr
->value
.u64
[i
],
731 instr
->value
.f64
[i
]);
733 fprintf(fp
, "0x%08x /* %f */", instr
->value
.u32
[i
], instr
->value
.f32
[i
]);
740 print_jump_instr(nir_jump_instr
*instr
, print_state
*state
)
742 FILE *fp
= state
->fp
;
744 switch (instr
->type
) {
746 fprintf(fp
, "break");
749 case nir_jump_continue
:
750 fprintf(fp
, "continue");
753 case nir_jump_return
:
754 fprintf(fp
, "return");
760 print_ssa_undef_instr(nir_ssa_undef_instr
* instr
, print_state
*state
)
762 FILE *fp
= state
->fp
;
763 print_ssa_def(&instr
->def
, state
);
764 fprintf(fp
, " = undefined");
768 print_phi_instr(nir_phi_instr
*instr
, print_state
*state
)
770 FILE *fp
= state
->fp
;
771 print_dest(&instr
->dest
, state
);
772 fprintf(fp
, " = phi ");
773 nir_foreach_phi_src(instr
, src
) {
774 if (&src
->node
!= exec_list_get_head(&instr
->srcs
))
777 fprintf(fp
, "block_%u: ", src
->pred
->index
);
778 print_src(&src
->src
, state
);
783 print_parallel_copy_instr(nir_parallel_copy_instr
*instr
, print_state
*state
)
785 FILE *fp
= state
->fp
;
786 nir_foreach_parallel_copy_entry(instr
, entry
) {
787 if (&entry
->node
!= exec_list_get_head(&instr
->entries
))
790 print_dest(&entry
->dest
, state
);
792 print_src(&entry
->src
, state
);
797 print_instr(const nir_instr
*instr
, print_state
*state
, unsigned tabs
)
799 FILE *fp
= state
->fp
;
800 print_tabs(tabs
, fp
);
802 switch (instr
->type
) {
803 case nir_instr_type_alu
:
804 print_alu_instr(nir_instr_as_alu(instr
), state
);
807 case nir_instr_type_call
:
808 print_call_instr(nir_instr_as_call(instr
), state
);
811 case nir_instr_type_intrinsic
:
812 print_intrinsic_instr(nir_instr_as_intrinsic(instr
), state
);
815 case nir_instr_type_tex
:
816 print_tex_instr(nir_instr_as_tex(instr
), state
);
819 case nir_instr_type_load_const
:
820 print_load_const_instr(nir_instr_as_load_const(instr
), state
);
823 case nir_instr_type_jump
:
824 print_jump_instr(nir_instr_as_jump(instr
), state
);
827 case nir_instr_type_ssa_undef
:
828 print_ssa_undef_instr(nir_instr_as_ssa_undef(instr
), state
);
831 case nir_instr_type_phi
:
832 print_phi_instr(nir_instr_as_phi(instr
), state
);
835 case nir_instr_type_parallel_copy
:
836 print_parallel_copy_instr(nir_instr_as_parallel_copy(instr
), state
);
840 unreachable("Invalid instruction type");
846 compare_block_index(const void *p1
, const void *p2
)
848 const nir_block
*block1
= *((const nir_block
**) p1
);
849 const nir_block
*block2
= *((const nir_block
**) p2
);
851 return (int) block1
->index
- (int) block2
->index
;
854 static void print_cf_node(nir_cf_node
*node
, print_state
*state
,
858 print_block(nir_block
*block
, print_state
*state
, unsigned tabs
)
860 FILE *fp
= state
->fp
;
862 print_tabs(tabs
, fp
);
863 fprintf(fp
, "block block_%u:\n", block
->index
);
865 /* sort the predecessors by index so we consistently print the same thing */
868 malloc(block
->predecessors
->entries
* sizeof(nir_block
*));
870 struct set_entry
*entry
;
872 set_foreach(block
->predecessors
, entry
) {
873 preds
[i
++] = (nir_block
*) entry
->key
;
876 qsort(preds
, block
->predecessors
->entries
, sizeof(nir_block
*),
877 compare_block_index
);
879 print_tabs(tabs
, fp
);
880 fprintf(fp
, "/* preds: ");
881 for (unsigned i
= 0; i
< block
->predecessors
->entries
; i
++) {
882 fprintf(fp
, "block_%u ", preds
[i
]->index
);
888 nir_foreach_instr(block
, instr
) {
889 print_instr(instr
, state
, tabs
);
893 print_tabs(tabs
, fp
);
894 fprintf(fp
, "/* succs: ");
895 for (unsigned i
= 0; i
< 2; i
++)
896 if (block
->successors
[i
]) {
897 fprintf(fp
, "block_%u ", block
->successors
[i
]->index
);
903 print_if(nir_if
*if_stmt
, print_state
*state
, unsigned tabs
)
905 FILE *fp
= state
->fp
;
907 print_tabs(tabs
, fp
);
909 print_src(&if_stmt
->condition
, state
);
911 foreach_list_typed(nir_cf_node
, node
, node
, &if_stmt
->then_list
) {
912 print_cf_node(node
, state
, tabs
+ 1);
914 print_tabs(tabs
, fp
);
915 fprintf(fp
, "} else {\n");
916 foreach_list_typed(nir_cf_node
, node
, node
, &if_stmt
->else_list
) {
917 print_cf_node(node
, state
, tabs
+ 1);
919 print_tabs(tabs
, fp
);
924 print_loop(nir_loop
*loop
, print_state
*state
, unsigned tabs
)
926 FILE *fp
= state
->fp
;
928 print_tabs(tabs
, fp
);
929 fprintf(fp
, "loop {\n");
930 foreach_list_typed(nir_cf_node
, node
, node
, &loop
->body
) {
931 print_cf_node(node
, state
, tabs
+ 1);
933 print_tabs(tabs
, fp
);
938 print_cf_node(nir_cf_node
*node
, print_state
*state
, unsigned int tabs
)
940 switch (node
->type
) {
941 case nir_cf_node_block
:
942 print_block(nir_cf_node_as_block(node
), state
, tabs
);
946 print_if(nir_cf_node_as_if(node
), state
, tabs
);
949 case nir_cf_node_loop
:
950 print_loop(nir_cf_node_as_loop(node
), state
, tabs
);
954 unreachable("Invalid CFG node type");
959 print_function_impl(nir_function_impl
*impl
, print_state
*state
)
961 FILE *fp
= state
->fp
;
963 fprintf(fp
, "\nimpl %s ", impl
->function
->name
);
965 for (unsigned i
= 0; i
< impl
->num_params
; i
++) {
969 print_arg(impl
->params
[i
], state
);
972 if (impl
->return_var
!= NULL
) {
973 if (impl
->num_params
!= 0)
975 fprintf(fp
, "returning ");
976 print_arg(impl
->return_var
, state
);
981 nir_foreach_variable(var
, &impl
->locals
) {
983 print_var_decl(var
, state
);
986 foreach_list_typed(nir_register
, reg
, node
, &impl
->registers
) {
988 print_register_decl(reg
, state
);
991 nir_index_blocks(impl
);
993 foreach_list_typed(nir_cf_node
, node
, node
, &impl
->body
) {
994 print_cf_node(node
, state
, 1);
997 fprintf(fp
, "\tblock block_%u:\n}\n\n", impl
->end_block
->index
);
1001 print_function(nir_function
*function
, print_state
*state
)
1003 FILE *fp
= state
->fp
;
1005 fprintf(fp
, "decl_function %s ", function
->name
);
1007 for (unsigned i
= 0; i
< function
->num_params
; i
++) {
1011 switch (function
->params
[i
].param_type
) {
1012 case nir_parameter_in
:
1015 case nir_parameter_out
:
1016 fprintf(fp
, "out ");
1018 case nir_parameter_inout
:
1019 fprintf(fp
, "inout ");
1022 unreachable("Invalid parameter type");
1025 glsl_print_type(function
->params
[i
].type
, fp
);
1028 if (function
->return_type
!= NULL
) {
1029 if (function
->num_params
!= 0)
1031 fprintf(fp
, "returning ");
1032 glsl_print_type(function
->return_type
, fp
);
1037 if (function
->impl
!= NULL
) {
1038 print_function_impl(function
->impl
, state
);
1044 init_print_state(print_state
*state
, nir_shader
*shader
, FILE *fp
)
1047 state
->shader
= shader
;
1048 state
->ht
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
1049 _mesa_key_pointer_equal
);
1050 state
->syms
= _mesa_set_create(NULL
, _mesa_key_hash_string
,
1051 _mesa_key_string_equal
);
1056 destroy_print_state(print_state
*state
)
1058 _mesa_hash_table_destroy(state
->ht
, NULL
);
1059 _mesa_set_destroy(state
->syms
, NULL
);
1063 nir_print_shader(nir_shader
*shader
, FILE *fp
)
1066 init_print_state(&state
, shader
, fp
);
1068 fprintf(fp
, "shader: %s\n", gl_shader_stage_name(shader
->stage
));
1070 if (shader
->info
.name
)
1071 fprintf(fp
, "name: %s\n", shader
->info
.name
);
1073 if (shader
->info
.label
)
1074 fprintf(fp
, "label: %s\n", shader
->info
.label
);
1076 fprintf(fp
, "inputs: %u\n", shader
->num_inputs
);
1077 fprintf(fp
, "outputs: %u\n", shader
->num_outputs
);
1078 fprintf(fp
, "uniforms: %u\n", shader
->num_uniforms
);
1079 fprintf(fp
, "shared: %u\n", shader
->num_shared
);
1081 nir_foreach_variable(var
, &shader
->uniforms
) {
1082 print_var_decl(var
, &state
);
1085 nir_foreach_variable(var
, &shader
->inputs
) {
1086 print_var_decl(var
, &state
);
1089 nir_foreach_variable(var
, &shader
->outputs
) {
1090 print_var_decl(var
, &state
);
1093 nir_foreach_variable(var
, &shader
->shared
) {
1094 print_var_decl(var
, &state
);
1097 nir_foreach_variable(var
, &shader
->globals
) {
1098 print_var_decl(var
, &state
);
1101 nir_foreach_variable(var
, &shader
->system_values
) {
1102 print_var_decl(var
, &state
);
1105 foreach_list_typed(nir_register
, reg
, node
, &shader
->registers
) {
1106 print_register_decl(reg
, &state
);
1109 foreach_list_typed(nir_function
, func
, node
, &shader
->functions
) {
1110 print_function(func
, &state
);
1113 destroy_print_state(&state
);
1117 nir_print_instr(const nir_instr
*instr
, FILE *fp
)
1119 print_state state
= {
1122 print_instr(instr
, &state
, 0);