• 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    case nir_op_f2i32: alu->op = nir_op_ftrunc; break;
69    case nir_op_f2u32: alu->op = nir_op_ffloor; break;
70    case nir_op_i2b1: alu->op = nir_op_f2b1; break;
71 
72    case nir_op_ilt: alu->op = nir_op_flt; break;
73    case nir_op_ige: alu->op = nir_op_fge; break;
74    case nir_op_ieq: alu->op = nir_op_feq; break;
75    case nir_op_ine: alu->op = nir_op_fneu; break;
76    case nir_op_ult: alu->op = nir_op_flt; break;
77    case nir_op_uge: alu->op = nir_op_fge; break;
78 
79    case nir_op_iadd: alu->op = nir_op_fadd; break;
80    case nir_op_isub: alu->op = nir_op_fsub; break;
81    case nir_op_imul: alu->op = nir_op_fmul; break;
82    case nir_op_idiv:
83       rep = nir_ftrunc(b, nir_fdiv(b,
84                                    nir_ssa_for_alu_src(b, alu, 0),
85                                    nir_ssa_for_alu_src(b, alu, 1)));
86       break;
87    case nir_op_iabs: alu->op = nir_op_fabs; break;
88    case nir_op_ineg: alu->op = nir_op_fneg; break;
89    case nir_op_imax: alu->op = nir_op_fmax; break;
90    case nir_op_imin: alu->op = nir_op_fmin; break;
91    case nir_op_umax: alu->op = nir_op_fmax; break;
92    case nir_op_umin: alu->op = nir_op_fmin; break;
93 
94    case nir_op_ball_iequal2:  alu->op = nir_op_ball_fequal2; break;
95    case nir_op_ball_iequal3:  alu->op = nir_op_ball_fequal3; break;
96    case nir_op_ball_iequal4:  alu->op = nir_op_ball_fequal4; break;
97    case nir_op_bany_inequal2: alu->op = nir_op_bany_fnequal2; break;
98    case nir_op_bany_inequal3: alu->op = nir_op_bany_fnequal3; break;
99    case nir_op_bany_inequal4: alu->op = nir_op_bany_fnequal4; break;
100 
101    default:
102       assert(nir_alu_type_get_base_type(info->output_type) != nir_type_int &&
103              nir_alu_type_get_base_type(info->output_type) != nir_type_uint);
104       for (unsigned i = 0; i < info->num_inputs; i++) {
105          assert(nir_alu_type_get_base_type(info->input_types[i]) != nir_type_int &&
106                 nir_alu_type_get_base_type(info->input_types[i]) != nir_type_uint);
107       }
108       return false;
109    }
110 
111    if (rep) {
112       /* We've emitted a replacement instruction */
113       nir_ssa_def_rewrite_uses(&alu->dest.dest.ssa, rep);
114       nir_instr_remove(&alu->instr);
115    }
116 
117    return true;
118 }
119 
120 static bool
nir_lower_int_to_float_impl(nir_function_impl * impl)121 nir_lower_int_to_float_impl(nir_function_impl *impl)
122 {
123    bool progress = false;
124    BITSET_WORD *float_types = NULL, *int_types = NULL;
125 
126    nir_builder b;
127    nir_builder_init(&b, impl);
128 
129    nir_index_ssa_defs(impl);
130    float_types = calloc(BITSET_WORDS(impl->ssa_alloc),
131                         sizeof(BITSET_WORD));
132    int_types = calloc(BITSET_WORDS(impl->ssa_alloc),
133                       sizeof(BITSET_WORD));
134    nir_gather_ssa_types(impl, float_types, int_types);
135 
136    nir_foreach_block(block, impl) {
137       nir_foreach_instr_safe(instr, block) {
138          switch (instr->type) {
139          case nir_instr_type_alu:
140             progress |= lower_alu_instr(&b, nir_instr_as_alu(instr));
141             break;
142 
143          case nir_instr_type_load_const: {
144             nir_load_const_instr *load = nir_instr_as_load_const(instr);
145             if (load->def.bit_size != 1 && BITSET_TEST(int_types, load->def.index)) {
146                for (unsigned i = 0; i < load->def.num_components; i++)
147                   load->value[i].f32 = load->value[i].i32;
148             }
149             break;
150          }
151 
152          case nir_instr_type_intrinsic:
153          case nir_instr_type_ssa_undef:
154          case nir_instr_type_phi:
155          case nir_instr_type_tex:
156             break;
157 
158          default:
159             nir_foreach_ssa_def(instr, assert_ssa_def_is_not_int, (void *)int_types);
160             break;
161          }
162       }
163    }
164 
165    if (progress) {
166       nir_metadata_preserve(impl, nir_metadata_block_index |
167                                   nir_metadata_dominance);
168    } else {
169       nir_metadata_preserve(impl, nir_metadata_all);
170    }
171 
172    free(float_types);
173    free(int_types);
174 
175    return progress;
176 }
177 
178 bool
nir_lower_int_to_float(nir_shader * shader)179 nir_lower_int_to_float(nir_shader *shader)
180 {
181    bool progress = false;
182 
183    nir_foreach_function(function, shader) {
184       if (function->impl && nir_lower_int_to_float_impl(function->impl))
185          progress = true;
186    }
187 
188    return progress;
189 }
190