2 * Copyright © 2014-2015 Broadcom
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:
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
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
25 #include "nir_builder.h"
27 /** @file nir_lower_alu_to_scalar.c
29 * Replaces nir_alu_instr operations with more than one channel used in the
30 * arguments with individual per-channel operations.
34 nir_alu_ssa_dest_init(nir_alu_instr
*instr
, unsigned num_components
,
37 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
.dest
, num_components
,
39 instr
->dest
.write_mask
= (1 << num_components
) - 1;
43 lower_reduction(nir_alu_instr
*instr
, nir_op chan_op
, nir_op merge_op
,
46 unsigned num_components
= nir_op_infos
[instr
->op
].input_sizes
[0];
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
];
59 chan
->exact
= instr
->exact
;
61 nir_builder_instr_insert(builder
, &chan
->instr
);
64 last
= &chan
->dest
.dest
.ssa
;
66 last
= nir_build_alu(builder
, merge_op
,
67 last
, &chan
->dest
.dest
.ssa
, NULL
, NULL
);
71 assert(instr
->dest
.write_mask
== 1);
72 nir_ssa_def_rewrite_uses(&instr
->dest
.dest
.ssa
, nir_src_for_ssa(last
));
73 nir_instr_remove(&instr
->instr
);
77 lower_alu_instr_scalar(nir_alu_instr
*instr
, nir_builder
*b
)
79 unsigned num_src
= nir_op_infos
[instr
->op
].num_inputs
;
82 assert(instr
->dest
.dest
.is_ssa
);
83 assert(instr
->dest
.write_mask
!= 0);
85 b
->cursor
= nir_before_instr(&instr
->instr
);
86 b
->exact
= instr
->exact
;
88 #define LOWER_REDUCTION(name, chan, merge) \
92 lower_reduction(instr, chan, merge, b); \
99 case nir_op_cube_face_coord
:
100 case nir_op_cube_face_index
:
101 /* We don't need to scalarize these ops, they're the ones generated to
102 * group up outputs into a value that can be SSAed.
106 case nir_op_pack_half_2x16
:
107 if (!b
->shader
->options
->lower_pack_half_2x16
)
110 nir_ssa_def
*src_vec2
= nir_ssa_for_alu_src(b
, instr
, 0);
113 nir_pack_half_2x16_split(b
, nir_channel(b
, src_vec2
, 0),
114 nir_channel(b
, src_vec2
, 1));
116 nir_ssa_def_rewrite_uses(&instr
->dest
.dest
.ssa
, nir_src_for_ssa(val
));
117 nir_instr_remove(&instr
->instr
);
120 case nir_op_unpack_unorm_4x8
:
121 case nir_op_unpack_snorm_4x8
:
122 case nir_op_unpack_unorm_2x16
:
123 case nir_op_unpack_snorm_2x16
:
124 /* There is no scalar version of these ops, unless we were to break it
125 * down to bitshifts and math (which is definitely not intended).
129 case nir_op_unpack_half_2x16
: {
130 if (!b
->shader
->options
->lower_unpack_half_2x16
)
133 nir_ssa_def
*packed
= nir_ssa_for_alu_src(b
, instr
, 0);
135 nir_ssa_def
*comps
[2];
136 comps
[0] = nir_unpack_half_2x16_split_x(b
, packed
);
137 comps
[1] = nir_unpack_half_2x16_split_y(b
, packed
);
138 nir_ssa_def
*vec
= nir_vec(b
, comps
, 2);
140 nir_ssa_def_rewrite_uses(&instr
->dest
.dest
.ssa
, nir_src_for_ssa(vec
));
141 nir_instr_remove(&instr
->instr
);
145 case nir_op_pack_uvec2_to_uint
: {
146 assert(b
->shader
->options
->lower_pack_snorm_2x16
||
147 b
->shader
->options
->lower_pack_unorm_2x16
);
149 nir_ssa_def
*word
= nir_extract_u16(b
, nir_ssa_for_alu_src(b
, instr
, 0),
152 nir_ior(b
, nir_ishl(b
, nir_channel(b
, word
, 1), nir_imm_int(b
, 16)),
153 nir_channel(b
, word
, 0));
155 nir_ssa_def_rewrite_uses(&instr
->dest
.dest
.ssa
, nir_src_for_ssa(val
));
156 nir_instr_remove(&instr
->instr
);
160 case nir_op_pack_uvec4_to_uint
: {
161 assert(b
->shader
->options
->lower_pack_snorm_4x8
||
162 b
->shader
->options
->lower_pack_unorm_4x8
);
164 nir_ssa_def
*byte
= nir_extract_u8(b
, nir_ssa_for_alu_src(b
, instr
, 0),
167 nir_ior(b
, nir_ior(b
, nir_ishl(b
, nir_channel(b
, byte
, 3), nir_imm_int(b
, 24)),
168 nir_ishl(b
, nir_channel(b
, byte
, 2), nir_imm_int(b
, 16))),
169 nir_ior(b
, nir_ishl(b
, nir_channel(b
, byte
, 1), nir_imm_int(b
, 8)),
170 nir_channel(b
, byte
, 0)));
172 nir_ssa_def_rewrite_uses(&instr
->dest
.dest
.ssa
, nir_src_for_ssa(val
));
173 nir_instr_remove(&instr
->instr
);
178 nir_ssa_def
*src0_vec
= nir_ssa_for_alu_src(b
, instr
, 0);
179 nir_ssa_def
*src1_vec
= nir_ssa_for_alu_src(b
, instr
, 1);
182 for (unsigned i
= 0; i
< 3; i
++) {
183 sum
[i
] = nir_fmul(b
, nir_channel(b
, src0_vec
, i
),
184 nir_channel(b
, src1_vec
, i
));
186 sum
[3] = nir_channel(b
, src1_vec
, 3);
188 nir_ssa_def
*val
= nir_fadd(b
, nir_fadd(b
, sum
[0], sum
[1]),
189 nir_fadd(b
, sum
[2], sum
[3]));
191 nir_ssa_def_rewrite_uses(&instr
->dest
.dest
.ssa
, nir_src_for_ssa(val
));
192 nir_instr_remove(&instr
->instr
);
196 case nir_op_unpack_64_2x32
:
197 case nir_op_unpack_32_2x16
:
200 LOWER_REDUCTION(nir_op_fdot
, nir_op_fmul
, nir_op_fadd
);
201 LOWER_REDUCTION(nir_op_b32all_fequal
, nir_op_feq32
, nir_op_iand
);
202 LOWER_REDUCTION(nir_op_b32all_iequal
, nir_op_ieq32
, nir_op_iand
);
203 LOWER_REDUCTION(nir_op_b32any_fnequal
, nir_op_fne32
, nir_op_ior
);
204 LOWER_REDUCTION(nir_op_b32any_inequal
, nir_op_ine32
, nir_op_ior
);
205 LOWER_REDUCTION(nir_op_fall_equal
, nir_op_seq
, nir_op_fand
);
206 LOWER_REDUCTION(nir_op_fany_nequal
, nir_op_sne
, nir_op_for
);
212 if (instr
->dest
.dest
.ssa
.num_components
== 1)
215 unsigned num_components
= instr
->dest
.dest
.ssa
.num_components
;
216 nir_ssa_def
*comps
[NIR_MAX_VEC_COMPONENTS
] = { NULL
};
218 for (chan
= 0; chan
< NIR_MAX_VEC_COMPONENTS
; chan
++) {
219 if (!(instr
->dest
.write_mask
& (1 << chan
)))
222 nir_alu_instr
*lower
= nir_alu_instr_create(b
->shader
, instr
->op
);
223 for (i
= 0; i
< num_src
; i
++) {
224 /* We only handle same-size-as-dest (input_sizes[] == 0) or scalar
225 * args (input_sizes[] == 1).
227 assert(nir_op_infos
[instr
->op
].input_sizes
[i
] < 2);
228 unsigned src_chan
= (nir_op_infos
[instr
->op
].input_sizes
[i
] == 1 ?
231 nir_alu_src_copy(&lower
->src
[i
], &instr
->src
[i
], lower
);
232 for (int j
= 0; j
< NIR_MAX_VEC_COMPONENTS
; j
++)
233 lower
->src
[i
].swizzle
[j
] = instr
->src
[i
].swizzle
[src_chan
];
236 nir_alu_ssa_dest_init(lower
, 1, instr
->dest
.dest
.ssa
.bit_size
);
237 lower
->dest
.saturate
= instr
->dest
.saturate
;
238 comps
[chan
] = &lower
->dest
.dest
.ssa
;
239 lower
->exact
= instr
->exact
;
241 nir_builder_instr_insert(b
, &lower
->instr
);
244 nir_ssa_def
*vec
= nir_vec(b
, comps
, num_components
);
246 nir_ssa_def_rewrite_uses(&instr
->dest
.dest
.ssa
, nir_src_for_ssa(vec
));
248 nir_instr_remove(&instr
->instr
);
253 nir_lower_alu_to_scalar_impl(nir_function_impl
*impl
)
256 nir_builder_init(&builder
, impl
);
257 bool progress
= false;
259 nir_foreach_block(block
, impl
) {
260 nir_foreach_instr_safe(instr
, block
) {
261 if (instr
->type
== nir_instr_type_alu
) {
262 progress
= lower_alu_instr_scalar(nir_instr_as_alu(instr
),
263 &builder
) || progress
;
268 nir_metadata_preserve(impl
, nir_metadata_block_index
|
269 nir_metadata_dominance
);
275 nir_lower_alu_to_scalar(nir_shader
*shader
)
277 bool progress
= false;
279 nir_foreach_function(function
, shader
) {
281 progress
= nir_lower_alu_to_scalar_impl(function
->impl
) || progress
;