Merge commit mesa-public/master into vulkan
[mesa.git] / src / compiler / nir / nir_lower_alu_to_scalar.c
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 /** @file nir_lower_alu_to_scalar.c
28 *
29 * Replaces nir_alu_instr operations with more than one channel used in the
30 * arguments with individual per-channel operations.
31 */
32
33 static void
34 nir_alu_ssa_dest_init(nir_alu_instr *instr, unsigned num_components)
35 {
36 nir_ssa_dest_init(&instr->instr, &instr->dest.dest, num_components, NULL);
37 instr->dest.write_mask = (1 << num_components) - 1;
38 }
39
40 static void
41 lower_reduction(nir_alu_instr *instr, nir_op chan_op, nir_op merge_op,
42 nir_builder *builder)
43 {
44 unsigned num_components = nir_op_infos[instr->op].input_sizes[0];
45
46 nir_ssa_def *last = NULL;
47 for (unsigned i = 0; i < num_components; i++) {
48 nir_alu_instr *chan = nir_alu_instr_create(builder->shader, chan_op);
49 nir_alu_ssa_dest_init(chan, 1);
50 nir_alu_src_copy(&chan->src[0], &instr->src[0], chan);
51 chan->src[0].swizzle[0] = chan->src[0].swizzle[i];
52 if (nir_op_infos[chan_op].num_inputs > 1) {
53 assert(nir_op_infos[chan_op].num_inputs == 2);
54 nir_alu_src_copy(&chan->src[1], &instr->src[1], chan);
55 chan->src[1].swizzle[0] = chan->src[1].swizzle[i];
56 }
57
58 nir_builder_instr_insert(builder, &chan->instr);
59
60 if (i == 0) {
61 last = &chan->dest.dest.ssa;
62 } else {
63 last = nir_build_alu(builder, merge_op,
64 last, &chan->dest.dest.ssa, NULL, NULL);
65 }
66 }
67
68 assert(instr->dest.write_mask == 1);
69 nir_ssa_def_rewrite_uses(&instr->dest.dest.ssa, nir_src_for_ssa(last));
70 nir_instr_remove(&instr->instr);
71 }
72
73 static void
74 lower_alu_instr_scalar(nir_alu_instr *instr, nir_builder *b)
75 {
76 unsigned num_src = nir_op_infos[instr->op].num_inputs;
77 unsigned i, chan;
78
79 assert(instr->dest.dest.is_ssa);
80 assert(instr->dest.write_mask != 0);
81
82 b->cursor = nir_before_instr(&instr->instr);
83
84 #define LOWER_REDUCTION(name, chan, merge) \
85 case name##2: \
86 case name##3: \
87 case name##4: \
88 lower_reduction(instr, chan, merge, b); \
89 return;
90
91 switch (instr->op) {
92 case nir_op_vec4:
93 case nir_op_vec3:
94 case nir_op_vec2:
95 /* We don't need to scalarize these ops, they're the ones generated to
96 * group up outputs into a value that can be SSAed.
97 */
98 return;
99
100 case nir_op_pack_half_2x16:
101 if (!b->shader->options->lower_pack_half_2x16)
102 return;
103
104 nir_ssa_def *val =
105 nir_pack_half_2x16_split(b, nir_channel(b, instr->src[0].src.ssa,
106 instr->src[0].swizzle[0]),
107 nir_channel(b, instr->src[0].src.ssa,
108 instr->src[0].swizzle[1]));
109
110 nir_ssa_def_rewrite_uses(&instr->dest.dest.ssa, nir_src_for_ssa(val));
111 nir_instr_remove(&instr->instr);
112 return;
113
114 case nir_op_unpack_unorm_4x8:
115 case nir_op_unpack_snorm_4x8:
116 case nir_op_unpack_unorm_2x16:
117 case nir_op_unpack_snorm_2x16:
118 /* There is no scalar version of these ops, unless we were to break it
119 * down to bitshifts and math (which is definitely not intended).
120 */
121 return;
122
123 case nir_op_unpack_half_2x16: {
124 if (!b->shader->options->lower_unpack_half_2x16)
125 return;
126
127 nir_ssa_def *comps[2];
128 comps[0] = nir_unpack_half_2x16_split_x(b, instr->src[0].src.ssa);
129 comps[1] = nir_unpack_half_2x16_split_y(b, instr->src[0].src.ssa);
130 nir_ssa_def *vec = nir_vec(b, comps, 2);
131
132 nir_ssa_def_rewrite_uses(&instr->dest.dest.ssa, nir_src_for_ssa(vec));
133 nir_instr_remove(&instr->instr);
134 return;
135 }
136
137 case nir_op_pack_uvec2_to_uint: {
138 assert(b->shader->options->lower_pack_snorm_2x16 ||
139 b->shader->options->lower_pack_unorm_2x16);
140
141 nir_ssa_def *word =
142 nir_extract_uword(b, instr->src[0].src.ssa, nir_imm_int(b, 0));
143 nir_ssa_def *val =
144 nir_ior(b, nir_ishl(b, nir_channel(b, word, 1), nir_imm_int(b, 16)),
145 nir_channel(b, word, 0));
146
147 nir_ssa_def_rewrite_uses(&instr->dest.dest.ssa, nir_src_for_ssa(val));
148 nir_instr_remove(&instr->instr);
149 break;
150 }
151
152 case nir_op_pack_uvec4_to_uint: {
153 assert(b->shader->options->lower_pack_snorm_4x8 ||
154 b->shader->options->lower_pack_unorm_4x8);
155
156 nir_ssa_def *byte =
157 nir_extract_ubyte(b, instr->src[0].src.ssa, nir_imm_int(b, 0));
158 nir_ssa_def *val =
159 nir_ior(b, nir_ior(b, nir_ishl(b, nir_channel(b, byte, 3), nir_imm_int(b, 24)),
160 nir_ishl(b, nir_channel(b, byte, 2), nir_imm_int(b, 16))),
161 nir_ior(b, nir_ishl(b, nir_channel(b, byte, 1), nir_imm_int(b, 8)),
162 nir_channel(b, byte, 0)));
163
164 nir_ssa_def_rewrite_uses(&instr->dest.dest.ssa, nir_src_for_ssa(val));
165 nir_instr_remove(&instr->instr);
166 break;
167 }
168
169 case nir_op_fdph: {
170 nir_ssa_def *sum[4];
171 for (unsigned i = 0; i < 3; i++) {
172 sum[i] = nir_fmul(b, nir_channel(b, instr->src[0].src.ssa,
173 instr->src[0].swizzle[i]),
174 nir_channel(b, instr->src[1].src.ssa,
175 instr->src[1].swizzle[i]));
176 }
177 sum[3] = nir_channel(b, instr->src[1].src.ssa, instr->src[1].swizzle[3]);
178
179 nir_ssa_def *val = nir_fadd(b, nir_fadd(b, sum[0], sum[1]),
180 nir_fadd(b, sum[2], sum[3]));
181
182 nir_ssa_def_rewrite_uses(&instr->dest.dest.ssa, nir_src_for_ssa(val));
183 nir_instr_remove(&instr->instr);
184 return;
185 }
186
187 LOWER_REDUCTION(nir_op_fdot, nir_op_fmul, nir_op_fadd);
188 LOWER_REDUCTION(nir_op_ball_fequal, nir_op_feq, nir_op_iand);
189 LOWER_REDUCTION(nir_op_ball_iequal, nir_op_ieq, nir_op_iand);
190 LOWER_REDUCTION(nir_op_bany_fnequal, nir_op_fne, nir_op_ior);
191 LOWER_REDUCTION(nir_op_bany_inequal, nir_op_ine, nir_op_ior);
192 LOWER_REDUCTION(nir_op_fall_equal, nir_op_seq, nir_op_fand);
193 LOWER_REDUCTION(nir_op_fany_nequal, nir_op_sne, nir_op_for);
194
195 default:
196 break;
197 }
198
199 if (instr->dest.dest.ssa.num_components == 1)
200 return;
201
202 unsigned num_components = instr->dest.dest.ssa.num_components;
203 nir_ssa_def *comps[] = { NULL, NULL, NULL, NULL };
204
205 for (chan = 0; chan < 4; chan++) {
206 if (!(instr->dest.write_mask & (1 << chan)))
207 continue;
208
209 nir_alu_instr *lower = nir_alu_instr_create(b->shader, instr->op);
210 for (i = 0; i < num_src; i++) {
211 /* We only handle same-size-as-dest (input_sizes[] == 0) or scalar
212 * args (input_sizes[] == 1).
213 */
214 assert(nir_op_infos[instr->op].input_sizes[i] < 2);
215 unsigned src_chan = (nir_op_infos[instr->op].input_sizes[i] == 1 ?
216 0 : chan);
217
218 nir_alu_src_copy(&lower->src[i], &instr->src[i], lower);
219 for (int j = 0; j < 4; j++)
220 lower->src[i].swizzle[j] = instr->src[i].swizzle[src_chan];
221 }
222
223 nir_alu_ssa_dest_init(lower, 1);
224 lower->dest.saturate = instr->dest.saturate;
225 comps[chan] = &lower->dest.dest.ssa;
226
227 nir_builder_instr_insert(b, &lower->instr);
228 }
229
230 nir_ssa_def *vec = nir_vec(b, comps, num_components);
231
232 nir_ssa_def_rewrite_uses(&instr->dest.dest.ssa, nir_src_for_ssa(vec));
233
234 nir_instr_remove(&instr->instr);
235 }
236
237 static bool
238 lower_alu_to_scalar_block(nir_block *block, void *builder)
239 {
240 nir_foreach_instr_safe(block, instr) {
241 if (instr->type == nir_instr_type_alu)
242 lower_alu_instr_scalar(nir_instr_as_alu(instr), builder);
243 }
244
245 return true;
246 }
247
248 static void
249 nir_lower_alu_to_scalar_impl(nir_function_impl *impl)
250 {
251 nir_builder builder;
252 nir_builder_init(&builder, impl);
253
254 nir_foreach_block(impl, lower_alu_to_scalar_block, &builder);
255 }
256
257 void
258 nir_lower_alu_to_scalar(nir_shader *shader)
259 {
260 nir_foreach_function(shader, function) {
261 if (function->impl)
262 nir_lower_alu_to_scalar_impl(function->impl);
263 }
264 }