1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
|
/*
* Copyright © 2015 Intel Corporation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice (including the next
* paragraph) shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
#include "nir.h"
#include "nir_builder.h"
#include "nir_control_flow.h"
#include "nir_vla.h"
void nir_inline_function_impl(struct nir_builder *b,
const nir_function_impl *impl,
nir_ssa_def **params)
{
nir_function_impl *copy = nir_function_impl_clone(b->shader, impl);
/* Insert a nop at the cursor so we can keep track of where things are as
* we add/remove stuff from the CFG.
*/
nir_intrinsic_instr *nop =
nir_intrinsic_instr_create(b->shader, nir_intrinsic_nop);
nir_builder_instr_insert(b, &nop->instr);
exec_list_append(&b->impl->locals, ©->locals);
exec_list_append(&b->impl->registers, ©->registers);
nir_foreach_block(block, copy) {
nir_foreach_instr_safe(instr, block) {
/* Returns have to be lowered for this to work */
assert(instr->type != nir_instr_type_jump ||
nir_instr_as_jump(instr)->type != nir_jump_return);
if (instr->type != nir_instr_type_intrinsic)
continue;
nir_intrinsic_instr *load = nir_instr_as_intrinsic(instr);
if (load->intrinsic != nir_intrinsic_load_param)
continue;
unsigned param_idx = nir_intrinsic_param_idx(load);
assert(param_idx < impl->function->num_params);
assert(load->dest.is_ssa);
nir_ssa_def_rewrite_uses(&load->dest.ssa,
nir_src_for_ssa(params[param_idx]));
/* Remove any left-over load_param intrinsics because they're soon
* to be in another function and therefore no longer valid.
*/
nir_instr_remove(&load->instr);
}
}
/* Pluck the body out of the function and place it here */
nir_cf_list body;
nir_cf_list_extract(&body, ©->body);
nir_cf_reinsert(&body, nir_before_instr(&nop->instr));
b->cursor = nir_instr_remove(&nop->instr);
}
static bool inline_function_impl(nir_function_impl *impl, struct set *inlined);
static bool
inline_functions_block(nir_block *block, nir_builder *b,
struct set *inlined)
{
bool progress = false;
/* This is tricky. We're iterating over instructions in a block but, as
* we go, the block and its instruction list are being split into
* pieces. However, this *should* be safe since foreach_safe always
* stashes the next thing in the iteration. That next thing will
* properly get moved to the next block when it gets split, and we
* continue iterating there.
*/
nir_foreach_instr_safe(instr, block) {
if (instr->type != nir_instr_type_call)
continue;
progress = true;
nir_call_instr *call = nir_instr_as_call(instr);
assert(call->callee->impl);
/* Make sure that the function we're calling is already inlined */
inline_function_impl(call->callee->impl, inlined);
b->cursor = nir_instr_remove(&call->instr);
/* Rewrite all of the uses of the callee's parameters to use the call
* instructions sources. In order to ensure that the "load" happens
* here and not later (for register sources), we make sure to convert it
* to an SSA value first.
*/
const unsigned num_params = call->num_params;
NIR_VLA(nir_ssa_def *, params, num_params);
for (unsigned i = 0; i < num_params; i++) {
params[i] = nir_ssa_for_src(b, call->params[i],
call->callee->params[i].num_components);
}
nir_inline_function_impl(b, call->callee->impl, params);
}
return progress;
}
static bool
inline_function_impl(nir_function_impl *impl, struct set *inlined)
{
if (_mesa_set_search(inlined, impl))
return false; /* Already inlined */
nir_builder b;
nir_builder_init(&b, impl);
bool progress = false;
nir_foreach_block_safe(block, impl) {
progress |= inline_functions_block(block, &b, inlined);
}
if (progress) {
/* SSA and register indices are completely messed up now */
nir_index_ssa_defs(impl);
nir_index_local_regs(impl);
nir_metadata_preserve(impl, nir_metadata_none);
} else {
#ifndef NDEBUG
impl->valid_metadata &= ~nir_metadata_not_properly_reset;
#endif
}
_mesa_set_add(inlined, impl);
return progress;
}
/** A pass to inline all functions in a shader into their callers
*
* For most use-cases, function inlining is a multi-step process. The general
* pattern employed by SPIR-V consumers and others is as follows:
*
* 1. nir_lower_constant_initializers(shader, nir_var_function_temp)
*
* This is needed because local variables from the callee are simply added
* to the locals list for the caller and the information about where the
* constant initializer logically happens is lost. If the callee is
* called in a loop, this can cause the variable to go from being
* initialized once per loop iteration to being initialized once at the
* top of the caller and values to persist from one invocation of the
* callee to the next. The simple solution to this problem is to get rid
* of constant initializers before function inlining.
*
* 2. nir_lower_returns(shader)
*
* nir_inline_functions assumes that all functions end "naturally" by
* execution reaching the end of the function without any return
* instructions causing instant jumps to the end. Thanks to NIR being
* structured, we can't represent arbitrary jumps to various points in the
* program which is what an early return in the callee would have to turn
* into when we inline it into the caller. Instead, we require returns to
* be lowered which lets us just copy+paste the callee directly into the
* caller.
*
* 3. nir_inline_functions(shader)
*
* This does the actual function inlining and the resulting shader will
* contain no call instructions.
*
* 4. nir_opt_deref(shader)
*
* Most functions contain pointer parameters where the result of a deref
* instruction is passed in as a parameter, loaded via a load_param
* intrinsic, and then turned back into a deref via a cast. Function
* inlining will get rid of the load_param but we are still left with a
* cast. Running nir_opt_deref gets rid of the intermediate cast and
* results in a whole deref chain again. This is currently required by a
* number of optimizations and lowering passes at least for certain
* variable modes.
*
* 5. Loop over the functions and delete all but the main entrypoint.
*
* In the Intel Vulkan driver this looks like this:
*
* foreach_list_typed_safe(nir_function, func, node, &nir->functions) {
* if (func != entry_point)
* exec_node_remove(&func->node);
* }
* assert(exec_list_length(&nir->functions) == 1);
*
* While nir_inline_functions does get rid of all call instructions, it
* doesn't get rid of any functions because it doesn't know what the "root
* function" is. Instead, it's up to the individual driver to know how to
* decide on a root function and delete the rest. With SPIR-V,
* spirv_to_nir returns the root function and so we can just use == whereas
* with GL, you may have to look for a function named "main".
*
* 6. nir_lower_constant_initializers(shader, ~nir_var_function_temp)
*
* Lowering constant initializers on inputs, outputs, global variables,
* etc. requires that we know the main entrypoint so that we know where to
* initialize them. Otherwise, we would have to assume that anything
* could be a main entrypoint and initialize them at the start of every
* function but that would clearly be wrong if any of those functions were
* ever called within another function. Simply requiring a single-
* entrypoint function shader is the best way to make it well-defined.
*/
bool
nir_inline_functions(nir_shader *shader)
{
struct set *inlined = _mesa_pointer_set_create(NULL);
bool progress = false;
nir_foreach_function(function, shader) {
if (function->impl)
progress = inline_function_impl(function->impl, inlined) || progress;
}
_mesa_set_destroy(inlined, NULL);
return progress;
}
|