1 /*
2 * Copyright © 2015 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 "elk_nir.h"
25
26 /*
27 * This file implements an analysis pass that determines when we have to do
28 * a boolean resolve on Gen <= 5. Instructions that need a boolean resolve
29 * will have the booleans portion of the instr->pass_flags field set to
30 * ELK_NIR_BOOLEAN_NEEDS_RESOLVE.
31 */
32
33
34 /** Returns the resolve status for the given source
35 *
36 * If the source has a parent instruction then the resolve status is the
37 * status of the parent instruction. If the source does not have a parent
38 * instruction then we don't know so we return NON_BOOLEAN.
39 */
40 static uint8_t
get_resolve_status_for_src(nir_src * src)41 get_resolve_status_for_src(nir_src *src)
42 {
43 nir_instr *src_instr = src->ssa->parent_instr;
44 uint8_t resolve_status = src_instr->pass_flags & ELK_NIR_BOOLEAN_MASK;
45
46 /* If the source instruction needs resolve, then from the perspective
47 * of the user, it's a true boolean.
48 */
49 if (resolve_status == ELK_NIR_BOOLEAN_NEEDS_RESOLVE)
50 resolve_status = ELK_NIR_BOOLEAN_NO_RESOLVE;
51 return resolve_status;
52 }
53
54 /** Marks the given source as needing a resolve
55 *
56 * If the given source corresponds to an unresolved boolean it marks it as
57 * needing a resolve. Otherwise, we leave it alone.
58 */
59 static bool
src_mark_needs_resolve(nir_src * src,void * void_state)60 src_mark_needs_resolve(nir_src *src, void *void_state)
61 {
62 nir_instr *src_instr = src->ssa->parent_instr;
63 uint8_t resolve_status = src_instr->pass_flags & ELK_NIR_BOOLEAN_MASK;
64
65 /* If the source instruction is unresolved, then mark it as needing
66 * to be resolved.
67 */
68 if (resolve_status == ELK_NIR_BOOLEAN_UNRESOLVED) {
69 src_instr->pass_flags &= ~ELK_NIR_BOOLEAN_MASK;
70 src_instr->pass_flags |= ELK_NIR_BOOLEAN_NEEDS_RESOLVE;
71 }
72
73 return true;
74 }
75
76 static bool
analyze_boolean_resolves_block(nir_block * block)77 analyze_boolean_resolves_block(nir_block *block)
78 {
79 nir_foreach_instr(instr, block) {
80 switch (instr->type) {
81 case nir_instr_type_alu: {
82 /* For ALU instructions, the resolve status is handled in a
83 * three-step process.
84 *
85 * 1) Look at the instruction type and sources and determine if it
86 * can be left unresolved.
87 *
88 * 2) Look at the destination and see if we have to resolve
89 * anyway. (This is the case if this instruction is not the
90 * only instruction writing to a given register.)
91 *
92 * 3) If the instruction has a resolve status other than
93 * BOOL_UNRESOLVED or BOOL_NEEDS_RESOLVE then we walk through
94 * the sources and ensure that they are also resolved. This
95 * ensures that we don't end up with any stray unresolved
96 * booleans going into ADDs or something like that.
97 */
98
99 uint8_t resolve_status;
100 nir_alu_instr *alu = nir_instr_as_alu(instr);
101 switch (alu->op) {
102 case nir_op_b32all_fequal2:
103 case nir_op_b32all_iequal2:
104 case nir_op_b32all_fequal3:
105 case nir_op_b32all_iequal3:
106 case nir_op_b32all_fequal4:
107 case nir_op_b32all_iequal4:
108 case nir_op_b32any_fnequal2:
109 case nir_op_b32any_inequal2:
110 case nir_op_b32any_fnequal3:
111 case nir_op_b32any_inequal3:
112 case nir_op_b32any_fnequal4:
113 case nir_op_b32any_inequal4:
114 /* These are only implemented by the vec4 backend and its
115 * implementation emits resolved booleans. At some point in the
116 * future, this may change and we'll have to remove some of the
117 * above cases.
118 */
119 resolve_status = ELK_NIR_BOOLEAN_NO_RESOLVE;
120 break;
121
122 case nir_op_mov:
123 case nir_op_inot:
124 /* This is a single-source instruction. Just copy the resolve
125 * status from the source.
126 */
127 resolve_status = get_resolve_status_for_src(&alu->src[0].src);
128 break;
129
130 case nir_op_b32csel:
131 case nir_op_iand:
132 case nir_op_ior:
133 case nir_op_ixor: {
134 const unsigned first = alu->op == nir_op_b32csel ? 1 : 0;
135 uint8_t src0_status = get_resolve_status_for_src(&alu->src[first + 0].src);
136 uint8_t src1_status = get_resolve_status_for_src(&alu->src[first + 1].src);
137
138 /* src0 of a bcsel is evaluated as a Boolean with the expectation
139 * that it has already been resolved. Mark it as such.
140 */
141 if (alu->op == nir_op_b32csel)
142 src_mark_needs_resolve(&alu->src[0].src, NULL);
143
144 if (src0_status == src1_status) {
145 resolve_status = src0_status;
146 } else if (src0_status == ELK_NIR_NON_BOOLEAN ||
147 src1_status == ELK_NIR_NON_BOOLEAN) {
148 /* If one of the sources is a non-boolean then the whole
149 * thing is a non-boolean.
150 */
151 resolve_status = ELK_NIR_NON_BOOLEAN;
152 } else {
153 /* At this point one of them is a true boolean and one is a
154 * boolean that needs a resolve. We could either resolve the
155 * unresolved source or we could resolve here. If we resolve
156 * the unresolved source then we get two resolves for the price
157 * of one. Just set this one to BOOLEAN_NO_RESOLVE and we'll
158 * let the code below force a resolve on the unresolved source.
159 */
160 resolve_status = ELK_NIR_BOOLEAN_NO_RESOLVE;
161 }
162 break;
163 }
164
165 default:
166 if (nir_alu_type_get_base_type(nir_op_infos[alu->op].output_type) == nir_type_bool) {
167 /* This instructions will turn into a CMP when we actually emit
168 * them so the result will have to be resolved before it can be
169 * used.
170 */
171 resolve_status = ELK_NIR_BOOLEAN_UNRESOLVED;
172
173 /* Even though the destination is allowed to be left
174 * unresolved, the sources are treated as regular integers or
175 * floats so they need to be resolved.
176 */
177 nir_foreach_src(instr, src_mark_needs_resolve, NULL);
178 } else {
179 resolve_status = ELK_NIR_NON_BOOLEAN;
180 }
181 }
182
183 /* Go ahead allow unresolved booleans. */
184 instr->pass_flags = (instr->pass_flags & ~ELK_NIR_BOOLEAN_MASK) |
185 resolve_status;
186
187 /* Finally, resolve sources if it's needed */
188 switch (resolve_status) {
189 case ELK_NIR_BOOLEAN_NEEDS_RESOLVE:
190 case ELK_NIR_BOOLEAN_UNRESOLVED:
191 /* This instruction is either unresolved or we're doing the
192 * resolve here; leave the sources alone.
193 */
194 break;
195
196 case ELK_NIR_BOOLEAN_NO_RESOLVE:
197 case ELK_NIR_NON_BOOLEAN:
198 nir_foreach_src(instr, src_mark_needs_resolve, NULL);
199 break;
200
201 default:
202 unreachable("Invalid boolean flag");
203 }
204
205 break;
206 }
207
208 case nir_instr_type_load_const: {
209 nir_load_const_instr *load = nir_instr_as_load_const(instr);
210
211 /* For load_const instructions, it's a boolean exactly when it holds
212 * one of the values NIR_TRUE or NIR_FALSE.
213 *
214 * Since load_const instructions don't have any sources, we don't
215 * have to worry about resolving them.
216 */
217 instr->pass_flags &= ~ELK_NIR_BOOLEAN_MASK;
218 if (load->value[0].u32 == NIR_TRUE || load->value[0].u32 == NIR_FALSE) {
219 instr->pass_flags |= ELK_NIR_BOOLEAN_NO_RESOLVE;
220 } else {
221 instr->pass_flags |= ELK_NIR_NON_BOOLEAN;
222 }
223 continue;
224 }
225
226 default:
227 /* Everything else is an unknown non-boolean value and needs to
228 * have all sources resolved.
229 */
230 instr->pass_flags = (instr->pass_flags & ~ELK_NIR_BOOLEAN_MASK) |
231 ELK_NIR_NON_BOOLEAN;
232 nir_foreach_src(instr, src_mark_needs_resolve, NULL);
233 continue;
234 }
235 }
236
237 nir_if *following_if = nir_block_get_following_if(block);
238 if (following_if)
239 src_mark_needs_resolve(&following_if->condition, NULL);
240
241 return true;
242 }
243
244 static void
analyze_boolean_resolves_impl(nir_function_impl * impl)245 analyze_boolean_resolves_impl(nir_function_impl *impl)
246 {
247 nir_foreach_block(block, impl) {
248 analyze_boolean_resolves_block(block);
249 }
250 }
251
252 void
elk_nir_analyze_boolean_resolves(nir_shader * shader)253 elk_nir_analyze_boolean_resolves(nir_shader *shader)
254 {
255 nir_foreach_function_impl(impl, shader) {
256 analyze_boolean_resolves_impl(impl);
257 }
258 }
259