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"
34 print_tabs(unsigned num_tabs
, FILE *fp
)
36 for (unsigned i
= 0; i
< num_tabs
; i
++)
43 /** map from nir_variable -> printable name */
44 struct hash_table
*ht
;
46 /** set of names used so far for nir_variables */
49 /* an index used to make new non-conflicting names */
54 print_register(nir_register
*reg
, print_state
*state
)
57 if (reg
->name
!= NULL
)
58 fprintf(fp
, "/* %s */ ", reg
->name
);
60 fprintf(fp
, "gr%u", reg
->index
);
62 fprintf(fp
, "r%u", reg
->index
);
65 static const char *sizes
[] = { "error", "vec1", "vec2", "vec3", "vec4" };
68 print_register_decl(nir_register
*reg
, print_state
*state
)
71 fprintf(fp
, "decl_reg %s ", sizes
[reg
->num_components
]);
73 fprintf(fp
, "(packed) ");
74 print_register(reg
, state
);
75 if (reg
->num_array_elems
!= 0)
76 fprintf(fp
, "[%u]", reg
->num_array_elems
);
81 print_ssa_def(nir_ssa_def
*def
, print_state
*state
)
84 if (def
->name
!= NULL
)
85 fprintf(fp
, "/* %s */ ", def
->name
);
86 fprintf(fp
, "%s ssa_%u", sizes
[def
->num_components
], def
->index
);
90 print_ssa_use(nir_ssa_def
*def
, print_state
*state
)
93 if (def
->name
!= NULL
)
94 fprintf(fp
, "/* %s */ ", def
->name
);
95 fprintf(fp
, "ssa_%u", def
->index
);
98 static void print_src(nir_src
*src
, print_state
*state
);
101 print_reg_src(nir_reg_src
*src
, print_state
*state
)
103 FILE *fp
= state
->fp
;
104 print_register(src
->reg
, state
);
105 if (src
->reg
->num_array_elems
!= 0) {
106 fprintf(fp
, "[%u", src
->base_offset
);
107 if (src
->indirect
!= NULL
) {
109 print_src(src
->indirect
, state
);
116 print_reg_dest(nir_reg_dest
*dest
, print_state
*state
)
118 FILE *fp
= state
->fp
;
119 print_register(dest
->reg
, state
);
120 if (dest
->reg
->num_array_elems
!= 0) {
121 fprintf(fp
, "[%u", dest
->base_offset
);
122 if (dest
->indirect
!= NULL
) {
124 print_src(dest
->indirect
, state
);
131 print_src(nir_src
*src
, print_state
*state
)
134 print_ssa_use(src
->ssa
, state
);
136 print_reg_src(&src
->reg
, state
);
140 print_dest(nir_dest
*dest
, print_state
*state
)
143 print_ssa_def(&dest
->ssa
, state
);
145 print_reg_dest(&dest
->reg
, state
);
149 print_alu_src(nir_alu_instr
*instr
, unsigned src
, print_state
*state
)
151 FILE *fp
= state
->fp
;
153 if (instr
->src
[src
].negate
)
155 if (instr
->src
[src
].abs
)
158 print_src(&instr
->src
[src
].src
, state
);
160 bool print_swizzle
= false;
161 for (unsigned i
= 0; i
< 4; i
++) {
162 if (!nir_alu_instr_channel_used(instr
, src
, i
))
165 if (instr
->src
[src
].swizzle
[i
] != i
) {
166 print_swizzle
= true;
173 for (unsigned i
= 0; i
< 4; i
++) {
174 if (!nir_alu_instr_channel_used(instr
, src
, i
))
177 fprintf(fp
, "%c", "xyzw"[instr
->src
[src
].swizzle
[i
]]);
181 if (instr
->src
[src
].abs
)
186 print_alu_dest(nir_alu_dest
*dest
, print_state
*state
)
188 FILE *fp
= state
->fp
;
189 /* we're going to print the saturate modifier later, after the opcode */
191 print_dest(&dest
->dest
, state
);
193 if (!dest
->dest
.is_ssa
&&
194 dest
->write_mask
!= (1 << dest
->dest
.reg
.reg
->num_components
) - 1) {
196 for (unsigned i
= 0; i
< 4; i
++)
197 if ((dest
->write_mask
>> i
) & 1)
198 fprintf(fp
, "%c", "xyzw"[i
]);
203 print_alu_instr(nir_alu_instr
*instr
, print_state
*state
)
205 FILE *fp
= state
->fp
;
207 print_alu_dest(&instr
->dest
, state
);
209 fprintf(fp
, " = %s", nir_op_infos
[instr
->op
].name
);
210 if (instr
->dest
.saturate
)
214 for (unsigned i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++) {
218 print_alu_src(instr
, i
, state
);
223 get_var_name(nir_variable
*var
, print_state
*state
)
225 if (state
->ht
== NULL
)
230 struct hash_entry
*entry
= _mesa_hash_table_search(state
->ht
, var
);
235 if (var
->name
== NULL
) {
236 name
= ralloc_asprintf(state
->syms
, "@%u", state
->index
++);
238 struct set_entry
*set_entry
= _mesa_set_search(state
->syms
, var
->name
);
239 if (set_entry
!= NULL
) {
240 /* we have a collision with another name, append an @ + a unique
242 name
= ralloc_asprintf(state
->syms
, "%s@%u", var
->name
,
245 /* Mark this one as seen */
246 _mesa_set_add(state
->syms
, var
->name
);
251 _mesa_hash_table_insert(state
->ht
, var
, name
);
257 print_constant(nir_constant
*c
, const struct glsl_type
*type
, print_state
*state
)
259 FILE *fp
= state
->fp
;
260 unsigned total_elems
= glsl_get_components(type
);
263 switch (glsl_get_base_type(type
)) {
267 for (i
= 0; i
< total_elems
; i
++) {
268 if (i
> 0) fprintf(fp
, ", ");
269 fprintf(fp
, "0x%08x", c
->value
.u
[i
]);
273 case GLSL_TYPE_FLOAT
:
274 for (i
= 0; i
< total_elems
; i
++) {
275 if (i
> 0) fprintf(fp
, ", ");
276 fprintf(fp
, "%f", c
->value
.f
[i
]);
280 case GLSL_TYPE_STRUCT
:
281 for (i
= 0; i
< c
->num_elements
; i
++) {
282 if (i
> 0) fprintf(fp
, ", ");
284 print_constant(c
->elements
[i
], glsl_get_struct_field(type
, i
), state
);
289 case GLSL_TYPE_ARRAY
:
290 for (i
= 0; i
< c
->num_elements
; i
++) {
291 if (i
> 0) fprintf(fp
, ", ");
293 print_constant(c
->elements
[i
], glsl_get_array_element(type
), state
);
299 unreachable("not reached");
304 print_var_decl(nir_variable
*var
, print_state
*state
)
306 FILE *fp
= state
->fp
;
308 fprintf(fp
, "decl_var ");
310 const char *const cent
= (var
->data
.centroid
) ? "centroid " : "";
311 const char *const samp
= (var
->data
.sample
) ? "sample " : "";
312 const char *const patch
= (var
->data
.patch
) ? "patch " : "";
313 const char *const inv
= (var
->data
.invariant
) ? "invariant " : "";
314 const char *const mode
[] = { "shader_in ", "shader_out ", "", "",
315 "uniform ", "shader_storage ", "shared ",
318 fprintf(fp
, "%s%s%s%s%s%s ",
319 cent
, samp
, patch
, inv
, mode
[var
->data
.mode
],
320 glsl_interp_qualifier_name(var
->data
.interpolation
));
322 glsl_print_type(var
->type
, fp
);
324 fprintf(fp
, " %s", get_var_name(var
, state
));
326 if (var
->data
.mode
== nir_var_shader_in
||
327 var
->data
.mode
== nir_var_shader_out
||
328 var
->data
.mode
== nir_var_uniform
||
329 var
->data
.mode
== nir_var_shader_storage
) {
330 const char *loc
= NULL
;
333 switch (state
->shader
->stage
) {
334 case MESA_SHADER_VERTEX
:
335 if (var
->data
.mode
== nir_var_shader_in
)
336 loc
= gl_vert_attrib_name(var
->data
.location
);
337 else if (var
->data
.mode
== nir_var_shader_out
)
338 loc
= gl_varying_slot_name(var
->data
.location
);
340 case MESA_SHADER_GEOMETRY
:
341 if ((var
->data
.mode
== nir_var_shader_in
) ||
342 (var
->data
.mode
== nir_var_shader_out
))
343 loc
= gl_varying_slot_name(var
->data
.location
);
345 case MESA_SHADER_FRAGMENT
:
346 if (var
->data
.mode
== nir_var_shader_in
)
347 loc
= gl_varying_slot_name(var
->data
.location
);
348 else if (var
->data
.mode
== nir_var_shader_out
)
349 loc
= gl_frag_result_name(var
->data
.location
);
351 case MESA_SHADER_TESS_CTRL
:
352 case MESA_SHADER_TESS_EVAL
:
353 case MESA_SHADER_COMPUTE
:
360 snprintf(buf
, sizeof(buf
), "%u", var
->data
.location
);
364 fprintf(fp
, " (%s, %u)", loc
, var
->data
.driver_location
);
367 if (var
->constant_initializer
) {
368 fprintf(fp
, " = { ");
369 print_constant(var
->constant_initializer
, var
->type
, state
);
377 print_var(nir_variable
*var
, print_state
*state
)
379 FILE *fp
= state
->fp
;
380 fprintf(fp
, "%s", get_var_name(var
, state
));
384 print_deref_var(nir_deref_var
*deref
, print_state
*state
)
386 print_var(deref
->var
, state
);
390 print_deref_array(nir_deref_array
*deref
, print_state
*state
)
392 FILE *fp
= state
->fp
;
394 switch (deref
->deref_array_type
) {
395 case nir_deref_array_type_direct
:
396 fprintf(fp
, "%u", deref
->base_offset
);
398 case nir_deref_array_type_indirect
:
399 if (deref
->base_offset
!= 0)
400 fprintf(fp
, "%u + ", deref
->base_offset
);
401 print_src(&deref
->indirect
, state
);
403 case nir_deref_array_type_wildcard
:
411 print_deref_struct(nir_deref_struct
*deref
, const struct glsl_type
*parent_type
,
414 FILE *fp
= state
->fp
;
415 fprintf(fp
, ".%s", glsl_get_struct_elem_name(parent_type
, deref
->index
));
419 print_deref(nir_deref_var
*deref
, print_state
*state
)
421 nir_deref
*tail
= &deref
->deref
;
422 nir_deref
*pretail
= NULL
;
423 while (tail
!= NULL
) {
424 switch (tail
->deref_type
) {
425 case nir_deref_type_var
:
426 assert(pretail
== NULL
);
427 assert(tail
== &deref
->deref
);
428 print_deref_var(deref
, state
);
431 case nir_deref_type_array
:
432 assert(pretail
!= NULL
);
433 print_deref_array(nir_deref_as_array(tail
), state
);
436 case nir_deref_type_struct
:
437 assert(pretail
!= NULL
);
438 print_deref_struct(nir_deref_as_struct(tail
),
439 pretail
->type
, state
);
443 unreachable("Invalid deref type");
447 tail
= pretail
->child
;
452 print_intrinsic_instr(nir_intrinsic_instr
*instr
, print_state
*state
)
454 unsigned num_srcs
= nir_intrinsic_infos
[instr
->intrinsic
].num_srcs
;
455 FILE *fp
= state
->fp
;
457 if (nir_intrinsic_infos
[instr
->intrinsic
].has_dest
) {
458 print_dest(&instr
->dest
, state
);
462 fprintf(fp
, "intrinsic %s (", nir_intrinsic_infos
[instr
->intrinsic
].name
);
464 for (unsigned i
= 0; i
< num_srcs
; i
++) {
468 print_src(&instr
->src
[i
], state
);
473 unsigned num_vars
= nir_intrinsic_infos
[instr
->intrinsic
].num_variables
;
475 for (unsigned i
= 0; i
< num_vars
; i
++) {
479 print_deref(instr
->variables
[i
], state
);
484 unsigned num_indices
= nir_intrinsic_infos
[instr
->intrinsic
].num_indices
;
486 for (unsigned i
= 0; i
< num_indices
; i
++) {
490 fprintf(fp
, "%d", instr
->const_index
[i
]);
498 struct exec_list
*var_list
= NULL
;
500 switch (instr
->intrinsic
) {
501 case nir_intrinsic_load_uniform
:
502 var_list
= &state
->shader
->uniforms
;
504 case nir_intrinsic_load_input
:
505 case nir_intrinsic_load_per_vertex_input
:
506 var_list
= &state
->shader
->inputs
;
508 case nir_intrinsic_load_output
:
509 case nir_intrinsic_store_output
:
510 case nir_intrinsic_store_per_vertex_output
:
511 var_list
= &state
->shader
->outputs
;
517 nir_foreach_variable(var
, var_list
) {
518 if ((var
->data
.driver_location
== instr
->const_index
[0]) &&
520 fprintf(fp
, "\t/* %s */", var
->name
);
527 print_tex_instr(nir_tex_instr
*instr
, print_state
*state
)
529 FILE *fp
= state
->fp
;
531 print_dest(&instr
->dest
, state
);
551 case nir_texop_txf_ms
:
552 fprintf(fp
, "txf_ms ");
563 case nir_texop_query_levels
:
564 fprintf(fp
, "query_levels ");
566 case nir_texop_texture_samples
:
567 fprintf(fp
, "texture_samples ");
569 case nir_texop_samples_identical
:
570 fprintf(fp
, "samples_identical ");
573 unreachable("Invalid texture operation");
577 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++) {
578 print_src(&instr
->src
[i
].src
, state
);
582 switch(instr
->src
[i
].src_type
) {
583 case nir_tex_src_coord
:
584 fprintf(fp
, "(coord)");
586 case nir_tex_src_projector
:
587 fprintf(fp
, "(projector)");
589 case nir_tex_src_comparitor
:
590 fprintf(fp
, "(comparitor)");
592 case nir_tex_src_offset
:
593 fprintf(fp
, "(offset)");
595 case nir_tex_src_bias
:
596 fprintf(fp
, "(bias)");
598 case nir_tex_src_lod
:
599 fprintf(fp
, "(lod)");
601 case nir_tex_src_ms_index
:
602 fprintf(fp
, "(ms_index)");
604 case nir_tex_src_ddx
:
605 fprintf(fp
, "(ddx)");
607 case nir_tex_src_ddy
:
608 fprintf(fp
, "(ddy)");
610 case nir_tex_src_texture_offset
:
611 fprintf(fp
, "(texture_offset)");
613 case nir_tex_src_sampler_offset
:
614 fprintf(fp
, "(sampler_offset)");
618 unreachable("Invalid texture source type");
625 bool has_nonzero_offset
= false;
626 for (unsigned i
= 0; i
< 4; i
++) {
627 if (instr
->const_offset
[i
] != 0) {
628 has_nonzero_offset
= true;
633 if (has_nonzero_offset
) {
634 fprintf(fp
, "[%i %i %i %i] (offset), ",
635 instr
->const_offset
[0], instr
->const_offset
[1],
636 instr
->const_offset
[2], instr
->const_offset
[3]);
639 if (instr
->op
== nir_texop_tg4
) {
640 fprintf(fp
, "%u (gather_component), ", instr
->component
);
643 if (instr
->texture
) {
644 assert(instr
->sampler
);
645 fprintf(fp
, " (texture)");
647 if (instr
->sampler
) {
648 print_deref(instr
->sampler
, state
);
649 fprintf(fp
, " (sampler)");
651 assert(instr
->texture
== NULL
);
652 fprintf(fp
, "%u (texture) %u (sampler)",
653 instr
->texture_index
, instr
->sampler_index
);
658 print_call_instr(nir_call_instr
*instr
, print_state
*state
)
660 FILE *fp
= state
->fp
;
662 fprintf(fp
, "call %s ", instr
->callee
->name
);
664 for (unsigned i
= 0; i
< instr
->num_params
; i
++) {
668 print_deref(instr
->params
[i
], state
);
671 if (instr
->return_deref
!= NULL
) {
672 if (instr
->num_params
!= 0)
674 fprintf(fp
, "returning ");
675 print_deref(instr
->return_deref
, state
);
680 print_load_const_instr(nir_load_const_instr
*instr
, print_state
*state
)
682 FILE *fp
= state
->fp
;
684 print_ssa_def(&instr
->def
, state
);
686 fprintf(fp
, " = load_const (");
688 for (unsigned i
= 0; i
< instr
->def
.num_components
; i
++) {
693 * we don't really know the type of the constant (if it will be used as a
694 * float or an int), so just print the raw constant in hex for fidelity
695 * and then print the float in a comment for readability.
698 fprintf(fp
, "0x%08x /* %f */", instr
->value
.u
[i
], instr
->value
.f
[i
]);
705 print_jump_instr(nir_jump_instr
*instr
, print_state
*state
)
707 FILE *fp
= state
->fp
;
709 switch (instr
->type
) {
711 fprintf(fp
, "break");
714 case nir_jump_continue
:
715 fprintf(fp
, "continue");
718 case nir_jump_return
:
719 fprintf(fp
, "return");
725 print_ssa_undef_instr(nir_ssa_undef_instr
* instr
, print_state
*state
)
727 FILE *fp
= state
->fp
;
728 print_ssa_def(&instr
->def
, state
);
729 fprintf(fp
, " = undefined");
733 print_phi_instr(nir_phi_instr
*instr
, print_state
*state
)
735 FILE *fp
= state
->fp
;
736 print_dest(&instr
->dest
, state
);
737 fprintf(fp
, " = phi ");
738 nir_foreach_phi_src(instr
, src
) {
739 if (&src
->node
!= exec_list_get_head(&instr
->srcs
))
742 fprintf(fp
, "block_%u: ", src
->pred
->index
);
743 print_src(&src
->src
, state
);
748 print_parallel_copy_instr(nir_parallel_copy_instr
*instr
, print_state
*state
)
750 FILE *fp
= state
->fp
;
751 nir_foreach_parallel_copy_entry(instr
, entry
) {
752 if (&entry
->node
!= exec_list_get_head(&instr
->entries
))
755 print_dest(&entry
->dest
, state
);
757 print_src(&entry
->src
, state
);
762 print_instr(const nir_instr
*instr
, print_state
*state
, unsigned tabs
)
764 FILE *fp
= state
->fp
;
765 print_tabs(tabs
, fp
);
767 switch (instr
->type
) {
768 case nir_instr_type_alu
:
769 print_alu_instr(nir_instr_as_alu(instr
), state
);
772 case nir_instr_type_call
:
773 print_call_instr(nir_instr_as_call(instr
), state
);
776 case nir_instr_type_intrinsic
:
777 print_intrinsic_instr(nir_instr_as_intrinsic(instr
), state
);
780 case nir_instr_type_tex
:
781 print_tex_instr(nir_instr_as_tex(instr
), state
);
784 case nir_instr_type_load_const
:
785 print_load_const_instr(nir_instr_as_load_const(instr
), state
);
788 case nir_instr_type_jump
:
789 print_jump_instr(nir_instr_as_jump(instr
), state
);
792 case nir_instr_type_ssa_undef
:
793 print_ssa_undef_instr(nir_instr_as_ssa_undef(instr
), state
);
796 case nir_instr_type_phi
:
797 print_phi_instr(nir_instr_as_phi(instr
), state
);
800 case nir_instr_type_parallel_copy
:
801 print_parallel_copy_instr(nir_instr_as_parallel_copy(instr
), state
);
805 unreachable("Invalid instruction type");
811 compare_block_index(const void *p1
, const void *p2
)
813 const nir_block
*block1
= *((const nir_block
**) p1
);
814 const nir_block
*block2
= *((const nir_block
**) p2
);
816 return (int) block1
->index
- (int) block2
->index
;
819 static void print_cf_node(nir_cf_node
*node
, print_state
*state
,
823 print_block(nir_block
*block
, print_state
*state
, unsigned tabs
)
825 FILE *fp
= state
->fp
;
827 print_tabs(tabs
, fp
);
828 fprintf(fp
, "block block_%u:\n", block
->index
);
830 /* sort the predecessors by index so we consistently print the same thing */
833 malloc(block
->predecessors
->entries
* sizeof(nir_block
*));
835 struct set_entry
*entry
;
837 set_foreach(block
->predecessors
, entry
) {
838 preds
[i
++] = (nir_block
*) entry
->key
;
841 qsort(preds
, block
->predecessors
->entries
, sizeof(nir_block
*),
842 compare_block_index
);
844 print_tabs(tabs
, fp
);
845 fprintf(fp
, "/* preds: ");
846 for (unsigned i
= 0; i
< block
->predecessors
->entries
; i
++) {
847 fprintf(fp
, "block_%u ", preds
[i
]->index
);
853 nir_foreach_instr(block
, instr
) {
854 print_instr(instr
, state
, tabs
);
858 print_tabs(tabs
, fp
);
859 fprintf(fp
, "/* succs: ");
860 for (unsigned i
= 0; i
< 2; i
++)
861 if (block
->successors
[i
]) {
862 fprintf(fp
, "block_%u ", block
->successors
[i
]->index
);
868 print_if(nir_if
*if_stmt
, print_state
*state
, unsigned tabs
)
870 FILE *fp
= state
->fp
;
872 print_tabs(tabs
, fp
);
874 print_src(&if_stmt
->condition
, state
);
876 foreach_list_typed(nir_cf_node
, node
, node
, &if_stmt
->then_list
) {
877 print_cf_node(node
, state
, tabs
+ 1);
879 print_tabs(tabs
, fp
);
880 fprintf(fp
, "} else {\n");
881 foreach_list_typed(nir_cf_node
, node
, node
, &if_stmt
->else_list
) {
882 print_cf_node(node
, state
, tabs
+ 1);
884 print_tabs(tabs
, fp
);
889 print_loop(nir_loop
*loop
, print_state
*state
, unsigned tabs
)
891 FILE *fp
= state
->fp
;
893 print_tabs(tabs
, fp
);
894 fprintf(fp
, "loop {\n");
895 foreach_list_typed(nir_cf_node
, node
, node
, &loop
->body
) {
896 print_cf_node(node
, state
, tabs
+ 1);
898 print_tabs(tabs
, fp
);
903 print_cf_node(nir_cf_node
*node
, print_state
*state
, unsigned int tabs
)
905 switch (node
->type
) {
906 case nir_cf_node_block
:
907 print_block(nir_cf_node_as_block(node
), state
, tabs
);
911 print_if(nir_cf_node_as_if(node
), state
, tabs
);
914 case nir_cf_node_loop
:
915 print_loop(nir_cf_node_as_loop(node
), state
, tabs
);
919 unreachable("Invalid CFG node type");
924 print_function_impl(nir_function_impl
*impl
, print_state
*state
)
926 FILE *fp
= state
->fp
;
928 fprintf(fp
, "\nimpl %s ", impl
->function
->name
);
930 for (unsigned i
= 0; i
< impl
->num_params
; i
++) {
934 print_var(impl
->params
[i
], state
);
937 if (impl
->return_var
!= NULL
) {
938 if (impl
->num_params
!= 0)
940 fprintf(fp
, "returning ");
941 print_var(impl
->return_var
, state
);
946 for (unsigned i
= 0; i
< impl
->num_params
; i
++) {
948 print_var_decl(impl
->params
[i
], state
);
951 if (impl
->return_var
) {
953 print_var_decl(impl
->return_var
, state
);
956 nir_foreach_variable(var
, &impl
->locals
) {
958 print_var_decl(var
, state
);
961 foreach_list_typed(nir_register
, reg
, node
, &impl
->registers
) {
963 print_register_decl(reg
, state
);
966 nir_index_blocks(impl
);
968 foreach_list_typed(nir_cf_node
, node
, node
, &impl
->body
) {
969 print_cf_node(node
, state
, 1);
972 fprintf(fp
, "\tblock block_%u:\n}\n\n", impl
->end_block
->index
);
976 print_function(nir_function
*function
, print_state
*state
)
978 FILE *fp
= state
->fp
;
980 fprintf(fp
, "decl_function %s ", function
->name
);
982 for (unsigned i
= 0; i
< function
->num_params
; i
++) {
986 switch (function
->params
[i
].param_type
) {
987 case nir_parameter_in
:
990 case nir_parameter_out
:
993 case nir_parameter_inout
:
994 fprintf(fp
, "inout ");
997 unreachable("Invalid parameter type");
1000 glsl_print_type(function
->params
[i
].type
, fp
);
1003 if (function
->return_type
!= NULL
) {
1004 if (function
->num_params
!= 0)
1006 fprintf(fp
, "returning ");
1007 glsl_print_type(function
->return_type
, fp
);
1012 if (function
->impl
!= NULL
) {
1013 print_function_impl(function
->impl
, state
);
1019 init_print_state(print_state
*state
, nir_shader
*shader
, FILE *fp
)
1022 state
->shader
= shader
;
1023 state
->ht
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
1024 _mesa_key_pointer_equal
);
1025 state
->syms
= _mesa_set_create(NULL
, _mesa_key_hash_string
,
1026 _mesa_key_string_equal
);
1031 destroy_print_state(print_state
*state
)
1033 _mesa_hash_table_destroy(state
->ht
, NULL
);
1034 _mesa_set_destroy(state
->syms
, NULL
);
1038 nir_print_shader(nir_shader
*shader
, FILE *fp
)
1041 init_print_state(&state
, shader
, fp
);
1043 fprintf(fp
, "shader: %s\n", gl_shader_stage_name(shader
->stage
));
1045 if (shader
->info
.name
)
1046 fprintf(fp
, "name: %s\n", shader
->info
.name
);
1048 if (shader
->info
.label
)
1049 fprintf(fp
, "label: %s\n", shader
->info
.label
);
1051 fprintf(fp
, "inputs: %u\n", shader
->num_inputs
);
1052 fprintf(fp
, "outputs: %u\n", shader
->num_outputs
);
1053 fprintf(fp
, "uniforms: %u\n", shader
->num_uniforms
);
1054 fprintf(fp
, "shared: %u\n", shader
->num_shared
);
1056 nir_foreach_variable(var
, &shader
->uniforms
) {
1057 print_var_decl(var
, &state
);
1060 nir_foreach_variable(var
, &shader
->inputs
) {
1061 print_var_decl(var
, &state
);
1064 nir_foreach_variable(var
, &shader
->outputs
) {
1065 print_var_decl(var
, &state
);
1068 nir_foreach_variable(var
, &shader
->shared
) {
1069 print_var_decl(var
, &state
);
1072 nir_foreach_variable(var
, &shader
->globals
) {
1073 print_var_decl(var
, &state
);
1076 nir_foreach_variable(var
, &shader
->system_values
) {
1077 print_var_decl(var
, &state
);
1080 foreach_list_typed(nir_register
, reg
, node
, &shader
->registers
) {
1081 print_register_decl(reg
, &state
);
1084 foreach_list_typed(nir_function
, func
, node
, &shader
->functions
) {
1085 print_function(func
, &state
);
1088 destroy_print_state(&state
);
1092 nir_print_instr(const nir_instr
*instr
, FILE *fp
)
1094 print_state state
= {
1097 print_instr(instr
, &state
, 0);