1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
|
/*
* Copyright © 2020 Intel Corporation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice (including the next
* paragraph) shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
#include "nir_builder.h"
static bool
opt_memcpy_deref_cast(nir_intrinsic_instr *cpy, nir_src *deref_src)
{
assert(cpy->intrinsic == nir_intrinsic_memcpy_deref);
nir_deref_instr *cast = nir_src_as_deref(*deref_src);
if (cast == NULL || cast->deref_type != nir_deref_type_cast)
return false;
/* We always have to replace the source with a deref, not a bare uint
* pointer. If it's the first deref in the chain, bail.
*/
nir_deref_instr *parent = nir_src_as_deref(cast->parent);
if (parent == NULL)
return false;
/* If it has useful alignment information, we want to keep that */
if (cast->cast.align_mul > 0)
return false;
/* Casts to uint8 or int8 never do us any good; get rid of them */
if (cast->type == glsl_int8_t_type() ||
cast->type == glsl_uint8_t_type()) {
nir_instr_rewrite_src(&cpy->instr, deref_src,
nir_src_for_ssa(&parent->dest.ssa));
return true;
}
int64_t parent_type_size = glsl_get_explicit_size(parent->type, false);
if (parent_type_size < 0)
return false;
if (!nir_src_is_const(cpy->src[2]))
return false;
/* We don't want to get rid of the cast if the resulting type would be
* smaller than the amount of data we're copying.
*/
if (nir_src_as_uint(cpy->src[2]) < (uint64_t)parent_type_size)
return false;
nir_instr_rewrite_src(&cpy->instr, deref_src,
nir_src_for_ssa(&parent->dest.ssa));
return true;
}
static bool
type_is_tightly_packed(const struct glsl_type *type, unsigned *size_out)
{
unsigned size = 0;
if (glsl_type_is_struct_or_ifc(type)) {
unsigned num_fields = glsl_get_length(type);
for (unsigned i = 0; i < num_fields; i++) {
const struct glsl_struct_field *field =
glsl_get_struct_field_data(type, i);
if (field->offset < 0 || field->offset != size)
return false;
unsigned field_size;
if (!type_is_tightly_packed(field->type, &field_size))
return false;
size = field->offset + field_size;
}
} else if (glsl_type_is_array_or_matrix(type)) {
if (glsl_type_is_unsized_array(type))
return false;
unsigned stride = glsl_get_explicit_stride(type);
if (stride == 0)
return false;
const struct glsl_type *elem_type = glsl_get_array_element(type);
unsigned elem_size;
if (!type_is_tightly_packed(elem_type, &elem_size))
return false;
if (elem_size != stride)
return false;
size = stride * glsl_get_length(type);
} else {
assert(glsl_type_is_vector_or_scalar(type));
if (glsl_get_explicit_stride(type) > 0)
return false;
if (glsl_type_is_boolean(type))
return false;
size = glsl_get_explicit_size(type, false);
}
if (size_out)
*size_out = size;
return true;
}
static bool
try_lower_memcpy(nir_builder *b, nir_intrinsic_instr *cpy,
struct set *complex_vars)
{
nir_deref_instr *dst = nir_src_as_deref(cpy->src[0]);
nir_deref_instr *src = nir_src_as_deref(cpy->src[1]);
/* A self-copy can always be eliminated */
if (dst == src) {
nir_instr_remove(&cpy->instr);
return true;
}
if (!nir_src_is_const(cpy->src[2]))
return false;
uint64_t size = nir_src_as_uint(cpy->src[2]);
if (size == 0) {
nir_instr_remove(&cpy->instr);
return true;
}
if (glsl_type_is_vector_or_scalar(src->type) &&
glsl_type_is_vector_or_scalar(dst->type) &&
glsl_get_explicit_size(dst->type, false) == size &&
glsl_get_explicit_size(src->type, false) == size) {
b->cursor = nir_instr_remove(&cpy->instr);
nir_ssa_def *data =
nir_load_deref_with_access(b, src, nir_intrinsic_src_access(cpy));
data = nir_bitcast_vector(b, data, glsl_get_bit_size(dst->type));
assert(data->num_components == glsl_get_vector_elements(dst->type));
nir_store_deref_with_access(b, dst, data, ~0 /* write mask */,
nir_intrinsic_dst_access(cpy));
return true;
}
unsigned type_size;
if (dst->type == src->type &&
type_is_tightly_packed(dst->type, &type_size) &&
type_size == size) {
b->cursor = nir_instr_remove(&cpy->instr);
nir_copy_deref_with_access(b, dst, src,
nir_intrinsic_dst_access(cpy),
nir_intrinsic_src_access(cpy));
return true;
}
/* If one of the two types is tightly packed and happens to equal the
* memcpy size, then we can get the memcpy by casting to that type and
* doing a deref copy.
*
* However, if we blindly apply this logic, we may end up with extra casts
* where we don't want them. The whole point of converting memcpy to
* copy_deref is in the hopes that nir_opt_copy_prop_vars or
* nir_lower_vars_to_ssa will get rid of the copy and those passes don't
* handle casts well. Heuristically, only do this optimization if the
* tightly packed type is on a deref with nir_var_function_temp so we stick
* the cast on the other mode.
*/
if (dst->modes == nir_var_function_temp &&
type_is_tightly_packed(dst->type, &type_size) &&
type_size == size) {
b->cursor = nir_instr_remove(&cpy->instr);
src = nir_build_deref_cast(b, &src->dest.ssa,
src->modes, dst->type, 0);
nir_copy_deref_with_access(b, dst, src,
nir_intrinsic_dst_access(cpy),
nir_intrinsic_src_access(cpy));
return true;
}
/* If we can get at the variable AND the only complex use of that variable
* is as a memcpy destination, then we don't have to care about any empty
* space in the variable. In particular, we know that the variable is never
* cast to any other type and it's never used as a memcpy source so nothing
* can see any padding bytes. This holds even if some other memcpy only
* writes to part of the variable.
*/
if (dst->deref_type == nir_deref_type_var &&
dst->modes == nir_var_function_temp &&
_mesa_set_search(complex_vars, dst->var) == NULL &&
glsl_get_explicit_size(dst->type, false) <= size) {
b->cursor = nir_instr_remove(&cpy->instr);
src = nir_build_deref_cast(b, &src->dest.ssa,
src->modes, dst->type, 0);
nir_copy_deref_with_access(b, dst, src,
nir_intrinsic_dst_access(cpy),
nir_intrinsic_src_access(cpy));
return true;
}
if (src->modes == nir_var_function_temp &&
type_is_tightly_packed(src->type, &type_size) &&
type_size == size) {
b->cursor = nir_instr_remove(&cpy->instr);
dst = nir_build_deref_cast(b, &dst->dest.ssa,
dst->modes, src->type, 0);
nir_copy_deref_with_access(b, dst, src,
nir_intrinsic_dst_access(cpy),
nir_intrinsic_src_access(cpy));
return true;
}
return false;
}
static bool
opt_memcpy_impl(nir_function_impl *impl)
{
bool progress = false;
nir_builder b;
nir_builder_init(&b, impl);
struct set *complex_vars = _mesa_pointer_set_create(NULL);
nir_foreach_block(block, impl) {
nir_foreach_instr(instr, block) {
if (instr->type != nir_instr_type_deref)
continue;
nir_deref_instr *deref = nir_instr_as_deref(instr);
if (deref->deref_type != nir_deref_type_var)
continue;
nir_deref_instr_has_complex_use_options opts =
nir_deref_instr_has_complex_use_allow_memcpy_dst;
if (nir_deref_instr_has_complex_use(deref, opts))
_mesa_set_add(complex_vars, deref->var);
}
}
nir_foreach_block(block, impl) {
nir_foreach_instr_safe(instr, block) {
if (instr->type != nir_instr_type_intrinsic)
continue;
nir_intrinsic_instr *cpy = nir_instr_as_intrinsic(instr);
if (cpy->intrinsic != nir_intrinsic_memcpy_deref)
continue;
while (opt_memcpy_deref_cast(cpy, &cpy->src[0]))
progress = true;
while (opt_memcpy_deref_cast(cpy, &cpy->src[1]))
progress = true;
if (try_lower_memcpy(&b, cpy, complex_vars)) {
progress = true;
continue;
}
}
}
_mesa_set_destroy(complex_vars, NULL);
if (progress) {
nir_metadata_preserve(impl, nir_metadata_block_index |
nir_metadata_dominance);
} else {
nir_metadata_preserve(impl, nir_metadata_all);
}
return progress;
}
bool
nir_opt_memcpy(nir_shader *shader)
{
bool progress = false;
nir_foreach_function(function, shader) {
if (function->impl && opt_memcpy_impl(function->impl))
progress = true;
}
return progress;
}
|