nir,spirv: Rework function calls
[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 #include "nir_vla.h"
28
29 static bool inline_function_impl(nir_function_impl *impl, struct set *inlined);
30
31 static bool
32 inline_functions_block(nir_block *block, nir_builder *b,
33 struct set *inlined)
34 {
35 bool progress = false;
36 /* This is tricky. We're iterating over instructions in a block but, as
37 * we go, the block and its instruction list are being split into
38 * pieces. However, this *should* be safe since foreach_safe always
39 * stashes the next thing in the iteration. That next thing will
40 * properly get moved to the next block when it gets split, and we
41 * continue iterating there.
42 */
43 nir_foreach_instr_safe(instr, block) {
44 if (instr->type != nir_instr_type_call)
45 continue;
46
47 progress = true;
48
49 nir_call_instr *call = nir_instr_as_call(instr);
50 assert(call->callee->impl);
51
52 inline_function_impl(call->callee->impl, inlined);
53
54 nir_function_impl *callee_copy =
55 nir_function_impl_clone(call->callee->impl);
56 callee_copy->function = call->callee;
57
58 exec_list_append(&b->impl->locals, &callee_copy->locals);
59 exec_list_append(&b->impl->registers, &callee_copy->registers);
60
61 b->cursor = nir_before_instr(&call->instr);
62
63 /* Rewrite all of the uses of the callee's parameters to use the call
64 * instructions sources. In order to ensure that the "load" happens
65 * here and not later (for register sources), we make sure to convert it
66 * to an SSA value first.
67 */
68 const unsigned num_params = call->num_params;
69 NIR_VLA(nir_ssa_def *, params, num_params);
70 for (unsigned i = 0; i < num_params; i++) {
71 params[i] = nir_ssa_for_src(b, call->params[i],
72 call->callee->params[i].num_components);
73 }
74
75 nir_foreach_block(block, callee_copy) {
76 nir_foreach_instr_safe(instr, block) {
77 if (instr->type != nir_instr_type_intrinsic)
78 continue;
79
80 nir_intrinsic_instr *load = nir_instr_as_intrinsic(instr);
81 if (load->intrinsic != nir_intrinsic_load_param)
82 continue;
83
84 unsigned param_idx = nir_intrinsic_param_idx(load);
85 assert(param_idx < num_params);
86 assert(load->dest.is_ssa);
87 nir_ssa_def_rewrite_uses(&load->dest.ssa,
88 nir_src_for_ssa(params[param_idx]));
89
90 /* Remove any left-over load_param intrinsics because they're soon
91 * to be in another function and therefore no longer valid.
92 */
93 nir_instr_remove(&load->instr);
94 }
95 }
96
97 /* Pluck the body out of the function and place it here */
98 nir_cf_list body;
99 nir_cf_list_extract(&body, &callee_copy->body);
100 nir_cf_reinsert(&body, b->cursor);
101
102 nir_instr_remove(&call->instr);
103 }
104
105 return progress;
106 }
107
108 static bool
109 inline_function_impl(nir_function_impl *impl, struct set *inlined)
110 {
111 if (_mesa_set_search(inlined, impl))
112 return false; /* Already inlined */
113
114 nir_builder b;
115 nir_builder_init(&b, impl);
116
117 bool progress = false;
118 nir_foreach_block_safe(block, impl) {
119 progress |= inline_functions_block(block, &b, inlined);
120 }
121
122 if (progress) {
123 /* SSA and register indices are completely messed up now */
124 nir_index_ssa_defs(impl);
125 nir_index_local_regs(impl);
126
127 nir_metadata_preserve(impl, nir_metadata_none);
128 }
129
130 _mesa_set_add(inlined, impl);
131
132 return progress;
133 }
134
135 bool
136 nir_inline_functions(nir_shader *shader)
137 {
138 struct set *inlined = _mesa_set_create(NULL, _mesa_hash_pointer,
139 _mesa_key_pointer_equal);
140 bool progress = false;
141
142 nir_foreach_function(function, shader) {
143 if (function->impl)
144 progress = inline_function_impl(function->impl, inlined) || progress;
145 }
146
147 _mesa_set_destroy(inlined, NULL);
148
149 return progress;
150 }