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