59805cdbd01f7ffcf68384babbd724e5697121db
[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 unsigned offset = 0;
283 for (nir_deref_instr **p = &path.path[1]; *p; p++) {
284 switch ((*p)->deref_type) {
285 case nir_deref_type_array:
286 offset += nir_src_as_uint((*p)->arr.index) *
287 type_get_array_stride((*p)->type, size_align);
288 break;
289 case nir_deref_type_struct: {
290 /* p starts at path[1], so this is safe */
291 nir_deref_instr *parent = *(p - 1);
292 offset += struct_type_get_field_offset(parent->type, size_align,
293 (*p)->strct.index);
294 break;
295 }
296 case nir_deref_type_cast:
297 /* A cast doesn't contribute to the offset */
298 break;
299 default:
300 unreachable("Unsupported deref type");
301 }
302 }
303
304 nir_deref_path_finish(&path);
305
306 return offset;
307 }
308
309 nir_ssa_def *
310 nir_build_deref_offset(nir_builder *b, nir_deref_instr *deref,
311 glsl_type_size_align_func size_align)
312 {
313 nir_deref_path path;
314 nir_deref_path_init(&path, deref, NULL);
315
316 nir_ssa_def *offset = nir_imm_intN_t(b, 0, deref->dest.ssa.bit_size);
317 for (nir_deref_instr **p = &path.path[1]; *p; p++) {
318 switch ((*p)->deref_type) {
319 case nir_deref_type_array: {
320 nir_ssa_def *index = nir_ssa_for_src(b, (*p)->arr.index, 1);
321 int stride = type_get_array_stride((*p)->type, size_align);
322 offset = nir_iadd(b, offset, nir_amul_imm(b, index, stride));
323 break;
324 }
325 case nir_deref_type_struct: {
326 /* p starts at path[1], so this is safe */
327 nir_deref_instr *parent = *(p - 1);
328 unsigned field_offset =
329 struct_type_get_field_offset(parent->type, size_align,
330 (*p)->strct.index);
331 offset = nir_iadd_imm(b, offset, field_offset);
332 break;
333 }
334 case nir_deref_type_cast:
335 /* A cast doesn't contribute to the offset */
336 break;
337 default:
338 unreachable("Unsupported deref type");
339 }
340 }
341
342 nir_deref_path_finish(&path);
343
344 return offset;
345 }
346
347 bool
348 nir_remove_dead_derefs_impl(nir_function_impl *impl)
349 {
350 bool progress = false;
351
352 nir_foreach_block(block, impl) {
353 nir_foreach_instr_safe(instr, block) {
354 if (instr->type == nir_instr_type_deref &&
355 nir_deref_instr_remove_if_unused(nir_instr_as_deref(instr)))
356 progress = true;
357 }
358 }
359
360 if (progress)
361 nir_metadata_preserve(impl, nir_metadata_block_index |
362 nir_metadata_dominance);
363
364 return progress;
365 }
366
367 bool
368 nir_remove_dead_derefs(nir_shader *shader)
369 {
370 bool progress = false;
371 nir_foreach_function(function, shader) {
372 if (function->impl && nir_remove_dead_derefs_impl(function->impl))
373 progress = true;
374 }
375
376 return progress;
377 }
378
379 void
380 nir_fixup_deref_modes(nir_shader *shader)
381 {
382 nir_foreach_function(function, shader) {
383 if (!function->impl)
384 continue;
385
386 nir_foreach_block(block, function->impl) {
387 nir_foreach_instr(instr, block) {
388 if (instr->type != nir_instr_type_deref)
389 continue;
390
391 nir_deref_instr *deref = nir_instr_as_deref(instr);
392 if (deref->deref_type == nir_deref_type_cast)
393 continue;
394
395 nir_variable_mode parent_mode;
396 if (deref->deref_type == nir_deref_type_var) {
397 parent_mode = deref->var->data.mode;
398 } else {
399 assert(deref->parent.is_ssa);
400 nir_deref_instr *parent =
401 nir_instr_as_deref(deref->parent.ssa->parent_instr);
402 parent_mode = parent->mode;
403 }
404
405 deref->mode = parent_mode;
406 }
407 }
408 }
409 }
410
411 static bool
412 modes_may_alias(nir_variable_mode a, nir_variable_mode b)
413 {
414 /* Generic pointers can alias with SSBOs */
415 if ((a == nir_var_mem_ssbo || a == nir_var_mem_global) &&
416 (b == nir_var_mem_ssbo || b == nir_var_mem_global))
417 return true;
418
419 /* In the general case, pointers can only alias if they have the same mode.
420 *
421 * NOTE: In future, with things like OpenCL generic pointers, this may not
422 * be true and will have to be re-evaluated. However, with graphics only,
423 * it should be safe.
424 */
425 return a == b;
426 }
427
428 static bool
429 deref_path_contains_coherent_decoration(nir_deref_path *path)
430 {
431 assert(path->path[0]->deref_type == nir_deref_type_var);
432
433 if (path->path[0]->var->data.access & ACCESS_COHERENT)
434 return true;
435
436 for (nir_deref_instr **p = &path->path[1]; *p; p++) {
437 if ((*p)->deref_type != nir_deref_type_struct)
438 continue;
439
440 const struct glsl_type *struct_type = (*(p - 1))->type;
441 const struct glsl_struct_field *field =
442 glsl_get_struct_field_data(struct_type, (*p)->strct.index);
443 if (field->memory_coherent)
444 return true;
445 }
446
447 return false;
448 }
449
450 nir_deref_compare_result
451 nir_compare_deref_paths(nir_deref_path *a_path,
452 nir_deref_path *b_path)
453 {
454 if (!modes_may_alias(b_path->path[0]->mode, a_path->path[0]->mode))
455 return nir_derefs_do_not_alias;
456
457 if (a_path->path[0]->deref_type != b_path->path[0]->deref_type)
458 return nir_derefs_may_alias_bit;
459
460 if (a_path->path[0]->deref_type == nir_deref_type_var) {
461 if (a_path->path[0]->var != b_path->path[0]->var) {
462 /* Shader and function temporaries aren't backed by memory so two
463 * distinct variables never alias.
464 */
465 static const nir_variable_mode temp_var_modes =
466 nir_var_shader_temp | nir_var_function_temp;
467 if ((a_path->path[0]->mode & temp_var_modes) ||
468 (b_path->path[0]->mode & temp_var_modes))
469 return nir_derefs_do_not_alias;
470
471 /* If they are both declared coherent or have coherent somewhere in
472 * their path (due to a member of an interface being declared
473 * coherent), we have to assume we that we could have any kind of
474 * aliasing. Otherwise, they could still alias but the client didn't
475 * tell us and that's their fault.
476 */
477 if (deref_path_contains_coherent_decoration(a_path) &&
478 deref_path_contains_coherent_decoration(b_path))
479 return nir_derefs_may_alias_bit;
480
481 /* If we can chase the deref all the way back to the variable and
482 * they're not the same variable and at least one is not declared
483 * coherent, we know they can't possibly alias.
484 */
485 return nir_derefs_do_not_alias;
486 }
487 } else {
488 assert(a_path->path[0]->deref_type == nir_deref_type_cast);
489 /* If they're not exactly the same cast, it's hard to compare them so we
490 * just assume they alias. Comparing casts is tricky as there are lots
491 * of things such as mode, type, etc. to make sure work out; for now, we
492 * just assume nit_opt_deref will combine them and compare the deref
493 * instructions.
494 *
495 * TODO: At some point in the future, we could be clever and understand
496 * that a float[] and int[] have the same layout and aliasing structure
497 * but double[] and vec3[] do not and we could potentially be a bit
498 * smarter here.
499 */
500 if (a_path->path[0] != b_path->path[0])
501 return nir_derefs_may_alias_bit;
502 }
503
504 /* Start off assuming they fully compare. We ignore equality for now. In
505 * the end, we'll determine that by containment.
506 */
507 nir_deref_compare_result result = nir_derefs_may_alias_bit |
508 nir_derefs_a_contains_b_bit |
509 nir_derefs_b_contains_a_bit;
510
511 nir_deref_instr **a_p = &a_path->path[1];
512 nir_deref_instr **b_p = &b_path->path[1];
513 while (*a_p != NULL && *a_p == *b_p) {
514 a_p++;
515 b_p++;
516 }
517
518 /* We're at either the tail or the divergence point between the two deref
519 * paths. Look to see if either contains cast or a ptr_as_array deref. If
520 * it does we don't know how to safely make any inferences. Hopefully,
521 * nir_opt_deref will clean most of these up and we can start inferring
522 * things again.
523 *
524 * In theory, we could do a bit better. For instance, we could detect the
525 * case where we have exactly one ptr_as_array deref in the chain after the
526 * divergence point and it's matched in both chains and the two chains have
527 * different constant indices.
528 */
529 for (nir_deref_instr **t_p = a_p; *t_p; t_p++) {
530 if ((*t_p)->deref_type == nir_deref_type_cast ||
531 (*t_p)->deref_type == nir_deref_type_ptr_as_array)
532 return nir_derefs_may_alias_bit;
533 }
534 for (nir_deref_instr **t_p = b_p; *t_p; t_p++) {
535 if ((*t_p)->deref_type == nir_deref_type_cast ||
536 (*t_p)->deref_type == nir_deref_type_ptr_as_array)
537 return nir_derefs_may_alias_bit;
538 }
539
540 while (*a_p != NULL && *b_p != NULL) {
541 nir_deref_instr *a_tail = *(a_p++);
542 nir_deref_instr *b_tail = *(b_p++);
543
544 switch (a_tail->deref_type) {
545 case nir_deref_type_array:
546 case nir_deref_type_array_wildcard: {
547 assert(b_tail->deref_type == nir_deref_type_array ||
548 b_tail->deref_type == nir_deref_type_array_wildcard);
549
550 if (a_tail->deref_type == nir_deref_type_array_wildcard) {
551 if (b_tail->deref_type != nir_deref_type_array_wildcard)
552 result &= ~nir_derefs_b_contains_a_bit;
553 } else if (b_tail->deref_type == nir_deref_type_array_wildcard) {
554 if (a_tail->deref_type != nir_deref_type_array_wildcard)
555 result &= ~nir_derefs_a_contains_b_bit;
556 } else {
557 assert(a_tail->deref_type == nir_deref_type_array &&
558 b_tail->deref_type == nir_deref_type_array);
559 assert(a_tail->arr.index.is_ssa && b_tail->arr.index.is_ssa);
560
561 if (nir_src_is_const(a_tail->arr.index) &&
562 nir_src_is_const(b_tail->arr.index)) {
563 /* If they're both direct and have different offsets, they
564 * don't even alias much less anything else.
565 */
566 if (nir_src_as_uint(a_tail->arr.index) !=
567 nir_src_as_uint(b_tail->arr.index))
568 return nir_derefs_do_not_alias;
569 } else if (a_tail->arr.index.ssa == b_tail->arr.index.ssa) {
570 /* They're the same indirect, continue on */
571 } else {
572 /* They're not the same index so we can't prove anything about
573 * containment.
574 */
575 result &= ~(nir_derefs_a_contains_b_bit | nir_derefs_b_contains_a_bit);
576 }
577 }
578 break;
579 }
580
581 case nir_deref_type_struct: {
582 /* If they're different struct members, they don't even alias */
583 if (a_tail->strct.index != b_tail->strct.index)
584 return nir_derefs_do_not_alias;
585 break;
586 }
587
588 default:
589 unreachable("Invalid deref type");
590 }
591 }
592
593 /* If a is longer than b, then it can't contain b */
594 if (*a_p != NULL)
595 result &= ~nir_derefs_a_contains_b_bit;
596 if (*b_p != NULL)
597 result &= ~nir_derefs_b_contains_a_bit;
598
599 /* If a contains b and b contains a they must be equal. */
600 if ((result & nir_derefs_a_contains_b_bit) && (result & nir_derefs_b_contains_a_bit))
601 result |= nir_derefs_equal_bit;
602
603 return result;
604 }
605
606 nir_deref_compare_result
607 nir_compare_derefs(nir_deref_instr *a, nir_deref_instr *b)
608 {
609 if (a == b) {
610 return nir_derefs_equal_bit | nir_derefs_may_alias_bit |
611 nir_derefs_a_contains_b_bit | nir_derefs_b_contains_a_bit;
612 }
613
614 nir_deref_path a_path, b_path;
615 nir_deref_path_init(&a_path, a, NULL);
616 nir_deref_path_init(&b_path, b, NULL);
617 assert(a_path.path[0]->deref_type == nir_deref_type_var ||
618 a_path.path[0]->deref_type == nir_deref_type_cast);
619 assert(b_path.path[0]->deref_type == nir_deref_type_var ||
620 b_path.path[0]->deref_type == nir_deref_type_cast);
621
622 nir_deref_compare_result result = nir_compare_deref_paths(&a_path, &b_path);
623
624 nir_deref_path_finish(&a_path);
625 nir_deref_path_finish(&b_path);
626
627 return result;
628 }
629
630 struct rematerialize_deref_state {
631 bool progress;
632 nir_builder builder;
633 nir_block *block;
634 struct hash_table *cache;
635 };
636
637 static nir_deref_instr *
638 rematerialize_deref_in_block(nir_deref_instr *deref,
639 struct rematerialize_deref_state *state)
640 {
641 if (deref->instr.block == state->block)
642 return deref;
643
644 if (!state->cache) {
645 state->cache = _mesa_pointer_hash_table_create(NULL);
646 }
647
648 struct hash_entry *cached = _mesa_hash_table_search(state->cache, deref);
649 if (cached)
650 return cached->data;
651
652 nir_builder *b = &state->builder;
653 nir_deref_instr *new_deref =
654 nir_deref_instr_create(b->shader, deref->deref_type);
655 new_deref->mode = deref->mode;
656 new_deref->type = deref->type;
657
658 if (deref->deref_type == nir_deref_type_var) {
659 new_deref->var = deref->var;
660 } else {
661 nir_deref_instr *parent = nir_src_as_deref(deref->parent);
662 if (parent) {
663 parent = rematerialize_deref_in_block(parent, state);
664 new_deref->parent = nir_src_for_ssa(&parent->dest.ssa);
665 } else {
666 nir_src_copy(&new_deref->parent, &deref->parent, new_deref);
667 }
668 }
669
670 switch (deref->deref_type) {
671 case nir_deref_type_var:
672 case nir_deref_type_array_wildcard:
673 /* Nothing more to do */
674 break;
675
676 case nir_deref_type_cast:
677 new_deref->cast.ptr_stride = deref->cast.ptr_stride;
678 break;
679
680 case nir_deref_type_array:
681 case nir_deref_type_ptr_as_array:
682 assert(!nir_src_as_deref(deref->arr.index));
683 nir_src_copy(&new_deref->arr.index, &deref->arr.index, new_deref);
684 break;
685
686 case nir_deref_type_struct:
687 new_deref->strct.index = deref->strct.index;
688 break;
689
690 default:
691 unreachable("Invalid deref instruction type");
692 }
693
694 nir_ssa_dest_init(&new_deref->instr, &new_deref->dest,
695 deref->dest.ssa.num_components,
696 deref->dest.ssa.bit_size,
697 deref->dest.ssa.name);
698 nir_builder_instr_insert(b, &new_deref->instr);
699
700 return new_deref;
701 }
702
703 static bool
704 rematerialize_deref_src(nir_src *src, void *_state)
705 {
706 struct rematerialize_deref_state *state = _state;
707
708 nir_deref_instr *deref = nir_src_as_deref(*src);
709 if (!deref)
710 return true;
711
712 nir_deref_instr *block_deref = rematerialize_deref_in_block(deref, state);
713 if (block_deref != deref) {
714 nir_instr_rewrite_src(src->parent_instr, src,
715 nir_src_for_ssa(&block_deref->dest.ssa));
716 nir_deref_instr_remove_if_unused(deref);
717 state->progress = true;
718 }
719
720 return true;
721 }
722
723 /** Re-materialize derefs in every block
724 *
725 * This pass re-materializes deref instructions in every block in which it is
726 * used. After this pass has been run, every use of a deref will be of a
727 * deref in the same block as the use. Also, all unused derefs will be
728 * deleted as a side-effect.
729 *
730 * Derefs used as sources of phi instructions are not rematerialized.
731 */
732 bool
733 nir_rematerialize_derefs_in_use_blocks_impl(nir_function_impl *impl)
734 {
735 struct rematerialize_deref_state state = { 0 };
736 nir_builder_init(&state.builder, impl);
737
738 nir_foreach_block(block, impl) {
739 state.block = block;
740
741 /* Start each block with a fresh cache */
742 if (state.cache)
743 _mesa_hash_table_clear(state.cache, NULL);
744
745 nir_foreach_instr_safe(instr, block) {
746 if (instr->type == nir_instr_type_deref &&
747 nir_deref_instr_remove_if_unused(nir_instr_as_deref(instr)))
748 continue;
749
750 /* If a deref is used in a phi, we can't rematerialize it, as the new
751 * derefs would appear before the phi, which is not valid.
752 */
753 if (instr->type == nir_instr_type_phi)
754 continue;
755
756 state.builder.cursor = nir_before_instr(instr);
757 nir_foreach_src(instr, rematerialize_deref_src, &state);
758 }
759
760 #ifndef NDEBUG
761 nir_if *following_if = nir_block_get_following_if(block);
762 if (following_if)
763 assert(!nir_src_as_deref(following_if->condition));
764 #endif
765 }
766
767 _mesa_hash_table_destroy(state.cache, NULL);
768
769 return state.progress;
770 }
771
772 static void
773 nir_deref_instr_fixup_child_types(nir_deref_instr *parent)
774 {
775 nir_foreach_use(use, &parent->dest.ssa) {
776 if (use->parent_instr->type != nir_instr_type_deref)
777 continue;
778
779 nir_deref_instr *child = nir_instr_as_deref(use->parent_instr);
780 switch (child->deref_type) {
781 case nir_deref_type_var:
782 unreachable("nir_deref_type_var cannot be a child");
783
784 case nir_deref_type_array:
785 case nir_deref_type_array_wildcard:
786 child->type = glsl_get_array_element(parent->type);
787 break;
788
789 case nir_deref_type_ptr_as_array:
790 child->type = parent->type;
791 break;
792
793 case nir_deref_type_struct:
794 child->type = glsl_get_struct_field(parent->type,
795 child->strct.index);
796 break;
797
798 case nir_deref_type_cast:
799 /* We stop the recursion here */
800 continue;
801 }
802
803 /* Recurse into children */
804 nir_deref_instr_fixup_child_types(child);
805 }
806 }
807
808 static bool
809 is_trivial_array_deref_cast(nir_deref_instr *cast)
810 {
811 assert(is_trivial_deref_cast(cast));
812
813 nir_deref_instr *parent = nir_src_as_deref(cast->parent);
814
815 if (parent->deref_type == nir_deref_type_array) {
816 return cast->cast.ptr_stride ==
817 glsl_get_explicit_stride(nir_deref_instr_parent(parent)->type);
818 } else if (parent->deref_type == nir_deref_type_ptr_as_array) {
819 return cast->cast.ptr_stride ==
820 nir_deref_instr_ptr_as_array_stride(parent);
821 } else {
822 return false;
823 }
824 }
825
826 static bool
827 is_deref_ptr_as_array(nir_instr *instr)
828 {
829 return instr->type == nir_instr_type_deref &&
830 nir_instr_as_deref(instr)->deref_type == nir_deref_type_ptr_as_array;
831 }
832
833 /**
834 * Remove casts that just wrap other casts.
835 */
836 static bool
837 opt_remove_cast_cast(nir_deref_instr *cast)
838 {
839 nir_deref_instr *first_cast = cast;
840
841 while (true) {
842 nir_deref_instr *parent = nir_deref_instr_parent(first_cast);
843 if (parent == NULL || parent->deref_type != nir_deref_type_cast)
844 break;
845 first_cast = parent;
846 }
847 if (cast == first_cast)
848 return false;
849
850 nir_instr_rewrite_src(&cast->instr, &cast->parent,
851 nir_src_for_ssa(first_cast->parent.ssa));
852 return true;
853 }
854
855 static bool
856 opt_remove_sampler_cast(nir_deref_instr *cast)
857 {
858 assert(cast->deref_type == nir_deref_type_cast);
859 nir_deref_instr *parent = nir_src_as_deref(cast->parent);
860 if (parent == NULL)
861 return false;
862
863 /* Strip both types down to their non-array type and bail if there are any
864 * discrepancies in array lengths.
865 */
866 const struct glsl_type *parent_type = parent->type;
867 const struct glsl_type *cast_type = cast->type;
868 while (glsl_type_is_array(parent_type) && glsl_type_is_array(cast_type)) {
869 if (glsl_get_length(parent_type) != glsl_get_length(cast_type))
870 return false;
871 parent_type = glsl_get_array_element(parent_type);
872 cast_type = glsl_get_array_element(cast_type);
873 }
874
875 if (glsl_type_is_array(parent_type) || glsl_type_is_array(cast_type))
876 return false;
877
878 if (!glsl_type_is_sampler(parent_type) ||
879 cast_type != glsl_bare_sampler_type())
880 return false;
881
882 /* We're a cast from a more detailed sampler type to a bare sampler */
883 nir_ssa_def_rewrite_uses(&cast->dest.ssa,
884 nir_src_for_ssa(&parent->dest.ssa));
885 nir_instr_remove(&cast->instr);
886
887 /* Recursively crawl the deref tree and clean up types */
888 nir_deref_instr_fixup_child_types(parent);
889
890 return true;
891 }
892
893 /**
894 * Is this casting a struct to a contained struct.
895 * struct a { struct b field0 };
896 * ssa_5 is structa;
897 * deref_cast (structb *)ssa_5 (function_temp structb);
898 * converts to
899 * deref_struct &ssa_5->field0 (function_temp structb);
900 * This allows subsequent copy propagation to work.
901 */
902 static bool
903 opt_replace_struct_wrapper_cast(nir_builder *b, nir_deref_instr *cast)
904 {
905 nir_deref_instr *parent = nir_src_as_deref(cast->parent);
906 if (!parent)
907 return false;
908
909 if (!glsl_type_is_struct(parent->type))
910 return false;
911
912 if (glsl_get_struct_field_offset(parent->type, 0) != 0)
913 return false;
914
915 if (cast->type != glsl_get_struct_field(parent->type, 0))
916 return false;
917
918 nir_deref_instr *replace = nir_build_deref_struct(b, parent, 0);
919 nir_ssa_def_rewrite_uses(&cast->dest.ssa, nir_src_for_ssa(&replace->dest.ssa));
920 nir_deref_instr_remove_if_unused(cast);
921 return true;
922 }
923
924 static bool
925 opt_deref_cast(nir_builder *b, nir_deref_instr *cast)
926 {
927 bool progress;
928
929 if (opt_replace_struct_wrapper_cast(b, cast))
930 return true;
931
932 if (opt_remove_sampler_cast(cast))
933 return true;
934
935 progress = opt_remove_cast_cast(cast);
936 if (!is_trivial_deref_cast(cast))
937 return progress;
938
939 bool trivial_array_cast = is_trivial_array_deref_cast(cast);
940
941 assert(cast->dest.is_ssa);
942 assert(cast->parent.is_ssa);
943
944 nir_foreach_use_safe(use_src, &cast->dest.ssa) {
945 /* If this isn't a trivial array cast, we can't propagate into
946 * ptr_as_array derefs.
947 */
948 if (is_deref_ptr_as_array(use_src->parent_instr) &&
949 !trivial_array_cast)
950 continue;
951
952 nir_instr_rewrite_src(use_src->parent_instr, use_src, cast->parent);
953 progress = true;
954 }
955
956 /* If uses would be a bit crazy */
957 assert(list_is_empty(&cast->dest.ssa.if_uses));
958
959 if (nir_deref_instr_remove_if_unused(cast))
960 progress = true;
961
962 return progress;
963 }
964
965 static bool
966 opt_deref_ptr_as_array(nir_builder *b, nir_deref_instr *deref)
967 {
968 assert(deref->deref_type == nir_deref_type_ptr_as_array);
969
970 nir_deref_instr *parent = nir_deref_instr_parent(deref);
971
972 if (nir_src_is_const(deref->arr.index) &&
973 nir_src_as_int(deref->arr.index) == 0) {
974 /* If it's a ptr_as_array deref with an index of 0, it does nothing
975 * and we can just replace its uses with its parent.
976 *
977 * The source of a ptr_as_array deref always has a deref_type of
978 * nir_deref_type_array or nir_deref_type_cast. If it's a cast, it
979 * may be trivial and we may be able to get rid of that too. Any
980 * trivial cast of trivial cast cases should be handled already by
981 * opt_deref_cast() above.
982 */
983 if (parent->deref_type == nir_deref_type_cast &&
984 is_trivial_deref_cast(parent))
985 parent = nir_deref_instr_parent(parent);
986 nir_ssa_def_rewrite_uses(&deref->dest.ssa,
987 nir_src_for_ssa(&parent->dest.ssa));
988 nir_instr_remove(&deref->instr);
989 return true;
990 }
991
992 if (parent->deref_type != nir_deref_type_array &&
993 parent->deref_type != nir_deref_type_ptr_as_array)
994 return false;
995
996 assert(parent->parent.is_ssa);
997 assert(parent->arr.index.is_ssa);
998 assert(deref->arr.index.is_ssa);
999
1000 nir_ssa_def *new_idx = nir_iadd(b, parent->arr.index.ssa,
1001 deref->arr.index.ssa);
1002
1003 deref->deref_type = parent->deref_type;
1004 nir_instr_rewrite_src(&deref->instr, &deref->parent, parent->parent);
1005 nir_instr_rewrite_src(&deref->instr, &deref->arr.index,
1006 nir_src_for_ssa(new_idx));
1007 return true;
1008 }
1009
1010 bool
1011 nir_opt_deref_impl(nir_function_impl *impl)
1012 {
1013 bool progress = false;
1014
1015 nir_builder b;
1016 nir_builder_init(&b, impl);
1017
1018 nir_foreach_block(block, impl) {
1019 nir_foreach_instr_safe(instr, block) {
1020 if (instr->type != nir_instr_type_deref)
1021 continue;
1022
1023 b.cursor = nir_before_instr(instr);
1024
1025 nir_deref_instr *deref = nir_instr_as_deref(instr);
1026 switch (deref->deref_type) {
1027 case nir_deref_type_ptr_as_array:
1028 if (opt_deref_ptr_as_array(&b, deref))
1029 progress = true;
1030 break;
1031
1032 case nir_deref_type_cast:
1033 if (opt_deref_cast(&b, deref))
1034 progress = true;
1035 break;
1036
1037 default:
1038 /* Do nothing */
1039 break;
1040 }
1041 }
1042 }
1043
1044 if (progress) {
1045 nir_metadata_preserve(impl, nir_metadata_block_index |
1046 nir_metadata_dominance);
1047 } else {
1048 nir_metadata_preserve(impl, nir_metadata_all);
1049 }
1050
1051 return progress;
1052 }
1053
1054 bool
1055 nir_opt_deref(nir_shader *shader)
1056 {
1057 bool progress = false;
1058
1059 nir_foreach_function(func, shader) {
1060 if (func->impl && nir_opt_deref_impl(func->impl))
1061 progress = true;
1062 }
1063
1064 return progress;
1065 }