[Mesa-dev] [PATCH 4/9] nir: Introduce a nir_opt_move_comparisons() pass.

Jason Ekstrand jason at jlekstrand.net
Wed Jan 11 20:49:22 UTC 2017


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),

Reviewed-by: Jason Ekstrand <jason at jlekstrand.net>

On Tue, Jan 10, 2017 at 1:41 AM, Timothy Arceri <
timothy.arceri at collabora.com> wrote:

> From: Kenneth Graunke <kenneth at whitecape.org>
>
> This tries to move comparisons (a common source of boolean values)
> closer to their first use.  For GPUs which use condition codes,
> this can eliminate a lot of temporary booleans and comparisons
> which reload the condition code register based on a boolean.
>
> V2: (Timothy Arceri)
>  - fix move comparision for phis so we dont end up with:
>
>     vec1 32 ssa_227 = phi block_34: ssa_1, block_38: ssa_240
>     vec1 32 ssa_235 = feq ssa_227, ssa_1
>     vec1 32 ssa_230 = phi block_34: ssa_221, block_38: ssa_235
>
>  - add nir_op_i2b/nir_op_f2b to the list of comparisons.
>
> V3: (Timothy Arceri)
>  - tidy up suggested by Jason.
>  - add inot/fnot to move comparison list
>
> Signed-off-by: Kenneth Graunke <kenneth at whitecape.org>
> Reviewed-by: Ian Romanick <ian.d.romanick at intel.com> [v1]
> ---
>  src/compiler/Makefile.sources               |   1 +
>  src/compiler/nir/nir.h                      |   2 +
>  src/compiler/nir/nir_opt_move_comparisons.c | 179
> ++++++++++++++++++++++++++++
>  3 files changed, 182 insertions(+)
>  create mode 100644 src/compiler/nir/nir_opt_move_comparisons.c
>
> diff --git a/src/compiler/Makefile.sources b/src/compiler/Makefile.sources
> index 52f6e54..6da854e 100644
> --- a/src/compiler/Makefile.sources
> +++ b/src/compiler/Makefile.sources
> @@ -245,6 +245,7 @@ NIR_FILES = \
>         nir/nir_opt_global_to_local.c \
>         nir/nir_opt_if.c \
>         nir/nir_opt_loop_unroll.c \
> +       nir/nir_opt_move_comparisons.c \
>         nir/nir_opt_peephole_select.c \
>         nir/nir_opt_remove_phis.c \
>         nir/nir_opt_trivial_continues.c \
> diff --git a/src/compiler/nir/nir.h b/src/compiler/nir/nir.h
> index c9226e9..2bff0b5 100644
> --- a/src/compiler/nir/nir.h
> +++ b/src/compiler/nir/nir.h
> @@ -2589,6 +2589,8 @@ bool nir_opt_if(nir_shader *shader);
>
>  bool nir_opt_loop_unroll(nir_shader *shader, nir_variable_mode
> indirect_mask);
>
> +bool nir_opt_move_comparisons(nir_shader *shader);
> +
>  bool nir_opt_peephole_select(nir_shader *shader, unsigned limit);
>
>  bool nir_opt_remove_phis(nir_shader *shader);
> diff --git a/src/compiler/nir/nir_opt_move_comparisons.c
> b/src/compiler/nir/nir_opt_move_comparisons.c
> new file mode 100644
> index 0000000..651b937
> --- /dev/null
> +++ b/src/compiler/nir/nir_opt_move_comparisons.c
> @@ -0,0 +1,179 @@
> +/*
> + * Copyright © 2016 Intel Corporation
> + *
> + * Permission is hereby granted, free of charge, to any person obtaining a
> + * copy of this software and associated documentation files (the
> "Software"),
> + * to deal in the Software without restriction, including without
> limitation
> + * the rights to use, copy, modify, merge, publish, distribute,
> sublicense,
> + * and/or sell copies of the Software, and to permit persons to whom the
> + * Software is furnished to do so, subject to the following conditions:
> + *
> + * The above copyright notice and this permission notice (including the
> next
> + * paragraph) shall be included in all copies or substantial portions of
> the
> + * Software.
> + *
> + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
> EXPRESS OR
> + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
> MERCHANTABILITY,
> + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT
> SHALL
> + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
> OTHER
> + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
> + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
> DEALINGS
> + * IN THE SOFTWARE.
> + */
> +
> +#include "nir.h"
> +
> +/**
> + * \file nir_opt_move_comparisons.c
> + *
> + * This pass moves ALU comparison operations just before their first use.
> + *
> + * It only moves instructions within a single basic block; cross-block
> + * movement is left to global code motion.
> + *
> + * Many GPUs generate condition codes for comparisons, and use predication
> + * for conditional selects and control flow.  In a sequence such as:
> + *
> + *     vec1 32 ssa_1 = flt a b
> + *     <some other operations>
> + *     vec1 32 ssa_2 = bcsel ssa_1 c d
> + *
> + * the backend would likely do the comparison, producing condition codes,
> + * then save those to a boolean value.  The intervening operations might
> + * trash the condition codes.  Then, in order to do the bcsel, it would
> + * need to re-populate the condition code register based on the boolean.
> + *
> + * By moving the comparison just before the bcsel, the condition codes
> could
> + * be used directly.  This eliminates the need to reload them from the
> boolean
> + * (generally eliminating an instruction).  It may also eliminate the
> need to
> + * create a boolean value altogether (unless it's used elsewhere), which
> could
> + * lower register pressure.
> + */
> +
> +static bool
> +is_comparison(nir_op op)
> +{
> +   switch (op) {
> +   case nir_op_flt:
> +   case nir_op_fge:
> +   case nir_op_feq:
> +   case nir_op_fne:
> +   case nir_op_ilt:
> +   case nir_op_ult:
> +   case nir_op_ige:
> +   case nir_op_uge:
> +   case nir_op_ieq:
> +   case nir_op_ine:
> +   case nir_op_i2b:
> +   case nir_op_f2b:
> +   case nir_op_inot:
> +   case nir_op_fnot:
> +      return true;
> +   default:
> +      return false;
> +   }
> +}
> +
> +static bool
> +move_comparison_source(nir_src *src, nir_block *block, nir_instr *before)
> +{
> +   if (src->is_ssa && src->ssa->parent_instr->block == block &&
> +       src->ssa->parent_instr->type == nir_instr_type_alu &&
> +       is_comparison(nir_instr_as_alu(src->ssa->parent_instr)->op) &&
> +       (!before || before->type != nir_instr_type_phi)) {
> +
> +      struct exec_node *src_node = &src->ssa->parent_instr->node;
> +      exec_node_remove(src_node);
> +
> +      if (before)
> +         exec_node_insert_node_before(&before->node, src_node);
> +      else
> +         exec_list_push_tail(&block->instr_list, src_node);
> +
> +      return true;
> +   }
> +
> +   return false;
> +}
> +
> +/* nir_foreach_src callback boilerplate */
> +struct nomc_tuple
> +{
> +   nir_instr *instr;
> +   bool progress;
> +};
> +
> +static bool
> +move_comparison_source_cb(nir_src *src, void *data)
> +{
> +   struct nomc_tuple *tuple = data;
> +
> +   if (move_comparison_source(src, tuple->instr->block, tuple->instr))
> +      tuple->progress = true;
> +
> +   return true; /* nir_foreach_src should keep going */
> +}
> +
> +static bool
> +move_comparisons(nir_block *block)
> +{
> +   bool progress = false;
> +
> +   /* We use a simple approach: walk instructions backwards.
> +    *
> +    * If the instruction's source is a comparison from the same block,
> +    * simply move it here.  This may break SSA if it's used earlier in
> +    * the block as well.  However, as we walk backwards, we'll find the
> +    * earlier use and move it again, further up.  It eventually ends up
> +    * dominating all uses again, restoring SSA form.
> +    *
> +    * Before walking instructions, we consider the if-condition at the
> +    * end of the block, if one exists.  It's effectively a use at the
> +    * bottom of the block.
> +    */
> +   nir_if *iff = nir_block_get_following_if(block);
> +   if (iff) {
> +      progress |= move_comparison_source(&iff->condition, block, NULL);
> +   }
> +
> +   nir_foreach_instr_reverse(instr, block) {
> +      if (instr->type == nir_instr_type_alu) {
> +         /* Walk ALU instruction sources backwards so that bcsel's boolean
> +          * condition is processed last.
> +          */
> +         nir_alu_instr *alu = nir_instr_as_alu(instr);
> +         for (int i = nir_op_infos[alu->op].num_inputs - 1; i >= 0; i--)
> {
> +            progress |= move_comparison_source(&alu->src[i].src,
> +                                               block, instr);
> +         }
> +      } else {
> +         struct nomc_tuple tuple = { instr, false };
> +         nir_foreach_src(instr, move_comparison_source_cb, &tuple);
> +         progress |= tuple.progress;
> +      }
> +   }
> +
> +   return progress;
> +}
> +
> +bool
> +nir_opt_move_comparisons(nir_shader *shader)
> +{
> +   bool progress = false;
> +
> +   nir_foreach_function(func, shader) {
> +      if (!func->impl)
> +         continue;
> +
> +      nir_foreach_block(block, func->impl) {
> +         if (move_comparisons(block)) {
> +            nir_metadata_preserve(func->impl, nir_metadata_block_index |
> +                                              nir_metadata_dominance |
> +                                              nir_metadata_live_ssa_defs);
> +            progress = true;
> +         }
> +      }
> +   }
> +
> +   return progress;
> +}
> --
> 2.9.3
>
> _______________________________________________
> mesa-dev mailing list
> mesa-dev at lists.freedesktop.org
> https://lists.freedesktop.org/mailman/listinfo/mesa-dev
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.freedesktop.org/archives/mesa-dev/attachments/20170111/a65dfce4/attachment-0001.html>


More information about the mesa-dev mailing list