Merge commit '8b0fb1c152fe191768953aa8c77b89034a377f83' into vulkan
[mesa.git] / src / compiler / nir / nir_lower_locals_to_regs.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 #include "nir_array.h"
30
31 struct locals_to_regs_state {
32 nir_shader *shader;
33 nir_function_impl *impl;
34
35 /* A hash table mapping derefs to registers */
36 struct hash_table *regs_table;
37
38 /* A growing array of derefs that we have encountered. There is exactly
39 * one element of this array per element in the hash table. This is
40 * used to make adding register initialization code deterministic.
41 */
42 nir_array derefs_array;
43
44 bool progress;
45 };
46
47 /* The following two functions implement a hash and equality check for
48 * variable dreferences. When the hash or equality function encounters an
49 * array, it ignores the offset and whether it is direct or indirect
50 * entirely.
51 */
52 static uint32_t
53 hash_deref(const void *void_deref)
54 {
55 uint32_t hash = _mesa_fnv32_1a_offset_bias;
56
57 const nir_deref_var *deref_var = void_deref;
58 hash = _mesa_fnv32_1a_accumulate(hash, deref_var->var);
59
60 for (const nir_deref *deref = deref_var->deref.child;
61 deref; deref = deref->child) {
62 if (deref->deref_type == nir_deref_type_struct) {
63 const nir_deref_struct *deref_struct = nir_deref_as_struct(deref);
64 hash = _mesa_fnv32_1a_accumulate(hash, deref_struct->index);
65 }
66 }
67
68 return hash;
69 }
70
71 static bool
72 derefs_equal(const void *void_a, const void *void_b)
73 {
74 const nir_deref_var *a_var = void_a;
75 const nir_deref_var *b_var = void_b;
76
77 if (a_var->var != b_var->var)
78 return false;
79
80 for (const nir_deref *a = a_var->deref.child, *b = b_var->deref.child;
81 a != NULL; a = a->child, b = b->child) {
82 if (a->deref_type != b->deref_type)
83 return false;
84
85 if (a->deref_type == nir_deref_type_struct) {
86 if (nir_deref_as_struct(a)->index != nir_deref_as_struct(b)->index)
87 return false;
88 }
89 /* Do nothing for arrays. They're all the same. */
90
91 assert((a->child == NULL) == (b->child == NULL));
92 if((a->child == NULL) != (b->child == NULL))
93 return false;
94 }
95
96 return true;
97 }
98
99 static nir_register *
100 get_reg_for_deref(nir_deref_var *deref, struct locals_to_regs_state *state)
101 {
102 uint32_t hash = hash_deref(deref);
103
104 struct hash_entry *entry =
105 _mesa_hash_table_search_pre_hashed(state->regs_table, hash, deref);
106 if (entry)
107 return entry->data;
108
109 unsigned array_size = 1;
110 nir_deref *tail = &deref->deref;
111 while (tail->child) {
112 if (tail->child->deref_type == nir_deref_type_array)
113 array_size *= glsl_get_length(tail->type);
114 tail = tail->child;
115 }
116
117 assert(glsl_type_is_vector(tail->type) || glsl_type_is_scalar(tail->type));
118
119 nir_register *reg = nir_local_reg_create(state->impl);
120 reg->num_components = glsl_get_vector_elements(tail->type);
121 reg->num_array_elems = array_size > 1 ? array_size : 0;
122
123 _mesa_hash_table_insert_pre_hashed(state->regs_table, hash, deref, reg);
124 nir_array_add(&state->derefs_array, nir_deref_var *, deref);
125
126 return reg;
127 }
128
129 static nir_src
130 get_deref_reg_src(nir_deref_var *deref, nir_instr *instr,
131 struct locals_to_regs_state *state)
132 {
133 nir_src src;
134
135 src.is_ssa = false;
136 src.reg.reg = get_reg_for_deref(deref, state);
137 src.reg.base_offset = 0;
138 src.reg.indirect = NULL;
139
140 /* It is possible for a user to create a shader that has an array with a
141 * single element and then proceed to access it indirectly. Indirectly
142 * accessing a non-array register is not allowed in NIR. In order to
143 * handle this case we just convert it to a direct reference.
144 */
145 if (src.reg.reg->num_array_elems == 0)
146 return src;
147
148 nir_deref *tail = &deref->deref;
149 while (tail->child != NULL) {
150 const struct glsl_type *parent_type = tail->type;
151 tail = tail->child;
152
153 if (tail->deref_type != nir_deref_type_array)
154 continue;
155
156 nir_deref_array *deref_array = nir_deref_as_array(tail);
157
158 src.reg.base_offset *= glsl_get_length(parent_type);
159 src.reg.base_offset += deref_array->base_offset;
160
161 if (src.reg.indirect) {
162 nir_load_const_instr *load_const =
163 nir_load_const_instr_create(state->shader, 1);
164 load_const->value.u[0] = glsl_get_length(parent_type);
165 nir_instr_insert_before(instr, &load_const->instr);
166
167 nir_alu_instr *mul = nir_alu_instr_create(state->shader, nir_op_imul);
168 mul->src[0].src = *src.reg.indirect;
169 mul->src[1].src.is_ssa = true;
170 mul->src[1].src.ssa = &load_const->def;
171 mul->dest.write_mask = 1;
172 nir_ssa_dest_init(&mul->instr, &mul->dest.dest, 1, NULL);
173 nir_instr_insert_before(instr, &mul->instr);
174
175 src.reg.indirect->is_ssa = true;
176 src.reg.indirect->ssa = &mul->dest.dest.ssa;
177 }
178
179 if (deref_array->deref_array_type == nir_deref_array_type_indirect) {
180 if (src.reg.indirect == NULL) {
181 src.reg.indirect = ralloc(state->shader, nir_src);
182 nir_src_copy(src.reg.indirect, &deref_array->indirect,
183 state->shader);
184 } else {
185 nir_alu_instr *add = nir_alu_instr_create(state->shader,
186 nir_op_iadd);
187 add->src[0].src = *src.reg.indirect;
188 nir_src_copy(&add->src[1].src, &deref_array->indirect, add);
189 add->dest.write_mask = 1;
190 nir_ssa_dest_init(&add->instr, &add->dest.dest, 1, NULL);
191 nir_instr_insert_before(instr, &add->instr);
192
193 src.reg.indirect->is_ssa = true;
194 src.reg.indirect->ssa = &add->dest.dest.ssa;
195 }
196 }
197 }
198
199 return src;
200 }
201
202 static bool
203 lower_locals_to_regs_block(nir_block *block, void *void_state)
204 {
205 struct locals_to_regs_state *state = void_state;
206
207 nir_foreach_instr_safe(block, instr) {
208 if (instr->type != nir_instr_type_intrinsic)
209 continue;
210
211 nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
212
213 switch (intrin->intrinsic) {
214 case nir_intrinsic_load_var: {
215 if (intrin->variables[0]->var->data.mode != nir_var_local)
216 continue;
217
218 nir_alu_instr *mov = nir_alu_instr_create(state->shader, nir_op_imov);
219 mov->src[0].src = get_deref_reg_src(intrin->variables[0],
220 &intrin->instr, state);
221 mov->dest.write_mask = (1 << intrin->num_components) - 1;
222 if (intrin->dest.is_ssa) {
223 nir_ssa_dest_init(&mov->instr, &mov->dest.dest,
224 intrin->num_components, NULL);
225 nir_ssa_def_rewrite_uses(&intrin->dest.ssa,
226 nir_src_for_ssa(&mov->dest.dest.ssa));
227 } else {
228 nir_dest_copy(&mov->dest.dest, &intrin->dest, &mov->instr);
229 }
230 nir_instr_insert_before(&intrin->instr, &mov->instr);
231
232 nir_instr_remove(&intrin->instr);
233 state->progress = true;
234 break;
235 }
236
237 case nir_intrinsic_store_var: {
238 if (intrin->variables[0]->var->data.mode != nir_var_local)
239 continue;
240
241 nir_src reg_src = get_deref_reg_src(intrin->variables[0],
242 &intrin->instr, state);
243
244 nir_alu_instr *mov = nir_alu_instr_create(state->shader, nir_op_imov);
245 nir_src_copy(&mov->src[0].src, &intrin->src[0], mov);
246 mov->dest.write_mask = nir_intrinsic_write_mask(intrin);
247 mov->dest.dest.is_ssa = false;
248 mov->dest.dest.reg.reg = reg_src.reg.reg;
249 mov->dest.dest.reg.base_offset = reg_src.reg.base_offset;
250 mov->dest.dest.reg.indirect = reg_src.reg.indirect;
251
252 nir_instr_insert_before(&intrin->instr, &mov->instr);
253
254 nir_instr_remove(&intrin->instr);
255 state->progress = true;
256 break;
257 }
258
259 case nir_intrinsic_copy_var:
260 unreachable("There should be no copies whatsoever at this point");
261 break;
262
263 default:
264 continue;
265 }
266 }
267
268 return true;
269 }
270
271 static nir_block *
272 compute_reg_usedef_lca(nir_register *reg)
273 {
274 nir_block *lca = NULL;
275
276 list_for_each_entry(nir_dest, def_dest, &reg->defs, reg.def_link)
277 lca = nir_dominance_lca(lca, def_dest->reg.parent_instr->block);
278
279 list_for_each_entry(nir_src, use_src, &reg->uses, use_link)
280 lca = nir_dominance_lca(lca, use_src->parent_instr->block);
281
282 list_for_each_entry(nir_src, use_src, &reg->if_uses, use_link) {
283 nir_cf_node *prev_node = nir_cf_node_prev(&use_src->parent_if->cf_node);
284 assert(prev_node->type == nir_cf_node_block);
285 lca = nir_dominance_lca(lca, nir_cf_node_as_block(prev_node));
286 }
287
288 return lca;
289 }
290
291 static void
292 insert_constant_initializer(nir_deref_var *deref_head, nir_deref *deref_tail,
293 nir_block *block,
294 struct locals_to_regs_state *state)
295 {
296 if (deref_tail->child) {
297 switch (deref_tail->child->deref_type) {
298 case nir_deref_type_array: {
299 unsigned array_elems = glsl_get_length(deref_tail->type);
300
301 nir_deref_array arr_deref;
302 arr_deref.deref = *deref_tail->child;
303 arr_deref.deref_array_type = nir_deref_array_type_direct;
304
305 nir_deref *old_child = deref_tail->child;
306 deref_tail->child = &arr_deref.deref;
307 for (unsigned i = 0; i < array_elems; i++) {
308 arr_deref.base_offset = i;
309 insert_constant_initializer(deref_head, &arr_deref.deref,
310 block, state);
311 }
312 deref_tail->child = old_child;
313 return;
314 }
315
316 case nir_deref_type_struct:
317 insert_constant_initializer(deref_head, deref_tail->child,
318 block, state);
319 return;
320
321 default:
322 unreachable("Invalid deref child type");
323 }
324 }
325
326 assert(deref_tail->child == NULL);
327
328 nir_load_const_instr *load =
329 nir_deref_get_const_initializer_load(state->shader, deref_head);
330 nir_instr_insert_before_block(block, &load->instr);
331
332 nir_src reg_src = get_deref_reg_src(deref_head, &load->instr, state);
333
334 nir_alu_instr *mov = nir_alu_instr_create(state->shader, nir_op_imov);
335 mov->src[0].src = nir_src_for_ssa(&load->def);
336 mov->dest.write_mask = (1 << load->def.num_components) - 1;
337 mov->dest.dest.is_ssa = false;
338 mov->dest.dest.reg.reg = reg_src.reg.reg;
339 mov->dest.dest.reg.base_offset = reg_src.reg.base_offset;
340 mov->dest.dest.reg.indirect = reg_src.reg.indirect;
341
342 nir_instr_insert_after(&load->instr, &mov->instr);
343 state->progress = true;
344 }
345
346 static bool
347 nir_lower_locals_to_regs_impl(nir_function_impl *impl)
348 {
349 struct locals_to_regs_state state;
350
351 state.shader = impl->function->shader;
352 state.impl = impl;
353 state.progress = false;
354 state.regs_table = _mesa_hash_table_create(NULL, hash_deref, derefs_equal);
355 nir_array_init(&state.derefs_array, NULL);
356
357 nir_metadata_require(impl, nir_metadata_dominance);
358
359 nir_foreach_block(impl, lower_locals_to_regs_block, &state);
360
361 nir_array_foreach(&state.derefs_array, nir_deref_var *, deref_ptr) {
362 nir_deref_var *deref = *deref_ptr;
363 struct hash_entry *deref_entry =
364 _mesa_hash_table_search(state.regs_table, deref);
365 assert(deref_entry && deref_entry->key == deref);
366 nir_register *reg = (nir_register *)deref_entry->data;
367
368 if (deref->var->constant_initializer == NULL)
369 continue;
370
371 nir_block *usedef_lca = compute_reg_usedef_lca(reg);
372
373 insert_constant_initializer(deref, &deref->deref, usedef_lca, &state);
374 }
375
376 nir_metadata_preserve(impl, nir_metadata_block_index |
377 nir_metadata_dominance);
378
379 nir_array_fini(&state.derefs_array);
380 _mesa_hash_table_destroy(state.regs_table, NULL);
381
382 return state.progress;
383 }
384
385 bool
386 nir_lower_locals_to_regs(nir_shader *shader)
387 {
388 bool progress = false;
389
390 nir_foreach_function(shader, function) {
391 if (function->impl)
392 progress = nir_lower_locals_to_regs_impl(function->impl) || progress;
393 }
394
395 return progress;
396 }