0dd69c958f9a6b6f2e6e47cff7fa5cbda23138e6
[mesa.git] / src / compiler / nir / nir_lower_bool_to_float.c
1 /*
2 * Copyright © 2018 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
27 static bool
28 assert_ssa_def_is_not_1bit(nir_ssa_def *def, UNUSED void *unused)
29 {
30 assert(def->bit_size > 1);
31 return true;
32 }
33
34 static bool
35 rewrite_1bit_ssa_def_to_32bit(nir_ssa_def *def, void *_progress)
36 {
37 bool *progress = _progress;
38 if (def->bit_size == 1) {
39 def->bit_size = 32;
40 *progress = true;
41 }
42 return true;
43 }
44
45 static bool
46 lower_alu_instr(nir_builder *b, nir_alu_instr *alu)
47 {
48 const nir_op_info *op_info = &nir_op_infos[alu->op];
49
50 b->cursor = nir_before_instr(&alu->instr);
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_vec8:
60 case nir_op_vec16:
61 /* These we expect to have booleans but the opcode doesn't change */
62 break;
63
64 case nir_op_b2f32: alu->op = nir_op_mov; break;
65 case nir_op_b2i32: alu->op = nir_op_mov; break;
66 case nir_op_f2b1:
67 case nir_op_i2b1:
68 rep = nir_sne(b, nir_ssa_for_alu_src(b, alu, 0),
69 nir_imm_float(b, 0));
70 break;
71 case nir_op_b2b1: alu->op = nir_op_mov; break;
72
73 case nir_op_flt: alu->op = nir_op_slt; break;
74 case nir_op_fge: alu->op = nir_op_sge; break;
75 case nir_op_feq: alu->op = nir_op_seq; break;
76 case nir_op_fneu: alu->op = nir_op_sne; break;
77 case nir_op_ilt: alu->op = nir_op_slt; break;
78 case nir_op_ige: alu->op = nir_op_sge; break;
79 case nir_op_ieq: alu->op = nir_op_seq; break;
80 case nir_op_ine: alu->op = nir_op_sne; break;
81 case nir_op_ult: alu->op = nir_op_slt; break;
82 case nir_op_uge: alu->op = nir_op_sge; break;
83
84 case nir_op_ball_fequal2: alu->op = nir_op_fall_equal2; break;
85 case nir_op_ball_fequal3: alu->op = nir_op_fall_equal3; break;
86 case nir_op_ball_fequal4: alu->op = nir_op_fall_equal4; break;
87 case nir_op_bany_fnequal2: alu->op = nir_op_fany_nequal2; break;
88 case nir_op_bany_fnequal3: alu->op = nir_op_fany_nequal3; break;
89 case nir_op_bany_fnequal4: alu->op = nir_op_fany_nequal4; break;
90 case nir_op_ball_iequal2: alu->op = nir_op_fall_equal2; break;
91 case nir_op_ball_iequal3: alu->op = nir_op_fall_equal3; break;
92 case nir_op_ball_iequal4: alu->op = nir_op_fall_equal4; break;
93 case nir_op_bany_inequal2: alu->op = nir_op_fany_nequal2; break;
94 case nir_op_bany_inequal3: alu->op = nir_op_fany_nequal3; break;
95 case nir_op_bany_inequal4: alu->op = nir_op_fany_nequal4; break;
96
97 case nir_op_bcsel: alu->op = nir_op_fcsel; break;
98
99 case nir_op_iand: alu->op = nir_op_fmul; break;
100 case nir_op_ixor: alu->op = nir_op_sne; break;
101 case nir_op_ior: alu->op = nir_op_fmax; break;
102
103 case nir_op_inot:
104 rep = nir_seq(b, nir_ssa_for_alu_src(b, alu, 0),
105 nir_imm_float(b, 0));
106 break;
107
108 default:
109 assert(alu->dest.dest.ssa.bit_size > 1);
110 for (unsigned i = 0; i < op_info->num_inputs; i++)
111 assert(alu->src[i].src.ssa->bit_size > 1);
112 return false;
113 }
114
115 if (rep) {
116 /* We've emitted a replacement instruction */
117 nir_ssa_def_rewrite_uses(&alu->dest.dest.ssa, nir_src_for_ssa(rep));
118 nir_instr_remove(&alu->instr);
119 } else {
120 if (alu->dest.dest.ssa.bit_size == 1)
121 alu->dest.dest.ssa.bit_size = 32;
122 }
123
124 return true;
125 }
126
127 static bool
128 nir_lower_bool_to_float_impl(nir_function_impl *impl)
129 {
130 bool progress = false;
131
132 nir_builder b;
133 nir_builder_init(&b, impl);
134
135 nir_foreach_block(block, impl) {
136 nir_foreach_instr_safe(instr, block) {
137 switch (instr->type) {
138 case nir_instr_type_alu:
139 progress |= lower_alu_instr(&b, nir_instr_as_alu(instr));
140 break;
141
142 case nir_instr_type_load_const: {
143 nir_load_const_instr *load = nir_instr_as_load_const(instr);
144 if (load->def.bit_size == 1) {
145 nir_const_value *value = load->value;
146 for (unsigned i = 0; i < load->def.num_components; i++)
147 load->value[i].f32 = value[i].b ? 1.0 : 0.0;
148 load->def.bit_size = 32;
149 progress = true;
150 }
151 break;
152 }
153
154 case nir_instr_type_intrinsic:
155 case nir_instr_type_ssa_undef:
156 case nir_instr_type_phi:
157 case nir_instr_type_tex:
158 nir_foreach_ssa_def(instr, rewrite_1bit_ssa_def_to_32bit,
159 &progress);
160 break;
161
162 default:
163 nir_foreach_ssa_def(instr, assert_ssa_def_is_not_1bit, NULL);
164 }
165 }
166 }
167
168 if (progress) {
169 nir_metadata_preserve(impl, nir_metadata_block_index |
170 nir_metadata_dominance);
171 }
172
173 return progress;
174 }
175
176 bool
177 nir_lower_bool_to_float(nir_shader *shader)
178 {
179 bool progress = false;
180
181 nir_foreach_function(function, shader) {
182 if (function->impl && nir_lower_bool_to_float_impl(function->impl))
183 progress = true;
184 }
185
186 return progress;
187 }