1 /*
2 * Copyright © 2015 Connor Abbott
3 *
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:
10 *
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
13 * Software.
14 *
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
21 * IN THE SOFTWARE.
22 *
23 */
24
25 /**
26 * nir_opt_vectorize() aims to vectorize ALU instructions.
27 *
28 * The default vectorization width is 4.
29 * If desired, a callback function which returns the max vectorization width
30 * per instruction can be provided.
31 *
32 * The max vectorization width must be a power of 2.
33 */
34
35 #include "util/u_dynarray.h"
36 #include "nir.h"
37 #include "nir_builder.h"
38 #include "nir_vla.h"
39
40 #define HASH(hash, data) XXH32(&data, sizeof(data), hash)
41
42 static uint32_t
hash_src(uint32_t hash,const nir_src * src)43 hash_src(uint32_t hash, const nir_src *src)
44 {
45 void *hash_data = nir_src_is_const(*src) ? NULL : src->ssa;
46
47 return HASH(hash, hash_data);
48 }
49
50 static uint32_t
hash_alu_src(uint32_t hash,const nir_alu_src * src,uint32_t num_components,uint32_t max_vec)51 hash_alu_src(uint32_t hash, const nir_alu_src *src,
52 uint32_t num_components, uint32_t max_vec)
53 {
54 /* hash whether a swizzle accesses elements beyond the maximum
55 * vectorization factor:
56 * For example accesses to .x and .y are considered different variables
57 * compared to accesses to .z and .w for 16-bit vec2.
58 */
59 uint32_t swizzle = (src->swizzle[0] & ~(max_vec - 1));
60 hash = HASH(hash, swizzle);
61
62 return hash_src(hash, &src->src);
63 }
64
65 static uint32_t
hash_instr(const void * data)66 hash_instr(const void *data)
67 {
68 const nir_instr *instr = (nir_instr *)data;
69 assert(instr->type == nir_instr_type_alu);
70 nir_alu_instr *alu = nir_instr_as_alu(instr);
71
72 uint32_t hash = HASH(0, alu->op);
73 hash = HASH(hash, alu->def.bit_size);
74
75 for (unsigned i = 0; i < nir_op_infos[alu->op].num_inputs; i++)
76 hash = hash_alu_src(hash, &alu->src[i],
77 alu->def.num_components,
78 instr->pass_flags);
79
80 return hash;
81 }
82
83 static bool
srcs_equal(const nir_src * src1,const nir_src * src2)84 srcs_equal(const nir_src *src1, const nir_src *src2)
85 {
86
87 return src1->ssa == src2->ssa ||
88 (nir_src_is_const(*src1) && nir_src_is_const(*src2));
89 }
90
91 static bool
alu_srcs_equal(const nir_alu_src * src1,const nir_alu_src * src2,uint32_t max_vec)92 alu_srcs_equal(const nir_alu_src *src1, const nir_alu_src *src2,
93 uint32_t max_vec)
94 {
95 uint32_t mask = ~(max_vec - 1);
96 if ((src1->swizzle[0] & mask) != (src2->swizzle[0] & mask))
97 return false;
98
99 return srcs_equal(&src1->src, &src2->src);
100 }
101
102 static bool
instrs_equal(const void * data1,const void * data2)103 instrs_equal(const void *data1, const void *data2)
104 {
105 const nir_instr *instr1 = (nir_instr *)data1;
106 const nir_instr *instr2 = (nir_instr *)data2;
107 assert(instr1->type == nir_instr_type_alu);
108 assert(instr2->type == nir_instr_type_alu);
109
110 nir_alu_instr *alu1 = nir_instr_as_alu(instr1);
111 nir_alu_instr *alu2 = nir_instr_as_alu(instr2);
112
113 if (alu1->op != alu2->op)
114 return false;
115
116 if (alu1->def.bit_size != alu2->def.bit_size)
117 return false;
118
119 for (unsigned i = 0; i < nir_op_infos[alu1->op].num_inputs; i++) {
120 if (!alu_srcs_equal(&alu1->src[i], &alu2->src[i], instr1->pass_flags))
121 return false;
122 }
123
124 return true;
125 }
126
127 static bool
instr_can_rewrite(nir_instr * instr)128 instr_can_rewrite(nir_instr *instr)
129 {
130 switch (instr->type) {
131 case nir_instr_type_alu: {
132 nir_alu_instr *alu = nir_instr_as_alu(instr);
133
134 /* Don't try and vectorize mov's. Either they'll be handled by copy
135 * prop, or they're actually necessary and trying to vectorize them
136 * would result in fighting with copy prop.
137 */
138 if (alu->op == nir_op_mov)
139 return false;
140
141 /* no need to hash instructions which are already vectorized */
142 if (alu->def.num_components >= instr->pass_flags)
143 return false;
144
145 if (nir_op_infos[alu->op].output_size != 0)
146 return false;
147
148 for (unsigned i = 0; i < nir_op_infos[alu->op].num_inputs; i++) {
149 if (nir_op_infos[alu->op].input_sizes[i] != 0)
150 return false;
151
152 /* don't hash instructions which are already swizzled
153 * outside of max_components: these should better be scalarized */
154 uint32_t mask = ~(instr->pass_flags - 1);
155 for (unsigned j = 1; j < alu->def.num_components; j++) {
156 if ((alu->src[i].swizzle[0] & mask) != (alu->src[i].swizzle[j] & mask))
157 return false;
158 }
159 }
160
161 return true;
162 }
163
164 /* TODO support phi nodes */
165 default:
166 break;
167 }
168
169 return false;
170 }
171
172 /*
173 * Tries to combine two instructions whose sources are different components of
174 * the same instructions into one vectorized instruction. Note that instr1
175 * should dominate instr2.
176 */
177 static nir_instr *
instr_try_combine(struct set * instr_set,nir_instr * instr1,nir_instr * instr2)178 instr_try_combine(struct set *instr_set, nir_instr *instr1, nir_instr *instr2)
179 {
180 assert(instr1->type == nir_instr_type_alu);
181 assert(instr2->type == nir_instr_type_alu);
182 nir_alu_instr *alu1 = nir_instr_as_alu(instr1);
183 nir_alu_instr *alu2 = nir_instr_as_alu(instr2);
184
185 assert(alu1->def.bit_size == alu2->def.bit_size);
186 unsigned alu1_components = alu1->def.num_components;
187 unsigned alu2_components = alu2->def.num_components;
188 unsigned total_components = alu1_components + alu2_components;
189
190 assert(instr1->pass_flags == instr2->pass_flags);
191 if (total_components > instr1->pass_flags)
192 return NULL;
193
194 nir_builder b = nir_builder_at(nir_after_instr(instr1));
195
196 nir_alu_instr *new_alu = nir_alu_instr_create(b.shader, alu1->op);
197 nir_def_init(&new_alu->instr, &new_alu->def, total_components,
198 alu1->def.bit_size);
199 new_alu->instr.pass_flags = alu1->instr.pass_flags;
200
201 /* If either channel is exact, we have to preserve it even if it's
202 * not optimal for other channels.
203 */
204 new_alu->exact = alu1->exact || alu2->exact;
205
206 /* If all channels don't wrap, we can say that the whole vector doesn't
207 * wrap.
208 */
209 new_alu->no_signed_wrap = alu1->no_signed_wrap && alu2->no_signed_wrap;
210 new_alu->no_unsigned_wrap = alu1->no_unsigned_wrap && alu2->no_unsigned_wrap;
211
212 for (unsigned i = 0; i < nir_op_infos[alu1->op].num_inputs; i++) {
213 /* handle constant merging case */
214 if (alu1->src[i].src.ssa != alu2->src[i].src.ssa) {
215 nir_const_value *c1 = nir_src_as_const_value(alu1->src[i].src);
216 nir_const_value *c2 = nir_src_as_const_value(alu2->src[i].src);
217 assert(c1 && c2);
218 nir_const_value value[NIR_MAX_VEC_COMPONENTS];
219 unsigned bit_size = alu1->src[i].src.ssa->bit_size;
220
221 for (unsigned j = 0; j < total_components; j++) {
222 value[j].u64 = j < alu1_components ? c1[alu1->src[i].swizzle[j]].u64 : c2[alu2->src[i].swizzle[j - alu1_components]].u64;
223 }
224 nir_def *def = nir_build_imm(&b, total_components, bit_size, value);
225
226 new_alu->src[i].src = nir_src_for_ssa(def);
227 for (unsigned j = 0; j < total_components; j++)
228 new_alu->src[i].swizzle[j] = j;
229 continue;
230 }
231
232 new_alu->src[i].src = alu1->src[i].src;
233
234 for (unsigned j = 0; j < alu1_components; j++)
235 new_alu->src[i].swizzle[j] = alu1->src[i].swizzle[j];
236
237 for (unsigned j = 0; j < alu2_components; j++) {
238 new_alu->src[i].swizzle[j + alu1_components] =
239 alu2->src[i].swizzle[j];
240 }
241 }
242
243 nir_builder_instr_insert(&b, &new_alu->instr);
244
245 /* update all ALU uses */
246 nir_foreach_use_safe(src, &alu1->def) {
247 nir_instr *user_instr = nir_src_parent_instr(src);
248 if (user_instr->type == nir_instr_type_alu) {
249 /* Check if user is found in the hashset */
250 struct set_entry *entry = _mesa_set_search(instr_set, user_instr);
251
252 /* For ALU instructions, rewrite the source directly to avoid a
253 * round-trip through copy propagation.
254 */
255 nir_src_rewrite(src, &new_alu->def);
256
257 /* Rehash user if it was found in the hashset */
258 if (entry && entry->key == user_instr) {
259 _mesa_set_remove(instr_set, entry);
260 _mesa_set_add(instr_set, user_instr);
261 }
262 }
263 }
264
265 nir_foreach_use_safe(src, &alu2->def) {
266 if (nir_src_parent_instr(src)->type == nir_instr_type_alu) {
267 /* For ALU instructions, rewrite the source directly to avoid a
268 * round-trip through copy propagation.
269 */
270 nir_src_rewrite(src, &new_alu->def);
271
272 nir_alu_src *alu_src = container_of(src, nir_alu_src, src);
273 nir_alu_instr *use = nir_instr_as_alu(nir_src_parent_instr(src));
274 unsigned components = nir_ssa_alu_instr_src_components(use, alu_src - use->src);
275 for (unsigned i = 0; i < components; i++)
276 alu_src->swizzle[i] += alu1_components;
277 }
278 }
279
280 /* update all other uses if there are any */
281 unsigned swiz[NIR_MAX_VEC_COMPONENTS];
282
283 if (!nir_def_is_unused(&alu1->def)) {
284 for (unsigned i = 0; i < alu1_components; i++)
285 swiz[i] = i;
286 nir_def *new_alu1 = nir_swizzle(&b, &new_alu->def, swiz,
287 alu1_components);
288 nir_def_rewrite_uses(&alu1->def, new_alu1);
289 }
290
291 if (!nir_def_is_unused(&alu2->def)) {
292 for (unsigned i = 0; i < alu2_components; i++)
293 swiz[i] = i + alu1_components;
294 nir_def *new_alu2 = nir_swizzle(&b, &new_alu->def, swiz,
295 alu2_components);
296 nir_def_rewrite_uses(&alu2->def, new_alu2);
297 }
298
299 nir_instr_remove(instr1);
300 nir_instr_remove(instr2);
301
302 return &new_alu->instr;
303 }
304
305 static struct set *
vec_instr_set_create(void)306 vec_instr_set_create(void)
307 {
308 return _mesa_set_create(NULL, hash_instr, instrs_equal);
309 }
310
311 static void
vec_instr_set_destroy(struct set * instr_set)312 vec_instr_set_destroy(struct set *instr_set)
313 {
314 _mesa_set_destroy(instr_set, NULL);
315 }
316
317 static bool
vec_instr_set_add_or_rewrite(struct set * instr_set,nir_instr * instr,nir_vectorize_cb filter,void * data)318 vec_instr_set_add_or_rewrite(struct set *instr_set, nir_instr *instr,
319 nir_vectorize_cb filter, void *data)
320 {
321 /* set max vector to instr pass flags: this is used to hash swizzles */
322 instr->pass_flags = filter ? filter(instr, data) : 4;
323 assert(util_is_power_of_two_or_zero(instr->pass_flags));
324
325 if (!instr_can_rewrite(instr))
326 return false;
327
328 struct set_entry *entry = _mesa_set_search(instr_set, instr);
329 if (entry) {
330 nir_instr *old_instr = (nir_instr *)entry->key;
331 _mesa_set_remove(instr_set, entry);
332 nir_instr *new_instr = instr_try_combine(instr_set, old_instr, instr);
333 if (new_instr) {
334 if (instr_can_rewrite(new_instr))
335 _mesa_set_add(instr_set, new_instr);
336 return true;
337 }
338 }
339
340 _mesa_set_add(instr_set, instr);
341 return false;
342 }
343
344 static bool
vectorize_block(nir_block * block,struct set * instr_set,nir_vectorize_cb filter,void * data)345 vectorize_block(nir_block *block, struct set *instr_set,
346 nir_vectorize_cb filter, void *data)
347 {
348 bool progress = false;
349
350 nir_foreach_instr_safe(instr, block) {
351 if (vec_instr_set_add_or_rewrite(instr_set, instr, filter, data))
352 progress = true;
353 }
354
355 for (unsigned i = 0; i < block->num_dom_children; i++) {
356 nir_block *child = block->dom_children[i];
357 progress |= vectorize_block(child, instr_set, filter, data);
358 }
359
360 nir_foreach_instr_reverse(instr, block) {
361 if (instr_can_rewrite(instr))
362 _mesa_set_remove_key(instr_set, instr);
363 }
364
365 return progress;
366 }
367
368 static bool
nir_opt_vectorize_impl(nir_function_impl * impl,nir_vectorize_cb filter,void * data)369 nir_opt_vectorize_impl(nir_function_impl *impl,
370 nir_vectorize_cb filter, void *data)
371 {
372 struct set *instr_set = vec_instr_set_create();
373
374 nir_metadata_require(impl, nir_metadata_dominance);
375
376 bool progress = vectorize_block(nir_start_block(impl), instr_set,
377 filter, data);
378
379 if (progress) {
380 nir_metadata_preserve(impl, nir_metadata_block_index |
381 nir_metadata_dominance);
382 } else {
383 nir_metadata_preserve(impl, nir_metadata_all);
384 }
385
386 vec_instr_set_destroy(instr_set);
387 return progress;
388 }
389
390 bool
nir_opt_vectorize(nir_shader * shader,nir_vectorize_cb filter,void * data)391 nir_opt_vectorize(nir_shader *shader, nir_vectorize_cb filter,
392 void *data)
393 {
394 bool progress = false;
395
396 nir_foreach_function_impl(impl, shader) {
397 progress |= nir_opt_vectorize_impl(impl, filter, data);
398 }
399
400 return progress;
401 }
402