2 * Copyright © 2018 Intel Corporation
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
24 #include "nir_builder.h"
27 * Some ALU operations may not be supported in hardware in specific bit-sizes.
28 * This pass allows implementations to selectively lower such operations to
29 * a bit-size that is supported natively and then converts the result back to
30 * the original bit-size.
33 static nir_ssa_def *convert_to_bit_size(nir_builder *bld, nir_ssa_def *src,
34 nir_alu_type type, unsigned bit_size)
36 assert(src->bit_size < bit_size);
38 /* create b2i32(a) instead of i2i32(b2i8(a))/i2i32(b2i16(a)) */
39 nir_alu_instr *alu = nir_src_as_alu_instr(nir_src_for_ssa(src));
40 if ((type & (nir_type_uint | nir_type_int)) && bit_size == 32 &&
41 alu && (alu->op == nir_op_b2i8 || alu->op == nir_op_b2i16)) {
42 nir_alu_instr *instr = nir_alu_instr_create(bld->shader, nir_op_b2i32);
43 nir_alu_src_copy(&instr->src[0], &alu->src[0], instr);
44 return nir_builder_alu_instr_finish_and_insert(bld, instr);
47 return nir_convert_to_bit_size(bld, src, type, bit_size);
51 lower_alu_instr(nir_builder *bld, nir_alu_instr *alu, unsigned bit_size)
53 const nir_op op = alu->op;
54 unsigned dst_bit_size = alu->dest.dest.ssa.bit_size;
56 bld->cursor = nir_before_instr(&alu->instr);
58 /* Convert each source to the requested bit-size */
59 nir_ssa_def *srcs[NIR_MAX_VEC_COMPONENTS] = { NULL };
60 for (unsigned i = 0; i < nir_op_infos[op].num_inputs; i++) {
61 nir_ssa_def *src = nir_ssa_for_alu_src(bld, alu, i);
63 nir_alu_type type = nir_op_infos[op].input_types[i];
64 if (nir_alu_type_get_type_size(type) == 0)
65 src = convert_to_bit_size(bld, src, type, bit_size);
67 if (i == 1 && (op == nir_op_ishl || op == nir_op_ishr || op == nir_op_ushr ||
68 op == nir_op_bitz || op == nir_op_bitz8 || op == nir_op_bitz16 ||
69 op == nir_op_bitz32 || op == nir_op_bitnz || op == nir_op_bitnz8 ||
70 op == nir_op_bitnz16 || op == nir_op_bitnz32)) {
71 assert(util_is_power_of_two_nonzero(dst_bit_size));
72 src = nir_iand(bld, src, nir_imm_int(bld, dst_bit_size - 1));
78 /* Emit the lowered ALU instruction */
79 nir_ssa_def *lowered_dst = NULL;
80 if (op == nir_op_imul_high || op == nir_op_umul_high) {
81 assert(dst_bit_size * 2 <= bit_size);
82 lowered_dst = nir_imul(bld, srcs[0], srcs[1]);
83 if (nir_op_infos[op].output_type & nir_type_uint)
84 lowered_dst = nir_ushr_imm(bld, lowered_dst, dst_bit_size);
86 lowered_dst = nir_ishr_imm(bld, lowered_dst, dst_bit_size);
87 } else if (op == nir_op_iadd_sat || op == nir_op_isub_sat || op == nir_op_uadd_sat ||
88 op == nir_op_uadd_carry) {
89 if (op == nir_op_isub_sat)
90 lowered_dst = nir_isub(bld, srcs[0], srcs[1]);
92 lowered_dst = nir_iadd(bld, srcs[0], srcs[1]);
94 /* The add_sat and sub_sat instructions need to clamp the result to the
95 * range of the original type.
97 if (op == nir_op_iadd_sat || op == nir_op_isub_sat) {
98 const int64_t int_max = u_intN_max(dst_bit_size);
99 const int64_t int_min = u_intN_min(dst_bit_size);
101 lowered_dst = nir_iclamp(bld, lowered_dst,
102 nir_imm_intN_t(bld, int_min, bit_size),
103 nir_imm_intN_t(bld, int_max, bit_size));
104 } else if (op == nir_op_uadd_sat) {
105 const uint64_t uint_max = u_uintN_max(dst_bit_size);
107 lowered_dst = nir_umin(bld, lowered_dst,
108 nir_imm_intN_t(bld, uint_max, bit_size));
110 assert(op == nir_op_uadd_carry);
111 lowered_dst = nir_ushr_imm(bld, lowered_dst, dst_bit_size);
114 lowered_dst = nir_build_alu_src_arr(bld, op, srcs);
118 /* Convert result back to the original bit-size */
119 if (nir_alu_type_get_type_size(nir_op_infos[op].output_type) == 0 &&
120 dst_bit_size != bit_size) {
121 nir_alu_type type = nir_op_infos[op].output_type;
122 nir_ssa_def *dst = nir_convert_to_bit_size(bld, lowered_dst, type, dst_bit_size);
123 nir_ssa_def_rewrite_uses(&alu->dest.dest.ssa, dst);
125 nir_ssa_def_rewrite_uses(&alu->dest.dest.ssa, lowered_dst);
130 lower_intrinsic_instr(nir_builder *b, nir_intrinsic_instr *intrin,
133 switch (intrin->intrinsic) {
134 case nir_intrinsic_read_invocation:
135 case nir_intrinsic_read_first_invocation:
136 case nir_intrinsic_vote_feq:
137 case nir_intrinsic_vote_ieq:
138 case nir_intrinsic_shuffle:
139 case nir_intrinsic_shuffle_xor:
140 case nir_intrinsic_shuffle_up:
141 case nir_intrinsic_shuffle_down:
142 case nir_intrinsic_quad_broadcast:
143 case nir_intrinsic_quad_swap_horizontal:
144 case nir_intrinsic_quad_swap_vertical:
145 case nir_intrinsic_quad_swap_diagonal:
146 case nir_intrinsic_reduce:
147 case nir_intrinsic_inclusive_scan:
148 case nir_intrinsic_exclusive_scan: {
149 assert(intrin->src[0].is_ssa && intrin->dest.is_ssa);
150 const unsigned old_bit_size = intrin->dest.ssa.bit_size;
151 assert(old_bit_size < bit_size);
153 nir_alu_type type = nir_type_uint;
154 if (nir_intrinsic_has_reduction_op(intrin))
155 type = nir_op_infos[nir_intrinsic_reduction_op(intrin)].input_types[0];
156 else if (intrin->intrinsic == nir_intrinsic_vote_feq)
157 type = nir_type_float;
159 b->cursor = nir_before_instr(&intrin->instr);
160 nir_intrinsic_instr *new_intrin =
161 nir_instr_as_intrinsic(nir_instr_clone(b->shader, &intrin->instr));
163 nir_ssa_def *new_src = nir_convert_to_bit_size(b, intrin->src[0].ssa,
165 new_intrin->src[0] = nir_src_for_ssa(new_src);
167 if (intrin->intrinsic == nir_intrinsic_vote_feq ||
168 intrin->intrinsic == nir_intrinsic_vote_ieq) {
169 /* These return a Boolean; it's always 1-bit */
170 assert(new_intrin->dest.ssa.bit_size == 1);
172 /* These return the same bit size as the source; we need to adjust
173 * the size and then we'll have to emit a down-cast.
175 assert(intrin->src[0].ssa->bit_size == intrin->dest.ssa.bit_size);
176 new_intrin->dest.ssa.bit_size = bit_size;
179 nir_builder_instr_insert(b, &new_intrin->instr);
181 nir_ssa_def *res = &new_intrin->dest.ssa;
182 if (intrin->intrinsic == nir_intrinsic_exclusive_scan) {
183 /* For exclusive scan, we have to be careful because the identity
184 * value for the higher bit size may get added into the mix by
185 * disabled channels. For some cases (imin/imax in particular),
186 * this value won't convert to the right identity value when we
187 * down-cast so we have to clamp it.
189 switch (nir_intrinsic_reduction_op(intrin)) {
191 int64_t int_max = (1ull << (old_bit_size - 1)) - 1;
192 res = nir_imin(b, res, nir_imm_intN_t(b, int_max, bit_size));
196 int64_t int_min = -(int64_t)(1ull << (old_bit_size - 1));
197 res = nir_imax(b, res, nir_imm_intN_t(b, int_min, bit_size));
205 if (intrin->intrinsic != nir_intrinsic_vote_feq &&
206 intrin->intrinsic != nir_intrinsic_vote_ieq)
207 res = nir_u2uN(b, res, old_bit_size);
209 nir_ssa_def_rewrite_uses(&intrin->dest.ssa, res);
214 unreachable("Unsupported instruction");
219 lower_phi_instr(nir_builder *b, nir_phi_instr *phi, unsigned bit_size,
220 nir_phi_instr *last_phi)
222 assert(phi->dest.is_ssa);
223 unsigned old_bit_size = phi->dest.ssa.bit_size;
224 assert(old_bit_size < bit_size);
226 nir_foreach_phi_src(src, phi) {
227 b->cursor = nir_after_block_before_jump(src->pred);
228 assert(src->src.is_ssa);
229 nir_ssa_def *new_src = nir_u2uN(b, src->src.ssa, bit_size);
231 nir_instr_rewrite_src(&phi->instr, &src->src, nir_src_for_ssa(new_src));
234 phi->dest.ssa.bit_size = bit_size;
236 b->cursor = nir_after_instr(&last_phi->instr);
238 nir_ssa_def *new_dest = nir_u2uN(b, &phi->dest.ssa, old_bit_size);
239 nir_ssa_def_rewrite_uses_after(&phi->dest.ssa, new_dest,
240 new_dest->parent_instr);
244 lower_impl(nir_function_impl *impl,
245 nir_lower_bit_size_callback callback,
248 nir_builder b = nir_builder_create(impl);
249 bool progress = false;
251 nir_foreach_block(block, impl) {
252 /* Stash this so we can rewrite phi destinations quickly. */
253 nir_phi_instr *last_phi = nir_block_last_phi_instr(block);
255 nir_foreach_instr_safe(instr, block) {
256 unsigned lower_bit_size = callback(instr, callback_data);
257 if (lower_bit_size == 0)
260 switch (instr->type) {
261 case nir_instr_type_alu:
262 lower_alu_instr(&b, nir_instr_as_alu(instr), lower_bit_size);
265 case nir_instr_type_intrinsic:
266 lower_intrinsic_instr(&b, nir_instr_as_intrinsic(instr),
270 case nir_instr_type_phi:
271 lower_phi_instr(&b, nir_instr_as_phi(instr),
272 lower_bit_size, last_phi);
276 unreachable("Unsupported instruction type");
283 nir_metadata_preserve(impl, nir_metadata_block_index |
284 nir_metadata_dominance);
286 nir_metadata_preserve(impl, nir_metadata_all);
293 nir_lower_bit_size(nir_shader *shader,
294 nir_lower_bit_size_callback callback,
297 bool progress = false;
299 nir_foreach_function_impl(impl, shader) {
300 progress |= lower_impl(impl, callback, callback_data);
307 split_phi(nir_builder *b, nir_phi_instr *phi)
309 nir_phi_instr *lowered[2] = {
310 nir_phi_instr_create(b->shader),
311 nir_phi_instr_create(b->shader)
313 int num_components = phi->dest.ssa.num_components;
314 assert(phi->dest.ssa.bit_size == 64);
316 nir_foreach_phi_src(src, phi) {
317 assert(num_components == src->src.ssa->num_components);
319 b->cursor = nir_before_src(&src->src);
321 nir_ssa_def *x = nir_unpack_64_2x32_split_x(b, src->src.ssa);
322 nir_ssa_def *y = nir_unpack_64_2x32_split_y(b, src->src.ssa);
324 nir_phi_instr_add_src(lowered[0], src->pred, nir_src_for_ssa(x));
325 nir_phi_instr_add_src(lowered[1], src->pred, nir_src_for_ssa(y));
328 nir_ssa_dest_init(&lowered[0]->instr, &lowered[0]->dest, num_components,
330 nir_ssa_dest_init(&lowered[1]->instr, &lowered[1]->dest, num_components,
333 b->cursor = nir_before_instr(&phi->instr);
334 nir_builder_instr_insert(b, &lowered[0]->instr);
335 nir_builder_instr_insert(b, &lowered[1]->instr);
337 b->cursor = nir_after_phis(nir_cursor_current_block(b->cursor));
338 nir_ssa_def *merged = nir_pack_64_2x32_split(b, &lowered[0]->dest.ssa, &lowered[1]->dest.ssa);
339 nir_ssa_def_rewrite_uses(&phi->dest.ssa, merged);
340 nir_instr_remove(&phi->instr);
344 lower_64bit_phi_instr(nir_builder *b, nir_instr *instr, UNUSED void *cb_data)
346 if (instr->type != nir_instr_type_phi)
349 nir_phi_instr *phi = nir_instr_as_phi(instr);
350 assert(phi->dest.is_ssa);
352 if (phi->dest.ssa.bit_size <= 32)
360 nir_lower_64bit_phis(nir_shader *shader)
362 return nir_shader_instructions_pass(shader, lower_64bit_phi_instr,
363 nir_metadata_block_index |
364 nir_metadata_dominance,