xref: /aosp_15_r20/external/mesa3d/src/compiler/nir/nir_lower_convert_alu_types.c (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 /*
2  * Copyright © 2020 Intel Corporation
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 #include "nir_conversion_builder.h"
27 
28 static bool
try_simplify_convert_intrin(nir_intrinsic_instr * conv)29 try_simplify_convert_intrin(nir_intrinsic_instr *conv)
30 {
31    bool progress = false;
32 
33    nir_alu_type src_type = nir_intrinsic_src_type(conv);
34    nir_alu_type dest_type = nir_intrinsic_dest_type(conv);
35 
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);
41       progress = true;
42    }
43 
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);
47       progress = true;
48    }
49 
50    return progress;
51 }
52 
53 static void
lower_convert_alu_types_instr(nir_builder * b,nir_intrinsic_instr * conv)54 lower_convert_alu_types_instr(nir_builder *b, nir_intrinsic_instr *conv)
55 {
56    assert(conv->intrinsic == nir_intrinsic_convert_alu_types);
57 
58    b->cursor = nir_instr_remove(&conv->instr);
59    nir_def *val =
60       nir_convert_with_rounding(b, conv->src[0].ssa,
61                                 nir_intrinsic_src_type(conv),
62                                 nir_intrinsic_dest_type(conv),
63                                 nir_intrinsic_rounding_mode(conv),
64                                 nir_intrinsic_saturate(conv));
65    nir_def_rewrite_uses(&conv->def, val);
66 }
67 
68 static bool
opt_simplify_convert_alu_types_impl(nir_function_impl * impl)69 opt_simplify_convert_alu_types_impl(nir_function_impl *impl)
70 {
71    bool progress = false;
72    bool lowered_instr = false;
73 
74    nir_builder b = nir_builder_create(impl);
75 
76    nir_foreach_block(block, impl) {
77       nir_foreach_instr_safe(instr, block) {
78          if (instr->type != nir_instr_type_intrinsic)
79             continue;
80 
81          nir_intrinsic_instr *conv = nir_instr_as_intrinsic(instr);
82          if (conv->intrinsic != nir_intrinsic_convert_alu_types)
83             continue;
84 
85          if (try_simplify_convert_intrin(conv))
86             progress = true;
87 
88          if (nir_intrinsic_rounding_mode(conv) == nir_rounding_mode_undef &&
89              !nir_intrinsic_saturate(conv)) {
90             lower_convert_alu_types_instr(&b, conv);
91             lowered_instr = true;
92          }
93       }
94    }
95 
96    if (lowered_instr) {
97       nir_metadata_preserve(impl, nir_metadata_control_flow);
98    } else {
99       nir_metadata_preserve(impl, nir_metadata_all);
100    }
101 
102    return progress;
103 }
104 
105 bool
nir_opt_simplify_convert_alu_types(nir_shader * shader)106 nir_opt_simplify_convert_alu_types(nir_shader *shader)
107 {
108    bool progress = false;
109 
110    nir_foreach_function_impl(impl, shader) {
111       if (opt_simplify_convert_alu_types_impl(impl))
112          progress = true;
113    }
114 
115    return progress;
116 }
117 
118 static bool
lower_convert_alu_types_impl(nir_function_impl * impl,bool (* should_lower)(nir_intrinsic_instr *))119 lower_convert_alu_types_impl(nir_function_impl *impl,
120                              bool (*should_lower)(nir_intrinsic_instr *))
121 {
122    bool progress = false;
123 
124    nir_builder b = nir_builder_create(impl);
125 
126    nir_foreach_block(block, impl) {
127       nir_foreach_instr_safe(instr, block) {
128          if (instr->type != nir_instr_type_intrinsic)
129             continue;
130 
131          nir_intrinsic_instr *conv = nir_instr_as_intrinsic(instr);
132          if (conv->intrinsic != nir_intrinsic_convert_alu_types)
133             continue;
134 
135          if (should_lower != NULL && !should_lower(conv))
136             continue;
137 
138          lower_convert_alu_types_instr(&b, conv);
139          progress = true;
140       }
141    }
142 
143    if (progress) {
144       nir_metadata_preserve(impl, nir_metadata_control_flow);
145    } else {
146       nir_metadata_preserve(impl, nir_metadata_all);
147    }
148 
149    return progress;
150 }
151 
152 bool
nir_lower_convert_alu_types(nir_shader * shader,bool (* should_lower)(nir_intrinsic_instr *))153 nir_lower_convert_alu_types(nir_shader *shader,
154                             bool (*should_lower)(nir_intrinsic_instr *))
155 {
156    bool progress = false;
157 
158    nir_foreach_function_impl(impl, shader) {
159       if (lower_convert_alu_types_impl(impl, should_lower))
160          progress = true;
161    }
162 
163    return progress;
164 }
165 
166 static bool
is_constant(nir_intrinsic_instr * conv)167 is_constant(nir_intrinsic_instr *conv)
168 {
169    assert(conv->intrinsic == nir_intrinsic_convert_alu_types);
170    return nir_src_is_const(conv->src[0]);
171 }
172 
173 bool
nir_lower_constant_convert_alu_types(nir_shader * shader)174 nir_lower_constant_convert_alu_types(nir_shader *shader)
175 {
176    return nir_lower_convert_alu_types(shader, is_constant);
177 }
178 
179 static bool
is_alu_conversion(const nir_instr * instr,UNUSED const void * _data)180 is_alu_conversion(const nir_instr *instr, UNUSED const void *_data)
181 {
182    return instr->type == nir_instr_type_alu &&
183           nir_op_infos[nir_instr_as_alu(instr)->op].is_conversion;
184 }
185 
186 static nir_def *
lower_alu_conversion(nir_builder * b,nir_instr * instr,UNUSED void * _data)187 lower_alu_conversion(nir_builder *b, nir_instr *instr, UNUSED void *_data)
188 {
189    nir_alu_instr *alu = nir_instr_as_alu(instr);
190    nir_def *src = nir_ssa_for_alu_src(b, alu, 0);
191    nir_alu_type src_type = nir_op_infos[alu->op].input_types[0] | src->bit_size;
192    nir_alu_type dst_type = nir_op_infos[alu->op].output_type;
193    return nir_convert_alu_types(b, alu->def.bit_size, src,
194                                 .src_type = src_type, .dest_type = dst_type,
195                                 .rounding_mode = nir_rounding_mode_undef,
196                                 .saturate = false);
197 }
198 
199 bool
nir_lower_alu_conversion_to_intrinsic(nir_shader * shader)200 nir_lower_alu_conversion_to_intrinsic(nir_shader *shader)
201 {
202    return nir_shader_lower_instructions(shader, is_alu_conversion,
203                                         lower_alu_conversion, NULL);
204 }
205