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 if (instr
->has_predicate
) {
183 print_src(&instr
->predicate
, fp
);
187 print_alu_dest(&instr
->dest
, fp
);
189 fprintf(fp
, " = %s", nir_op_infos
[instr
->op
].name
);
190 if (instr
->dest
.saturate
)
195 for (unsigned i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++) {
199 print_alu_src(&instr
->src
[i
], fp
);
206 print_var_decl(nir_variable
*var
, print_var_state
*state
, FILE *fp
)
208 fprintf(fp
, "decl_var ");
210 const char *const cent
= (var
->data
.centroid
) ? "centroid " : "";
211 const char *const samp
= (var
->data
.sample
) ? "sample " : "";
212 const char *const inv
= (var
->data
.invariant
) ? "invariant " : "";
213 const char *const mode
[] = { "shader_in ", "shader_out ", "", "",
214 "uniform ", "system " };
215 const char *const interp
[] = { "", "smooth", "flat", "noperspective" };
217 fprintf(fp
, "%s%s%s%s%s ",
218 cent
, samp
, inv
, mode
[var
->data
.mode
], interp
[var
->data
.interpolation
]);
220 glsl_print_type(var
->type
, fp
);
222 struct set_entry
*entry
=
223 _mesa_set_search(state
->syms
, _mesa_hash_string(var
->name
), var
->name
);
228 /* we have a collision with another name, append an @ + a unique index */
229 name
= ralloc_asprintf(state
->syms
, "%s@%u", var
->name
, state
->index
++);
234 fprintf(fp
, " %s", name
);
236 if (var
->data
.mode
== nir_var_shader_in
||
237 var
->data
.mode
== nir_var_shader_out
||
238 var
->data
.mode
== nir_var_uniform
) {
239 fprintf(fp
, " (%u)", var
->data
.driver_location
);
244 _mesa_set_add(state
->syms
, _mesa_hash_string(name
), name
);
245 _mesa_hash_table_insert(state
->ht
, var
, name
);
249 print_var(nir_variable
*var
, print_var_state
*state
, FILE *fp
)
251 struct hash_entry
*entry
= _mesa_hash_table_search(state
->ht
, var
);
253 assert(entry
!= NULL
);
255 fprintf(fp
, "%s", (char *) entry
->data
);
259 print_deref_var(nir_deref_var
*deref
, print_var_state
*state
, FILE *fp
)
261 print_var(deref
->var
, state
, fp
);
265 print_deref_array(nir_deref_array
*deref
, print_var_state
*state
, FILE *fp
)
268 if (!deref
->has_indirect
|| deref
->base_offset
!= 0)
269 fprintf(fp
, "%u", deref
->base_offset
);
270 if (deref
->has_indirect
) {
271 if (deref
->base_offset
!= 0)
273 print_src(&deref
->indirect
, fp
);
279 print_deref_struct(nir_deref_struct
*deref
, print_var_state
*state
, FILE *fp
)
281 fprintf(fp
, ".%s", deref
->elem
);
285 print_deref(nir_deref
*deref
, print_var_state
*state
, FILE *fp
)
287 while (deref
!= NULL
) {
288 switch (deref
->deref_type
) {
289 case nir_deref_type_var
:
290 print_deref_var(nir_deref_as_var(deref
), state
, fp
);
293 case nir_deref_type_array
:
294 print_deref_array(nir_deref_as_array(deref
), state
, fp
);
297 case nir_deref_type_struct
:
298 print_deref_struct(nir_deref_as_struct(deref
), state
, fp
);
302 unreachable("Invalid deref type");
305 deref
= deref
->child
;
310 print_intrinsic_instr(nir_intrinsic_instr
*instr
, print_var_state
*state
,
313 unsigned num_srcs
= nir_intrinsic_infos
[instr
->intrinsic
].num_srcs
;
315 if (instr
->has_predicate
) {
317 print_src(&instr
->predicate
, fp
);
321 if (nir_intrinsic_infos
[instr
->intrinsic
].has_dest
) {
322 print_dest(&instr
->dest
, fp
);
326 fprintf(fp
, "intrinsic %s (", nir_intrinsic_infos
[instr
->intrinsic
].name
);
329 for (unsigned i
= 0; i
< num_srcs
; i
++) {
333 print_src(&instr
->src
[i
], fp
);
340 unsigned num_vars
= nir_intrinsic_infos
[instr
->intrinsic
].num_variables
;
343 for (unsigned i
= 0; i
< num_vars
; i
++) {
347 print_deref(&instr
->variables
[i
]->deref
, state
, fp
);
354 unsigned num_indices
= nir_intrinsic_infos
[instr
->intrinsic
].num_indices
;
357 for (unsigned i
= 0; i
< num_indices
; i
++) {
361 fprintf(fp
, "%u", instr
->const_index
[i
]);
370 print_tex_instr(nir_tex_instr
*instr
, print_var_state
*state
, FILE *fp
)
372 if (instr
->has_predicate
) {
374 print_src(&instr
->predicate
, fp
);
378 print_dest(&instr
->dest
, fp
);
398 case nir_texop_txf_ms
:
399 fprintf(fp
, "txf_ms ");
410 case nir_texop_query_levels
:
411 fprintf(fp
, "query_levels ");
415 unreachable("Invalid texture operation");
419 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++) {
420 print_src(&instr
->src
[i
], fp
);
424 switch(instr
->src_type
[i
]) {
425 case nir_tex_src_coord
:
426 fprintf(fp
, "(coord)");
428 case nir_tex_src_projector
:
429 fprintf(fp
, "(projector)");
431 case nir_tex_src_comparitor
:
432 fprintf(fp
, "(comparitor)");
434 case nir_tex_src_offset
:
435 fprintf(fp
, "(offset)");
437 case nir_tex_src_bias
:
438 fprintf(fp
, "(bias)");
440 case nir_tex_src_lod
:
441 fprintf(fp
, "(lod)");
443 case nir_tex_src_ms_index
:
444 fprintf(fp
, "(ms_index)");
446 case nir_tex_src_ddx
:
447 fprintf(fp
, "(ddx)");
449 case nir_tex_src_ddy
:
450 fprintf(fp
, "(ddy)");
452 case nir_tex_src_sampler_index
:
453 fprintf(fp
, "(sampler_index)");
457 unreachable("Invalid texture source type");
464 bool offset_nonzero
= false;
465 for (unsigned i
= 0; i
< 4; i
++)
466 if (instr
->const_offset
[i
] != 0) {
467 offset_nonzero
= true;
471 if (offset_nonzero
) {
472 fprintf(fp
, "[%i %i %i %i] (offset), ",
473 instr
->const_offset
[0], instr
->const_offset
[1],
474 instr
->const_offset
[2], instr
->const_offset
[3]);
477 if (instr
->op
== nir_texop_tg4
) {
478 fprintf(fp
, "%u (gather_component), ", instr
->component
);
481 if (instr
->sampler
) {
482 print_deref(&instr
->sampler
->deref
, state
, fp
);
484 fprintf(fp
, "%u", instr
->sampler_index
);
487 fprintf(fp
, " (sampler)");
491 print_call_instr(nir_call_instr
*instr
, print_var_state
*state
, FILE *fp
)
493 if (instr
->has_predicate
) {
495 print_src(&instr
->predicate
, fp
);
499 fprintf(fp
, "call %s ", instr
->callee
->function
->name
);
501 for (unsigned i
= 0; i
< instr
->num_params
; i
++) {
505 print_deref(&instr
->params
[i
]->deref
, state
, fp
);
508 if (instr
->return_deref
!= NULL
) {
509 if (instr
->num_params
!= 0)
511 fprintf(fp
, "returning ");
512 print_deref(&instr
->return_deref
->deref
, state
, fp
);
517 print_const_value(nir_const_value value
, unsigned num_components
, FILE *fp
)
522 for (unsigned i
= 0; i
< num_components
; i
++) {
527 * we don't really know the type of the constant (if it will be used as a
528 * float or an int), so just print the raw constant in hex for fidelity
529 * and then print the float in a comment for readability.
532 fprintf(fp
, "0x%08x /* %f */", value
.u
[i
], value
.f
[i
]);
541 print_load_const_instr(nir_load_const_instr
*instr
, unsigned tabs
, FILE *fp
)
543 if (instr
->has_predicate
) {
545 print_src(&instr
->predicate
, fp
);
549 print_dest(&instr
->dest
, fp
);
551 fprintf(fp
, " = load_const ");
553 if (instr
->array_elems
== 0) {
554 print_const_value(instr
->value
, instr
->num_components
, fp
);
557 for (unsigned i
= 0; i
< instr
->array_elems
; i
++) {
558 print_tabs(tabs
+ 1, fp
);
559 print_const_value(instr
->array
[i
], instr
->num_components
, fp
);
567 print_jump_instr(nir_jump_instr
*instr
, FILE *fp
)
569 switch (instr
->type
) {
571 fprintf(fp
, "break");
574 case nir_jump_continue
:
575 fprintf(fp
, "continue");
578 case nir_jump_return
:
579 fprintf(fp
, "return");
585 print_ssa_undef_instr(nir_ssa_undef_instr
* instr
, FILE *fp
)
587 print_ssa_def(&instr
->def
, fp
);
588 fprintf(fp
, " = undefined");
592 print_phi_instr(nir_phi_instr
*instr
, FILE *fp
)
594 print_dest(&instr
->dest
, fp
);
595 fprintf(fp
, " = phi ");
597 foreach_list_typed(nir_phi_src
, src
, node
, &instr
->srcs
) {
601 fprintf(fp
, "block_%u: ", src
->pred
->index
);
602 print_src(&src
->src
, fp
);
609 print_instr(nir_instr
*instr
, print_var_state
*state
, unsigned tabs
, FILE *fp
)
611 print_tabs(tabs
, fp
);
613 switch (instr
->type
) {
614 case nir_instr_type_alu
:
615 print_alu_instr(nir_instr_as_alu(instr
), fp
);
618 case nir_instr_type_call
:
619 print_call_instr(nir_instr_as_call(instr
), state
, fp
);
622 case nir_instr_type_intrinsic
:
623 print_intrinsic_instr(nir_instr_as_intrinsic(instr
), state
, fp
);
626 case nir_instr_type_texture
:
627 print_tex_instr(nir_instr_as_texture(instr
), state
, fp
);
630 case nir_instr_type_load_const
:
631 print_load_const_instr(nir_instr_as_load_const(instr
), tabs
, fp
);
634 case nir_instr_type_jump
:
635 print_jump_instr(nir_instr_as_jump(instr
), fp
);
638 case nir_instr_type_ssa_undef
:
639 print_ssa_undef_instr(nir_instr_as_ssa_undef(instr
), fp
);
642 case nir_instr_type_phi
:
643 print_phi_instr(nir_instr_as_phi(instr
), fp
);
647 unreachable("Invalid instruction type");
655 compare_block_index(const void *p1
, const void *p2
)
657 const nir_block
*block1
= *((const nir_block
**) p1
);
658 const nir_block
*block2
= *((const nir_block
**) p2
);
660 return (int) block1
->index
- (int) block2
->index
;
663 static void print_cf_node(nir_cf_node
*node
, print_var_state
*state
,
664 unsigned tabs
, FILE *fp
);
667 print_block(nir_block
*block
, print_var_state
*state
, unsigned tabs
, FILE *fp
)
669 print_tabs(tabs
, fp
);
670 fprintf(fp
, "block block_%u:\n", block
->index
);
672 /* sort the predecessors by index so we consistently print the same thing */
675 malloc(block
->predecessors
->entries
* sizeof(nir_block
*));
677 struct set_entry
*entry
;
679 set_foreach(block
->predecessors
, entry
) {
680 preds
[i
++] = (nir_block
*) entry
->key
;
683 qsort(preds
, block
->predecessors
->entries
, sizeof(nir_block
*),
684 compare_block_index
);
686 print_tabs(tabs
, fp
);
687 fprintf(fp
, "/* preds: ");
688 for (unsigned i
= 0; i
< block
->predecessors
->entries
; i
++) {
689 fprintf(fp
, "block_%u ", preds
[i
]->index
);
695 nir_foreach_instr(block
, instr
) {
696 print_instr(instr
, state
, tabs
, fp
);
699 print_tabs(tabs
, fp
);
700 fprintf(fp
, "/* succs: ");
701 for (unsigned i
= 0; i
< 2; i
++)
702 if (block
->successors
[i
]) {
703 fprintf(fp
, "block_%u ", block
->successors
[i
]->index
);
709 print_if(nir_if
*if_stmt
, print_var_state
*state
, unsigned tabs
, FILE *fp
)
711 print_tabs(tabs
, fp
);
713 print_src(&if_stmt
->condition
, fp
);
715 foreach_list_typed(nir_cf_node
, node
, node
, &if_stmt
->then_list
) {
716 print_cf_node(node
, state
, tabs
+ 1, fp
);
718 print_tabs(tabs
, fp
);
719 fprintf(fp
, "} else {\n");
720 foreach_list_typed(nir_cf_node
, node
, node
, &if_stmt
->else_list
) {
721 print_cf_node(node
, state
, tabs
+ 1, fp
);
723 print_tabs(tabs
, fp
);
728 print_loop(nir_loop
*loop
, print_var_state
*state
, unsigned tabs
, FILE *fp
)
730 print_tabs(tabs
, fp
);
731 fprintf(fp
, "loop {\n");
732 foreach_list_typed(nir_cf_node
, node
, node
, &loop
->body
) {
733 print_cf_node(node
, state
, tabs
+ 1, fp
);
735 print_tabs(tabs
, fp
);
740 print_cf_node(nir_cf_node
*node
, print_var_state
*state
, unsigned int tabs
,
743 switch (node
->type
) {
744 case nir_cf_node_block
:
745 print_block(nir_cf_node_as_block(node
), state
, tabs
, fp
);
749 print_if(nir_cf_node_as_if(node
), state
, tabs
, fp
);
752 case nir_cf_node_loop
:
753 print_loop(nir_cf_node_as_loop(node
), state
, tabs
, fp
);
757 unreachable("Invalid CFG node type");
762 print_function_impl(nir_function_impl
*impl
, print_var_state
*state
, FILE *fp
)
764 fprintf(fp
, "\nimpl %s ", impl
->overload
->function
->name
);
766 for (unsigned i
= 0; i
< impl
->num_params
; i
++) {
770 print_var(impl
->params
[i
], state
, fp
);
773 if (impl
->return_var
!= NULL
) {
774 if (impl
->num_params
!= 0)
776 fprintf(fp
, "returning ");
777 print_var(impl
->return_var
, state
, fp
);
782 foreach_list_typed(nir_variable
, var
, node
, &impl
->locals
) {
784 print_var_decl(var
, state
, fp
);
787 nir_index_local_regs(impl
);
789 foreach_list_typed(nir_register
, reg
, node
, &impl
->registers
) {
791 print_register_decl(reg
, fp
);
794 nir_index_blocks(impl
);
796 foreach_list_typed(nir_cf_node
, node
, node
, &impl
->body
) {
797 print_cf_node(node
, state
, 1, fp
);
800 fprintf(fp
, "\tblock block_%u:\n}\n\n", impl
->end_block
->index
);
804 print_function_overload(nir_function_overload
*overload
,
805 print_var_state
*state
, FILE *fp
)
807 fprintf(fp
, "decl_overload %s ", overload
->function
->name
);
809 for (unsigned i
= 0; i
< overload
->num_params
; i
++) {
813 switch (overload
->params
[i
].param_type
) {
814 case nir_parameter_in
:
817 case nir_parameter_out
:
820 case nir_parameter_inout
:
821 fprintf(fp
, "inout ");
824 unreachable("Invalid parameter type");
827 glsl_print_type(overload
->params
[i
].type
, fp
);
830 if (overload
->return_type
!= NULL
) {
831 if (overload
->num_params
!= 0)
833 fprintf(fp
, "returning ");
834 glsl_print_type(overload
->return_type
, fp
);
839 if (overload
->impl
!= NULL
) {
840 print_function_impl(overload
->impl
, state
, fp
);
846 print_function(nir_function
*func
, print_var_state
*state
, FILE *fp
)
848 foreach_list_typed(nir_function_overload
, overload
, node
, &func
->overload_list
) {
849 print_function_overload(overload
, state
, fp
);
854 init_print_state(print_var_state
*state
)
856 state
->ht
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
857 _mesa_key_pointer_equal
);
858 state
->syms
= _mesa_set_create(NULL
, _mesa_key_string_equal
);
863 destroy_print_state(print_var_state
*state
)
865 _mesa_hash_table_destroy(state
->ht
, NULL
);
866 _mesa_set_destroy(state
->syms
, NULL
);
870 nir_print_shader(nir_shader
*shader
, FILE *fp
)
872 print_var_state state
;
873 init_print_state(&state
);
875 for (unsigned i
= 0; i
< shader
->num_user_structures
; i
++) {
876 glsl_print_struct(shader
->user_structures
[i
], fp
);
879 struct hash_entry
*entry
;
881 hash_table_foreach(shader
->uniforms
, entry
) {
882 print_var_decl((nir_variable
*) entry
->data
, &state
, fp
);
885 hash_table_foreach(shader
->inputs
, entry
) {
886 print_var_decl((nir_variable
*) entry
->data
, &state
, fp
);
889 hash_table_foreach(shader
->outputs
, entry
) {
890 print_var_decl((nir_variable
*) entry
->data
, &state
, fp
);
893 foreach_list_typed(nir_variable
, var
, node
, &shader
->globals
) {
894 print_var_decl(var
, &state
, fp
);
897 foreach_list_typed(nir_variable
, var
, node
, &shader
->system_values
) {
898 print_var_decl(var
, &state
, fp
);
901 nir_index_global_regs(shader
);
903 foreach_list_typed(nir_register
, reg
, node
, &shader
->registers
) {
904 print_register_decl(reg
, fp
);
907 foreach_list_typed(nir_function
, func
, node
, &shader
->functions
) {
908 print_function(func
, &state
, fp
);
911 destroy_print_state(&state
);