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_interp_deref_at_vertex
&&
84 intrin
->intrinsic
!= nir_intrinsic_store_deref
)
87 nir_deref_instr
*deref
= nir_src_as_deref(intrin
->src
[0]);
88 if (!(deref
->mode
& modes
))
91 /* We only care about array derefs that act on vectors */
92 if (deref
->deref_type
!= nir_deref_type_array
)
95 nir_deref_instr
*vec_deref
= nir_deref_instr_parent(deref
);
96 if (!glsl_type_is_vector(vec_deref
->type
))
99 assert(intrin
->num_components
== 1);
100 unsigned num_components
= glsl_get_components(vec_deref
->type
);
101 assert(num_components
> 1 && num_components
<= NIR_MAX_VEC_COMPONENTS
);
103 b
.cursor
= nir_after_instr(&intrin
->instr
);
105 if (intrin
->intrinsic
== nir_intrinsic_store_deref
) {
106 assert(intrin
->src
[1].is_ssa
);
107 nir_ssa_def
*value
= intrin
->src
[1].ssa
;
109 if (nir_src_is_const(deref
->arr
.index
)) {
110 if (!(options
& nir_lower_direct_array_deref_of_vec_store
))
113 unsigned index
= nir_src_as_uint(deref
->arr
.index
);
114 /* If index is OOB, we throw the old store away and don't
115 * replace it with anything.
117 if (index
< num_components
)
118 build_write_masked_store(&b
, vec_deref
, value
, index
);
120 if (!(options
& nir_lower_indirect_array_deref_of_vec_store
))
123 nir_ssa_def
*index
= nir_ssa_for_src(&b
, deref
->arr
.index
, 1);
124 build_write_masked_stores(&b
, vec_deref
, value
, index
,
127 nir_instr_remove(&intrin
->instr
);
131 if (nir_src_is_const(deref
->arr
.index
)) {
132 if (!(options
& nir_lower_direct_array_deref_of_vec_load
))
135 if (!(options
& nir_lower_indirect_array_deref_of_vec_load
))
139 /* Turn the load into a vector load */
140 nir_instr_rewrite_src(&intrin
->instr
, &intrin
->src
[0],
141 nir_src_for_ssa(&vec_deref
->dest
.ssa
));
142 intrin
->dest
.ssa
.num_components
= num_components
;
143 intrin
->num_components
= num_components
;
145 nir_ssa_def
*index
= nir_ssa_for_src(&b
, deref
->arr
.index
, 1);
146 nir_ssa_def
*scalar
=
147 nir_vector_extract(&b
, &intrin
->dest
.ssa
, index
);
148 if (scalar
->parent_instr
->type
== nir_instr_type_ssa_undef
) {
149 nir_ssa_def_rewrite_uses(&intrin
->dest
.ssa
,
150 nir_src_for_ssa(scalar
));
151 nir_instr_remove(&intrin
->instr
);
153 nir_ssa_def_rewrite_uses_after(&intrin
->dest
.ssa
,
154 nir_src_for_ssa(scalar
),
155 scalar
->parent_instr
);
163 nir_metadata_preserve(impl
, nir_metadata_block_index
|
164 nir_metadata_dominance
);
170 /* Lowers away array dereferences on vectors
172 * These are allowed on certain variable types such as SSBOs and TCS outputs.
173 * However, not everyone can actually handle them everywhere. There are also
174 * cases where we want to lower them for performance reasons.
176 * This patch assumes that copy_deref instructions have already been lowered.
179 nir_lower_array_deref_of_vec(nir_shader
*shader
, nir_variable_mode modes
,
180 nir_lower_array_deref_of_vec_options options
)
182 bool progress
= false;
184 nir_foreach_function(function
, shader
) {
185 if (function
->impl
&&
186 nir_lower_array_deref_of_vec_impl(function
->impl
, modes
, options
))