2 * Copyright © 2019 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"
28 build_write_masked_store(nir_builder
*b
, nir_deref_instr
*vec_deref
,
29 nir_ssa_def
*value
, unsigned component
)
31 assert(value
->num_components
== 1);
32 unsigned num_components
= glsl_get_components(vec_deref
->type
);
33 assert(num_components
> 1 && num_components
<= NIR_MAX_VEC_COMPONENTS
);
35 nir_ssa_def
*u
= nir_ssa_undef(b
, 1, value
->bit_size
);
36 nir_ssa_def
*comps
[NIR_MAX_VEC_COMPONENTS
];
37 for (unsigned i
= 0; i
< num_components
; i
++)
38 comps
[i
] = (i
== component
) ? value
: u
;
40 nir_ssa_def
*vec
= nir_vec(b
, comps
, num_components
);
41 nir_store_deref(b
, vec_deref
, vec
, (1u << component
));
45 build_write_masked_stores(nir_builder
*b
, nir_deref_instr
*vec_deref
,
46 nir_ssa_def
*value
, nir_ssa_def
*index
,
47 unsigned start
, unsigned end
)
49 if (start
== end
- 1) {
50 build_write_masked_store(b
, vec_deref
, value
, start
);
52 unsigned mid
= start
+ (end
- start
) / 2;
53 nir_push_if(b
, nir_ilt(b
, index
, nir_imm_int(b
, mid
)));
54 build_write_masked_stores(b
, vec_deref
, value
, index
, start
, mid
);
55 nir_push_else(b
, NULL
);
56 build_write_masked_stores(b
, vec_deref
, value
, index
, mid
, end
);
62 nir_lower_array_deref_of_vec_impl(nir_function_impl
*impl
,
63 nir_variable_mode modes
,
64 nir_lower_array_deref_of_vec_options options
)
66 bool progress
= false;
69 nir_builder_init(&b
, impl
);
71 nir_foreach_block(block
, impl
) {
72 nir_foreach_instr_safe(instr
, block
) {
73 if (instr
->type
!= nir_instr_type_intrinsic
)
76 nir_intrinsic_instr
*intrin
= nir_instr_as_intrinsic(instr
);
77 assert(intrin
->intrinsic
!= nir_intrinsic_copy_deref
);
79 if (intrin
->intrinsic
!= nir_intrinsic_load_deref
&&
80 intrin
->intrinsic
!= nir_intrinsic_interp_deref_at_centroid
&&
81 intrin
->intrinsic
!= nir_intrinsic_interp_deref_at_sample
&&
82 intrin
->intrinsic
!= nir_intrinsic_interp_deref_at_offset
&&
83 intrin
->intrinsic
!= nir_intrinsic_store_deref
)
86 nir_deref_instr
*deref
= nir_src_as_deref(intrin
->src
[0]);
87 if (!(deref
->mode
& modes
))
90 /* We only care about array derefs that act on vectors */
91 if (deref
->deref_type
!= nir_deref_type_array
)
94 nir_deref_instr
*vec_deref
= nir_deref_instr_parent(deref
);
95 if (!glsl_type_is_vector(vec_deref
->type
))
98 assert(intrin
->num_components
== 1);
99 unsigned num_components
= glsl_get_components(vec_deref
->type
);
100 assert(num_components
> 1 && num_components
<= NIR_MAX_VEC_COMPONENTS
);
102 b
.cursor
= nir_after_instr(&intrin
->instr
);
104 if (intrin
->intrinsic
== nir_intrinsic_store_deref
) {
105 assert(intrin
->src
[1].is_ssa
);
106 nir_ssa_def
*value
= intrin
->src
[1].ssa
;
108 if (nir_src_is_const(deref
->arr
.index
)) {
109 if (!(options
& nir_lower_direct_array_deref_of_vec_store
))
112 unsigned index
= nir_src_as_uint(deref
->arr
.index
);
113 /* If index is OOB, we throw the old store away and don't
114 * replace it with anything.
116 if (index
< num_components
)
117 build_write_masked_store(&b
, vec_deref
, value
, index
);
119 if (!(options
& nir_lower_indirect_array_deref_of_vec_store
))
122 nir_ssa_def
*index
= nir_ssa_for_src(&b
, deref
->arr
.index
, 1);
123 build_write_masked_stores(&b
, vec_deref
, value
, index
,
126 nir_instr_remove(&intrin
->instr
);
130 if (nir_src_is_const(deref
->arr
.index
)) {
131 if (!(options
& nir_lower_direct_array_deref_of_vec_load
))
134 if (!(options
& nir_lower_indirect_array_deref_of_vec_load
))
138 /* Turn the load into a vector load */
139 nir_instr_rewrite_src(&intrin
->instr
, &intrin
->src
[0],
140 nir_src_for_ssa(&vec_deref
->dest
.ssa
));
141 intrin
->dest
.ssa
.num_components
= num_components
;
142 intrin
->num_components
= num_components
;
144 nir_ssa_def
*index
= nir_ssa_for_src(&b
, deref
->arr
.index
, 1);
145 nir_ssa_def
*scalar
=
146 nir_vector_extract(&b
, &intrin
->dest
.ssa
, index
);
147 if (scalar
->parent_instr
->type
== nir_instr_type_ssa_undef
) {
148 nir_ssa_def_rewrite_uses(&intrin
->dest
.ssa
,
149 nir_src_for_ssa(scalar
));
150 nir_instr_remove(&intrin
->instr
);
152 nir_ssa_def_rewrite_uses_after(&intrin
->dest
.ssa
,
153 nir_src_for_ssa(scalar
),
154 scalar
->parent_instr
);
162 nir_metadata_preserve(impl
, nir_metadata_block_index
|
163 nir_metadata_dominance
);
169 /* Lowers away array dereferences on vectors
171 * These are allowed on certain variable types such as SSBOs and TCS outputs.
172 * However, not everyone can actually handle them everywhere. There are also
173 * cases where we want to lower them for performance reasons.
175 * This patch assumes that copy_deref instructions have already been lowered.
178 nir_lower_array_deref_of_vec(nir_shader
*shader
, nir_variable_mode modes
,
179 nir_lower_array_deref_of_vec_options options
)
181 bool progress
= false;
183 nir_foreach_function(function
, shader
) {
184 if (function
->impl
&&
185 nir_lower_array_deref_of_vec_impl(function
->impl
, modes
, options
))