<div dir="ltr">On 2 April 2013 23:33, Kenneth Graunke <span dir="ltr"><<a href="mailto:kenneth@whitecape.org" target="_blank">kenneth@whitecape.org</a>></span> wrote:<br><div class="gmail_extra"><div class="gmail_quote">
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">This pass flips (matrix * vector) operations to (vector *<br>
matrixTranspose) for certain built-in matrices (currently<br>
gl_ModelViewProjectionMatrix and gl_TextureMatrix).<br>
<br>
This is equivalent, but results in dot products rather than multiplies<br>
and adds. On some hardware, this is more efficient.<br>
<br>
This pass is conditionalized on ctx->mvp_with_dp4, the flag drivers set<br>
to indicate they prefer dot products.<br>
<br>
Improves performance in Lightsmark by 1.01131% +/- 0.162069% (n = 10)<br>
on a Haswell GT2 system. Passes Piglit on Ivybridge.<br>
<br>
Signed-off-by: Kenneth Graunke <<a href="mailto:kenneth@whitecape.org">kenneth@whitecape.org</a>><br>
---<br>
src/glsl/Makefile.sources | 1 +<br>
src/glsl/glsl_parser_extras.cpp | 7 +-<br>
src/glsl/ir_optimization.h | 4 +-<br>
src/glsl/linker.cpp | 2 +-<br>
src/glsl/main.cpp | 2 +-<br>
src/glsl/opt_flip_matrices.cpp | 122 +++++++++++++++++++++++++++++<br>
src/glsl/test_optpass.cpp | 2 +-<br>
src/mesa/drivers/dri/i965/brw_shader.cpp | 2 +-<br>
src/mesa/main/ff_fragment_shader.cpp | 2 +-<br>
src/mesa/program/ir_to_mesa.cpp | 5 +-<br>
src/mesa/state_tracker/st_glsl_to_tgsi.cpp | 3 +-<br>
11 files changed, 142 insertions(+), 10 deletions(-)<br>
create mode 100644 src/glsl/opt_flip_matrices.cpp<br>
<br>
diff --git a/src/glsl/Makefile.sources b/src/glsl/Makefile.sources<br>
index c294aa4..df1e9d5 100644<br>
--- a/src/glsl/Makefile.sources<br>
+++ b/src/glsl/Makefile.sources<br>
@@ -80,6 +80,7 @@ LIBGLSL_FILES = \<br>
$(GLSL_SRCDIR)/opt_dead_code.cpp \<br>
$(GLSL_SRCDIR)/opt_dead_code_local.cpp \<br>
$(GLSL_SRCDIR)/opt_dead_functions.cpp \<br>
+ $(GLSL_SRCDIR)/opt_flip_matrices.cpp \<br>
$(GLSL_SRCDIR)/opt_function_inlining.cpp \<br>
$(GLSL_SRCDIR)/opt_if_simplification.cpp \<br>
$(GLSL_SRCDIR)/opt_noop_swizzle.cpp \<br>
diff --git a/src/glsl/glsl_parser_extras.cpp b/src/glsl/glsl_parser_extras.cpp<br>
index 9740903..e0387b8 100644<br>
--- a/src/glsl/glsl_parser_extras.cpp<br>
+++ b/src/glsl/glsl_parser_extras.cpp<br>
@@ -1206,7 +1206,8 @@ ast_struct_specifier::ast_struct_specifier(const char *identifier,<br>
bool<br>
do_common_optimization(exec_list *ir, bool linked,<br>
bool uniform_locations_assigned,<br>
- unsigned max_unroll_iterations)<br>
+ unsigned max_unroll_iterations,<br>
+ bool prefer_dp4)<br>
{<br>
GLboolean progress = GL_FALSE;<br>
<br>
@@ -1220,6 +1221,10 @@ do_common_optimization(exec_list *ir, bool linked,<br>
progress = do_if_simplification(ir) || progress;<br>
progress = do_copy_propagation(ir) || progress;<br>
progress = do_copy_propagation_elements(ir) || progress;<br>
+<br>
+ if (prefer_dp4 && !linked)<br>
+ progress = opt_flip_matrices(ir) || progress;<br>
+<br>
if (linked)<br>
progress = do_dead_code(ir, uniform_locations_assigned) || progress;<br>
else<br>
diff --git a/src/glsl/ir_optimization.h b/src/glsl/ir_optimization.h<br>
index 2454bbe..b404256 100644<br>
--- a/src/glsl/ir_optimization.h<br>
+++ b/src/glsl/ir_optimization.h<br>
@@ -65,7 +65,8 @@ enum lower_packing_builtins_op {<br>
<br>
bool do_common_optimization(exec_list *ir, bool linked,<br>
bool uniform_locations_assigned,<br>
- unsigned max_unroll_iterations);<br>
+ unsigned max_unroll_iterations,<br>
+ bool prefer_dp4);<br>
<br>
bool do_algebraic(exec_list *instructions);<br>
bool do_constant_folding(exec_list *instructions);<br>
@@ -78,6 +79,7 @@ bool do_dead_code(exec_list *instructions, bool uniform_locations_assigned);<br>
bool do_dead_code_local(exec_list *instructions);<br>
bool do_dead_code_unlinked(exec_list *instructions);<br>
bool do_dead_functions(exec_list *instructions);<br>
+bool opt_flip_matrices(exec_list *instructions);<br>
bool do_function_inlining(exec_list *instructions);<br>
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);<br>
bool do_lower_texture_projection(exec_list *instructions);<br>
diff --git a/src/glsl/linker.cpp b/src/glsl/linker.cpp<br>
index 2b30d2b..67a6c5a 100644<br>
--- a/src/glsl/linker.cpp<br>
+++ b/src/glsl/linker.cpp<br>
@@ -1767,7 +1767,7 @@ link_shaders(struct gl_context *ctx, struct gl_shader_program *prog)<br>
<br>
unsigned max_unroll = ctx->ShaderCompilerOptions[i].MaxUnrollIterations;<br>
<br>
- while (do_common_optimization(prog->_LinkedShaders[i]->ir, true, false, max_unroll))<br>
+ while (do_common_optimization(prog->_LinkedShaders[i]->ir, true, false, max_unroll, ctx->mvp_with_dp4))<br>
;<br>
}<br>
<br>
diff --git a/src/glsl/main.cpp b/src/glsl/main.cpp<br>
index ce084b4..13dfdd3 100644<br>
--- a/src/glsl/main.cpp<br>
+++ b/src/glsl/main.cpp<br>
@@ -176,7 +176,7 @@ compile_shader(struct gl_context *ctx, struct gl_shader *shader)<br>
if (!state->error && !shader->ir->is_empty()) {<br>
bool progress;<br>
do {<br>
- progress = do_common_optimization(shader->ir, false, false, 32);<br>
+ progress = do_common_optimization(shader->ir, false, false, 32, false);<br></blockquote><div><br></div><div>What's the reason for passing false in this case? It seems like we ought to pass ctx->mvp_with_dp4 in all cases.<br>
<br></div><div>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.<br><br>With that question addressed, this series is:<br>
<br></div><div>Reviewed-by: Paul Berry <<a href="mailto:stereotype441@gmail.com">stereotype441@gmail.com</a>><br></div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
} while (progress);<br>
<br>
validate_ir_tree(shader->ir);<br>
diff --git a/src/glsl/opt_flip_matrices.cpp b/src/glsl/opt_flip_matrices.cpp<br>
new file mode 100644<br>
index 0000000..497513f<br>
--- /dev/null<br>
+++ b/src/glsl/opt_flip_matrices.cpp<br>
@@ -0,0 +1,122 @@<br>
+/*<br>
+ * Copyright © 2013 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<br>
+ * DEALINGS IN THE SOFTWARE.<br>
+ */<br>
+<br>
+/**<br>
+ * \file opt_flip_matrices.cpp<br>
+ *<br>
+ * Convert (matrix * vector) operations to (vector * matrixTranspose),<br>
+ * which can be done using dot products rather than multiplies and adds.<br>
+ * On some hardware, this is more efficient.<br>
+ *<br>
+ * This currently only does the conversion for built-in matrices which<br>
+ * already have transposed equivalents. Namely, gl_ModelViewProjectionMatrix<br>
+ * and gl_TextureMatrix.<br>
+ */<br>
+#include "ir.h"<br>
+#include "ir_optimization.h"<br>
+#include "main/macros.h"<br>
+<br>
+namespace {<br>
+class matrix_flipper : public ir_hierarchical_visitor {<br>
+public:<br>
+ matrix_flipper(exec_list *instructions)<br>
+ {<br>
+ progress = false;<br>
+ mvp_transpose = NULL;<br>
+ texmat_transpose = NULL;<br>
+<br>
+ foreach_list(n, instructions) {<br>
+ ir_instruction *ir = (ir_instruction *) n;<br>
+ ir_variable *var = ir->as_variable();<br>
+ if (!var)<br>
+ continue;<br>
+ if (strcmp(var->name, "gl_ModelViewProjectionMatrixTranspose") == 0)<br>
+ mvp_transpose = var;<br>
+ if (strcmp(var->name, "gl_TextureMatrixTranspose") == 0)<br>
+ texmat_transpose = var;<br>
+ }<br>
+ }<br>
+<br>
+ ir_visitor_status visit_enter(ir_expression *ir);<br>
+<br>
+ bool progress;<br>
+<br>
+private:<br>
+ ir_variable *mvp_transpose;<br>
+ ir_variable *texmat_transpose;<br>
+};<br>
+}<br>
+<br>
+ir_visitor_status<br>
+matrix_flipper::visit_enter(ir_expression *ir)<br>
+{<br>
+ if (ir->operation != ir_binop_mul ||<br>
+ !ir->operands[0]->type->is_matrix() ||<br>
+ !ir->operands[1]->type->is_vector())<br>
+ return visit_continue;<br>
+<br>
+ ir_variable *mat_var = ir->operands[0]->variable_referenced();<br>
+ if (!mat_var)<br>
+ return visit_continue;<br>
+<br>
+ if (mvp_transpose &&<br>
+ strcmp(mat_var->name, "gl_ModelViewProjectionMatrix") == 0) {<br>
+ ir_dereference_variable *deref = ir->operands[0]->as_dereference_variable();<br>
+ assert(deref && deref->var == mat_var);<br>
+<br>
+ void *mem_ctx = ralloc_parent(ir);<br>
+<br>
+ ir->operands[0] = ir->operands[1];<br>
+ ir->operands[1] = new(mem_ctx) ir_dereference_variable(mvp_transpose);<br>
+<br>
+ progress = true;<br>
+ } else if (texmat_transpose &&<br>
+ strcmp(mat_var->name, "gl_TextureMatrix") == 0) {<br>
+ ir_dereference_array *array_ref = ir->operands[0]->as_dereference_array();<br>
+ assert(array_ref != NULL);<br>
+ ir_dereference_variable *var_ref = array_ref->array->as_dereference_variable();<br>
+ assert(var_ref && var_ref->var == mat_var);<br>
+<br>
+ ir->operands[0] = ir->operands[1];<br>
+ ir->operands[1] = array_ref;<br>
+<br>
+ var_ref->var = texmat_transpose;<br>
+<br>
+ texmat_transpose->max_array_access =<br>
+ MAX2(texmat_transpose->max_array_access, mat_var->max_array_access);<br>
+<br>
+ progress = true;<br>
+ }<br>
+<br>
+ return visit_continue;<br>
+}<br>
+<br>
+bool<br>
+opt_flip_matrices(struct exec_list *instructions)<br>
+{<br>
+ matrix_flipper v(instructions);<br>
+<br>
+ visit_list_elements(&v, instructions);<br>
+<br>
+ return v.progress;<br>
+}<br>
diff --git a/src/glsl/test_optpass.cpp b/src/glsl/test_optpass.cpp<br>
index 117b0b0..87c3705 100644<br>
--- a/src/glsl/test_optpass.cpp<br>
+++ b/src/glsl/test_optpass.cpp<br>
@@ -64,7 +64,7 @@ do_optimization(struct exec_list *ir, const char *optimization)<br>
<br>
if (sscanf(optimization, "do_common_optimization ( %d , %d ) ",<br>
&int_0, &int_1) == 2) {<br>
- return do_common_optimization(ir, int_0 != 0, false, int_1);<br>
+ return do_common_optimization(ir, int_0 != 0, false, int_1, false);<br>
} else if (strcmp(optimization, "do_algebraic") == 0) {<br>
return do_algebraic(ir);<br>
} else if (strcmp(optimization, "do_constant_folding") == 0) {<br>
diff --git a/src/mesa/drivers/dri/i965/brw_shader.cpp b/src/mesa/drivers/dri/i965/brw_shader.cpp<br>
index 1a52039..54537b1 100644<br>
--- a/src/mesa/drivers/dri/i965/brw_shader.cpp<br>
+++ b/src/mesa/drivers/dri/i965/brw_shader.cpp<br>
@@ -207,7 +207,7 @@ brw_link_shader(struct gl_context *ctx, struct gl_shader_program *shProg)<br>
false /* loops */<br>
) || progress;<br>
<br>
- progress = do_common_optimization(shader->ir, true, true, 32)<br>
+ progress = do_common_optimization(shader->ir, true, true, 32, true)<br>
|| progress;<br>
} while (progress);<br>
<br>
diff --git a/src/mesa/main/ff_fragment_shader.cpp b/src/mesa/main/ff_fragment_shader.cpp<br>
index 01a4542..fb45578 100644<br>
--- a/src/mesa/main/ff_fragment_shader.cpp<br>
+++ b/src/mesa/main/ff_fragment_shader.cpp<br>
@@ -1336,7 +1336,7 @@ create_new_program(struct gl_context *ctx, struct state_key *key)<br>
<br>
validate_ir_tree(p.shader->ir);<br>
<br>
- while (do_common_optimization(p.shader->ir, false, false, 32))<br>
+ while (do_common_optimization(p.shader->ir, false, false, 32, ctx->mvp_with_dp4))<br>
;<br>
reparent_ir(p.shader->ir, p.shader->ir);<br>
<br>
diff --git a/src/mesa/program/ir_to_mesa.cpp b/src/mesa/program/ir_to_mesa.cpp<br>
index 14cf5ba..9456571 100644<br>
--- a/src/mesa/program/ir_to_mesa.cpp<br>
+++ b/src/mesa/program/ir_to_mesa.cpp<br>
@@ -3012,7 +3012,8 @@ _mesa_ir_link_shader(struct gl_context *ctx, struct gl_shader_program *prog)<br>
progress = do_lower_jumps(ir, true, true, options->EmitNoMainReturn, options->EmitNoCont, options->EmitNoLoops) || progress;<br>
<br>
progress = do_common_optimization(ir, true, true,<br>
- options->MaxUnrollIterations)<br>
+ options->MaxUnrollIterations,<br>
+ ctx->mvp_with_dp4)<br>
|| progress;<br>
<br>
progress = lower_quadop_vector(ir, true) || progress;<br>
@@ -3122,7 +3123,7 @@ _mesa_glsl_compile_shader(struct gl_context *ctx, struct gl_shader *shader)<br>
/* Do some optimization at compile time to reduce shader IR size<br>
* and reduce later work if the same shader is linked multiple times<br>
*/<br>
- while (do_common_optimization(shader->ir, false, false, 32))<br>
+ while (do_common_optimization(shader->ir, false, false, 32, ctx->mvp_with_dp4))<br>
;<br>
<br>
validate_ir_tree(shader->ir);<br>
diff --git a/src/mesa/state_tracker/st_glsl_to_tgsi.cpp b/src/mesa/state_tracker/st_glsl_to_tgsi.cpp<br>
index 338c652..a23c705 100644<br>
--- a/src/mesa/state_tracker/st_glsl_to_tgsi.cpp<br>
+++ b/src/mesa/state_tracker/st_glsl_to_tgsi.cpp<br>
@@ -5228,7 +5228,8 @@ st_link_shader(struct gl_context *ctx, struct gl_shader_program *prog)<br>
progress = do_lower_jumps(ir, true, true, options->EmitNoMainReturn, options->EmitNoCont, options->EmitNoLoops) || progress;<br>
<br>
progress = do_common_optimization(ir, true, true,<br>
- options->MaxUnrollIterations)<br>
+ options->MaxUnrollIterations,<br>
+ ctx->mvp_with_dp4)<br>
|| progress;<br>
<br>
progress = lower_if_to_cond_assign(ir, options->MaxIfDepth) || progress;<br>
<span class="HOEnZb"><font color="#888888">--<br>
1.8.2<br>
<br>
_______________________________________________<br>
mesa-dev mailing list<br>
<a href="mailto:mesa-dev@lists.freedesktop.org">mesa-dev@lists.freedesktop.org</a><br>
<a href="http://lists.freedesktop.org/mailman/listinfo/mesa-dev" target="_blank">http://lists.freedesktop.org/mailman/listinfo/mesa-dev</a><br>
</font></span></blockquote></div><br></div></div>