[Mesa-dev] [PATCH 2/6] glsl: Rebalance expression trees that are reduction operations.
Matt Turner
mattst88 at gmail.com
Mon Mar 10 15:54:49 PDT 2014
The intention of this pass was to give us better instruction scheduling
opportunities, but it unexpectedly reduced some instruction counts as
well:
total instructions in shared programs: 1666639 -> 1666073 (-0.03%)
instructions in affected programs: 54612 -> 54046 (-1.04%)
(and trades 4 SIMD16 programs in SS3)
---
src/glsl/Makefile.sources | 1 +
src/glsl/glsl_parser_extras.cpp | 1 +
src/glsl/ir_optimization.h | 1 +
src/glsl/opt_rebalance_tree.cpp | 260 ++++++++++++++++++++++++++++++++++++++++
4 files changed, 263 insertions(+)
create mode 100644 src/glsl/opt_rebalance_tree.cpp
diff --git a/src/glsl/Makefile.sources b/src/glsl/Makefile.sources
index 5945590..b54eae7 100644
--- a/src/glsl/Makefile.sources
+++ b/src/glsl/Makefile.sources
@@ -96,6 +96,7 @@ LIBGLSL_FILES = \
$(GLSL_SRCDIR)/opt_function_inlining.cpp \
$(GLSL_SRCDIR)/opt_if_simplification.cpp \
$(GLSL_SRCDIR)/opt_noop_swizzle.cpp \
+ $(GLSL_SRCDIR)/opt_rebalance_tree.cpp \
$(GLSL_SRCDIR)/opt_redundant_jumps.cpp \
$(GLSL_SRCDIR)/opt_structure_splitting.cpp \
$(GLSL_SRCDIR)/opt_swizzle_swizzle.cpp \
diff --git a/src/glsl/glsl_parser_extras.cpp b/src/glsl/glsl_parser_extras.cpp
index 61ae621..e0cbbb1 100644
--- a/src/glsl/glsl_parser_extras.cpp
+++ b/src/glsl/glsl_parser_extras.cpp
@@ -1538,6 +1538,7 @@ do_common_optimization(exec_list *ir, bool linked,
progress = do_constant_variable_unlinked(ir) || progress;
progress = do_constant_folding(ir) || progress;
progress = do_cse(ir) || progress;
+ progress = do_rebalance_tree(ir) || progress;
progress = do_algebraic(ir) || progress;
progress = do_lower_jumps(ir) || progress;
progress = do_vec_index_to_swizzle(ir) || progress;
diff --git a/src/glsl/ir_optimization.h b/src/glsl/ir_optimization.h
index 5f4a2f4..4cad49b 100644
--- a/src/glsl/ir_optimization.h
+++ b/src/glsl/ir_optimization.h
@@ -70,6 +70,7 @@ bool do_common_optimization(exec_list *ir, bool linked,
const struct gl_shader_compiler_options *options);
bool do_algebraic(exec_list *instructions);
+bool do_rebalance_tree(exec_list *instructions);
bool do_constant_folding(exec_list *instructions);
bool do_constant_variable(exec_list *instructions);
bool do_constant_variable_unlinked(exec_list *instructions);
diff --git a/src/glsl/opt_rebalance_tree.cpp b/src/glsl/opt_rebalance_tree.cpp
new file mode 100644
index 0000000..91aa999
--- /dev/null
+++ b/src/glsl/opt_rebalance_tree.cpp
@@ -0,0 +1,260 @@
+/*
+ * Copyright © 2014 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_rebalance_tree.cpp
+ *
+ * Rebalances a reduction expression tree.
+ *
+ * For reduction operations (e.g., x + y + z + w) we generate an expression
+ * tree like
+ *
+ * +
+ * / \
+ * + w
+ * / \
+ * + z
+ * / \
+ * x y
+ *
+ * which we can rebalance into
+ *
+ * +
+ * / \
+ * / \
+ * + +
+ * / \ / \
+ * x y z w
+ *
+ * to get a better instruction scheduling.
+ *
+ * See "Tree Rebalancing in Optimal Editor Time and Space" by Quentin F. Stout
+ * and Bette L. Warren.
+ */
+
+#include "ir.h"
+#include "ir_visitor.h"
+#include "ir_rvalue_visitor.h"
+#include "ir_optimization.h"
+
+/* The DSW algorithm generates a degenerate tree (really, a linked list) in
+ * tree_to_vine(). We'd rather not leave a binary expression with only one
+ * operand, so trivial modifications (the ternary operators below) are needed
+ * to ensure that we only rotate around the ir_expression nodes of the tree.
+ */
+static unsigned
+tree_to_vine(ir_expression *root)
+{
+ unsigned size = 0;
+ ir_rvalue *vine_tail = root;
+ ir_rvalue *remainder = root->operands[1];
+
+ while (remainder != NULL) {
+ ir_expression *remainder_left = remainder->as_expression() ?
+ remainder->as_expression()->operands[0]->as_expression() : NULL;
+
+ if (remainder_left == NULL) {
+ /* move vine_tail down one */
+ vine_tail = remainder;
+ remainder = remainder->as_expression() ?
+ remainder->as_expression()->operands[1] : NULL;
+ size++;
+ } else {
+ /* rotate */
+ ir_expression *tempptr = remainder_left;
+ remainder->as_expression()->operands[0] = tempptr->operands[1];
+ tempptr->operands[1] = remainder;
+ remainder = tempptr;
+ vine_tail->as_expression()->operands[1] = tempptr;
+ }
+ }
+
+ return size;
+}
+
+static void
+compression(ir_expression *root, unsigned count)
+{
+ ir_expression *scanner = root;
+
+ for (unsigned i = 0; i < count; i++) {
+ ir_expression *child = scanner->operands[1]->as_expression();
+ scanner->operands[1] = child->operands[1];
+ scanner = scanner->operands[1]->as_expression();
+ child->operands[1] = scanner->operands[0];
+ scanner->operands[0] = child;
+ }
+}
+
+static void
+vine_to_tree(ir_expression *root, unsigned size)
+{
+ int n = size - 1;
+ for (int m = n / 2; m > 0; m = n / 2) {
+ compression(root, m);
+ n -= m + 1;
+ }
+}
+
+namespace {
+
+class ir_rebalance_visitor : public ir_rvalue_enter_visitor {
+public:
+ ir_rebalance_visitor()
+ {
+ progress = false;
+ }
+
+ void handle_rvalue(ir_rvalue **rvalue);
+
+ bool progress;
+};
+
+struct is_reduction_data {
+ ir_expression_operation operation;
+ const glsl_type *type;
+ unsigned num_expr;
+ bool is_reduction;
+ bool contains_constant;
+};
+
+} /* anonymous namespace */
+
+static bool
+is_reduction_operation(ir_expression_operation operation)
+{
+ switch (operation) {
+ case ir_binop_add:
+ case ir_binop_mul:
+ case ir_binop_bit_and:
+ case ir_binop_bit_xor:
+ case ir_binop_bit_or:
+ case ir_binop_logic_and:
+ case ir_binop_logic_xor:
+ case ir_binop_logic_or:
+ case ir_binop_min:
+ case ir_binop_max:
+ return true;
+ default:
+ return false;
+ }
+}
+
+static void
+is_reduction(ir_instruction *ir, void *data)
+{
+ struct is_reduction_data *ird = (struct is_reduction_data *)data;
+ if (!ird->is_reduction)
+ return;
+
+ /* We don't want to balance a tree that contains multiple constants, since
+ * we'll be able to constant fold them if they're not in separate subtrees.
+ */
+ if (ir->as_constant()) {
+ if (ird->contains_constant) {
+ ird->is_reduction = false;
+ }
+ ird->contains_constant = true;
+ return;
+ }
+
+ ir_expression *expr = ir->as_expression();
+ if (!expr)
+ return;
+
+ /* Non-constant matrices might still contain constant vec4 that we can
+ * constant fold once split up. Handling matrices will need some more
+ * work.
+ */
+ if (expr->type->is_matrix()) {
+ ird->is_reduction = false;
+ return;
+ }
+
+ if (ird->type != NULL && ird->type != expr->type) {
+ ird->is_reduction = false;
+ return;
+ }
+ ird->type = expr->type;
+
+ ird->num_expr++;
+ if (is_reduction_operation(expr->operation)) {
+ if (ird->operation != 0 && ird->operation != expr->operation)
+ ird->is_reduction = false;
+ ird->operation = expr->operation;
+ } else {
+ ird->is_reduction = false;
+ }
+}
+
+static ir_rvalue *
+handle_expression(ir_expression *expr)
+{
+ struct is_reduction_data ird;
+ ird.operation = (ir_expression_operation)0;
+ ird.type = NULL;
+ ird.num_expr = 0;
+ ird.is_reduction = true;
+ ird.contains_constant = false;
+
+ visit_tree(expr, is_reduction, (void *)&ird);
+
+ if (ird.is_reduction && ird.num_expr > 2) {
+ ir_constant z = ir_constant(0.0f);
+ ir_expression pseudo_root = ir_expression(ir_binop_add, &z, expr);
+
+ unsigned size = tree_to_vine(&pseudo_root);
+ vine_to_tree(&pseudo_root, size);
+
+ expr = pseudo_root.operands[1]->as_expression();
+ }
+ return expr;
+}
+
+void
+ir_rebalance_visitor::handle_rvalue(ir_rvalue **rvalue)
+{
+ if (!*rvalue)
+ return;
+
+ ir_expression *expr = (*rvalue)->as_expression();
+ if (!expr || !is_reduction_operation(expr->operation))
+ return;
+
+ ir_rvalue *new_rvalue = handle_expression(expr);
+ if (new_rvalue == *rvalue)
+ return;
+
+ *rvalue = new_rvalue;
+ this->progress = true;
+}
+
+bool
+do_rebalance_tree(exec_list *instructions)
+{
+ ir_rebalance_visitor v;
+
+ v.run(instructions);
+
+ return v.progress;
+}
--
1.8.3.2
More information about the mesa-dev
mailing list