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_instr
*instr
, unsigned src
, FILE *fp
)
142 if (instr
->src
[src
].negate
)
144 if (instr
->src
[src
].abs
)
147 print_src(&instr
->src
[src
].src
, fp
);
149 bool print_swizzle
= false;
150 for (unsigned i
= 0; i
< 4; i
++) {
151 if (!nir_alu_instr_channel_used(instr
, src
, i
))
154 if (instr
->src
[src
].swizzle
[i
] != i
) {
155 print_swizzle
= true;
162 for (unsigned i
= 0; i
< 4; i
++) {
163 if (!nir_alu_instr_channel_used(instr
, src
, i
))
166 fprintf(fp
, "%c", "xyzw"[instr
->src
[src
].swizzle
[i
]]);
170 if (instr
->src
[src
].abs
)
175 print_alu_dest(nir_alu_dest
*dest
, FILE *fp
)
177 /* we're going to print the saturate modifier later, after the opcode */
179 print_dest(&dest
->dest
, fp
);
181 if (!dest
->dest
.is_ssa
&&
182 dest
->write_mask
!= (1 << dest
->dest
.reg
.reg
->num_components
) - 1) {
184 for (unsigned i
= 0; i
< 4; i
++)
185 if ((dest
->write_mask
>> i
) & 1)
186 fprintf(fp
, "%c", "xyzw"[i
]);
191 print_alu_instr(nir_alu_instr
*instr
, FILE *fp
)
193 print_alu_dest(&instr
->dest
, fp
);
195 fprintf(fp
, " = %s", nir_op_infos
[instr
->op
].name
);
196 if (instr
->dest
.saturate
)
200 for (unsigned i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++) {
204 print_alu_src(instr
, i
, fp
);
209 print_var_decl(nir_variable
*var
, print_var_state
*state
, FILE *fp
)
211 fprintf(fp
, "decl_var ");
213 const char *const cent
= (var
->data
.centroid
) ? "centroid " : "";
214 const char *const samp
= (var
->data
.sample
) ? "sample " : "";
215 const char *const inv
= (var
->data
.invariant
) ? "invariant " : "";
216 const char *const mode
[] = { "shader_in ", "shader_out ", "", "",
217 "uniform ", "system " };
218 const char *const interp
[] = { "", "smooth", "flat", "noperspective" };
220 fprintf(fp
, "%s%s%s%s%s ",
221 cent
, samp
, inv
, mode
[var
->data
.mode
], interp
[var
->data
.interpolation
]);
223 glsl_print_type(var
->type
, fp
);
225 struct set_entry
*entry
= NULL
;
227 entry
= _mesa_set_search(state
->syms
, var
->name
);
232 /* we have a collision with another name, append an @ + a unique index */
233 name
= ralloc_asprintf(state
->syms
, "%s@%u", var
->name
, state
->index
++);
238 fprintf(fp
, " %s", name
);
240 if (var
->data
.mode
== nir_var_shader_in
||
241 var
->data
.mode
== nir_var_shader_out
||
242 var
->data
.mode
== nir_var_uniform
) {
243 fprintf(fp
, " (%u, %u)", var
->data
.location
, var
->data
.driver_location
);
249 _mesa_set_add(state
->syms
, name
);
250 _mesa_hash_table_insert(state
->ht
, var
, name
);
255 print_var(nir_variable
*var
, print_var_state
*state
, FILE *fp
)
259 struct hash_entry
*entry
= _mesa_hash_table_search(state
->ht
, var
);
261 assert(entry
!= NULL
);
267 fprintf(fp
, "%s", name
);
271 print_deref_var(nir_deref_var
*deref
, print_var_state
*state
, FILE *fp
)
273 print_var(deref
->var
, state
, fp
);
277 print_deref_array(nir_deref_array
*deref
, print_var_state
*state
, FILE *fp
)
280 switch (deref
->deref_array_type
) {
281 case nir_deref_array_type_direct
:
282 fprintf(fp
, "%u", deref
->base_offset
);
284 case nir_deref_array_type_indirect
:
285 if (deref
->base_offset
!= 0)
286 fprintf(fp
, "%u + ", deref
->base_offset
);
287 print_src(&deref
->indirect
, fp
);
289 case nir_deref_array_type_wildcard
:
297 print_deref_struct(nir_deref_struct
*deref
, const struct glsl_type
*parent_type
,
298 print_var_state
*state
, FILE *fp
)
300 fprintf(fp
, ".%s", glsl_get_struct_elem_name(parent_type
, deref
->index
));
304 print_deref(nir_deref_var
*deref
, print_var_state
*state
, FILE *fp
)
306 nir_deref
*tail
= &deref
->deref
;
307 nir_deref
*pretail
= NULL
;
308 while (tail
!= NULL
) {
309 switch (tail
->deref_type
) {
310 case nir_deref_type_var
:
311 assert(pretail
== NULL
);
312 assert(tail
== &deref
->deref
);
313 print_deref_var(deref
, state
, fp
);
316 case nir_deref_type_array
:
317 assert(pretail
!= NULL
);
318 print_deref_array(nir_deref_as_array(tail
), state
, fp
);
321 case nir_deref_type_struct
:
322 assert(pretail
!= NULL
);
323 print_deref_struct(nir_deref_as_struct(tail
),
324 pretail
->type
, state
, fp
);
328 unreachable("Invalid deref type");
332 tail
= pretail
->child
;
337 print_intrinsic_instr(nir_intrinsic_instr
*instr
, print_var_state
*state
,
340 unsigned num_srcs
= nir_intrinsic_infos
[instr
->intrinsic
].num_srcs
;
342 if (nir_intrinsic_infos
[instr
->intrinsic
].has_dest
) {
343 print_dest(&instr
->dest
, fp
);
347 fprintf(fp
, "intrinsic %s (", nir_intrinsic_infos
[instr
->intrinsic
].name
);
349 for (unsigned i
= 0; i
< num_srcs
; i
++) {
353 print_src(&instr
->src
[i
], fp
);
358 unsigned num_vars
= nir_intrinsic_infos
[instr
->intrinsic
].num_variables
;
360 for (unsigned i
= 0; i
< num_vars
; i
++) {
364 print_deref(instr
->variables
[i
], state
, fp
);
369 unsigned num_indices
= nir_intrinsic_infos
[instr
->intrinsic
].num_indices
;
371 for (unsigned i
= 0; i
< num_indices
; i
++) {
375 fprintf(fp
, "%u", instr
->const_index
[i
]);
382 print_tex_instr(nir_tex_instr
*instr
, print_var_state
*state
, FILE *fp
)
384 print_dest(&instr
->dest
, fp
);
404 case nir_texop_txf_ms
:
405 fprintf(fp
, "txf_ms ");
416 case nir_texop_query_levels
:
417 fprintf(fp
, "query_levels ");
421 unreachable("Invalid texture operation");
425 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++) {
426 print_src(&instr
->src
[i
].src
, fp
);
430 switch(instr
->src
[i
].src_type
) {
431 case nir_tex_src_coord
:
432 fprintf(fp
, "(coord)");
434 case nir_tex_src_projector
:
435 fprintf(fp
, "(projector)");
437 case nir_tex_src_comparitor
:
438 fprintf(fp
, "(comparitor)");
440 case nir_tex_src_offset
:
441 fprintf(fp
, "(offset)");
443 case nir_tex_src_bias
:
444 fprintf(fp
, "(bias)");
446 case nir_tex_src_lod
:
447 fprintf(fp
, "(lod)");
449 case nir_tex_src_ms_index
:
450 fprintf(fp
, "(ms_index)");
452 case nir_tex_src_ddx
:
453 fprintf(fp
, "(ddx)");
455 case nir_tex_src_ddy
:
456 fprintf(fp
, "(ddy)");
458 case nir_tex_src_sampler_offset
:
459 fprintf(fp
, "(sampler_offset)");
463 unreachable("Invalid texture source type");
470 bool has_nonzero_offset
= false;
471 for (unsigned i
= 0; i
< 4; i
++) {
472 if (instr
->const_offset
[i
] != 0) {
473 has_nonzero_offset
= true;
478 if (has_nonzero_offset
) {
479 fprintf(fp
, "[%i %i %i %i] (offset), ",
480 instr
->const_offset
[0], instr
->const_offset
[1],
481 instr
->const_offset
[2], instr
->const_offset
[3]);
484 if (instr
->op
== nir_texop_tg4
) {
485 fprintf(fp
, "%u (gather_component), ", instr
->component
);
488 if (instr
->sampler
) {
489 print_deref(instr
->sampler
, state
, fp
);
491 fprintf(fp
, "%u", instr
->sampler_index
);
494 fprintf(fp
, " (sampler)");
498 print_call_instr(nir_call_instr
*instr
, print_var_state
*state
, FILE *fp
)
500 fprintf(fp
, "call %s ", instr
->callee
->function
->name
);
502 for (unsigned i
= 0; i
< instr
->num_params
; i
++) {
506 print_deref(instr
->params
[i
], state
, fp
);
509 if (instr
->return_deref
!= NULL
) {
510 if (instr
->num_params
!= 0)
512 fprintf(fp
, "returning ");
513 print_deref(instr
->return_deref
, state
, fp
);
518 print_load_const_instr(nir_load_const_instr
*instr
, unsigned tabs
, FILE *fp
)
520 print_ssa_def(&instr
->def
, fp
);
522 fprintf(fp
, " = load_const (");
524 for (unsigned i
= 0; i
< instr
->def
.num_components
; i
++) {
529 * we don't really know the type of the constant (if it will be used as a
530 * float or an int), so just print the raw constant in hex for fidelity
531 * and then print the float in a comment for readability.
534 fprintf(fp
, "0x%08x /* %f */", instr
->value
.u
[i
], instr
->value
.f
[i
]);
541 print_jump_instr(nir_jump_instr
*instr
, FILE *fp
)
543 switch (instr
->type
) {
545 fprintf(fp
, "break");
548 case nir_jump_continue
:
549 fprintf(fp
, "continue");
552 case nir_jump_return
:
553 fprintf(fp
, "return");
559 print_ssa_undef_instr(nir_ssa_undef_instr
* instr
, FILE *fp
)
561 print_ssa_def(&instr
->def
, fp
);
562 fprintf(fp
, " = undefined");
566 print_phi_instr(nir_phi_instr
*instr
, FILE *fp
)
568 print_dest(&instr
->dest
, fp
);
569 fprintf(fp
, " = phi ");
570 nir_foreach_phi_src(instr
, src
) {
571 if (&src
->node
!= exec_list_get_head(&instr
->srcs
))
574 fprintf(fp
, "block_%u: ", src
->pred
->index
);
575 print_src(&src
->src
, fp
);
580 print_parallel_copy_instr(nir_parallel_copy_instr
*instr
, FILE *fp
)
582 nir_foreach_parallel_copy_entry(instr
, entry
) {
583 if (&entry
->node
!= exec_list_get_head(&instr
->entries
))
586 print_dest(&entry
->dest
, fp
);
588 print_src(&entry
->src
, fp
);
593 print_instr(const nir_instr
*instr
, print_var_state
*state
, unsigned tabs
, FILE *fp
)
595 print_tabs(tabs
, fp
);
597 switch (instr
->type
) {
598 case nir_instr_type_alu
:
599 print_alu_instr(nir_instr_as_alu(instr
), fp
);
602 case nir_instr_type_call
:
603 print_call_instr(nir_instr_as_call(instr
), state
, fp
);
606 case nir_instr_type_intrinsic
:
607 print_intrinsic_instr(nir_instr_as_intrinsic(instr
), state
, fp
);
610 case nir_instr_type_tex
:
611 print_tex_instr(nir_instr_as_tex(instr
), state
, fp
);
614 case nir_instr_type_load_const
:
615 print_load_const_instr(nir_instr_as_load_const(instr
), tabs
, fp
);
618 case nir_instr_type_jump
:
619 print_jump_instr(nir_instr_as_jump(instr
), fp
);
622 case nir_instr_type_ssa_undef
:
623 print_ssa_undef_instr(nir_instr_as_ssa_undef(instr
), fp
);
626 case nir_instr_type_phi
:
627 print_phi_instr(nir_instr_as_phi(instr
), fp
);
630 case nir_instr_type_parallel_copy
:
631 print_parallel_copy_instr(nir_instr_as_parallel_copy(instr
), fp
);
635 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
);
686 print_tabs(tabs
, fp
);
687 fprintf(fp
, "/* succs: ");
688 for (unsigned i
= 0; i
< 2; i
++)
689 if (block
->successors
[i
]) {
690 fprintf(fp
, "block_%u ", block
->successors
[i
]->index
);
696 print_if(nir_if
*if_stmt
, print_var_state
*state
, unsigned tabs
, FILE *fp
)
698 print_tabs(tabs
, fp
);
700 print_src(&if_stmt
->condition
, fp
);
702 foreach_list_typed(nir_cf_node
, node
, node
, &if_stmt
->then_list
) {
703 print_cf_node(node
, state
, tabs
+ 1, fp
);
705 print_tabs(tabs
, fp
);
706 fprintf(fp
, "} else {\n");
707 foreach_list_typed(nir_cf_node
, node
, node
, &if_stmt
->else_list
) {
708 print_cf_node(node
, state
, tabs
+ 1, fp
);
710 print_tabs(tabs
, fp
);
715 print_loop(nir_loop
*loop
, print_var_state
*state
, unsigned tabs
, FILE *fp
)
717 print_tabs(tabs
, fp
);
718 fprintf(fp
, "loop {\n");
719 foreach_list_typed(nir_cf_node
, node
, node
, &loop
->body
) {
720 print_cf_node(node
, state
, tabs
+ 1, fp
);
722 print_tabs(tabs
, fp
);
727 print_cf_node(nir_cf_node
*node
, print_var_state
*state
, unsigned int tabs
,
730 switch (node
->type
) {
731 case nir_cf_node_block
:
732 print_block(nir_cf_node_as_block(node
), state
, tabs
, fp
);
736 print_if(nir_cf_node_as_if(node
), state
, tabs
, fp
);
739 case nir_cf_node_loop
:
740 print_loop(nir_cf_node_as_loop(node
), state
, tabs
, fp
);
744 unreachable("Invalid CFG node type");
749 print_function_impl(nir_function_impl
*impl
, print_var_state
*state
, FILE *fp
)
751 fprintf(fp
, "\nimpl %s ", impl
->overload
->function
->name
);
753 for (unsigned i
= 0; i
< impl
->num_params
; i
++) {
757 print_var(impl
->params
[i
], state
, fp
);
760 if (impl
->return_var
!= NULL
) {
761 if (impl
->num_params
!= 0)
763 fprintf(fp
, "returning ");
764 print_var(impl
->return_var
, state
, fp
);
769 foreach_list_typed(nir_variable
, var
, node
, &impl
->locals
) {
771 print_var_decl(var
, state
, fp
);
774 foreach_list_typed(nir_register
, reg
, node
, &impl
->registers
) {
776 print_register_decl(reg
, fp
);
779 nir_index_blocks(impl
);
781 foreach_list_typed(nir_cf_node
, node
, node
, &impl
->body
) {
782 print_cf_node(node
, state
, 1, fp
);
785 fprintf(fp
, "\tblock block_%u:\n}\n\n", impl
->end_block
->index
);
789 print_function_overload(nir_function_overload
*overload
,
790 print_var_state
*state
, FILE *fp
)
792 fprintf(fp
, "decl_overload %s ", overload
->function
->name
);
794 for (unsigned i
= 0; i
< overload
->num_params
; i
++) {
798 switch (overload
->params
[i
].param_type
) {
799 case nir_parameter_in
:
802 case nir_parameter_out
:
805 case nir_parameter_inout
:
806 fprintf(fp
, "inout ");
809 unreachable("Invalid parameter type");
812 glsl_print_type(overload
->params
[i
].type
, fp
);
815 if (overload
->return_type
!= NULL
) {
816 if (overload
->num_params
!= 0)
818 fprintf(fp
, "returning ");
819 glsl_print_type(overload
->return_type
, fp
);
824 if (overload
->impl
!= NULL
) {
825 print_function_impl(overload
->impl
, state
, fp
);
831 print_function(nir_function
*func
, print_var_state
*state
, FILE *fp
)
833 foreach_list_typed(nir_function_overload
, overload
, node
, &func
->overload_list
) {
834 print_function_overload(overload
, state
, fp
);
839 init_print_state(print_var_state
*state
)
841 state
->ht
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
842 _mesa_key_pointer_equal
);
843 state
->syms
= _mesa_set_create(NULL
, _mesa_key_hash_string
,
844 _mesa_key_string_equal
);
849 destroy_print_state(print_var_state
*state
)
851 _mesa_hash_table_destroy(state
->ht
, NULL
);
852 _mesa_set_destroy(state
->syms
, NULL
);
856 nir_print_shader(nir_shader
*shader
, FILE *fp
)
858 print_var_state state
;
859 init_print_state(&state
);
861 foreach_list_typed(nir_variable
, var
, node
, &shader
->uniforms
) {
862 print_var_decl(var
, &state
, fp
);
865 foreach_list_typed(nir_variable
, var
, node
, &shader
->inputs
) {
866 print_var_decl(var
, &state
, fp
);
869 foreach_list_typed(nir_variable
, var
, node
, &shader
->outputs
) {
870 print_var_decl(var
, &state
, fp
);
873 foreach_list_typed(nir_variable
, var
, node
, &shader
->globals
) {
874 print_var_decl(var
, &state
, fp
);
877 foreach_list_typed(nir_variable
, var
, node
, &shader
->system_values
) {
878 print_var_decl(var
, &state
, fp
);
881 foreach_list_typed(nir_register
, reg
, node
, &shader
->registers
) {
882 print_register_decl(reg
, fp
);
885 foreach_list_typed(nir_function
, func
, node
, &shader
->functions
) {
886 print_function(func
, &state
, fp
);
889 destroy_print_state(&state
);
893 nir_print_instr(const nir_instr
*instr
, FILE *fp
)
895 print_instr(instr
, NULL
, 0, fp
);