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 */
35 print_tabs(unsigned num_tabs
, FILE *fp
)
37 for (unsigned i
= 0; i
< num_tabs
; i
++)
44 /** map from nir_variable -> printable name */
45 struct hash_table
*ht
;
47 /** set of names used so far for nir_variables */
50 /* an index used to make new non-conflicting names */
55 print_register(nir_register
*reg
, print_state
*state
)
58 if (reg
->name
!= NULL
)
59 fprintf(fp
, "/* %s */ ", reg
->name
);
61 fprintf(fp
, "gr%u", reg
->index
);
63 fprintf(fp
, "r%u", reg
->index
);
66 static const char *sizes
[] = { "error", "vec1", "vec2", "vec3", "vec4" };
69 print_register_decl(nir_register
*reg
, print_state
*state
)
72 fprintf(fp
, "decl_reg %s %u ", sizes
[reg
->num_components
], reg
->bit_size
);
74 fprintf(fp
, "(packed) ");
75 print_register(reg
, state
);
76 if (reg
->num_array_elems
!= 0)
77 fprintf(fp
, "[%u]", reg
->num_array_elems
);
82 print_ssa_def(nir_ssa_def
*def
, print_state
*state
)
85 if (def
->name
!= NULL
)
86 fprintf(fp
, "/* %s */ ", def
->name
);
87 fprintf(fp
, "%s %u ssa_%u", sizes
[def
->num_components
], def
->bit_size
,
92 print_ssa_use(nir_ssa_def
*def
, print_state
*state
)
95 if (def
->name
!= NULL
)
96 fprintf(fp
, "/* %s */ ", def
->name
);
97 fprintf(fp
, "ssa_%u", def
->index
);
100 static void print_src(nir_src
*src
, print_state
*state
);
103 print_reg_src(nir_reg_src
*src
, print_state
*state
)
105 FILE *fp
= state
->fp
;
106 print_register(src
->reg
, state
);
107 if (src
->reg
->num_array_elems
!= 0) {
108 fprintf(fp
, "[%u", src
->base_offset
);
109 if (src
->indirect
!= NULL
) {
111 print_src(src
->indirect
, state
);
118 print_reg_dest(nir_reg_dest
*dest
, print_state
*state
)
120 FILE *fp
= state
->fp
;
121 print_register(dest
->reg
, state
);
122 if (dest
->reg
->num_array_elems
!= 0) {
123 fprintf(fp
, "[%u", dest
->base_offset
);
124 if (dest
->indirect
!= NULL
) {
126 print_src(dest
->indirect
, state
);
133 print_src(nir_src
*src
, print_state
*state
)
136 print_ssa_use(src
->ssa
, state
);
138 print_reg_src(&src
->reg
, state
);
142 print_dest(nir_dest
*dest
, print_state
*state
)
145 print_ssa_def(&dest
->ssa
, state
);
147 print_reg_dest(&dest
->reg
, state
);
151 print_alu_src(nir_alu_instr
*instr
, unsigned src
, print_state
*state
)
153 FILE *fp
= state
->fp
;
155 if (instr
->src
[src
].negate
)
157 if (instr
->src
[src
].abs
)
160 print_src(&instr
->src
[src
].src
, state
);
162 bool print_swizzle
= false;
163 for (unsigned i
= 0; i
< 4; i
++) {
164 if (!nir_alu_instr_channel_used(instr
, src
, i
))
167 if (instr
->src
[src
].swizzle
[i
] != i
) {
168 print_swizzle
= true;
175 for (unsigned i
= 0; i
< 4; i
++) {
176 if (!nir_alu_instr_channel_used(instr
, src
, i
))
179 fprintf(fp
, "%c", "xyzw"[instr
->src
[src
].swizzle
[i
]]);
183 if (instr
->src
[src
].abs
)
188 print_alu_dest(nir_alu_dest
*dest
, print_state
*state
)
190 FILE *fp
= state
->fp
;
191 /* we're going to print the saturate modifier later, after the opcode */
193 print_dest(&dest
->dest
, state
);
195 if (!dest
->dest
.is_ssa
&&
196 dest
->write_mask
!= (1 << dest
->dest
.reg
.reg
->num_components
) - 1) {
198 for (unsigned i
= 0; i
< 4; i
++)
199 if ((dest
->write_mask
>> i
) & 1)
200 fprintf(fp
, "%c", "xyzw"[i
]);
205 print_alu_instr(nir_alu_instr
*instr
, print_state
*state
)
207 FILE *fp
= state
->fp
;
209 print_alu_dest(&instr
->dest
, state
);
211 fprintf(fp
, " = %s", nir_op_infos
[instr
->op
].name
);
214 if (instr
->dest
.saturate
)
218 for (unsigned i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++) {
222 print_alu_src(instr
, i
, state
);
227 get_var_name(nir_variable
*var
, print_state
*state
)
229 if (state
->ht
== NULL
)
234 struct hash_entry
*entry
= _mesa_hash_table_search(state
->ht
, var
);
239 if (var
->name
== NULL
) {
240 name
= ralloc_asprintf(state
->syms
, "@%u", state
->index
++);
242 struct set_entry
*set_entry
= _mesa_set_search(state
->syms
, var
->name
);
243 if (set_entry
!= NULL
) {
244 /* we have a collision with another name, append an @ + a unique
246 name
= ralloc_asprintf(state
->syms
, "%s@%u", var
->name
,
249 /* Mark this one as seen */
250 _mesa_set_add(state
->syms
, var
->name
);
255 _mesa_hash_table_insert(state
->ht
, var
, name
);
261 print_constant(nir_constant
*c
, const struct glsl_type
*type
, print_state
*state
)
263 FILE *fp
= state
->fp
;
264 unsigned total_elems
= glsl_get_components(type
);
267 switch (glsl_get_base_type(type
)) {
271 for (i
= 0; i
< total_elems
; i
++) {
272 if (i
> 0) fprintf(fp
, ", ");
273 fprintf(fp
, "0x%08x", c
->value
.u
[i
]);
277 case GLSL_TYPE_FLOAT
:
278 for (i
= 0; i
< total_elems
; i
++) {
279 if (i
> 0) fprintf(fp
, ", ");
280 fprintf(fp
, "%f", c
->value
.f
[i
]);
284 case GLSL_TYPE_DOUBLE
:
285 for (i
= 0; i
< total_elems
; i
++) {
286 if (i
> 0) fprintf(fp
, ", ");
287 fprintf(fp
, "%f", c
->value
.d
[i
]);
291 case GLSL_TYPE_STRUCT
:
292 for (i
= 0; i
< c
->num_elements
; i
++) {
293 if (i
> 0) fprintf(fp
, ", ");
295 print_constant(c
->elements
[i
], glsl_get_struct_field(type
, i
), state
);
300 case GLSL_TYPE_ARRAY
:
301 for (i
= 0; i
< c
->num_elements
; i
++) {
302 if (i
> 0) fprintf(fp
, ", ");
304 print_constant(c
->elements
[i
], glsl_get_array_element(type
), state
);
310 unreachable("not reached");
315 print_var_decl(nir_variable
*var
, print_state
*state
)
317 FILE *fp
= state
->fp
;
319 fprintf(fp
, "decl_var ");
321 const char *const cent
= (var
->data
.centroid
) ? "centroid " : "";
322 const char *const samp
= (var
->data
.sample
) ? "sample " : "";
323 const char *const patch
= (var
->data
.patch
) ? "patch " : "";
324 const char *const inv
= (var
->data
.invariant
) ? "invariant " : "";
325 const char *const mode
[] = { "shader_in ", "shader_out ", "", "",
326 "uniform ", "shader_storage ", "shared ",
329 fprintf(fp
, "%s%s%s%s%s%s ",
330 cent
, samp
, patch
, inv
, mode
[var
->data
.mode
],
331 glsl_interp_qualifier_name(var
->data
.interpolation
));
333 glsl_print_type(var
->type
, fp
);
335 fprintf(fp
, " %s", get_var_name(var
, state
));
337 if (var
->data
.mode
== nir_var_shader_in
||
338 var
->data
.mode
== nir_var_shader_out
||
339 var
->data
.mode
== nir_var_uniform
||
340 var
->data
.mode
== nir_var_shader_storage
) {
341 const char *loc
= NULL
;
344 switch (state
->shader
->stage
) {
345 case MESA_SHADER_VERTEX
:
346 if (var
->data
.mode
== nir_var_shader_in
)
347 loc
= gl_vert_attrib_name(var
->data
.location
);
348 else if (var
->data
.mode
== nir_var_shader_out
)
349 loc
= gl_varying_slot_name(var
->data
.location
);
351 case MESA_SHADER_GEOMETRY
:
352 if ((var
->data
.mode
== nir_var_shader_in
) ||
353 (var
->data
.mode
== nir_var_shader_out
))
354 loc
= gl_varying_slot_name(var
->data
.location
);
356 case MESA_SHADER_FRAGMENT
:
357 if (var
->data
.mode
== nir_var_shader_in
)
358 loc
= gl_varying_slot_name(var
->data
.location
);
359 else if (var
->data
.mode
== nir_var_shader_out
)
360 loc
= gl_frag_result_name(var
->data
.location
);
362 case MESA_SHADER_TESS_CTRL
:
363 case MESA_SHADER_TESS_EVAL
:
364 case MESA_SHADER_COMPUTE
:
371 snprintf(buf
, sizeof(buf
), "%u", var
->data
.location
);
375 fprintf(fp
, " (%s, %u)", loc
, var
->data
.driver_location
);
378 if (var
->constant_initializer
) {
379 fprintf(fp
, " = { ");
380 print_constant(var
->constant_initializer
, var
->type
, state
);
388 print_var(nir_variable
*var
, print_state
*state
)
390 FILE *fp
= state
->fp
;
391 fprintf(fp
, "%s", get_var_name(var
, state
));
395 print_arg(nir_variable
*var
, print_state
*state
)
397 FILE *fp
= state
->fp
;
398 glsl_print_type(var
->type
, fp
);
399 fprintf(fp
, " %s", get_var_name(var
, state
));
403 print_deref_var(nir_deref_var
*deref
, print_state
*state
)
405 print_var(deref
->var
, state
);
409 print_deref_array(nir_deref_array
*deref
, print_state
*state
)
411 FILE *fp
= state
->fp
;
413 switch (deref
->deref_array_type
) {
414 case nir_deref_array_type_direct
:
415 fprintf(fp
, "%u", deref
->base_offset
);
417 case nir_deref_array_type_indirect
:
418 if (deref
->base_offset
!= 0)
419 fprintf(fp
, "%u + ", deref
->base_offset
);
420 print_src(&deref
->indirect
, state
);
422 case nir_deref_array_type_wildcard
:
430 print_deref_struct(nir_deref_struct
*deref
, const struct glsl_type
*parent_type
,
433 FILE *fp
= state
->fp
;
434 fprintf(fp
, ".%s", glsl_get_struct_elem_name(parent_type
, deref
->index
));
438 print_deref(nir_deref_var
*deref
, print_state
*state
)
440 nir_deref
*tail
= &deref
->deref
;
441 nir_deref
*pretail
= NULL
;
442 while (tail
!= NULL
) {
443 switch (tail
->deref_type
) {
444 case nir_deref_type_var
:
445 assert(pretail
== NULL
);
446 assert(tail
== &deref
->deref
);
447 print_deref_var(deref
, state
);
450 case nir_deref_type_array
:
451 assert(pretail
!= NULL
);
452 print_deref_array(nir_deref_as_array(tail
), state
);
455 case nir_deref_type_struct
:
456 assert(pretail
!= NULL
);
457 print_deref_struct(nir_deref_as_struct(tail
),
458 pretail
->type
, state
);
462 unreachable("Invalid deref type");
466 tail
= pretail
->child
;
471 print_intrinsic_instr(nir_intrinsic_instr
*instr
, print_state
*state
)
473 const nir_intrinsic_info
*info
= &nir_intrinsic_infos
[instr
->intrinsic
];
474 unsigned num_srcs
= info
->num_srcs
;
475 FILE *fp
= state
->fp
;
477 if (info
->has_dest
) {
478 print_dest(&instr
->dest
, state
);
482 fprintf(fp
, "intrinsic %s (", info
->name
);
484 for (unsigned i
= 0; i
< num_srcs
; i
++) {
488 print_src(&instr
->src
[i
], state
);
493 for (unsigned i
= 0; i
< info
->num_variables
; i
++) {
497 print_deref(instr
->variables
[i
], state
);
502 for (unsigned i
= 0; i
< info
->num_indices
; i
++) {
506 fprintf(fp
, "%d", instr
->const_index
[i
]);
511 static const char *index_name
[NIR_INTRINSIC_NUM_INDEX_FLAGS
] = {
512 [NIR_INTRINSIC_BASE
] = "base",
513 [NIR_INTRINSIC_WRMASK
] = "wrmask",
514 [NIR_INTRINSIC_STREAM_ID
] = "stream-id",
515 [NIR_INTRINSIC_UCP_ID
] = "ucp-id",
517 for (unsigned idx
= 1; idx
< NIR_INTRINSIC_NUM_INDEX_FLAGS
; idx
++) {
518 if (!info
->index_map
[idx
])
521 if (idx
== NIR_INTRINSIC_WRMASK
) {
522 /* special case wrmask to show it as a writemask.. */
523 unsigned wrmask
= nir_intrinsic_write_mask(instr
);
524 fprintf(fp
, " wrmask=");
525 for (unsigned i
= 0; i
< 4; i
++)
526 if ((wrmask
>> i
) & 1)
527 fprintf(fp
, "%c", "xyzw"[i
]);
529 unsigned off
= info
->index_map
[idx
] - 1;
530 assert(index_name
[idx
]); /* forgot to update index_name table? */
531 fprintf(fp
, " %s=%d", index_name
[idx
], instr
->const_index
[off
]);
539 struct exec_list
*var_list
= NULL
;
541 switch (instr
->intrinsic
) {
542 case nir_intrinsic_load_uniform
:
543 var_list
= &state
->shader
->uniforms
;
545 case nir_intrinsic_load_input
:
546 case nir_intrinsic_load_per_vertex_input
:
547 var_list
= &state
->shader
->inputs
;
549 case nir_intrinsic_load_output
:
550 case nir_intrinsic_store_output
:
551 case nir_intrinsic_store_per_vertex_output
:
552 var_list
= &state
->shader
->outputs
;
558 nir_foreach_variable(var
, var_list
) {
559 if ((var
->data
.driver_location
== nir_intrinsic_base(instr
)) &&
561 fprintf(fp
, "\t/* %s */", var
->name
);
568 print_tex_instr(nir_tex_instr
*instr
, print_state
*state
)
570 FILE *fp
= state
->fp
;
572 print_dest(&instr
->dest
, state
);
592 case nir_texop_txf_ms
:
593 fprintf(fp
, "txf_ms ");
604 case nir_texop_query_levels
:
605 fprintf(fp
, "query_levels ");
607 case nir_texop_texture_samples
:
608 fprintf(fp
, "texture_samples ");
610 case nir_texop_samples_identical
:
611 fprintf(fp
, "samples_identical ");
614 unreachable("Invalid texture operation");
618 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++) {
619 print_src(&instr
->src
[i
].src
, state
);
623 switch(instr
->src
[i
].src_type
) {
624 case nir_tex_src_coord
:
625 fprintf(fp
, "(coord)");
627 case nir_tex_src_projector
:
628 fprintf(fp
, "(projector)");
630 case nir_tex_src_comparitor
:
631 fprintf(fp
, "(comparitor)");
633 case nir_tex_src_offset
:
634 fprintf(fp
, "(offset)");
636 case nir_tex_src_bias
:
637 fprintf(fp
, "(bias)");
639 case nir_tex_src_lod
:
640 fprintf(fp
, "(lod)");
642 case nir_tex_src_ms_index
:
643 fprintf(fp
, "(ms_index)");
645 case nir_tex_src_ddx
:
646 fprintf(fp
, "(ddx)");
648 case nir_tex_src_ddy
:
649 fprintf(fp
, "(ddy)");
651 case nir_tex_src_texture_offset
:
652 fprintf(fp
, "(texture_offset)");
654 case nir_tex_src_sampler_offset
:
655 fprintf(fp
, "(sampler_offset)");
659 unreachable("Invalid texture source type");
666 if (instr
->op
== nir_texop_tg4
) {
667 fprintf(fp
, "%u (gather_component), ", instr
->component
);
670 if (instr
->texture
) {
671 print_deref(instr
->texture
, state
);
672 fprintf(fp
, " (texture)");
673 if (instr
->sampler
) {
674 print_deref(instr
->sampler
, state
);
675 fprintf(fp
, " (sampler)");
678 assert(instr
->sampler
== NULL
);
679 fprintf(fp
, "%u (texture) %u (sampler)",
680 instr
->texture_index
, instr
->sampler_index
);
685 print_call_instr(nir_call_instr
*instr
, print_state
*state
)
687 FILE *fp
= state
->fp
;
689 fprintf(fp
, "call %s ", instr
->callee
->name
);
691 for (unsigned i
= 0; i
< instr
->num_params
; i
++) {
695 print_deref(instr
->params
[i
], state
);
698 if (instr
->return_deref
!= NULL
) {
699 if (instr
->num_params
!= 0)
701 fprintf(fp
, "returning ");
702 print_deref(instr
->return_deref
, state
);
707 print_load_const_instr(nir_load_const_instr
*instr
, print_state
*state
)
709 FILE *fp
= state
->fp
;
711 print_ssa_def(&instr
->def
, state
);
713 fprintf(fp
, " = load_const (");
715 for (unsigned i
= 0; i
< instr
->def
.num_components
; i
++) {
720 * we don't really know the type of the constant (if it will be used as a
721 * float or an int), so just print the raw constant in hex for fidelity
722 * and then print the float in a comment for readability.
725 if (instr
->def
.bit_size
== 64)
726 fprintf(fp
, "0x%16" PRIx64
" /* %f */", instr
->value
.u64
[i
],
727 instr
->value
.f64
[i
]);
729 fprintf(fp
, "0x%08x /* %f */", instr
->value
.u32
[i
], instr
->value
.f32
[i
]);
736 print_jump_instr(nir_jump_instr
*instr
, print_state
*state
)
738 FILE *fp
= state
->fp
;
740 switch (instr
->type
) {
742 fprintf(fp
, "break");
745 case nir_jump_continue
:
746 fprintf(fp
, "continue");
749 case nir_jump_return
:
750 fprintf(fp
, "return");
756 print_ssa_undef_instr(nir_ssa_undef_instr
* instr
, print_state
*state
)
758 FILE *fp
= state
->fp
;
759 print_ssa_def(&instr
->def
, state
);
760 fprintf(fp
, " = undefined");
764 print_phi_instr(nir_phi_instr
*instr
, print_state
*state
)
766 FILE *fp
= state
->fp
;
767 print_dest(&instr
->dest
, state
);
768 fprintf(fp
, " = phi ");
769 nir_foreach_phi_src(instr
, src
) {
770 if (&src
->node
!= exec_list_get_head(&instr
->srcs
))
773 fprintf(fp
, "block_%u: ", src
->pred
->index
);
774 print_src(&src
->src
, state
);
779 print_parallel_copy_instr(nir_parallel_copy_instr
*instr
, print_state
*state
)
781 FILE *fp
= state
->fp
;
782 nir_foreach_parallel_copy_entry(instr
, entry
) {
783 if (&entry
->node
!= exec_list_get_head(&instr
->entries
))
786 print_dest(&entry
->dest
, state
);
788 print_src(&entry
->src
, state
);
793 print_instr(const nir_instr
*instr
, print_state
*state
, unsigned tabs
)
795 FILE *fp
= state
->fp
;
796 print_tabs(tabs
, fp
);
798 switch (instr
->type
) {
799 case nir_instr_type_alu
:
800 print_alu_instr(nir_instr_as_alu(instr
), state
);
803 case nir_instr_type_call
:
804 print_call_instr(nir_instr_as_call(instr
), state
);
807 case nir_instr_type_intrinsic
:
808 print_intrinsic_instr(nir_instr_as_intrinsic(instr
), state
);
811 case nir_instr_type_tex
:
812 print_tex_instr(nir_instr_as_tex(instr
), state
);
815 case nir_instr_type_load_const
:
816 print_load_const_instr(nir_instr_as_load_const(instr
), state
);
819 case nir_instr_type_jump
:
820 print_jump_instr(nir_instr_as_jump(instr
), state
);
823 case nir_instr_type_ssa_undef
:
824 print_ssa_undef_instr(nir_instr_as_ssa_undef(instr
), state
);
827 case nir_instr_type_phi
:
828 print_phi_instr(nir_instr_as_phi(instr
), state
);
831 case nir_instr_type_parallel_copy
:
832 print_parallel_copy_instr(nir_instr_as_parallel_copy(instr
), state
);
836 unreachable("Invalid instruction type");
842 compare_block_index(const void *p1
, const void *p2
)
844 const nir_block
*block1
= *((const nir_block
**) p1
);
845 const nir_block
*block2
= *((const nir_block
**) p2
);
847 return (int) block1
->index
- (int) block2
->index
;
850 static void print_cf_node(nir_cf_node
*node
, print_state
*state
,
854 print_block(nir_block
*block
, print_state
*state
, unsigned tabs
)
856 FILE *fp
= state
->fp
;
858 print_tabs(tabs
, fp
);
859 fprintf(fp
, "block block_%u:\n", block
->index
);
861 /* sort the predecessors by index so we consistently print the same thing */
864 malloc(block
->predecessors
->entries
* sizeof(nir_block
*));
866 struct set_entry
*entry
;
868 set_foreach(block
->predecessors
, entry
) {
869 preds
[i
++] = (nir_block
*) entry
->key
;
872 qsort(preds
, block
->predecessors
->entries
, sizeof(nir_block
*),
873 compare_block_index
);
875 print_tabs(tabs
, fp
);
876 fprintf(fp
, "/* preds: ");
877 for (unsigned i
= 0; i
< block
->predecessors
->entries
; i
++) {
878 fprintf(fp
, "block_%u ", preds
[i
]->index
);
884 nir_foreach_instr(block
, instr
) {
885 print_instr(instr
, state
, tabs
);
889 print_tabs(tabs
, fp
);
890 fprintf(fp
, "/* succs: ");
891 for (unsigned i
= 0; i
< 2; i
++)
892 if (block
->successors
[i
]) {
893 fprintf(fp
, "block_%u ", block
->successors
[i
]->index
);
899 print_if(nir_if
*if_stmt
, print_state
*state
, unsigned tabs
)
901 FILE *fp
= state
->fp
;
903 print_tabs(tabs
, fp
);
905 print_src(&if_stmt
->condition
, state
);
907 foreach_list_typed(nir_cf_node
, node
, node
, &if_stmt
->then_list
) {
908 print_cf_node(node
, state
, tabs
+ 1);
910 print_tabs(tabs
, fp
);
911 fprintf(fp
, "} else {\n");
912 foreach_list_typed(nir_cf_node
, node
, node
, &if_stmt
->else_list
) {
913 print_cf_node(node
, state
, tabs
+ 1);
915 print_tabs(tabs
, fp
);
920 print_loop(nir_loop
*loop
, print_state
*state
, unsigned tabs
)
922 FILE *fp
= state
->fp
;
924 print_tabs(tabs
, fp
);
925 fprintf(fp
, "loop {\n");
926 foreach_list_typed(nir_cf_node
, node
, node
, &loop
->body
) {
927 print_cf_node(node
, state
, tabs
+ 1);
929 print_tabs(tabs
, fp
);
934 print_cf_node(nir_cf_node
*node
, print_state
*state
, unsigned int tabs
)
936 switch (node
->type
) {
937 case nir_cf_node_block
:
938 print_block(nir_cf_node_as_block(node
), state
, tabs
);
942 print_if(nir_cf_node_as_if(node
), state
, tabs
);
945 case nir_cf_node_loop
:
946 print_loop(nir_cf_node_as_loop(node
), state
, tabs
);
950 unreachable("Invalid CFG node type");
955 print_function_impl(nir_function_impl
*impl
, print_state
*state
)
957 FILE *fp
= state
->fp
;
959 fprintf(fp
, "\nimpl %s ", impl
->function
->name
);
961 for (unsigned i
= 0; i
< impl
->num_params
; i
++) {
965 print_arg(impl
->params
[i
], state
);
968 if (impl
->return_var
!= NULL
) {
969 if (impl
->num_params
!= 0)
971 fprintf(fp
, "returning ");
972 print_arg(impl
->return_var
, state
);
977 nir_foreach_variable(var
, &impl
->locals
) {
979 print_var_decl(var
, state
);
982 foreach_list_typed(nir_register
, reg
, node
, &impl
->registers
) {
984 print_register_decl(reg
, state
);
987 nir_index_blocks(impl
);
989 foreach_list_typed(nir_cf_node
, node
, node
, &impl
->body
) {
990 print_cf_node(node
, state
, 1);
993 fprintf(fp
, "\tblock block_%u:\n}\n\n", impl
->end_block
->index
);
997 print_function(nir_function
*function
, print_state
*state
)
999 FILE *fp
= state
->fp
;
1001 fprintf(fp
, "decl_function %s ", function
->name
);
1003 for (unsigned i
= 0; i
< function
->num_params
; i
++) {
1007 switch (function
->params
[i
].param_type
) {
1008 case nir_parameter_in
:
1011 case nir_parameter_out
:
1012 fprintf(fp
, "out ");
1014 case nir_parameter_inout
:
1015 fprintf(fp
, "inout ");
1018 unreachable("Invalid parameter type");
1021 glsl_print_type(function
->params
[i
].type
, fp
);
1024 if (function
->return_type
!= NULL
) {
1025 if (function
->num_params
!= 0)
1027 fprintf(fp
, "returning ");
1028 glsl_print_type(function
->return_type
, fp
);
1033 if (function
->impl
!= NULL
) {
1034 print_function_impl(function
->impl
, state
);
1040 init_print_state(print_state
*state
, nir_shader
*shader
, FILE *fp
)
1043 state
->shader
= shader
;
1044 state
->ht
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
1045 _mesa_key_pointer_equal
);
1046 state
->syms
= _mesa_set_create(NULL
, _mesa_key_hash_string
,
1047 _mesa_key_string_equal
);
1052 destroy_print_state(print_state
*state
)
1054 _mesa_hash_table_destroy(state
->ht
, NULL
);
1055 _mesa_set_destroy(state
->syms
, NULL
);
1059 nir_print_shader(nir_shader
*shader
, FILE *fp
)
1062 init_print_state(&state
, shader
, fp
);
1064 fprintf(fp
, "shader: %s\n", gl_shader_stage_name(shader
->stage
));
1066 if (shader
->info
.name
)
1067 fprintf(fp
, "name: %s\n", shader
->info
.name
);
1069 if (shader
->info
.label
)
1070 fprintf(fp
, "label: %s\n", shader
->info
.label
);
1072 fprintf(fp
, "inputs: %u\n", shader
->num_inputs
);
1073 fprintf(fp
, "outputs: %u\n", shader
->num_outputs
);
1074 fprintf(fp
, "uniforms: %u\n", shader
->num_uniforms
);
1075 fprintf(fp
, "shared: %u\n", shader
->num_shared
);
1077 nir_foreach_variable(var
, &shader
->uniforms
) {
1078 print_var_decl(var
, &state
);
1081 nir_foreach_variable(var
, &shader
->inputs
) {
1082 print_var_decl(var
, &state
);
1085 nir_foreach_variable(var
, &shader
->outputs
) {
1086 print_var_decl(var
, &state
);
1089 nir_foreach_variable(var
, &shader
->shared
) {
1090 print_var_decl(var
, &state
);
1093 nir_foreach_variable(var
, &shader
->globals
) {
1094 print_var_decl(var
, &state
);
1097 nir_foreach_variable(var
, &shader
->system_values
) {
1098 print_var_decl(var
, &state
);
1101 foreach_list_typed(nir_register
, reg
, node
, &shader
->registers
) {
1102 print_register_decl(reg
, &state
);
1105 foreach_list_typed(nir_function
, func
, node
, &shader
->functions
) {
1106 print_function(func
, &state
);
1109 destroy_print_state(&state
);
1113 nir_print_instr(const nir_instr
*instr
, FILE *fp
)
1115 print_state state
= {
1118 print_instr(instr
, &state
, 0);