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.
34 lower_instr(nir_builder
*bld
, nir_alu_instr
*alu
, unsigned bit_size
)
36 const nir_op op
= alu
->op
;
37 unsigned dst_bit_size
= alu
->dest
.dest
.ssa
.bit_size
;
39 bld
->cursor
= nir_before_instr(&alu
->instr
);
41 /* Convert each source to the requested bit-size */
42 nir_ssa_def
*srcs
[NIR_MAX_VEC_COMPONENTS
] = { NULL
};
43 for (unsigned i
= 0; i
< nir_op_infos
[op
].num_inputs
; i
++) {
44 nir_ssa_def
*src
= nir_ssa_for_alu_src(bld
, alu
, i
);
46 nir_alu_type type
= nir_op_infos
[op
].input_types
[i
];
47 if (nir_alu_type_get_type_size(type
) == 0)
48 src
= nir_convert_to_bit_size(bld
, src
, type
, bit_size
);
50 if (i
== 1 && (op
== nir_op_ishl
|| op
== nir_op_ishr
|| op
== nir_op_ushr
)) {
51 assert(util_is_power_of_two_nonzero(dst_bit_size
));
52 src
= nir_iand(bld
, src
, nir_imm_int(bld
, dst_bit_size
- 1));
58 /* Emit the lowered ALU instruction */
59 nir_ssa_def
*lowered_dst
= NULL
;
60 if (op
== nir_op_imul_high
|| op
== nir_op_umul_high
) {
61 assert(dst_bit_size
* 2 <= bit_size
);
62 nir_ssa_def
*lowered_dst
= nir_imul(bld
, srcs
[0], srcs
[1]);
63 if (nir_op_infos
[op
].output_type
& nir_type_uint
)
64 lowered_dst
= nir_ushr_imm(bld
, lowered_dst
, dst_bit_size
);
66 lowered_dst
= nir_ishr_imm(bld
, lowered_dst
, dst_bit_size
);
68 lowered_dst
= nir_build_alu_src_arr(bld
, op
, srcs
);
72 /* Convert result back to the original bit-size */
73 nir_alu_type type
= nir_op_infos
[op
].output_type
;
74 nir_ssa_def
*dst
= nir_convert_to_bit_size(bld
, lowered_dst
, type
, dst_bit_size
);
75 nir_ssa_def_rewrite_uses(&alu
->dest
.dest
.ssa
, nir_src_for_ssa(dst
));
79 lower_impl(nir_function_impl
*impl
,
80 nir_lower_bit_size_callback callback
,
84 nir_builder_init(&b
, impl
);
85 bool progress
= false;
87 nir_foreach_block(block
, impl
) {
88 nir_foreach_instr_safe(instr
, block
) {
89 if (instr
->type
!= nir_instr_type_alu
)
92 nir_alu_instr
*alu
= nir_instr_as_alu(instr
);
93 assert(alu
->dest
.dest
.is_ssa
);
95 unsigned lower_bit_size
= callback(alu
, callback_data
);
96 if (lower_bit_size
== 0)
99 assert(lower_bit_size
!= alu
->dest
.dest
.ssa
.bit_size
);
101 lower_instr(&b
, alu
, lower_bit_size
);
107 nir_metadata_preserve(impl
, nir_metadata_block_index
|
108 nir_metadata_dominance
);
110 nir_metadata_preserve(impl
, nir_metadata_all
);
117 nir_lower_bit_size(nir_shader
*shader
,
118 nir_lower_bit_size_callback callback
,
121 bool progress
= false;
123 nir_foreach_function(function
, shader
) {
125 progress
|= lower_impl(function
->impl
, callback
, callback_data
);