<div dir="ltr">On 31 October 2013 18:57, 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:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
fs_visitor::try_replace_with_sel optimizes only if statements whose<br>
"then" and "else" bodies contain a single MOV instruction. It also<br>
could not handle constant arguments, since they cause an extra MOV<br>
immediate to be generated (since we haven't run constant propagation,<br>
there are more than the single MOV).<br>
<br>
This peephole fixes both of these and operates as a normal optimization<br>
pass.<br>
<br>
fs_visitor::try_replace_with_sel is still arguably necessary, since it<br>
runs before pull constant loads are lowered.<br>
<br>
total instructions in shared programs: 1417596 -> 1405551 (-0.85%)<br>
instructions in affected programs: 168590 -> 156545 (-7.14%)<br>
GAINED: 10<br>
LOST: 4<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>
src/mesa/drivers/dri/i965/brw_fs_sel_peephole.cpp | 217 ++++++++++++++++++++++<br>
4 files changed, 220 insertions(+)<br>
create mode 100644 src/mesa/drivers/dri/i965/brw_fs_sel_peephole.cpp<br>
<br>
diff --git a/src/mesa/drivers/dri/i965/Makefile.sources b/src/mesa/drivers/dri/i965/Makefile.sources<br>
index 06ac843..79ed3dd 100644<br>
--- a/src/mesa/drivers/dri/i965/Makefile.sources<br>
+++ b/src/mesa/drivers/dri/i965/Makefile.sources<br>
@@ -60,6 +60,7 @@ i965_FILES = \<br>
brw_fs_fp.cpp \<br>
brw_fs_generator.cpp \<br>
brw_fs_live_variables.cpp \<br>
+ brw_fs_sel_peephole.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 df17684..345dc19 100644<br>
--- a/src/mesa/drivers/dri/i965/brw_fs.cpp<br>
+++ b/src/mesa/drivers/dri/i965/brw_fs.cpp<br>
@@ -3107,6 +3107,7 @@ fs_visitor::run()<br>
progress = opt_algebraic() || progress;<br>
progress = opt_cse() || progress;<br>
progress = opt_copy_propagate() || progress;<br>
+ progress = opt_peephole_sel() || progress;<br>
progress = dead_code_eliminate() || progress;<br>
progress = dead_code_eliminate_local() || progress;<br>
progress = dead_control_flow_eliminate(this) || progress;<br>
diff --git a/src/mesa/drivers/dri/i965/brw_fs.h b/src/mesa/drivers/dri/i965/brw_fs.h<br>
index 849e49f..a592dda 100644<br>
--- a/src/mesa/drivers/dri/i965/brw_fs.h<br>
+++ b/src/mesa/drivers/dri/i965/brw_fs.h<br>
@@ -363,6 +363,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_sel();<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_sel_peephole.cpp b/src/mesa/drivers/dri/i965/brw_fs_sel_peephole.cpp<br>
new file mode 100644<br>
index 0000000..9626751<br>
--- /dev/null<br>
+++ b/src/mesa/drivers/dri/i965/brw_fs_sel_peephole.cpp<br>
@@ -0,0 +1,217 @@<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_sel_peephole.cpp<br>
+ *<br>
+ * This file contains the opt_peephole_sel() optimization pass that replaces<br>
+ * MOV instructions to the same destination in the "then" and "else" bodies of<br>
+ * an if statement with SEL instructions.<br>
+ */<br>
+<br>
+#define MAX_MOVS 8 /**< The maximum number of MOVs to attempt to match. */<br></blockquote><div><br></div><div>In the last round of review, you gave me some nice clarification on why this was chosen to be 8. Would you mind documenting that in the comment?<br>
<br></div><div>Other than that, the patch is:<br><br>Reviewed-by: Paul Berry <<a href="mailto:stereotype441@gmail.com">stereotype441@gmail.com</a>><br></div><div><br></div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
+<br>
+/**<br>
+ * Scans forwards from an IF counting consecutive MOV instructions in the<br>
+ * "then" and "else" blocks of the if statement.<br>
+ *<br>
+ * A pointer to the fs_inst* for IF is passed as the <if_inst> argument. The<br>
+ * function stores pointers to the MOV instructions in the <then_mov> and<br>
+ * <else_mov> arrays.<br>
+ *<br>
+ * \return the minimum number of MOVs found in the two branches or zero if<br>
+ * an error occurred.<br>
+ *<br>
+ * E.g.:<br>
+ * IF ...<br>
+ * then_mov[0] = MOV g4, ...<br>
+ * then_mov[1] = MOV g5, ...<br>
+ * then_mov[2] = MOV g6, ...<br>
+ * ELSE ...<br>
+ * else_mov[0] = MOV g4, ...<br>
+ * else_mov[1] = MOV g5, ...<br>
+ * else_mov[2] = MOV g7, ...<br>
+ * ENDIF<br>
+ * returns 3. <br></blockquote><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
+ */<br>
+static int<br>
+count_movs_from_if(fs_inst *then_mov[MAX_MOVS], fs_inst *else_mov[MAX_MOVS],<br>
+ fs_inst *if_inst, fs_inst *else_inst)<br>
+{<br>
+ fs_inst *m = if_inst;<br>
+<br>
+ assert(m->opcode == BRW_OPCODE_IF);<br>
+ m = (fs_inst *) m->next;<br>
+<br>
+ int then_movs = 0;<br>
+ while (then_movs < MAX_MOVS && m->opcode == BRW_OPCODE_MOV) {<br>
+ then_mov[then_movs] = m;<br>
+ m = (fs_inst *) m->next;<br>
+ then_movs++;<br>
+ }<br>
+<br>
+ m = (fs_inst *) else_inst->next;<br>
+<br>
+ int else_movs = 0;<br>
+ while (else_movs < MAX_MOVS && m->opcode == BRW_OPCODE_MOV) {<br>
+ else_mov[else_movs] = m;<br>
+ m = (fs_inst *) m->next;<br>
+ else_movs++;<br>
+ }<br>
+<br>
+ return MIN2(then_movs, else_movs);<br>
+}<br>
+<br>
+/**<br>
+ * Try to replace IF/MOV+/ELSE/MOV+/ENDIF with SEL.<br>
+ *<br>
+ * Many GLSL shaders contain the following pattern:<br>
+ *<br>
+ * x = condition ? foo : bar<br>
+ *<br>
+ * or<br>
+ *<br>
+ * if (...) a.xyzw = foo.xyzw;<br>
+ * else a.xyzw = bar.xyzw;<br>
+ *<br>
+ * The compiler emits an ir_if tree for this, since each subexpression might be<br>
+ * a complex tree that could have side-effects or short-circuit logic.<br>
+ *<br>
+ * However, the common case is to simply select one of two constants or<br>
+ * variable values---which is exactly what SEL is for. In this case, the<br>
+ * assembly looks like:<br>
+ *<br>
+ * (+f0) IF<br>
+ * MOV dst src0<br>
+ * ...<br>
+ * ELSE<br>
+ * MOV dst src1<br>
+ * ...<br>
+ * ENDIF<br>
+ *<br>
+ * where each pair of MOVs to a common destination and can be easily translated<br>
+ * into<br>
+ *<br>
+ * (+f0) SEL dst src0 src1<br>
+ *<br>
+ * If src0 is an immediate value, we promote it to a temporary GRF.<br>
+ */<br>
+bool<br>
+fs_visitor::opt_peephole_sel()<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>
+ if (!block->else_inst)<br>
+ continue;<br>
+<br>
+ fs_inst *else_inst = (fs_inst *) block->else_inst;<br>
+<br>
+ fs_inst *else_mov[MAX_MOVS] = { NULL };<br>
+ fs_inst *then_mov[MAX_MOVS] = { NULL };<br>
+ bool malformed_mov_found = false;<br>
+<br>
+ int movs = count_movs_from_if(then_mov, else_mov, if_inst, else_inst);<br>
+<br>
+ if (movs == 0)<br>
+ continue;<br>
+<br>
+ fs_inst *sel_inst[MAX_MOVS] = { NULL };<br>
+ fs_inst *mov_imm_inst[MAX_MOVS] = { NULL };<br>
+<br>
+ /* Generate SEL instructions for pairs of MOVs to a common destination. */<br>
+ for (int i = 0; i < movs; i++) {<br>
+ if (!then_mov[i] || !else_mov[i])<br>
+ break;<br>
+<br>
+ /* Check that the MOVs are the right form. */<br>
+ if (!then_mov[i]->dst.equals(else_mov[i]->dst) ||<br>
+ then_mov[i]->is_partial_write() ||<br>
+ else_mov[i]->is_partial_write()) {<br>
+ malformed_mov_found = true;<br>
+ break;<br>
+ }<br>
+<br>
+ /* Only the last source register can be a constant, so if the MOV in<br>
+ * the "then" clause uses a constant, we need to put it in a<br>
+ * temporary.<br>
+ */<br>
+ fs_reg src0(then_mov[i]->src[0]);<br>
+ if (src0.file == IMM) {<br>
+ src0 = fs_reg(this, glsl_type::float_type);<br>
+ src0.type = then_mov[i]->src[0].type;<br>
+ mov_imm_inst[i] = MOV(src0, then_mov[i]->src[0]);<br>
+ }<br>
+<br>
+ sel_inst[i] = SEL(then_mov[i]->dst, src0, else_mov[i]->src[0]);<br>
+<br>
+ if (brw->gen == 6 && if_inst->conditional_mod) {<br>
+ /* For Sandybridge with IF with embedded comparison */<br>
+ sel_inst[i]->predicate = BRW_PREDICATE_NORMAL;<br>
+ } else {<br>
+ /* Separate CMP and IF instructions */<br>
+ sel_inst[i]->predicate = if_inst->predicate;<br>
+ sel_inst[i]->predicate_inverse = if_inst->predicate_inverse;<br>
+ }<br>
+ }<br>
+<br>
+ if (malformed_mov_found)<br>
+ continue;<br>
+<br>
+ /* Emit a CMP if our IF used the embedded comparison */<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>
+ }<br>
+<br>
+ for (int i = 0; i < movs; i++) {<br>
+ if (mov_imm_inst[i])<br>
+ if_inst->insert_before(mov_imm_inst[i]);<br>
+ if_inst->insert_before(sel_inst[i]);<br>
+<br>
+ then_mov[i]->remove();<br>
+ else_mov[i]->remove();<br>
+ }<br>
+<br>
+ progress = true;<br>
+ }<br>
+<br>
+ if (progress)<br>
+ invalidate_live_intervals();<br>
+<br>
+ return progress;<br>
+}<br>
<span class=""><font color="#888888">--<br>
1.8.3.2<br>
<br>
</font></span></blockquote></div><br></div></div>