2 * Copyright © 2015 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
25 #include "nir_builder.h"
26 #include "nir_control_flow.h"
28 struct inline_functions_state
{
29 nir_function_impl
*impl
;
35 inline_functions_block(nir_block
*block
, void *void_state
)
37 struct inline_functions_state
*state
= void_state
;
39 nir_builder
*b
= &state
->builder
;
41 /* This is tricky. We're iterating over instructions in a block but, as
42 * we go, the block and its instruction list are being split into
43 * pieces. However, this *should* be safe since foreach_safe always
44 * stashes the next thing in the iteration. That next thing will
45 * properly get moved to the next block when it gets split, and we
46 * continue iterating there.
48 nir_foreach_instr_safe(block
, instr
) {
49 if (instr
->type
!= nir_instr_type_call
)
52 state
->progress
= true;
54 nir_call_instr
*call
= nir_instr_as_call(instr
);
55 assert(call
->callee
->impl
);
57 nir_function_impl
*callee_copy
=
58 nir_function_impl_clone(call
->callee
->impl
);
60 exec_list_append(&state
->impl
->locals
, &callee_copy
->locals
);
61 exec_list_append(&state
->impl
->registers
, &callee_copy
->registers
);
63 b
->cursor
= nir_before_instr(&call
->instr
);
65 /* Add copies of all in parameters */
66 assert(call
->num_params
== callee_copy
->num_params
);
67 for (unsigned i
= 0; i
< callee_copy
->num_params
; i
++) {
68 /* Only in or inout parameters */
69 if (call
->callee
->params
[i
].param_type
== nir_parameter_out
)
72 nir_copy_deref_var(b
, nir_deref_var_create(b
->shader
,
73 callee_copy
->params
[i
]),
77 /* Pluck the body out of the function and place it here */
79 nir_cf_list_extract(&body
, &callee_copy
->body
);
80 nir_cf_reinsert(&body
, b
->cursor
);
82 b
->cursor
= nir_before_instr(&call
->instr
);
84 /* Add copies of all out parameters and the return */
85 assert(call
->num_params
== callee_copy
->num_params
);
86 for (unsigned i
= 0; i
< callee_copy
->num_params
; i
++) {
87 /* Only out or inout parameters */
88 if (call
->callee
->params
[i
].param_type
== nir_parameter_in
)
91 nir_copy_deref_var(b
, call
->params
[i
],
92 nir_deref_var_create(b
->shader
,
93 callee_copy
->params
[i
]));
95 if (!glsl_type_is_void(call
->callee
->return_type
)) {
96 nir_copy_deref_var(b
, call
->return_deref
,
97 nir_deref_var_create(b
->shader
,
98 callee_copy
->return_var
));
101 nir_instr_remove(&call
->instr
);
108 nir_inline_functions_impl(nir_function_impl
*impl
)
110 struct inline_functions_state state
;
112 state
.progress
= false;
114 nir_builder_init(&state
.builder
, impl
);
116 nir_foreach_block(impl
, inline_functions_block
, &state
);
118 /* SSA and register indices are completely messed up now */
119 nir_index_ssa_defs(impl
);
120 nir_index_local_regs(impl
);
122 nir_metadata_preserve(impl
, nir_metadata_none
);
124 return state
.progress
;
128 nir_inline_functions(nir_shader
*shader
)
130 bool progress
= false;
132 nir_foreach_function(shader
, function
) {
134 progress
= nir_inline_functions_impl(function
->impl
) || progress
;