2 * Copyright © 2015 Intel Corporation
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:
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
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
24 * Jason Ekstrand <jason@jlekstrand.net>
30 * This file implements an analysis pass that determines when we have to do
31 * a boolean resolve on Gen <= 5. Instructions that need a boolean resolve
32 * will have the booleans portion of the instr->pass_flags field set to
33 * BRW_NIR_BOOLEAN_NEEDS_RESOLVE.
37 /** Returns the resolve status for the given source
39 * If the source has a parent instruction then the resolve status is the
40 * status of the parent instruction. If the source does not have a parent
41 * instruction then we don't know so we return NON_BOOLEAN.
44 get_resolve_status_for_src(nir_src
*src
)
47 nir_instr
*src_instr
= src
->ssa
->parent_instr
;
48 uint8_t resolve_status
= src_instr
->pass_flags
& BRW_NIR_BOOLEAN_MASK
;
50 /* If the source instruction needs resolve, then from the perspective
51 * of the user, it's a true boolean.
53 if (resolve_status
== BRW_NIR_BOOLEAN_NEEDS_RESOLVE
)
54 resolve_status
= BRW_NIR_BOOLEAN_NO_RESOLVE
;
55 return resolve_status
;
57 return BRW_NIR_NON_BOOLEAN
;
61 /** Marks the given source as needing a resolve
63 * If the given source corresponds to an unresolved boolean it marks it as
64 * needing a resolve. Otherwise, we leave it alone.
67 src_mark_needs_resolve(nir_src
*src
, void *void_state
)
70 nir_instr
*src_instr
= src
->ssa
->parent_instr
;
71 uint8_t resolve_status
= src_instr
->pass_flags
& BRW_NIR_BOOLEAN_MASK
;
73 /* If the source instruction is unresolved, then mark it as needing
76 if (resolve_status
== BRW_NIR_BOOLEAN_UNRESOLVED
) {
77 src_instr
->pass_flags
&= ~BRW_NIR_BOOLEAN_MASK
;
78 src_instr
->pass_flags
|= BRW_NIR_BOOLEAN_NEEDS_RESOLVE
;
87 analyze_boolean_resolves_block(nir_block
*block
, void *void_state
)
89 nir_foreach_instr(block
, instr
) {
90 switch (instr
->type
) {
91 case nir_instr_type_alu
: {
92 /* For ALU instructions, the resolve status is handled in a
95 * 1) Look at the instruction type and sources and determine if it
96 * can be left unresolved.
98 * 2) Look at the destination and see if we have to resolve
99 * anyway. (This is the case if this instruction is not the
100 * only instruction writing to a given register.)
102 * 3) If the instruction has a resolve status other than
103 * BOOL_UNRESOLVED or BOOL_NEEDS_RESOLVE then we walk through
104 * the sources and ensure that they are also resolved. This
105 * ensures that we don't end up with any stray unresolved
106 * booleans going into ADDs or something like that.
109 uint8_t resolve_status
;
110 nir_alu_instr
*alu
= nir_instr_as_alu(instr
);
124 /* This instruction will turn into a CMP when we actually emit
125 * so the result will have to be resolved before it can be used.
127 resolve_status
= BRW_NIR_BOOLEAN_UNRESOLVED
;
129 /* Even though the destination is allowed to be left unresolved,
130 * the sources are treated as regular integers or floats so
131 * they need to be resolved.
133 nir_foreach_src(instr
, src_mark_needs_resolve
, NULL
);
138 /* This is a single-source instruction. Just copy the resolve
139 * status from the source.
141 resolve_status
= get_resolve_status_for_src(&alu
->src
[0].src
);
147 uint8_t src0_status
= get_resolve_status_for_src(&alu
->src
[0].src
);
148 uint8_t src1_status
= get_resolve_status_for_src(&alu
->src
[1].src
);
150 if (src0_status
== src1_status
) {
151 resolve_status
= src0_status
;
152 } else if (src0_status
== BRW_NIR_NON_BOOLEAN
||
153 src1_status
== BRW_NIR_NON_BOOLEAN
) {
154 /* If one of the sources is a non-boolean then the whole
155 * thing is a non-boolean.
157 resolve_status
= BRW_NIR_NON_BOOLEAN
;
159 /* At this point one of them is a true boolean and one is a
160 * boolean that needs a resolve. We could either resolve the
161 * unresolved source or we could resolve here. If we resolve
162 * the unresolved source then we get two resolves for the price
163 * of one. Just set this one to BOOLEAN_NO_RESOLVE and we'll
164 * let the code below force a resolve on the unresolved source.
166 resolve_status
= BRW_NIR_BOOLEAN_NO_RESOLVE
;
172 resolve_status
= BRW_NIR_NON_BOOLEAN
;
175 /* If the destination is SSA, go ahead allow unresolved booleans.
176 * If the destination register doesn't have a well-defined parent_instr
177 * we need to resolve immediately.
179 if (!alu
->dest
.dest
.is_ssa
&&
180 resolve_status
== BRW_NIR_BOOLEAN_UNRESOLVED
) {
181 resolve_status
= BRW_NIR_BOOLEAN_NEEDS_RESOLVE
;
184 instr
->pass_flags
= (instr
->pass_flags
& ~BRW_NIR_BOOLEAN_MASK
) |
187 /* Finally, resolve sources if it's needed */
188 switch (resolve_status
) {
189 case BRW_NIR_BOOLEAN_NEEDS_RESOLVE
:
190 case BRW_NIR_BOOLEAN_UNRESOLVED
:
191 /* This instruction is either unresolved or we're doing the
192 * resolve here; leave the sources alone.
196 case BRW_NIR_BOOLEAN_NO_RESOLVE
:
197 case BRW_NIR_NON_BOOLEAN
:
198 nir_foreach_src(instr
, src_mark_needs_resolve
, NULL
);
202 unreachable("Invalid boolean flag");
208 case nir_instr_type_load_const
: {
209 nir_load_const_instr
*load
= nir_instr_as_load_const(instr
);
211 /* For load_const instructions, it's a boolean exactly when it holds
212 * one of the values NIR_TRUE or NIR_FALSE.
214 * Since load_const instructions don't have any sources, we don't
215 * have to worry about resolving them.
217 instr
->pass_flags
&= ~BRW_NIR_BOOLEAN_MASK
;
218 if (load
->value
.u
[0] == NIR_TRUE
|| load
->value
.u
[0] == NIR_FALSE
) {
219 instr
->pass_flags
|= BRW_NIR_BOOLEAN_NO_RESOLVE
;
221 instr
->pass_flags
|= BRW_NIR_NON_BOOLEAN
;
227 /* Everything else is an unknown non-boolean value and needs to
228 * have all sources resolved.
230 instr
->pass_flags
= (instr
->pass_flags
& ~BRW_NIR_BOOLEAN_MASK
) |
232 nir_foreach_src(instr
, src_mark_needs_resolve
, NULL
);
237 nir_if
*following_if
= nir_block_get_following_if(block
);
239 src_mark_needs_resolve(&following_if
->condition
, NULL
);
245 analyze_boolean_resolves_impl(nir_function_impl
*impl
)
247 nir_foreach_block(impl
, analyze_boolean_resolves_block
, NULL
);
251 brw_nir_analyze_boolean_resolves(nir_shader
*shader
)
253 nir_foreach_overload(shader
, overload
)
255 analyze_boolean_resolves_impl(overload
->impl
);