[Mesa-dev] [PATCH 1/4] glsl: Merge opt_noop_swizzle, and opt_swizzle_swizzle

Ian Romanick idr at freedesktop.org
Mon May 15 22:24:12 UTC 2017


On 04/06/2017 12:49 PM, Thomas Helland wrote:
> The pass to optimize two swizzles in a row can be interpreted as
> a noop swizzle optimization. Merge the two passes, and avoid having
> the extra pass that runs a visitor pattern.
> ---
>  src/compiler/Makefile.sources             |  1 -
>  src/compiler/glsl/glsl_parser_extras.cpp  |  1 -
>  src/compiler/glsl/ir_optimization.h       |  1 -
>  src/compiler/glsl/opt_noop_swizzle.cpp    | 43 +++++++++++++-
>  src/compiler/glsl/opt_swizzle_swizzle.cpp | 97 -------------------------------

I think I would have been inclined to keep the swizzle_swizzle name, but
meh.

>  5 files changed, 42 insertions(+), 101 deletions(-)
>  delete mode 100644 src/compiler/glsl/opt_swizzle_swizzle.cpp
> 
> diff --git a/src/compiler/Makefile.sources b/src/compiler/Makefile.sources
> index 2455d4eb5a..8b4245612b 100644
> --- a/src/compiler/Makefile.sources
> +++ b/src/compiler/Makefile.sources
> @@ -134,7 +134,6 @@ LIBGLSL_FILES = \
>  	glsl/opt_rebalance_tree.cpp \
>  	glsl/opt_redundant_jumps.cpp \
>  	glsl/opt_structure_splitting.cpp \
> -	glsl/opt_swizzle_swizzle.cpp \
>  	glsl/opt_tree_grafting.cpp \
>  	glsl/opt_vectorize.cpp \
>  	glsl/program.h \
> diff --git a/src/compiler/glsl/glsl_parser_extras.cpp b/src/compiler/glsl/glsl_parser_extras.cpp
> index 48cbc01ba7..53fcb0e45a 100644
> --- a/src/compiler/glsl/glsl_parser_extras.cpp
> +++ b/src/compiler/glsl/glsl_parser_extras.cpp
> @@ -2170,7 +2170,6 @@ do_common_optimization(exec_list *ir, bool linked,
>         options->EmitNoCont, options->EmitNoLoops);
>     OPT(do_vec_index_to_swizzle, ir);
>     OPT(lower_vector_insert, ir, false);
> -   OPT(do_swizzle_swizzle, ir);
>     OPT(do_noop_swizzle, ir);
>  
>     OPT(optimize_split_arrays, ir, linked);
> diff --git a/src/compiler/glsl/ir_optimization.h b/src/compiler/glsl/ir_optimization.h
> index 67a7514c7d..65d3b3e0db 100644
> --- a/src/compiler/glsl/ir_optimization.h
> +++ b/src/compiler/glsl/ir_optimization.h
> @@ -122,7 +122,6 @@ bool do_mat_op_to_vec(exec_list *instructions);
>  bool do_minmax_prune(exec_list *instructions);
>  bool do_noop_swizzle(exec_list *instructions);
>  bool do_structure_splitting(exec_list *instructions);
> -bool do_swizzle_swizzle(exec_list *instructions);
>  bool do_vectorize(exec_list *instructions);
>  bool do_tree_grafting(exec_list *instructions);
>  bool do_vec_index_to_cond_assign(exec_list *instructions);
> diff --git a/src/compiler/glsl/opt_noop_swizzle.cpp b/src/compiler/glsl/opt_noop_swizzle.cpp
> index 41890ab2b1..082e0195fe 100644
> --- a/src/compiler/glsl/opt_noop_swizzle.cpp
> +++ b/src/compiler/glsl/opt_noop_swizzle.cpp
> @@ -49,6 +49,39 @@ public:
>  
>  } /* unnamed namespace */
>  
> +static bool
> +opt_swizzle_swizzle(ir_swizzle *ir)
> +{
> +   int mask2[4];
> +
> +   ir_swizzle *swiz2 = ir->val->as_swizzle();
> +   if (!swiz2)
> +      return visit_continue;
> +
> +   memset(&mask2, 0, sizeof(mask2));
> +   if (swiz2->mask.num_components >= 1)
> +      mask2[0] = swiz2->mask.x;
> +   if (swiz2->mask.num_components >= 2)
> +      mask2[1] = swiz2->mask.y;
> +   if (swiz2->mask.num_components >= 3)
> +      mask2[2] = swiz2->mask.z;
> +   if (swiz2->mask.num_components >= 4)
> +      mask2[3] = swiz2->mask.w;
> +
> +   if (ir->mask.num_components >= 1)
> +      ir->mask.x = mask2[ir->mask.x];
> +   if (ir->mask.num_components >= 2)
> +      ir->mask.y = mask2[ir->mask.y];
> +   if (ir->mask.num_components >= 3)
> +      ir->mask.z = mask2[ir->mask.z];
> +   if (ir->mask.num_components >= 4)
> +      ir->mask.w = mask2[ir->mask.w];
> +
> +   ir->val = swiz2->val;
> +
> +   return true;
> +}
> +
>  void
>  ir_noop_swizzle_visitor::handle_rvalue(ir_rvalue **rvalue)
>  {
> @@ -56,7 +89,15 @@ ir_noop_swizzle_visitor::handle_rvalue(ir_rvalue **rvalue)
>        return;
>  
>     ir_swizzle *swiz = (*rvalue)->as_swizzle();
> -   if (!swiz || swiz->type != swiz->val->type)
> +
> +   if (!swiz)
> +      return;
> +
> +   /* Check first if we can optimize two a sequence of two swizzles */

                                        to

The rest looks like it's just moving code from one place to another.
Assuming there are no shader-db changes, this patch is

Reviewed-by: Ian Romanick <ian.d.romanick at intel.com>

> +   if (opt_swizzle_swizzle(swiz))
> +      this->progress = true;
> +
> +   if (swiz->type != swiz->val->type)
>        return;
>  
>     int elems = swiz->val->type->vector_elements;
> diff --git a/src/compiler/glsl/opt_swizzle_swizzle.cpp b/src/compiler/glsl/opt_swizzle_swizzle.cpp
> deleted file mode 100644
> index 7285474b08..0000000000
> --- a/src/compiler/glsl/opt_swizzle_swizzle.cpp
> +++ /dev/null
> @@ -1,97 +0,0 @@
> -/*
> - * Copyright © 2010 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.
> - */
> -
> -/**
> - * \file opt_swizzle_swizzle.cpp
> - *
> - * Eliminates the second swizzle in a swizzle chain.
> - */
> -
> -#include "ir.h"
> -#include "ir_visitor.h"
> -#include "ir_optimization.h"
> -#include "compiler/glsl_types.h"
> -
> -namespace {
> -
> -class ir_swizzle_swizzle_visitor : public ir_hierarchical_visitor {
> -public:
> -   ir_swizzle_swizzle_visitor()
> -   {
> -      progress = false;
> -   }
> -
> -   virtual ir_visitor_status visit_enter(ir_swizzle *);
> -
> -   bool progress;
> -};
> -
> -} /* unnamed namespace */
> -
> -ir_visitor_status
> -ir_swizzle_swizzle_visitor::visit_enter(ir_swizzle *ir)
> -{
> -   int mask2[4];
> -
> -   ir_swizzle *swiz2 = ir->val->as_swizzle();
> -   if (!swiz2)
> -      return visit_continue;
> -
> -   memset(&mask2, 0, sizeof(mask2));
> -   if (swiz2->mask.num_components >= 1)
> -      mask2[0] = swiz2->mask.x;
> -   if (swiz2->mask.num_components >= 2)
> -      mask2[1] = swiz2->mask.y;
> -   if (swiz2->mask.num_components >= 3)
> -      mask2[2] = swiz2->mask.z;
> -   if (swiz2->mask.num_components >= 4)
> -      mask2[3] = swiz2->mask.w;
> -
> -   if (ir->mask.num_components >= 1)
> -      ir->mask.x = mask2[ir->mask.x];
> -   if (ir->mask.num_components >= 2)
> -      ir->mask.y = mask2[ir->mask.y];
> -   if (ir->mask.num_components >= 3)
> -      ir->mask.z = mask2[ir->mask.z];
> -   if (ir->mask.num_components >= 4)
> -      ir->mask.w = mask2[ir->mask.w];
> -
> -   ir->val = swiz2->val;
> -
> -   this->progress = true;
> -
> -   return visit_continue;
> -}
> -
> -/**
> - * Does a copy propagation pass on the code present in the instruction stream.
> - */
> -bool
> -do_swizzle_swizzle(exec_list *instructions)
> -{
> -   ir_swizzle_swizzle_visitor v;
> -
> -   v.run(instructions);
> -
> -   return v.progress;
> -}
> 



More information about the mesa-dev mailing list