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)
33 print_tabs(unsigned num_tabs
, FILE *fp
)
35 for (unsigned i
= 0; i
< num_tabs
; i
++)
40 /** map from nir_variable -> printable name */
41 struct hash_table
*ht
;
43 /** set of names used so far for nir_variables */
46 /* an index used to make new non-conflicting names */
51 print_register(nir_register
*reg
, FILE *fp
)
53 if (reg
->name
!= NULL
)
54 fprintf(fp
, "/* %s */ ", reg
->name
);
56 fprintf(fp
, "gr%u", reg
->index
);
58 fprintf(fp
, "r%u", reg
->index
);
61 static const char *sizes
[] = { "error", "vec1", "vec2", "vec3", "vec4" };
64 print_register_decl(nir_register
*reg
, FILE *fp
)
66 fprintf(fp
, "decl_reg %s ", sizes
[reg
->num_components
]);
68 fprintf(fp
, "(packed) ");
69 print_register(reg
, fp
);
70 if (reg
->num_array_elems
!= 0)
71 fprintf(fp
, "[%u]", reg
->num_array_elems
);
76 print_ssa_def(nir_ssa_def
*def
, FILE *fp
)
78 if (def
->name
!= NULL
)
79 fprintf(fp
, "/* %s */ ", def
->name
);
80 fprintf(fp
, "%s ssa_%u", sizes
[def
->num_components
], def
->index
);
84 print_ssa_use(nir_ssa_def
*def
, FILE *fp
)
86 if (def
->name
!= NULL
)
87 fprintf(fp
, "/* %s */ ", def
->name
);
88 fprintf(fp
, "ssa_%u", def
->index
);
91 static void print_src(nir_src
*src
, FILE *fp
);
94 print_reg_src(nir_reg_src
*src
, FILE *fp
)
96 print_register(src
->reg
, fp
);
97 if (src
->reg
->num_array_elems
!= 0) {
98 fprintf(fp
, "[%u", src
->base_offset
);
99 if (src
->indirect
!= NULL
) {
101 print_src(src
->indirect
, fp
);
108 print_reg_dest(nir_reg_dest
*dest
, FILE *fp
)
110 print_register(dest
->reg
, fp
);
111 if (dest
->reg
->num_array_elems
!= 0) {
112 fprintf(fp
, "[%u", dest
->base_offset
);
113 if (dest
->indirect
!= NULL
) {
115 print_src(dest
->indirect
, fp
);
122 print_src(nir_src
*src
, FILE *fp
)
125 print_ssa_use(src
->ssa
, fp
);
127 print_reg_src(&src
->reg
, fp
);
131 print_dest(nir_dest
*dest
, FILE *fp
)
134 print_ssa_def(&dest
->ssa
, fp
);
136 print_reg_dest(&dest
->reg
, fp
);
140 print_alu_src(nir_alu_src
*src
, FILE *fp
)
147 print_src(&src
->src
, fp
);
149 if (src
->swizzle
[0] != 0 ||
150 src
->swizzle
[1] != 1 ||
151 src
->swizzle
[2] != 2 ||
152 src
->swizzle
[3] != 3) {
154 for (unsigned i
= 0; i
< 4; i
++)
155 fprintf(fp
, "%c", "xyzw"[src
->swizzle
[i
]]);
163 print_alu_dest(nir_alu_dest
*dest
, FILE *fp
)
165 /* we're going to print the saturate modifier later, after the opcode */
167 print_dest(&dest
->dest
, fp
);
169 if (!dest
->dest
.is_ssa
&&
170 dest
->write_mask
!= (1 << dest
->dest
.reg
.reg
->num_components
) - 1) {
172 for (unsigned i
= 0; i
< 4; i
++)
173 if ((dest
->write_mask
>> i
) & 1)
174 fprintf(fp
, "%c", "xyzw"[i
]);
179 print_alu_instr(nir_alu_instr
*instr
, FILE *fp
)
181 print_alu_dest(&instr
->dest
, fp
);
183 fprintf(fp
, " = %s", nir_op_infos
[instr
->op
].name
);
184 if (instr
->dest
.saturate
)
189 for (unsigned i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++) {
193 print_alu_src(&instr
->src
[i
], fp
);
200 print_var_decl(nir_variable
*var
, print_var_state
*state
, FILE *fp
)
202 fprintf(fp
, "decl_var ");
204 const char *const cent
= (var
->data
.centroid
) ? "centroid " : "";
205 const char *const samp
= (var
->data
.sample
) ? "sample " : "";
206 const char *const inv
= (var
->data
.invariant
) ? "invariant " : "";
207 const char *const mode
[] = { "shader_in ", "shader_out ", "", "",
208 "uniform ", "system " };
209 const char *const interp
[] = { "", "smooth", "flat", "noperspective" };
211 fprintf(fp
, "%s%s%s%s%s ",
212 cent
, samp
, inv
, mode
[var
->data
.mode
], interp
[var
->data
.interpolation
]);
214 glsl_print_type(var
->type
, fp
);
216 struct set_entry
*entry
=
217 _mesa_set_search(state
->syms
, _mesa_hash_string(var
->name
), var
->name
);
222 /* we have a collision with another name, append an @ + a unique index */
223 name
= ralloc_asprintf(state
->syms
, "%s@%u", var
->name
, state
->index
++);
228 fprintf(fp
, " %s", name
);
230 if (var
->data
.mode
== nir_var_shader_in
||
231 var
->data
.mode
== nir_var_shader_out
||
232 var
->data
.mode
== nir_var_uniform
) {
233 fprintf(fp
, " (%u)", var
->data
.driver_location
);
238 _mesa_set_add(state
->syms
, _mesa_hash_string(name
), name
);
239 _mesa_hash_table_insert(state
->ht
, var
, name
);
243 print_var(nir_variable
*var
, print_var_state
*state
, FILE *fp
)
245 struct hash_entry
*entry
= _mesa_hash_table_search(state
->ht
, var
);
247 assert(entry
!= NULL
);
249 fprintf(fp
, "%s", (char *) entry
->data
);
253 print_deref_var(nir_deref_var
*deref
, print_var_state
*state
, FILE *fp
)
255 print_var(deref
->var
, state
, fp
);
259 print_deref_array(nir_deref_array
*deref
, print_var_state
*state
, FILE *fp
)
262 switch (deref
->deref_array_type
) {
263 case nir_deref_array_type_direct
:
264 fprintf(fp
, "%u", deref
->base_offset
);
266 case nir_deref_array_type_indirect
:
267 if (deref
->base_offset
!= 0)
268 fprintf(fp
, "%u + ", deref
->base_offset
);
269 print_src(&deref
->indirect
, fp
);
271 case nir_deref_array_type_wildcard
:
279 print_deref_struct(nir_deref_struct
*deref
, const struct glsl_type
*parent_type
,
280 print_var_state
*state
, FILE *fp
)
282 fprintf(fp
, ".%s", glsl_get_struct_elem_name(parent_type
, deref
->index
));
286 print_deref(nir_deref_var
*deref
, print_var_state
*state
, FILE *fp
)
288 nir_deref
*tail
= &deref
->deref
;
289 nir_deref
*pretail
= NULL
;
290 while (tail
!= NULL
) {
291 switch (tail
->deref_type
) {
292 case nir_deref_type_var
:
293 assert(pretail
== NULL
);
294 assert(tail
== &deref
->deref
);
295 print_deref_var(deref
, state
, fp
);
298 case nir_deref_type_array
:
299 assert(pretail
!= NULL
);
300 print_deref_array(nir_deref_as_array(tail
), state
, fp
);
303 case nir_deref_type_struct
:
304 assert(pretail
!= NULL
);
305 print_deref_struct(nir_deref_as_struct(tail
),
306 pretail
->type
, state
, fp
);
310 unreachable("Invalid deref type");
314 tail
= pretail
->child
;
319 print_intrinsic_instr(nir_intrinsic_instr
*instr
, print_var_state
*state
,
322 unsigned num_srcs
= nir_intrinsic_infos
[instr
->intrinsic
].num_srcs
;
324 if (nir_intrinsic_infos
[instr
->intrinsic
].has_dest
) {
325 print_dest(&instr
->dest
, fp
);
329 fprintf(fp
, "intrinsic %s (", nir_intrinsic_infos
[instr
->intrinsic
].name
);
332 for (unsigned i
= 0; i
< num_srcs
; i
++) {
336 print_src(&instr
->src
[i
], fp
);
343 unsigned num_vars
= nir_intrinsic_infos
[instr
->intrinsic
].num_variables
;
346 for (unsigned i
= 0; i
< num_vars
; i
++) {
350 print_deref(instr
->variables
[i
], state
, fp
);
357 unsigned num_indices
= nir_intrinsic_infos
[instr
->intrinsic
].num_indices
;
360 for (unsigned i
= 0; i
< num_indices
; i
++) {
364 fprintf(fp
, "%u", instr
->const_index
[i
]);
373 print_tex_instr(nir_tex_instr
*instr
, print_var_state
*state
, FILE *fp
)
375 print_dest(&instr
->dest
, fp
);
395 case nir_texop_txf_ms
:
396 fprintf(fp
, "txf_ms ");
407 case nir_texop_query_levels
:
408 fprintf(fp
, "query_levels ");
412 unreachable("Invalid texture operation");
416 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++) {
417 print_src(&instr
->src
[i
], fp
);
421 switch(instr
->src_type
[i
]) {
422 case nir_tex_src_coord
:
423 fprintf(fp
, "(coord)");
425 case nir_tex_src_projector
:
426 fprintf(fp
, "(projector)");
428 case nir_tex_src_comparitor
:
429 fprintf(fp
, "(comparitor)");
431 case nir_tex_src_offset
:
432 fprintf(fp
, "(offset)");
434 case nir_tex_src_bias
:
435 fprintf(fp
, "(bias)");
437 case nir_tex_src_lod
:
438 fprintf(fp
, "(lod)");
440 case nir_tex_src_ms_index
:
441 fprintf(fp
, "(ms_index)");
443 case nir_tex_src_ddx
:
444 fprintf(fp
, "(ddx)");
446 case nir_tex_src_ddy
:
447 fprintf(fp
, "(ddy)");
449 case nir_tex_src_sampler_offset
:
450 fprintf(fp
, "(sampler_offset)");
454 unreachable("Invalid texture source type");
461 bool offset_nonzero
= false;
462 for (unsigned i
= 0; i
< 4; i
++)
463 if (instr
->const_offset
[i
] != 0) {
464 offset_nonzero
= true;
468 if (offset_nonzero
) {
469 fprintf(fp
, "[%i %i %i %i] (offset), ",
470 instr
->const_offset
[0], instr
->const_offset
[1],
471 instr
->const_offset
[2], instr
->const_offset
[3]);
474 if (instr
->op
== nir_texop_tg4
) {
475 fprintf(fp
, "%u (gather_component), ", instr
->component
);
478 if (instr
->sampler
) {
479 print_deref(instr
->sampler
, state
, fp
);
481 fprintf(fp
, "%u", instr
->sampler_index
);
484 fprintf(fp
, " (sampler)");
488 print_call_instr(nir_call_instr
*instr
, print_var_state
*state
, FILE *fp
)
490 fprintf(fp
, "call %s ", instr
->callee
->function
->name
);
492 for (unsigned i
= 0; i
< instr
->num_params
; i
++) {
496 print_deref(instr
->params
[i
], state
, fp
);
499 if (instr
->return_deref
!= NULL
) {
500 if (instr
->num_params
!= 0)
502 fprintf(fp
, "returning ");
503 print_deref(instr
->return_deref
, state
, fp
);
508 print_load_const_instr(nir_load_const_instr
*instr
, unsigned tabs
, FILE *fp
)
510 print_ssa_def(&instr
->def
, fp
);
512 fprintf(fp
, " = load_const (");
515 for (unsigned i
= 0; i
< instr
->def
.num_components
; i
++) {
520 * we don't really know the type of the constant (if it will be used as a
521 * float or an int), so just print the raw constant in hex for fidelity
522 * and then print the float in a comment for readability.
525 fprintf(fp
, "0x%08x /* %f */", instr
->value
.u
[i
], instr
->value
.f
[i
]);
532 print_jump_instr(nir_jump_instr
*instr
, FILE *fp
)
534 switch (instr
->type
) {
536 fprintf(fp
, "break");
539 case nir_jump_continue
:
540 fprintf(fp
, "continue");
543 case nir_jump_return
:
544 fprintf(fp
, "return");
550 print_ssa_undef_instr(nir_ssa_undef_instr
* instr
, FILE *fp
)
552 print_ssa_def(&instr
->def
, fp
);
553 fprintf(fp
, " = undefined");
557 print_phi_instr(nir_phi_instr
*instr
, FILE *fp
)
559 print_dest(&instr
->dest
, fp
);
560 fprintf(fp
, " = phi ");
562 foreach_list_typed(nir_phi_src
, src
, node
, &instr
->srcs
) {
566 fprintf(fp
, "block_%u: ", src
->pred
->index
);
567 print_src(&src
->src
, fp
);
574 print_parallel_copy_instr(nir_parallel_copy_instr
*instr
, FILE *fp
)
577 fprintf(fp
, "pcopy: ");
578 foreach_list_typed(nir_parallel_copy_copy
, copy
, node
, &instr
->copies
) {
582 print_dest(©
->dest
, fp
);
584 print_src(©
->src
, fp
);
591 print_instr(nir_instr
*instr
, print_var_state
*state
, unsigned tabs
, FILE *fp
)
593 print_tabs(tabs
, fp
);
595 switch (instr
->type
) {
596 case nir_instr_type_alu
:
597 print_alu_instr(nir_instr_as_alu(instr
), fp
);
600 case nir_instr_type_call
:
601 print_call_instr(nir_instr_as_call(instr
), state
, fp
);
604 case nir_instr_type_intrinsic
:
605 print_intrinsic_instr(nir_instr_as_intrinsic(instr
), state
, fp
);
608 case nir_instr_type_tex
:
609 print_tex_instr(nir_instr_as_tex(instr
), state
, fp
);
612 case nir_instr_type_load_const
:
613 print_load_const_instr(nir_instr_as_load_const(instr
), tabs
, fp
);
616 case nir_instr_type_jump
:
617 print_jump_instr(nir_instr_as_jump(instr
), fp
);
620 case nir_instr_type_ssa_undef
:
621 print_ssa_undef_instr(nir_instr_as_ssa_undef(instr
), fp
);
624 case nir_instr_type_phi
:
625 print_phi_instr(nir_instr_as_phi(instr
), fp
);
628 case nir_instr_type_parallel_copy
:
629 print_parallel_copy_instr(nir_instr_as_parallel_copy(instr
), fp
);
633 unreachable("Invalid instruction type");
641 compare_block_index(const void *p1
, const void *p2
)
643 const nir_block
*block1
= *((const nir_block
**) p1
);
644 const nir_block
*block2
= *((const nir_block
**) p2
);
646 return (int) block1
->index
- (int) block2
->index
;
649 static void print_cf_node(nir_cf_node
*node
, print_var_state
*state
,
650 unsigned tabs
, FILE *fp
);
653 print_block(nir_block
*block
, print_var_state
*state
, unsigned tabs
, FILE *fp
)
655 print_tabs(tabs
, fp
);
656 fprintf(fp
, "block block_%u:\n", block
->index
);
658 /* sort the predecessors by index so we consistently print the same thing */
661 malloc(block
->predecessors
->entries
* sizeof(nir_block
*));
663 struct set_entry
*entry
;
665 set_foreach(block
->predecessors
, entry
) {
666 preds
[i
++] = (nir_block
*) entry
->key
;
669 qsort(preds
, block
->predecessors
->entries
, sizeof(nir_block
*),
670 compare_block_index
);
672 print_tabs(tabs
, fp
);
673 fprintf(fp
, "/* preds: ");
674 for (unsigned i
= 0; i
< block
->predecessors
->entries
; i
++) {
675 fprintf(fp
, "block_%u ", preds
[i
]->index
);
681 nir_foreach_instr(block
, instr
) {
682 print_instr(instr
, state
, tabs
, fp
);
685 print_tabs(tabs
, fp
);
686 fprintf(fp
, "/* succs: ");
687 for (unsigned i
= 0; i
< 2; i
++)
688 if (block
->successors
[i
]) {
689 fprintf(fp
, "block_%u ", block
->successors
[i
]->index
);
695 print_if(nir_if
*if_stmt
, print_var_state
*state
, unsigned tabs
, FILE *fp
)
697 print_tabs(tabs
, fp
);
699 print_src(&if_stmt
->condition
, fp
);
701 foreach_list_typed(nir_cf_node
, node
, node
, &if_stmt
->then_list
) {
702 print_cf_node(node
, state
, tabs
+ 1, fp
);
704 print_tabs(tabs
, fp
);
705 fprintf(fp
, "} else {\n");
706 foreach_list_typed(nir_cf_node
, node
, node
, &if_stmt
->else_list
) {
707 print_cf_node(node
, state
, tabs
+ 1, fp
);
709 print_tabs(tabs
, fp
);
714 print_loop(nir_loop
*loop
, print_var_state
*state
, unsigned tabs
, FILE *fp
)
716 print_tabs(tabs
, fp
);
717 fprintf(fp
, "loop {\n");
718 foreach_list_typed(nir_cf_node
, node
, node
, &loop
->body
) {
719 print_cf_node(node
, state
, tabs
+ 1, fp
);
721 print_tabs(tabs
, fp
);
726 print_cf_node(nir_cf_node
*node
, print_var_state
*state
, unsigned int tabs
,
729 switch (node
->type
) {
730 case nir_cf_node_block
:
731 print_block(nir_cf_node_as_block(node
), state
, tabs
, fp
);
735 print_if(nir_cf_node_as_if(node
), state
, tabs
, fp
);
738 case nir_cf_node_loop
:
739 print_loop(nir_cf_node_as_loop(node
), state
, tabs
, fp
);
743 unreachable("Invalid CFG node type");
748 print_function_impl(nir_function_impl
*impl
, print_var_state
*state
, FILE *fp
)
750 fprintf(fp
, "\nimpl %s ", impl
->overload
->function
->name
);
752 for (unsigned i
= 0; i
< impl
->num_params
; i
++) {
756 print_var(impl
->params
[i
], state
, fp
);
759 if (impl
->return_var
!= NULL
) {
760 if (impl
->num_params
!= 0)
762 fprintf(fp
, "returning ");
763 print_var(impl
->return_var
, state
, fp
);
768 foreach_list_typed(nir_variable
, var
, node
, &impl
->locals
) {
770 print_var_decl(var
, state
, fp
);
773 foreach_list_typed(nir_register
, reg
, node
, &impl
->registers
) {
775 print_register_decl(reg
, fp
);
778 nir_index_blocks(impl
);
780 foreach_list_typed(nir_cf_node
, node
, node
, &impl
->body
) {
781 print_cf_node(node
, state
, 1, fp
);
784 fprintf(fp
, "\tblock block_%u:\n}\n\n", impl
->end_block
->index
);
788 print_function_overload(nir_function_overload
*overload
,
789 print_var_state
*state
, FILE *fp
)
791 fprintf(fp
, "decl_overload %s ", overload
->function
->name
);
793 for (unsigned i
= 0; i
< overload
->num_params
; i
++) {
797 switch (overload
->params
[i
].param_type
) {
798 case nir_parameter_in
:
801 case nir_parameter_out
:
804 case nir_parameter_inout
:
805 fprintf(fp
, "inout ");
808 unreachable("Invalid parameter type");
811 glsl_print_type(overload
->params
[i
].type
, fp
);
814 if (overload
->return_type
!= NULL
) {
815 if (overload
->num_params
!= 0)
817 fprintf(fp
, "returning ");
818 glsl_print_type(overload
->return_type
, fp
);
823 if (overload
->impl
!= NULL
) {
824 print_function_impl(overload
->impl
, state
, fp
);
830 print_function(nir_function
*func
, print_var_state
*state
, FILE *fp
)
832 foreach_list_typed(nir_function_overload
, overload
, node
, &func
->overload_list
) {
833 print_function_overload(overload
, state
, fp
);
838 init_print_state(print_var_state
*state
)
840 state
->ht
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
841 _mesa_key_pointer_equal
);
842 state
->syms
= _mesa_set_create(NULL
, _mesa_key_string_equal
);
847 destroy_print_state(print_var_state
*state
)
849 _mesa_hash_table_destroy(state
->ht
, NULL
);
850 _mesa_set_destroy(state
->syms
, NULL
);
854 nir_print_shader(nir_shader
*shader
, FILE *fp
)
856 print_var_state state
;
857 init_print_state(&state
);
859 for (unsigned i
= 0; i
< shader
->num_user_structures
; i
++) {
860 glsl_print_struct(shader
->user_structures
[i
], fp
);
863 struct hash_entry
*entry
;
865 hash_table_foreach(shader
->uniforms
, entry
) {
866 print_var_decl((nir_variable
*) entry
->data
, &state
, fp
);
869 hash_table_foreach(shader
->inputs
, entry
) {
870 print_var_decl((nir_variable
*) entry
->data
, &state
, fp
);
873 hash_table_foreach(shader
->outputs
, entry
) {
874 print_var_decl((nir_variable
*) entry
->data
, &state
, fp
);
877 foreach_list_typed(nir_variable
, var
, node
, &shader
->globals
) {
878 print_var_decl(var
, &state
, fp
);
881 foreach_list_typed(nir_variable
, var
, node
, &shader
->system_values
) {
882 print_var_decl(var
, &state
, fp
);
885 foreach_list_typed(nir_register
, reg
, node
, &shader
->registers
) {
886 print_register_decl(reg
, fp
);
889 foreach_list_typed(nir_function
, func
, node
, &shader
->functions
) {
890 print_function(func
, &state
, fp
);
893 destroy_print_state(&state
);