Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mesa
Path: blob/21.2-virgl/src/compiler/nir/nir_inline_functions.c
4546 views
1
/*
2
* Copyright © 2015 Intel Corporation
3
*
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:
10
*
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
13
* Software.
14
*
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
21
* IN THE SOFTWARE.
22
*/
23
24
#include "nir.h"
25
#include "nir_builder.h"
26
#include "nir_control_flow.h"
27
#include "nir_vla.h"
28
29
void nir_inline_function_impl(struct nir_builder *b,
30
const nir_function_impl *impl,
31
nir_ssa_def **params,
32
struct hash_table *shader_var_remap)
33
{
34
nir_function_impl *copy = nir_function_impl_clone(b->shader, impl);
35
36
/* Insert a nop at the cursor so we can keep track of where things are as
37
* we add/remove stuff from the CFG.
38
*/
39
nir_intrinsic_instr *nop = nir_nop(b);
40
41
exec_list_append(&b->impl->locals, &copy->locals);
42
exec_list_append(&b->impl->registers, &copy->registers);
43
44
nir_foreach_block(block, copy) {
45
nir_foreach_instr_safe(instr, block) {
46
switch (instr->type) {
47
case nir_instr_type_deref: {
48
nir_deref_instr *deref = nir_instr_as_deref(instr);
49
if (deref->deref_type != nir_deref_type_var)
50
break;
51
52
/* We don't need to remap function variables. We already cloned
53
* them as part of nir_function_impl_clone and appended them to
54
* b->impl->locals.
55
*/
56
if (deref->var->data.mode == nir_var_function_temp)
57
break;
58
59
/* If no map is provided, we assume that there are either no
60
* shader variables or they already live b->shader (this is the
61
* case for function inlining within a single shader.
62
*/
63
if (shader_var_remap == NULL)
64
break;
65
66
struct hash_entry *entry =
67
_mesa_hash_table_search(shader_var_remap, deref->var);
68
if (entry == NULL) {
69
nir_variable *nvar = nir_variable_clone(deref->var, b->shader);
70
nir_shader_add_variable(b->shader, nvar);
71
entry = _mesa_hash_table_insert(shader_var_remap,
72
deref->var, nvar);
73
}
74
deref->var = entry->data;
75
break;
76
}
77
78
case nir_instr_type_intrinsic: {
79
nir_intrinsic_instr *load = nir_instr_as_intrinsic(instr);
80
if (load->intrinsic != nir_intrinsic_load_param)
81
break;
82
83
unsigned param_idx = nir_intrinsic_param_idx(load);
84
assert(param_idx < impl->function->num_params);
85
assert(load->dest.is_ssa);
86
nir_ssa_def_rewrite_uses(&load->dest.ssa,
87
params[param_idx]);
88
89
/* Remove any left-over load_param intrinsics because they're soon
90
* to be in another function and therefore no longer valid.
91
*/
92
nir_instr_remove(&load->instr);
93
break;
94
}
95
96
case nir_instr_type_jump:
97
/* Returns have to be lowered for this to work */
98
assert(nir_instr_as_jump(instr)->type != nir_jump_return);
99
break;
100
101
default:
102
break;
103
}
104
}
105
}
106
107
/* Pluck the body out of the function and place it here */
108
nir_cf_list body;
109
nir_cf_list_extract(&body, &copy->body);
110
nir_cf_reinsert(&body, nir_before_instr(&nop->instr));
111
112
b->cursor = nir_instr_remove(&nop->instr);
113
}
114
115
static bool inline_function_impl(nir_function_impl *impl, struct set *inlined);
116
117
static bool
118
inline_functions_block(nir_block *block, nir_builder *b,
119
struct set *inlined)
120
{
121
bool progress = false;
122
/* This is tricky. We're iterating over instructions in a block but, as
123
* we go, the block and its instruction list are being split into
124
* pieces. However, this *should* be safe since foreach_safe always
125
* stashes the next thing in the iteration. That next thing will
126
* properly get moved to the next block when it gets split, and we
127
* continue iterating there.
128
*/
129
nir_foreach_instr_safe(instr, block) {
130
if (instr->type != nir_instr_type_call)
131
continue;
132
133
progress = true;
134
135
nir_call_instr *call = nir_instr_as_call(instr);
136
assert(call->callee->impl);
137
138
/* Make sure that the function we're calling is already inlined */
139
inline_function_impl(call->callee->impl, inlined);
140
141
b->cursor = nir_instr_remove(&call->instr);
142
143
/* Rewrite all of the uses of the callee's parameters to use the call
144
* instructions sources. In order to ensure that the "load" happens
145
* here and not later (for register sources), we make sure to convert it
146
* to an SSA value first.
147
*/
148
const unsigned num_params = call->num_params;
149
NIR_VLA(nir_ssa_def *, params, num_params);
150
for (unsigned i = 0; i < num_params; i++) {
151
params[i] = nir_ssa_for_src(b, call->params[i],
152
call->callee->params[i].num_components);
153
}
154
155
nir_inline_function_impl(b, call->callee->impl, params, NULL);
156
}
157
158
return progress;
159
}
160
161
static bool
162
inline_function_impl(nir_function_impl *impl, struct set *inlined)
163
{
164
if (_mesa_set_search(inlined, impl))
165
return false; /* Already inlined */
166
167
nir_builder b;
168
nir_builder_init(&b, impl);
169
170
bool progress = false;
171
nir_foreach_block_safe(block, impl) {
172
progress |= inline_functions_block(block, &b, inlined);
173
}
174
175
if (progress) {
176
/* SSA and register indices are completely messed up now */
177
nir_index_ssa_defs(impl);
178
nir_index_local_regs(impl);
179
180
nir_metadata_preserve(impl, nir_metadata_none);
181
} else {
182
nir_metadata_preserve(impl, nir_metadata_all);
183
}
184
185
_mesa_set_add(inlined, impl);
186
187
return progress;
188
}
189
190
/** A pass to inline all functions in a shader into their callers
191
*
192
* For most use-cases, function inlining is a multi-step process. The general
193
* pattern employed by SPIR-V consumers and others is as follows:
194
*
195
* 1. nir_lower_variable_initializers(shader, nir_var_function_temp)
196
*
197
* This is needed because local variables from the callee are simply added
198
* to the locals list for the caller and the information about where the
199
* constant initializer logically happens is lost. If the callee is
200
* called in a loop, this can cause the variable to go from being
201
* initialized once per loop iteration to being initialized once at the
202
* top of the caller and values to persist from one invocation of the
203
* callee to the next. The simple solution to this problem is to get rid
204
* of constant initializers before function inlining.
205
*
206
* 2. nir_lower_returns(shader)
207
*
208
* nir_inline_functions assumes that all functions end "naturally" by
209
* execution reaching the end of the function without any return
210
* instructions causing instant jumps to the end. Thanks to NIR being
211
* structured, we can't represent arbitrary jumps to various points in the
212
* program which is what an early return in the callee would have to turn
213
* into when we inline it into the caller. Instead, we require returns to
214
* be lowered which lets us just copy+paste the callee directly into the
215
* caller.
216
*
217
* 3. nir_inline_functions(shader)
218
*
219
* This does the actual function inlining and the resulting shader will
220
* contain no call instructions.
221
*
222
* 4. nir_opt_deref(shader)
223
*
224
* Most functions contain pointer parameters where the result of a deref
225
* instruction is passed in as a parameter, loaded via a load_param
226
* intrinsic, and then turned back into a deref via a cast. Function
227
* inlining will get rid of the load_param but we are still left with a
228
* cast. Running nir_opt_deref gets rid of the intermediate cast and
229
* results in a whole deref chain again. This is currently required by a
230
* number of optimizations and lowering passes at least for certain
231
* variable modes.
232
*
233
* 5. Loop over the functions and delete all but the main entrypoint.
234
*
235
* In the Intel Vulkan driver this looks like this:
236
*
237
* foreach_list_typed_safe(nir_function, func, node, &nir->functions) {
238
* if (func != entry_point)
239
* exec_node_remove(&func->node);
240
* }
241
* assert(exec_list_length(&nir->functions) == 1);
242
*
243
* While nir_inline_functions does get rid of all call instructions, it
244
* doesn't get rid of any functions because it doesn't know what the "root
245
* function" is. Instead, it's up to the individual driver to know how to
246
* decide on a root function and delete the rest. With SPIR-V,
247
* spirv_to_nir returns the root function and so we can just use == whereas
248
* with GL, you may have to look for a function named "main".
249
*
250
* 6. nir_lower_variable_initializers(shader, ~nir_var_function_temp)
251
*
252
* Lowering constant initializers on inputs, outputs, global variables,
253
* etc. requires that we know the main entrypoint so that we know where to
254
* initialize them. Otherwise, we would have to assume that anything
255
* could be a main entrypoint and initialize them at the start of every
256
* function but that would clearly be wrong if any of those functions were
257
* ever called within another function. Simply requiring a single-
258
* entrypoint function shader is the best way to make it well-defined.
259
*/
260
bool
261
nir_inline_functions(nir_shader *shader)
262
{
263
struct set *inlined = _mesa_pointer_set_create(NULL);
264
bool progress = false;
265
266
nir_foreach_function(function, shader) {
267
if (function->impl)
268
progress = inline_function_impl(function->impl, inlined) || progress;
269
}
270
271
_mesa_set_destroy(inlined, NULL);
272
273
return progress;
274
}
275
276