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",
516 [NIR_INTRINSIC_RANGE
] = "range",
517 [NIR_INTRINSIC_DESC_SET
] = "desc-set",
518 [NIR_INTRINSIC_BINDING
] = "binding",
520 for (unsigned idx
= 1; idx
< NIR_INTRINSIC_NUM_INDEX_FLAGS
; idx
++) {
521 if (!info
->index_map
[idx
])
524 if (idx
== NIR_INTRINSIC_WRMASK
) {
525 /* special case wrmask to show it as a writemask.. */
526 unsigned wrmask
= nir_intrinsic_write_mask(instr
);
527 fprintf(fp
, " wrmask=");
528 for (unsigned i
= 0; i
< 4; i
++)
529 if ((wrmask
>> i
) & 1)
530 fprintf(fp
, "%c", "xyzw"[i
]);
532 unsigned off
= info
->index_map
[idx
] - 1;
533 assert(index_name
[idx
]); /* forgot to update index_name table? */
534 fprintf(fp
, " %s=%d", index_name
[idx
], instr
->const_index
[off
]);
542 struct exec_list
*var_list
= NULL
;
544 switch (instr
->intrinsic
) {
545 case nir_intrinsic_load_uniform
:
546 var_list
= &state
->shader
->uniforms
;
548 case nir_intrinsic_load_input
:
549 case nir_intrinsic_load_per_vertex_input
:
550 var_list
= &state
->shader
->inputs
;
552 case nir_intrinsic_load_output
:
553 case nir_intrinsic_store_output
:
554 case nir_intrinsic_store_per_vertex_output
:
555 var_list
= &state
->shader
->outputs
;
561 nir_foreach_variable(var
, var_list
) {
562 if ((var
->data
.driver_location
== nir_intrinsic_base(instr
)) &&
564 fprintf(fp
, "\t/* %s */", var
->name
);
571 print_tex_instr(nir_tex_instr
*instr
, print_state
*state
)
573 FILE *fp
= state
->fp
;
575 print_dest(&instr
->dest
, state
);
595 case nir_texop_txf_ms
:
596 fprintf(fp
, "txf_ms ");
607 case nir_texop_query_levels
:
608 fprintf(fp
, "query_levels ");
610 case nir_texop_texture_samples
:
611 fprintf(fp
, "texture_samples ");
613 case nir_texop_samples_identical
:
614 fprintf(fp
, "samples_identical ");
617 unreachable("Invalid texture operation");
621 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++) {
622 print_src(&instr
->src
[i
].src
, state
);
626 switch(instr
->src
[i
].src_type
) {
627 case nir_tex_src_coord
:
628 fprintf(fp
, "(coord)");
630 case nir_tex_src_projector
:
631 fprintf(fp
, "(projector)");
633 case nir_tex_src_comparitor
:
634 fprintf(fp
, "(comparitor)");
636 case nir_tex_src_offset
:
637 fprintf(fp
, "(offset)");
639 case nir_tex_src_bias
:
640 fprintf(fp
, "(bias)");
642 case nir_tex_src_lod
:
643 fprintf(fp
, "(lod)");
645 case nir_tex_src_ms_index
:
646 fprintf(fp
, "(ms_index)");
648 case nir_tex_src_ddx
:
649 fprintf(fp
, "(ddx)");
651 case nir_tex_src_ddy
:
652 fprintf(fp
, "(ddy)");
654 case nir_tex_src_texture_offset
:
655 fprintf(fp
, "(texture_offset)");
657 case nir_tex_src_sampler_offset
:
658 fprintf(fp
, "(sampler_offset)");
662 unreachable("Invalid texture source type");
669 if (instr
->op
== nir_texop_tg4
) {
670 fprintf(fp
, "%u (gather_component), ", instr
->component
);
673 if (instr
->texture
) {
674 print_deref(instr
->texture
, state
);
675 fprintf(fp
, " (texture)");
676 if (instr
->sampler
) {
677 print_deref(instr
->sampler
, state
);
678 fprintf(fp
, " (sampler)");
681 assert(instr
->sampler
== NULL
);
682 fprintf(fp
, "%u (texture) %u (sampler)",
683 instr
->texture_index
, instr
->sampler_index
);
688 print_call_instr(nir_call_instr
*instr
, print_state
*state
)
690 FILE *fp
= state
->fp
;
692 fprintf(fp
, "call %s ", instr
->callee
->name
);
694 for (unsigned i
= 0; i
< instr
->num_params
; i
++) {
698 print_deref(instr
->params
[i
], state
);
701 if (instr
->return_deref
!= NULL
) {
702 if (instr
->num_params
!= 0)
704 fprintf(fp
, "returning ");
705 print_deref(instr
->return_deref
, state
);
710 print_load_const_instr(nir_load_const_instr
*instr
, print_state
*state
)
712 FILE *fp
= state
->fp
;
714 print_ssa_def(&instr
->def
, state
);
716 fprintf(fp
, " = load_const (");
718 for (unsigned i
= 0; i
< instr
->def
.num_components
; i
++) {
723 * we don't really know the type of the constant (if it will be used as a
724 * float or an int), so just print the raw constant in hex for fidelity
725 * and then print the float in a comment for readability.
728 if (instr
->def
.bit_size
== 64)
729 fprintf(fp
, "0x%16" PRIx64
" /* %f */", instr
->value
.u64
[i
],
730 instr
->value
.f64
[i
]);
732 fprintf(fp
, "0x%08x /* %f */", instr
->value
.u32
[i
], instr
->value
.f32
[i
]);
739 print_jump_instr(nir_jump_instr
*instr
, print_state
*state
)
741 FILE *fp
= state
->fp
;
743 switch (instr
->type
) {
745 fprintf(fp
, "break");
748 case nir_jump_continue
:
749 fprintf(fp
, "continue");
752 case nir_jump_return
:
753 fprintf(fp
, "return");
759 print_ssa_undef_instr(nir_ssa_undef_instr
* instr
, print_state
*state
)
761 FILE *fp
= state
->fp
;
762 print_ssa_def(&instr
->def
, state
);
763 fprintf(fp
, " = undefined");
767 print_phi_instr(nir_phi_instr
*instr
, print_state
*state
)
769 FILE *fp
= state
->fp
;
770 print_dest(&instr
->dest
, state
);
771 fprintf(fp
, " = phi ");
772 nir_foreach_phi_src(instr
, src
) {
773 if (&src
->node
!= exec_list_get_head(&instr
->srcs
))
776 fprintf(fp
, "block_%u: ", src
->pred
->index
);
777 print_src(&src
->src
, state
);
782 print_parallel_copy_instr(nir_parallel_copy_instr
*instr
, print_state
*state
)
784 FILE *fp
= state
->fp
;
785 nir_foreach_parallel_copy_entry(instr
, entry
) {
786 if (&entry
->node
!= exec_list_get_head(&instr
->entries
))
789 print_dest(&entry
->dest
, state
);
791 print_src(&entry
->src
, state
);
796 print_instr(const nir_instr
*instr
, print_state
*state
, unsigned tabs
)
798 FILE *fp
= state
->fp
;
799 print_tabs(tabs
, fp
);
801 switch (instr
->type
) {
802 case nir_instr_type_alu
:
803 print_alu_instr(nir_instr_as_alu(instr
), state
);
806 case nir_instr_type_call
:
807 print_call_instr(nir_instr_as_call(instr
), state
);
810 case nir_instr_type_intrinsic
:
811 print_intrinsic_instr(nir_instr_as_intrinsic(instr
), state
);
814 case nir_instr_type_tex
:
815 print_tex_instr(nir_instr_as_tex(instr
), state
);
818 case nir_instr_type_load_const
:
819 print_load_const_instr(nir_instr_as_load_const(instr
), state
);
822 case nir_instr_type_jump
:
823 print_jump_instr(nir_instr_as_jump(instr
), state
);
826 case nir_instr_type_ssa_undef
:
827 print_ssa_undef_instr(nir_instr_as_ssa_undef(instr
), state
);
830 case nir_instr_type_phi
:
831 print_phi_instr(nir_instr_as_phi(instr
), state
);
834 case nir_instr_type_parallel_copy
:
835 print_parallel_copy_instr(nir_instr_as_parallel_copy(instr
), state
);
839 unreachable("Invalid instruction type");
845 compare_block_index(const void *p1
, const void *p2
)
847 const nir_block
*block1
= *((const nir_block
**) p1
);
848 const nir_block
*block2
= *((const nir_block
**) p2
);
850 return (int) block1
->index
- (int) block2
->index
;
853 static void print_cf_node(nir_cf_node
*node
, print_state
*state
,
857 print_block(nir_block
*block
, print_state
*state
, unsigned tabs
)
859 FILE *fp
= state
->fp
;
861 print_tabs(tabs
, fp
);
862 fprintf(fp
, "block block_%u:\n", block
->index
);
864 /* sort the predecessors by index so we consistently print the same thing */
867 malloc(block
->predecessors
->entries
* sizeof(nir_block
*));
869 struct set_entry
*entry
;
871 set_foreach(block
->predecessors
, entry
) {
872 preds
[i
++] = (nir_block
*) entry
->key
;
875 qsort(preds
, block
->predecessors
->entries
, sizeof(nir_block
*),
876 compare_block_index
);
878 print_tabs(tabs
, fp
);
879 fprintf(fp
, "/* preds: ");
880 for (unsigned i
= 0; i
< block
->predecessors
->entries
; i
++) {
881 fprintf(fp
, "block_%u ", preds
[i
]->index
);
887 nir_foreach_instr(block
, instr
) {
888 print_instr(instr
, state
, tabs
);
892 print_tabs(tabs
, fp
);
893 fprintf(fp
, "/* succs: ");
894 for (unsigned i
= 0; i
< 2; i
++)
895 if (block
->successors
[i
]) {
896 fprintf(fp
, "block_%u ", block
->successors
[i
]->index
);
902 print_if(nir_if
*if_stmt
, print_state
*state
, unsigned tabs
)
904 FILE *fp
= state
->fp
;
906 print_tabs(tabs
, fp
);
908 print_src(&if_stmt
->condition
, state
);
910 foreach_list_typed(nir_cf_node
, node
, node
, &if_stmt
->then_list
) {
911 print_cf_node(node
, state
, tabs
+ 1);
913 print_tabs(tabs
, fp
);
914 fprintf(fp
, "} else {\n");
915 foreach_list_typed(nir_cf_node
, node
, node
, &if_stmt
->else_list
) {
916 print_cf_node(node
, state
, tabs
+ 1);
918 print_tabs(tabs
, fp
);
923 print_loop(nir_loop
*loop
, print_state
*state
, unsigned tabs
)
925 FILE *fp
= state
->fp
;
927 print_tabs(tabs
, fp
);
928 fprintf(fp
, "loop {\n");
929 foreach_list_typed(nir_cf_node
, node
, node
, &loop
->body
) {
930 print_cf_node(node
, state
, tabs
+ 1);
932 print_tabs(tabs
, fp
);
937 print_cf_node(nir_cf_node
*node
, print_state
*state
, unsigned int tabs
)
939 switch (node
->type
) {
940 case nir_cf_node_block
:
941 print_block(nir_cf_node_as_block(node
), state
, tabs
);
945 print_if(nir_cf_node_as_if(node
), state
, tabs
);
948 case nir_cf_node_loop
:
949 print_loop(nir_cf_node_as_loop(node
), state
, tabs
);
953 unreachable("Invalid CFG node type");
958 print_function_impl(nir_function_impl
*impl
, print_state
*state
)
960 FILE *fp
= state
->fp
;
962 fprintf(fp
, "\nimpl %s ", impl
->function
->name
);
964 for (unsigned i
= 0; i
< impl
->num_params
; i
++) {
968 print_arg(impl
->params
[i
], state
);
971 if (impl
->return_var
!= NULL
) {
972 if (impl
->num_params
!= 0)
974 fprintf(fp
, "returning ");
975 print_arg(impl
->return_var
, state
);
980 nir_foreach_variable(var
, &impl
->locals
) {
982 print_var_decl(var
, state
);
985 foreach_list_typed(nir_register
, reg
, node
, &impl
->registers
) {
987 print_register_decl(reg
, state
);
990 nir_index_blocks(impl
);
992 foreach_list_typed(nir_cf_node
, node
, node
, &impl
->body
) {
993 print_cf_node(node
, state
, 1);
996 fprintf(fp
, "\tblock block_%u:\n}\n\n", impl
->end_block
->index
);
1000 print_function(nir_function
*function
, print_state
*state
)
1002 FILE *fp
= state
->fp
;
1004 fprintf(fp
, "decl_function %s ", function
->name
);
1006 for (unsigned i
= 0; i
< function
->num_params
; i
++) {
1010 switch (function
->params
[i
].param_type
) {
1011 case nir_parameter_in
:
1014 case nir_parameter_out
:
1015 fprintf(fp
, "out ");
1017 case nir_parameter_inout
:
1018 fprintf(fp
, "inout ");
1021 unreachable("Invalid parameter type");
1024 glsl_print_type(function
->params
[i
].type
, fp
);
1027 if (function
->return_type
!= NULL
) {
1028 if (function
->num_params
!= 0)
1030 fprintf(fp
, "returning ");
1031 glsl_print_type(function
->return_type
, fp
);
1036 if (function
->impl
!= NULL
) {
1037 print_function_impl(function
->impl
, state
);
1043 init_print_state(print_state
*state
, nir_shader
*shader
, FILE *fp
)
1046 state
->shader
= shader
;
1047 state
->ht
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
1048 _mesa_key_pointer_equal
);
1049 state
->syms
= _mesa_set_create(NULL
, _mesa_key_hash_string
,
1050 _mesa_key_string_equal
);
1055 destroy_print_state(print_state
*state
)
1057 _mesa_hash_table_destroy(state
->ht
, NULL
);
1058 _mesa_set_destroy(state
->syms
, NULL
);
1062 nir_print_shader(nir_shader
*shader
, FILE *fp
)
1065 init_print_state(&state
, shader
, fp
);
1067 fprintf(fp
, "shader: %s\n", gl_shader_stage_name(shader
->stage
));
1069 if (shader
->info
.name
)
1070 fprintf(fp
, "name: %s\n", shader
->info
.name
);
1072 if (shader
->info
.label
)
1073 fprintf(fp
, "label: %s\n", shader
->info
.label
);
1075 fprintf(fp
, "inputs: %u\n", shader
->num_inputs
);
1076 fprintf(fp
, "outputs: %u\n", shader
->num_outputs
);
1077 fprintf(fp
, "uniforms: %u\n", shader
->num_uniforms
);
1078 fprintf(fp
, "shared: %u\n", shader
->num_shared
);
1080 nir_foreach_variable(var
, &shader
->uniforms
) {
1081 print_var_decl(var
, &state
);
1084 nir_foreach_variable(var
, &shader
->inputs
) {
1085 print_var_decl(var
, &state
);
1088 nir_foreach_variable(var
, &shader
->outputs
) {
1089 print_var_decl(var
, &state
);
1092 nir_foreach_variable(var
, &shader
->shared
) {
1093 print_var_decl(var
, &state
);
1096 nir_foreach_variable(var
, &shader
->globals
) {
1097 print_var_decl(var
, &state
);
1100 nir_foreach_variable(var
, &shader
->system_values
) {
1101 print_var_decl(var
, &state
);
1104 foreach_list_typed(nir_register
, reg
, node
, &shader
->registers
) {
1105 print_register_decl(reg
, &state
);
1108 foreach_list_typed(nir_function
, func
, node
, &shader
->functions
) {
1109 print_function(func
, &state
);
1112 destroy_print_state(&state
);
1116 nir_print_instr(const nir_instr
*instr
, FILE *fp
)
1118 print_state state
= {
1121 print_instr(instr
, &state
, 0);