• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright © 2014-2015 Broadcom
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 #include "nir.h"
25 #include "nir_builder.h"
26 
27 struct alu_width_data {
28    nir_vectorize_cb cb;
29    const void *data;
30 };
31 
32 /** @file nir_lower_alu_width.c
33  *
34  * Replaces nir_alu_instr operations with more than one channel used in the
35  * arguments with individual per-channel operations.
36  *
37  * Optionally, a callback function which returns the max vectorization width
38  * per instruction can be provided.
39  *
40  * The max vectorization width must be a power of 2.
41  */
42 
43 static bool
inst_is_vector_alu(const nir_instr * instr,const void * _state)44 inst_is_vector_alu(const nir_instr *instr, const void *_state)
45 {
46    if (instr->type != nir_instr_type_alu)
47       return false;
48 
49    nir_alu_instr *alu = nir_instr_as_alu(instr);
50 
51    /* There is no ALU instruction which has a scalar destination, scalar
52     * src[0], and some other vector source.
53     */
54    return alu->def.num_components > 1 ||
55           nir_op_infos[alu->op].input_sizes[0] > 1;
56 }
57 
58 /* Checks whether all operands of an ALU instruction are swizzled
59  * within the targeted vectorization width.
60  *
61  * The assumption here is that a vecN instruction can only swizzle
62  * within the first N channels of the values it consumes, irrespective
63  * of the capabilities of the instruction which produced those values.
64  * If we assume values are packed consistently (i.e., they always start
65  * at the beginning of a hardware register), we can actually access any
66  * aligned group of N channels so long as we stay within the group.
67  * This means for a vectorization width of 4 that only swizzles from
68  * either [xyzw] or [abcd] etc are allowed.  For a width of 2 these are
69  * swizzles from either [xy] or [zw] etc.
70  */
71 static bool
alu_is_swizzled_in_bounds(const nir_alu_instr * alu,unsigned width)72 alu_is_swizzled_in_bounds(const nir_alu_instr *alu, unsigned width)
73 {
74    for (unsigned i = 0; i < nir_op_infos[alu->op].num_inputs; i++) {
75       if (nir_op_infos[alu->op].input_sizes[i] == 1)
76          continue;
77 
78       unsigned mask = ~(width - 1);
79       for (unsigned j = 1; j < alu->def.num_components; j++) {
80          if ((alu->src[i].swizzle[0] & mask) != (alu->src[i].swizzle[j] & mask))
81             return false;
82       }
83    }
84 
85    return true;
86 }
87 
88 static void
nir_alu_ssa_dest_init(nir_alu_instr * alu,unsigned num_components,unsigned bit_size)89 nir_alu_ssa_dest_init(nir_alu_instr *alu, unsigned num_components,
90                       unsigned bit_size)
91 {
92    nir_def_init(&alu->instr, &alu->def, num_components, bit_size);
93 }
94 
95 static nir_def *
lower_reduction(nir_alu_instr * alu,nir_op chan_op,nir_op merge_op,nir_builder * builder,bool reverse_order)96 lower_reduction(nir_alu_instr *alu, nir_op chan_op, nir_op merge_op,
97                 nir_builder *builder, bool reverse_order)
98 {
99    unsigned num_components = nir_op_infos[alu->op].input_sizes[0];
100 
101    nir_def *last = NULL;
102    for (int i = 0; i < num_components; i++) {
103       int channel = reverse_order ? num_components - 1 - i : i;
104       nir_alu_instr *chan = nir_alu_instr_create(builder->shader, chan_op);
105       nir_alu_ssa_dest_init(chan, 1, alu->def.bit_size);
106       nir_alu_src_copy(&chan->src[0], &alu->src[0]);
107       chan->src[0].swizzle[0] = chan->src[0].swizzle[channel];
108       if (nir_op_infos[chan_op].num_inputs > 1) {
109          assert(nir_op_infos[chan_op].num_inputs == 2);
110          nir_alu_src_copy(&chan->src[1], &alu->src[1]);
111          chan->src[1].swizzle[0] = chan->src[1].swizzle[channel];
112       }
113       chan->exact = alu->exact;
114 
115       nir_builder_instr_insert(builder, &chan->instr);
116 
117       if (i == 0) {
118          last = &chan->def;
119       } else {
120          last = nir_build_alu(builder, merge_op,
121                               last, &chan->def, NULL, NULL);
122       }
123    }
124 
125    return last;
126 }
127 
128 static inline bool
will_lower_ffma(nir_shader * shader,unsigned bit_size)129 will_lower_ffma(nir_shader *shader, unsigned bit_size)
130 {
131    switch (bit_size) {
132    case 16:
133       return shader->options->lower_ffma16;
134    case 32:
135       return shader->options->lower_ffma32;
136    case 64:
137       return shader->options->lower_ffma64;
138    }
139    unreachable("bad bit size");
140 }
141 
142 static nir_def *
lower_fdot(nir_alu_instr * alu,nir_builder * builder)143 lower_fdot(nir_alu_instr *alu, nir_builder *builder)
144 {
145    /* Reversed order can result in lower instruction count because it
146     * creates more MAD/FMA in the case of fdot(a, vec4(b, 1.0)).
147     * Some games expect xyzw order, so only reverse the order for imprecise fdot.
148     */
149    bool reverse_order = !builder->exact;
150 
151    /* If we don't want to lower ffma, create several ffma instead of fmul+fadd
152     * and fusing later because fusing is not possible for exact fdot instructions.
153     */
154    if (will_lower_ffma(builder->shader, alu->def.bit_size))
155       return lower_reduction(alu, nir_op_fmul, nir_op_fadd, builder, reverse_order);
156 
157    unsigned num_components = nir_op_infos[alu->op].input_sizes[0];
158 
159    nir_def *prev = NULL;
160    for (int i = 0; i < num_components; i++) {
161       int channel = reverse_order ? num_components - 1 - i : i;
162       nir_alu_instr *instr = nir_alu_instr_create(
163          builder->shader, prev ? nir_op_ffma : nir_op_fmul);
164       nir_alu_ssa_dest_init(instr, 1, alu->def.bit_size);
165       for (unsigned j = 0; j < 2; j++) {
166          nir_alu_src_copy(&instr->src[j], &alu->src[j]);
167          instr->src[j].swizzle[0] = alu->src[j].swizzle[channel];
168       }
169       if (i != 0)
170          instr->src[2].src = nir_src_for_ssa(prev);
171       instr->exact = builder->exact;
172 
173       nir_builder_instr_insert(builder, &instr->instr);
174 
175       prev = &instr->def;
176    }
177 
178    return prev;
179 }
180 
181 static nir_def *
lower_alu_instr_width(nir_builder * b,nir_instr * instr,void * _data)182 lower_alu_instr_width(nir_builder *b, nir_instr *instr, void *_data)
183 {
184    struct alu_width_data *data = _data;
185    nir_alu_instr *alu = nir_instr_as_alu(instr);
186    unsigned num_src = nir_op_infos[alu->op].num_inputs;
187    unsigned i, chan;
188 
189    b->exact = alu->exact;
190 
191    unsigned num_components = alu->def.num_components;
192    unsigned target_width = 1;
193 
194    if (data->cb) {
195       target_width = data->cb(instr, data->data);
196       assert(util_is_power_of_two_or_zero(target_width));
197       if (target_width == 0)
198          return NULL;
199    }
200 
201 #define LOWER_REDUCTION(name, chan, merge) \
202    case name##2:                           \
203    case name##3:                           \
204    case name##4:                           \
205    case name##8:                           \
206    case name##16:                          \
207       return lower_reduction(alu, chan, merge, b, true);
208 
209    switch (alu->op) {
210    case nir_op_vec16:
211    case nir_op_vec8:
212    case nir_op_vec5:
213    case nir_op_vec4:
214    case nir_op_vec3:
215    case nir_op_vec2:
216    case nir_op_cube_amd:
217       /* We don't need to scalarize these ops, they're the ones generated to
218        * group up outputs into a value that can be SSAed.
219        */
220       return NULL;
221 
222    case nir_op_pack_half_2x16: {
223       if (!b->shader->options->lower_pack_half_2x16)
224          return NULL;
225 
226       nir_def *src_vec2 = nir_ssa_for_alu_src(b, alu, 0);
227       return nir_pack_half_2x16_split(b, nir_channel(b, src_vec2, 0),
228                                       nir_channel(b, src_vec2, 1));
229    }
230 
231    case nir_op_unpack_unorm_4x8:
232    case nir_op_unpack_snorm_4x8:
233    case nir_op_unpack_unorm_2x16:
234    case nir_op_unpack_snorm_2x16:
235       /* There is no scalar version of these ops, unless we were to break it
236        * down to bitshifts and math (which is definitely not intended).
237        */
238       return NULL;
239 
240    case nir_op_unpack_half_2x16_flush_to_zero:
241    case nir_op_unpack_half_2x16: {
242       if (!b->shader->options->lower_unpack_half_2x16)
243          return NULL;
244 
245       nir_def *packed = nir_ssa_for_alu_src(b, alu, 0);
246       if (alu->op == nir_op_unpack_half_2x16_flush_to_zero) {
247          return nir_vec2(b,
248                          nir_unpack_half_2x16_split_x_flush_to_zero(b,
249                                                                     packed),
250                          nir_unpack_half_2x16_split_y_flush_to_zero(b,
251                                                                     packed));
252       } else {
253          return nir_vec2(b,
254                          nir_unpack_half_2x16_split_x(b, packed),
255                          nir_unpack_half_2x16_split_y(b, packed));
256       }
257    }
258 
259    case nir_op_pack_uvec2_to_uint: {
260       assert(b->shader->options->lower_pack_snorm_2x16 ||
261              b->shader->options->lower_pack_unorm_2x16);
262 
263       nir_def *word = nir_extract_u16(b, nir_ssa_for_alu_src(b, alu, 0),
264                                       nir_imm_int(b, 0));
265       return nir_ior(b, nir_ishl(b, nir_channel(b, word, 1), nir_imm_int(b, 16)),
266                      nir_channel(b, word, 0));
267    }
268 
269    case nir_op_pack_uvec4_to_uint: {
270       assert(b->shader->options->lower_pack_snorm_4x8 ||
271              b->shader->options->lower_pack_unorm_4x8);
272 
273       nir_def *byte = nir_extract_u8(b, nir_ssa_for_alu_src(b, alu, 0),
274                                      nir_imm_int(b, 0));
275       return nir_ior(b, nir_ior(b, nir_ishl(b, nir_channel(b, byte, 3), nir_imm_int(b, 24)), nir_ishl(b, nir_channel(b, byte, 2), nir_imm_int(b, 16))),
276                      nir_ior(b, nir_ishl(b, nir_channel(b, byte, 1), nir_imm_int(b, 8)),
277                              nir_channel(b, byte, 0)));
278    }
279 
280    case nir_op_fdph: {
281       nir_def *src0_vec = nir_ssa_for_alu_src(b, alu, 0);
282       nir_def *src1_vec = nir_ssa_for_alu_src(b, alu, 1);
283 
284       /* Only use reverse order for imprecise fdph, see explanation in lower_fdot. */
285       bool reverse_order = !b->exact;
286       if (will_lower_ffma(b->shader, alu->def.bit_size)) {
287          nir_def *sum[4];
288          for (unsigned i = 0; i < 3; i++) {
289             int dest = reverse_order ? 3 - i : i;
290             sum[dest] = nir_fmul(b, nir_channel(b, src0_vec, i),
291                                  nir_channel(b, src1_vec, i));
292          }
293          sum[reverse_order ? 0 : 3] = nir_channel(b, src1_vec, 3);
294 
295          return nir_fadd(b, nir_fadd(b, nir_fadd(b, sum[0], sum[1]), sum[2]), sum[3]);
296       } else if (reverse_order) {
297          nir_def *sum = nir_channel(b, src1_vec, 3);
298          for (int i = 2; i >= 0; i--)
299             sum = nir_ffma(b, nir_channel(b, src0_vec, i), nir_channel(b, src1_vec, i), sum);
300          return sum;
301       } else {
302          nir_def *sum = nir_fmul(b, nir_channel(b, src0_vec, 0), nir_channel(b, src1_vec, 0));
303          sum = nir_ffma(b, nir_channel(b, src0_vec, 1), nir_channel(b, src1_vec, 1), sum);
304          sum = nir_ffma(b, nir_channel(b, src0_vec, 2), nir_channel(b, src1_vec, 2), sum);
305          return nir_fadd(b, sum, nir_channel(b, src1_vec, 3));
306       }
307    }
308 
309    case nir_op_pack_64_2x32: {
310       if (!b->shader->options->lower_pack_64_2x32)
311          return NULL;
312 
313       nir_def *src_vec2 = nir_ssa_for_alu_src(b, alu, 0);
314       return nir_pack_64_2x32_split(b, nir_channel(b, src_vec2, 0),
315                                     nir_channel(b, src_vec2, 1));
316    }
317    case nir_op_pack_64_4x16: {
318       if (!b->shader->options->lower_pack_64_4x16)
319          return NULL;
320 
321       nir_def *src_vec4 = nir_ssa_for_alu_src(b, alu, 0);
322       nir_def *xy = nir_pack_32_2x16_split(b, nir_channel(b, src_vec4, 0),
323                                            nir_channel(b, src_vec4, 1));
324       nir_def *zw = nir_pack_32_2x16_split(b, nir_channel(b, src_vec4, 2),
325                                            nir_channel(b, src_vec4, 3));
326 
327       return nir_pack_64_2x32_split(b, xy, zw);
328    }
329    case nir_op_pack_32_2x16: {
330       if (!b->shader->options->lower_pack_32_2x16)
331          return NULL;
332 
333       nir_def *src_vec2 = nir_ssa_for_alu_src(b, alu, 0);
334       return nir_pack_32_2x16_split(b, nir_channel(b, src_vec2, 0),
335                                     nir_channel(b, src_vec2, 1));
336    }
337    case nir_op_unpack_64_2x32:
338    case nir_op_unpack_64_4x16:
339    case nir_op_unpack_32_2x16:
340    case nir_op_unpack_32_4x8:
341    case nir_op_unpack_double_2x32_dxil:
342       return NULL;
343 
344    case nir_op_fdot2:
345    case nir_op_fdot3:
346    case nir_op_fdot4:
347    case nir_op_fdot8:
348    case nir_op_fdot16:
349       return lower_fdot(alu, b);
350 
351       LOWER_REDUCTION(nir_op_ball_fequal, nir_op_feq, nir_op_iand);
352       LOWER_REDUCTION(nir_op_ball_iequal, nir_op_ieq, nir_op_iand);
353       LOWER_REDUCTION(nir_op_bany_fnequal, nir_op_fneu, nir_op_ior);
354       LOWER_REDUCTION(nir_op_bany_inequal, nir_op_ine, nir_op_ior);
355       LOWER_REDUCTION(nir_op_b8all_fequal, nir_op_feq8, nir_op_iand);
356       LOWER_REDUCTION(nir_op_b8all_iequal, nir_op_ieq8, nir_op_iand);
357       LOWER_REDUCTION(nir_op_b8any_fnequal, nir_op_fneu8, nir_op_ior);
358       LOWER_REDUCTION(nir_op_b8any_inequal, nir_op_ine8, nir_op_ior);
359       LOWER_REDUCTION(nir_op_b16all_fequal, nir_op_feq16, nir_op_iand);
360       LOWER_REDUCTION(nir_op_b16all_iequal, nir_op_ieq16, nir_op_iand);
361       LOWER_REDUCTION(nir_op_b16any_fnequal, nir_op_fneu16, nir_op_ior);
362       LOWER_REDUCTION(nir_op_b16any_inequal, nir_op_ine16, nir_op_ior);
363       LOWER_REDUCTION(nir_op_b32all_fequal, nir_op_feq32, nir_op_iand);
364       LOWER_REDUCTION(nir_op_b32all_iequal, nir_op_ieq32, nir_op_iand);
365       LOWER_REDUCTION(nir_op_b32any_fnequal, nir_op_fneu32, nir_op_ior);
366       LOWER_REDUCTION(nir_op_b32any_inequal, nir_op_ine32, nir_op_ior);
367       LOWER_REDUCTION(nir_op_fall_equal, nir_op_seq, nir_op_fmin);
368       LOWER_REDUCTION(nir_op_fany_nequal, nir_op_sne, nir_op_fmax);
369 
370    default:
371       break;
372    }
373 
374    if (num_components == 1)
375       return NULL;
376 
377    if (num_components <= target_width) {
378       /* If the ALU instr is swizzled outside the target width,
379        * reduce the target width.
380        */
381       if (alu_is_swizzled_in_bounds(alu, target_width))
382          return NULL;
383       else
384          target_width = DIV_ROUND_UP(num_components, 2);
385    }
386 
387    nir_alu_instr *vec = nir_alu_instr_create(b->shader, nir_op_vec(num_components));
388 
389    for (chan = 0; chan < num_components; chan += target_width) {
390       unsigned components = MIN2(target_width, num_components - chan);
391       nir_alu_instr *lower = nir_alu_instr_create(b->shader, alu->op);
392 
393       for (i = 0; i < num_src; i++) {
394          nir_alu_src_copy(&lower->src[i], &alu->src[i]);
395 
396          /* We only handle same-size-as-dest (input_sizes[] == 0) or scalar
397           * args (input_sizes[] == 1).
398           */
399          assert(nir_op_infos[alu->op].input_sizes[i] < 2);
400          for (int j = 0; j < components; j++) {
401             unsigned src_chan = nir_op_infos[alu->op].input_sizes[i] == 1 ? 0 : chan + j;
402             lower->src[i].swizzle[j] = alu->src[i].swizzle[src_chan];
403          }
404       }
405 
406       nir_alu_ssa_dest_init(lower, components, alu->def.bit_size);
407       lower->exact = alu->exact;
408 
409       for (i = 0; i < components; i++) {
410          vec->src[chan + i].src = nir_src_for_ssa(&lower->def);
411          vec->src[chan + i].swizzle[0] = i;
412       }
413 
414       nir_builder_instr_insert(b, &lower->instr);
415    }
416 
417    return nir_builder_alu_instr_finish_and_insert(b, vec);
418 }
419 
420 bool
nir_lower_alu_width(nir_shader * shader,nir_vectorize_cb cb,const void * _data)421 nir_lower_alu_width(nir_shader *shader, nir_vectorize_cb cb, const void *_data)
422 {
423    struct alu_width_data data = {
424       .cb = cb,
425       .data = _data,
426    };
427 
428    return nir_shader_lower_instructions(shader,
429                                         inst_is_vector_alu,
430                                         lower_alu_instr_width,
431                                         &data);
432 }
433 
434 struct alu_to_scalar_data {
435    nir_instr_filter_cb cb;
436    const void *data;
437 };
438 
439 static uint8_t
scalar_cb(const nir_instr * instr,const void * data)440 scalar_cb(const nir_instr *instr, const void *data)
441 {
442    /* return vectorization-width = 1 for filtered instructions */
443    const struct alu_to_scalar_data *filter = data;
444    return filter->cb(instr, filter->data) ? 1 : 0;
445 }
446 
447 bool
nir_lower_alu_to_scalar(nir_shader * shader,nir_instr_filter_cb cb,const void * _data)448 nir_lower_alu_to_scalar(nir_shader *shader, nir_instr_filter_cb cb, const void *_data)
449 {
450    struct alu_to_scalar_data data = {
451       .cb = cb,
452       .data = _data,
453    };
454 
455    return nir_lower_alu_width(shader, cb ? scalar_cb : NULL, &data);
456 }
457 
458 static bool
lower_alu_vec8_16_src(nir_builder * b,nir_instr * instr,void * _data)459 lower_alu_vec8_16_src(nir_builder *b, nir_instr *instr, void *_data)
460 {
461    if (instr->type != nir_instr_type_alu)
462       return false;
463 
464    nir_alu_instr *alu = nir_instr_as_alu(instr);
465    const nir_op_info *info = &nir_op_infos[alu->op];
466 
467    bool changed = false;
468    b->cursor = nir_before_instr(instr);
469    for (int i = 0; i < info->num_inputs; i++) {
470       if (alu->src[i].src.ssa->num_components < 8 || info->input_sizes[i])
471          continue;
472 
473       changed = true;
474       nir_def *comps[4];
475       for (int c = 0; c < alu->def.num_components; c++) {
476          unsigned swizzle = alu->src[i].swizzle[c];
477          alu->src[i].swizzle[c] = c;
478 
479          nir_const_value *const_val = nir_src_as_const_value(alu->src[i].src);
480          if (const_val) {
481             comps[c] = nir_build_imm(b, 1, alu->src[i].src.ssa->bit_size, &const_val[swizzle]);
482          } else {
483             comps[c] = nir_swizzle(b, alu->src[i].src.ssa, &swizzle, 1);
484          }
485       }
486       nir_def *src = nir_vec(b, comps, alu->def.num_components);
487       nir_src_rewrite(&alu->src[i].src, src);
488    }
489 
490    return changed;
491 }
492 
493 bool
nir_lower_alu_vec8_16_srcs(nir_shader * shader)494 nir_lower_alu_vec8_16_srcs(nir_shader *shader)
495 {
496    return nir_shader_instructions_pass(shader, lower_alu_vec8_16_src,
497       nir_metadata_block_index | nir_metadata_dominance,
498       NULL);
499 }
500