<div dir="auto"><div><div class="gmail_extra"><div class="gmail_quote" dir="auto">Looks functionally correct. I left a few simple comments.</div><div class="gmail_quote" dir="auto"><br></div><div class="gmail_quote">On Jan 7, 2017 3:59 AM, "Timothy Arceri" <<a href="mailto:timothy.arceri@collabora.com">timothy.arceri@collabora.com</a>> wrote:<br type="attribution"><blockquote class="quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">From: Kenneth Graunke <<a href="mailto:kenneth@whitecape.org">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>
Signed-off-by: Kenneth Graunke <<a href="mailto:kenneth@whitecape.org">kenneth@whitecape.org</a>><br>
Reviewed-by: Ian Romanick <<a href="mailto:ian.d.romanick@intel.com">ian.d.romanick@intel.com</a>> [v1]<br>
---<br>
src/compiler/Makefile.sources | 1 +<br>
src/compiler/nir/nir.h | 2 +<br>
src/compiler/nir/nir_opt_move_<wbr>comparisons.c | 176 ++++++++++++++++++++++++++++<br>
3 files changed, 179 insertions(+)<br>
create mode 100644 src/compiler/nir/nir_opt_move_<wbr>comparisons.c<br>
<br>
diff --git a/src/compiler/Makefile.<wbr>sources b/src/compiler/Makefile.<wbr>sources<br>
index 52f6e54..6da854e 100644<br>
--- a/src/compiler/Makefile.<wbr>sources<br>
+++ b/src/compiler/Makefile.<wbr>sources<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.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 d17924c..325d73b 100644<br>
--- a/src/compiler/nir/nir.h<br>
+++ b/src/compiler/nir/nir.h<br>
@@ -2563,6 +2563,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_<wbr>shader *shader);<br>
+<br>
bool nir_opt_peephole_select(nir_<wbr>shader *shader, unsigned limit);<br>
<br>
bool nir_opt_remove_phis(nir_shader *shader);<br>
diff --git a/src/compiler/nir/nir_opt_<wbr>move_comparisons.c b/src/compiler/nir/nir_opt_<wbr>move_comparisons.c<br>
new file mode 100644<br>
index 0000000..e6bee94<br>
--- /dev/null<br>
+++ b/src/compiler/nir/nir_opt_<wbr>move_comparisons.c<br>
@@ -0,0 +1,176 @@<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>
+ return true;<br>
+ default:<br>
+ return false;<br>
+ }<br>
+}<br>
+<br>
+static bool<br>
+move_comparison_source(nir_<wbr>src *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_<wbr>alu(src->ssa->parent_instr)-><wbr>op) &&<br>
+ (!before || before->type != nir_instr_type_phi)) {<br>
+<br>
+ struct exec_node *src_node = &src->ssa->parent_instr->node;</blockquote></div></div></div><div dir="auto"><br></div><div dir="auto">I think I would mildly prefer this to be "nir_instr *src_instr". It means a bit more typing but the thing you are really doing is adding/removing an instruction not a node.</div><div dir="auto"></div><div dir="auto"><br></div><div dir="auto"><div class="gmail_extra"><div class="gmail_quote"><blockquote class="quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
+ 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-><wbr>instr_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>
+};</blockquote></div></div></div><div dir="auto"><br></div><div dir="auto">nir_src has a parent_instr field so you can pull everything other then the progress boolean out of the SRC and you don't need this struct.</div><div dir="auto"><br></div><div dir="auto"></div><div dir="auto"><div class="gmail_extra"><div class="gmail_quote"><blockquote class="quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
+<br>
+static bool<br>
+move_comparison_source_cb(<wbr>nir_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(<wbr>block);<br>
+ if (iff) {<br>
+ progress |= move_comparison_source(&iff-><wbr>condition, block, NULL);<br>
+ }<br>
+<br>
+ nir_foreach_instr_reverse(<wbr>instr, 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_<wbr>inputs - 1; i >= 0; i--) {<br>
+ progress |= move_comparison_source(&alu-><wbr>src[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></blockquote></div></div></div><div dir="auto"><br></div><div dir="auto">I tend to prefer "if (!func->impl) continue;" myself but it doesn't really matter.</div><div dir="auto"><br></div><div dir="auto"><div class="gmail_extra"><div class="gmail_quote"><blockquote class="quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
+ nir_foreach_block(block, func->impl) {<br>
+ if (move_comparisons(block)) {<br>
+ nir_metadata_preserve(func-><wbr>impl, nir_metadata_block_index |<br>
+ nir_metadata_dominance |<br>
+ nir_metadata_live_ssa_defs);<br>
+ progress = true;<br>
+ }<br>
+ }<br>
+ }<br>
+ }<br>
+<br>
+ return progress;<br>
+}<br>
<font color="#888888">--<br>
2.9.3<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></blockquote></div><br></div></div></div>