[Mesa-dev] [PATCH 1/2] nir: add an optimization to remove useless phi nodes

Connor Abbott cwabbott0 at gmail.com
Mon Feb 2 22:59:43 PST 2015


On Tue, Feb 3, 2015 at 1:54 AM, Connor Abbott <cwabbott0 at gmail.com> wrote:
> This removes phi nodes whose sources all point to the same thing.
>
> Only compile tested.
>
> Signed-off-by: Connor Abbott <cwabbott0 at gmail.com>
> ---
>  src/glsl/Makefile.sources          |   1 +
>  src/glsl/nir/nir.h                 |   2 +
>  src/glsl/nir/nir_opt_remove_phis.c | 111 +++++++++++++++++++++++++++++++++++++
>  3 files changed, 114 insertions(+)
>  create mode 100644 src/glsl/nir/nir_opt_remove_phis.c
>
> diff --git a/src/glsl/Makefile.sources b/src/glsl/Makefile.sources
> index face22e..6d51c71 100644
> --- a/src/glsl/Makefile.sources
> +++ b/src/glsl/Makefile.sources
> @@ -44,6 +44,7 @@ NIR_FILES = \
>         nir/nir_opt_dce.c \
>         nir/nir_opt_global_to_local.c \
>         nir/nir_opt_peephole_select.c \
> +       nir/nir_opt_remove_phis.c \
>         nir/nir_print.c \
>         nir/nir_remove_dead_variables.c \
>         nir/nir_search.c \
> diff --git a/src/glsl/nir/nir.h b/src/glsl/nir/nir.h
> index 98d2689..d1860d4 100644
> --- a/src/glsl/nir/nir.h
> +++ b/src/glsl/nir/nir.h
> @@ -1559,6 +1559,8 @@ bool nir_opt_dce(nir_shader *shader);
>  bool nir_opt_peephole_select(nir_shader *shader);
>  bool nir_opt_peephole_ffma(nir_shader *shader);
>
> +bool nir_opt_remove_phis(nir_shader *shader);
> +
>  #ifdef __cplusplus
>  } /* extern "C" */
>  #endif
> diff --git a/src/glsl/nir/nir_opt_remove_phis.c b/src/glsl/nir/nir_opt_remove_phis.c
> new file mode 100644
> index 0000000..aec5dc01
> --- /dev/null
> +++ b/src/glsl/nir/nir_opt_remove_phis.c
> @@ -0,0 +1,111 @@
> +/*
> + * Copyright © 2015 Connor Abbott
> + *
> + * 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.
> + *
> + * Authors:
> + *    Connor Abbott (cwabbott0 at gmail.com)
> + *
> + */
> +
> +#include "nir.h"
> +
> +/*
> + * This is a pass for removing phi nodes that look like:
> + * a = phi(b, b, b, ...)
> + *
> + * Note that we can't ignore undef sources here, or else we may create a
> + * situation where the definition of b isn't dominated by its uses. We're
> + * allowed to do this since the definition of b must dominate all of the
> + * phi node's predecessors, which means it must dominate the phi node as well
> + * as all of the phi node's uses. In essence, the phi node acts as a copy
> + * instruction. b can't be another phi node in the same block, since the only
> + * time when phi nodes can source other phi nodes defined in the same block is
> + * at the loop header, and in that case one of the sources of the phi has to
> + * be from before the loop and that source can't be b.
> + */
> +
> +/* returns true if any progress was made */

Gah, this shouldn't be there. Fixed locally.

> +
> +static bool
> +remove_phis_block(nir_block *block, void *state)
> +{
> +   bool *progress = state;
> +
> +   void *mem_ctx = ralloc_parent(block);
> +
> +   nir_foreach_instr_safe(block, instr) {
> +      if (instr->type != nir_instr_type_phi)
> +         break;
> +
> +      nir_phi_instr *phi = nir_instr_as_phi(instr);
> +
> +      nir_ssa_def *def = NULL;
> +      bool srcs_same = true;
> +
> +      nir_foreach_phi_src(phi, src) {
> +         assert(src->src.is_ssa);
> +
> +         if (def == NULL) {
> +            def  = src->src.ssa;
> +         } else {
> +            if (src->src.ssa != def) {
> +               srcs_same = false;
> +               break;
> +            }
> +         }
> +      }
> +
> +      if (!srcs_same)
> +         continue;
> +
> +      assert(phi->dest.is_ssa);
> +      nir_ssa_def_rewrite_uses(&phi->dest.ssa, nir_src_for_ssa(def),
> +                               mem_ctx);
> +      nir_instr_remove(instr);
> +
> +      *progress = true;
> +   }
> +
> +   return true;
> +}
> +
> +static bool
> +remove_phis_impl(nir_function_impl *impl)
> +{
> +   bool progress = false;
> +
> +   nir_foreach_block(impl, remove_phis_block, &progress);
> +
> +   return progress;
> +}
> +
> +bool
> +nir_opt_remove_phis(nir_shader *shader)
> +{
> +   bool progress = false;
> +
> +   nir_foreach_overload(shader, overload)
> +      if (overload->impl)
> +         progress = remove_phis_impl(overload->impl) || progress;
> +
> +   return progress;
> +}
> +
> --
> 2.1.0
>


More information about the mesa-dev mailing list