nir: Add a deref path helper struct
[mesa.git] / src / compiler / nir / nir_deref.c
1 /*
2 * Copyright © 2018 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_deref.h"
27
28 void
29 nir_deref_path_init(nir_deref_path *path,
30 nir_deref_instr *deref, void *mem_ctx)
31 {
32 assert(deref != NULL);
33
34 /* The length of the short path is at most ARRAY_SIZE - 1 because we need
35 * room for the NULL terminator.
36 */
37 static const int max_short_path_len = ARRAY_SIZE(path->_short_path) - 1;
38
39 int count = 0;
40
41 nir_deref_instr **tail = &path->_short_path[max_short_path_len];
42 nir_deref_instr **head = tail;
43
44 *tail = NULL;
45 for (nir_deref_instr *d = deref; d; d = nir_deref_instr_parent(d)) {
46 count++;
47 if (count <= max_short_path_len)
48 *(--head) = d;
49 }
50
51 if (count <= max_short_path_len) {
52 /* If we're under max_short_path_len, just use the short path. */
53 path->path = head;
54 goto done;
55 }
56
57 #ifndef NDEBUG
58 /* Just in case someone uses short_path by accident */
59 for (unsigned i = 0; i < ARRAY_SIZE(path->_short_path); i++)
60 path->_short_path[i] = (void *)0xdeadbeef;
61 #endif
62
63 path->path = ralloc_array(mem_ctx, nir_deref_instr *, count + 1);
64 head = tail = path->path + count;
65 *tail = NULL;
66 for (nir_deref_instr *d = deref; d; d = nir_deref_instr_parent(d))
67 *(--head) = d;
68
69 done:
70 assert(head == path->path);
71 assert(tail == head + count);
72 assert((*head)->deref_type == nir_deref_type_var);
73 assert(*tail == NULL);
74 }
75
76 void
77 nir_deref_path_finish(nir_deref_path *path)
78 {
79 if (path->path < &path->_short_path[0] ||
80 path->path > &path->_short_path[ARRAY_SIZE(path->_short_path) - 1])
81 ralloc_free(path->path);
82 }
83
84 /**
85 * Recursively removes unused deref instructions
86 */
87 bool
88 nir_deref_instr_remove_if_unused(nir_deref_instr *instr)
89 {
90 bool progress = false;
91
92 for (nir_deref_instr *d = instr; d; d = nir_deref_instr_parent(d)) {
93 /* If anyone is using this deref, leave it alone */
94 assert(d->dest.is_ssa);
95 if (!list_empty(&d->dest.ssa.uses))
96 break;
97
98 nir_instr_remove(&d->instr);
99 progress = true;
100 }
101
102 return progress;
103 }
104
105 bool
106 nir_remove_dead_derefs_impl(nir_function_impl *impl)
107 {
108 bool progress = false;
109
110 nir_foreach_block(block, impl) {
111 nir_foreach_instr_safe(instr, block) {
112 if (instr->type == nir_instr_type_deref &&
113 nir_deref_instr_remove_if_unused(nir_instr_as_deref(instr)))
114 progress = true;
115 }
116 }
117
118 if (progress)
119 nir_metadata_preserve(impl, nir_metadata_block_index |
120 nir_metadata_dominance);
121
122 return progress;
123 }
124
125 bool
126 nir_remove_dead_derefs(nir_shader *shader)
127 {
128 bool progress = false;
129 nir_foreach_function(function, shader) {
130 if (function->impl && nir_remove_dead_derefs_impl(function->impl))
131 progress = true;
132 }
133
134 return progress;
135 }
136
137 nir_deref_var *
138 nir_deref_instr_to_deref(nir_deref_instr *instr, void *mem_ctx)
139 {
140 nir_deref *deref = NULL;
141
142 while (instr->deref_type != nir_deref_type_var) {
143 nir_deref *nderef;
144 switch (instr->deref_type) {
145 case nir_deref_type_array:
146 case nir_deref_type_array_wildcard: {
147 nir_deref_array *deref_arr = nir_deref_array_create(mem_ctx);
148 if (instr->deref_type == nir_deref_type_array) {
149 nir_const_value *const_index =
150 nir_src_as_const_value(instr->arr.index);
151 if (const_index) {
152 deref_arr->deref_array_type = nir_deref_array_type_direct;
153 deref_arr->base_offset = const_index->u32[0];
154 } else {
155 deref_arr->deref_array_type = nir_deref_array_type_indirect;
156 deref_arr->base_offset = 0;
157 nir_src_copy(&deref_arr->indirect, &instr->arr.index, mem_ctx);
158 }
159 } else {
160 deref_arr->deref_array_type = nir_deref_array_type_wildcard;
161 }
162 nderef = &deref_arr->deref;
163 break;
164 }
165
166 case nir_deref_type_struct:
167 nderef = &nir_deref_struct_create(mem_ctx, instr->strct.index)->deref;
168 break;
169
170 default:
171 unreachable("Invalid deref instruction type");
172 }
173
174 nderef->child = deref;
175 ralloc_steal(nderef, deref);
176 nderef->type = instr->type;
177
178 deref = nderef;
179 assert(instr->parent.is_ssa);
180 instr = nir_src_as_deref(instr->parent);
181 }
182
183 assert(instr->deref_type == nir_deref_type_var);
184 nir_deref_var *deref_var = nir_deref_var_create(mem_ctx, instr->var);
185 deref_var->deref.child = deref;
186 ralloc_steal(deref_var, deref);
187
188 return deref_var;
189 }
190
191 static nir_deref_var *
192 nir_deref_src_to_deref(nir_src src, void *mem_ctx)
193 {
194 return nir_deref_instr_to_deref(nir_src_as_deref(src), mem_ctx);
195 }
196
197 static bool
198 nir_lower_deref_instrs_tex(nir_tex_instr *tex)
199 {
200 bool progress = false;
201
202 /* Remove the instruction before we modify it. This way we won't mess up
203 * use-def chains when we move sources around.
204 */
205 nir_cursor cursor = nir_instr_remove(&tex->instr);
206
207 unsigned new_num_srcs = 0;
208 for (unsigned i = 0; i < tex->num_srcs; i++) {
209 if (tex->src[i].src_type == nir_tex_src_texture_deref) {
210 tex->texture = nir_deref_src_to_deref(tex->src[i].src, tex);
211 progress = true;
212 continue;
213 } else if (tex->src[i].src_type == nir_tex_src_sampler_deref) {
214 tex->sampler = nir_deref_src_to_deref(tex->src[i].src, tex);
215 progress = true;
216 continue;
217 }
218
219 /* Compact the sources down to remove the deref sources */
220 assert(new_num_srcs <= i);
221 tex->src[new_num_srcs++] = tex->src[i];
222 }
223 tex->num_srcs = new_num_srcs;
224
225 nir_instr_insert(cursor, &tex->instr);
226
227 return progress;
228 }
229
230 static bool
231 nir_lower_deref_instrs_intrin(nir_intrinsic_instr *intrin,
232 enum nir_lower_deref_flags flags)
233 {
234 nir_intrinsic_op deref_op = intrin->intrinsic;
235 nir_intrinsic_op var_op;
236
237 switch (deref_op) {
238 #define CASE(a) \
239 case nir_intrinsic_##a##_deref: \
240 if (!(flags & nir_lower_load_store_derefs)) \
241 return false; \
242 var_op = nir_intrinsic_##a##_var; \
243 break;
244 CASE(load)
245 CASE(store)
246 CASE(copy)
247 #undef CASE
248
249 #define CASE(a) \
250 case nir_intrinsic_interp_deref_##a: \
251 if (!(flags & nir_lower_interp_derefs)) \
252 return false; \
253 var_op = nir_intrinsic_interp_var_##a; \
254 break;
255 CASE(at_centroid)
256 CASE(at_sample)
257 CASE(at_offset)
258 #undef CASE
259
260 #define CASE(a) \
261 case nir_intrinsic_atomic_counter_##a##_deref: \
262 if (!(flags & nir_lower_atomic_counter_derefs)) \
263 return false; \
264 var_op = nir_intrinsic_atomic_counter_##a##_var; \
265 break;
266 CASE(inc)
267 CASE(dec)
268 CASE(read)
269 CASE(add)
270 CASE(min)
271 CASE(max)
272 CASE(and)
273 CASE(or)
274 CASE(xor)
275 CASE(exchange)
276 CASE(comp_swap)
277 #undef CASE
278
279 #define CASE(a) \
280 case nir_intrinsic_deref_atomic_##a: \
281 if (!(flags & nir_lower_atomic_derefs)) \
282 return false; \
283 var_op = nir_intrinsic_var_atomic_##a; \
284 break;
285 CASE(add)
286 CASE(imin)
287 CASE(umin)
288 CASE(imax)
289 CASE(umax)
290 CASE(and)
291 CASE(or)
292 CASE(xor)
293 CASE(exchange)
294 CASE(comp_swap)
295 #undef CASE
296
297 #define CASE(a) \
298 case nir_intrinsic_image_deref_##a: \
299 if (!(flags & nir_lower_image_derefs)) \
300 return false; \
301 var_op = nir_intrinsic_image_var_##a; \
302 break;
303 CASE(load)
304 CASE(store)
305 CASE(atomic_add)
306 CASE(atomic_min)
307 CASE(atomic_max)
308 CASE(atomic_and)
309 CASE(atomic_or)
310 CASE(atomic_xor)
311 CASE(atomic_exchange)
312 CASE(atomic_comp_swap)
313 CASE(size)
314 CASE(samples)
315 #undef CASE
316
317 default:
318 return false;
319 }
320
321 /* Remove the instruction before we modify it. This way we won't mess up
322 * use-def chains when we move sources around.
323 */
324 nir_cursor cursor = nir_instr_remove(&intrin->instr);
325
326 unsigned num_derefs = nir_intrinsic_infos[var_op].num_variables;
327 assert(nir_intrinsic_infos[var_op].num_srcs + num_derefs ==
328 nir_intrinsic_infos[deref_op].num_srcs);
329
330 /* Move deref sources to variables */
331 for (unsigned i = 0; i < num_derefs; i++)
332 intrin->variables[i] = nir_deref_src_to_deref(intrin->src[i], intrin);
333
334 /* Shift all the other sources down */
335 for (unsigned i = 0; i < nir_intrinsic_infos[var_op].num_srcs; i++)
336 nir_src_copy(&intrin->src[i], &intrin->src[i + num_derefs], intrin);
337
338 /* Rewrite the extra sources to NIR_SRC_INIT just in case */
339 for (unsigned i = 0; i < num_derefs; i++)
340 intrin->src[nir_intrinsic_infos[var_op].num_srcs + i] = NIR_SRC_INIT;
341
342 /* It's safe to just stomp the intrinsic to var intrinsic since every
343 * intrinsic has room for some variables and the number of sources only
344 * shrinks.
345 */
346 intrin->intrinsic = var_op;
347
348 nir_instr_insert(cursor, &intrin->instr);
349
350 return true;
351 }
352
353 static bool
354 nir_lower_deref_instrs_impl(nir_function_impl *impl,
355 enum nir_lower_deref_flags flags)
356 {
357 bool progress = false;
358
359 /* Walk the instructions in reverse order so that we can safely clean up
360 * the deref instructions after we clean up their uses.
361 */
362 nir_foreach_block_reverse(block, impl) {
363 nir_foreach_instr_reverse_safe(instr, block) {
364 switch (instr->type) {
365 case nir_instr_type_deref:
366 if (list_empty(&nir_instr_as_deref(instr)->dest.ssa.uses)) {
367 nir_instr_remove(instr);
368 progress = true;
369 }
370 break;
371
372 case nir_instr_type_tex:
373 if (flags & nir_lower_texture_derefs)
374 progress |= nir_lower_deref_instrs_tex(nir_instr_as_tex(instr));
375 break;
376
377 case nir_instr_type_intrinsic:
378 progress |=
379 nir_lower_deref_instrs_intrin(nir_instr_as_intrinsic(instr),
380 flags);
381 break;
382
383 default:
384 break; /* Nothing to do */
385 }
386 }
387 }
388
389 if (progress) {
390 nir_metadata_preserve(impl, nir_metadata_block_index |
391 nir_metadata_dominance);
392 }
393
394 return progress;
395 }
396
397 bool
398 nir_lower_deref_instrs(nir_shader *shader,
399 enum nir_lower_deref_flags flags)
400 {
401 bool progress = false;
402
403 nir_foreach_function(function, shader) {
404 if (!function->impl)
405 continue;
406
407 progress |= nir_lower_deref_instrs_impl(function->impl, flags);
408 }
409
410 shader->lowered_derefs |= flags;
411
412 return progress;
413 }
414
415 void
416 nir_fixup_deref_modes(nir_shader *shader)
417 {
418 nir_foreach_function(function, shader) {
419 if (!function->impl)
420 continue;
421
422 nir_foreach_block(block, function->impl) {
423 nir_foreach_instr(instr, block) {
424 if (instr->type != nir_instr_type_deref)
425 continue;
426
427 nir_deref_instr *deref = nir_instr_as_deref(instr);
428
429 nir_variable_mode parent_mode;
430 if (deref->deref_type == nir_deref_type_var) {
431 parent_mode = deref->var->data.mode;
432 } else {
433 assert(deref->parent.is_ssa);
434 nir_deref_instr *parent =
435 nir_instr_as_deref(deref->parent.ssa->parent_instr);
436 parent_mode = parent->mode;
437 }
438
439 deref->mode = parent_mode;
440 }
441 }
442 }
443 }