aboutsummaryrefslogtreecommitdiff
path: root/mesalib/src/glsl/lower_variable_index_to_cond_assign.cpp
diff options
context:
space:
mode:
authormarha <marha@users.sourceforge.net>2011-07-26 08:58:05 +0200
committermarha <marha@users.sourceforge.net>2011-07-26 08:58:05 +0200
commitece07bdeaa82ea59e74bcfc588bcabe5e47567c1 (patch)
tree661e19ae13980e19f6fe827be5322ca9e0f8e4d8 /mesalib/src/glsl/lower_variable_index_to_cond_assign.cpp
parent91e91b72f07f4e61db17ee86c6933a7217f0e25c (diff)
downloadvcxsrv-ece07bdeaa82ea59e74bcfc588bcabe5e47567c1.tar.gz
vcxsrv-ece07bdeaa82ea59e74bcfc588bcabe5e47567c1.tar.bz2
vcxsrv-ece07bdeaa82ea59e74bcfc588bcabe5e47567c1.zip
libX11 libXext mesa pixman xserver xkeyboard-config git update 26 July 2011
Diffstat (limited to 'mesalib/src/glsl/lower_variable_index_to_cond_assign.cpp')
-rw-r--r--mesalib/src/glsl/lower_variable_index_to_cond_assign.cpp309
1 files changed, 231 insertions, 78 deletions
diff --git a/mesalib/src/glsl/lower_variable_index_to_cond_assign.cpp b/mesalib/src/glsl/lower_variable_index_to_cond_assign.cpp
index 8eb1612f0..f8e4a1de4 100644
--- a/mesalib/src/glsl/lower_variable_index_to_cond_assign.cpp
+++ b/mesalib/src/glsl/lower_variable_index_to_cond_assign.cpp
@@ -29,6 +29,21 @@
*
* Pre-DX10 GPUs often don't have a native way to do this operation,
* and this works around that.
+ *
+ * The lowering process proceeds as follows. Each non-constant index
+ * found in an r-value is converted to a canonical form \c array[i]. Each
+ * element of the array is conditionally assigned to a temporary by comparing
+ * \c i to a constant index. This is done by cloning the canonical form and
+ * replacing all occurances of \c i with a constant. Each remaining occurance
+ * of the canonical form in the IR is replaced with a dereference of the
+ * temporary variable.
+ *
+ * L-values with non-constant indices are handled similarly. In this case,
+ * the RHS of the assignment is assigned to a temporary. The non-constant
+ * index is replace with the canonical form (just like for r-values). The
+ * temporary is conditionally assigned to each element of the canonical form
+ * by comparing \c i with each index. The same clone-and-replace scheme is
+ * used.
*/
#include "ir.h"
@@ -37,10 +52,140 @@
#include "glsl_types.h"
#include "main/macros.h"
+/**
+ * Generate a comparison value for a block of indices
+ *
+ * Lowering passes for non-constant indexing of arrays, matrices, or vectors
+ * can use this to generate blocks of index comparison values.
+ *
+ * \param instructions List where new instructions will be appended
+ * \param index \c ir_variable containing the desired index
+ * \param base Base value for this block of comparisons
+ * \param components Number of unique index values to compare. This must
+ * be on the range [1, 4].
+ * \param mem_ctx ralloc memory context to be used for all allocations.
+ *
+ * \returns
+ * An \c ir_rvalue that \b must be cloned for each use in conditional
+ * assignments, etc.
+ */
+ir_rvalue *
+compare_index_block(exec_list *instructions, ir_variable *index,
+ unsigned base, unsigned components, void *mem_ctx)
+{
+ ir_rvalue *broadcast_index = new(mem_ctx) ir_dereference_variable(index);
+
+ assert(index->type->is_scalar());
+ assert(index->type->base_type == GLSL_TYPE_INT);
+ assert(components >= 1 && components <= 4);
+
+ if (components > 1) {
+ const ir_swizzle_mask m = { 0, 0, 0, 0, components, false };
+ broadcast_index = new(mem_ctx) ir_swizzle(broadcast_index, m);
+ }
+
+ /* Compare the desired index value with the next block of four indices.
+ */
+ ir_constant_data test_indices_data;
+ memset(&test_indices_data, 0, sizeof(test_indices_data));
+ test_indices_data.i[0] = base;
+ test_indices_data.i[1] = base + 1;
+ test_indices_data.i[2] = base + 2;
+ test_indices_data.i[3] = base + 3;
+
+ ir_constant *const test_indices =
+ new(mem_ctx) ir_constant(broadcast_index->type,
+ &test_indices_data);
+
+ ir_rvalue *const condition_val =
+ new(mem_ctx) ir_expression(ir_binop_equal,
+ &glsl_type::bool_type[components - 1],
+ broadcast_index,
+ test_indices);
+
+ ir_variable *const condition =
+ new(mem_ctx) ir_variable(condition_val->type,
+ "dereference_condition",
+ ir_var_temporary);
+ instructions->push_tail(condition);
+
+ ir_rvalue *const cond_deref =
+ new(mem_ctx) ir_dereference_variable(condition);
+ instructions->push_tail(new(mem_ctx) ir_assignment(cond_deref, condition_val, 0));
+
+ return cond_deref;
+}
+
+static inline bool
+is_array_or_matrix(const ir_instruction *ir)
+{
+ return (ir->type->is_array() || ir->type->is_matrix());
+}
+
+/**
+ * Replace a dereference of a variable with a specified r-value
+ *
+ * Each time a dereference of the specified value is replaced, the r-value
+ * tree is cloned.
+ */
+class deref_replacer : public ir_rvalue_visitor {
+public:
+ deref_replacer(const ir_variable *variable_to_replace, ir_rvalue *value)
+ : variable_to_replace(variable_to_replace), value(value),
+ progress(false)
+ {
+ assert(this->variable_to_replace != NULL);
+ assert(this->value != NULL);
+ }
+
+ virtual void handle_rvalue(ir_rvalue **rvalue)
+ {
+ ir_dereference_variable *const dv = (*rvalue)->as_dereference_variable();
+
+ if ((dv != NULL) && (dv->var == this->variable_to_replace)) {
+ this->progress = true;
+ *rvalue = this->value->clone(ralloc_parent(*rvalue), NULL);
+ }
+ }
+
+ const ir_variable *variable_to_replace;
+ ir_rvalue *value;
+ bool progress;
+};
+
+/**
+ * Find a variable index dereference of an array in an rvalue tree
+ */
+class find_variable_index : public ir_hierarchical_visitor {
+public:
+ find_variable_index()
+ : deref(NULL)
+ {
+ /* empty */
+ }
+
+ virtual ir_visitor_status visit_enter(ir_dereference_array *ir)
+ {
+ if (is_array_or_matrix(ir->array)
+ && (ir->array_index->as_constant() == NULL)) {
+ this->deref = ir;
+ return visit_stop;
+ }
+
+ return visit_continue;
+ }
+
+ /**
+ * First array dereference found in the tree that has a non-constant index.
+ */
+ ir_dereference_array *deref;
+};
+
struct assignment_generator
{
ir_instruction* base_ir;
- ir_rvalue* array;
+ ir_dereference *rvalue;
+ ir_variable *old_index;
bool is_write;
unsigned int write_mask;
ir_variable* var;
@@ -55,18 +200,23 @@ struct assignment_generator
* underlying variable.
*/
void *mem_ctx = ralloc_parent(base_ir);
- ir_dereference *element =
- new(mem_ctx) ir_dereference_array(this->array->clone(mem_ctx, NULL),
- new(mem_ctx) ir_constant(i));
- ir_rvalue *variable = new(mem_ctx) ir_dereference_variable(this->var);
- ir_assignment *assignment;
- if (is_write) {
- assignment = new(mem_ctx) ir_assignment(element, variable, condition,
- write_mask);
- } else {
- assignment = new(mem_ctx) ir_assignment(variable, element, condition);
- }
+ /* Clone the old r-value in its entirety. Then replace any occurances of
+ * the old variable index with the new constant index.
+ */
+ ir_dereference *element = this->rvalue->clone(mem_ctx, NULL);
+ ir_constant *const index = new(mem_ctx) ir_constant(i);
+ deref_replacer r(this->old_index, index);
+ element->accept(&r);
+ assert(r.progress);
+
+ /* Generate a conditional assignment to (or from) the constant indexed
+ * array dereference.
+ */
+ ir_rvalue *variable = new(mem_ctx) ir_dereference_variable(this->var);
+ ir_assignment *const assignment = (is_write)
+ ? new(mem_ctx) ir_assignment(element, variable, condition, write_mask)
+ : new(mem_ctx) ir_assignment(variable, element, condition);
list->push_tail(assignment);
}
@@ -118,54 +268,17 @@ struct switch_generator
for (unsigned i = first; i < end; i += 4) {
const unsigned comps = MIN2(condition_components, end - i);
- ir_rvalue *broadcast_index =
- new(this->mem_ctx) ir_dereference_variable(index);
-
- if (comps) {
- const ir_swizzle_mask m = { 0, 0, 0, 0, comps, false };
- broadcast_index = new(this->mem_ctx) ir_swizzle(broadcast_index, m);
- }
-
- /* Compare the desired index value with the next block of four indices.
- */
- ir_constant_data test_indices_data;
- memset(&test_indices_data, 0, sizeof(test_indices_data));
- test_indices_data.i[0] = i;
- test_indices_data.i[1] = i + 1;
- test_indices_data.i[2] = i + 2;
- test_indices_data.i[3] = i + 3;
- ir_constant *const test_indices =
- new(this->mem_ctx) ir_constant(broadcast_index->type,
- &test_indices_data);
-
- ir_rvalue *const condition_val =
- new(this->mem_ctx) ir_expression(ir_binop_equal,
- &glsl_type::bool_type[comps - 1],
- broadcast_index,
- test_indices);
-
- ir_variable *const condition =
- new(this->mem_ctx) ir_variable(condition_val->type,
- "dereference_array_condition",
- ir_var_temporary);
- list->push_tail(condition);
-
ir_rvalue *const cond_deref =
- new(this->mem_ctx) ir_dereference_variable(condition);
- list->push_tail(new(this->mem_ctx) ir_assignment(cond_deref,
- condition_val, 0));
+ compare_index_block(list, index, i, comps, this->mem_ctx);
if (comps == 1) {
- ir_rvalue *const cond_deref =
- new(this->mem_ctx) ir_dereference_variable(condition);
-
- this->generator.generate(i, cond_deref, list);
+ this->generator.generate(i, cond_deref->clone(this->mem_ctx, NULL),
+ list);
} else {
for (unsigned j = 0; j < comps; j++) {
- ir_rvalue *const cond_deref =
- new(this->mem_ctx) ir_dereference_variable(condition);
ir_rvalue *const cond_swiz =
- new(this->mem_ctx) ir_swizzle(cond_deref, j, 0, 0, 0, 1);
+ new(this->mem_ctx) ir_swizzle(cond_deref->clone(this->mem_ctx, NULL),
+ j, 0, 0, 0, 1);
this->generator.generate(i + j, cond_swiz, list);
}
@@ -233,21 +346,18 @@ public:
bool lower_temps;
bool lower_uniforms;
- bool is_array_or_matrix(const ir_instruction *ir) const
- {
- return (ir->type->is_array() || ir->type->is_matrix());
- }
-
- bool needs_lowering(ir_dereference_array *deref) const
+ bool storage_type_needs_lowering(ir_dereference_array *deref) const
{
- if (deref == NULL || deref->array_index->as_constant()
- || !is_array_or_matrix(deref->array))
- return false;
-
- if (deref->array->ir_type == ir_type_constant)
+ /* If a variable isn't eventually the target of this dereference, then
+ * it must be a constant or some sort of anonymous temporary storage.
+ *
+ * FINISHME: Is this correct? Most drivers treat arrays of constants as
+ * FINISHME: uniforms. It seems like this should do the same.
+ */
+ const ir_variable *const var = deref->array->variable_referenced();
+ if (var == NULL)
return this->lower_temps;
- const ir_variable *const var = deref->array->variable_referenced();
switch (var->mode) {
case ir_var_auto:
case ir_var_temporary:
@@ -267,8 +377,18 @@ public:
return false;
}
+ bool needs_lowering(ir_dereference_array *deref) const
+ {
+ if (deref == NULL || deref->array_index->as_constant()
+ || !is_array_or_matrix(deref->array))
+ return false;
+
+ return this->storage_type_needs_lowering(deref);
+ }
+
ir_variable *convert_dereference_array(ir_dereference_array *orig_deref,
- ir_assignment* orig_assign)
+ ir_assignment* orig_assign,
+ ir_dereference *orig_base)
{
assert(is_array_or_matrix(orig_deref->array));
@@ -314,9 +434,12 @@ public:
new(mem_ctx) ir_assignment(lhs, orig_deref->array_index, NULL);
base_ir->insert_before(assign);
+ orig_deref->array_index = lhs->clone(mem_ctx, NULL);
+
assignment_generator ag;
- ag.array = orig_deref->array;
+ ag.rvalue = orig_base;
ag.base_ir = base_ir;
+ ag.old_index = index;
ag.var = var;
if (orig_assign) {
ag.is_write = true;
@@ -327,21 +450,40 @@ public:
switch_generator sg(ag, index, 4, 4);
- exec_list list;
- sg.generate(0, length, &list);
- base_ir->insert_before(&list);
+ /* If the original assignment has a condition, respect that original
+ * condition! This is acomplished by wrapping the new conditional
+ * assignments in an if-statement that uses the original condition.
+ */
+ if ((orig_assign != NULL) && (orig_assign->condition != NULL)) {
+ /* No need to clone the condition because the IR that it hangs on is
+ * going to be removed from the instruction sequence.
+ */
+ ir_if *if_stmt = new(mem_ctx) ir_if(orig_assign->condition);
+
+ sg.generate(0, length, &if_stmt->then_instructions);
+ base_ir->insert_before(if_stmt);
+ } else {
+ exec_list list;
+
+ sg.generate(0, length, &list);
+ base_ir->insert_before(&list);
+ }
return var;
}
virtual void handle_rvalue(ir_rvalue **pir)
{
+ if (this->in_assignee)
+ return;
+
if (!*pir)
return;
ir_dereference_array* orig_deref = (*pir)->as_dereference_array();
if (needs_lowering(orig_deref)) {
- ir_variable* var = convert_dereference_array(orig_deref, 0);
+ ir_variable *var =
+ convert_dereference_array(orig_deref, NULL, orig_deref);
assert(var);
*pir = new(ralloc_parent(base_ir)) ir_dereference_variable(var);
this->progress = true;
@@ -353,10 +495,11 @@ public:
{
ir_rvalue_visitor::visit_leave(ir);
- ir_dereference_array *orig_deref = ir->lhs->as_dereference_array();
+ find_variable_index f;
+ ir->lhs->accept(&f);
- if (needs_lowering(orig_deref)) {
- convert_dereference_array(orig_deref, ir);
+ if ((f.deref != NULL) && storage_type_needs_lowering(f.deref)) {
+ convert_dereference_array(f.deref, ir, ir->lhs);
ir->remove();
this->progress = true;
}
@@ -377,7 +520,17 @@ lower_variable_index_to_cond_assign(exec_list *instructions,
lower_temp,
lower_uniform);
- visit_list_elements(&v, instructions);
-
- return v.progress;
+ /* Continue lowering until no progress is made. If there are multiple
+ * levels of indirection (e.g., non-constant indexing of array elements and
+ * matrix columns of an array of matrix), each pass will only lower one
+ * level of indirection.
+ */
+ bool progress_ever = false;
+ do {
+ v.progress = false;
+ visit_list_elements(&v, instructions);
+ progress_ever = v.progress || progress_ever;
+ } while (v.progress);
+
+ return progress_ever;
}