[Mesa-dev] [PATCH 2/2] glsl: Add a pass to flip matrix/vector multiplies to use dot products.

Paul Berry stereotype441 at gmail.com
Thu Apr 4 08:13:15 PDT 2013


On 2 April 2013 23:33, Kenneth Graunke <kenneth at whitecape.org> wrote:

> This pass flips (matrix * vector) operations to (vector *
> matrixTranspose) for certain built-in matrices (currently
> gl_ModelViewProjectionMatrix and gl_TextureMatrix).
>
> This is equivalent, but results in dot products rather than multiplies
> and adds.  On some hardware, this is more efficient.
>
> This pass is conditionalized on ctx->mvp_with_dp4, the flag drivers set
> to indicate they prefer dot products.
>
> Improves performance in Lightsmark by 1.01131% +/- 0.162069% (n = 10)
> on a Haswell GT2 system.  Passes Piglit on Ivybridge.
>
> Signed-off-by: Kenneth Graunke <kenneth at whitecape.org>
> ---
>  src/glsl/Makefile.sources                  |   1 +
>  src/glsl/glsl_parser_extras.cpp            |   7 +-
>  src/glsl/ir_optimization.h                 |   4 +-
>  src/glsl/linker.cpp                        |   2 +-
>  src/glsl/main.cpp                          |   2 +-
>  src/glsl/opt_flip_matrices.cpp             | 122
> +++++++++++++++++++++++++++++
>  src/glsl/test_optpass.cpp                  |   2 +-
>  src/mesa/drivers/dri/i965/brw_shader.cpp   |   2 +-
>  src/mesa/main/ff_fragment_shader.cpp       |   2 +-
>  src/mesa/program/ir_to_mesa.cpp            |   5 +-
>  src/mesa/state_tracker/st_glsl_to_tgsi.cpp |   3 +-
>  11 files changed, 142 insertions(+), 10 deletions(-)
>  create mode 100644 src/glsl/opt_flip_matrices.cpp
>
> diff --git a/src/glsl/Makefile.sources b/src/glsl/Makefile.sources
> index c294aa4..df1e9d5 100644
> --- a/src/glsl/Makefile.sources
> +++ b/src/glsl/Makefile.sources
> @@ -80,6 +80,7 @@ LIBGLSL_FILES = \
>         $(GLSL_SRCDIR)/opt_dead_code.cpp \
>         $(GLSL_SRCDIR)/opt_dead_code_local.cpp \
>         $(GLSL_SRCDIR)/opt_dead_functions.cpp \
> +       $(GLSL_SRCDIR)/opt_flip_matrices.cpp \
>         $(GLSL_SRCDIR)/opt_function_inlining.cpp \
>         $(GLSL_SRCDIR)/opt_if_simplification.cpp \
>         $(GLSL_SRCDIR)/opt_noop_swizzle.cpp \
> diff --git a/src/glsl/glsl_parser_extras.cpp
> b/src/glsl/glsl_parser_extras.cpp
> index 9740903..e0387b8 100644
> --- a/src/glsl/glsl_parser_extras.cpp
> +++ b/src/glsl/glsl_parser_extras.cpp
> @@ -1206,7 +1206,8 @@ ast_struct_specifier::ast_struct_specifier(const
> char *identifier,
>  bool
>  do_common_optimization(exec_list *ir, bool linked,
>                        bool uniform_locations_assigned,
> -                      unsigned max_unroll_iterations)
> +                      unsigned max_unroll_iterations,
> +                       bool prefer_dp4)
>  {
>     GLboolean progress = GL_FALSE;
>
> @@ -1220,6 +1221,10 @@ do_common_optimization(exec_list *ir, bool linked,
>     progress = do_if_simplification(ir) || progress;
>     progress = do_copy_propagation(ir) || progress;
>     progress = do_copy_propagation_elements(ir) || progress;
> +
> +   if (prefer_dp4 && !linked)
> +      progress = opt_flip_matrices(ir) || progress;
> +
>     if (linked)
>        progress = do_dead_code(ir, uniform_locations_assigned) || progress;
>     else
> diff --git a/src/glsl/ir_optimization.h b/src/glsl/ir_optimization.h
> index 2454bbe..b404256 100644
> --- a/src/glsl/ir_optimization.h
> +++ b/src/glsl/ir_optimization.h
> @@ -65,7 +65,8 @@ enum lower_packing_builtins_op {
>
>  bool do_common_optimization(exec_list *ir, bool linked,
>                             bool uniform_locations_assigned,
> -                           unsigned max_unroll_iterations);
> +                           unsigned max_unroll_iterations,
> +                            bool prefer_dp4);
>
>  bool do_algebraic(exec_list *instructions);
>  bool do_constant_folding(exec_list *instructions);
> @@ -78,6 +79,7 @@ bool do_dead_code(exec_list *instructions, bool
> uniform_locations_assigned);
>  bool do_dead_code_local(exec_list *instructions);
>  bool do_dead_code_unlinked(exec_list *instructions);
>  bool do_dead_functions(exec_list *instructions);
> +bool opt_flip_matrices(exec_list *instructions);
>  bool do_function_inlining(exec_list *instructions);
>  bool do_lower_jumps(exec_list *instructions, bool pull_out_jumps = true,
> bool lower_sub_return = true, bool lower_main_return = false, bool
> lower_continue = false, bool lower_break = false);
>  bool do_lower_texture_projection(exec_list *instructions);
> diff --git a/src/glsl/linker.cpp b/src/glsl/linker.cpp
> index 2b30d2b..67a6c5a 100644
> --- a/src/glsl/linker.cpp
> +++ b/src/glsl/linker.cpp
> @@ -1767,7 +1767,7 @@ link_shaders(struct gl_context *ctx, struct
> gl_shader_program *prog)
>
>        unsigned max_unroll =
> ctx->ShaderCompilerOptions[i].MaxUnrollIterations;
>
> -      while (do_common_optimization(prog->_LinkedShaders[i]->ir, true,
> false, max_unroll))
> +      while (do_common_optimization(prog->_LinkedShaders[i]->ir, true,
> false, max_unroll, ctx->mvp_with_dp4))
>          ;
>     }
>
> diff --git a/src/glsl/main.cpp b/src/glsl/main.cpp
> index ce084b4..13dfdd3 100644
> --- a/src/glsl/main.cpp
> +++ b/src/glsl/main.cpp
> @@ -176,7 +176,7 @@ compile_shader(struct gl_context *ctx, struct
> gl_shader *shader)
>     if (!state->error && !shader->ir->is_empty()) {
>        bool progress;
>        do {
> -        progress = do_common_optimization(shader->ir, false, false, 32);
> +        progress = do_common_optimization(shader->ir, false, false, 32,
> false);
>

What's the reason for passing false in this case?  It seems like we ought
to pass ctx->mvp_with_dp4 in all cases.

For that matter, I'm curious why we don't just check the value of
ctx->mvp_with_dp4 from inside do_common_optimization()--it seems like that
would be easier to maintain.

With that question addressed, this series is:

Reviewed-by: Paul Berry <stereotype441 at gmail.com>


>        } while (progress);
>
>        validate_ir_tree(shader->ir);
> diff --git a/src/glsl/opt_flip_matrices.cpp
> b/src/glsl/opt_flip_matrices.cpp
> new file mode 100644
> index 0000000..497513f
> --- /dev/null
> +++ b/src/glsl/opt_flip_matrices.cpp
> @@ -0,0 +1,122 @@
> +/*
> + * Copyright © 2013 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_flip_matrices.cpp
> + *
> + * Convert (matrix * vector) operations to (vector * matrixTranspose),
> + * which can be done using dot products rather than multiplies and adds.
> + * On some hardware, this is more efficient.
> + *
> + * This currently only does the conversion for built-in matrices which
> + * already have transposed equivalents.  Namely,
> gl_ModelViewProjectionMatrix
> + * and gl_TextureMatrix.
> + */
> +#include "ir.h"
> +#include "ir_optimization.h"
> +#include "main/macros.h"
> +
> +namespace {
> +class matrix_flipper : public ir_hierarchical_visitor {
> +public:
> +   matrix_flipper(exec_list *instructions)
> +   {
> +      progress = false;
> +      mvp_transpose = NULL;
> +      texmat_transpose = NULL;
> +
> +      foreach_list(n, instructions) {
> +         ir_instruction *ir = (ir_instruction *) n;
> +         ir_variable *var = ir->as_variable();
> +         if (!var)
> +            continue;
> +         if (strcmp(var->name, "gl_ModelViewProjectionMatrixTranspose")
> == 0)
> +            mvp_transpose = var;
> +         if (strcmp(var->name, "gl_TextureMatrixTranspose") == 0)
> +            texmat_transpose = var;
> +      }
> +   }
> +
> +   ir_visitor_status visit_enter(ir_expression *ir);
> +
> +   bool progress;
> +
> +private:
> +   ir_variable *mvp_transpose;
> +   ir_variable *texmat_transpose;
> +};
> +}
> +
> +ir_visitor_status
> +matrix_flipper::visit_enter(ir_expression *ir)
> +{
> +   if (ir->operation != ir_binop_mul ||
> +       !ir->operands[0]->type->is_matrix() ||
> +       !ir->operands[1]->type->is_vector())
> +      return visit_continue;
> +
> +   ir_variable *mat_var = ir->operands[0]->variable_referenced();
> +   if (!mat_var)
> +      return visit_continue;
> +
> +   if (mvp_transpose &&
> +       strcmp(mat_var->name, "gl_ModelViewProjectionMatrix") == 0) {
> +      ir_dereference_variable *deref =
> ir->operands[0]->as_dereference_variable();
> +      assert(deref && deref->var == mat_var);
> +
> +      void *mem_ctx = ralloc_parent(ir);
> +
> +      ir->operands[0] = ir->operands[1];
> +      ir->operands[1] = new(mem_ctx)
> ir_dereference_variable(mvp_transpose);
> +
> +      progress = true;
> +   } else if (texmat_transpose &&
> +              strcmp(mat_var->name, "gl_TextureMatrix") == 0) {
> +      ir_dereference_array *array_ref =
> ir->operands[0]->as_dereference_array();
> +      assert(array_ref != NULL);
> +      ir_dereference_variable *var_ref =
> array_ref->array->as_dereference_variable();
> +      assert(var_ref && var_ref->var == mat_var);
> +
> +      ir->operands[0] = ir->operands[1];
> +      ir->operands[1] = array_ref;
> +
> +      var_ref->var = texmat_transpose;
> +
> +      texmat_transpose->max_array_access =
> +         MAX2(texmat_transpose->max_array_access,
> mat_var->max_array_access);
> +
> +      progress = true;
> +   }
> +
> +   return visit_continue;
> +}
> +
> +bool
> +opt_flip_matrices(struct exec_list *instructions)
> +{
> +   matrix_flipper v(instructions);
> +
> +   visit_list_elements(&v, instructions);
> +
> +   return v.progress;
> +}
> diff --git a/src/glsl/test_optpass.cpp b/src/glsl/test_optpass.cpp
> index 117b0b0..87c3705 100644
> --- a/src/glsl/test_optpass.cpp
> +++ b/src/glsl/test_optpass.cpp
> @@ -64,7 +64,7 @@ do_optimization(struct exec_list *ir, const char
> *optimization)
>
>     if (sscanf(optimization, "do_common_optimization ( %d , %d ) ",
>                &int_0, &int_1) == 2) {
> -      return do_common_optimization(ir, int_0 != 0, false, int_1);
> +      return do_common_optimization(ir, int_0 != 0, false, int_1, false);
>     } else if (strcmp(optimization, "do_algebraic") == 0) {
>        return do_algebraic(ir);
>     } else if (strcmp(optimization, "do_constant_folding") == 0) {
> diff --git a/src/mesa/drivers/dri/i965/brw_shader.cpp
> b/src/mesa/drivers/dri/i965/brw_shader.cpp
> index 1a52039..54537b1 100644
> --- a/src/mesa/drivers/dri/i965/brw_shader.cpp
> +++ b/src/mesa/drivers/dri/i965/brw_shader.cpp
> @@ -207,7 +207,7 @@ brw_link_shader(struct gl_context *ctx, struct
> gl_shader_program *shProg)
>                                    false /* loops */
>                                    ) || progress;
>
> -        progress = do_common_optimization(shader->ir, true, true, 32)
> +        progress = do_common_optimization(shader->ir, true, true, 32,
> true)
>            || progress;
>        } while (progress);
>
> diff --git a/src/mesa/main/ff_fragment_shader.cpp
> b/src/mesa/main/ff_fragment_shader.cpp
> index 01a4542..fb45578 100644
> --- a/src/mesa/main/ff_fragment_shader.cpp
> +++ b/src/mesa/main/ff_fragment_shader.cpp
> @@ -1336,7 +1336,7 @@ create_new_program(struct gl_context *ctx, struct
> state_key *key)
>
>     validate_ir_tree(p.shader->ir);
>
> -   while (do_common_optimization(p.shader->ir, false, false, 32))
> +   while (do_common_optimization(p.shader->ir, false, false, 32,
> ctx->mvp_with_dp4))
>        ;
>     reparent_ir(p.shader->ir, p.shader->ir);
>
> diff --git a/src/mesa/program/ir_to_mesa.cpp
> b/src/mesa/program/ir_to_mesa.cpp
> index 14cf5ba..9456571 100644
> --- a/src/mesa/program/ir_to_mesa.cpp
> +++ b/src/mesa/program/ir_to_mesa.cpp
> @@ -3012,7 +3012,8 @@ _mesa_ir_link_shader(struct gl_context *ctx, struct
> gl_shader_program *prog)
>          progress = do_lower_jumps(ir, true, true,
> options->EmitNoMainReturn, options->EmitNoCont, options->EmitNoLoops) ||
> progress;
>
>          progress = do_common_optimization(ir, true, true,
> -                                          options->MaxUnrollIterations)
> +                                          options->MaxUnrollIterations,
> +                                           ctx->mvp_with_dp4)
>            || progress;
>
>          progress = lower_quadop_vector(ir, true) || progress;
> @@ -3122,7 +3123,7 @@ _mesa_glsl_compile_shader(struct gl_context *ctx,
> struct gl_shader *shader)
>        /* Do some optimization at compile time to reduce shader IR size
>         * and reduce later work if the same shader is linked multiple times
>         */
> -      while (do_common_optimization(shader->ir, false, false, 32))
> +      while (do_common_optimization(shader->ir, false, false, 32,
> ctx->mvp_with_dp4))
>          ;
>
>        validate_ir_tree(shader->ir);
> diff --git a/src/mesa/state_tracker/st_glsl_to_tgsi.cpp
> b/src/mesa/state_tracker/st_glsl_to_tgsi.cpp
> index 338c652..a23c705 100644
> --- a/src/mesa/state_tracker/st_glsl_to_tgsi.cpp
> +++ b/src/mesa/state_tracker/st_glsl_to_tgsi.cpp
> @@ -5228,7 +5228,8 @@ st_link_shader(struct gl_context *ctx, struct
> gl_shader_program *prog)
>           progress = do_lower_jumps(ir, true, true,
> options->EmitNoMainReturn, options->EmitNoCont, options->EmitNoLoops) ||
> progress;
>
>           progress = do_common_optimization(ir, true, true,
> -                                          options->MaxUnrollIterations)
> +                                          options->MaxUnrollIterations,
> +                                           ctx->mvp_with_dp4)
>            || progress;
>
>           progress = lower_if_to_cond_assign(ir, options->MaxIfDepth) ||
> progress;
> --
> 1.8.2
>
> _______________________________________________
> mesa-dev mailing list
> mesa-dev at lists.freedesktop.org
> http://lists.freedesktop.org/mailman/listinfo/mesa-dev
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freedesktop.org/archives/mesa-dev/attachments/20130404/2482070a/attachment-0001.html>


More information about the mesa-dev mailing list