glsl: Convert lower_variable_index_to_cond_assign to ir_builder

Signed-off-by: Ian Romanick <ian.d.romanick@intel.com>
Reviewed-by: Alejandro Piñeiro <apinheiro@igalia.com>
This commit is contained in:
Ian Romanick 2017-09-18 15:04:03 -05:00
parent eb58668525
commit 3cc997c7c8
3 changed files with 65 additions and 105 deletions

View File

@ -170,9 +170,11 @@ bool lower_blend_equation_advanced(gl_linked_shader *shader);
bool lower_subroutine(exec_list *instructions, struct _mesa_glsl_parse_state *state); bool lower_subroutine(exec_list *instructions, struct _mesa_glsl_parse_state *state);
void propagate_invariance(exec_list *instructions); void propagate_invariance(exec_list *instructions);
ir_variable *compare_index_block(exec_list *instructions, ir_variable *index, namespace ir_builder { class ir_factory; };
unsigned base, unsigned components,
void *mem_ctx); ir_variable *compare_index_block(ir_builder::ir_factory &body,
ir_variable *index,
unsigned base, unsigned components);
bool lower_64bit_integer_instructions(exec_list *instructions, bool lower_64bit_integer_instructions(exec_list *instructions,
unsigned what_to_lower); unsigned what_to_lower);

View File

@ -51,6 +51,10 @@
#include "ir_optimization.h" #include "ir_optimization.h"
#include "compiler/glsl_types.h" #include "compiler/glsl_types.h"
#include "main/macros.h" #include "main/macros.h"
#include "program/prog_instruction.h" /* For SWIZZLE_XXXX */
#include "ir_builder.h"
using namespace ir_builder;
/** /**
* Generate a comparison value for a block of indices * Generate a comparison value for a block of indices
@ -70,20 +74,17 @@
* must be dereferenced per use. * must be dereferenced per use.
*/ */
ir_variable * ir_variable *
compare_index_block(exec_list *instructions, ir_variable *index, compare_index_block(ir_factory &body, ir_variable *index,
unsigned base, unsigned components, void *mem_ctx) unsigned base, unsigned components)
{ {
ir_rvalue *broadcast_index = new(mem_ctx) ir_dereference_variable(index);
assert(index->type->is_scalar()); assert(index->type->is_scalar());
assert(index->type->base_type == GLSL_TYPE_INT || assert(index->type->base_type == GLSL_TYPE_INT ||
index->type->base_type == GLSL_TYPE_UINT); index->type->base_type == GLSL_TYPE_UINT);
assert(components >= 1 && components <= 4); assert(components >= 1 && components <= 4);
if (components > 1) { ir_rvalue *const broadcast_index = components > 1
const ir_swizzle_mask m = { 0, 0, 0, 0, components, false }; ? swizzle(index, SWIZZLE_XXXX, components)
broadcast_index = new(mem_ctx) ir_swizzle(broadcast_index, m); : operand(index).val;
}
/* Compare the desired index value with the next block of four indices. /* Compare the desired index value with the next block of four indices.
*/ */
@ -95,23 +96,14 @@ compare_index_block(exec_list *instructions, ir_variable *index,
test_indices_data.i[3] = base + 3; test_indices_data.i[3] = base + 3;
ir_constant *const test_indices = ir_constant *const test_indices =
new(mem_ctx) ir_constant(broadcast_index->type, &test_indices_data); new(body.mem_ctx) ir_constant(broadcast_index->type, &test_indices_data);
ir_rvalue *const condition_val = ir_rvalue *const condition_val = equal(broadcast_index, test_indices);
new(mem_ctx) ir_expression(ir_binop_equal,
glsl_type::bvec(components),
broadcast_index,
test_indices);
ir_variable *const condition = ir_variable *const condition = body.make_temp(condition_val->type,
new(mem_ctx) ir_variable(condition_val->type, "dereference_condition");
"dereference_condition",
ir_var_temporary);
instructions->push_tail(condition);
ir_rvalue *const cond_deref = body.emit(assign(condition, condition_val));
new(mem_ctx) ir_dereference_variable(condition);
instructions->push_tail(new(mem_ctx) ir_assignment(cond_deref, condition_val, 0));
return condition; return condition;
} }
@ -201,18 +193,13 @@ struct assignment_generator
{ {
} }
void generate(unsigned i, ir_rvalue* condition, exec_list *list) const void generate(unsigned i, ir_rvalue* condition, ir_factory &body) const
{ {
/* Just clone the rest of the deref chain when trying to get at the
* underlying variable.
*/
void *mem_ctx = ralloc_parent(base_ir);
/* Clone the old r-value in its entirety. Then replace any occurances of /* Clone the old r-value in its entirety. Then replace any occurances of
* the old variable index with the new constant index. * the old variable index with the new constant index.
*/ */
ir_dereference *element = this->rvalue->clone(mem_ctx, NULL); ir_dereference *element = this->rvalue->clone(body.mem_ctx, NULL);
ir_constant *const index = new(mem_ctx) ir_constant(i); ir_constant *const index = body.constant(i);
deref_replacer r(this->old_index, index); deref_replacer r(this->old_index, index);
element->accept(&r); element->accept(&r);
assert(r.progress); assert(r.progress);
@ -220,12 +207,11 @@ struct assignment_generator
/* Generate a conditional assignment to (or from) the constant indexed /* Generate a conditional assignment to (or from) the constant indexed
* array dereference. * array dereference.
*/ */
ir_rvalue *variable = new(mem_ctx) ir_dereference_variable(this->var);
ir_assignment *const assignment = (is_write) ir_assignment *const assignment = (is_write)
? new(mem_ctx) ir_assignment(element, variable, condition, write_mask) ? assign(element, this->var, condition, write_mask)
: new(mem_ctx) ir_assignment(variable, element, condition); : assign(this->var, element, condition);
list->push_tail(assignment); body.emit(assignment);
} }
}; };
@ -251,7 +237,7 @@ struct switch_generator
this->mem_ctx = ralloc_parent(index); this->mem_ctx = ralloc_parent(index);
} }
void linear_sequence(unsigned begin, unsigned end, exec_list *list) void linear_sequence(unsigned begin, unsigned end, ir_factory &body)
{ {
if (begin == end) if (begin == end)
return; return;
@ -266,7 +252,7 @@ struct switch_generator
*/ */
unsigned first; unsigned first;
if (!this->generator.is_write) { if (!this->generator.is_write) {
this->generator.generate(begin, 0, list); this->generator.generate(begin, 0, body);
first = begin + 1; first = begin + 1;
} else { } else {
first = begin; first = begin;
@ -274,62 +260,49 @@ struct switch_generator
for (unsigned i = first; i < end; i += 4) { for (unsigned i = first; i < end; i += 4) {
const unsigned comps = MIN2(condition_components, end - i); const unsigned comps = MIN2(condition_components, end - i);
ir_variable *const cond = compare_index_block(body, index, i, comps);
ir_variable *const cond =
compare_index_block(list, index, i, comps, this->mem_ctx);
if (comps == 1) { if (comps == 1) {
ir_rvalue *const cond_deref = this->generator.generate(i,
new(mem_ctx) ir_dereference_variable(cond); operand(cond).val,
body);
this->generator.generate(i, cond_deref, list);
} else { } else {
for (unsigned j = 0; j < comps; j++) { for (unsigned j = 0; j < comps; j++) {
ir_rvalue *const cond_deref = this->generator.generate(i + j,
new(mem_ctx) ir_dereference_variable(cond); swizzle(cond, j, 1),
ir_rvalue *const cond_swiz = body);
new(this->mem_ctx) ir_swizzle(cond_deref,
j, 0, 0, 0, 1);
this->generator.generate(i + j, cond_swiz, list);
} }
} }
} }
} }
void bisect(unsigned begin, unsigned end, exec_list *list) void bisect(unsigned begin, unsigned end, ir_factory &body)
{ {
unsigned middle = (begin + end) >> 1; unsigned middle = (begin + end) >> 1;
assert(index->type->is_integer()); assert(index->type->is_integer());
ir_constant *const middle_c = (index->type->base_type == GLSL_TYPE_UINT) ir_constant *const middle_c = (index->type->base_type == GLSL_TYPE_UINT)
? new(this->mem_ctx) ir_constant((unsigned)middle) ? new(body.mem_ctx) ir_constant((unsigned)middle)
: new(this->mem_ctx) ir_constant((int)middle); : new(body.mem_ctx) ir_constant((int)middle);
ir_if *if_less = new(body.mem_ctx) ir_if(less(this->index, middle_c));
ir_dereference_variable *deref = ir_factory then_body(&if_less->then_instructions, body.mem_ctx);
new(this->mem_ctx) ir_dereference_variable(this->index); ir_factory else_body(&if_less->else_instructions, body.mem_ctx);
generate(begin, middle, then_body);
generate(middle, end, else_body);
ir_expression *less = body.emit(if_less);
new(this->mem_ctx) ir_expression(ir_binop_less, glsl_type::bool_type,
deref, middle_c);
ir_if *if_less = new(this->mem_ctx) ir_if(less);
generate(begin, middle, &if_less->then_instructions);
generate(middle, end, &if_less->else_instructions);
list->push_tail(if_less);
} }
void generate(unsigned begin, unsigned end, exec_list *list) void generate(unsigned begin, unsigned end, ir_factory &body)
{ {
unsigned length = end - begin; unsigned length = end - begin;
if (length <= this->linear_sequence_max_length) if (length <= this->linear_sequence_max_length)
return linear_sequence(begin, end, list); return linear_sequence(begin, end, body);
else else
return bisect(begin, end, list); return bisect(begin, end, body);
} }
}; };
@ -457,14 +430,16 @@ public:
ir_assignment* orig_assign, ir_assignment* orig_assign,
ir_dereference *orig_base) ir_dereference *orig_base)
{ {
void *const mem_ctx = ralloc_parent(base_ir);
exec_list list;
ir_factory body(&list, mem_ctx);
assert(is_array_or_matrix(orig_deref->array)); assert(is_array_or_matrix(orig_deref->array));
const unsigned length = (orig_deref->array->type->is_array()) const unsigned length = (orig_deref->array->type->is_array())
? orig_deref->array->type->length ? orig_deref->array->type->length
: orig_deref->array->type->matrix_columns; : orig_deref->array->type->matrix_columns;
void *const mem_ctx = ralloc_parent(base_ir);
/* Temporary storage for either the result of the dereference of /* Temporary storage for either the result of the dereference of
* the array, or the RHS that's being assigned into the * the array, or the RHS that's being assigned into the
* dereference of the array. * dereference of the array.
@ -472,36 +447,22 @@ public:
ir_variable *var; ir_variable *var;
if (orig_assign) { if (orig_assign) {
var = new(mem_ctx) ir_variable(orig_assign->rhs->type, var = body.make_temp(orig_assign->rhs->type,
"dereference_array_value", "dereference_array_value");
ir_var_temporary);
base_ir->insert_before(var);
ir_dereference *lhs = new(mem_ctx) ir_dereference_variable(var); body.emit(assign(var, orig_assign->rhs));
ir_assignment *assign = new(mem_ctx) ir_assignment(lhs,
orig_assign->rhs,
NULL);
base_ir->insert_before(assign);
} else { } else {
var = new(mem_ctx) ir_variable(orig_deref->type, var = body.make_temp(orig_deref->type,
"dereference_array_value", "dereference_array_value");
ir_var_temporary);
base_ir->insert_before(var);
} }
/* Store the index to a temporary to avoid reusing its tree. */ /* Store the index to a temporary to avoid reusing its tree. */
ir_variable *index = ir_variable *index = body.make_temp(orig_deref->array_index->type,
new(mem_ctx) ir_variable(orig_deref->array_index->type, "dereference_array_index");
"dereference_array_index", ir_var_temporary);
base_ir->insert_before(index);
ir_dereference *lhs = new(mem_ctx) ir_dereference_variable(index); body.emit(assign(index, orig_deref->array_index));
ir_assignment *assign =
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); orig_deref->array_index = deref(index).val;
assignment_generator ag; assignment_generator ag;
ag.rvalue = orig_base; ag.rvalue = orig_base;
@ -526,16 +487,15 @@ public:
* going to be removed from the instruction sequence. * going to be removed from the instruction sequence.
*/ */
ir_if *if_stmt = new(mem_ctx) ir_if(orig_assign->condition); ir_if *if_stmt = new(mem_ctx) ir_if(orig_assign->condition);
ir_factory then_body(&if_stmt->then_instructions, body.mem_ctx);
sg.generate(0, length, &if_stmt->then_instructions); sg.generate(0, length, then_body);
base_ir->insert_before(if_stmt); body.emit(if_stmt);
} else { } else {
exec_list list; sg.generate(0, length, body);
sg.generate(0, length, &list);
base_ir->insert_before(&list);
} }
base_ir->insert_before(&list);
return var; return var;
} }

View File

@ -108,9 +108,7 @@ ir_vec_index_to_cond_assign_visitor::convert_vec_index_to_cond_assign(void *mem_
* in the vector. * in the vector.
*/ */
ir_variable *const cond = ir_variable *const cond =
compare_index_block(&list, index, 0, compare_index_block(body, index, 0, orig_vector->type->vector_elements);
orig_vector->type->vector_elements,
mem_ctx);
/* Generate a conditional move of each vector element to the temp. */ /* Generate a conditional move of each vector element to the temp. */
for (unsigned i = 0; i < orig_vector->type->vector_elements; i++) for (unsigned i = 0; i < orig_vector->type->vector_elements; i++)