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 nir_deref_path_init(nir_deref_path
*path
,
31 nir_deref_instr
*deref
, void *mem_ctx
)
33 assert(deref
!= NULL
);
35 /* The length of the short path is at most ARRAY_SIZE - 1 because we need
36 * room for the NULL terminator.
38 static const int max_short_path_len
= ARRAY_SIZE(path
->_short_path
) - 1;
42 nir_deref_instr
**tail
= &path
->_short_path
[max_short_path_len
];
43 nir_deref_instr
**head
= tail
;
46 for (nir_deref_instr
*d
= deref
; d
; d
= nir_deref_instr_parent(d
)) {
48 if (count
<= max_short_path_len
)
52 if (count
<= max_short_path_len
) {
53 /* If we're under max_short_path_len, just use the short path. */
59 /* Just in case someone uses short_path by accident */
60 for (unsigned i
= 0; i
< ARRAY_SIZE(path
->_short_path
); i
++)
61 path
->_short_path
[i
] = (void *)0xdeadbeef;
64 path
->path
= ralloc_array(mem_ctx
, nir_deref_instr
*, count
+ 1);
65 head
= tail
= path
->path
+ count
;
67 for (nir_deref_instr
*d
= deref
; d
; d
= nir_deref_instr_parent(d
))
71 assert(head
== path
->path
);
72 assert(tail
== head
+ count
);
73 assert(*tail
== NULL
);
77 nir_deref_path_finish(nir_deref_path
*path
)
79 if (path
->path
< &path
->_short_path
[0] ||
80 path
->path
> &path
->_short_path
[ARRAY_SIZE(path
->_short_path
) - 1])
81 ralloc_free(path
->path
);
85 * Recursively removes unused deref instructions
88 nir_deref_instr_remove_if_unused(nir_deref_instr
*instr
)
90 bool progress
= false;
92 for (nir_deref_instr
*d
= instr
; d
; d
= nir_deref_instr_parent(d
)) {
93 /* If anyone is using this deref, leave it alone */
94 assert(d
->dest
.is_ssa
);
95 if (!list_empty(&d
->dest
.ssa
.uses
))
98 nir_instr_remove(&d
->instr
);
106 nir_deref_instr_has_indirect(nir_deref_instr
*instr
)
108 while (instr
->deref_type
!= nir_deref_type_var
) {
109 /* Consider casts to be indirects */
110 if (instr
->deref_type
== nir_deref_type_cast
)
113 if ((instr
->deref_type
== nir_deref_type_array
||
114 instr
->deref_type
== nir_deref_type_ptr_as_array
) &&
115 !nir_src_is_const(instr
->arr
.index
))
118 instr
= nir_deref_instr_parent(instr
);
125 nir_deref_instr_ptr_as_array_stride(nir_deref_instr
*deref
)
127 assert(deref
->deref_type
== nir_deref_type_ptr_as_array
);
128 nir_deref_instr
*parent
= nir_deref_instr_parent(deref
);
129 switch (parent
->deref_type
) {
130 case nir_deref_type_array
:
131 return glsl_get_explicit_stride(nir_deref_instr_parent(parent
)->type
);
132 case nir_deref_type_ptr_as_array
:
133 return nir_deref_instr_ptr_as_array_stride(parent
);
134 case nir_deref_type_cast
:
135 return parent
->cast
.ptr_stride
;
137 unreachable("Invalid parent for ptr_as_array deref");
142 type_get_array_stride(const struct glsl_type
*elem_type
,
143 glsl_type_size_align_func size_align
)
145 unsigned elem_size
, elem_align
;
146 size_align(elem_type
, &elem_size
, &elem_align
);
147 return ALIGN_POT(elem_size
, elem_align
);
151 struct_type_get_field_offset(const struct glsl_type
*struct_type
,
152 glsl_type_size_align_func size_align
,
155 assert(glsl_type_is_struct(struct_type
));
157 for (unsigned i
= 0; i
<= field_idx
; i
++) {
158 unsigned elem_size
, elem_align
;
159 size_align(glsl_get_struct_field(struct_type
, i
), &elem_size
, &elem_align
);
160 offset
= ALIGN_POT(offset
, elem_align
);
168 nir_deref_instr_get_const_offset(nir_deref_instr
*deref
,
169 glsl_type_size_align_func size_align
)
172 nir_deref_path_init(&path
, deref
, NULL
);
174 assert(path
.path
[0]->deref_type
== nir_deref_type_var
);
177 for (nir_deref_instr
**p
= &path
.path
[1]; *p
; p
++) {
178 if ((*p
)->deref_type
== nir_deref_type_array
) {
179 offset
+= nir_src_as_uint((*p
)->arr
.index
) *
180 type_get_array_stride((*p
)->type
, size_align
);
181 } else if ((*p
)->deref_type
== nir_deref_type_struct
) {
182 /* p starts at path[1], so this is safe */
183 nir_deref_instr
*parent
= *(p
- 1);
184 offset
+= struct_type_get_field_offset(parent
->type
, size_align
,
187 unreachable("Unsupported deref type");
191 nir_deref_path_finish(&path
);
197 nir_build_deref_offset(nir_builder
*b
, nir_deref_instr
*deref
,
198 glsl_type_size_align_func size_align
)
201 nir_deref_path_init(&path
, deref
, NULL
);
203 assert(path
.path
[0]->deref_type
== nir_deref_type_var
);
205 nir_ssa_def
*offset
= nir_imm_int(b
, 0);
206 for (nir_deref_instr
**p
= &path
.path
[1]; *p
; p
++) {
207 if ((*p
)->deref_type
== nir_deref_type_array
) {
208 nir_ssa_def
*index
= nir_ssa_for_src(b
, (*p
)->arr
.index
, 1);
209 nir_ssa_def
*stride
=
210 nir_imm_int(b
, type_get_array_stride((*p
)->type
, size_align
));
211 offset
= nir_iadd(b
, offset
, nir_imul(b
, index
, stride
));
212 } else if ((*p
)->deref_type
== nir_deref_type_struct
) {
213 /* p starts at path[1], so this is safe */
214 nir_deref_instr
*parent
= *(p
- 1);
215 unsigned field_offset
=
216 struct_type_get_field_offset(parent
->type
, size_align
,
218 nir_iadd(b
, offset
, nir_imm_int(b
, field_offset
));
220 unreachable("Unsupported deref type");
224 nir_deref_path_finish(&path
);
230 nir_remove_dead_derefs_impl(nir_function_impl
*impl
)
232 bool progress
= false;
234 nir_foreach_block(block
, impl
) {
235 nir_foreach_instr_safe(instr
, block
) {
236 if (instr
->type
== nir_instr_type_deref
&&
237 nir_deref_instr_remove_if_unused(nir_instr_as_deref(instr
)))
243 nir_metadata_preserve(impl
, nir_metadata_block_index
|
244 nir_metadata_dominance
);
250 nir_remove_dead_derefs(nir_shader
*shader
)
252 bool progress
= false;
253 nir_foreach_function(function
, shader
) {
254 if (function
->impl
&& nir_remove_dead_derefs_impl(function
->impl
))
262 nir_fixup_deref_modes(nir_shader
*shader
)
264 nir_foreach_function(function
, shader
) {
268 nir_foreach_block(block
, function
->impl
) {
269 nir_foreach_instr(instr
, block
) {
270 if (instr
->type
!= nir_instr_type_deref
)
273 nir_deref_instr
*deref
= nir_instr_as_deref(instr
);
274 if (deref
->deref_type
== nir_deref_type_cast
)
277 nir_variable_mode parent_mode
;
278 if (deref
->deref_type
== nir_deref_type_var
) {
279 parent_mode
= deref
->var
->data
.mode
;
281 assert(deref
->parent
.is_ssa
);
282 nir_deref_instr
*parent
=
283 nir_instr_as_deref(deref
->parent
.ssa
->parent_instr
);
284 parent_mode
= parent
->mode
;
287 deref
->mode
= parent_mode
;
294 modes_may_alias(nir_variable_mode a
, nir_variable_mode b
)
296 /* Two pointers can only alias if they have the same mode.
298 * NOTE: In future, with things like OpenCL generic pointers, this may not
299 * be true and will have to be re-evaluated. However, with graphics only,
305 nir_deref_compare_result
306 nir_compare_deref_paths(nir_deref_path
*a_path
,
307 nir_deref_path
*b_path
)
309 if (!modes_may_alias(b_path
->path
[0]->mode
, a_path
->path
[0]->mode
))
310 return nir_derefs_do_not_alias
;
312 if (a_path
->path
[0]->deref_type
!= b_path
->path
[0]->deref_type
)
313 return nir_derefs_may_alias_bit
;
315 if (a_path
->path
[0]->deref_type
== nir_deref_type_var
) {
316 /* If we can chase the deref all the way back to the variable and
317 * they're not the same variable, we know they can't possibly alias.
319 if (a_path
->path
[0]->var
!= b_path
->path
[0]->var
)
320 return nir_derefs_do_not_alias
;
322 assert(a_path
->path
[0]->deref_type
== nir_deref_type_cast
);
323 /* If they're not exactly the same cast, it's hard to compare them so we
324 * just assume they alias. Comparing casts is tricky as there are lots
325 * of things such as mode, type, etc. to make sure work out; for now, we
326 * just assume nit_opt_deref will combine them and compare the deref
329 * TODO: At some point in the future, we could be clever and understand
330 * that a float[] and int[] have the same layout and aliasing structure
331 * but double[] and vec3[] do not and we could potentially be a bit
334 if (a_path
->path
[0] != b_path
->path
[0])
335 return nir_derefs_may_alias_bit
;
338 /* Start off assuming they fully compare. We ignore equality for now. In
339 * the end, we'll determine that by containment.
341 nir_deref_compare_result result
= nir_derefs_may_alias_bit
|
342 nir_derefs_a_contains_b_bit
|
343 nir_derefs_b_contains_a_bit
;
345 nir_deref_instr
**a_p
= &a_path
->path
[1];
346 nir_deref_instr
**b_p
= &b_path
->path
[1];
347 while (*a_p
!= NULL
&& *a_p
== *b_p
) {
352 /* We're at either the tail or the divergence point between the two deref
353 * paths. Look to see if either contains a ptr_as_array deref. It it
354 * does we don't know how to safely make any inferences. Hopefully,
355 * nir_opt_deref will clean most of these up and we can start inferring
358 * In theory, we could do a bit better. For instance, we could detect the
359 * case where we have exactly one ptr_as_array deref in the chain after the
360 * divergence point and it's matched in both chains and the two chains have
361 * different constant indices.
363 for (nir_deref_instr
**t_p
= a_p
; *t_p
; t_p
++) {
364 if ((*t_p
)->deref_type
== nir_deref_type_ptr_as_array
)
365 return nir_derefs_may_alias_bit
;
367 for (nir_deref_instr
**t_p
= b_p
; *t_p
; t_p
++) {
368 if ((*t_p
)->deref_type
== nir_deref_type_ptr_as_array
)
369 return nir_derefs_may_alias_bit
;
372 while (*a_p
!= NULL
&& *b_p
!= NULL
) {
373 nir_deref_instr
*a_tail
= *(a_p
++);
374 nir_deref_instr
*b_tail
= *(b_p
++);
376 switch (a_tail
->deref_type
) {
377 case nir_deref_type_array
:
378 case nir_deref_type_array_wildcard
: {
379 assert(b_tail
->deref_type
== nir_deref_type_array
||
380 b_tail
->deref_type
== nir_deref_type_array_wildcard
);
382 if (a_tail
->deref_type
== nir_deref_type_array_wildcard
) {
383 if (b_tail
->deref_type
!= nir_deref_type_array_wildcard
)
384 result
&= ~nir_derefs_b_contains_a_bit
;
385 } else if (b_tail
->deref_type
== nir_deref_type_array_wildcard
) {
386 if (a_tail
->deref_type
!= nir_deref_type_array_wildcard
)
387 result
&= ~nir_derefs_a_contains_b_bit
;
389 assert(a_tail
->deref_type
== nir_deref_type_array
&&
390 b_tail
->deref_type
== nir_deref_type_array
);
391 assert(a_tail
->arr
.index
.is_ssa
&& b_tail
->arr
.index
.is_ssa
);
393 if (nir_src_is_const(a_tail
->arr
.index
) &&
394 nir_src_is_const(b_tail
->arr
.index
)) {
395 /* If they're both direct and have different offsets, they
396 * don't even alias much less anything else.
398 if (nir_src_as_uint(a_tail
->arr
.index
) !=
399 nir_src_as_uint(b_tail
->arr
.index
))
400 return nir_derefs_do_not_alias
;
401 } else if (a_tail
->arr
.index
.ssa
== b_tail
->arr
.index
.ssa
) {
402 /* They're the same indirect, continue on */
404 /* They're not the same index so we can't prove anything about
407 result
&= ~(nir_derefs_a_contains_b_bit
| nir_derefs_b_contains_a_bit
);
413 case nir_deref_type_struct
: {
414 /* If they're different struct members, they don't even alias */
415 if (a_tail
->strct
.index
!= b_tail
->strct
.index
)
416 return nir_derefs_do_not_alias
;
421 unreachable("Invalid deref type");
425 /* If a is longer than b, then it can't contain b */
427 result
&= ~nir_derefs_a_contains_b_bit
;
429 result
&= ~nir_derefs_b_contains_a_bit
;
431 /* If a contains b and b contains a they must be equal. */
432 if ((result
& nir_derefs_a_contains_b_bit
) && (result
& nir_derefs_b_contains_a_bit
))
433 result
|= nir_derefs_equal_bit
;
438 nir_deref_compare_result
439 nir_compare_derefs(nir_deref_instr
*a
, nir_deref_instr
*b
)
442 return nir_derefs_equal_bit
| nir_derefs_may_alias_bit
|
443 nir_derefs_a_contains_b_bit
| nir_derefs_b_contains_a_bit
;
446 nir_deref_path a_path
, b_path
;
447 nir_deref_path_init(&a_path
, a
, NULL
);
448 nir_deref_path_init(&b_path
, b
, NULL
);
449 assert(a_path
.path
[0]->deref_type
== nir_deref_type_var
||
450 a_path
.path
[0]->deref_type
== nir_deref_type_cast
);
451 assert(b_path
.path
[0]->deref_type
== nir_deref_type_var
||
452 b_path
.path
[0]->deref_type
== nir_deref_type_cast
);
454 nir_deref_compare_result result
= nir_compare_deref_paths(&a_path
, &b_path
);
456 nir_deref_path_finish(&a_path
);
457 nir_deref_path_finish(&b_path
);
462 struct rematerialize_deref_state
{
466 struct hash_table
*cache
;
469 static nir_deref_instr
*
470 rematerialize_deref_in_block(nir_deref_instr
*deref
,
471 struct rematerialize_deref_state
*state
)
473 if (deref
->instr
.block
== state
->block
)
477 state
->cache
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
478 _mesa_key_pointer_equal
);
481 struct hash_entry
*cached
= _mesa_hash_table_search(state
->cache
, deref
);
485 nir_builder
*b
= &state
->builder
;
486 nir_deref_instr
*new_deref
=
487 nir_deref_instr_create(b
->shader
, deref
->deref_type
);
488 new_deref
->mode
= deref
->mode
;
489 new_deref
->type
= deref
->type
;
491 if (deref
->deref_type
== nir_deref_type_var
) {
492 new_deref
->var
= deref
->var
;
494 nir_deref_instr
*parent
= nir_src_as_deref(deref
->parent
);
496 parent
= rematerialize_deref_in_block(parent
, state
);
497 new_deref
->parent
= nir_src_for_ssa(&parent
->dest
.ssa
);
499 nir_src_copy(&new_deref
->parent
, &deref
->parent
, new_deref
);
503 switch (deref
->deref_type
) {
504 case nir_deref_type_var
:
505 case nir_deref_type_array_wildcard
:
506 case nir_deref_type_cast
:
507 /* Nothing more to do */
510 case nir_deref_type_array
:
511 assert(!nir_src_as_deref(deref
->arr
.index
));
512 nir_src_copy(&new_deref
->arr
.index
, &deref
->arr
.index
, new_deref
);
515 case nir_deref_type_struct
:
516 new_deref
->strct
.index
= deref
->strct
.index
;
520 unreachable("Invalid deref instruction type");
523 nir_ssa_dest_init(&new_deref
->instr
, &new_deref
->dest
,
524 deref
->dest
.ssa
.num_components
,
525 deref
->dest
.ssa
.bit_size
,
526 deref
->dest
.ssa
.name
);
527 nir_builder_instr_insert(b
, &new_deref
->instr
);
533 rematerialize_deref_src(nir_src
*src
, void *_state
)
535 struct rematerialize_deref_state
*state
= _state
;
537 nir_deref_instr
*deref
= nir_src_as_deref(*src
);
541 nir_deref_instr
*block_deref
= rematerialize_deref_in_block(deref
, state
);
542 if (block_deref
!= deref
) {
543 nir_instr_rewrite_src(src
->parent_instr
, src
,
544 nir_src_for_ssa(&block_deref
->dest
.ssa
));
545 nir_deref_instr_remove_if_unused(deref
);
546 state
->progress
= true;
552 /** Re-materialize derefs in every block
554 * This pass re-materializes deref instructions in every block in which it is
555 * used. After this pass has been run, every use of a deref will be of a
556 * deref in the same block as the use. Also, all unused derefs will be
557 * deleted as a side-effect.
560 nir_rematerialize_derefs_in_use_blocks_impl(nir_function_impl
*impl
)
562 struct rematerialize_deref_state state
= { 0 };
563 nir_builder_init(&state
.builder
, impl
);
565 nir_foreach_block(block
, impl
) {
568 /* Start each block with a fresh cache */
570 _mesa_hash_table_clear(state
.cache
, NULL
);
572 nir_foreach_instr_safe(instr
, block
) {
573 if (instr
->type
== nir_instr_type_deref
) {
574 nir_deref_instr_remove_if_unused(nir_instr_as_deref(instr
));
578 state
.builder
.cursor
= nir_before_instr(instr
);
579 nir_foreach_src(instr
, rematerialize_deref_src
, &state
);
583 nir_if
*following_if
= nir_block_get_following_if(block
);
585 assert(!nir_src_as_deref(following_if
->condition
));
589 _mesa_hash_table_destroy(state
.cache
, NULL
);
591 return state
.progress
;
595 is_trivial_deref_cast(nir_deref_instr
*cast
)
597 nir_deref_instr
*parent
= nir_src_as_deref(cast
->parent
);
601 return cast
->mode
== parent
->mode
&&
602 cast
->type
== parent
->type
&&
603 cast
->dest
.ssa
.num_components
== parent
->dest
.ssa
.num_components
&&
604 cast
->dest
.ssa
.bit_size
== parent
->dest
.ssa
.bit_size
;
608 is_trivial_array_deref_cast(nir_deref_instr
*cast
)
610 assert(is_trivial_deref_cast(cast
));
612 nir_deref_instr
*parent
= nir_src_as_deref(cast
->parent
);
614 if (parent
->deref_type
== nir_deref_type_array
) {
615 return cast
->cast
.ptr_stride
==
616 glsl_get_explicit_stride(nir_deref_instr_parent(parent
)->type
);
617 } else if (parent
->deref_type
== nir_deref_type_ptr_as_array
) {
618 return cast
->cast
.ptr_stride
==
619 nir_deref_instr_ptr_as_array_stride(parent
);
626 is_deref_ptr_as_array(nir_instr
*instr
)
628 return instr
->type
== nir_instr_type_deref
&&
629 nir_instr_as_deref(instr
)->deref_type
== nir_deref_type_ptr_as_array
;
633 opt_deref_cast(nir_deref_instr
*cast
)
635 if (!is_trivial_deref_cast(cast
))
638 bool trivial_array_cast
= is_trivial_array_deref_cast(cast
);
640 assert(cast
->dest
.is_ssa
);
641 assert(cast
->parent
.is_ssa
);
643 bool progress
= false;
644 nir_foreach_use_safe(use_src
, &cast
->dest
.ssa
) {
645 /* If this isn't a trivial array cast, we can't propagate into
646 * ptr_as_array derefs.
648 if (is_deref_ptr_as_array(use_src
->parent_instr
) &&
652 nir_instr_rewrite_src(use_src
->parent_instr
, use_src
, cast
->parent
);
656 /* If uses would be a bit crazy */
657 assert(list_empty(&cast
->dest
.ssa
.if_uses
));
659 nir_deref_instr_remove_if_unused(cast
);
664 opt_deref_ptr_as_array(nir_builder
*b
, nir_deref_instr
*deref
)
666 assert(deref
->deref_type
== nir_deref_type_ptr_as_array
);
668 nir_deref_instr
*parent
= nir_deref_instr_parent(deref
);
669 if (parent
->deref_type
!= nir_deref_type_array
&&
670 parent
->deref_type
!= nir_deref_type_ptr_as_array
)
673 assert(parent
->parent
.is_ssa
);
674 assert(parent
->arr
.index
.is_ssa
);
675 assert(deref
->arr
.index
.is_ssa
);
677 nir_ssa_def
*new_idx
= nir_iadd(b
, parent
->arr
.index
.ssa
,
678 deref
->arr
.index
.ssa
);
680 deref
->deref_type
= parent
->deref_type
;
681 nir_instr_rewrite_src(&deref
->instr
, &deref
->parent
, parent
->parent
);
682 nir_instr_rewrite_src(&deref
->instr
, &deref
->arr
.index
,
683 nir_src_for_ssa(new_idx
));
688 nir_opt_deref_impl(nir_function_impl
*impl
)
690 bool progress
= false;
693 nir_builder_init(&b
, impl
);
695 nir_foreach_block(block
, impl
) {
696 nir_foreach_instr_safe(instr
, block
) {
697 if (instr
->type
!= nir_instr_type_deref
)
700 b
.cursor
= nir_before_instr(instr
);
702 nir_deref_instr
*deref
= nir_instr_as_deref(instr
);
703 switch (deref
->deref_type
) {
704 case nir_deref_type_ptr_as_array
:
705 if (opt_deref_ptr_as_array(&b
, deref
))
709 case nir_deref_type_cast
:
710 if (opt_deref_cast(deref
))
722 nir_metadata_preserve(impl
, nir_metadata_block_index
|
723 nir_metadata_dominance
);
730 nir_opt_deref(nir_shader
*shader
)
732 bool progress
= false;
734 nir_foreach_function(func
, shader
) {
735 if (func
->impl
&& nir_opt_deref_impl(func
->impl
))