<p dir="ltr"></p>
<p dir="ltr">On 3 Nov. 2016 18:21, "Gustaw Smolarczyk" <<a href="mailto:wielkiegie@gmail.com">wielkiegie@gmail.com</a>> wrote:<br>
><br>
> I am not really that much familiar with nir, so I apologize if what I write below is wrong.<br>
><br>
> What if we had something like:<br>
><br>
> a = 0;<br>
> if (x) {<br>
>   discard_if(y);<br>
>   a = 1;<br>
> }<br>
><br>
> Then if (x && !y), we must not discard and both at the same time set a to 1. Your transformation would probably change it to:<br>
><br>
> a = 0;<br>
> discard_if(x && y);<br>
><br>
> Which is incorrect.<br>
><br>
> I think you have to ensure (if the intrinsic is discard_if) that discard_if is the only instruction inside the then block. Or just remove the intrinsic from the then block and leave the rest of the if alone.<br>
></p>
<p dir="ltr">Yes you are right I need to add a check that there is only one instruction in the then block.</p>
<p dir="ltr">Thanks,<br>
Dave.<br>
> Regards,<br>
> Gustaw<br>
><br>
> 2016-11-03 4:28 GMT+01:00 Dave Airlie <<a href="mailto:airlied@gmail.com">airlied@gmail.com</a>>:<br>
>><br>
>> From: Dave Airlie <<a href="mailto:airlied@redhat.com">airlied@redhat.com</a>><br>
>><br>
>> This is ported from GLSL and converts<br>
>><br>
>> if (cond)<br>
>>         discard;<br>
>><br>
>> into<br>
>> discard_if(cond);<br>
>><br>
>> This removes a block, but also is needed by radv<br>
>> to workaround a bug in the LLVM backend.<br>
>><br>
>> v2: handle if (a) discard_if(b) (nha)<br>
>> cleanup and drop pointless loop (Matt)<br>
>> make sure there are no dependent phis (Eric)<br>
>><br>
>> Signed-off-by: Dave Airlie <<a href="mailto:airlied@redhat.com">airlied@redhat.com</a>><br>
>> ---<br>
>>  src/compiler/Makefile.sources                  |   1 +<br>
>>  src/compiler/nir/nir.h                         |   2 +<br>
>>  src/compiler/nir/nir_opt_conditional_discard.c | 124 +++++++++++++++++++++++++<br>
>>  3 files changed, 127 insertions(+)<br>
>>  create mode 100644 src/compiler/nir/nir_opt_conditional_discard.c<br>
>><br>
>> diff --git a/src/compiler/Makefile.sources b/src/compiler/Makefile.sources<br>
>> index 669c499..b710cbd 100644<br>
>> --- a/src/compiler/Makefile.sources<br>
>> +++ b/src/compiler/Makefile.sources<br>
>> @@ -228,6 +228,7 @@ NIR_FILES = \<br>
>>         nir/nir_metadata.c \<br>
>>         nir/nir_move_vec_src_uses_to_dest.c \<br>
>>         nir/nir_normalize_cubemap_coords.c \<br>
>> +       nir/nir_opt_conditional_discard.c \<br>
>>         nir/nir_opt_constant_folding.c \<br>
>>         nir/nir_opt_copy_propagate.c \<br>
>>         nir/nir_opt_cse.c \<br>
>> diff --git a/src/compiler/nir/nir.h b/src/compiler/nir/nir.h<br>
>> index 9264763..2a77139 100644<br>
>> --- a/src/compiler/nir/nir.h<br>
>> +++ b/src/compiler/nir/nir.h<br>
>> @@ -2531,6 +2531,8 @@ bool nir_opt_remove_phis(nir_shader *shader);<br>
>><br>
>>  bool nir_opt_undef(nir_shader *shader);<br>
>><br>
>> +bool nir_opt_conditional_discard(nir_shader *shader);<br>
>> +<br>
>>  void nir_sweep(nir_shader *shader);<br>
>><br>
>>  nir_intrinsic_op nir_intrinsic_from_system_value(gl_system_value val);<br>
>> diff --git a/src/compiler/nir/nir_opt_conditional_discard.c b/src/compiler/nir/nir_opt_conditional_discard.c<br>
>> new file mode 100644<br>
>> index 0000000..2259a14<br>
>> --- /dev/null<br>
>> +++ b/src/compiler/nir/nir_opt_conditional_discard.c<br>
>> @@ -0,0 +1,124 @@<br>
>> +/*<br>
>> + * Copyright © 2016 Red Hat<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>
>> +#include "nir.h"<br>
>> +#include "nir_builder.h"<br>
>> +<br>
>> +/** @file nir_opt_conditional_discard.c<br>
>> + *<br>
>> + * Handles optimization of lowering if (cond) discard to discard_if(cond).<br>
>> + */<br>
>> +<br>
>> +static bool<br>
>> +nir_opt_conditional_discard_block(nir_block *block, void *mem_ctx)<br>
>> +{<br>
>> +   nir_builder bld;<br>
>> +<br>
>> +   if (nir_cf_node_is_first(&block->cf_node))<br>
>> +      return false;<br>
>> +<br>
>> +   nir_cf_node *prev_node = nir_cf_node_prev(&block->cf_node);<br>
>> +   if (prev_node->type != nir_cf_node_if)<br>
>> +      return false;<br>
>> +<br>
>> +   nir_if *if_stmt = nir_cf_node_as_if(prev_node);<br>
>> +   nir_block *then_block = nir_if_first_then_block(if_stmt);<br>
>> +   nir_block *else_block = nir_if_first_else_block(if_stmt);<br>
>> +<br>
>> +   /* check there is only one else block and it is empty */<br>
>> +   if (nir_if_last_else_block(if_stmt) != else_block)<br>
>> +      return false;<br>
>> +   if (!exec_list_is_empty(&else_block->instr_list))<br>
>> +      return false;<br>
>> +<br>
>> +   /* check there is only one then block and it has instructions in it */<br>
>> +   if (nir_if_last_then_block(if_stmt) != then_block)<br>
>> +      return false;<br>
>> +   if (exec_list_is_empty(&then_block->instr_list))<br>
>> +      return false;<br>
>> +<br>
>> +   /*<br>
>> +    * make sure no subsequent phi nodes point at this if.<br>
>> +    */<br>
>> +   nir_block *after = nir_cf_node_as_block(nir_cf_node_next(&if_stmt->cf_node));<br>
>> +   nir_foreach_instr_safe(instr, after) {<br>
>> +      if (instr->type != nir_instr_type_phi)<br>
>> +         break;<br>
>> +      nir_phi_instr *phi = nir_instr_as_phi(instr);<br>
>> +<br>
>> +      nir_foreach_phi_src(phi_src, phi) {<br>
>> +         if (phi_src->pred == then_block ||<br>
>> +             phi_src->pred == else_block)<br>
>> +            return false;<br>
>> +      }<br>
>> +   }<br>
>> +<br>
>> +   /* Get the first instruction in the then block and confirm it is<br>
>> +    * a discard or a discard_if<br>
>> +    */<br>
>> +   nir_instr *instr = nir_block_first_instr(then_block);<br>
>> +   if (instr->type != nir_instr_type_intrinsic)<br>
>> +      return false;<br>
>> +<br>
>> +   nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);<br>
>> +   if (intrin->intrinsic != nir_intrinsic_discard &&<br>
>> +       intrin->intrinsic != nir_intrinsic_discard_if)<br>
>> +      return false;<br>
>> +<br>
>> +   nir_src cond;<br>
>> +<br>
>> +   nir_builder_init(&bld, mem_ctx);<br>
>> +<br>
>> +   if (intrin->intrinsic == nir_intrinsic_discard)<br>
>> +      cond = if_stmt->condition;<br>
>> +   else<br>
>> +      cond = nir_src_for_ssa(nir_iand(&bld,<br>
>> +                                      nir_ssa_for_src(&bld, if_stmt->condition, 1),<br>
>> +                                    nir_ssa_for_src(&bld, intrin->src[0], 1)));<br>
>> +<br>
>> +   nir_intrinsic_instr *discard_if =<br>
>> +      nir_intrinsic_instr_create(mem_ctx, nir_intrinsic_discard_if);<br>
>> +   nir_src_copy(&discard_if->src[0], &cond, discard_if);<br>
>> +<br>
>> +   nir_instr_insert_before_cf(prev_node, &discard_if->instr);<br>
>> +   nir_instr_remove(&intrin->instr);<br>
>> +   nir_cf_node_remove(&if_stmt->cf_node);<br>
>> +<br>
>> +   return true;<br>
>> +}<br>
>> +<br>
>> +bool<br>
>> +nir_opt_conditional_discard(nir_shader *shader)<br>
>> +{<br>
>> +   bool progress = false;<br>
>> +<br>
>> +   nir_foreach_function(function, shader) {<br>
>> +      if (function->impl) {<br>
>> +         void *mem_ctx = ralloc_parent(function->impl);<br>
>> +         nir_foreach_block_safe(block, function->impl) {<br>
>> +            progress |= nir_opt_conditional_discard_block(block, mem_ctx);<br>
>> +         }<br>
>> +      }<br>
>> +   }<br>
>> +   return progress;<br>
>> +}<br>
>> --<br>
>> 2.7.4<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="https://lists.freedesktop.org/mailman/listinfo/mesa-dev">https://lists.freedesktop.org/mailman/listinfo/mesa-dev</a><br>
><br>
></p>