83c401282c551cf01f79c99d2259970c9d9bad91
[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 #include "util/hash_table.h"
28
29 static bool
30 is_trivial_deref_cast(nir_deref_instr *cast)
31 {
32 nir_deref_instr *parent = nir_src_as_deref(cast->parent);
33 if (!parent)
34 return false;
35
36 return cast->mode == parent->mode &&
37 cast->type == parent->type &&
38 cast->dest.ssa.num_components == parent->dest.ssa.num_components &&
39 cast->dest.ssa.bit_size == parent->dest.ssa.bit_size;
40 }
41
42 void
43 nir_deref_path_init(nir_deref_path *path,
44 nir_deref_instr *deref, void *mem_ctx)
45 {
46 assert(deref != NULL);
47
48 /* The length of the short path is at most ARRAY_SIZE - 1 because we need
49 * room for the NULL terminator.
50 */
51 static const int max_short_path_len = ARRAY_SIZE(path->_short_path) - 1;
52
53 int count = 0;
54
55 nir_deref_instr **tail = &path->_short_path[max_short_path_len];
56 nir_deref_instr **head = tail;
57
58 *tail = NULL;
59 for (nir_deref_instr *d = deref; d; d = nir_deref_instr_parent(d)) {
60 if (d->deref_type == nir_deref_type_cast && is_trivial_deref_cast(d))
61 continue;
62 count++;
63 if (count <= max_short_path_len)
64 *(--head) = d;
65 }
66
67 if (count <= max_short_path_len) {
68 /* If we're under max_short_path_len, just use the short path. */
69 path->path = head;
70 goto done;
71 }
72
73 #ifndef NDEBUG
74 /* Just in case someone uses short_path by accident */
75 for (unsigned i = 0; i < ARRAY_SIZE(path->_short_path); i++)
76 path->_short_path[i] = (void *)(uintptr_t)0xdeadbeef;
77 #endif
78
79 path->path = ralloc_array(mem_ctx, nir_deref_instr *, count + 1);
80 head = tail = path->path + count;
81 *tail = NULL;
82 for (nir_deref_instr *d = deref; d; d = nir_deref_instr_parent(d)) {
83 if (d->deref_type == nir_deref_type_cast && is_trivial_deref_cast(d))
84 continue;
85 *(--head) = d;
86 }
87
88 done:
89 assert(head == path->path);
90 assert(tail == head + count);
91 assert(*tail == NULL);
92 }
93
94 void
95 nir_deref_path_finish(nir_deref_path *path)
96 {
97 if (path->path < &path->_short_path[0] ||
98 path->path > &path->_short_path[ARRAY_SIZE(path->_short_path) - 1])
99 ralloc_free(path->path);
100 }
101
102 /**
103 * Recursively removes unused deref instructions
104 */
105 bool
106 nir_deref_instr_remove_if_unused(nir_deref_instr *instr)
107 {
108 bool progress = false;
109
110 for (nir_deref_instr *d = instr; d; d = nir_deref_instr_parent(d)) {
111 /* If anyone is using this deref, leave it alone */
112 assert(d->dest.is_ssa);
113 if (!list_is_empty(&d->dest.ssa.uses))
114 break;
115
116 nir_instr_remove(&d->instr);
117 progress = true;
118 }
119
120 return progress;
121 }
122
123 bool
124 nir_deref_instr_has_indirect(nir_deref_instr *instr)
125 {
126 while (instr->deref_type != nir_deref_type_var) {
127 /* Consider casts to be indirects */
128 if (instr->deref_type == nir_deref_type_cast)
129 return true;
130
131 if ((instr->deref_type == nir_deref_type_array ||
132 instr->deref_type == nir_deref_type_ptr_as_array) &&
133 !nir_src_is_const(instr->arr.index))
134 return true;
135
136 instr = nir_deref_instr_parent(instr);
137 }
138
139 return false;
140 }
141
142 bool
143 nir_deref_instr_is_known_out_of_bounds(nir_deref_instr *instr)
144 {
145 for (; instr; instr = nir_deref_instr_parent(instr)) {
146 if (instr->deref_type == nir_deref_type_array &&
147 nir_src_is_const(instr->arr.index) &&
148 nir_src_as_uint(instr->arr.index) >=
149 glsl_get_length(nir_deref_instr_parent(instr)->type))
150 return true;
151 }
152
153 return false;
154 }
155
156 bool
157 nir_deref_instr_has_complex_use(nir_deref_instr *deref)
158 {
159 nir_foreach_use(use_src, &deref->dest.ssa) {
160 nir_instr *use_instr = use_src->parent_instr;
161
162 switch (use_instr->type) {
163 case nir_instr_type_deref: {
164 nir_deref_instr *use_deref = nir_instr_as_deref(use_instr);
165
166 /* A var deref has no sources */
167 assert(use_deref->deref_type != nir_deref_type_var);
168
169 /* If a deref shows up in an array index or something like that, it's
170 * a complex use.
171 */
172 if (use_src != &use_deref->parent)
173 return true;
174
175 /* Anything that isn't a basic struct or array deref is considered to
176 * be a "complex" use. In particular, we don't allow ptr_as_array
177 * because we assume that opt_deref will turn any non-complex
178 * ptr_as_array derefs into regular array derefs eventually so passes
179 * which only want to handle simple derefs will pick them up in a
180 * later pass.
181 */
182 if (use_deref->deref_type != nir_deref_type_struct &&
183 use_deref->deref_type != nir_deref_type_array_wildcard &&
184 use_deref->deref_type != nir_deref_type_array)
185 return true;
186
187 if (nir_deref_instr_has_complex_use(use_deref))
188 return true;
189
190 continue;
191 }
192
193 case nir_instr_type_intrinsic: {
194 nir_intrinsic_instr *use_intrin = nir_instr_as_intrinsic(use_instr);
195 switch (use_intrin->intrinsic) {
196 case nir_intrinsic_load_deref:
197 assert(use_src == &use_intrin->src[0]);
198 continue;
199
200 case nir_intrinsic_copy_deref:
201 assert(use_src == &use_intrin->src[0] ||
202 use_src == &use_intrin->src[1]);
203 continue;
204
205 case nir_intrinsic_store_deref:
206 /* A use in src[1] of a store means we're taking that pointer and
207 * writing it to a variable. Because we have no idea who will
208 * read that variable and what they will do with the pointer, it's
209 * considered a "complex" use. A use in src[0], on the other
210 * hand, is a simple use because we're just going to dereference
211 * it and write a value there.
212 */
213 if (use_src == &use_intrin->src[0])
214 continue;
215 return true;
216
217 default:
218 return true;
219 }
220 unreachable("Switch default failed");
221 }
222
223 default:
224 return true;
225 }
226 }
227
228 nir_foreach_if_use(use, &deref->dest.ssa)
229 return true;
230
231 return false;
232 }
233
234 unsigned
235 nir_deref_instr_ptr_as_array_stride(nir_deref_instr *deref)
236 {
237 switch (deref->deref_type) {
238 case nir_deref_type_array:
239 return glsl_get_explicit_stride(nir_deref_instr_parent(deref)->type);
240 case nir_deref_type_ptr_as_array:
241 return nir_deref_instr_ptr_as_array_stride(nir_deref_instr_parent(deref));
242 case nir_deref_type_cast:
243 return deref->cast.ptr_stride;
244 default:
245 return 0;
246 }
247 }
248
249 static unsigned
250 type_get_array_stride(const struct glsl_type *elem_type,
251 glsl_type_size_align_func size_align)
252 {
253 unsigned elem_size, elem_align;
254 size_align(elem_type, &elem_size, &elem_align);
255 return ALIGN_POT(elem_size, elem_align);
256 }
257
258 static unsigned
259 struct_type_get_field_offset(const struct glsl_type *struct_type,
260 glsl_type_size_align_func size_align,
261 unsigned field_idx)
262 {
263 assert(glsl_type_is_struct_or_ifc(struct_type));
264 unsigned offset = 0;
265 for (unsigned i = 0; i <= field_idx; i++) {
266 unsigned elem_size, elem_align;
267 size_align(glsl_get_struct_field(struct_type, i), &elem_size, &elem_align);
268 offset = ALIGN_POT(offset, elem_align);
269 if (i < field_idx)
270 offset += elem_size;
271 }
272 return offset;
273 }
274
275 unsigned
276 nir_deref_instr_get_const_offset(nir_deref_instr *deref,
277 glsl_type_size_align_func size_align)
278 {
279 nir_deref_path path;
280 nir_deref_path_init(&path, deref, NULL);
281
282 assert(path.path[0]->deref_type == nir_deref_type_var);
283
284 unsigned offset = 0;
285 for (nir_deref_instr **p = &path.path[1]; *p; p++) {
286 switch ((*p)->deref_type) {
287 case nir_deref_type_array:
288 offset += nir_src_as_uint((*p)->arr.index) *
289 type_get_array_stride((*p)->type, size_align);
290 break;
291 case nir_deref_type_struct: {
292 /* p starts at path[1], so this is safe */
293 nir_deref_instr *parent = *(p - 1);
294 offset += struct_type_get_field_offset(parent->type, size_align,
295 (*p)->strct.index);
296 break;
297 }
298 default:
299 unreachable("Unsupported deref type");
300 }
301 }
302
303 nir_deref_path_finish(&path);
304
305 return offset;
306 }
307
308 nir_ssa_def *
309 nir_build_deref_offset(nir_builder *b, nir_deref_instr *deref,
310 glsl_type_size_align_func size_align)
311 {
312 nir_deref_path path;
313 nir_deref_path_init(&path, deref, NULL);
314
315 assert(path.path[0]->deref_type == nir_deref_type_var);
316
317 nir_ssa_def *offset = nir_imm_intN_t(b, 0, deref->dest.ssa.bit_size);
318 for (nir_deref_instr **p = &path.path[1]; *p; p++) {
319 switch ((*p)->deref_type) {
320 case nir_deref_type_array: {
321 nir_ssa_def *index = nir_ssa_for_src(b, (*p)->arr.index, 1);
322 int stride = type_get_array_stride((*p)->type, size_align);
323 offset = nir_iadd(b, offset, nir_amul_imm(b, index, stride));
324 break;
325 }
326 case nir_deref_type_struct: {
327 /* p starts at path[1], so this is safe */
328 nir_deref_instr *parent = *(p - 1);
329 unsigned field_offset =
330 struct_type_get_field_offset(parent->type, size_align,
331 (*p)->strct.index);
332 offset = nir_iadd_imm(b, offset, field_offset);
333 break;
334 }
335 default:
336 unreachable("Unsupported deref type");
337 }
338 }
339
340 nir_deref_path_finish(&path);
341
342 return offset;
343 }
344
345 bool
346 nir_remove_dead_derefs_impl(nir_function_impl *impl)
347 {
348 bool progress = false;
349
350 nir_foreach_block(block, impl) {
351 nir_foreach_instr_safe(instr, block) {
352 if (instr->type == nir_instr_type_deref &&
353 nir_deref_instr_remove_if_unused(nir_instr_as_deref(instr)))
354 progress = true;
355 }
356 }
357
358 if (progress)
359 nir_metadata_preserve(impl, nir_metadata_block_index |
360 nir_metadata_dominance);
361
362 return progress;
363 }
364
365 bool
366 nir_remove_dead_derefs(nir_shader *shader)
367 {
368 bool progress = false;
369 nir_foreach_function(function, shader) {
370 if (function->impl && nir_remove_dead_derefs_impl(function->impl))
371 progress = true;
372 }
373
374 return progress;
375 }
376
377 void
378 nir_fixup_deref_modes(nir_shader *shader)
379 {
380 nir_foreach_function(function, shader) {
381 if (!function->impl)
382 continue;
383
384 nir_foreach_block(block, function->impl) {
385 nir_foreach_instr(instr, block) {
386 if (instr->type != nir_instr_type_deref)
387 continue;
388
389 nir_deref_instr *deref = nir_instr_as_deref(instr);
390 if (deref->deref_type == nir_deref_type_cast)
391 continue;
392
393 nir_variable_mode parent_mode;
394 if (deref->deref_type == nir_deref_type_var) {
395 parent_mode = deref->var->data.mode;
396 } else {
397 assert(deref->parent.is_ssa);
398 nir_deref_instr *parent =
399 nir_instr_as_deref(deref->parent.ssa->parent_instr);
400 parent_mode = parent->mode;
401 }
402
403 deref->mode = parent_mode;
404 }
405 }
406 }
407 }
408
409 static bool
410 modes_may_alias(nir_variable_mode a, nir_variable_mode b)
411 {
412 /* Generic pointers can alias with SSBOs */
413 if ((a == nir_var_mem_ssbo || a == nir_var_mem_global) &&
414 (b == nir_var_mem_ssbo || b == nir_var_mem_global))
415 return true;
416
417 /* In the general case, pointers can only alias if they have the same mode.
418 *
419 * NOTE: In future, with things like OpenCL generic pointers, this may not
420 * be true and will have to be re-evaluated. However, with graphics only,
421 * it should be safe.
422 */
423 return a == b;
424 }
425
426 static bool
427 deref_path_contains_coherent_decoration(nir_deref_path *path)
428 {
429 assert(path->path[0]->deref_type == nir_deref_type_var);
430
431 if (path->path[0]->var->data.access & ACCESS_COHERENT)
432 return true;
433
434 for (nir_deref_instr **p = &path->path[1]; *p; p++) {
435 if ((*p)->deref_type != nir_deref_type_struct)
436 continue;
437
438 const struct glsl_type *struct_type = (*(p - 1))->type;
439 const struct glsl_struct_field *field =
440 glsl_get_struct_field_data(struct_type, (*p)->strct.index);
441 if (field->memory_coherent)
442 return true;
443 }
444
445 return false;
446 }
447
448 nir_deref_compare_result
449 nir_compare_deref_paths(nir_deref_path *a_path,
450 nir_deref_path *b_path)
451 {
452 if (!modes_may_alias(b_path->path[0]->mode, a_path->path[0]->mode))
453 return nir_derefs_do_not_alias;
454
455 if (a_path->path[0]->deref_type != b_path->path[0]->deref_type)
456 return nir_derefs_may_alias_bit;
457
458 if (a_path->path[0]->deref_type == nir_deref_type_var) {
459 if (a_path->path[0]->var != b_path->path[0]->var) {
460 /* Shader and function temporaries aren't backed by memory so two
461 * distinct variables never alias.
462 */
463 static const nir_variable_mode temp_var_modes =
464 nir_var_shader_temp | nir_var_function_temp;
465 if ((a_path->path[0]->mode & temp_var_modes) ||
466 (b_path->path[0]->mode & temp_var_modes))
467 return nir_derefs_do_not_alias;
468
469 /* If they are both declared coherent or have coherent somewhere in
470 * their path (due to a member of an interface being declared
471 * coherent), we have to assume we that we could have any kind of
472 * aliasing. Otherwise, they could still alias but the client didn't
473 * tell us and that's their fault.
474 */
475 if (deref_path_contains_coherent_decoration(a_path) &&
476 deref_path_contains_coherent_decoration(b_path))
477 return nir_derefs_may_alias_bit;
478
479 /* If we can chase the deref all the way back to the variable and
480 * they're not the same variable and at least one is not declared
481 * coherent, we know they can't possibly alias.
482 */
483 return nir_derefs_do_not_alias;
484 }
485 } else {
486 assert(a_path->path[0]->deref_type == nir_deref_type_cast);
487 /* If they're not exactly the same cast, it's hard to compare them so we
488 * just assume they alias. Comparing casts is tricky as there are lots
489 * of things such as mode, type, etc. to make sure work out; for now, we
490 * just assume nit_opt_deref will combine them and compare the deref
491 * instructions.
492 *
493 * TODO: At some point in the future, we could be clever and understand
494 * that a float[] and int[] have the same layout and aliasing structure
495 * but double[] and vec3[] do not and we could potentially be a bit
496 * smarter here.
497 */
498 if (a_path->path[0] != b_path->path[0])
499 return nir_derefs_may_alias_bit;
500 }
501
502 /* Start off assuming they fully compare. We ignore equality for now. In
503 * the end, we'll determine that by containment.
504 */
505 nir_deref_compare_result result = nir_derefs_may_alias_bit |
506 nir_derefs_a_contains_b_bit |
507 nir_derefs_b_contains_a_bit;
508
509 nir_deref_instr **a_p = &a_path->path[1];
510 nir_deref_instr **b_p = &b_path->path[1];
511 while (*a_p != NULL && *a_p == *b_p) {
512 a_p++;
513 b_p++;
514 }
515
516 /* We're at either the tail or the divergence point between the two deref
517 * paths. Look to see if either contains a ptr_as_array deref. It it
518 * does we don't know how to safely make any inferences. Hopefully,
519 * nir_opt_deref will clean most of these up and we can start inferring
520 * things again.
521 *
522 * In theory, we could do a bit better. For instance, we could detect the
523 * case where we have exactly one ptr_as_array deref in the chain after the
524 * divergence point and it's matched in both chains and the two chains have
525 * different constant indices.
526 */
527 for (nir_deref_instr **t_p = a_p; *t_p; t_p++) {
528 if ((*t_p)->deref_type == nir_deref_type_ptr_as_array)
529 return nir_derefs_may_alias_bit;
530 }
531 for (nir_deref_instr **t_p = b_p; *t_p; t_p++) {
532 if ((*t_p)->deref_type == nir_deref_type_ptr_as_array)
533 return nir_derefs_may_alias_bit;
534 }
535
536 while (*a_p != NULL && *b_p != NULL) {
537 nir_deref_instr *a_tail = *(a_p++);
538 nir_deref_instr *b_tail = *(b_p++);
539
540 switch (a_tail->deref_type) {
541 case nir_deref_type_array:
542 case nir_deref_type_array_wildcard: {
543 assert(b_tail->deref_type == nir_deref_type_array ||
544 b_tail->deref_type == nir_deref_type_array_wildcard);
545
546 if (a_tail->deref_type == nir_deref_type_array_wildcard) {
547 if (b_tail->deref_type != nir_deref_type_array_wildcard)
548 result &= ~nir_derefs_b_contains_a_bit;
549 } else if (b_tail->deref_type == nir_deref_type_array_wildcard) {
550 if (a_tail->deref_type != nir_deref_type_array_wildcard)
551 result &= ~nir_derefs_a_contains_b_bit;
552 } else {
553 assert(a_tail->deref_type == nir_deref_type_array &&
554 b_tail->deref_type == nir_deref_type_array);
555 assert(a_tail->arr.index.is_ssa && b_tail->arr.index.is_ssa);
556
557 if (nir_src_is_const(a_tail->arr.index) &&
558 nir_src_is_const(b_tail->arr.index)) {
559 /* If they're both direct and have different offsets, they
560 * don't even alias much less anything else.
561 */
562 if (nir_src_as_uint(a_tail->arr.index) !=
563 nir_src_as_uint(b_tail->arr.index))
564 return nir_derefs_do_not_alias;
565 } else if (a_tail->arr.index.ssa == b_tail->arr.index.ssa) {
566 /* They're the same indirect, continue on */
567 } else {
568 /* They're not the same index so we can't prove anything about
569 * containment.
570 */
571 result &= ~(nir_derefs_a_contains_b_bit | nir_derefs_b_contains_a_bit);
572 }
573 }
574 break;
575 }
576
577 case nir_deref_type_struct: {
578 /* If they're different struct members, they don't even alias */
579 if (a_tail->strct.index != b_tail->strct.index)
580 return nir_derefs_do_not_alias;
581 break;
582 }
583
584 default:
585 unreachable("Invalid deref type");
586 }
587 }
588
589 /* If a is longer than b, then it can't contain b */
590 if (*a_p != NULL)
591 result &= ~nir_derefs_a_contains_b_bit;
592 if (*b_p != NULL)
593 result &= ~nir_derefs_b_contains_a_bit;
594
595 /* If a contains b and b contains a they must be equal. */
596 if ((result & nir_derefs_a_contains_b_bit) && (result & nir_derefs_b_contains_a_bit))
597 result |= nir_derefs_equal_bit;
598
599 return result;
600 }
601
602 nir_deref_compare_result
603 nir_compare_derefs(nir_deref_instr *a, nir_deref_instr *b)
604 {
605 if (a == b) {
606 return nir_derefs_equal_bit | nir_derefs_may_alias_bit |
607 nir_derefs_a_contains_b_bit | nir_derefs_b_contains_a_bit;
608 }
609
610 nir_deref_path a_path, b_path;
611 nir_deref_path_init(&a_path, a, NULL);
612 nir_deref_path_init(&b_path, b, NULL);
613 assert(a_path.path[0]->deref_type == nir_deref_type_var ||
614 a_path.path[0]->deref_type == nir_deref_type_cast);
615 assert(b_path.path[0]->deref_type == nir_deref_type_var ||
616 b_path.path[0]->deref_type == nir_deref_type_cast);
617
618 nir_deref_compare_result result = nir_compare_deref_paths(&a_path, &b_path);
619
620 nir_deref_path_finish(&a_path);
621 nir_deref_path_finish(&b_path);
622
623 return result;
624 }
625
626 struct rematerialize_deref_state {
627 bool progress;
628 nir_builder builder;
629 nir_block *block;
630 struct hash_table *cache;
631 };
632
633 static nir_deref_instr *
634 rematerialize_deref_in_block(nir_deref_instr *deref,
635 struct rematerialize_deref_state *state)
636 {
637 if (deref->instr.block == state->block)
638 return deref;
639
640 if (!state->cache) {
641 state->cache = _mesa_pointer_hash_table_create(NULL);
642 }
643
644 struct hash_entry *cached = _mesa_hash_table_search(state->cache, deref);
645 if (cached)
646 return cached->data;
647
648 nir_builder *b = &state->builder;
649 nir_deref_instr *new_deref =
650 nir_deref_instr_create(b->shader, deref->deref_type);
651 new_deref->mode = deref->mode;
652 new_deref->type = deref->type;
653
654 if (deref->deref_type == nir_deref_type_var) {
655 new_deref->var = deref->var;
656 } else {
657 nir_deref_instr *parent = nir_src_as_deref(deref->parent);
658 if (parent) {
659 parent = rematerialize_deref_in_block(parent, state);
660 new_deref->parent = nir_src_for_ssa(&parent->dest.ssa);
661 } else {
662 nir_src_copy(&new_deref->parent, &deref->parent, new_deref);
663 }
664 }
665
666 switch (deref->deref_type) {
667 case nir_deref_type_var:
668 case nir_deref_type_array_wildcard:
669 /* Nothing more to do */
670 break;
671
672 case nir_deref_type_cast:
673 new_deref->cast.ptr_stride = deref->cast.ptr_stride;
674 break;
675
676 case nir_deref_type_array:
677 case nir_deref_type_ptr_as_array:
678 assert(!nir_src_as_deref(deref->arr.index));
679 nir_src_copy(&new_deref->arr.index, &deref->arr.index, new_deref);
680 break;
681
682 case nir_deref_type_struct:
683 new_deref->strct.index = deref->strct.index;
684 break;
685
686 default:
687 unreachable("Invalid deref instruction type");
688 }
689
690 nir_ssa_dest_init(&new_deref->instr, &new_deref->dest,
691 deref->dest.ssa.num_components,
692 deref->dest.ssa.bit_size,
693 deref->dest.ssa.name);
694 nir_builder_instr_insert(b, &new_deref->instr);
695
696 return new_deref;
697 }
698
699 static bool
700 rematerialize_deref_src(nir_src *src, void *_state)
701 {
702 struct rematerialize_deref_state *state = _state;
703
704 nir_deref_instr *deref = nir_src_as_deref(*src);
705 if (!deref)
706 return true;
707
708 nir_deref_instr *block_deref = rematerialize_deref_in_block(deref, state);
709 if (block_deref != deref) {
710 nir_instr_rewrite_src(src->parent_instr, src,
711 nir_src_for_ssa(&block_deref->dest.ssa));
712 nir_deref_instr_remove_if_unused(deref);
713 state->progress = true;
714 }
715
716 return true;
717 }
718
719 /** Re-materialize derefs in every block
720 *
721 * This pass re-materializes deref instructions in every block in which it is
722 * used. After this pass has been run, every use of a deref will be of a
723 * deref in the same block as the use. Also, all unused derefs will be
724 * deleted as a side-effect.
725 *
726 * Derefs used as sources of phi instructions are not rematerialized.
727 */
728 bool
729 nir_rematerialize_derefs_in_use_blocks_impl(nir_function_impl *impl)
730 {
731 struct rematerialize_deref_state state = { 0 };
732 nir_builder_init(&state.builder, impl);
733
734 nir_foreach_block(block, impl) {
735 state.block = block;
736
737 /* Start each block with a fresh cache */
738 if (state.cache)
739 _mesa_hash_table_clear(state.cache, NULL);
740
741 nir_foreach_instr_safe(instr, block) {
742 if (instr->type == nir_instr_type_deref &&
743 nir_deref_instr_remove_if_unused(nir_instr_as_deref(instr)))
744 continue;
745
746 /* If a deref is used in a phi, we can't rematerialize it, as the new
747 * derefs would appear before the phi, which is not valid.
748 */
749 if (instr->type == nir_instr_type_phi)
750 continue;
751
752 state.builder.cursor = nir_before_instr(instr);
753 nir_foreach_src(instr, rematerialize_deref_src, &state);
754 }
755
756 #ifndef NDEBUG
757 nir_if *following_if = nir_block_get_following_if(block);
758 if (following_if)
759 assert(!nir_src_as_deref(following_if->condition));
760 #endif
761 }
762
763 _mesa_hash_table_destroy(state.cache, NULL);
764
765 return state.progress;
766 }
767
768 static void
769 nir_deref_instr_fixup_child_types(nir_deref_instr *parent)
770 {
771 nir_foreach_use(use, &parent->dest.ssa) {
772 if (use->parent_instr->type != nir_instr_type_deref)
773 continue;
774
775 nir_deref_instr *child = nir_instr_as_deref(use->parent_instr);
776 switch (child->deref_type) {
777 case nir_deref_type_var:
778 unreachable("nir_deref_type_var cannot be a child");
779
780 case nir_deref_type_array:
781 case nir_deref_type_array_wildcard:
782 child->type = glsl_get_array_element(parent->type);
783 break;
784
785 case nir_deref_type_ptr_as_array:
786 child->type = parent->type;
787 break;
788
789 case nir_deref_type_struct:
790 child->type = glsl_get_struct_field(parent->type,
791 child->strct.index);
792 break;
793
794 case nir_deref_type_cast:
795 /* We stop the recursion here */
796 continue;
797 }
798
799 /* Recurse into children */
800 nir_deref_instr_fixup_child_types(child);
801 }
802 }
803
804 static bool
805 is_trivial_array_deref_cast(nir_deref_instr *cast)
806 {
807 assert(is_trivial_deref_cast(cast));
808
809 nir_deref_instr *parent = nir_src_as_deref(cast->parent);
810
811 if (parent->deref_type == nir_deref_type_array) {
812 return cast->cast.ptr_stride ==
813 glsl_get_explicit_stride(nir_deref_instr_parent(parent)->type);
814 } else if (parent->deref_type == nir_deref_type_ptr_as_array) {
815 return cast->cast.ptr_stride ==
816 nir_deref_instr_ptr_as_array_stride(parent);
817 } else {
818 return false;
819 }
820 }
821
822 static bool
823 is_deref_ptr_as_array(nir_instr *instr)
824 {
825 return instr->type == nir_instr_type_deref &&
826 nir_instr_as_deref(instr)->deref_type == nir_deref_type_ptr_as_array;
827 }
828
829 /**
830 * Remove casts that just wrap other casts.
831 */
832 static bool
833 opt_remove_cast_cast(nir_deref_instr *cast)
834 {
835 nir_deref_instr *first_cast = cast;
836
837 while (true) {
838 nir_deref_instr *parent = nir_deref_instr_parent(first_cast);
839 if (parent == NULL || parent->deref_type != nir_deref_type_cast)
840 break;
841 first_cast = parent;
842 }
843 if (cast == first_cast)
844 return false;
845
846 nir_instr_rewrite_src(&cast->instr, &cast->parent,
847 nir_src_for_ssa(first_cast->parent.ssa));
848 return true;
849 }
850
851 static bool
852 opt_remove_sampler_cast(nir_deref_instr *cast)
853 {
854 assert(cast->deref_type == nir_deref_type_cast);
855 nir_deref_instr *parent = nir_src_as_deref(cast->parent);
856 if (parent == NULL)
857 return false;
858
859 /* Strip both types down to their non-array type and bail if there are any
860 * discrepancies in array lengths.
861 */
862 const struct glsl_type *parent_type = parent->type;
863 const struct glsl_type *cast_type = cast->type;
864 while (glsl_type_is_array(parent_type) && glsl_type_is_array(cast_type)) {
865 if (glsl_get_length(parent_type) != glsl_get_length(cast_type))
866 return false;
867 parent_type = glsl_get_array_element(parent_type);
868 cast_type = glsl_get_array_element(cast_type);
869 }
870
871 if (glsl_type_is_array(parent_type) || glsl_type_is_array(cast_type))
872 return false;
873
874 if (!glsl_type_is_sampler(parent_type) ||
875 cast_type != glsl_bare_sampler_type())
876 return false;
877
878 /* We're a cast from a more detailed sampler type to a bare sampler */
879 nir_ssa_def_rewrite_uses(&cast->dest.ssa,
880 nir_src_for_ssa(&parent->dest.ssa));
881 nir_instr_remove(&cast->instr);
882
883 /* Recursively crawl the deref tree and clean up types */
884 nir_deref_instr_fixup_child_types(parent);
885
886 return true;
887 }
888
889 /**
890 * Is this casting a struct to a contained struct.
891 * struct a { struct b field0 };
892 * ssa_5 is structa;
893 * deref_cast (structb *)ssa_5 (function_temp structb);
894 * converts to
895 * deref_struct &ssa_5->field0 (function_temp structb);
896 * This allows subsequent copy propagation to work.
897 */
898 static bool
899 opt_replace_struct_wrapper_cast(nir_builder *b, nir_deref_instr *cast)
900 {
901 nir_deref_instr *parent = nir_src_as_deref(cast->parent);
902 if (!parent)
903 return false;
904
905 if (!glsl_type_is_struct(parent->type))
906 return false;
907
908 if (glsl_get_struct_field_offset(parent->type, 0) != 0)
909 return false;
910
911 if (cast->type != glsl_get_struct_field(parent->type, 0))
912 return false;
913
914 nir_deref_instr *replace = nir_build_deref_struct(b, parent, 0);
915 nir_ssa_def_rewrite_uses(&cast->dest.ssa, nir_src_for_ssa(&replace->dest.ssa));
916 nir_deref_instr_remove_if_unused(cast);
917 return true;
918 }
919
920 static bool
921 opt_deref_cast(nir_builder *b, nir_deref_instr *cast)
922 {
923 bool progress;
924
925 if (opt_replace_struct_wrapper_cast(b, cast))
926 return true;
927
928 if (opt_remove_sampler_cast(cast))
929 return true;
930
931 progress = opt_remove_cast_cast(cast);
932 if (!is_trivial_deref_cast(cast))
933 return progress;
934
935 bool trivial_array_cast = is_trivial_array_deref_cast(cast);
936
937 assert(cast->dest.is_ssa);
938 assert(cast->parent.is_ssa);
939
940 nir_foreach_use_safe(use_src, &cast->dest.ssa) {
941 /* If this isn't a trivial array cast, we can't propagate into
942 * ptr_as_array derefs.
943 */
944 if (is_deref_ptr_as_array(use_src->parent_instr) &&
945 !trivial_array_cast)
946 continue;
947
948 nir_instr_rewrite_src(use_src->parent_instr, use_src, cast->parent);
949 progress = true;
950 }
951
952 /* If uses would be a bit crazy */
953 assert(list_is_empty(&cast->dest.ssa.if_uses));
954
955 if (nir_deref_instr_remove_if_unused(cast))
956 progress = true;
957
958 return progress;
959 }
960
961 static bool
962 opt_deref_ptr_as_array(nir_builder *b, nir_deref_instr *deref)
963 {
964 assert(deref->deref_type == nir_deref_type_ptr_as_array);
965
966 nir_deref_instr *parent = nir_deref_instr_parent(deref);
967
968 if (nir_src_is_const(deref->arr.index) &&
969 nir_src_as_int(deref->arr.index) == 0) {
970 /* If it's a ptr_as_array deref with an index of 0, it does nothing
971 * and we can just replace its uses with its parent.
972 *
973 * The source of a ptr_as_array deref always has a deref_type of
974 * nir_deref_type_array or nir_deref_type_cast. If it's a cast, it
975 * may be trivial and we may be able to get rid of that too. Any
976 * trivial cast of trivial cast cases should be handled already by
977 * opt_deref_cast() above.
978 */
979 if (parent->deref_type == nir_deref_type_cast &&
980 is_trivial_deref_cast(parent))
981 parent = nir_deref_instr_parent(parent);
982 nir_ssa_def_rewrite_uses(&deref->dest.ssa,
983 nir_src_for_ssa(&parent->dest.ssa));
984 nir_instr_remove(&deref->instr);
985 return true;
986 }
987
988 if (parent->deref_type != nir_deref_type_array &&
989 parent->deref_type != nir_deref_type_ptr_as_array)
990 return false;
991
992 assert(parent->parent.is_ssa);
993 assert(parent->arr.index.is_ssa);
994 assert(deref->arr.index.is_ssa);
995
996 nir_ssa_def *new_idx = nir_iadd(b, parent->arr.index.ssa,
997 deref->arr.index.ssa);
998
999 deref->deref_type = parent->deref_type;
1000 nir_instr_rewrite_src(&deref->instr, &deref->parent, parent->parent);
1001 nir_instr_rewrite_src(&deref->instr, &deref->arr.index,
1002 nir_src_for_ssa(new_idx));
1003 return true;
1004 }
1005
1006 bool
1007 nir_opt_deref_impl(nir_function_impl *impl)
1008 {
1009 bool progress = false;
1010
1011 nir_builder b;
1012 nir_builder_init(&b, impl);
1013
1014 nir_foreach_block(block, impl) {
1015 nir_foreach_instr_safe(instr, block) {
1016 if (instr->type != nir_instr_type_deref)
1017 continue;
1018
1019 b.cursor = nir_before_instr(instr);
1020
1021 nir_deref_instr *deref = nir_instr_as_deref(instr);
1022 switch (deref->deref_type) {
1023 case nir_deref_type_ptr_as_array:
1024 if (opt_deref_ptr_as_array(&b, deref))
1025 progress = true;
1026 break;
1027
1028 case nir_deref_type_cast:
1029 if (opt_deref_cast(&b, deref))
1030 progress = true;
1031 break;
1032
1033 default:
1034 /* Do nothing */
1035 break;
1036 }
1037 }
1038 }
1039
1040 if (progress) {
1041 nir_metadata_preserve(impl, nir_metadata_block_index |
1042 nir_metadata_dominance);
1043 } else {
1044 nir_metadata_preserve(impl, nir_metadata_all);
1045 }
1046
1047 return progress;
1048 }
1049
1050 bool
1051 nir_opt_deref(nir_shader *shader)
1052 {
1053 bool progress = false;
1054
1055 nir_foreach_function(func, shader) {
1056 if (func->impl && nir_opt_deref_impl(func->impl))
1057 progress = true;
1058 }
1059
1060 return progress;
1061 }