2
* Copyright © 2020 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
25
#include "nir_builder.h"
26
#include "nir_conversion_builder.h"
29
try_simplify_convert_intrin(nir_intrinsic_instr *conv)
31
bool progress = false;
33
nir_alu_type src_type = nir_intrinsic_src_type(conv);
34
nir_alu_type dest_type = nir_intrinsic_dest_type(conv);
36
nir_rounding_mode rounding = nir_intrinsic_rounding_mode(conv);
37
nir_rounding_mode simple_rounding =
38
nir_simplify_conversion_rounding(src_type, dest_type, rounding);
39
if (rounding != simple_rounding) {
40
nir_intrinsic_set_rounding_mode(conv, simple_rounding);
44
if (nir_intrinsic_saturate(conv) &&
45
nir_alu_type_range_contains_type_range(dest_type, src_type)) {
46
nir_intrinsic_set_saturate(conv, false);
54
lower_convert_alu_types_instr(nir_builder *b, nir_intrinsic_instr *conv)
56
assert(conv->intrinsic == nir_intrinsic_convert_alu_types);
57
assert(conv->src[0].is_ssa && conv->dest.is_ssa);
59
b->cursor = nir_instr_remove(&conv->instr);
61
nir_convert_with_rounding(b, conv->src[0].ssa,
62
nir_intrinsic_src_type(conv),
63
nir_intrinsic_dest_type(conv),
64
nir_intrinsic_rounding_mode(conv),
65
nir_intrinsic_saturate(conv));
66
nir_ssa_def_rewrite_uses(&conv->dest.ssa, val);
70
opt_simplify_convert_alu_types_impl(nir_function_impl *impl)
72
bool progress = false;
73
bool lowered_instr = false;
76
nir_builder_init(&b, impl);
78
nir_foreach_block(block, impl) {
79
nir_foreach_instr_safe(instr, block) {
80
if (instr->type != nir_instr_type_intrinsic)
83
nir_intrinsic_instr *conv = nir_instr_as_intrinsic(instr);
84
if (conv->intrinsic != nir_intrinsic_convert_alu_types)
87
if (try_simplify_convert_intrin(conv))
90
if (nir_intrinsic_rounding_mode(conv) == nir_rounding_mode_undef &&
91
!nir_intrinsic_saturate(conv)) {
92
lower_convert_alu_types_instr(&b, conv);
99
nir_metadata_preserve(impl, nir_metadata_block_index |
100
nir_metadata_dominance);
102
nir_metadata_preserve(impl, nir_metadata_all);
109
nir_opt_simplify_convert_alu_types(nir_shader *shader)
111
bool progress = false;
113
nir_foreach_function(func, shader) {
114
if (func->impl && opt_simplify_convert_alu_types_impl(func->impl))
122
lower_convert_alu_types_impl(nir_function_impl *impl,
123
bool (*should_lower)(nir_intrinsic_instr *))
125
bool progress = false;
128
nir_builder_init(&b, impl);
130
nir_foreach_block(block, impl) {
131
nir_foreach_instr_safe(instr, block) {
132
if (instr->type != nir_instr_type_intrinsic)
135
nir_intrinsic_instr *conv = nir_instr_as_intrinsic(instr);
136
if (conv->intrinsic != nir_intrinsic_convert_alu_types)
139
if (should_lower != NULL && !should_lower(conv))
142
lower_convert_alu_types_instr(&b, conv);
148
nir_metadata_preserve(impl, nir_metadata_block_index |
149
nir_metadata_dominance);
151
nir_metadata_preserve(impl, nir_metadata_all);
158
nir_lower_convert_alu_types(nir_shader *shader,
159
bool (*should_lower)(nir_intrinsic_instr *))
161
bool progress = false;
163
nir_foreach_function(func, shader) {
164
if (func->impl && lower_convert_alu_types_impl(func->impl, should_lower))
172
is_constant(nir_intrinsic_instr *conv)
174
assert(conv->intrinsic == nir_intrinsic_convert_alu_types);
175
return nir_src_is_const(conv->src[0]);
179
nir_lower_constant_convert_alu_types(nir_shader *shader)
181
return nir_lower_convert_alu_types(shader, is_constant);
185
is_alu_conversion(const nir_instr *instr, UNUSED const void *_data)
187
return instr->type == nir_instr_type_alu &&
188
nir_op_infos[nir_instr_as_alu(instr)->op].is_conversion;
192
lower_alu_conversion(nir_builder *b, nir_instr *instr, UNUSED void *_data)
194
nir_alu_instr *alu = nir_instr_as_alu(instr);
195
nir_ssa_def *src = nir_ssa_for_alu_src(b, alu, 0);
196
nir_alu_type src_type = nir_op_infos[alu->op].input_types[0] | src->bit_size;
197
nir_alu_type dst_type = nir_op_infos[alu->op].output_type;
198
return nir_convert_alu_types(b, alu->dest.dest.ssa.bit_size, src,
199
.src_type = src_type, .dest_type = dst_type,
200
.rounding_mode = nir_rounding_mode_undef,
205
nir_lower_alu_conversion_to_intrinsic(nir_shader *shader)
207
return nir_shader_lower_instructions(shader, is_alu_conversion,
208
lower_alu_conversion, NULL);