<div dir="ltr"><br><div class="gmail_extra"><br><div class="gmail_quote">On Wed, Aug 10, 2016 at 11:14 AM, Ian Romanick <span dir="ltr"><<a href="mailto:idr@freedesktop.org" target="_blank">idr@freedesktop.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="HOEnZb"><div class="h5">On 08/09/2016 07:30 PM, Kenneth Graunke wrote:<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>
> Signed-off-by: Kenneth Graunke <<a href="mailto:kenneth@whitecape.org">kenneth@whitecape.org</a>><br>
> ---<br>
> src/compiler/Makefile.sources | 1 +<br>
> src/compiler/nir/nir.h | 2 +<br>
> src/compiler/nir/nir_opt_move_<wbr>comparisons.c | 173 ++++++++++++++++++++++++++++<br>
> 3 files changed, 176 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 0ff9b23..008a101 100644<br>
> --- a/src/compiler/Makefile.<wbr>sources<br>
> +++ b/src/compiler/Makefile.<wbr>sources<br>
> @@ -226,6 +226,7 @@ NIR_FILES = \<br>
> nir/nir_opt_gcm.c \<br>
> nir/nir_opt_global_to_local.c \<br>
> nir/nir_opt_peephole_select.c \<br>
> + nir/nir_opt_move_comparisons.c \<br>
> nir/nir_opt_remove_phis.c \<br>
> nir/nir_opt_undef.c \<br>
> nir/nir_phi_builder.c \<br>
> diff --git a/src/compiler/nir/nir.h b/src/compiler/nir/nir.h<br>
> index 9ce5be2..79511a7 100644<br>
> --- a/src/compiler/nir/nir.h<br>
> +++ b/src/compiler/nir/nir.h<br>
> @@ -2574,6 +2574,8 @@ bool nir_opt_dead_cf(nir_shader *shader);<br>
><br>
> void nir_opt_gcm(nir_shader *shader);<br>
><br>
> +bool nir_opt_move_comparisons(nir_<wbr>shader *shader);<br>
> +<br>
> bool nir_opt_peephole_select(nir_<wbr>shader *shader);<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..74927c9<br>
> --- /dev/null<br>
> +++ b/src/compiler/nir/nir_opt_<wbr>move_comparisons.c<br>
> @@ -0,0 +1,173 @@<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>
> + return true;<br>
> + default:<br>
> + return false;<br>
> + }<br>
> +}<br>
> +<br>
> +static bool<br>
> +move_comparison_source(nir_<wbr>src *src, nir_block *block, struct exec_node *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>
> +<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, 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>
> +};<br>
> +<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->node))<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>
</div></div>I assume the NIR validation passes double-check that for us? :)<br></blockquote><div><br></div><div>Oh, yes! Not validating that would be baaaaad.<br></div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<br>
Reviewed-by: Ian Romanick <<a href="mailto:ian.d.romanick@intel.com">ian.d.romanick@intel.com</a>> </blockquote><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div class="HOEnZb"><div class="h5"><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->node);<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>
> + 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>
><br>
<br>
</div></div><div class="HOEnZb"><div class="h5">______________________________<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>
</div></div></blockquote></div><br></div></div>