OSDN Git Service

4641e8cc20e96a43c0e07087b53343b2b74fa4c1
[android-x86/external-mesa.git] / src / compiler / nir / nir_inline_functions.c
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
28 static bool inline_function_impl(nir_function_impl *impl, struct set *inlined);
29
30 static bool
31 rewrite_param_derefs_block(nir_block *block, nir_call_instr *call)
32 {
33    nir_foreach_instr_safe(instr, block) {
34       if (instr->type != nir_instr_type_intrinsic)
35          continue;
36
37       nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
38
39       for (unsigned i = 0;
40            i < nir_intrinsic_infos[intrin->intrinsic].num_variables; i++) {
41          if (intrin->variables[i]->var->data.mode != nir_var_param)
42             continue;
43
44          int param_idx = intrin->variables[i]->var->data.location;
45
46          nir_deref_var *call_deref;
47          if (param_idx >= 0) {
48             assert(param_idx < call->callee->num_params);
49             call_deref = call->params[param_idx];
50          } else {
51             call_deref = call->return_deref;
52          }
53          assert(call_deref);
54
55          nir_deref_var *new_deref = nir_deref_as_var(nir_copy_deref(intrin, &call_deref->deref));
56          nir_deref *new_tail = nir_deref_tail(&new_deref->deref);
57          new_tail->child = intrin->variables[i]->deref.child;
58          ralloc_steal(new_tail, new_tail->child);
59          intrin->variables[i] = new_deref;
60       }
61    }
62
63    return true;
64 }
65
66 static void
67 lower_param_to_local(nir_variable *param, nir_function_impl *impl, bool write)
68 {
69    if (param->data.mode != nir_var_param)
70       return;
71
72    nir_parameter_type param_type;
73    if (param->data.location >= 0) {
74       assert(param->data.location < impl->num_params);
75       param_type = impl->function->params[param->data.location].param_type;
76    } else {
77       /* Return variable */
78       param_type = nir_parameter_out;
79    }
80
81    if ((write && param_type == nir_parameter_in) ||
82        (!write && param_type == nir_parameter_out)) {
83       /* In this case, we need a shadow copy.  Turn it into a local */
84       param->data.mode = nir_var_local;
85       exec_list_push_tail(&impl->locals, &param->node);
86    }
87 }
88
89 static bool
90 lower_params_to_locals_block(nir_block *block, nir_function_impl *impl)
91 {
92    nir_foreach_instr_safe(instr, block) {
93       if (instr->type != nir_instr_type_intrinsic)
94          continue;
95
96       nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
97
98       switch (intrin->intrinsic) {
99       case nir_intrinsic_store_var:
100          lower_param_to_local(intrin->variables[0]->var, impl, true);
101          break;
102
103       case nir_intrinsic_copy_var:
104          lower_param_to_local(intrin->variables[0]->var, impl, true);
105          lower_param_to_local(intrin->variables[1]->var, impl, false);
106          break;
107
108       case nir_intrinsic_load_var:
109          /* All other intrinsics which access variables (image_load_store)
110           * do so in a read-only fasion.
111           */
112          for (unsigned i = 0;
113               i < nir_intrinsic_infos[intrin->intrinsic].num_variables; i++) {
114             lower_param_to_local(intrin->variables[i]->var, impl, false);
115          }
116          break;
117
118       default:
119          continue;
120       }
121    }
122
123    return true;
124 }
125
126 static bool
127 inline_functions_block(nir_block *block, nir_builder *b,
128                        struct set *inlined)
129 {
130    bool progress = false;
131    /* This is tricky.  We're iterating over instructions in a block but, as
132     * we go, the block and its instruction list are being split into
133     * pieces.  However, this *should* be safe since foreach_safe always
134     * stashes the next thing in the iteration.  That next thing will
135     * properly get moved to the next block when it gets split, and we
136     * continue iterating there.
137     */
138    nir_foreach_instr_safe(instr, block) {
139       if (instr->type != nir_instr_type_call)
140          continue;
141
142       progress = true;
143
144       nir_call_instr *call = nir_instr_as_call(instr);
145       assert(call->callee->impl);
146
147       inline_function_impl(call->callee->impl, inlined);
148
149       nir_function_impl *callee_copy =
150          nir_function_impl_clone(call->callee->impl);
151       callee_copy->function = call->callee;
152
153       /* Add copies of all in parameters */
154       assert(call->num_params == callee_copy->num_params);
155
156       exec_list_append(&b->impl->locals, &callee_copy->locals);
157       exec_list_append(&b->impl->registers, &callee_copy->registers);
158
159       b->cursor = nir_before_instr(&call->instr);
160
161       /* We now need to tie the two functions together using the
162        * parameters.  There are two ways we do this: One is to turn the
163        * parameter into a local variable and do a shadow-copy.  The other
164        * is to treat the parameter as a "proxy" and rewrite derefs to use
165        * the actual variable that comes from the call instruction.  We
166        * implement both schemes.  The first is needed in the case where we
167        * have an in parameter that we write or similar.  The second case is
168        * needed for handling things such as images and uniforms properly.
169        */
170
171       /* Figure out when we need to lower to a shadow local */
172       nir_foreach_block(block, callee_copy) {
173          lower_params_to_locals_block(block, callee_copy);
174       }
175
176       for (unsigned i = 0; i < callee_copy->num_params; i++) {
177          nir_variable *param = callee_copy->params[i];
178
179          if (param->data.mode == nir_var_local &&
180              call->callee->params[i].param_type != nir_parameter_out) {
181             nir_copy_deref_var(b, nir_deref_var_create(b->shader, param),
182                                   call->params[i]);
183          }
184       }
185
186       nir_foreach_block(block, callee_copy) {
187          rewrite_param_derefs_block(block, call);
188       }
189
190       /* Pluck the body out of the function and place it here */
191       nir_cf_list body;
192       nir_cf_list_extract(&body, &callee_copy->body);
193       nir_cf_reinsert(&body, b->cursor);
194
195       b->cursor = nir_before_instr(&call->instr);
196
197       /* Add copies of all out parameters and the return */
198       assert(call->num_params == callee_copy->num_params);
199       for (unsigned i = 0; i < callee_copy->num_params; i++) {
200          nir_variable *param = callee_copy->params[i];
201
202          if (param->data.mode == nir_var_local &&
203              call->callee->params[i].param_type != nir_parameter_in) {
204             nir_copy_deref_var(b, call->params[i],
205                                   nir_deref_var_create(b->shader, param));
206          }
207       }
208       if (!glsl_type_is_void(call->callee->return_type) &&
209           callee_copy->return_var->data.mode == nir_var_local) {
210          nir_copy_deref_var(b, call->return_deref,
211                                nir_deref_var_create(b->shader,
212                                                     callee_copy->return_var));
213       }
214
215       nir_instr_remove(&call->instr);
216    }
217
218    return progress;
219 }
220
221 static bool
222 inline_function_impl(nir_function_impl *impl, struct set *inlined)
223 {
224    if (_mesa_set_search(inlined, impl))
225       return false; /* Already inlined */
226
227    nir_builder b;
228    nir_builder_init(&b, impl);
229
230    bool progress = false;
231    nir_foreach_block_safe(block, impl) {
232       progress |= inline_functions_block(block, &b, inlined);
233    }
234
235    if (progress) {
236       /* SSA and register indices are completely messed up now */
237       nir_index_ssa_defs(impl);
238       nir_index_local_regs(impl);
239
240       nir_metadata_preserve(impl, nir_metadata_none);
241    }
242
243    _mesa_set_add(inlined, impl);
244
245    return progress;
246 }
247
248 bool
249 nir_inline_functions(nir_shader *shader)
250 {
251    struct set *inlined = _mesa_set_create(NULL, _mesa_hash_pointer,
252                                           _mesa_key_pointer_equal);
253    bool progress = false;
254
255    nir_foreach_function(function, shader) {
256       if (function->impl)
257          progress = inline_function_impl(function->impl, inlined) || progress;
258    }
259
260    _mesa_set_destroy(inlined, NULL);
261
262    return progress;
263 }