<p dir="ltr"><br>
On Mar 22, 2015 8:48 PM, "Connor Abbott" <<a href="mailto:cwabbott0@gmail.com">cwabbott0@gmail.com</a>> wrote:<br>
><br>
> On Fri, Mar 20, 2015 at 5:23 PM, Jason Ekstrand <<a href="mailto:jason@jlekstrand.net">jason@jlekstrand.net</a>> wrote:<br>
> > v2: Fix the spelling of analyze and re-arrange code for better readability<br>
> > as per Connor's comments.<br>
> > v3: Make the naming of things more consistent and add a pile of comments<br>
> > ---<br>
> > src/mesa/drivers/dri/i965/Makefile.sources | 2 +<br>
> > src/mesa/drivers/dri/i965/brw_nir.h | 78 ++++++<br>
> > .../dri/i965/brw_nir_analyze_boolean_resolves.c | 275 +++++++++++++++++++++<br>
> > 3 files changed, 355 insertions(+)<br>
> > create mode 100644 src/mesa/drivers/dri/i965/brw_nir.h<br>
> > create mode 100644 src/mesa/drivers/dri/i965/brw_nir_analyze_boolean_resolves.c<br>
> ><br>
> > diff --git a/src/mesa/drivers/dri/i965/Makefile.sources b/src/mesa/drivers/dri/i965/Makefile.sources<br>
> > index c69441b..3a3df70 100644<br>
> > --- a/src/mesa/drivers/dri/i965/Makefile.sources<br>
> > +++ b/src/mesa/drivers/dri/i965/Makefile.sources<br>
> > @@ -73,6 +73,8 @@ i965_FILES = \<br>
> > brw_meta_util.h \<br>
> > brw_misc_state.c \<br>
> > brw_multisample_state.h \<br>
> > + brw_nir.h \<br>
> > + brw_nir_analyze_boolean_resolves.c \<br>
> > brw_object_purgeable.c \<br>
> > brw_packed_float.c \<br>
> > brw_performance_monitor.c \<br>
> > diff --git a/src/mesa/drivers/dri/i965/brw_nir.h b/src/mesa/drivers/dri/i965/brw_nir.h<br>
> > new file mode 100644<br>
> > index 0000000..27782a3<br>
> > --- /dev/null<br>
> > +++ b/src/mesa/drivers/dri/i965/brw_nir.h<br>
> > @@ -0,0 +1,78 @@<br>
> > +/*<br>
> > + * Copyright © 2015 Intel Corporation<br>
> > + *<br>
> > + * Permission is hereby granted, free of charge, to any person obtaining a<br>
> > + * copy of this software and associated documentation files (the "Software"),<br>
> > + * to deal in the Software without restriction, including without limitation<br>
> > + * the rights to use, copy, modify, merge, publish, distribute, sublicense,<br>
> > + * and/or sell copies of the Software, and to permit persons to whom the<br>
> > + * Software is furnished to do so, subject to the following conditions:<br>
> > + *<br>
> > + * The above copyright notice and this permission notice (including the next<br>
> > + * paragraph) shall be included in all copies or substantial portions of the<br>
> > + * Software.<br>
> > + *<br>
> > + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR<br>
> > + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,<br>
> > + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL<br>
> > + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER<br>
> > + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING<br>
> > + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS<br>
> > + * IN THE SOFTWARE.<br>
> > + */<br>
> > +<br>
> > +#pragma once<br>
> > +<br>
> > +#include "glsl/nir/nir.h"<br>
> > +<br>
> > +#ifdef __cplusplus<br>
> > +extern "C" {<br>
> > +#endif<br>
> > +<br>
> > +/* Flags set in the instr->pass_flags field by i965 analysis passes */<br>
> > +enum {<br>
> > + BRW_NIR_NON_BOOLEAN = 0x0,<br>
> > +<br>
> > + /* Indicates that the given instruction's destination is a boolean<br>
> > + * value but that it needs to be resolved before it can be used.<br>
> > + * On Gen <= 5, CMP instructions return a 32-bit value where the bottom<br>
> > + * bit represents the actual true/false value of the compare and the top<br>
> > + * 31 bits are undefined. In order to use this value, we have to do a<br>
> > + * "resolve" operation by replacing the value of the CMP with -(x & 1)<br>
> > + * to sign-extend the bottom bit to 0/~0.<br>
> > + */<br>
> > + BRW_NIR_BOOLEAN_NEEDS_RESOLVE = 0x1,<br>
> > +<br>
> > + /* Indicates that the given instruction's destination is a boolean<br>
> > + * value that has intentionally been left unresolved. Not all boolean<br>
> > + * values need to be resolved immediately. For instance, if we have<br>
> > + *<br>
> > + * CMP r1 r2 r3<br>
> > + * CMP r4 r5 r6<br>
> > + * AND r7 r1 r4<br>
> > + *<br>
> > + * We don't have to resolve the result of the two CMP instructions<br>
> > + * immediately because the AND still does an AND of the bottom bits.<br>
> > + * Instead, we can save ourselves instructions by delaying the resolve<br>
> > + * until after the AND. The result of the two CMP instructions is left<br>
> > + * as BRW_NIR_BOOLEAN_UNRESOLVED.<br>
> > + */<br>
> > + BRW_NIR_BOOLEAN_UNRESOLVED = 0x2,<br>
> > +<br>
> > + /* Indicates a that the given instruction's destination is a boolean<br>
> > + * value that does not need a resolve. For instance, if you AND two<br>
> > + * values that are BRW_NIR_BOOLEAN_NEEDS_RESOLVE then we know that both<br>
> > + * values will be 0/~0 before we get them and the result of the AND is<br>
> > + * also guaranteed to be 0/~0 and does not need a resolve.<br>
> > + */<br>
> > + BRW_NIR_BOOLEAN_NO_RESOLVE = 0x3,<br>
> > +<br>
> > + /* A mask to mask the boolean status values off of instr->pass_flags */<br>
> > + BRW_NIR_BOOLEAN_MASK = 0x3,<br>
> > +};<br>
> > +<br>
> > +void brw_nir_analyze_boolean_resolves(nir_shader *nir);<br>
> > +<br>
> > +#ifdef __cplusplus<br>
> > +}<br>
> > +#endif<br>
> > diff --git a/src/mesa/drivers/dri/i965/brw_nir_analyze_boolean_resolves.c b/src/mesa/drivers/dri/i965/brw_nir_analyze_boolean_resolves.c<br>
> > new file mode 100644<br>
> > index 0000000..e893a65<br>
> > --- /dev/null<br>
> > +++ b/src/mesa/drivers/dri/i965/brw_nir_analyze_boolean_resolves.c<br>
> > @@ -0,0 +1,275 @@<br>
> > +/*<br>
> > + * Copyright © 2015 Intel Corporation<br>
> > + *<br>
> > + * Permission is hereby granted, free of charge, to any person obtaining a<br>
> > + * copy of this software and associated documentation files (the "Software"),<br>
> > + * to deal in the Software without restriction, including without limitation<br>
> > + * the rights to use, copy, modify, merge, publish, distribute, sublicense,<br>
> > + * and/or sell copies of the Software, and to permit persons to whom the<br>
> > + * Software is furnished to do so, subject to the following conditions:<br>
> > + *<br>
> > + * The above copyright notice and this permission notice (including the next<br>
> > + * paragraph) shall be included in all copies or substantial portions of the<br>
> > + * Software.<br>
> > + *<br>
> > + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR<br>
> > + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,<br>
> > + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL<br>
> > + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER<br>
> > + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING<br>
> > + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS<br>
> > + * IN THE SOFTWARE.<br>
> > + *<br>
> > + * Authors:<br>
> > + * Jason Ekstrand <<a href="mailto:jason@jlekstrand.net">jason@jlekstrand.net</a>><br>
> > + */<br>
> > +<br>
> > +#include "brw_nir.h"<br>
> > +<br>
> > +/*<br>
> > + * This file implements an analysis pass that determines when we have to do<br>
> > + * a boolean resolve on Gen <= 5. Instructions that need a boolean resolve<br>
> > + * will have the booleans portion of the instr->pass_flags field set to<br>
> > + * BRW_NIR_BOOLEAN_NEEDS_RESOLVE.<br>
> > + */<br>
> > +<br>
> > +<br>
> > +/** Returns the resolve status for the given source<br>
> > + *<br>
> > + * If the source has a parent instruction then the resolve status is the<br>
> > + * status of the parent instruction. If the source does not have a parent<br>
> > + * instruction then we don't know so we return NON_BOOLEAN.<br>
> > + */<br>
> > +static uint8_t<br>
> > +get_resolve_status_for_src(nir_src *src)<br>
> > +{<br>
> > + nir_instr *src_instr;<br>
> > + if (src->is_ssa) {<br>
> > + src_instr = src->ssa->parent_instr;<br>
> > + } else {<br>
> > + src_instr = src->reg.reg->parent_instr;<br>
> > + }<br>
> > +<br>
> > + if (src_instr) {<br>
> > + uint8_t resolve_status = src_instr->pass_flags & BRW_NIR_BOOLEAN_MASK;<br>
> > +<br>
> > + /* If the source instruction needs resolve, then from the perspective<br>
> > + * of the user, it's a true boolean.<br>
> > + */<br>
> > + if (resolve_status == BRW_NIR_BOOLEAN_NEEDS_RESOLVE)<br>
> > + resolve_status = BRW_NIR_BOOLEAN_NO_RESOLVE;<br>
> > + return resolve_status;<br>
> > + } else {<br>
> > + return BRW_NIR_NON_BOOLEAN;<br>
> > + }<br>
> > +}<br>
> > +<br>
> > +/** Marks the given source as needing a resolve<br>
> > + *<br>
> > + * If the given source corresponds to an unresolved boolean it marks it as<br>
> > + * needing a resolve. Otherwise, we leave it alone.<br>
> > + */<br>
> > +static bool<br>
> > +src_mark_needs_resolve(nir_src *src, void *void_state)<br>
> > +{<br>
> > + nir_instr *src_instr;<br>
> > + if (src->is_ssa) {<br>
> > + src_instr = src->ssa->parent_instr;<br>
> > + } else {<br>
> > + src_instr = src->reg.reg->parent_instr;<br>
> > + }<br>
> > +<br>
> > + if (src_instr) {<br>
> > + uint8_t resolve_status = src_instr->pass_flags & BRW_NIR_BOOLEAN_MASK;<br>
> > +<br>
> > + /* If the source instruction is unresolved, then mark it as needing<br>
> > + * to be resolved.<br>
> > + */<br>
> > + if (resolve_status == BRW_NIR_BOOLEAN_UNRESOLVED) {<br>
> > + src_instr->pass_flags &= ~BRW_NIR_BOOLEAN_MASK;<br>
> > + src_instr->pass_flags |= BRW_NIR_BOOLEAN_NEEDS_RESOLVE;<br>
> > + }<br>
> > +<br>
> > + }<br>
> > +<br>
> > + return true;<br>
> > +}<br>
> > +<br>
> > +static bool<br>
> > +analyze_boolean_resolves_block(nir_block *block, void *void_state)<br>
> > +{<br>
> > + nir_foreach_instr(block, instr) {<br>
> > + switch (instr->type) {<br>
> > + case nir_instr_type_alu: {<br>
> > + /* For ALU instructions, the resolve status is handled in a<br>
> > + * three-step process.<br>
> > + *<br>
> > + * 1) Look at the instruction type and sources and determine if it<br>
> > + * can be left unresolved.<br>
> > + *<br>
> > + * 2) Look at the destination and see if we have to resolve<br>
> > + * anyway. (This is the case if this instruction is not the<br>
> > + * only instruction writing to a given register.)<br>
> > + *<br>
> > + * 3) If the instruction has a resolve status other than<br>
> > + * BOOL_UNRESOLVED or BOOL_NEEDS_RESOLVE then we walk through<br>
> > + * the sources and ensure that they are also resolved. This<br>
> > + * ensures that we don't end up with any stray unresolved<br>
> > + * booleans going into ADDs or something like that.<br>
> > + */<br>
> > +<br>
> > + uint8_t resolve_status;<br>
> > + nir_alu_instr *alu = nir_instr_as_alu(instr);<br>
> > + switch (alu->op) {<br>
> > + case nir_op_flt:<br>
> > + case nir_op_ilt:<br>
> > + case nir_op_ult:<br>
> > + case nir_op_fge:<br>
> > + case nir_op_ige:<br>
> > + case nir_op_uge:<br>
> > + case nir_op_feq:<br>
> > + case nir_op_ieq:<br>
> > + case nir_op_fne:<br>
> > + case nir_op_ine:<br>
> > + case nir_op_f2b:<br>
> > + case nir_op_i2b:<br>
> > + /* This instruction will turn into a CMP when we actually emit<br>
> > + * so the result will have to be resolved before it can be used.<br>
> > + */<br>
> > + resolve_status = BRW_NIR_BOOLEAN_UNRESOLVED;<br>
> > +<br>
> > + /* Even though the destination is allowed to be left unresolved,<br>
> > + * the sources are treated as regular integers or floats so<br>
> > + * they need to be resolved.<br>
> > + */<br>
> > + nir_foreach_src(instr, src_mark_needs_resolve, NULL);<br>
> > + break;<br>
> > +<br>
> > + case nir_op_imov:<br>
> > + case nir_op_inot:<br>
> > + if (alu->dest.write_mask == 1) {<br>
> > + /* This is a single-source instruction. Just copy the<br>
> > + * resolve status from the source.<br>
> > + */<br>
> > + resolve_status = get_resolve_status_for_src(&alu->src[0].src);<br>
> > + } else {<br>
> > + /* Vector versions will be assumed to be non-boolean. */<br>
> > + resolve_status = BRW_NIR_NON_BOOLEAN;<br>
><br>
> Even though all non-vector things are currently lowered via<br>
> lower_alu_to_scalar so it won't matter until we add NIR -> vec4<br>
> support, I think we should delete this if-statement and always go with<br>
> the first case -- you're just being too conservative, and it's less<br>
> code :)<br>
><br>
> > + }<br>
> > + break;<br>
> > +<br>
> > + case nir_op_iand:<br>
> > + case nir_op_ior:<br>
> > + case nir_op_ixor: {<br>
> > + assert(alu->dest.write_mask == 1);<br>
><br>
> Similarly, I think vector and/or/etc should "just work" with this<br>
> code. I know this is fixing a case that we don't hit yet, but given it<br>
> only involves deleting code I think it still makes sense to do it now.<br>
> Otherwise, this patch is</p>
<p dir="ltr">Sure, I can do that. Most of that was added as I was trying to figure out how these would interact. No reason not to just let it work on vectors. I'll get rid of the pointless code.<br>
--Jason</p>
<p dir="ltr">> Reviewed-by: Connor Abbott <<a href="mailto:cwabbott0@gmail.com">cwabbott0@gmail.com</a>><br>
><br>
> > +<br>
> > + uint8_t src0_status = get_resolve_status_for_src(&alu->src[0].src);<br>
> > + uint8_t src1_status = get_resolve_status_for_src(&alu->src[1].src);<br>
> > +<br>
> > + if (src0_status == src1_status) {<br>
> > + resolve_status = src0_status;<br>
> > + } else if (src0_status == BRW_NIR_NON_BOOLEAN ||<br>
> > + src1_status == BRW_NIR_NON_BOOLEAN) {<br>
> > + /* If one of the sources is a non-boolean then the whole<br>
> > + * thing is a non-boolean.<br>
> > + */<br>
> > + resolve_status = BRW_NIR_NON_BOOLEAN;<br>
> > + } else {<br>
> > + /* At this point one of them is a true boolean and one is a<br>
> > + * boolean that needs a resolve. We could either resolve the<br>
> > + * unresolved source or we could resolve here. If we resolve<br>
> > + * the unresolved source then we get two resolves for the price<br>
> > + * of one. Just set this one to BOOLEAN_NO_RESOLVE and we'll<br>
> > + * let the code below force a resolve on the unresolved source.<br>
> > + */<br>
> > + resolve_status = BRW_NIR_BOOLEAN_NO_RESOLVE;<br>
> > + }<br>
> > + break;<br>
> > + }<br>
> > +<br>
> > + default:<br>
> > + resolve_status = BRW_NIR_NON_BOOLEAN;<br>
> > + }<br>
> > +<br>
> > + /* If the destination is SSA-like, go ahead allow unresolved booleans.<br>
> > + * If the destination register doesn't have a well-defined parent_instr<br>
> > + * we need to resolve immediately.<br>
> > + */<br>
> > + if (alu->dest.dest.reg.reg->parent_instr == NULL &&<br>
> > + resolve_status == BRW_NIR_BOOLEAN_UNRESOLVED) {<br>
> > + resolve_status = BRW_NIR_BOOLEAN_NEEDS_RESOLVE;<br>
> > + }<br>
> > +<br>
> > + instr->pass_flags = (instr->pass_flags & ~BRW_NIR_BOOLEAN_MASK) |<br>
> > + resolve_status;<br>
> > +<br>
> > + /* Finally, resolve sources if it's needed */<br>
> > + switch (resolve_status) {<br>
> > + case BRW_NIR_BOOLEAN_NEEDS_RESOLVE:<br>
> > + case BRW_NIR_BOOLEAN_UNRESOLVED:<br>
> > + /* This instruction is either unresolved or we're doing the<br>
> > + * resolve here; leave the sources alone.<br>
> > + */<br>
> > + break;<br>
> > +<br>
> > + case BRW_NIR_BOOLEAN_NO_RESOLVE:<br>
> > + case BRW_NIR_NON_BOOLEAN:<br>
> > + nir_foreach_src(instr, src_mark_needs_resolve, NULL);<br>
> > + break;<br>
> > +<br>
> > + default:<br>
> > + unreachable("Invalid boolean flag");<br>
> > + }<br>
> > +<br>
> > + break;<br>
> > + }<br>
> > +<br>
> > + case nir_instr_type_load_const: {<br>
> > + nir_load_const_instr *load = nir_instr_as_load_const(instr);<br>
> > +<br>
> > + /* For load_const instructions, it's a boolean exactly when it holds<br>
> > + * one of the values NIR_TRUE or NIR_FALSE.<br>
> > + *<br>
> > + * Since load_const instructions don't have any sources, we don't<br>
> > + * have to worry about resolving them.<br>
> > + */<br>
> > + instr->pass_flags &= ~BRW_NIR_BOOLEAN_MASK;<br>
> > + if (load->value.u[0] == NIR_TRUE || load->value.u[0] == NIR_FALSE) {<br>
> > + instr->pass_flags |= BRW_NIR_BOOLEAN_NO_RESOLVE;<br>
> > + } else {<br>
> > + instr->pass_flags |= BRW_NIR_NON_BOOLEAN;<br>
> > + }<br>
> > + continue;<br>
> > + }<br>
> > +<br>
> > + default:<br>
> > + /* Everything else is an unknown non-boolean value and needs to<br>
> > + * have all sources resolved.<br>
> > + */<br>
> > + instr->pass_flags = (instr->pass_flags & ~BRW_NIR_BOOLEAN_MASK) |<br>
> > + BRW_NIR_NON_BOOLEAN;<br>
> > + nir_foreach_src(instr, src_mark_needs_resolve, NULL);<br>
> > + continue;<br>
> > + }<br>
> > + }<br>
> > +<br>
> > + nir_if *following_if = nir_block_get_following_if(block);<br>
> > + if (following_if)<br>
> > + src_mark_needs_resolve(&following_if->condition, NULL);<br>
> > +<br>
> > + return true;<br>
> > +}<br>
> > +<br>
> > +static void<br>
> > +analyze_boolean_resolves_impl(nir_function_impl *impl)<br>
> > +{<br>
> > + nir_foreach_block(impl, analyze_boolean_resolves_block, NULL);<br>
> > +}<br>
> > +<br>
> > +void<br>
> > +brw_nir_analyze_boolean_resolves(nir_shader *shader)<br>
> > +{<br>
> > + nir_foreach_overload(shader, overload)<br>
> > + if (overload->impl)<br>
> > + analyze_boolean_resolves_impl(overload->impl);<br>
> > +}<br>
> > --<br>
> > 2.3.3<br>
> ><br>
> > _______________________________________________<br>
> > mesa-dev mailing list<br>
> > <a href="mailto:mesa-dev@lists.freedesktop.org">mesa-dev@lists.freedesktop.org</a><br>
> > <a href="http://lists.freedesktop.org/mailman/listinfo/mesa-dev">http://lists.freedesktop.org/mailman/listinfo/mesa-dev</a><br>
</p>