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