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    nir_shader *shader;
66    void *dead_ctx;
67    bool progress;
68 };
69 
70 /* Recursively constructs deref chains to split a copy instruction into
71  * multiple (if needed) copy instructions with full-length deref chains.
72  * External callers of this function should pass the tail and head of the
73  * deref chains found as the source and destination of the copy instruction
74  * into this function.
75  *
76  * \param  old_copy  The copy instruction we are splitting
77  * \param  dest_head The head of the destination deref chain we are building
78  * \param  src_head  The head of the source deref chain we are building
79  * \param  dest_tail The tail of the destination deref chain we are building
80  * \param  src_tail  The tail of the source deref chain we are building
81  * \param  state     The current split_var_copies_state object
82  */
83 static void
split_var_copy_instr(nir_intrinsic_instr * old_copy,nir_deref_var * dest_head,nir_deref_var * src_head,nir_deref * dest_tail,nir_deref * src_tail,struct split_var_copies_state * state)84 split_var_copy_instr(nir_intrinsic_instr *old_copy,
85                      nir_deref_var *dest_head, nir_deref_var *src_head,
86                      nir_deref *dest_tail, nir_deref *src_tail,
87                      struct split_var_copies_state *state)
88 {
89    assert(src_tail->type == dest_tail->type);
90 
91    /* Make sure these really are the tails of the deref chains */
92    assert(dest_tail->child == NULL);
93    assert(src_tail->child == NULL);
94 
95    switch (glsl_get_base_type(src_tail->type)) {
96    case GLSL_TYPE_ARRAY: {
97       /* Make a wildcard dereference */
98       nir_deref_array *deref = nir_deref_array_create(state->dead_ctx);
99       deref->deref.type = glsl_get_array_element(src_tail->type);
100       deref->deref_array_type = nir_deref_array_type_wildcard;
101 
102       /* Set the tail of both as the newly created wildcard deref.  It is
103        * safe to use the same wildcard in both places because a) we will be
104        * copying it before we put it in an actual instruction and b)
105        * everything that will potentially add another link in the deref
106        * chain will also add the same thing to both chains.
107        */
108       src_tail->child = &deref->deref;
109       dest_tail->child = &deref->deref;
110 
111       split_var_copy_instr(old_copy, dest_head, src_head,
112                            dest_tail->child, src_tail->child, state);
113 
114       /* Set it back to the way we found it */
115       src_tail->child = NULL;
116       dest_tail->child = NULL;
117       break;
118    }
119 
120    case GLSL_TYPE_STRUCT:
121       /* This is the only part that actually does any interesting
122        * splitting.  For array types, we just use wildcards and resolve
123        * them later.  For structure types, we need to emit one copy
124        * instruction for every structure element.  Because we may have
125        * structs inside structs, we just recurse and let the next level
126        * take care of any additional structures.
127        */
128       for (unsigned i = 0; i < glsl_get_length(src_tail->type); i++) {
129          nir_deref_struct *deref = nir_deref_struct_create(state->dead_ctx, i);
130          deref->deref.type = glsl_get_struct_field(src_tail->type, i);
131 
132          /* Set the tail of both as the newly created structure deref.  It
133           * is safe to use the same wildcard in both places because a) we
134           * will be copying it before we put it in an actual instruction
135           * and b) everything that will potentially add another link in the
136           * deref chain will also add the same thing to both chains.
137           */
138          src_tail->child = &deref->deref;
139          dest_tail->child = &deref->deref;
140 
141          split_var_copy_instr(old_copy, dest_head, src_head,
142                               dest_tail->child, src_tail->child, state);
143       }
144       /* Set it back to the way we found it */
145       src_tail->child = NULL;
146       dest_tail->child = NULL;
147       break;
148 
149    case GLSL_TYPE_UINT:
150    case GLSL_TYPE_UINT16:
151    case GLSL_TYPE_UINT64:
152    case GLSL_TYPE_INT:
153    case GLSL_TYPE_INT16:
154    case GLSL_TYPE_INT64:
155    case GLSL_TYPE_FLOAT:
156    case GLSL_TYPE_FLOAT16:
157    case GLSL_TYPE_DOUBLE:
158    case GLSL_TYPE_BOOL:
159       if (glsl_type_is_matrix(src_tail->type)) {
160          nir_deref_array *deref = nir_deref_array_create(state->dead_ctx);
161          deref->deref.type = glsl_get_column_type(src_tail->type);
162          deref->deref_array_type = nir_deref_array_type_wildcard;
163 
164          /* Set the tail of both as the newly created wildcard deref.  It
165           * is safe to use the same wildcard in both places because a) we
166           * will be copying it before we put it in an actual instruction
167           * and b) everything that will potentially add another link in the
168           * deref chain will also add the same thing to both chains.
169           */
170          src_tail->child = &deref->deref;
171          dest_tail->child = &deref->deref;
172 
173          split_var_copy_instr(old_copy, dest_head, src_head,
174                               dest_tail->child, src_tail->child, state);
175 
176          /* Set it back to the way we found it */
177          src_tail->child = NULL;
178          dest_tail->child = NULL;
179       } else {
180          /* At this point, we have fully built our deref chains and can
181           * actually add the new copy instruction.
182           */
183          nir_intrinsic_instr *new_copy =
184             nir_intrinsic_instr_create(state->shader, nir_intrinsic_copy_var);
185 
186          /* We need to make copies because a) this deref chain actually
187           * belongs to the copy instruction and b) the deref chains may
188           * have some of the same links due to the way we constructed them
189           */
190          new_copy->variables[0] = nir_deref_var_clone(dest_head, new_copy);
191          new_copy->variables[1] = nir_deref_var_clone(src_head, new_copy);
192 
193          /* Emit the copy instruction after the old instruction.  We'll
194           * remove the old one later.
195           */
196          nir_instr_insert_after(&old_copy->instr, &new_copy->instr);
197          state->progress = true;
198       }
199       break;
200 
201    case GLSL_TYPE_SAMPLER:
202    case GLSL_TYPE_IMAGE:
203    case GLSL_TYPE_ATOMIC_UINT:
204    case GLSL_TYPE_INTERFACE:
205    default:
206       unreachable("Cannot copy these types");
207    }
208 }
209 
210 static bool
split_var_copies_block(nir_block * block,struct split_var_copies_state * state)211 split_var_copies_block(nir_block *block, struct split_var_copies_state *state)
212 {
213    nir_foreach_instr_safe(instr, block) {
214       if (instr->type != nir_instr_type_intrinsic)
215          continue;
216 
217       nir_intrinsic_instr *intrinsic = nir_instr_as_intrinsic(instr);
218       if (intrinsic->intrinsic != nir_intrinsic_copy_var)
219          continue;
220 
221       nir_deref_var *dest_head = intrinsic->variables[0];
222       nir_deref_var *src_head = intrinsic->variables[1];
223       nir_deref *dest_tail = nir_deref_tail(&dest_head->deref);
224       nir_deref *src_tail = nir_deref_tail(&src_head->deref);
225 
226       switch (glsl_get_base_type(src_tail->type)) {
227       case GLSL_TYPE_ARRAY:
228       case GLSL_TYPE_STRUCT:
229          split_var_copy_instr(intrinsic, dest_head, src_head,
230                               dest_tail, src_tail, state);
231          nir_instr_remove(&intrinsic->instr);
232          ralloc_steal(state->dead_ctx, instr);
233          break;
234       case GLSL_TYPE_FLOAT:
235       case GLSL_TYPE_FLOAT16:
236       case GLSL_TYPE_DOUBLE:
237          if (glsl_type_is_matrix(src_tail->type)) {
238             split_var_copy_instr(intrinsic, dest_head, src_head,
239                                  dest_tail, src_tail, state);
240             nir_instr_remove(&intrinsic->instr);
241             ralloc_steal(state->dead_ctx, instr);
242          }
243          break;
244       case GLSL_TYPE_INT:
245       case GLSL_TYPE_UINT:
246       case GLSL_TYPE_INT16:
247       case GLSL_TYPE_UINT16:
248       case GLSL_TYPE_INT64:
249       case GLSL_TYPE_UINT64:
250       case GLSL_TYPE_BOOL:
251          assert(!glsl_type_is_matrix(src_tail->type));
252          break;
253       default:
254          unreachable("Invalid type");
255          break;
256       }
257    }
258 
259    return true;
260 }
261 
262 static bool
split_var_copies_impl(nir_function_impl * impl)263 split_var_copies_impl(nir_function_impl *impl)
264 {
265    struct split_var_copies_state state;
266 
267    state.shader = impl->function->shader;
268    state.dead_ctx = ralloc_context(NULL);
269    state.progress = false;
270 
271    nir_foreach_block(block, impl) {
272       split_var_copies_block(block, &state);
273    }
274 
275    ralloc_free(state.dead_ctx);
276 
277    if (state.progress) {
278       nir_metadata_preserve(impl, nir_metadata_block_index |
279                                   nir_metadata_dominance);
280    }
281 
282    return state.progress;
283 }
284 
285 bool
nir_split_var_copies(nir_shader * shader)286 nir_split_var_copies(nir_shader *shader)
287 {
288    bool progress = false;
289 
290    nir_foreach_function(function, shader) {
291       if (function->impl)
292          progress = split_var_copies_impl(function->impl) || progress;
293    }
294 
295    return progress;
296 }
297