<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On Wed, Jan 11, 2017 at 12:49 PM, Jason Ekstrand <span dir="ltr"><<a href="mailto:jason@jlekstrand.net" target="_blank">jason@jlekstrand.net</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div><div>I just sent three squash-in patches that I think improve this pass a bit and make things more clear.  With those (or a good reason why not), <br></div></div></div></blockquote><div><br></div><div>FYI, they've already been run through Jenkins. :-)<br></div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div><div></div></div>Reviewed-by: Jason Ekstrand <<a href="mailto:jason@jlekstrand.net" target="_blank">jason@jlekstrand.net</a>><br></div><div class="HOEnZb"><div class="h5"><div class="gmail_extra"><br><div class="gmail_quote">On Tue, Jan 10, 2017 at 1:41 AM, Timothy Arceri <span dir="ltr"><<a href="mailto:timothy.arceri@collabora.com" target="_blank">timothy.arceri@collabora.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">From: Kenneth Graunke <<a href="mailto:kenneth@whitecape.org" target="_blank">kenneth@whitecape.org</a>><br>
<br>
This tries to move comparisons (a common source of boolean values)<br>
closer to their first use.  For GPUs which use condition codes,<br>
this can eliminate a lot of temporary booleans and comparisons<br>
which reload the condition code register based on a boolean.<br>
<br>
V2: (Timothy Arceri)<br>
 - fix move comparision for phis so we dont end up with:<br>
<br>
    vec1 32 ssa_227 = phi block_34: ssa_1, block_38: ssa_240<br>
    vec1 32 ssa_235 = feq ssa_227, ssa_1<br>
    vec1 32 ssa_230 = phi block_34: ssa_221, block_38: ssa_235<br>
<br>
 - add nir_op_i2b/nir_op_f2b to the list of comparisons.<br>
<br>
V3: (Timothy Arceri)<br>
 - tidy up suggested by Jason.<br>
 - add inot/fnot to move comparison list<br>
<br>
Signed-off-by: Kenneth Graunke <<a href="mailto:kenneth@whitecape.org" target="_blank">kenneth@whitecape.org</a>><br>
Reviewed-by: Ian Romanick <<a href="mailto:ian.d.romanick@intel.com" target="_blank">ian.d.romanick@intel.com</a>> [v1]<br>
---<br>
 src/compiler/Makefile.<wbr>sources               |   1 +<br>
 src/compiler/nir/nir.h                      |   2 +<br>
 src/compiler/nir/nir_opt_<wbr>move_comparisons.c | 179 ++++++++++++++++++++++++++++<br>
 3 files changed, 182 insertions(+)<br>
 create mode 100644 src/compiler/nir/nir_opt_move_<wbr>comparisons.c<br>
<br>
diff --git a/src/compiler/Makefile.source<wbr>s b/src/compiler/Makefile.source<wbr>s<br>
index 52f6e54..6da854e 100644<br>
--- a/src/compiler/Makefile.source<wbr>s<br>
+++ b/src/compiler/Makefile.source<wbr>s<br>
@@ -245,6 +245,7 @@ NIR_FILES = \<br>
        nir/nir_opt_global_to_local.c \<br>
        nir/nir_opt_if.c \<br>
        nir/nir_opt_loop_unroll.c \<br>
+       nir/nir_opt_move_comparisons.<wbr>c \<br>
        nir/nir_opt_peephole_select.c \<br>
        nir/nir_opt_remove_phis.c \<br>
        nir/nir_opt_trivial_continues.<wbr>c \<br>
diff --git a/src/compiler/nir/nir.h b/src/compiler/nir/nir.h<br>
index c9226e9..2bff0b5 100644<br>
--- a/src/compiler/nir/nir.h<br>
+++ b/src/compiler/nir/nir.h<br>
@@ -2589,6 +2589,8 @@ bool nir_opt_if(nir_shader *shader);<br>
<br>
 bool nir_opt_loop_unroll(nir_shader *shader, nir_variable_mode indirect_mask);<br>
<br>
+bool nir_opt_move_comparisons(nir_s<wbr>hader *shader);<br>
+<br>
 bool nir_opt_peephole_select(nir_sh<wbr>ader *shader, unsigned limit);<br>
<br>
 bool nir_opt_remove_phis(nir_shader *shader);<br>
diff --git a/src/compiler/nir/nir_opt_mov<wbr>e_comparisons.c b/src/compiler/nir/nir_opt_mov<wbr>e_comparisons.c<br>
new file mode 100644<br>
index 0000000..651b937<br>
--- /dev/null<br>
+++ b/src/compiler/nir/nir_opt_mov<wbr>e_comparisons.c<br>
@@ -0,0 +1,179 @@<br>
+/*<br>
+ * Copyright © 2016 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>
+#include "nir.h"<br>
+<br>
+/**<br>
+ * \file nir_opt_move_comparisons.c<br>
+ *<br>
+ * This pass moves ALU comparison operations just before their first use.<br>
+ *<br>
+ * It only moves instructions within a single basic block; cross-block<br>
+ * movement is left to global code motion.<br>
+ *<br>
+ * Many GPUs generate condition codes for comparisons, and use predication<br>
+ * for conditional selects and control flow.  In a sequence such as:<br>
+ *<br>
+ *     vec1 32 ssa_1 = flt a b<br>
+ *     <some other operations><br>
+ *     vec1 32 ssa_2 = bcsel ssa_1 c d<br>
+ *<br>
+ * the backend would likely do the comparison, producing condition codes,<br>
+ * then save those to a boolean value.  The intervening operations might<br>
+ * trash the condition codes.  Then, in order to do the bcsel, it would<br>
+ * need to re-populate the condition code register based on the boolean.<br>
+ *<br>
+ * By moving the comparison just before the bcsel, the condition codes could<br>
+ * be used directly.  This eliminates the need to reload them from the boolean<br>
+ * (generally eliminating an instruction).  It may also eliminate the need to<br>
+ * create a boolean value altogether (unless it's used elsewhere), which could<br>
+ * lower register pressure.<br>
+ */<br>
+<br>
+static bool<br>
+is_comparison(nir_op op)<br>
+{<br>
+   switch (op) {<br>
+   case nir_op_flt:<br>
+   case nir_op_fge:<br>
+   case nir_op_feq:<br>
+   case nir_op_fne:<br>
+   case nir_op_ilt:<br>
+   case nir_op_ult:<br>
+   case nir_op_ige:<br>
+   case nir_op_uge:<br>
+   case nir_op_ieq:<br>
+   case nir_op_ine:<br>
+   case nir_op_i2b:<br>
+   case nir_op_f2b:<br>
+   case nir_op_inot:<br>
+   case nir_op_fnot:<br>
+      return true;<br>
+   default:<br>
+      return false;<br>
+   }<br>
+}<br>
+<br>
+static bool<br>
+move_comparison_source(nir_sr<wbr>c *src, nir_block *block, nir_instr *before)<br>
+{<br>
+   if (src->is_ssa && src->ssa->parent_instr->block == block &&<br>
+       src->ssa->parent_instr->type == nir_instr_type_alu &&<br>
+       is_comparison(nir_instr_as_al<wbr>u(src->ssa->parent_instr)->op) &&<br>
+       (!before || before->type != nir_instr_type_phi)) {<br>
+<br>
+      struct exec_node *src_node = &src->ssa->parent_instr->node;<br>
+      exec_node_remove(src_node);<br>
+<br>
+      if (before)<br>
+         exec_node_insert_node_before(<wbr>&before->node, src_node);<br>
+      else<br>
+         exec_list_push_tail(&block->i<wbr>nstr_list, src_node);<br>
+<br>
+      return true;<br>
+   }<br>
+<br>
+   return false;<br>
+}<br>
+<br>
+/* nir_foreach_src callback boilerplate */<br>
+struct nomc_tuple<br>
+{<br>
+   nir_instr *instr;<br>
+   bool progress;<br>
+};<br>
+<br>
+static bool<br>
+move_comparison_source_cb(nir<wbr>_src *src, void *data)<br>
+{<br>
+   struct nomc_tuple *tuple = data;<br>
+<br>
+   if (move_comparison_source(src, tuple->instr->block, tuple->instr))<br>
+      tuple->progress = true;<br>
+<br>
+   return true; /* nir_foreach_src should keep going */<br>
+}<br>
+<br>
+static bool<br>
+move_comparisons(nir_block *block)<br>
+{<br>
+   bool progress = false;<br>
+<br>
+   /* We use a simple approach: walk instructions backwards.<br>
+    *<br>
+    * If the instruction's source is a comparison from the same block,<br>
+    * simply move it here.  This may break SSA if it's used earlier in<br>
+    * the block as well.  However, as we walk backwards, we'll find the<br>
+    * earlier use and move it again, further up.  It eventually ends up<br>
+    * dominating all uses again, restoring SSA form.<br>
+    *<br>
+    * Before walking instructions, we consider the if-condition at the<br>
+    * end of the block, if one exists.  It's effectively a use at the<br>
+    * bottom of the block.<br>
+    */<br>
+   nir_if *iff = nir_block_get_following_if(blo<wbr>ck);<br>
+   if (iff) {<br>
+      progress |= move_comparison_source(&iff->c<wbr>ondition, block, NULL);<br>
+   }<br>
+<br>
+   nir_foreach_instr_reverse(ins<wbr>tr, block) {<br>
+      if (instr->type == nir_instr_type_alu) {<br>
+         /* Walk ALU instruction sources backwards so that bcsel's boolean<br>
+          * condition is processed last.<br>
+          */<br>
+         nir_alu_instr *alu = nir_instr_as_alu(instr);<br>
+         for (int i = nir_op_infos[alu->op].num_inpu<wbr>ts - 1; i >= 0; i--) {<br>
+            progress |= move_comparison_source(&alu->s<wbr>rc[i].src,<br>
+                                               block, instr);<br>
+         }<br>
+      } else {<br>
+         struct nomc_tuple tuple = { instr, false };<br>
+         nir_foreach_src(instr, move_comparison_source_cb, &tuple);<br>
+         progress |= tuple.progress;<br>
+      }<br>
+   }<br>
+<br>
+   return progress;<br>
+}<br>
+<br>
+bool<br>
+nir_opt_move_comparisons(nir_<wbr>shader *shader)<br>
+{<br>
+   bool progress = false;<br>
+<br>
+   nir_foreach_function(func, shader) {<br>
+      if (!func->impl)<br>
+         continue;<br>
+<br>
+      nir_foreach_block(block, func->impl) {<br>
+         if (move_comparisons(block)) {<br>
+            nir_metadata_preserve(func->im<wbr>pl, nir_metadata_block_index |<br>
+                                              nir_metadata_dominance |<br>
+                                              nir_metadata_live_ssa_defs);<br>
+            progress = true;<br>
+         }<br>
+      }<br>
+   }<br>
+<br>
+   return progress;<br>
+}<br>
<span class="m_3256316756759565609HOEnZb"><font color="#888888">--<br>
2.9.3<br>
<br>
______________________________<wbr>_________________<br>
mesa-dev mailing list<br>
<a href="mailto:mesa-dev@lists.freedesktop.org" target="_blank">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>
</div></div></blockquote></div><br></div></div>