nir/algebraic: Make algebraic_parser_test.sh executable.
[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_builder.h"
30
31 struct locals_to_regs_state {
32 nir_builder builder;
33
34 /* A hash table mapping derefs to registers */
35 struct hash_table *regs_table;
36
37 bool progress;
38 };
39
40 /* The following two functions implement a hash and equality check for
41 * variable dreferences. When the hash or equality function encounters an
42 * array, it ignores the offset and whether it is direct or indirect
43 * entirely.
44 */
45 static uint32_t
46 hash_deref(const void *void_deref)
47 {
48 uint32_t hash = _mesa_fnv32_1a_offset_bias;
49
50 for (const nir_deref_instr *deref = void_deref; deref;
51 deref = nir_deref_instr_parent(deref)) {
52 switch (deref->deref_type) {
53 case nir_deref_type_var:
54 return _mesa_fnv32_1a_accumulate(hash, deref->var);
55
56 case nir_deref_type_array:
57 continue; /* Do nothing */
58
59 case nir_deref_type_struct:
60 hash = _mesa_fnv32_1a_accumulate(hash, deref->strct.index);
61 continue;
62
63 default:
64 unreachable("Invalid deref type");
65 }
66 }
67
68 unreachable("We should have hit a variable dereference");
69 }
70
71 static bool
72 derefs_equal(const void *void_a, const void *void_b)
73 {
74 for (const nir_deref_instr *a = void_a, *b = void_b; a || b;
75 a = nir_deref_instr_parent(a), b = nir_deref_instr_parent(b)) {
76 if (a->deref_type != b->deref_type)
77 return false;
78
79 switch (a->deref_type) {
80 case nir_deref_type_var:
81 return a->var == b->var;
82
83 case nir_deref_type_array:
84 continue; /* Do nothing */
85
86 case nir_deref_type_struct:
87 if (a->strct.index != b->strct.index)
88 return false;
89 continue;
90
91 default:
92 unreachable("Invalid deref type");
93 }
94 }
95
96 unreachable("We should have hit a variable dereference");
97 }
98
99 static nir_register *
100 get_reg_for_deref(nir_deref_instr *deref, struct locals_to_regs_state *state)
101 {
102 uint32_t hash = hash_deref(deref);
103
104 assert(nir_deref_instr_get_variable(deref)->constant_initializer == NULL);
105
106 struct hash_entry *entry =
107 _mesa_hash_table_search_pre_hashed(state->regs_table, hash, deref);
108 if (entry)
109 return entry->data;
110
111 unsigned array_size = 1;
112 for (nir_deref_instr *d = deref; d; d = nir_deref_instr_parent(d)) {
113 if (d->deref_type == nir_deref_type_array)
114 array_size *= glsl_get_length(nir_deref_instr_parent(d)->type);
115 }
116
117 assert(glsl_type_is_vector_or_scalar(deref->type));
118
119 nir_register *reg = nir_local_reg_create(state->builder.impl);
120 reg->num_components = glsl_get_vector_elements(deref->type);
121 reg->num_array_elems = array_size > 1 ? array_size : 0;
122 reg->bit_size = glsl_get_bit_size(deref->type);
123
124 _mesa_hash_table_insert_pre_hashed(state->regs_table, hash, deref, reg);
125
126 return reg;
127 }
128
129 static nir_src
130 get_deref_reg_src(nir_deref_instr *deref, struct locals_to_regs_state *state)
131 {
132 nir_builder *b = &state->builder;
133
134 nir_src src;
135
136 src.is_ssa = false;
137 src.reg.reg = get_reg_for_deref(deref, state);
138 src.reg.base_offset = 0;
139 src.reg.indirect = NULL;
140
141 /* It is possible for a user to create a shader that has an array with a
142 * single element and then proceed to access it indirectly. Indirectly
143 * accessing a non-array register is not allowed in NIR. In order to
144 * handle this case we just convert it to a direct reference.
145 */
146 if (src.reg.reg->num_array_elems == 0)
147 return src;
148
149 unsigned inner_array_size = 1;
150 for (const nir_deref_instr *d = deref; d; d = nir_deref_instr_parent(d)) {
151 if (d->deref_type != nir_deref_type_array)
152 continue;
153
154 if (nir_src_is_const(d->arr.index) && !src.reg.indirect) {
155 src.reg.base_offset += nir_src_as_uint(d->arr.index) *
156 inner_array_size;
157 } else {
158 if (src.reg.indirect) {
159 assert(src.reg.base_offset == 0);
160 } else {
161 src.reg.indirect = ralloc(b->shader, nir_src);
162 *src.reg.indirect =
163 nir_src_for_ssa(nir_imm_int(b, src.reg.base_offset));
164 src.reg.base_offset = 0;
165 }
166
167 assert(src.reg.indirect->is_ssa);
168 src.reg.indirect->ssa =
169 nir_iadd(b, src.reg.indirect->ssa,
170 nir_imul(b, nir_ssa_for_src(b, d->arr.index, 1),
171 nir_imm_int(b, inner_array_size)));
172 }
173
174 inner_array_size *= glsl_get_length(nir_deref_instr_parent(d)->type);
175 }
176
177 return src;
178 }
179
180 static bool
181 lower_locals_to_regs_block(nir_block *block,
182 struct locals_to_regs_state *state)
183 {
184 nir_builder *b = &state->builder;
185
186 nir_foreach_instr_safe(instr, block) {
187 if (instr->type != nir_instr_type_intrinsic)
188 continue;
189
190 nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
191
192 switch (intrin->intrinsic) {
193 case nir_intrinsic_load_deref: {
194 nir_deref_instr *deref = nir_src_as_deref(intrin->src[0]);
195 if (deref->mode != nir_var_local)
196 continue;
197
198 b->cursor = nir_before_instr(&intrin->instr);
199
200 nir_alu_instr *mov = nir_alu_instr_create(b->shader, nir_op_imov);
201 mov->src[0].src = get_deref_reg_src(deref, state);
202 mov->dest.write_mask = (1 << intrin->num_components) - 1;
203 if (intrin->dest.is_ssa) {
204 nir_ssa_dest_init(&mov->instr, &mov->dest.dest,
205 intrin->num_components,
206 intrin->dest.ssa.bit_size, NULL);
207 nir_ssa_def_rewrite_uses(&intrin->dest.ssa,
208 nir_src_for_ssa(&mov->dest.dest.ssa));
209 } else {
210 nir_dest_copy(&mov->dest.dest, &intrin->dest, &mov->instr);
211 }
212 nir_builder_instr_insert(b, &mov->instr);
213
214 nir_instr_remove(&intrin->instr);
215 state->progress = true;
216 break;
217 }
218
219 case nir_intrinsic_store_deref: {
220 nir_deref_instr *deref = nir_src_as_deref(intrin->src[0]);
221 if (deref->mode != nir_var_local)
222 continue;
223
224 b->cursor = nir_before_instr(&intrin->instr);
225
226 nir_src reg_src = get_deref_reg_src(deref, state);
227
228 nir_alu_instr *mov = nir_alu_instr_create(b->shader, nir_op_imov);
229 nir_src_copy(&mov->src[0].src, &intrin->src[1], mov);
230 mov->dest.write_mask = nir_intrinsic_write_mask(intrin);
231 mov->dest.dest.is_ssa = false;
232 mov->dest.dest.reg.reg = reg_src.reg.reg;
233 mov->dest.dest.reg.base_offset = reg_src.reg.base_offset;
234 mov->dest.dest.reg.indirect = reg_src.reg.indirect;
235
236 nir_builder_instr_insert(b, &mov->instr);
237
238 nir_instr_remove(&intrin->instr);
239 state->progress = true;
240 break;
241 }
242
243 case nir_intrinsic_copy_deref:
244 unreachable("There should be no copies whatsoever at this point");
245 break;
246
247 default:
248 continue;
249 }
250 }
251
252 return true;
253 }
254
255 static bool
256 nir_lower_locals_to_regs_impl(nir_function_impl *impl)
257 {
258 struct locals_to_regs_state state;
259
260 nir_builder_init(&state.builder, impl);
261 state.progress = false;
262 state.regs_table = _mesa_hash_table_create(NULL, hash_deref, derefs_equal);
263
264 nir_metadata_require(impl, nir_metadata_dominance);
265
266 nir_foreach_block(block, impl) {
267 lower_locals_to_regs_block(block, &state);
268 }
269
270 nir_metadata_preserve(impl, nir_metadata_block_index |
271 nir_metadata_dominance);
272
273 _mesa_hash_table_destroy(state.regs_table, NULL);
274
275 return state.progress;
276 }
277
278 bool
279 nir_lower_locals_to_regs(nir_shader *shader)
280 {
281 bool progress = false;
282
283 nir_foreach_function(function, shader) {
284 if (function->impl)
285 progress = nir_lower_locals_to_regs_impl(function->impl) || progress;
286 }
287
288 return progress;
289 }