<div dir="ltr">On 1 November 2013 17:31, Matt Turner <span dir="ltr"><<a href="mailto:mattst88@gmail.com" target="_blank">mattst88@gmail.com</a>></span> wrote:<br><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
total instructions in shared programs: 1649485 -> 1649157 (-0.02%)<br>
instructions in affected programs:     7823 -> 7495 (-4.19%)<br>
---<br>
 src/mesa/drivers/dri/i965/Makefile.sources         |  1 +<br>
 src/mesa/drivers/dri/i965/brw_fs.cpp               |  1 +<br>
 src/mesa/drivers/dri/i965/brw_fs.h                 |  1 +<br>
 .../dri/i965/brw_fs_peephole_predicated_break.cpp  | 96 ++++++++++++++++++++++<br>
 4 files changed, 99 insertions(+)<br>
 create mode 100644 src/mesa/drivers/dri/i965/brw_fs_peephole_predicated_break.cpp<br>
<br>
diff --git a/src/mesa/drivers/dri/i965/Makefile.sources b/src/mesa/drivers/dri/i965/Makefile.sources<br>
index 14b2f61..fcc6fdb 100644<br>
--- a/src/mesa/drivers/dri/i965/Makefile.sources<br>
+++ b/src/mesa/drivers/dri/i965/Makefile.sources<br>
@@ -59,6 +59,7 @@ i965_FILES = \<br>
        brw_fs_fp.cpp \<br>
        brw_fs_generator.cpp \<br>
        brw_fs_live_variables.cpp \<br>
+       brw_fs_peephole_predicated_break.cpp \<br>
        brw_fs_reg_allocate.cpp \<br>
        brw_fs_vector_splitting.cpp \<br>
        brw_fs_visitor.cpp \<br>
diff --git a/src/mesa/drivers/dri/i965/brw_fs.cpp b/src/mesa/drivers/dri/i965/brw_fs.cpp<br>
index 7064910..5c8443f 100644<br>
--- a/src/mesa/drivers/dri/i965/brw_fs.cpp<br>
+++ b/src/mesa/drivers/dri/i965/brw_fs.cpp<br>
@@ -3149,6 +3149,7 @@ fs_visitor::run()<br>
         progress = opt_algebraic() || progress;<br>
         progress = opt_cse() || progress;<br>
         progress = opt_copy_propagate() || progress;<br>
+         progress = opt_peephole_predicated_break() || progress;<br>
         progress = dead_code_eliminate() || progress;<br>
         progress = dead_code_eliminate_local() || progress;<br>
         progress = register_coalesce() || progress;<br>
diff --git a/src/mesa/drivers/dri/i965/brw_fs.h b/src/mesa/drivers/dri/i965/brw_fs.h<br>
index 43e4761..09e5174 100644<br>
--- a/src/mesa/drivers/dri/i965/brw_fs.h<br>
+++ b/src/mesa/drivers/dri/i965/brw_fs.h<br>
@@ -362,6 +362,7 @@ public:<br>
    bool try_emit_saturate(ir_expression *ir);<br>
    bool try_emit_mad(ir_expression *ir, int mul_arg);<br>
    void try_replace_with_sel();<br>
+   bool opt_peephole_predicated_break();<br>
    void emit_bool_to_cond_code(ir_rvalue *condition);<br>
    void emit_if_gen6(ir_if *ir);<br>
    void emit_unspill(fs_inst *inst, fs_reg reg, uint32_t spill_offset,<br>
diff --git a/src/mesa/drivers/dri/i965/brw_fs_peephole_predicated_break.cpp b/src/mesa/drivers/dri/i965/brw_fs_peephole_predicated_break.cpp<br>
new file mode 100644<br>
index 0000000..7845113<br>
--- /dev/null<br>
+++ b/src/mesa/drivers/dri/i965/brw_fs_peephole_predicated_break.cpp<br>
@@ -0,0 +1,96 @@<br>
+/*<br>
+ * Copyright © 2013 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 "brw_fs.h"<br>
+#include "brw_cfg.h"<br>
+<br>
+/** @file brw_fs_peephole_predicated_break.cpp<br>
+ *<br>
+ * Loops are often structured as<br>
+ *<br>
+ * loop:<br>
+ *    CMP.f0<br>
+ *    (+f0) IF<br>
+ *    BREAK<br>
+ *    ENDIF<br>
+ *    ...<br>
+ *    WHILE loop<br>
+ *<br>
+ * This peephole pass removes the IF and ENDIF instructions and predicates the<br>
+ * BREAK, dropping two instructions from the loop body.<br></blockquote><div><br></div><div>Instead of this special-purpose peephole optimization, I'm wondering if it would be more useful to create a more general optimization pass that just converts the entire IF block to predicated instructions, assuming certain conditions hold.  Those conditions would be something like:<br>
<br></div><div>- If any instruction but the last writes to the flag register, then don't do the optimization.<br></div><div>- If there's nested control flow, then don't do the optimization.<br></div><div>- If any instruction inside the IF block is incompatible with predication (are there any such instructions?  I don't know) then don't do the optimization.<br>
</div><div>- If the block is larger than a certain heuristically-determined size, then don't do the optimization.<br><br></div><div>Such an optimization could in principle even handle IF/ELSE blocks, by inverting the predicate in the ELSE section.<br>
<br></div><br><div>Having said all that, I think this optimization pass does what you intend, so with the exception of one comment I've noted below, the patch is:<br><br>Reviewed-by: Paul Berry <<a href="mailto:stereotype441@gmail.com">stereotype441@gmail.com</a>><br>
</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
+ */<br>
+<br>
+bool<br>
+fs_visitor::opt_peephole_predicated_break()<br>
+{<br>
+   bool progress = false;<br>
+<br>
+   cfg_t cfg(this);<br>
+<br>
+   for (int b = 0; b < cfg.num_blocks; b++) {<br>
+      bblock_t *block = cfg.blocks[b];<br>
+<br>
+      /* IF instructions, by definition, can only be found at the ends of<br>
+       * basic blocks.<br>
+       */<br>
+      fs_inst *if_inst = (fs_inst *) block->end;<br>
+      if (if_inst->opcode != BRW_OPCODE_IF)<br>
+         continue;<br>
+<br>
+      fs_inst *inst = (fs_inst *) if_inst->next;<br>
+      if (inst->opcode != BRW_OPCODE_BREAK && inst->opcode != BRW_OPCODE_CONTINUE)<br>
+         continue;<br>
+<br>
+      fs_inst *endif_inst = (fs_inst *) inst->next;<br>
+      if (endif_inst->opcode != BRW_OPCODE_ENDIF)<br>
+         continue;<br>
+<br>
+      /* For Sandybridge with IF with embedded comparison we need to emit an<br>
+       * instruction to set the flag register.<br>
+       */<br>
+      if (brw->gen == 6 && if_inst->conditional_mod) {<br>
+         fs_inst *cmp_inst = CMP(reg_null_d, if_inst->src[0], if_inst->src[1],<br>
+                                 if_inst->conditional_mod);<br>
+         if_inst->insert_before(cmp_inst);<br>
+         inst->predicate = BRW_PREDICATE_NORMAL;<br>
+      } else {<br>
+         inst->predicate = if_inst->predicate;<br>
+         inst->predicate_inverse = if_inst->predicate_inverse;<br>
+      }<br>
+<br>
+      if_inst->remove();<br>
+      endif_inst->remove();<br>
+<br>
+      /* By removing the ENDIF instruction we removed a basic block. Skip over<br>
+       * it for the next iteration.<br>
+       */<br>
+      b++;<br></blockquote><div><br></div><div>The correctness of this code depends on basic blocks being generated in order (something which could conceivably change in the future).  Just for the sake of safety, can we have an assertion or two here just to verify that the basic block being skipped is the same basic block that was removed?<br>
</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
+<br>
+      progress = true;<br>
+   }<br>
+<br>
+   if (progress)<br>
+      invalidate_live_intervals();<br>
+<br>
+   return progress;<br>
+}<br>
<span class="HOEnZb"><font color="#888888">--<br>
1.8.3.2<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" target="_blank">http://lists.freedesktop.org/mailman/listinfo/mesa-dev</a><br>
</font></span></blockquote></div><br></div></div>