2 * Copyright © 2018 Intel Corporation
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:
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
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
25 #include "nir_builder.h"
26 #include "nir_deref.h"
27 #include "util/hash_table.h"
30 is_trivial_deref_cast(nir_deref_instr
*cast
)
32 nir_deref_instr
*parent
= nir_src_as_deref(cast
->parent
);
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
;
43 nir_deref_path_init(nir_deref_path
*path
,
44 nir_deref_instr
*deref
, void *mem_ctx
)
46 assert(deref
!= NULL
);
48 /* The length of the short path is at most ARRAY_SIZE - 1 because we need
49 * room for the NULL terminator.
51 static const int max_short_path_len
= ARRAY_SIZE(path
->_short_path
) - 1;
55 nir_deref_instr
**tail
= &path
->_short_path
[max_short_path_len
];
56 nir_deref_instr
**head
= tail
;
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
))
63 if (count
<= max_short_path_len
)
67 if (count
<= max_short_path_len
) {
68 /* If we're under max_short_path_len, just use the short path. */
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;
79 path
->path
= ralloc_array(mem_ctx
, nir_deref_instr
*, count
+ 1);
80 head
= tail
= path
->path
+ count
;
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
))
89 assert(head
== path
->path
);
90 assert(tail
== head
+ count
);
91 assert(*tail
== NULL
);
95 nir_deref_path_finish(nir_deref_path
*path
)
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
);
103 * Recursively removes unused deref instructions
106 nir_deref_instr_remove_if_unused(nir_deref_instr
*instr
)
108 bool progress
= false;
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
))
116 nir_instr_remove(&d
->instr
);
124 nir_deref_instr_has_indirect(nir_deref_instr
*instr
)
126 while (instr
->deref_type
!= nir_deref_type_var
) {
127 /* Consider casts to be indirects */
128 if (instr
->deref_type
== nir_deref_type_cast
)
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
))
136 instr
= nir_deref_instr_parent(instr
);
143 nir_deref_instr_is_known_out_of_bounds(nir_deref_instr
*instr
)
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
))
157 nir_deref_instr_has_complex_use(nir_deref_instr
*deref
)
159 nir_foreach_use(use_src
, &deref
->dest
.ssa
) {
160 nir_instr
*use_instr
= use_src
->parent_instr
;
162 switch (use_instr
->type
) {
163 case nir_instr_type_deref
: {
164 nir_deref_instr
*use_deref
= nir_instr_as_deref(use_instr
);
166 /* A var deref has no sources */
167 assert(use_deref
->deref_type
!= nir_deref_type_var
);
169 /* If a deref shows up in an array index or something like that, it's
172 if (use_src
!= &use_deref
->parent
)
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
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
)
187 if (nir_deref_instr_has_complex_use(use_deref
))
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]);
200 case nir_intrinsic_copy_deref
:
201 assert(use_src
== &use_intrin
->src
[0] ||
202 use_src
== &use_intrin
->src
[1]);
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.
213 if (use_src
== &use_intrin
->src
[0])
220 unreachable("Switch default failed");
228 nir_foreach_if_use(use
, &deref
->dest
.ssa
)
235 nir_deref_instr_ptr_as_array_stride(nir_deref_instr
*deref
)
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
;
250 type_get_array_stride(const struct glsl_type
*elem_type
,
251 glsl_type_size_align_func size_align
)
253 unsigned elem_size
, elem_align
;
254 size_align(elem_type
, &elem_size
, &elem_align
);
255 return ALIGN_POT(elem_size
, elem_align
);
259 struct_type_get_field_offset(const struct glsl_type
*struct_type
,
260 glsl_type_size_align_func size_align
,
263 assert(glsl_type_is_struct_or_ifc(struct_type
));
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
);
276 nir_deref_instr_get_const_offset(nir_deref_instr
*deref
,
277 glsl_type_size_align_func size_align
)
280 nir_deref_path_init(&path
, deref
, NULL
);
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
);
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
,
296 case nir_deref_type_cast
:
297 /* A cast doesn't contribute to the offset */
300 unreachable("Unsupported deref type");
304 nir_deref_path_finish(&path
);
310 nir_build_deref_offset(nir_builder
*b
, nir_deref_instr
*deref
,
311 glsl_type_size_align_func size_align
)
314 nir_deref_path_init(&path
, deref
, NULL
);
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
));
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
,
331 offset
= nir_iadd_imm(b
, offset
, field_offset
);
334 case nir_deref_type_cast
:
335 /* A cast doesn't contribute to the offset */
338 unreachable("Unsupported deref type");
342 nir_deref_path_finish(&path
);
348 nir_remove_dead_derefs_impl(nir_function_impl
*impl
)
350 bool progress
= false;
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
)))
361 nir_metadata_preserve(impl
, nir_metadata_block_index
|
362 nir_metadata_dominance
);
368 nir_remove_dead_derefs(nir_shader
*shader
)
370 bool progress
= false;
371 nir_foreach_function(function
, shader
) {
372 if (function
->impl
&& nir_remove_dead_derefs_impl(function
->impl
))
380 nir_fixup_deref_modes(nir_shader
*shader
)
382 nir_foreach_function(function
, shader
) {
386 nir_foreach_block(block
, function
->impl
) {
387 nir_foreach_instr(instr
, block
) {
388 if (instr
->type
!= nir_instr_type_deref
)
391 nir_deref_instr
*deref
= nir_instr_as_deref(instr
);
392 if (deref
->deref_type
== nir_deref_type_cast
)
395 nir_variable_mode parent_mode
;
396 if (deref
->deref_type
== nir_deref_type_var
) {
397 parent_mode
= deref
->var
->data
.mode
;
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
;
405 deref
->mode
= parent_mode
;
412 modes_may_alias(nir_variable_mode a
, nir_variable_mode b
)
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
))
419 /* In the general case, pointers can only alias if they have the same mode.
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,
429 deref_path_contains_coherent_decoration(nir_deref_path
*path
)
431 assert(path
->path
[0]->deref_type
== nir_deref_type_var
);
433 if (path
->path
[0]->var
->data
.access
& ACCESS_COHERENT
)
436 for (nir_deref_instr
**p
= &path
->path
[1]; *p
; p
++) {
437 if ((*p
)->deref_type
!= nir_deref_type_struct
)
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
)
450 nir_deref_compare_result
451 nir_compare_deref_paths(nir_deref_path
*a_path
,
452 nir_deref_path
*b_path
)
454 if (!modes_may_alias(b_path
->path
[0]->mode
, a_path
->path
[0]->mode
))
455 return nir_derefs_do_not_alias
;
457 if (a_path
->path
[0]->deref_type
!= b_path
->path
[0]->deref_type
)
458 return nir_derefs_may_alias_bit
;
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.
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
;
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.
477 if (deref_path_contains_coherent_decoration(a_path
) &&
478 deref_path_contains_coherent_decoration(b_path
))
479 return nir_derefs_may_alias_bit
;
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.
485 return nir_derefs_do_not_alias
;
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
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
500 if (a_path
->path
[0] != b_path
->path
[0])
501 return nir_derefs_may_alias_bit
;
504 /* Start off assuming they fully compare. We ignore equality for now. In
505 * the end, we'll determine that by containment.
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
;
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
) {
518 /* We're at either the tail or the divergence point between the two deref
519 * paths. Look to see if either contains a ptr_as_array deref. It it
520 * 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
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.
529 for (nir_deref_instr
**t_p
= a_p
; *t_p
; t_p
++) {
530 if ((*t_p
)->deref_type
== nir_deref_type_ptr_as_array
)
531 return nir_derefs_may_alias_bit
;
533 for (nir_deref_instr
**t_p
= b_p
; *t_p
; t_p
++) {
534 if ((*t_p
)->deref_type
== nir_deref_type_ptr_as_array
)
535 return nir_derefs_may_alias_bit
;
538 while (*a_p
!= NULL
&& *b_p
!= NULL
) {
539 nir_deref_instr
*a_tail
= *(a_p
++);
540 nir_deref_instr
*b_tail
= *(b_p
++);
542 switch (a_tail
->deref_type
) {
543 case nir_deref_type_array
:
544 case nir_deref_type_array_wildcard
: {
545 assert(b_tail
->deref_type
== nir_deref_type_array
||
546 b_tail
->deref_type
== nir_deref_type_array_wildcard
);
548 if (a_tail
->deref_type
== nir_deref_type_array_wildcard
) {
549 if (b_tail
->deref_type
!= nir_deref_type_array_wildcard
)
550 result
&= ~nir_derefs_b_contains_a_bit
;
551 } else if (b_tail
->deref_type
== nir_deref_type_array_wildcard
) {
552 if (a_tail
->deref_type
!= nir_deref_type_array_wildcard
)
553 result
&= ~nir_derefs_a_contains_b_bit
;
555 assert(a_tail
->deref_type
== nir_deref_type_array
&&
556 b_tail
->deref_type
== nir_deref_type_array
);
557 assert(a_tail
->arr
.index
.is_ssa
&& b_tail
->arr
.index
.is_ssa
);
559 if (nir_src_is_const(a_tail
->arr
.index
) &&
560 nir_src_is_const(b_tail
->arr
.index
)) {
561 /* If they're both direct and have different offsets, they
562 * don't even alias much less anything else.
564 if (nir_src_as_uint(a_tail
->arr
.index
) !=
565 nir_src_as_uint(b_tail
->arr
.index
))
566 return nir_derefs_do_not_alias
;
567 } else if (a_tail
->arr
.index
.ssa
== b_tail
->arr
.index
.ssa
) {
568 /* They're the same indirect, continue on */
570 /* They're not the same index so we can't prove anything about
573 result
&= ~(nir_derefs_a_contains_b_bit
| nir_derefs_b_contains_a_bit
);
579 case nir_deref_type_struct
: {
580 /* If they're different struct members, they don't even alias */
581 if (a_tail
->strct
.index
!= b_tail
->strct
.index
)
582 return nir_derefs_do_not_alias
;
587 unreachable("Invalid deref type");
591 /* If a is longer than b, then it can't contain b */
593 result
&= ~nir_derefs_a_contains_b_bit
;
595 result
&= ~nir_derefs_b_contains_a_bit
;
597 /* If a contains b and b contains a they must be equal. */
598 if ((result
& nir_derefs_a_contains_b_bit
) && (result
& nir_derefs_b_contains_a_bit
))
599 result
|= nir_derefs_equal_bit
;
604 nir_deref_compare_result
605 nir_compare_derefs(nir_deref_instr
*a
, nir_deref_instr
*b
)
608 return nir_derefs_equal_bit
| nir_derefs_may_alias_bit
|
609 nir_derefs_a_contains_b_bit
| nir_derefs_b_contains_a_bit
;
612 nir_deref_path a_path
, b_path
;
613 nir_deref_path_init(&a_path
, a
, NULL
);
614 nir_deref_path_init(&b_path
, b
, NULL
);
615 assert(a_path
.path
[0]->deref_type
== nir_deref_type_var
||
616 a_path
.path
[0]->deref_type
== nir_deref_type_cast
);
617 assert(b_path
.path
[0]->deref_type
== nir_deref_type_var
||
618 b_path
.path
[0]->deref_type
== nir_deref_type_cast
);
620 nir_deref_compare_result result
= nir_compare_deref_paths(&a_path
, &b_path
);
622 nir_deref_path_finish(&a_path
);
623 nir_deref_path_finish(&b_path
);
628 struct rematerialize_deref_state
{
632 struct hash_table
*cache
;
635 static nir_deref_instr
*
636 rematerialize_deref_in_block(nir_deref_instr
*deref
,
637 struct rematerialize_deref_state
*state
)
639 if (deref
->instr
.block
== state
->block
)
643 state
->cache
= _mesa_pointer_hash_table_create(NULL
);
646 struct hash_entry
*cached
= _mesa_hash_table_search(state
->cache
, deref
);
650 nir_builder
*b
= &state
->builder
;
651 nir_deref_instr
*new_deref
=
652 nir_deref_instr_create(b
->shader
, deref
->deref_type
);
653 new_deref
->mode
= deref
->mode
;
654 new_deref
->type
= deref
->type
;
656 if (deref
->deref_type
== nir_deref_type_var
) {
657 new_deref
->var
= deref
->var
;
659 nir_deref_instr
*parent
= nir_src_as_deref(deref
->parent
);
661 parent
= rematerialize_deref_in_block(parent
, state
);
662 new_deref
->parent
= nir_src_for_ssa(&parent
->dest
.ssa
);
664 nir_src_copy(&new_deref
->parent
, &deref
->parent
, new_deref
);
668 switch (deref
->deref_type
) {
669 case nir_deref_type_var
:
670 case nir_deref_type_array_wildcard
:
671 /* Nothing more to do */
674 case nir_deref_type_cast
:
675 new_deref
->cast
.ptr_stride
= deref
->cast
.ptr_stride
;
678 case nir_deref_type_array
:
679 case nir_deref_type_ptr_as_array
:
680 assert(!nir_src_as_deref(deref
->arr
.index
));
681 nir_src_copy(&new_deref
->arr
.index
, &deref
->arr
.index
, new_deref
);
684 case nir_deref_type_struct
:
685 new_deref
->strct
.index
= deref
->strct
.index
;
689 unreachable("Invalid deref instruction type");
692 nir_ssa_dest_init(&new_deref
->instr
, &new_deref
->dest
,
693 deref
->dest
.ssa
.num_components
,
694 deref
->dest
.ssa
.bit_size
,
695 deref
->dest
.ssa
.name
);
696 nir_builder_instr_insert(b
, &new_deref
->instr
);
702 rematerialize_deref_src(nir_src
*src
, void *_state
)
704 struct rematerialize_deref_state
*state
= _state
;
706 nir_deref_instr
*deref
= nir_src_as_deref(*src
);
710 nir_deref_instr
*block_deref
= rematerialize_deref_in_block(deref
, state
);
711 if (block_deref
!= deref
) {
712 nir_instr_rewrite_src(src
->parent_instr
, src
,
713 nir_src_for_ssa(&block_deref
->dest
.ssa
));
714 nir_deref_instr_remove_if_unused(deref
);
715 state
->progress
= true;
721 /** Re-materialize derefs in every block
723 * This pass re-materializes deref instructions in every block in which it is
724 * used. After this pass has been run, every use of a deref will be of a
725 * deref in the same block as the use. Also, all unused derefs will be
726 * deleted as a side-effect.
728 * Derefs used as sources of phi instructions are not rematerialized.
731 nir_rematerialize_derefs_in_use_blocks_impl(nir_function_impl
*impl
)
733 struct rematerialize_deref_state state
= { 0 };
734 nir_builder_init(&state
.builder
, impl
);
736 nir_foreach_block(block
, impl
) {
739 /* Start each block with a fresh cache */
741 _mesa_hash_table_clear(state
.cache
, NULL
);
743 nir_foreach_instr_safe(instr
, block
) {
744 if (instr
->type
== nir_instr_type_deref
&&
745 nir_deref_instr_remove_if_unused(nir_instr_as_deref(instr
)))
748 /* If a deref is used in a phi, we can't rematerialize it, as the new
749 * derefs would appear before the phi, which is not valid.
751 if (instr
->type
== nir_instr_type_phi
)
754 state
.builder
.cursor
= nir_before_instr(instr
);
755 nir_foreach_src(instr
, rematerialize_deref_src
, &state
);
759 nir_if
*following_if
= nir_block_get_following_if(block
);
761 assert(!nir_src_as_deref(following_if
->condition
));
765 _mesa_hash_table_destroy(state
.cache
, NULL
);
767 return state
.progress
;
771 nir_deref_instr_fixup_child_types(nir_deref_instr
*parent
)
773 nir_foreach_use(use
, &parent
->dest
.ssa
) {
774 if (use
->parent_instr
->type
!= nir_instr_type_deref
)
777 nir_deref_instr
*child
= nir_instr_as_deref(use
->parent_instr
);
778 switch (child
->deref_type
) {
779 case nir_deref_type_var
:
780 unreachable("nir_deref_type_var cannot be a child");
782 case nir_deref_type_array
:
783 case nir_deref_type_array_wildcard
:
784 child
->type
= glsl_get_array_element(parent
->type
);
787 case nir_deref_type_ptr_as_array
:
788 child
->type
= parent
->type
;
791 case nir_deref_type_struct
:
792 child
->type
= glsl_get_struct_field(parent
->type
,
796 case nir_deref_type_cast
:
797 /* We stop the recursion here */
801 /* Recurse into children */
802 nir_deref_instr_fixup_child_types(child
);
807 is_trivial_array_deref_cast(nir_deref_instr
*cast
)
809 assert(is_trivial_deref_cast(cast
));
811 nir_deref_instr
*parent
= nir_src_as_deref(cast
->parent
);
813 if (parent
->deref_type
== nir_deref_type_array
) {
814 return cast
->cast
.ptr_stride
==
815 glsl_get_explicit_stride(nir_deref_instr_parent(parent
)->type
);
816 } else if (parent
->deref_type
== nir_deref_type_ptr_as_array
) {
817 return cast
->cast
.ptr_stride
==
818 nir_deref_instr_ptr_as_array_stride(parent
);
825 is_deref_ptr_as_array(nir_instr
*instr
)
827 return instr
->type
== nir_instr_type_deref
&&
828 nir_instr_as_deref(instr
)->deref_type
== nir_deref_type_ptr_as_array
;
832 * Remove casts that just wrap other casts.
835 opt_remove_cast_cast(nir_deref_instr
*cast
)
837 nir_deref_instr
*first_cast
= cast
;
840 nir_deref_instr
*parent
= nir_deref_instr_parent(first_cast
);
841 if (parent
== NULL
|| parent
->deref_type
!= nir_deref_type_cast
)
845 if (cast
== first_cast
)
848 nir_instr_rewrite_src(&cast
->instr
, &cast
->parent
,
849 nir_src_for_ssa(first_cast
->parent
.ssa
));
854 opt_remove_sampler_cast(nir_deref_instr
*cast
)
856 assert(cast
->deref_type
== nir_deref_type_cast
);
857 nir_deref_instr
*parent
= nir_src_as_deref(cast
->parent
);
861 /* Strip both types down to their non-array type and bail if there are any
862 * discrepancies in array lengths.
864 const struct glsl_type
*parent_type
= parent
->type
;
865 const struct glsl_type
*cast_type
= cast
->type
;
866 while (glsl_type_is_array(parent_type
) && glsl_type_is_array(cast_type
)) {
867 if (glsl_get_length(parent_type
) != glsl_get_length(cast_type
))
869 parent_type
= glsl_get_array_element(parent_type
);
870 cast_type
= glsl_get_array_element(cast_type
);
873 if (glsl_type_is_array(parent_type
) || glsl_type_is_array(cast_type
))
876 if (!glsl_type_is_sampler(parent_type
) ||
877 cast_type
!= glsl_bare_sampler_type())
880 /* We're a cast from a more detailed sampler type to a bare sampler */
881 nir_ssa_def_rewrite_uses(&cast
->dest
.ssa
,
882 nir_src_for_ssa(&parent
->dest
.ssa
));
883 nir_instr_remove(&cast
->instr
);
885 /* Recursively crawl the deref tree and clean up types */
886 nir_deref_instr_fixup_child_types(parent
);
892 * Is this casting a struct to a contained struct.
893 * struct a { struct b field0 };
895 * deref_cast (structb *)ssa_5 (function_temp structb);
897 * deref_struct &ssa_5->field0 (function_temp structb);
898 * This allows subsequent copy propagation to work.
901 opt_replace_struct_wrapper_cast(nir_builder
*b
, nir_deref_instr
*cast
)
903 nir_deref_instr
*parent
= nir_src_as_deref(cast
->parent
);
907 if (!glsl_type_is_struct(parent
->type
))
910 if (glsl_get_struct_field_offset(parent
->type
, 0) != 0)
913 if (cast
->type
!= glsl_get_struct_field(parent
->type
, 0))
916 nir_deref_instr
*replace
= nir_build_deref_struct(b
, parent
, 0);
917 nir_ssa_def_rewrite_uses(&cast
->dest
.ssa
, nir_src_for_ssa(&replace
->dest
.ssa
));
918 nir_deref_instr_remove_if_unused(cast
);
923 opt_deref_cast(nir_builder
*b
, nir_deref_instr
*cast
)
927 if (opt_replace_struct_wrapper_cast(b
, cast
))
930 if (opt_remove_sampler_cast(cast
))
933 progress
= opt_remove_cast_cast(cast
);
934 if (!is_trivial_deref_cast(cast
))
937 bool trivial_array_cast
= is_trivial_array_deref_cast(cast
);
939 assert(cast
->dest
.is_ssa
);
940 assert(cast
->parent
.is_ssa
);
942 nir_foreach_use_safe(use_src
, &cast
->dest
.ssa
) {
943 /* If this isn't a trivial array cast, we can't propagate into
944 * ptr_as_array derefs.
946 if (is_deref_ptr_as_array(use_src
->parent_instr
) &&
950 nir_instr_rewrite_src(use_src
->parent_instr
, use_src
, cast
->parent
);
954 /* If uses would be a bit crazy */
955 assert(list_is_empty(&cast
->dest
.ssa
.if_uses
));
957 if (nir_deref_instr_remove_if_unused(cast
))
964 opt_deref_ptr_as_array(nir_builder
*b
, nir_deref_instr
*deref
)
966 assert(deref
->deref_type
== nir_deref_type_ptr_as_array
);
968 nir_deref_instr
*parent
= nir_deref_instr_parent(deref
);
970 if (nir_src_is_const(deref
->arr
.index
) &&
971 nir_src_as_int(deref
->arr
.index
) == 0) {
972 /* If it's a ptr_as_array deref with an index of 0, it does nothing
973 * and we can just replace its uses with its parent.
975 * The source of a ptr_as_array deref always has a deref_type of
976 * nir_deref_type_array or nir_deref_type_cast. If it's a cast, it
977 * may be trivial and we may be able to get rid of that too. Any
978 * trivial cast of trivial cast cases should be handled already by
979 * opt_deref_cast() above.
981 if (parent
->deref_type
== nir_deref_type_cast
&&
982 is_trivial_deref_cast(parent
))
983 parent
= nir_deref_instr_parent(parent
);
984 nir_ssa_def_rewrite_uses(&deref
->dest
.ssa
,
985 nir_src_for_ssa(&parent
->dest
.ssa
));
986 nir_instr_remove(&deref
->instr
);
990 if (parent
->deref_type
!= nir_deref_type_array
&&
991 parent
->deref_type
!= nir_deref_type_ptr_as_array
)
994 assert(parent
->parent
.is_ssa
);
995 assert(parent
->arr
.index
.is_ssa
);
996 assert(deref
->arr
.index
.is_ssa
);
998 nir_ssa_def
*new_idx
= nir_iadd(b
, parent
->arr
.index
.ssa
,
999 deref
->arr
.index
.ssa
);
1001 deref
->deref_type
= parent
->deref_type
;
1002 nir_instr_rewrite_src(&deref
->instr
, &deref
->parent
, parent
->parent
);
1003 nir_instr_rewrite_src(&deref
->instr
, &deref
->arr
.index
,
1004 nir_src_for_ssa(new_idx
));
1009 nir_opt_deref_impl(nir_function_impl
*impl
)
1011 bool progress
= false;
1014 nir_builder_init(&b
, impl
);
1016 nir_foreach_block(block
, impl
) {
1017 nir_foreach_instr_safe(instr
, block
) {
1018 if (instr
->type
!= nir_instr_type_deref
)
1021 b
.cursor
= nir_before_instr(instr
);
1023 nir_deref_instr
*deref
= nir_instr_as_deref(instr
);
1024 switch (deref
->deref_type
) {
1025 case nir_deref_type_ptr_as_array
:
1026 if (opt_deref_ptr_as_array(&b
, deref
))
1030 case nir_deref_type_cast
:
1031 if (opt_deref_cast(&b
, deref
))
1043 nir_metadata_preserve(impl
, nir_metadata_block_index
|
1044 nir_metadata_dominance
);
1046 nir_metadata_preserve(impl
, nir_metadata_all
);
1053 nir_opt_deref(nir_shader
*shader
)
1055 bool progress
= false;
1057 nir_foreach_function(func
, shader
) {
1058 if (func
->impl
&& nir_opt_deref_impl(func
->impl
))