<div dir="ltr"><div>I am not really that much familiar with nir, so I apologize if what I write below is wrong.</div><div><br></div>What if we had something like:<div><br></div><div>a = 0;</div><div>if (x) {</div><div>  discard_if(y);</div><div>  a = 1;</div><div>}</div><div><br></div><div>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:</div><div><br></div><div>a = 0;</div><div>discard_if(x && y);</div><div><br></div><div>Which is incorrect.</div><div><br></div><div>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.</div><div><br></div><div>Regards,</div><div>Gustaw</div></div><div class="gmail_extra"><br><div class="gmail_quote">2016-11-03 4:28 GMT+01:00 Dave Airlie <span dir="ltr"><<a href="mailto:airlied@gmail.com" target="_blank">airlied@gmail.com</a>></span>:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">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_<wbr>conditional_discard.c | 124 +++++++++++++++++++++++++<br>
 3 files changed, 127 insertions(+)<br>
 create mode 100644 src/compiler/nir/nir_opt_<wbr>conditional_discard.c<br>
<br>
diff --git a/src/compiler/Makefile.<wbr>sources b/src/compiler/Makefile.<wbr>sources<br>
index 669c499..b710cbd 100644<br>
--- a/src/compiler/Makefile.<wbr>sources<br>
+++ b/src/compiler/Makefile.<wbr>sources<br>
@@ -228,6 +228,7 @@ NIR_FILES = \<br>
        nir/nir_metadata.c \<br>
        nir/nir_move_vec_src_uses_to_<wbr>dest.c \<br>
        nir/nir_normalize_cubemap_<wbr>coords.c \<br>
+       nir/nir_opt_conditional_<wbr>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(<wbr>nir_shader *shader);<br>
+<br>
 void nir_sweep(nir_shader *shader);<br>
<br>
 nir_intrinsic_op nir_intrinsic_from_system_<wbr>value(gl_system_value val);<br>
diff --git a/src/compiler/nir/nir_opt_<wbr>conditional_discard.c b/src/compiler/nir/nir_opt_<wbr>conditional_discard.c<br>
new file mode 100644<br>
index 0000000..2259a14<br>
--- /dev/null<br>
+++ b/src/compiler/nir/nir_opt_<wbr>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_<wbr>block(nir_block *block, void *mem_ctx)<br>
+{<br>
+   nir_builder bld;<br>
+<br>
+   if (nir_cf_node_is_first(&block-><wbr>cf_node))<br>
+      return false;<br>
+<br>
+   nir_cf_node *prev_node = nir_cf_node_prev(&block->cf_<wbr>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_<wbr>stmt);<br>
+   nir_block *else_block = nir_if_first_else_block(if_<wbr>stmt);<br>
+<br>
+   /* check there is only one else block and it is empty */<br>
+   if (nir_if_last_else_block(if_<wbr>stmt) != else_block)<br>
+      return false;<br>
+   if (!exec_list_is_empty(&else_<wbr>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_<wbr>stmt) != then_block)<br>
+      return false;<br>
+   if (exec_list_is_empty(&then_<wbr>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_<wbr>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_<wbr>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(<wbr>mem_ctx, nir_intrinsic_discard_if);<br>
+   nir_src_copy(&discard_if->src[<wbr>0], &cond, discard_if);<br>
+<br>
+   nir_instr_insert_before_cf(<wbr>prev_node, &discard_if->instr);<br>
+   nir_instr_remove(&intrin-><wbr>instr);<br>
+   nir_cf_node_remove(&if_stmt-><wbr>cf_node);<br>
+<br>
+   return true;<br>
+}<br>
+<br>
+bool<br>
+nir_opt_conditional_discard(<wbr>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_<wbr>block(block, mem_ctx);<br>
+         }<br>
+      }<br>
+   }<br>
+   return progress;<br>
+}<br>
<span class="HOEnZb"><font color="#888888">--<br>
2.7.4<br>
<br>
______________________________<wbr>_________________<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" rel="noreferrer" target="_blank">https://lists.freedesktop.org/<wbr>mailman/listinfo/mesa-dev</a><br>
</font></span></blockquote></div><br></div>