nir: Report progress from nir_split_var_copies().
[mesa.git] / src / glsl / nir / nir_split_var_copies.c
1 /*
2 * Copyright © 2014 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 * Authors:
24 * Jason Ekstrand (jason@jlekstrand.net)
25 *
26 */
27
28 #include "nir.h"
29
30 /*
31 * Implements "copy splitting" which is similar to structure splitting only
32 * it works on copy operations rather than the datatypes themselves. The
33 * GLSL language allows you to copy one variable to another an entire
34 * structure (which may contain arrays or other structures) at a time.
35 * Normally, in a language such as C this would be handled by a "structure
36 * splitting" pass that breaks up the structures. Unfortunately for us,
37 * structures used in inputs or outputs can't be split. Therefore,
38 * regardlesss of what we do, we have to be able to copy to/from
39 * structures.
40 *
41 * The primary purpose of structure splitting is to allow you to better
42 * optimize variable access and lower things to registers where you can.
43 * The primary issue here is that, if you lower the copy to a bunch of
44 * loads and stores, you loose a lot of information about the copy
45 * operation that you would like to keep around. To solve this problem, we
46 * have a "copy splitting" pass that, instead of splitting the structures
47 * or lowering the copy into loads and storres, splits the copy operation
48 * into a bunch of copy operations one for each leaf of the structure tree.
49 * If an intermediate array is encountered, it is referenced with a
50 * wildcard reference to indicate that the entire array is to be copied.
51 *
52 * As things become direct, array copies may be able to be losslessly
53 * lowered to having fewer and fewer wildcards. However, until that
54 * happens we want to keep the information about the arrays intact.
55 *
56 * Prior to the copy splitting pass, there are no wildcard references but
57 * there may be incomplete references where the tail of the deref chain is
58 * an array or a structure and not a specific element. After the copy
59 * splitting pass has completed, every variable deref will be a full-length
60 * dereference pointing to a single leaf in the structure type tree with
61 * possibly a few wildcard array dereferences.
62 */
63
64 struct split_var_copies_state {
65 void *mem_ctx;
66 void *dead_ctx;
67 bool progress;
68 };
69
70 static nir_deref *
71 get_deref_tail(nir_deref *deref)
72 {
73 while (deref->child != NULL)
74 deref = deref->child;
75 return deref;
76 }
77
78 /* Recursively constructs deref chains to split a copy instruction into
79 * multiple (if needed) copy instructions with full-length deref chains.
80 * External callers of this function should pass the tail and head of the
81 * deref chains found as the source and destination of the copy instruction
82 * into this function.
83 *
84 * \param old_copy The copy instruction we are splitting
85 * \param dest_head The head of the destination deref chain we are building
86 * \param src_head The head of the source deref chain we are building
87 * \param dest_tail The tail of the destination deref chain we are building
88 * \param src_tail The tail of the source deref chain we are building
89 * \param state The current split_var_copies_state object
90 */
91 static void
92 split_var_copy_instr(nir_intrinsic_instr *old_copy,
93 nir_deref *dest_head, nir_deref *src_head,
94 nir_deref *dest_tail, nir_deref *src_tail,
95 struct split_var_copies_state *state)
96 {
97 assert(src_tail->type == dest_tail->type);
98
99 /* Make sure these really are the tails of the deref chains */
100 assert(dest_tail->child == NULL);
101 assert(src_tail->child == NULL);
102
103 switch (glsl_get_base_type(src_tail->type)) {
104 case GLSL_TYPE_ARRAY: {
105 /* Make a wildcard dereference */
106 nir_deref_array *deref = nir_deref_array_create(state->dead_ctx);
107 deref->deref.type = glsl_get_array_element(src_tail->type);
108 deref->deref_array_type = nir_deref_array_type_wildcard;
109
110 /* Set the tail of both as the newly created wildcard deref. It is
111 * safe to use the same wildcard in both places because a) we will be
112 * copying it before we put it in an actual instruction and b)
113 * everything that will potentially add another link in the deref
114 * chain will also add the same thing to both chains.
115 */
116 src_tail->child = &deref->deref;
117 dest_tail->child = &deref->deref;
118
119 split_var_copy_instr(old_copy, dest_head, src_head,
120 dest_tail->child, src_tail->child, state);
121
122 /* Set it back to the way we found it */
123 src_tail->child = NULL;
124 dest_tail->child = NULL;
125 break;
126 }
127
128 case GLSL_TYPE_STRUCT:
129 /* This is the only part that actually does any interesting
130 * splitting. For array types, we just use wildcards and resolve
131 * them later. For structure types, we need to emit one copy
132 * instruction for every structure element. Because we may have
133 * structs inside structs, we just recurse and let the next level
134 * take care of any additional structures.
135 */
136 for (unsigned i = 0; i < glsl_get_length(src_tail->type); i++) {
137 nir_deref_struct *deref = nir_deref_struct_create(state->dead_ctx, i);
138 deref->deref.type = glsl_get_struct_field(src_tail->type, i);
139
140 /* Set the tail of both as the newly created structure deref. It
141 * is safe to use the same wildcard in both places because a) we
142 * will be copying it before we put it in an actual instruction
143 * and b) everything that will potentially add another link in the
144 * deref chain will also add the same thing to both chains.
145 */
146 src_tail->child = &deref->deref;
147 dest_tail->child = &deref->deref;
148
149 split_var_copy_instr(old_copy, dest_head, src_head,
150 dest_tail->child, src_tail->child, state);
151 }
152 /* Set it back to the way we found it */
153 src_tail->child = NULL;
154 dest_tail->child = NULL;
155 break;
156
157 case GLSL_TYPE_UINT:
158 case GLSL_TYPE_INT:
159 case GLSL_TYPE_FLOAT:
160 case GLSL_TYPE_BOOL:
161 if (glsl_type_is_matrix(src_tail->type)) {
162 nir_deref_array *deref = nir_deref_array_create(state->dead_ctx);
163 deref->deref.type = glsl_get_column_type(src_tail->type);
164 deref->deref_array_type = nir_deref_array_type_wildcard;
165
166 /* Set the tail of both as the newly created wildcard deref. It
167 * is safe to use the same wildcard in both places because a) we
168 * will be copying it before we put it in an actual instruction
169 * and b) everything that will potentially add another link in the
170 * deref chain will also add the same thing to both chains.
171 */
172 src_tail->child = &deref->deref;
173 dest_tail->child = &deref->deref;
174
175 split_var_copy_instr(old_copy, dest_head, src_head,
176 dest_tail->child, src_tail->child, state);
177
178 /* Set it back to the way we found it */
179 src_tail->child = NULL;
180 dest_tail->child = NULL;
181 } else {
182 /* At this point, we have fully built our deref chains and can
183 * actually add the new copy instruction.
184 */
185 nir_intrinsic_instr *new_copy =
186 nir_intrinsic_instr_create(state->mem_ctx, nir_intrinsic_copy_var);
187
188 /* We need to make copies because a) this deref chain actually
189 * belongs to the copy instruction and b) the deref chains may
190 * have some of the same links due to the way we constructed them
191 */
192 nir_deref *src = nir_copy_deref(new_copy, src_head);
193 nir_deref *dest = nir_copy_deref(new_copy, dest_head);
194
195 new_copy->variables[0] = nir_deref_as_var(dest);
196 new_copy->variables[1] = nir_deref_as_var(src);
197
198 /* Emit the copy instruction after the old instruction. We'll
199 * remove the old one later.
200 */
201 nir_instr_insert_after(&old_copy->instr, &new_copy->instr);
202 state->progress = true;
203 }
204 break;
205
206 case GLSL_TYPE_SAMPLER:
207 case GLSL_TYPE_IMAGE:
208 case GLSL_TYPE_ATOMIC_UINT:
209 case GLSL_TYPE_INTERFACE:
210 default:
211 unreachable("Cannot copy these types");
212 }
213 }
214
215 static bool
216 split_var_copies_block(nir_block *block, void *void_state)
217 {
218 struct split_var_copies_state *state = void_state;
219
220 nir_foreach_instr_safe(block, instr) {
221 if (instr->type != nir_instr_type_intrinsic)
222 continue;
223
224 nir_intrinsic_instr *intrinsic = nir_instr_as_intrinsic(instr);
225 if (intrinsic->intrinsic != nir_intrinsic_copy_var)
226 continue;
227
228 nir_deref *dest_head = &intrinsic->variables[0]->deref;
229 nir_deref *src_head = &intrinsic->variables[1]->deref;
230 nir_deref *dest_tail = get_deref_tail(dest_head);
231 nir_deref *src_tail = get_deref_tail(src_head);
232
233 switch (glsl_get_base_type(src_tail->type)) {
234 case GLSL_TYPE_ARRAY:
235 case GLSL_TYPE_STRUCT:
236 split_var_copy_instr(intrinsic, dest_head, src_head,
237 dest_tail, src_tail, state);
238 nir_instr_remove(&intrinsic->instr);
239 ralloc_steal(state->dead_ctx, instr);
240 break;
241 case GLSL_TYPE_FLOAT:
242 case GLSL_TYPE_INT:
243 case GLSL_TYPE_UINT:
244 case GLSL_TYPE_BOOL:
245 if (glsl_type_is_matrix(src_tail->type)) {
246 split_var_copy_instr(intrinsic, dest_head, src_head,
247 dest_tail, src_tail, state);
248 nir_instr_remove(&intrinsic->instr);
249 ralloc_steal(state->dead_ctx, instr);
250 }
251 break;
252 default:
253 unreachable("Invalid type");
254 break;
255 }
256 }
257
258 return true;
259 }
260
261 static bool
262 split_var_copies_impl(nir_function_impl *impl)
263 {
264 struct split_var_copies_state state;
265
266 state.mem_ctx = ralloc_parent(impl);
267 state.dead_ctx = ralloc_context(NULL);
268 state.progress = false;
269
270 nir_foreach_block(impl, split_var_copies_block, &state);
271
272 ralloc_free(state.dead_ctx);
273
274 return state.progress;
275 }
276
277 bool
278 nir_split_var_copies(nir_shader *shader)
279 {
280 bool progress = false;
281
282 nir_foreach_overload(shader, overload) {
283 if (overload->impl)
284 progress = split_var_copies_impl(overload->impl) || progress;
285 }
286
287 return progress;
288 }