• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 bool
lower_convert_alu_types_instr(nir_builder * b,nir_intrinsic_instr * conv,void * data)54 lower_convert_alu_types_instr(nir_builder *b, nir_intrinsic_instr *conv, void *data)
55 {
56    bool (*cb)(nir_intrinsic_instr *) = data;
57    if (conv->intrinsic != nir_intrinsic_convert_alu_types || (cb && !cb(conv)))
58       return false;
59 
60    b->cursor = nir_instr_remove(&conv->instr);
61    nir_def *val =
62       nir_convert_with_rounding(b, conv->src[0].ssa,
63                                 nir_intrinsic_src_type(conv),
64                                 nir_intrinsic_dest_type(conv),
65                                 nir_intrinsic_rounding_mode(conv),
66                                 nir_intrinsic_saturate(conv));
67    nir_def_rewrite_uses(&conv->def, val);
68    return true;
69 }
70 
71 static bool
opt_simplify(nir_builder * b,nir_intrinsic_instr * intr,void * _)72 opt_simplify(nir_builder *b, nir_intrinsic_instr *intr, void *_)
73 {
74    if (intr->intrinsic != nir_intrinsic_convert_alu_types)
75       return false;
76 
77    bool progress = try_simplify_convert_intrin(intr);
78 
79    if (nir_intrinsic_rounding_mode(intr) == nir_rounding_mode_undef &&
80        !nir_intrinsic_saturate(intr)) {
81       lower_convert_alu_types_instr(b, intr, NULL);
82       progress = true;
83    }
84 
85    return progress;
86 }
87 
88 bool
nir_opt_simplify_convert_alu_types(nir_shader * shader)89 nir_opt_simplify_convert_alu_types(nir_shader *shader)
90 {
91    return nir_shader_intrinsics_pass(shader, opt_simplify,
92                                      nir_metadata_control_flow, NULL);
93 }
94 
95 bool
nir_lower_convert_alu_types(nir_shader * shader,bool (* should_lower)(nir_intrinsic_instr *))96 nir_lower_convert_alu_types(nir_shader *shader,
97                             bool (*should_lower)(nir_intrinsic_instr *))
98 {
99    return nir_shader_intrinsics_pass(shader, lower_convert_alu_types_instr,
100                                      nir_metadata_control_flow, should_lower);
101 }
102 
103 static bool
is_constant(nir_intrinsic_instr * conv)104 is_constant(nir_intrinsic_instr *conv)
105 {
106    assert(conv->intrinsic == nir_intrinsic_convert_alu_types);
107    return nir_src_is_const(conv->src[0]);
108 }
109 
110 bool
nir_lower_constant_convert_alu_types(nir_shader * shader)111 nir_lower_constant_convert_alu_types(nir_shader *shader)
112 {
113    return nir_lower_convert_alu_types(shader, is_constant);
114 }
115 
116 static bool
is_alu_conversion(const nir_instr * instr,UNUSED const void * _data)117 is_alu_conversion(const nir_instr *instr, UNUSED const void *_data)
118 {
119    return instr->type == nir_instr_type_alu &&
120           nir_op_infos[nir_instr_as_alu(instr)->op].is_conversion;
121 }
122 
123 static nir_def *
lower_alu_conversion(nir_builder * b,nir_instr * instr,UNUSED void * _data)124 lower_alu_conversion(nir_builder *b, nir_instr *instr, UNUSED void *_data)
125 {
126    nir_alu_instr *alu = nir_instr_as_alu(instr);
127    nir_def *src = nir_ssa_for_alu_src(b, alu, 0);
128    nir_alu_type src_type = nir_op_infos[alu->op].input_types[0] | src->bit_size;
129    nir_alu_type dst_type = nir_op_infos[alu->op].output_type;
130    return nir_convert_alu_types(b, alu->def.bit_size, src,
131                                 .src_type = src_type, .dest_type = dst_type,
132                                 .rounding_mode = nir_rounding_mode_undef,
133                                 .saturate = false);
134 }
135 
136 bool
nir_lower_alu_conversion_to_intrinsic(nir_shader * shader)137 nir_lower_alu_conversion_to_intrinsic(nir_shader *shader)
138 {
139    return nir_shader_lower_instructions(shader, is_alu_conversion,
140                                         lower_alu_conversion, NULL);
141 }
142