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_deref.h"
25 #include "util/hash_table.h"
26 #include "nir.h"
27 #include "nir_builder.h"
28
29 bool
nir_deref_cast_is_trivial(nir_deref_instr * cast)30 nir_deref_cast_is_trivial(nir_deref_instr *cast)
31 {
32 assert(cast->deref_type == nir_deref_type_cast);
33
34 nir_deref_instr *parent = nir_src_as_deref(cast->parent);
35 if (!parent)
36 return false;
37
38 return cast->modes == parent->modes &&
39 cast->type == parent->type &&
40 cast->def.num_components == parent->def.num_components &&
41 cast->def.bit_size == parent->def.bit_size;
42 }
43
44 void
nir_deref_path_init(nir_deref_path * path,nir_deref_instr * deref,void * mem_ctx)45 nir_deref_path_init(nir_deref_path *path,
46 nir_deref_instr *deref, void *mem_ctx)
47 {
48 assert(deref != NULL);
49
50 /* The length of the short path is at most ARRAY_SIZE - 1 because we need
51 * room for the NULL terminator.
52 */
53 static const int max_short_path_len = ARRAY_SIZE(path->_short_path) - 1;
54
55 int count = 0;
56
57 nir_deref_instr **tail = &path->_short_path[max_short_path_len];
58 nir_deref_instr **head = tail;
59
60 *tail = NULL;
61 for (nir_deref_instr *d = deref; d; d = nir_deref_instr_parent(d)) {
62 if (d->deref_type == nir_deref_type_cast && nir_deref_cast_is_trivial(d))
63 continue;
64 count++;
65 if (count <= max_short_path_len)
66 *(--head) = d;
67 }
68
69 if (count <= max_short_path_len) {
70 /* If we're under max_short_path_len, just use the short path. */
71 path->path = head;
72 goto done;
73 }
74
75 #ifndef NDEBUG
76 /* Just in case someone uses short_path by accident */
77 for (unsigned i = 0; i < ARRAY_SIZE(path->_short_path); i++)
78 path->_short_path[i] = (void *)(uintptr_t)0xdeadbeef;
79 #endif
80
81 path->path = ralloc_array(mem_ctx, nir_deref_instr *, count + 1);
82 head = tail = path->path + count;
83 *tail = NULL;
84 for (nir_deref_instr *d = deref; d; d = nir_deref_instr_parent(d)) {
85 if (d->deref_type == nir_deref_type_cast && nir_deref_cast_is_trivial(d))
86 continue;
87 *(--head) = d;
88 }
89
90 done:
91 assert(head == path->path);
92 assert(tail == head + count);
93 assert(*tail == NULL);
94 }
95
96 void
nir_deref_path_finish(nir_deref_path * path)97 nir_deref_path_finish(nir_deref_path *path)
98 {
99 if (path->path < &path->_short_path[0] ||
100 path->path > &path->_short_path[ARRAY_SIZE(path->_short_path) - 1])
101 ralloc_free(path->path);
102 }
103
104 /**
105 * Recursively removes unused deref instructions
106 */
107 bool
nir_deref_instr_remove_if_unused(nir_deref_instr * instr)108 nir_deref_instr_remove_if_unused(nir_deref_instr *instr)
109 {
110 bool progress = false;
111
112 for (nir_deref_instr *d = instr; d; d = nir_deref_instr_parent(d)) {
113 /* If anyone is using this deref, leave it alone */
114 if (!nir_def_is_unused(&d->def))
115 break;
116
117 nir_instr_remove(&d->instr);
118 progress = true;
119 }
120
121 return progress;
122 }
123
124 bool
nir_deref_instr_has_indirect(nir_deref_instr * instr)125 nir_deref_instr_has_indirect(nir_deref_instr *instr)
126 {
127 while (instr->deref_type != nir_deref_type_var) {
128 /* Consider casts to be indirects */
129 if (instr->deref_type == nir_deref_type_cast)
130 return true;
131
132 if ((instr->deref_type == nir_deref_type_array ||
133 instr->deref_type == nir_deref_type_ptr_as_array) &&
134 !nir_src_is_const(instr->arr.index))
135 return true;
136
137 instr = nir_deref_instr_parent(instr);
138 }
139
140 return false;
141 }
142
143 bool
nir_deref_instr_is_known_out_of_bounds(nir_deref_instr * instr)144 nir_deref_instr_is_known_out_of_bounds(nir_deref_instr *instr)
145 {
146 for (; instr; instr = nir_deref_instr_parent(instr)) {
147 if (instr->deref_type == nir_deref_type_array &&
148 nir_src_is_const(instr->arr.index) &&
149 nir_src_as_uint(instr->arr.index) >=
150 glsl_get_length(nir_deref_instr_parent(instr)->type))
151 return true;
152 }
153
154 return false;
155 }
156
157 bool
nir_deref_instr_has_complex_use(nir_deref_instr * deref,nir_deref_instr_has_complex_use_options opts)158 nir_deref_instr_has_complex_use(nir_deref_instr *deref,
159 nir_deref_instr_has_complex_use_options opts)
160 {
161 nir_foreach_use_including_if(use_src, &deref->def) {
162 if (nir_src_is_if(use_src))
163 return true;
164
165 nir_instr *use_instr = nir_src_parent_instr(use_src);
166
167 switch (use_instr->type) {
168 case nir_instr_type_deref: {
169 nir_deref_instr *use_deref = nir_instr_as_deref(use_instr);
170
171 /* A var deref has no sources */
172 assert(use_deref->deref_type != nir_deref_type_var);
173
174 /* If a deref shows up in an array index or something like that, it's
175 * a complex use.
176 */
177 if (use_src != &use_deref->parent)
178 return true;
179
180 /* Anything that isn't a basic struct or array deref is considered to
181 * be a "complex" use. In particular, we don't allow ptr_as_array
182 * because we assume that opt_deref will turn any non-complex
183 * ptr_as_array derefs into regular array derefs eventually so passes
184 * which only want to handle simple derefs will pick them up in a
185 * later pass.
186 */
187 if (use_deref->deref_type != nir_deref_type_struct &&
188 use_deref->deref_type != nir_deref_type_array_wildcard &&
189 use_deref->deref_type != nir_deref_type_array)
190 return true;
191
192 if (nir_deref_instr_has_complex_use(use_deref, opts))
193 return true;
194
195 continue;
196 }
197
198 case nir_instr_type_intrinsic: {
199 nir_intrinsic_instr *use_intrin = nir_instr_as_intrinsic(use_instr);
200 switch (use_intrin->intrinsic) {
201 case nir_intrinsic_load_deref:
202 assert(use_src == &use_intrin->src[0]);
203 continue;
204
205 case nir_intrinsic_copy_deref:
206 assert(use_src == &use_intrin->src[0] ||
207 use_src == &use_intrin->src[1]);
208 continue;
209
210 case nir_intrinsic_store_deref:
211 /* A use in src[1] of a store means we're taking that pointer and
212 * writing it to a variable. Because we have no idea who will
213 * read that variable and what they will do with the pointer, it's
214 * considered a "complex" use. A use in src[0], on the other
215 * hand, is a simple use because we're just going to dereference
216 * it and write a value there.
217 */
218 if (use_src == &use_intrin->src[0])
219 continue;
220 return true;
221
222 case nir_intrinsic_memcpy_deref:
223 if (use_src == &use_intrin->src[0] &&
224 (opts & nir_deref_instr_has_complex_use_allow_memcpy_dst))
225 continue;
226 if (use_src == &use_intrin->src[1] &&
227 (opts & nir_deref_instr_has_complex_use_allow_memcpy_src))
228 continue;
229 return true;
230
231 case nir_intrinsic_deref_atomic:
232 case nir_intrinsic_deref_atomic_swap:
233 if (opts & nir_deref_instr_has_complex_use_allow_atomics)
234 continue;
235 return true;
236
237 default:
238 return true;
239 }
240 unreachable("Switch default failed");
241 }
242
243 default:
244 return true;
245 }
246 }
247
248 return false;
249 }
250
251 static unsigned
type_scalar_size_bytes(const struct glsl_type * type)252 type_scalar_size_bytes(const struct glsl_type *type)
253 {
254 assert(glsl_type_is_vector_or_scalar(type) ||
255 glsl_type_is_matrix(type));
256 return glsl_type_is_boolean(type) ? 4 : glsl_get_bit_size(type) / 8;
257 }
258
259 unsigned
nir_deref_instr_array_stride(nir_deref_instr * deref)260 nir_deref_instr_array_stride(nir_deref_instr *deref)
261 {
262 switch (deref->deref_type) {
263 case nir_deref_type_array:
264 case nir_deref_type_array_wildcard: {
265 const struct glsl_type *arr_type = nir_deref_instr_parent(deref)->type;
266 unsigned stride = glsl_get_explicit_stride(arr_type);
267
268 if ((glsl_type_is_matrix(arr_type) &&
269 glsl_matrix_type_is_row_major(arr_type)) ||
270 (glsl_type_is_vector(arr_type) && stride == 0))
271 stride = type_scalar_size_bytes(arr_type);
272
273 return stride;
274 }
275 case nir_deref_type_ptr_as_array:
276 return nir_deref_instr_array_stride(nir_deref_instr_parent(deref));
277 case nir_deref_type_cast:
278 return deref->cast.ptr_stride;
279 default:
280 return 0;
281 }
282 }
283
284 static unsigned
type_get_array_stride(const struct glsl_type * elem_type,glsl_type_size_align_func size_align)285 type_get_array_stride(const struct glsl_type *elem_type,
286 glsl_type_size_align_func size_align)
287 {
288 unsigned elem_size, elem_align;
289 size_align(elem_type, &elem_size, &elem_align);
290 return ALIGN_POT(elem_size, elem_align);
291 }
292
293 static unsigned
struct_type_get_field_offset(const struct glsl_type * struct_type,glsl_type_size_align_func size_align,unsigned field_idx)294 struct_type_get_field_offset(const struct glsl_type *struct_type,
295 glsl_type_size_align_func size_align,
296 unsigned field_idx)
297 {
298 assert(glsl_type_is_struct_or_ifc(struct_type));
299 unsigned offset = 0;
300 for (unsigned i = 0; i <= field_idx; i++) {
301 unsigned elem_size, elem_align;
302 size_align(glsl_get_struct_field(struct_type, i), &elem_size, &elem_align);
303 offset = ALIGN_POT(offset, elem_align);
304 if (i < field_idx)
305 offset += elem_size;
306 }
307 return offset;
308 }
309
310 unsigned
nir_deref_instr_get_const_offset(nir_deref_instr * deref,glsl_type_size_align_func size_align)311 nir_deref_instr_get_const_offset(nir_deref_instr *deref,
312 glsl_type_size_align_func size_align)
313 {
314 nir_deref_path path;
315 nir_deref_path_init(&path, deref, NULL);
316
317 unsigned offset = 0;
318 for (nir_deref_instr **p = &path.path[1]; *p; p++) {
319 switch ((*p)->deref_type) {
320 case nir_deref_type_array:
321 offset += nir_src_as_uint((*p)->arr.index) *
322 type_get_array_stride((*p)->type, size_align);
323 break;
324 case nir_deref_type_struct: {
325 /* p starts at path[1], so this is safe */
326 nir_deref_instr *parent = *(p - 1);
327 offset += struct_type_get_field_offset(parent->type, size_align,
328 (*p)->strct.index);
329 break;
330 }
331 case nir_deref_type_cast:
332 /* A cast doesn't contribute to the offset */
333 break;
334 default:
335 unreachable("Unsupported deref type");
336 }
337 }
338
339 nir_deref_path_finish(&path);
340
341 return offset;
342 }
343
344 nir_def *
nir_build_deref_offset(nir_builder * b,nir_deref_instr * deref,glsl_type_size_align_func size_align)345 nir_build_deref_offset(nir_builder *b, nir_deref_instr *deref,
346 glsl_type_size_align_func size_align)
347 {
348 nir_deref_path path;
349 nir_deref_path_init(&path, deref, NULL);
350
351 nir_def *offset = nir_imm_intN_t(b, 0, deref->def.bit_size);
352 for (nir_deref_instr **p = &path.path[1]; *p; p++) {
353 switch ((*p)->deref_type) {
354 case nir_deref_type_array:
355 case nir_deref_type_ptr_as_array: {
356 nir_def *index = (*p)->arr.index.ssa;
357 int stride = type_get_array_stride((*p)->type, size_align);
358 offset = nir_iadd(b, offset, nir_amul_imm(b, index, stride));
359 break;
360 }
361 case nir_deref_type_struct: {
362 /* p starts at path[1], so this is safe */
363 nir_deref_instr *parent = *(p - 1);
364 unsigned field_offset =
365 struct_type_get_field_offset(parent->type, size_align,
366 (*p)->strct.index);
367 offset = nir_iadd_imm(b, offset, field_offset);
368 break;
369 }
370 case nir_deref_type_cast:
371 /* A cast doesn't contribute to the offset */
372 break;
373 default:
374 unreachable("Unsupported deref type");
375 }
376 }
377
378 nir_deref_path_finish(&path);
379
380 return offset;
381 }
382
383 bool
nir_remove_dead_derefs_impl(nir_function_impl * impl)384 nir_remove_dead_derefs_impl(nir_function_impl *impl)
385 {
386 bool progress = false;
387
388 nir_foreach_block(block, impl) {
389 nir_foreach_instr_safe(instr, block) {
390 if (instr->type == nir_instr_type_deref &&
391 nir_deref_instr_remove_if_unused(nir_instr_as_deref(instr)))
392 progress = true;
393 }
394 }
395
396 if (progress) {
397 nir_metadata_preserve(impl, nir_metadata_control_flow);
398 } else {
399 nir_metadata_preserve(impl, nir_metadata_all);
400 }
401
402 return progress;
403 }
404
405 bool
nir_remove_dead_derefs(nir_shader * shader)406 nir_remove_dead_derefs(nir_shader *shader)
407 {
408 bool progress = false;
409 nir_foreach_function_impl(impl, shader) {
410 if (nir_remove_dead_derefs_impl(impl))
411 progress = true;
412 }
413
414 return progress;
415 }
416
417 static bool
nir_fixup_deref_modes_instr(UNUSED struct nir_builder * b,nir_instr * instr,UNUSED void * data)418 nir_fixup_deref_modes_instr(UNUSED struct nir_builder *b, nir_instr *instr, UNUSED void *data)
419 {
420 if (instr->type != nir_instr_type_deref)
421 return false;
422
423 nir_deref_instr *deref = nir_instr_as_deref(instr);
424 nir_variable_mode parent_modes;
425 if (deref->deref_type == nir_deref_type_var) {
426 parent_modes = deref->var->data.mode;
427 } else {
428 nir_deref_instr *parent = nir_src_as_deref(deref->parent);
429 if (parent == NULL) {
430 /* Cast to some non-deref value, nothing to propagate. */
431 assert(deref->deref_type == nir_deref_type_cast);
432 return false;
433 }
434
435 /* It's safe to propagate a specific mode into a more generic one
436 * but never the other way around.
437 */
438 if (util_bitcount(parent->modes) != 1)
439 return false;
440
441 parent_modes = parent->modes;
442 }
443
444 if (deref->modes == parent_modes)
445 return false;
446
447 deref->modes = parent_modes;
448 return true;
449 }
450
451 void
nir_fixup_deref_modes(nir_shader * shader)452 nir_fixup_deref_modes(nir_shader *shader)
453 {
454 nir_shader_instructions_pass(shader, nir_fixup_deref_modes_instr,
455 nir_metadata_control_flow |
456 nir_metadata_live_defs |
457 nir_metadata_instr_index,
458 NULL);
459 }
460
461 static bool
nir_fixup_deref_types_instr(UNUSED struct nir_builder * b,nir_instr * instr,UNUSED void * data)462 nir_fixup_deref_types_instr(UNUSED struct nir_builder *b, nir_instr *instr, UNUSED void *data)
463 {
464 if (instr->type != nir_instr_type_deref)
465 return false;
466
467 nir_deref_instr *deref = nir_instr_as_deref(instr);
468 const struct glsl_type *parent_derived_type;
469 if (deref->deref_type == nir_deref_type_var) {
470 parent_derived_type = deref->var->type;
471 } else if (deref->deref_type == nir_deref_type_array ||
472 deref->deref_type == nir_deref_type_array_wildcard) {
473 nir_deref_instr *parent = nir_src_as_deref(deref->parent);
474 parent_derived_type = glsl_get_array_element(parent->type);
475 } else if (deref->deref_type == nir_deref_type_struct) {
476 nir_deref_instr *parent = nir_src_as_deref(deref->parent);
477 parent_derived_type = glsl_get_struct_field(parent->type, deref->strct.index);
478 } else if (deref->deref_type == nir_deref_type_ptr_as_array) {
479 nir_deref_instr *parent = nir_src_as_deref(deref->parent);
480 parent_derived_type = parent->type;
481 } else if (deref->deref_type == nir_deref_type_cast) {
482 return false;
483 } else {
484 unreachable("Unsupported deref type");
485 }
486
487 if (deref->type == parent_derived_type)
488 return false;
489
490 deref->type = parent_derived_type;
491 return true;
492 }
493
494 /* Update deref types when array sizes have changed. */
495 void
nir_fixup_deref_types(nir_shader * shader)496 nir_fixup_deref_types(nir_shader *shader)
497 {
498 nir_shader_instructions_pass(shader, nir_fixup_deref_types_instr,
499 nir_metadata_control_flow |
500 nir_metadata_live_defs |
501 nir_metadata_instr_index,
502 NULL);
503 }
504
505 static bool
modes_may_alias(nir_variable_mode a,nir_variable_mode b)506 modes_may_alias(nir_variable_mode a, nir_variable_mode b)
507 {
508 /* Generic pointers can alias with SSBOs */
509 if ((a & (nir_var_mem_ssbo | nir_var_mem_global)) &&
510 (b & (nir_var_mem_ssbo | nir_var_mem_global)))
511 return true;
512
513 /* Pointers can only alias if they share a mode. */
514 return a & b;
515 }
516
517 ALWAYS_INLINE static nir_deref_compare_result
compare_deref_paths(nir_deref_path * a_path,nir_deref_path * b_path,unsigned * i,bool (* stop_fn)(const nir_deref_instr *))518 compare_deref_paths(nir_deref_path *a_path, nir_deref_path *b_path,
519 unsigned *i, bool (*stop_fn)(const nir_deref_instr *))
520 {
521 /* Start off assuming they fully compare. We ignore equality for now. In
522 * the end, we'll determine that by containment.
523 */
524 nir_deref_compare_result result = nir_derefs_may_alias_bit |
525 nir_derefs_a_contains_b_bit |
526 nir_derefs_b_contains_a_bit;
527
528 nir_deref_instr **a = a_path->path;
529 nir_deref_instr **b = b_path->path;
530
531 for (; a[*i] != NULL; (*i)++) {
532 if (a[*i] != b[*i])
533 break;
534
535 if (stop_fn && stop_fn(a[*i]))
536 break;
537 }
538
539 /* We're at either the tail or the divergence point between the two deref
540 * paths. Look to see if either contains cast or a ptr_as_array deref. If
541 * it does we don't know how to safely make any inferences. Hopefully,
542 * nir_opt_deref will clean most of these up and we can start inferring
543 * things again.
544 *
545 * In theory, we could do a bit better. For instance, we could detect the
546 * case where we have exactly one ptr_as_array deref in the chain after the
547 * divergence point and it's matched in both chains and the two chains have
548 * different constant indices.
549 */
550 for (unsigned j = *i; a[j] != NULL; j++) {
551 if (stop_fn && stop_fn(a[j]))
552 break;
553
554 if (a[j]->deref_type == nir_deref_type_cast ||
555 a[j]->deref_type == nir_deref_type_ptr_as_array)
556 return nir_derefs_may_alias_bit;
557 }
558 for (unsigned j = *i; b[j] != NULL; j++) {
559 if (stop_fn && stop_fn(b[j]))
560 break;
561
562 if (b[j]->deref_type == nir_deref_type_cast ||
563 b[j]->deref_type == nir_deref_type_ptr_as_array)
564 return nir_derefs_may_alias_bit;
565 }
566
567 for (; a[*i] != NULL && b[*i] != NULL; (*i)++) {
568 if (stop_fn && (stop_fn(a[*i]) || stop_fn(b[*i])))
569 break;
570
571 switch (a[*i]->deref_type) {
572 case nir_deref_type_array:
573 case nir_deref_type_array_wildcard: {
574 assert(b[*i]->deref_type == nir_deref_type_array ||
575 b[*i]->deref_type == nir_deref_type_array_wildcard);
576
577 if (a[*i]->deref_type == nir_deref_type_array_wildcard) {
578 if (b[*i]->deref_type != nir_deref_type_array_wildcard)
579 result &= ~nir_derefs_b_contains_a_bit;
580 } else if (b[*i]->deref_type == nir_deref_type_array_wildcard) {
581 if (a[*i]->deref_type != nir_deref_type_array_wildcard)
582 result &= ~nir_derefs_a_contains_b_bit;
583 } else {
584 assert(a[*i]->deref_type == nir_deref_type_array &&
585 b[*i]->deref_type == nir_deref_type_array);
586
587 if (nir_src_is_const(a[*i]->arr.index) &&
588 nir_src_is_const(b[*i]->arr.index)) {
589 /* If they're both direct and have different offsets, they
590 * don't even alias much less anything else.
591 */
592 if (nir_src_as_uint(a[*i]->arr.index) !=
593 nir_src_as_uint(b[*i]->arr.index))
594 return nir_derefs_do_not_alias;
595 } else if (a[*i]->arr.index.ssa == b[*i]->arr.index.ssa) {
596 /* They're the same indirect, continue on */
597 } else {
598 /* They're not the same index so we can't prove anything about
599 * containment.
600 */
601 result &= ~(nir_derefs_a_contains_b_bit | nir_derefs_b_contains_a_bit);
602 }
603 }
604 break;
605 }
606
607 case nir_deref_type_struct: {
608 /* If they're different struct members, they don't even alias */
609 if (a[*i]->strct.index != b[*i]->strct.index)
610 return nir_derefs_do_not_alias;
611 break;
612 }
613
614 default:
615 unreachable("Invalid deref type");
616 }
617 }
618
619 /* If a is longer than b, then it can't contain b. If neither a[i] nor
620 * b[i] are NULL then we aren't at the end of the chain and we know nothing
621 * about containment.
622 */
623 if (a[*i] != NULL)
624 result &= ~nir_derefs_a_contains_b_bit;
625 if (b[*i] != NULL)
626 result &= ~nir_derefs_b_contains_a_bit;
627
628 /* If a contains b and b contains a they must be equal. */
629 if ((result & nir_derefs_a_contains_b_bit) &&
630 (result & nir_derefs_b_contains_a_bit))
631 result |= nir_derefs_equal_bit;
632
633 return result;
634 }
635
636 static bool
is_interface_struct_deref(const nir_deref_instr * deref)637 is_interface_struct_deref(const nir_deref_instr *deref)
638 {
639 if (deref->deref_type == nir_deref_type_struct) {
640 assert(glsl_type_is_struct_or_ifc(nir_deref_instr_parent(deref)->type));
641 return true;
642 } else {
643 return false;
644 }
645 }
646
647 nir_deref_compare_result
nir_compare_deref_paths(nir_deref_path * a_path,nir_deref_path * b_path)648 nir_compare_deref_paths(nir_deref_path *a_path,
649 nir_deref_path *b_path)
650 {
651 if (!modes_may_alias(b_path->path[0]->modes, a_path->path[0]->modes))
652 return nir_derefs_do_not_alias;
653
654 if (a_path->path[0]->deref_type != b_path->path[0]->deref_type)
655 return nir_derefs_may_alias_bit;
656
657 unsigned path_idx = 1;
658 if (a_path->path[0]->deref_type == nir_deref_type_var) {
659 const nir_variable *a_var = a_path->path[0]->var;
660 const nir_variable *b_var = b_path->path[0]->var;
661
662 /* If we got here, the two variables must have the same mode. The
663 * only way modes_may_alias() can return true for two different modes
664 * is if one is global and the other ssbo. However, Global variables
665 * only exist in OpenCL and SSBOs don't exist there. No API allows
666 * both for variables.
667 */
668 assert(a_var->data.mode == b_var->data.mode);
669
670 switch (a_var->data.mode) {
671 case nir_var_mem_ssbo: {
672 nir_deref_compare_result binding_compare;
673 if (a_var == b_var) {
674 binding_compare = compare_deref_paths(a_path, b_path, &path_idx,
675 is_interface_struct_deref);
676 } else {
677 binding_compare = nir_derefs_do_not_alias;
678 }
679
680 if (binding_compare & nir_derefs_equal_bit)
681 break;
682
683 /* If the binding derefs can't alias and at least one is RESTRICT,
684 * then we know they can't alias.
685 */
686 if (!(binding_compare & nir_derefs_may_alias_bit) &&
687 ((a_var->data.access & ACCESS_RESTRICT) ||
688 (b_var->data.access & ACCESS_RESTRICT)))
689 return nir_derefs_do_not_alias;
690
691 return nir_derefs_may_alias_bit;
692 }
693
694 case nir_var_mem_shared:
695 if (a_var == b_var)
696 break;
697
698 /* Per SPV_KHR_workgroup_memory_explicit_layout and
699 * GL_EXT_shared_memory_block, shared blocks alias each other.
700 * We will have either all blocks or all non-blocks.
701 */
702 if (glsl_type_is_interface(a_var->type) ||
703 glsl_type_is_interface(b_var->type)) {
704 assert(glsl_type_is_interface(a_var->type) &&
705 glsl_type_is_interface(b_var->type));
706 return nir_derefs_may_alias_bit;
707 }
708
709 /* Otherwise, distinct shared vars don't alias */
710 return nir_derefs_do_not_alias;
711
712 default:
713 /* For any other variable types, if we can chase them back to the
714 * variable, and the variables are different, they don't alias.
715 */
716 if (a_var == b_var)
717 break;
718
719 return nir_derefs_do_not_alias;
720 }
721 } else {
722 assert(a_path->path[0]->deref_type == nir_deref_type_cast);
723 /* If they're not exactly the same cast, it's hard to compare them so we
724 * just assume they alias. Comparing casts is tricky as there are lots
725 * of things such as mode, type, etc. to make sure work out; for now, we
726 * just assume nit_opt_deref will combine them and compare the deref
727 * instructions.
728 *
729 * TODO: At some point in the future, we could be clever and understand
730 * that a float[] and int[] have the same layout and aliasing structure
731 * but double[] and vec3[] do not and we could potentially be a bit
732 * smarter here.
733 */
734 if (a_path->path[0] != b_path->path[0])
735 return nir_derefs_may_alias_bit;
736 }
737
738 return compare_deref_paths(a_path, b_path, &path_idx, NULL);
739 }
740
741 nir_deref_compare_result
nir_compare_derefs(nir_deref_instr * a,nir_deref_instr * b)742 nir_compare_derefs(nir_deref_instr *a, nir_deref_instr *b)
743 {
744 if (a == b) {
745 return nir_derefs_equal_bit | nir_derefs_may_alias_bit |
746 nir_derefs_a_contains_b_bit | nir_derefs_b_contains_a_bit;
747 }
748
749 nir_deref_path a_path, b_path;
750 nir_deref_path_init(&a_path, a, NULL);
751 nir_deref_path_init(&b_path, b, NULL);
752 assert(a_path.path[0]->deref_type == nir_deref_type_var ||
753 a_path.path[0]->deref_type == nir_deref_type_cast);
754 assert(b_path.path[0]->deref_type == nir_deref_type_var ||
755 b_path.path[0]->deref_type == nir_deref_type_cast);
756
757 nir_deref_compare_result result = nir_compare_deref_paths(&a_path, &b_path);
758
759 nir_deref_path_finish(&a_path);
760 nir_deref_path_finish(&b_path);
761
762 return result;
763 }
764
765 nir_deref_path *
nir_get_deref_path(void * mem_ctx,nir_deref_and_path * deref)766 nir_get_deref_path(void *mem_ctx, nir_deref_and_path *deref)
767 {
768 if (!deref->_path) {
769 deref->_path = ralloc(mem_ctx, nir_deref_path);
770 nir_deref_path_init(deref->_path, deref->instr, mem_ctx);
771 }
772 return deref->_path;
773 }
774
775 nir_deref_compare_result
nir_compare_derefs_and_paths(void * mem_ctx,nir_deref_and_path * a,nir_deref_and_path * b)776 nir_compare_derefs_and_paths(void *mem_ctx,
777 nir_deref_and_path *a,
778 nir_deref_and_path *b)
779 {
780 if (a->instr == b->instr) /* nir_compare_derefs has a fast path if a == b */
781 return nir_compare_derefs(a->instr, b->instr);
782
783 return nir_compare_deref_paths(nir_get_deref_path(mem_ctx, a),
784 nir_get_deref_path(mem_ctx, b));
785 }
786
787 struct rematerialize_deref_state {
788 bool progress;
789 nir_builder builder;
790 nir_block *block;
791 };
792
793 static nir_deref_instr *
rematerialize_deref_in_block(nir_deref_instr * deref,struct rematerialize_deref_state * state)794 rematerialize_deref_in_block(nir_deref_instr *deref,
795 struct rematerialize_deref_state *state)
796 {
797 if (deref->instr.block == state->block)
798 return deref;
799
800 nir_builder *b = &state->builder;
801 nir_deref_instr *new_deref =
802 nir_deref_instr_create(b->shader, deref->deref_type);
803 new_deref->modes = deref->modes;
804 new_deref->type = deref->type;
805
806 if (deref->deref_type == nir_deref_type_var) {
807 new_deref->var = deref->var;
808 } else {
809 nir_deref_instr *parent = nir_src_as_deref(deref->parent);
810 if (parent) {
811 parent = rematerialize_deref_in_block(parent, state);
812 new_deref->parent = nir_src_for_ssa(&parent->def);
813 } else {
814 new_deref->parent = nir_src_for_ssa(deref->parent.ssa);
815 }
816 }
817
818 switch (deref->deref_type) {
819 case nir_deref_type_var:
820 case nir_deref_type_array_wildcard:
821 /* Nothing more to do */
822 break;
823
824 case nir_deref_type_cast:
825 new_deref->cast.ptr_stride = deref->cast.ptr_stride;
826 new_deref->cast.align_mul = deref->cast.align_mul;
827 new_deref->cast.align_offset = deref->cast.align_offset;
828 break;
829
830 case nir_deref_type_array:
831 case nir_deref_type_ptr_as_array:
832 assert(!nir_src_as_deref(deref->arr.index));
833 new_deref->arr.index = nir_src_for_ssa(deref->arr.index.ssa);
834 break;
835
836 case nir_deref_type_struct:
837 new_deref->strct.index = deref->strct.index;
838 break;
839
840 default:
841 unreachable("Invalid deref instruction type");
842 }
843
844 nir_def_init(&new_deref->instr, &new_deref->def,
845 deref->def.num_components, deref->def.bit_size);
846 nir_builder_instr_insert(b, &new_deref->instr);
847
848 return new_deref;
849 }
850
851 static bool
rematerialize_deref_src(nir_src * src,void * _state)852 rematerialize_deref_src(nir_src *src, void *_state)
853 {
854 struct rematerialize_deref_state *state = _state;
855
856 nir_deref_instr *deref = nir_src_as_deref(*src);
857 if (!deref)
858 return true;
859
860 nir_deref_instr *block_deref = rematerialize_deref_in_block(deref, state);
861 if (block_deref != deref) {
862 nir_src_rewrite(src, &block_deref->def);
863 nir_deref_instr_remove_if_unused(deref);
864 state->progress = true;
865 }
866
867 return true;
868 }
869
870 bool
nir_rematerialize_deref_in_use_blocks(nir_deref_instr * instr)871 nir_rematerialize_deref_in_use_blocks(nir_deref_instr *instr)
872 {
873 if (nir_deref_instr_remove_if_unused(instr))
874 return true;
875
876 struct rematerialize_deref_state state = {
877 .builder = nir_builder_create(nir_cf_node_get_function(&instr->instr.block->cf_node)),
878 };
879
880 nir_foreach_use_safe(use, &instr->def) {
881 nir_instr *parent = nir_src_parent_instr(use);
882 if (parent->block == instr->instr.block)
883 continue;
884
885 /* If a deref is used in a phi, we can't rematerialize it, as the new
886 * derefs would appear before the phi, which is not valid.
887 */
888 if (parent->type == nir_instr_type_phi)
889 continue;
890
891 state.block = parent->block;
892 state.builder.cursor = nir_before_instr(parent);
893 rematerialize_deref_src(use, &state);
894 }
895
896 return state.progress;
897 }
898
899 /** Re-materialize derefs in every block
900 *
901 * This pass re-materializes deref instructions in every block in which it is
902 * used. After this pass has been run, every use of a deref will be of a
903 * deref in the same block as the use. Also, all unused derefs will be
904 * deleted as a side-effect.
905 *
906 * Derefs used as sources of phi instructions are not rematerialized.
907 */
908 bool
nir_rematerialize_derefs_in_use_blocks_impl(nir_function_impl * impl)909 nir_rematerialize_derefs_in_use_blocks_impl(nir_function_impl *impl)
910 {
911 bool progress = false;
912 nir_foreach_block_unstructured(block, impl) {
913 nir_foreach_instr_safe(instr, block) {
914 if (instr->type == nir_instr_type_deref) {
915 nir_deref_instr *deref = nir_instr_as_deref(instr);
916 progress |= nir_rematerialize_deref_in_use_blocks(deref);
917 }
918 }
919
920 #ifndef NDEBUG
921 nir_if *following_if = nir_block_get_following_if(block);
922 if (following_if)
923 assert(!nir_src_as_deref(following_if->condition));
924 #endif
925 }
926
927 return progress;
928 }
929
930 static void
nir_deref_instr_fixup_child_types(nir_deref_instr * parent)931 nir_deref_instr_fixup_child_types(nir_deref_instr *parent)
932 {
933 nir_foreach_use(use, &parent->def) {
934 if (nir_src_parent_instr(use)->type != nir_instr_type_deref)
935 continue;
936
937 nir_deref_instr *child = nir_instr_as_deref(nir_src_parent_instr(use));
938 switch (child->deref_type) {
939 case nir_deref_type_var:
940 unreachable("nir_deref_type_var cannot be a child");
941
942 case nir_deref_type_array:
943 case nir_deref_type_array_wildcard:
944 child->type = glsl_get_array_element(parent->type);
945 break;
946
947 case nir_deref_type_ptr_as_array:
948 child->type = parent->type;
949 break;
950
951 case nir_deref_type_struct:
952 child->type = glsl_get_struct_field(parent->type,
953 child->strct.index);
954 break;
955
956 case nir_deref_type_cast:
957 /* We stop the recursion here */
958 continue;
959 }
960
961 /* Recurse into children */
962 nir_deref_instr_fixup_child_types(child);
963 }
964 }
965
966 static bool
opt_alu_of_cast(nir_alu_instr * alu)967 opt_alu_of_cast(nir_alu_instr *alu)
968 {
969 bool progress = false;
970
971 for (unsigned i = 0; i < nir_op_infos[alu->op].num_inputs; i++) {
972 nir_instr *src_instr = alu->src[i].src.ssa->parent_instr;
973 if (src_instr->type != nir_instr_type_deref)
974 continue;
975
976 nir_deref_instr *src_deref = nir_instr_as_deref(src_instr);
977 if (src_deref->deref_type != nir_deref_type_cast)
978 continue;
979
980 nir_src_rewrite(&alu->src[i].src, src_deref->parent.ssa);
981 progress = true;
982 }
983
984 return progress;
985 }
986
987 static bool
is_trivial_array_deref_cast(nir_deref_instr * cast)988 is_trivial_array_deref_cast(nir_deref_instr *cast)
989 {
990 assert(nir_deref_cast_is_trivial(cast));
991
992 nir_deref_instr *parent = nir_src_as_deref(cast->parent);
993
994 if (parent->deref_type == nir_deref_type_array) {
995 return cast->cast.ptr_stride ==
996 glsl_get_explicit_stride(nir_deref_instr_parent(parent)->type);
997 } else if (parent->deref_type == nir_deref_type_ptr_as_array) {
998 return cast->cast.ptr_stride ==
999 nir_deref_instr_array_stride(parent);
1000 } else {
1001 return false;
1002 }
1003 }
1004
1005 static bool
is_deref_ptr_as_array(nir_instr * instr)1006 is_deref_ptr_as_array(nir_instr *instr)
1007 {
1008 return instr->type == nir_instr_type_deref &&
1009 nir_instr_as_deref(instr)->deref_type == nir_deref_type_ptr_as_array;
1010 }
1011
1012 static bool
opt_remove_restricting_cast_alignments(nir_deref_instr * cast)1013 opt_remove_restricting_cast_alignments(nir_deref_instr *cast)
1014 {
1015 assert(cast->deref_type == nir_deref_type_cast);
1016 if (cast->cast.align_mul == 0)
1017 return false;
1018
1019 nir_deref_instr *parent = nir_src_as_deref(cast->parent);
1020 if (parent == NULL)
1021 return false;
1022
1023 /* Don't use any default alignment for this check. We don't want to fall
1024 * back to type alignment too early in case we find out later that we're
1025 * somehow a child of a packed struct.
1026 */
1027 uint32_t parent_mul, parent_offset;
1028 if (!nir_get_explicit_deref_align(parent, false /* default_to_type_align */,
1029 &parent_mul, &parent_offset))
1030 return false;
1031
1032 /* If this cast increases the alignment, we want to keep it.
1033 *
1034 * There is a possibility that the larger alignment provided by this cast
1035 * somehow disagrees with the smaller alignment further up the deref chain.
1036 * In that case, we choose to favor the alignment closer to the actual
1037 * memory operation which, in this case, is the cast and not its parent so
1038 * keeping the cast alignment is the right thing to do.
1039 */
1040 if (parent_mul < cast->cast.align_mul)
1041 return false;
1042
1043 /* If we've gotten here, we have a parent deref with an align_mul at least
1044 * as large as ours so we can potentially throw away the alignment
1045 * information on this deref. There are two cases to consider here:
1046 *
1047 * 1. We can chase the deref all the way back to the variable. In this
1048 * case, we have "perfect" knowledge, modulo indirect array derefs.
1049 * Unless we've done something wrong in our indirect/wildcard stride
1050 * calculations, our knowledge from the deref walk is better than the
1051 * client's.
1052 *
1053 * 2. We can't chase it all the way back to the variable. In this case,
1054 * because our call to nir_get_explicit_deref_align(parent, ...) above
1055 * above passes default_to_type_align=false, the only way we can even
1056 * get here is if something further up the deref chain has a cast with
1057 * an alignment which can only happen if we get an alignment from the
1058 * client (most likely a decoration in the SPIR-V). If the client has
1059 * provided us with two conflicting alignments in the deref chain,
1060 * that's their fault and we can do whatever we want.
1061 *
1062 * In either case, we should be without our rights, at this point, to throw
1063 * away the alignment information on this deref. However, to be "nice" to
1064 * weird clients, we do one more check. It really shouldn't happen but
1065 * it's possible that the parent's alignment offset disagrees with the
1066 * cast's alignment offset. In this case, we consider the cast as
1067 * providing more information (or at least more valid information) and keep
1068 * it even if the align_mul from the parent is larger.
1069 */
1070 assert(cast->cast.align_mul <= parent_mul);
1071 if (parent_offset % cast->cast.align_mul != cast->cast.align_offset)
1072 return false;
1073
1074 /* If we got here, the parent has better alignment information than the
1075 * child and we can get rid of the child alignment information.
1076 */
1077 cast->cast.align_mul = 0;
1078 cast->cast.align_offset = 0;
1079 return true;
1080 }
1081
1082 /**
1083 * Remove casts that just wrap other casts.
1084 */
1085 static bool
opt_remove_cast_cast(nir_deref_instr * cast)1086 opt_remove_cast_cast(nir_deref_instr *cast)
1087 {
1088 nir_deref_instr *parent = nir_deref_instr_parent(cast);
1089 if (parent == NULL || parent->deref_type != nir_deref_type_cast)
1090 return false;
1091
1092 /* Copy align info from the parent cast if needed
1093 *
1094 * In the case that align_mul = 0, the alignment for this cast is inhereted
1095 * from the parent deref (if any). If we aren't careful, removing our
1096 * parent cast from the chain may lose alignment information so we need to
1097 * copy the parent's alignment information (if any).
1098 *
1099 * opt_remove_restricting_cast_alignments() above is run before this pass
1100 * and will will have cleared our alignment (set align_mul = 0) in the case
1101 * where the parent's alignment information is somehow superior.
1102 */
1103 if (cast->cast.align_mul == 0) {
1104 cast->cast.align_mul = parent->cast.align_mul;
1105 cast->cast.align_offset = parent->cast.align_offset;
1106 }
1107
1108 nir_src_rewrite(&cast->parent, parent->parent.ssa);
1109 return true;
1110 }
1111
1112 /* Restrict variable modes in casts.
1113 *
1114 * If we know from something higher up the deref chain that the deref has a
1115 * specific mode, we can cast to more general and back but we can never cast
1116 * across modes. For non-cast derefs, we should only ever do anything here if
1117 * the parent eventually comes from a cast that we restricted earlier.
1118 */
1119 static bool
opt_restrict_deref_modes(nir_deref_instr * deref)1120 opt_restrict_deref_modes(nir_deref_instr *deref)
1121 {
1122 if (deref->deref_type == nir_deref_type_var) {
1123 assert(deref->modes == deref->var->data.mode);
1124 return false;
1125 }
1126
1127 nir_deref_instr *parent = nir_src_as_deref(deref->parent);
1128 if (parent == NULL || parent->modes == deref->modes)
1129 return false;
1130
1131 assert(parent->modes & deref->modes);
1132 deref->modes &= parent->modes;
1133 return true;
1134 }
1135
1136 static bool
opt_remove_sampler_cast(nir_deref_instr * cast)1137 opt_remove_sampler_cast(nir_deref_instr *cast)
1138 {
1139 assert(cast->deref_type == nir_deref_type_cast);
1140 nir_deref_instr *parent = nir_src_as_deref(cast->parent);
1141 if (parent == NULL)
1142 return false;
1143
1144 /* Strip both types down to their non-array type and bail if there are any
1145 * discrepancies in array lengths.
1146 */
1147 const struct glsl_type *parent_type = parent->type;
1148 const struct glsl_type *cast_type = cast->type;
1149 while (glsl_type_is_array(parent_type) && glsl_type_is_array(cast_type)) {
1150 if (glsl_get_length(parent_type) != glsl_get_length(cast_type))
1151 return false;
1152 parent_type = glsl_get_array_element(parent_type);
1153 cast_type = glsl_get_array_element(cast_type);
1154 }
1155
1156 if (!glsl_type_is_sampler(parent_type))
1157 return false;
1158
1159 if (cast_type != glsl_bare_sampler_type() &&
1160 (glsl_type_is_bare_sampler(parent_type) ||
1161 cast_type != glsl_sampler_type_to_texture(parent_type)))
1162 return false;
1163
1164 /* We're a cast from a more detailed sampler type to a bare sampler or a
1165 * texture type with the same dimensionality.
1166 */
1167 nir_def_replace(&cast->def, &parent->def);
1168
1169 /* Recursively crawl the deref tree and clean up types */
1170 nir_deref_instr_fixup_child_types(parent);
1171
1172 return true;
1173 }
1174
1175 /**
1176 * Is this casting a struct to a contained struct.
1177 * struct a { struct b field0 };
1178 * ssa_5 is structa;
1179 * deref_cast (structb *)ssa_5 (function_temp structb);
1180 * converts to
1181 * deref_struct &ssa_5->field0 (function_temp structb);
1182 * This allows subsequent copy propagation to work.
1183 */
1184 static bool
opt_replace_struct_wrapper_cast(nir_builder * b,nir_deref_instr * cast)1185 opt_replace_struct_wrapper_cast(nir_builder *b, nir_deref_instr *cast)
1186 {
1187 nir_deref_instr *parent = nir_src_as_deref(cast->parent);
1188 if (!parent)
1189 return false;
1190
1191 if (cast->cast.align_mul > 0)
1192 return false;
1193
1194 if (!glsl_type_is_struct(parent->type))
1195 return false;
1196
1197 /* Empty struct */
1198 if (glsl_get_length(parent->type) < 1)
1199 return false;
1200
1201 if (glsl_get_struct_field_offset(parent->type, 0) != 0)
1202 return false;
1203
1204 const struct glsl_type *field_type = glsl_get_struct_field(parent->type, 0);
1205 if (cast->type != field_type)
1206 return false;
1207
1208 /* we can't drop the stride information */
1209 if (cast->cast.ptr_stride != glsl_get_explicit_stride(field_type))
1210 return false;
1211
1212 nir_deref_instr *replace = nir_build_deref_struct(b, parent, 0);
1213 nir_def_rewrite_uses(&cast->def, &replace->def);
1214 nir_deref_instr_remove_if_unused(cast);
1215 return true;
1216 }
1217
1218 static bool
opt_deref_cast(nir_builder * b,nir_deref_instr * cast)1219 opt_deref_cast(nir_builder *b, nir_deref_instr *cast)
1220 {
1221 bool progress = false;
1222
1223 progress |= opt_remove_restricting_cast_alignments(cast);
1224
1225 if (opt_replace_struct_wrapper_cast(b, cast))
1226 return true;
1227
1228 if (opt_remove_sampler_cast(cast))
1229 return true;
1230
1231 progress |= opt_remove_cast_cast(cast);
1232 if (!nir_deref_cast_is_trivial(cast))
1233 return progress;
1234
1235 /* If this deref still contains useful alignment information, we don't want
1236 * to delete it.
1237 */
1238 if (cast->cast.align_mul > 0)
1239 return progress;
1240
1241 bool trivial_array_cast = is_trivial_array_deref_cast(cast);
1242
1243 nir_foreach_use_including_if_safe(use_src, &cast->def) {
1244 assert(!nir_src_is_if(use_src) && "there cannot be if-uses");
1245
1246 /* If this isn't a trivial array cast, we can't propagate into
1247 * ptr_as_array derefs.
1248 */
1249 if (is_deref_ptr_as_array(nir_src_parent_instr(use_src)) &&
1250 !trivial_array_cast)
1251 continue;
1252
1253 nir_src_rewrite(use_src, cast->parent.ssa);
1254 progress = true;
1255 }
1256
1257 if (nir_deref_instr_remove_if_unused(cast))
1258 progress = true;
1259
1260 return progress;
1261 }
1262
1263 static bool
opt_deref_ptr_as_array(nir_builder * b,nir_deref_instr * deref)1264 opt_deref_ptr_as_array(nir_builder *b, nir_deref_instr *deref)
1265 {
1266 assert(deref->deref_type == nir_deref_type_ptr_as_array);
1267
1268 nir_deref_instr *parent = nir_deref_instr_parent(deref);
1269
1270 if (nir_src_is_const(deref->arr.index) &&
1271 nir_src_as_int(deref->arr.index) == 0) {
1272 /* If it's a ptr_as_array deref with an index of 0, it does nothing
1273 * and we can just replace its uses with its parent, unless it has
1274 * alignment information.
1275 *
1276 * The source of a ptr_as_array deref always has a deref_type of
1277 * nir_deref_type_array or nir_deref_type_cast. If it's a cast, it
1278 * may be trivial and we may be able to get rid of that too. Any
1279 * trivial cast of trivial cast cases should be handled already by
1280 * opt_deref_cast() above.
1281 */
1282 if (parent->deref_type == nir_deref_type_cast &&
1283 parent->cast.align_mul == 0 &&
1284 nir_deref_cast_is_trivial(parent))
1285 parent = nir_deref_instr_parent(parent);
1286 nir_def_replace(&deref->def, &parent->def);
1287 return true;
1288 }
1289
1290 if (parent->deref_type != nir_deref_type_array &&
1291 parent->deref_type != nir_deref_type_ptr_as_array)
1292 return false;
1293
1294 deref->arr.in_bounds &= parent->arr.in_bounds;
1295
1296 nir_def *new_idx = nir_iadd(b, parent->arr.index.ssa,
1297 deref->arr.index.ssa);
1298
1299 deref->deref_type = parent->deref_type;
1300 nir_src_rewrite(&deref->parent, parent->parent.ssa);
1301 nir_src_rewrite(&deref->arr.index, new_idx);
1302 return true;
1303 }
1304
1305 static bool
is_vector_bitcast_deref(nir_deref_instr * cast,nir_component_mask_t mask,bool is_write)1306 is_vector_bitcast_deref(nir_deref_instr *cast,
1307 nir_component_mask_t mask,
1308 bool is_write)
1309 {
1310 if (cast->deref_type != nir_deref_type_cast)
1311 return false;
1312
1313 /* Don't throw away useful alignment information */
1314 if (cast->cast.align_mul > 0)
1315 return false;
1316
1317 /* It has to be a cast of another deref */
1318 nir_deref_instr *parent = nir_src_as_deref(cast->parent);
1319 if (parent == NULL)
1320 return false;
1321
1322 /* The parent has to be a vector or scalar */
1323 if (!glsl_type_is_vector_or_scalar(parent->type))
1324 return false;
1325
1326 /* Don't bother with 1-bit types */
1327 unsigned cast_bit_size = glsl_get_bit_size(cast->type);
1328 unsigned parent_bit_size = glsl_get_bit_size(parent->type);
1329 if (cast_bit_size == 1 || parent_bit_size == 1)
1330 return false;
1331
1332 /* A strided vector type means it's not tightly packed */
1333 if (glsl_get_explicit_stride(cast->type) ||
1334 glsl_get_explicit_stride(parent->type))
1335 return false;
1336
1337 assert(cast_bit_size > 0 && cast_bit_size % 8 == 0);
1338 assert(parent_bit_size > 0 && parent_bit_size % 8 == 0);
1339 unsigned bytes_used = util_last_bit(mask) * (cast_bit_size / 8);
1340 unsigned parent_bytes = glsl_get_vector_elements(parent->type) *
1341 (parent_bit_size / 8);
1342 if (bytes_used > parent_bytes)
1343 return false;
1344
1345 if (is_write && !nir_component_mask_can_reinterpret(mask, cast_bit_size,
1346 parent_bit_size))
1347 return false;
1348
1349 return true;
1350 }
1351
1352 static nir_def *
resize_vector(nir_builder * b,nir_def * data,unsigned num_components)1353 resize_vector(nir_builder *b, nir_def *data, unsigned num_components)
1354 {
1355 if (num_components == data->num_components)
1356 return data;
1357
1358 unsigned swiz[NIR_MAX_VEC_COMPONENTS] = {
1359 0,
1360 };
1361 for (unsigned i = 0; i < MIN2(num_components, data->num_components); i++)
1362 swiz[i] = i;
1363
1364 return nir_swizzle(b, data, swiz, num_components);
1365 }
1366
1367 static bool
opt_load_vec_deref(nir_builder * b,nir_intrinsic_instr * load)1368 opt_load_vec_deref(nir_builder *b, nir_intrinsic_instr *load)
1369 {
1370 nir_deref_instr *deref = nir_src_as_deref(load->src[0]);
1371 nir_component_mask_t read_mask =
1372 nir_def_components_read(&load->def);
1373
1374 /* LLVM loves take advantage of the fact that vec3s in OpenCL are
1375 * vec4-aligned and so it can just read/write them as vec4s. This
1376 * results in a LOT of vec4->vec3 casts on loads and stores.
1377 */
1378 if (is_vector_bitcast_deref(deref, read_mask, false)) {
1379 const unsigned old_num_comps = load->def.num_components;
1380 const unsigned old_bit_size = load->def.bit_size;
1381
1382 nir_deref_instr *parent = nir_src_as_deref(deref->parent);
1383 const unsigned new_num_comps = glsl_get_vector_elements(parent->type);
1384 const unsigned new_bit_size = glsl_get_bit_size(parent->type);
1385
1386 /* Stomp it to reference the parent */
1387 nir_src_rewrite(&load->src[0], &parent->def);
1388 load->def.bit_size = new_bit_size;
1389 load->def.num_components = new_num_comps;
1390 load->num_components = new_num_comps;
1391
1392 b->cursor = nir_after_instr(&load->instr);
1393 nir_def *data = &load->def;
1394 if (old_bit_size != new_bit_size)
1395 data = nir_bitcast_vector(b, &load->def, old_bit_size);
1396 data = resize_vector(b, data, old_num_comps);
1397
1398 nir_def_rewrite_uses_after(&load->def, data,
1399 data->parent_instr);
1400 return true;
1401 }
1402
1403 return false;
1404 }
1405
1406 static bool
opt_store_vec_deref(nir_builder * b,nir_intrinsic_instr * store)1407 opt_store_vec_deref(nir_builder *b, nir_intrinsic_instr *store)
1408 {
1409 nir_deref_instr *deref = nir_src_as_deref(store->src[0]);
1410 nir_component_mask_t write_mask = nir_intrinsic_write_mask(store);
1411
1412 /* LLVM loves take advantage of the fact that vec3s in OpenCL are
1413 * vec4-aligned and so it can just read/write them as vec4s. This
1414 * results in a LOT of vec4->vec3 casts on loads and stores.
1415 */
1416 if (is_vector_bitcast_deref(deref, write_mask, true)) {
1417 nir_def *data = store->src[1].ssa;
1418
1419 const unsigned old_bit_size = data->bit_size;
1420
1421 nir_deref_instr *parent = nir_src_as_deref(deref->parent);
1422 const unsigned new_num_comps = glsl_get_vector_elements(parent->type);
1423 const unsigned new_bit_size = glsl_get_bit_size(parent->type);
1424
1425 nir_src_rewrite(&store->src[0], &parent->def);
1426
1427 /* Restrict things down as needed so the bitcast doesn't fail */
1428 data = nir_trim_vector(b, data, util_last_bit(write_mask));
1429 if (old_bit_size != new_bit_size)
1430 data = nir_bitcast_vector(b, data, new_bit_size);
1431 data = resize_vector(b, data, new_num_comps);
1432 nir_src_rewrite(&store->src[1], data);
1433 store->num_components = new_num_comps;
1434
1435 /* Adjust the write mask */
1436 write_mask = nir_component_mask_reinterpret(write_mask, old_bit_size,
1437 new_bit_size);
1438 nir_intrinsic_set_write_mask(store, write_mask);
1439 return true;
1440 }
1441
1442 return false;
1443 }
1444
1445 static bool
opt_known_deref_mode_is(nir_builder * b,nir_intrinsic_instr * intrin)1446 opt_known_deref_mode_is(nir_builder *b, nir_intrinsic_instr *intrin)
1447 {
1448 nir_variable_mode modes = nir_intrinsic_memory_modes(intrin);
1449 nir_deref_instr *deref = nir_src_as_deref(intrin->src[0]);
1450 if (deref == NULL)
1451 return false;
1452
1453 nir_def *deref_is = NULL;
1454
1455 if (nir_deref_mode_must_be(deref, modes))
1456 deref_is = nir_imm_true(b);
1457
1458 if (!nir_deref_mode_may_be(deref, modes))
1459 deref_is = nir_imm_false(b);
1460
1461 if (deref_is == NULL)
1462 return false;
1463
1464 nir_def_replace(&intrin->def, deref_is);
1465 return true;
1466 }
1467
1468 bool
nir_opt_deref_impl(nir_function_impl * impl)1469 nir_opt_deref_impl(nir_function_impl *impl)
1470 {
1471 bool progress = false;
1472
1473 nir_builder b = nir_builder_create(impl);
1474
1475 nir_foreach_block(block, impl) {
1476 nir_foreach_instr_safe(instr, block) {
1477 b.cursor = nir_before_instr(instr);
1478
1479 switch (instr->type) {
1480 case nir_instr_type_alu: {
1481 nir_alu_instr *alu = nir_instr_as_alu(instr);
1482 if (opt_alu_of_cast(alu))
1483 progress = true;
1484 break;
1485 }
1486
1487 case nir_instr_type_deref: {
1488 nir_deref_instr *deref = nir_instr_as_deref(instr);
1489
1490 if (opt_restrict_deref_modes(deref))
1491 progress = true;
1492
1493 switch (deref->deref_type) {
1494 case nir_deref_type_ptr_as_array:
1495 if (opt_deref_ptr_as_array(&b, deref))
1496 progress = true;
1497 break;
1498
1499 case nir_deref_type_cast:
1500 if (opt_deref_cast(&b, deref))
1501 progress = true;
1502 break;
1503
1504 default:
1505 /* Do nothing */
1506 break;
1507 }
1508 break;
1509 }
1510
1511 case nir_instr_type_intrinsic: {
1512 nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
1513 switch (intrin->intrinsic) {
1514 case nir_intrinsic_load_deref:
1515 if (opt_load_vec_deref(&b, intrin))
1516 progress = true;
1517 break;
1518
1519 case nir_intrinsic_store_deref:
1520 if (opt_store_vec_deref(&b, intrin))
1521 progress = true;
1522 break;
1523
1524 case nir_intrinsic_deref_mode_is:
1525 if (opt_known_deref_mode_is(&b, intrin))
1526 progress = true;
1527 break;
1528
1529 default:
1530 /* Do nothing */
1531 break;
1532 }
1533 break;
1534 }
1535
1536 default:
1537 /* Do nothing */
1538 break;
1539 }
1540 }
1541 }
1542
1543 if (progress) {
1544 nir_metadata_preserve(impl, nir_metadata_control_flow);
1545 } else {
1546 nir_metadata_preserve(impl, nir_metadata_all);
1547 }
1548
1549 return progress;
1550 }
1551
1552 bool
nir_opt_deref(nir_shader * shader)1553 nir_opt_deref(nir_shader *shader)
1554 {
1555 bool progress = false;
1556
1557 nir_foreach_function_impl(impl, shader) {
1558 if (nir_opt_deref_impl(impl))
1559 progress = true;
1560 }
1561
1562 return progress;
1563 }
1564