• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright © 2018 Intel Corporation
3  * Copyright © 2019 Vasily Khoruzhick <anarsoul@gmail.com>
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice (including the next
13  * paragraph) shall be included in all copies or substantial portions of the
14  * Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22  * IN THE SOFTWARE.
23  */
24 
25 #include "nir.h"
26 #include "nir_builder.h"
27 
28 static bool
assert_ssa_def_is_not_int(nir_ssa_def * def,void * arg)29 assert_ssa_def_is_not_int(nir_ssa_def *def, void *arg)
30 {
31    ASSERTED BITSET_WORD *int_types = arg;
32    assert(!BITSET_TEST(int_types, def->index));
33    return true;
34 }
35 
36 static bool
lower_alu_instr(nir_builder * b,nir_alu_instr * alu)37 lower_alu_instr(nir_builder *b, nir_alu_instr *alu)
38 {
39    const nir_op_info *info = &nir_op_infos[alu->op];
40 
41    bool is_bool_only = alu->dest.dest.ssa.bit_size == 1;
42    for (unsigned i = 0; i < info->num_inputs; i++) {
43       if (alu->src[i].src.ssa->bit_size != 1)
44          is_bool_only = false;
45    }
46 
47    if (is_bool_only) {
48       /* avoid lowering integers ops are used for booleans (ieq,ine,etc) */
49       return false;
50    }
51 
52    b->cursor = nir_before_instr(&alu->instr);
53 
54    /* Replacement SSA value */
55    nir_ssa_def *rep = NULL;
56    switch (alu->op) {
57    case nir_op_mov:
58    case nir_op_vec2:
59    case nir_op_vec3:
60    case nir_op_vec4:
61    case nir_op_bcsel:
62       /* These we expect to have integers but the opcode doesn't change */
63       break;
64 
65    case nir_op_b2i32: alu->op = nir_op_b2f32; break;
66    case nir_op_i2f32: alu->op = nir_op_mov; break;
67    case nir_op_u2f32: alu->op = nir_op_mov; break;
68 
69    case nir_op_f2i32: {
70       alu->op = nir_op_ftrunc;
71 
72       /* If the source was already integer, then we did't need to truncate and
73        * can switch it to a mov that can be copy-propagated away.
74        */
75       nir_alu_instr *src_alu = nir_src_as_alu_instr(alu->src[0].src);
76       if (src_alu) {
77          switch (src_alu->op) {
78          case nir_op_fround_even:
79          case nir_op_fceil:
80          case nir_op_ftrunc:
81          case nir_op_ffloor:
82             alu->op = nir_op_mov;
83             break;
84          default:
85             break;
86          }
87       }
88       break;
89    }
90 
91    case nir_op_f2u32: alu->op = nir_op_ffloor; break;
92    case nir_op_i2b1: alu->op = nir_op_f2b1; break;
93 
94    case nir_op_ilt: alu->op = nir_op_flt; break;
95    case nir_op_ige: alu->op = nir_op_fge; break;
96    case nir_op_ieq: alu->op = nir_op_feq; break;
97    case nir_op_ine: alu->op = nir_op_fneu; break;
98    case nir_op_ult: alu->op = nir_op_flt; break;
99    case nir_op_uge: alu->op = nir_op_fge; break;
100 
101    case nir_op_iadd: alu->op = nir_op_fadd; break;
102    case nir_op_isub: alu->op = nir_op_fsub; break;
103    case nir_op_imul: alu->op = nir_op_fmul; break;
104 
105    case nir_op_idiv: {
106       nir_ssa_def *x = nir_ssa_for_alu_src(b, alu, 0);
107       nir_ssa_def *y = nir_ssa_for_alu_src(b, alu, 1);
108 
109       /* Hand-lower fdiv, since lower_int_to_float is after nir_opt_algebraic. */
110       if (b->shader->options->lower_fdiv) {
111          rep = nir_ftrunc(b, nir_fmul(b, x, nir_frcp(b, y)));
112       } else {
113          rep = nir_ftrunc(b, nir_fdiv(b, x, y));
114       }
115       break;
116    }
117 
118    case nir_op_iabs: alu->op = nir_op_fabs; break;
119    case nir_op_ineg: alu->op = nir_op_fneg; break;
120    case nir_op_imax: alu->op = nir_op_fmax; break;
121    case nir_op_imin: alu->op = nir_op_fmin; break;
122    case nir_op_umax: alu->op = nir_op_fmax; break;
123    case nir_op_umin: alu->op = nir_op_fmin; break;
124 
125    case nir_op_ball_iequal2:  alu->op = nir_op_ball_fequal2; break;
126    case nir_op_ball_iequal3:  alu->op = nir_op_ball_fequal3; break;
127    case nir_op_ball_iequal4:  alu->op = nir_op_ball_fequal4; break;
128    case nir_op_bany_inequal2: alu->op = nir_op_bany_fnequal2; break;
129    case nir_op_bany_inequal3: alu->op = nir_op_bany_fnequal3; break;
130    case nir_op_bany_inequal4: alu->op = nir_op_bany_fnequal4; break;
131 
132    default:
133       assert(nir_alu_type_get_base_type(info->output_type) != nir_type_int &&
134              nir_alu_type_get_base_type(info->output_type) != nir_type_uint);
135       for (unsigned i = 0; i < info->num_inputs; i++) {
136          assert(nir_alu_type_get_base_type(info->input_types[i]) != nir_type_int &&
137                 nir_alu_type_get_base_type(info->input_types[i]) != nir_type_uint);
138       }
139       return false;
140    }
141 
142    if (rep) {
143       /* We've emitted a replacement instruction */
144       nir_ssa_def_rewrite_uses(&alu->dest.dest.ssa, rep);
145       nir_instr_remove(&alu->instr);
146    }
147 
148    return true;
149 }
150 
151 static bool
nir_lower_int_to_float_impl(nir_function_impl * impl)152 nir_lower_int_to_float_impl(nir_function_impl *impl)
153 {
154    bool progress = false;
155    BITSET_WORD *float_types = NULL, *int_types = NULL;
156 
157    nir_builder b;
158    nir_builder_init(&b, impl);
159 
160    nir_index_ssa_defs(impl);
161    float_types = calloc(BITSET_WORDS(impl->ssa_alloc),
162                         sizeof(BITSET_WORD));
163    int_types = calloc(BITSET_WORDS(impl->ssa_alloc),
164                       sizeof(BITSET_WORD));
165    nir_gather_ssa_types(impl, float_types, int_types);
166 
167    nir_foreach_block(block, impl) {
168       nir_foreach_instr_safe(instr, block) {
169          switch (instr->type) {
170          case nir_instr_type_alu:
171             progress |= lower_alu_instr(&b, nir_instr_as_alu(instr));
172             break;
173 
174          case nir_instr_type_load_const: {
175             nir_load_const_instr *load = nir_instr_as_load_const(instr);
176             if (load->def.bit_size != 1 && BITSET_TEST(int_types, load->def.index)) {
177                for (unsigned i = 0; i < load->def.num_components; i++)
178                   load->value[i].f32 = load->value[i].i32;
179             }
180             break;
181          }
182 
183          case nir_instr_type_intrinsic:
184          case nir_instr_type_ssa_undef:
185          case nir_instr_type_phi:
186          case nir_instr_type_tex:
187             break;
188 
189          default:
190             nir_foreach_ssa_def(instr, assert_ssa_def_is_not_int, (void *)int_types);
191             break;
192          }
193       }
194    }
195 
196    if (progress) {
197       nir_metadata_preserve(impl, nir_metadata_block_index |
198                                   nir_metadata_dominance);
199    } else {
200       nir_metadata_preserve(impl, nir_metadata_all);
201    }
202 
203    free(float_types);
204    free(int_types);
205 
206    return progress;
207 }
208 
209 bool
nir_lower_int_to_float(nir_shader * shader)210 nir_lower_int_to_float(nir_shader *shader)
211 {
212    bool progress = false;
213 
214    nir_foreach_function(function, shader) {
215       if (function->impl && nir_lower_int_to_float_impl(function->impl))
216          progress = true;
217    }
218 
219    return progress;
220 }
221