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