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
{
34 static bool inline_function_impl(nir_function_impl
*impl
, struct set
*inlined
);
37 inline_functions_block(nir_block
*block
, void *void_state
)
39 struct inline_functions_state
*state
= void_state
;
41 nir_builder
*b
= &state
->builder
;
43 /* This is tricky. We're iterating over instructions in a block but, as
44 * we go, the block and its instruction list are being split into
45 * pieces. However, this *should* be safe since foreach_safe always
46 * stashes the next thing in the iteration. That next thing will
47 * properly get moved to the next block when it gets split, and we
48 * continue iterating there.
50 nir_foreach_instr_safe(block
, instr
) {
51 if (instr
->type
!= nir_instr_type_call
)
54 state
->progress
= true;
56 nir_call_instr
*call
= nir_instr_as_call(instr
);
57 assert(call
->callee
->impl
);
59 inline_function_impl(call
->callee
->impl
, state
->inlined
);
61 nir_function_impl
*callee_copy
=
62 nir_function_impl_clone(call
->callee
->impl
);
64 exec_list_append(&b
->impl
->locals
, &callee_copy
->locals
);
65 exec_list_append(&b
->impl
->registers
, &callee_copy
->registers
);
67 b
->cursor
= nir_before_instr(&call
->instr
);
69 /* Add copies of all in parameters */
70 assert(call
->num_params
== callee_copy
->num_params
);
71 for (unsigned i
= 0; i
< callee_copy
->num_params
; i
++) {
72 /* Only in or inout parameters */
73 if (call
->callee
->params
[i
].param_type
== nir_parameter_out
)
76 nir_copy_deref_var(b
, nir_deref_var_create(b
->shader
,
77 callee_copy
->params
[i
]),
81 /* Pluck the body out of the function and place it here */
83 nir_cf_list_extract(&body
, &callee_copy
->body
);
84 nir_cf_reinsert(&body
, b
->cursor
);
86 b
->cursor
= nir_before_instr(&call
->instr
);
88 /* Add copies of all out parameters and the return */
89 assert(call
->num_params
== callee_copy
->num_params
);
90 for (unsigned i
= 0; i
< callee_copy
->num_params
; i
++) {
91 /* Only out or inout parameters */
92 if (call
->callee
->params
[i
].param_type
== nir_parameter_in
)
95 nir_copy_deref_var(b
, call
->params
[i
],
96 nir_deref_var_create(b
->shader
,
97 callee_copy
->params
[i
]));
99 if (!glsl_type_is_void(call
->callee
->return_type
)) {
100 nir_copy_deref_var(b
, call
->return_deref
,
101 nir_deref_var_create(b
->shader
,
102 callee_copy
->return_var
));
105 nir_instr_remove(&call
->instr
);
112 inline_function_impl(nir_function_impl
*impl
, struct set
*inlined
)
114 if (_mesa_set_search(inlined
, impl
))
115 return false; /* Already inlined */
117 struct inline_functions_state state
;
119 state
.inlined
= inlined
;
120 state
.progress
= false;
121 nir_builder_init(&state
.builder
, impl
);
123 nir_foreach_block(impl
, inline_functions_block
, &state
);
125 if (state
.progress
) {
126 /* SSA and register indices are completely messed up now */
127 nir_index_ssa_defs(impl
);
128 nir_index_local_regs(impl
);
130 nir_metadata_preserve(impl
, nir_metadata_none
);
133 _mesa_set_add(inlined
, impl
);
135 return state
.progress
;
139 nir_inline_functions(nir_shader
*shader
)
141 struct set
*inlined
= _mesa_set_create(NULL
, _mesa_hash_pointer
,
142 _mesa_key_pointer_equal
);
143 bool progress
= false;
145 nir_foreach_function(shader
, function
) {
147 progress
= inline_function_impl(function
->impl
, inlined
) || progress
;
150 _mesa_set_destroy(inlined
, NULL
);