glsl: remove the now unused GLSL IR loop unrolling code
This code was slow, buggy and hard to understand. All drivers have now switched to using the NIR unrolling code \o/ Reviewed-by: Emma Anholt <emma@anholt.net> Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/16366>
This commit is contained in:

committed by
Marge Bot

parent
26ff49038c
commit
5aec67a1e1
@@ -38,7 +38,6 @@
|
||||
#include "glsl_parser_extras.h"
|
||||
#include "glsl_parser.h"
|
||||
#include "ir_optimization.h"
|
||||
#include "loop_analysis.h"
|
||||
#include "builtin_functions.h"
|
||||
|
||||
/**
|
||||
@@ -2433,37 +2432,6 @@ do_common_optimization(exec_list *ir, bool linked,
|
||||
do_constant_propagation(ir);
|
||||
progress |= array_split;
|
||||
|
||||
if (options->MaxUnrollIterations) {
|
||||
loop_state *ls = analyze_loop_variables(ir);
|
||||
if (ls->loop_found) {
|
||||
bool loop_progress = unroll_loops(ir, ls, options);
|
||||
while (loop_progress) {
|
||||
loop_progress = false;
|
||||
loop_progress |= do_constant_propagation(ir);
|
||||
loop_progress |= do_if_simplification(ir);
|
||||
|
||||
/* Some drivers only call do_common_optimization() once rather
|
||||
* than in a loop. So we must call do_lower_jumps() after
|
||||
* unrolling a loop because for drivers that use LLVM validation
|
||||
* will fail if a jump is not the last instruction in the block.
|
||||
* For example the following will fail LLVM validation:
|
||||
*
|
||||
* (loop (
|
||||
* ...
|
||||
* break
|
||||
* (assign (x) (var_ref v124) (expression int + (var_ref v124)
|
||||
* (constant int (1)) ) )
|
||||
* ))
|
||||
*/
|
||||
loop_progress |= do_lower_jumps(ir, true, true,
|
||||
options->EmitNoMainReturn,
|
||||
options->EmitNoCont);
|
||||
}
|
||||
progress |= loop_progress;
|
||||
}
|
||||
delete ls;
|
||||
}
|
||||
|
||||
/* If an optimization pass fails to preserve the invariant flag, calling
|
||||
* the pass only once earlier may result in incorrect code generation. Always call
|
||||
* propagate_invariance() last to avoid this possibility.
|
||||
|
@@ -1,856 +0,0 @@
|
||||
/*
|
||||
* Copyright © 2010 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.
|
||||
*/
|
||||
|
||||
#include "compiler/glsl_types.h"
|
||||
#include "loop_analysis.h"
|
||||
#include "ir_hierarchical_visitor.h"
|
||||
|
||||
static void try_add_loop_terminator(loop_variable_state *ls, ir_if *ir);
|
||||
|
||||
static bool all_expression_operands_are_loop_constant(ir_rvalue *,
|
||||
hash_table *);
|
||||
|
||||
static ir_rvalue *get_basic_induction_increment(ir_assignment *, hash_table *);
|
||||
|
||||
/**
|
||||
* Find an initializer of a variable outside a loop
|
||||
*
|
||||
* Works backwards from the loop to find the pre-loop value of the variable.
|
||||
* This is used, for example, to find the initial value of loop induction
|
||||
* variables.
|
||||
*
|
||||
* \param loop Loop where \c var is an induction variable
|
||||
* \param var Variable whose initializer is to be found
|
||||
*
|
||||
* \return
|
||||
* The \c ir_rvalue assigned to the variable outside the loop. May return
|
||||
* \c NULL if no initializer can be found.
|
||||
*/
|
||||
static ir_rvalue *
|
||||
find_initial_value(ir_loop *loop, ir_variable *var)
|
||||
{
|
||||
for (exec_node *node = loop->prev; !node->is_head_sentinel();
|
||||
node = node->prev) {
|
||||
ir_instruction *ir = (ir_instruction *) node;
|
||||
|
||||
switch (ir->ir_type) {
|
||||
case ir_type_call:
|
||||
case ir_type_loop:
|
||||
case ir_type_loop_jump:
|
||||
case ir_type_return:
|
||||
case ir_type_if:
|
||||
return NULL;
|
||||
|
||||
case ir_type_function:
|
||||
case ir_type_function_signature:
|
||||
assert(!"Should not get here.");
|
||||
return NULL;
|
||||
|
||||
case ir_type_assignment: {
|
||||
ir_assignment *assign = ir->as_assignment();
|
||||
ir_variable *assignee = assign->lhs->whole_variable_referenced();
|
||||
|
||||
if (assignee == var)
|
||||
return assign->rhs;
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
static int
|
||||
calculate_iterations(ir_rvalue *from, ir_rvalue *to, ir_rvalue *increment,
|
||||
enum ir_expression_operation op, bool continue_from_then,
|
||||
bool swap_compare_operands, bool inc_before_terminator)
|
||||
{
|
||||
if (from == NULL || to == NULL || increment == NULL)
|
||||
return -1;
|
||||
|
||||
void *mem_ctx = ralloc_context(NULL);
|
||||
|
||||
ir_expression *const sub =
|
||||
new(mem_ctx) ir_expression(ir_binop_sub, from->type, to, from);
|
||||
|
||||
ir_expression *const div =
|
||||
new(mem_ctx) ir_expression(ir_binop_div, sub->type, sub, increment);
|
||||
|
||||
ir_constant *iter = div->constant_expression_value(mem_ctx);
|
||||
if (iter == NULL) {
|
||||
ralloc_free(mem_ctx);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (!iter->type->is_integer()) {
|
||||
const ir_expression_operation op = iter->type->is_double()
|
||||
? ir_unop_d2i : ir_unop_f2i;
|
||||
ir_rvalue *cast =
|
||||
new(mem_ctx) ir_expression(op, glsl_type::int_type, iter, NULL);
|
||||
|
||||
iter = cast->constant_expression_value(mem_ctx);
|
||||
}
|
||||
|
||||
int64_t iter_value = iter->get_int64_component(0);
|
||||
|
||||
/* Code after this block works under assumption that iterator will be
|
||||
* incremented or decremented until it hits the limit,
|
||||
* however the loop condition can be false on the first iteration.
|
||||
* Handle such loops first.
|
||||
*/
|
||||
{
|
||||
ir_rvalue *first_value = from;
|
||||
if (inc_before_terminator) {
|
||||
first_value =
|
||||
new(mem_ctx) ir_expression(ir_binop_add, from->type, from, increment);
|
||||
}
|
||||
|
||||
ir_expression *cmp = swap_compare_operands
|
||||
? new(mem_ctx) ir_expression(op, glsl_type::bool_type, to, first_value)
|
||||
: new(mem_ctx) ir_expression(op, glsl_type::bool_type, first_value, to);
|
||||
if (continue_from_then)
|
||||
cmp = new(mem_ctx) ir_expression(ir_unop_logic_not, cmp);
|
||||
|
||||
ir_constant *const cmp_result = cmp->constant_expression_value(mem_ctx);
|
||||
assert(cmp_result != NULL);
|
||||
if (cmp_result->get_bool_component(0)) {
|
||||
ralloc_free(mem_ctx);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
/* Make sure that the calculated number of iterations satisfies the exit
|
||||
* condition. This is needed to catch off-by-one errors and some types of
|
||||
* ill-formed loops. For example, we need to detect that the following
|
||||
* loop does not have a maximum iteration count.
|
||||
*
|
||||
* for (float x = 0.0; x != 0.9; x += 0.2)
|
||||
* ;
|
||||
*/
|
||||
const int bias[] = { -1, 0, 1 };
|
||||
bool valid_loop = false;
|
||||
|
||||
for (unsigned i = 0; i < ARRAY_SIZE(bias); i++) {
|
||||
/* Increment may be of type int, uint or float. */
|
||||
switch (increment->type->base_type) {
|
||||
case GLSL_TYPE_INT:
|
||||
iter = new(mem_ctx) ir_constant(int32_t(iter_value + bias[i]));
|
||||
break;
|
||||
case GLSL_TYPE_INT16:
|
||||
iter = new(mem_ctx) ir_constant(int16_t(iter_value + bias[i]));
|
||||
break;
|
||||
case GLSL_TYPE_INT64:
|
||||
iter = new(mem_ctx) ir_constant(int64_t(iter_value + bias[i]));
|
||||
break;
|
||||
case GLSL_TYPE_UINT:
|
||||
iter = new(mem_ctx) ir_constant(unsigned(iter_value + bias[i]));
|
||||
break;
|
||||
case GLSL_TYPE_UINT16:
|
||||
iter = new(mem_ctx) ir_constant(uint16_t(iter_value + bias[i]));
|
||||
break;
|
||||
case GLSL_TYPE_UINT64:
|
||||
iter = new(mem_ctx) ir_constant(uint64_t(iter_value + bias[i]));
|
||||
break;
|
||||
case GLSL_TYPE_FLOAT:
|
||||
iter = new(mem_ctx) ir_constant(float(iter_value + bias[i]));
|
||||
break;
|
||||
case GLSL_TYPE_FLOAT16:
|
||||
iter = new(mem_ctx) ir_constant(float16_t(float(iter_value + bias[i])));
|
||||
break;
|
||||
case GLSL_TYPE_DOUBLE:
|
||||
iter = new(mem_ctx) ir_constant(double(iter_value + bias[i]));
|
||||
break;
|
||||
default:
|
||||
unreachable("Unsupported type for loop iterator.");
|
||||
}
|
||||
|
||||
ir_expression *const mul =
|
||||
new(mem_ctx) ir_expression(ir_binop_mul, increment->type, iter,
|
||||
increment);
|
||||
|
||||
ir_expression *const add =
|
||||
new(mem_ctx) ir_expression(ir_binop_add, mul->type, mul, from);
|
||||
|
||||
ir_expression *cmp = swap_compare_operands
|
||||
? new(mem_ctx) ir_expression(op, glsl_type::bool_type, to, add)
|
||||
: new(mem_ctx) ir_expression(op, glsl_type::bool_type, add, to);
|
||||
if (continue_from_then)
|
||||
cmp = new(mem_ctx) ir_expression(ir_unop_logic_not, cmp);
|
||||
|
||||
ir_constant *const cmp_result = cmp->constant_expression_value(mem_ctx);
|
||||
|
||||
assert(cmp_result != NULL);
|
||||
if (cmp_result->get_bool_component(0)) {
|
||||
iter_value += bias[i];
|
||||
valid_loop = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
ralloc_free(mem_ctx);
|
||||
|
||||
if (inc_before_terminator) {
|
||||
iter_value--;
|
||||
}
|
||||
|
||||
return (valid_loop) ? iter_value : -1;
|
||||
}
|
||||
|
||||
static bool
|
||||
incremented_before_terminator(ir_loop *loop, ir_variable *var,
|
||||
ir_if *terminator)
|
||||
{
|
||||
for (exec_node *node = loop->body_instructions.get_head();
|
||||
!node->is_tail_sentinel();
|
||||
node = node->get_next()) {
|
||||
ir_instruction *ir = (ir_instruction *) node;
|
||||
|
||||
switch (ir->ir_type) {
|
||||
case ir_type_if:
|
||||
if (ir->as_if() == terminator)
|
||||
return false;
|
||||
break;
|
||||
|
||||
case ir_type_assignment: {
|
||||
ir_assignment *assign = ir->as_assignment();
|
||||
ir_variable *assignee = assign->lhs->whole_variable_referenced();
|
||||
|
||||
if (assignee == var) {
|
||||
return true;
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
unreachable("Unable to find induction variable");
|
||||
}
|
||||
|
||||
/**
|
||||
* Record the fact that the given loop variable was referenced inside the loop.
|
||||
*
|
||||
* \arg in_assignee is true if the reference was on the LHS of an assignment.
|
||||
*
|
||||
* \arg in_conditional_code_or_nested_loop is true if the reference occurred
|
||||
* inside an if statement or a nested loop.
|
||||
*
|
||||
* \arg current_assignment is the ir_assignment node that the loop variable is
|
||||
* on the LHS of, if any (ignored if \c in_assignee is false).
|
||||
*/
|
||||
void
|
||||
loop_variable::record_reference(bool in_assignee,
|
||||
bool in_conditional_code_or_nested_loop,
|
||||
ir_assignment *current_assignment)
|
||||
{
|
||||
if (in_assignee) {
|
||||
assert(current_assignment != NULL);
|
||||
|
||||
if (in_conditional_code_or_nested_loop) {
|
||||
this->conditional_or_nested_assignment = true;
|
||||
}
|
||||
|
||||
if (this->first_assignment == NULL) {
|
||||
assert(this->num_assignments == 0);
|
||||
|
||||
this->first_assignment = current_assignment;
|
||||
}
|
||||
|
||||
this->num_assignments++;
|
||||
} else if (this->first_assignment == current_assignment) {
|
||||
/* This catches the case where the variable is used in the RHS of an
|
||||
* assignment where it is also in the LHS.
|
||||
*/
|
||||
this->read_before_write = true;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
loop_state::loop_state()
|
||||
{
|
||||
this->ht = _mesa_pointer_hash_table_create(NULL);
|
||||
this->mem_ctx = ralloc_context(NULL);
|
||||
this->loop_found = false;
|
||||
}
|
||||
|
||||
|
||||
loop_state::~loop_state()
|
||||
{
|
||||
_mesa_hash_table_destroy(this->ht, NULL);
|
||||
ralloc_free(this->mem_ctx);
|
||||
}
|
||||
|
||||
|
||||
loop_variable_state *
|
||||
loop_state::insert(ir_loop *ir)
|
||||
{
|
||||
loop_variable_state *ls = new(this->mem_ctx) loop_variable_state;
|
||||
|
||||
_mesa_hash_table_insert(this->ht, ir, ls);
|
||||
this->loop_found = true;
|
||||
|
||||
return ls;
|
||||
}
|
||||
|
||||
|
||||
loop_variable_state *
|
||||
loop_state::get(const ir_loop *ir)
|
||||
{
|
||||
hash_entry *entry = _mesa_hash_table_search(this->ht, ir);
|
||||
return entry ? (loop_variable_state *) entry->data : NULL;
|
||||
}
|
||||
|
||||
|
||||
loop_variable *
|
||||
loop_variable_state::get(const ir_variable *ir)
|
||||
{
|
||||
if (ir == NULL)
|
||||
return NULL;
|
||||
|
||||
hash_entry *entry = _mesa_hash_table_search(this->var_hash, ir);
|
||||
return entry ? (loop_variable *) entry->data : NULL;
|
||||
}
|
||||
|
||||
|
||||
loop_variable *
|
||||
loop_variable_state::insert(ir_variable *var)
|
||||
{
|
||||
void *mem_ctx = ralloc_parent(this);
|
||||
loop_variable *lv = rzalloc(mem_ctx, loop_variable);
|
||||
|
||||
lv->var = var;
|
||||
|
||||
_mesa_hash_table_insert(this->var_hash, lv->var, lv);
|
||||
this->variables.push_tail(lv);
|
||||
|
||||
return lv;
|
||||
}
|
||||
|
||||
|
||||
loop_terminator *
|
||||
loop_variable_state::insert(ir_if *if_stmt, bool continue_from_then)
|
||||
{
|
||||
void *mem_ctx = ralloc_parent(this);
|
||||
loop_terminator *t = new(mem_ctx) loop_terminator(if_stmt,
|
||||
continue_from_then);
|
||||
|
||||
this->terminators.push_tail(t);
|
||||
|
||||
return t;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* If the given variable already is recorded in the state for this loop,
|
||||
* return the corresponding loop_variable object that records information
|
||||
* about it.
|
||||
*
|
||||
* Otherwise, create a new loop_variable object to record information about
|
||||
* the variable, and set its \c read_before_write field appropriately based on
|
||||
* \c in_assignee.
|
||||
*
|
||||
* \arg in_assignee is true if this variable was encountered on the LHS of an
|
||||
* assignment.
|
||||
*/
|
||||
loop_variable *
|
||||
loop_variable_state::get_or_insert(ir_variable *var, bool in_assignee)
|
||||
{
|
||||
loop_variable *lv = this->get(var);
|
||||
|
||||
if (lv == NULL) {
|
||||
lv = this->insert(var);
|
||||
lv->read_before_write = !in_assignee;
|
||||
}
|
||||
|
||||
return lv;
|
||||
}
|
||||
|
||||
|
||||
namespace {
|
||||
|
||||
class loop_analysis : public ir_hierarchical_visitor {
|
||||
public:
|
||||
loop_analysis(loop_state *loops);
|
||||
|
||||
virtual ir_visitor_status visit(ir_loop_jump *);
|
||||
virtual ir_visitor_status visit(ir_dereference_variable *);
|
||||
|
||||
virtual ir_visitor_status visit_enter(ir_call *);
|
||||
|
||||
virtual ir_visitor_status visit_enter(ir_loop *);
|
||||
virtual ir_visitor_status visit_leave(ir_loop *);
|
||||
virtual ir_visitor_status visit_enter(ir_assignment *);
|
||||
virtual ir_visitor_status visit_leave(ir_assignment *);
|
||||
virtual ir_visitor_status visit_enter(ir_if *);
|
||||
virtual ir_visitor_status visit_leave(ir_if *);
|
||||
|
||||
loop_state *loops;
|
||||
|
||||
int if_statement_depth;
|
||||
|
||||
ir_assignment *current_assignment;
|
||||
|
||||
exec_list state;
|
||||
};
|
||||
|
||||
} /* anonymous namespace */
|
||||
|
||||
loop_analysis::loop_analysis(loop_state *loops)
|
||||
: loops(loops), if_statement_depth(0), current_assignment(NULL)
|
||||
{
|
||||
/* empty */
|
||||
}
|
||||
|
||||
|
||||
ir_visitor_status
|
||||
loop_analysis::visit(ir_loop_jump *ir)
|
||||
{
|
||||
(void) ir;
|
||||
|
||||
assert(!this->state.is_empty());
|
||||
|
||||
loop_variable_state *const ls =
|
||||
(loop_variable_state *) this->state.get_head();
|
||||
|
||||
ls->num_loop_jumps++;
|
||||
|
||||
return visit_continue;
|
||||
}
|
||||
|
||||
|
||||
ir_visitor_status
|
||||
loop_analysis::visit_enter(ir_call *)
|
||||
{
|
||||
/* Mark every loop that we're currently analyzing as containing an ir_call
|
||||
* (even those at outer nesting levels).
|
||||
*/
|
||||
foreach_in_list(loop_variable_state, ls, &this->state) {
|
||||
ls->contains_calls = true;
|
||||
}
|
||||
|
||||
return visit_continue_with_parent;
|
||||
}
|
||||
|
||||
|
||||
ir_visitor_status
|
||||
loop_analysis::visit(ir_dereference_variable *ir)
|
||||
{
|
||||
/* If we're not somewhere inside a loop, there's nothing to do.
|
||||
*/
|
||||
if (this->state.is_empty())
|
||||
return visit_continue;
|
||||
|
||||
bool nested = false;
|
||||
|
||||
foreach_in_list(loop_variable_state, ls, &this->state) {
|
||||
ir_variable *var = ir->variable_referenced();
|
||||
loop_variable *lv = ls->get_or_insert(var, this->in_assignee);
|
||||
|
||||
lv->record_reference(this->in_assignee,
|
||||
nested || this->if_statement_depth > 0,
|
||||
this->current_assignment);
|
||||
nested = true;
|
||||
}
|
||||
|
||||
return visit_continue;
|
||||
}
|
||||
|
||||
ir_visitor_status
|
||||
loop_analysis::visit_enter(ir_loop *ir)
|
||||
{
|
||||
loop_variable_state *ls = this->loops->insert(ir);
|
||||
this->state.push_head(ls);
|
||||
|
||||
return visit_continue;
|
||||
}
|
||||
|
||||
ir_visitor_status
|
||||
loop_analysis::visit_leave(ir_loop *ir)
|
||||
{
|
||||
loop_variable_state *const ls =
|
||||
(loop_variable_state *) this->state.pop_head();
|
||||
|
||||
/* Function calls may contain side effects. These could alter any of our
|
||||
* variables in ways that cannot be known, and may even terminate shader
|
||||
* execution (say, calling discard in the fragment shader). So we can't
|
||||
* rely on any of our analysis about assignments to variables.
|
||||
*
|
||||
* We could perform some conservative analysis (prove there's no statically
|
||||
* possible assignment, etc.) but it isn't worth it for now; function
|
||||
* inlining will allow us to unroll loops anyway.
|
||||
*/
|
||||
if (ls->contains_calls)
|
||||
return visit_continue;
|
||||
|
||||
foreach_in_list(ir_instruction, node, &ir->body_instructions) {
|
||||
/* Skip over declarations at the start of a loop.
|
||||
*/
|
||||
if (node->as_variable())
|
||||
continue;
|
||||
|
||||
ir_if *if_stmt = ((ir_instruction *) node)->as_if();
|
||||
|
||||
if (if_stmt != NULL)
|
||||
try_add_loop_terminator(ls, if_stmt);
|
||||
}
|
||||
|
||||
|
||||
foreach_in_list_safe(loop_variable, lv, &ls->variables) {
|
||||
/* Move variables that are already marked as being loop constant to
|
||||
* a separate list. These trivially don't need to be tested.
|
||||
*/
|
||||
if (lv->is_loop_constant()) {
|
||||
lv->remove();
|
||||
ls->constants.push_tail(lv);
|
||||
}
|
||||
}
|
||||
|
||||
/* Each variable assigned in the loop that isn't already marked as being loop
|
||||
* constant might still be loop constant. The requirements at this point
|
||||
* are:
|
||||
*
|
||||
* - Variable is written before it is read.
|
||||
*
|
||||
* - Only one assignment to the variable.
|
||||
*
|
||||
* - All operands on the RHS of the assignment are also loop constants.
|
||||
*
|
||||
* The last requirement is the reason for the progress loop. A variable
|
||||
* marked as a loop constant on one pass may allow other variables to be
|
||||
* marked as loop constant on following passes.
|
||||
*/
|
||||
bool progress;
|
||||
do {
|
||||
progress = false;
|
||||
|
||||
foreach_in_list_safe(loop_variable, lv, &ls->variables) {
|
||||
if (lv->conditional_or_nested_assignment || (lv->num_assignments > 1))
|
||||
continue;
|
||||
|
||||
/* Process the RHS of the assignment. If all of the variables
|
||||
* accessed there are loop constants, then add this
|
||||
*/
|
||||
ir_rvalue *const rhs = lv->first_assignment->rhs;
|
||||
if (all_expression_operands_are_loop_constant(rhs, ls->var_hash)) {
|
||||
lv->rhs_clean = true;
|
||||
|
||||
if (lv->is_loop_constant()) {
|
||||
progress = true;
|
||||
|
||||
lv->remove();
|
||||
ls->constants.push_tail(lv);
|
||||
}
|
||||
}
|
||||
}
|
||||
} while (progress);
|
||||
|
||||
/* The remaining variables that are not loop invariant might be loop
|
||||
* induction variables.
|
||||
*/
|
||||
foreach_in_list_safe(loop_variable, lv, &ls->variables) {
|
||||
/* If there is more than one assignment to a variable, it cannot be a
|
||||
* loop induction variable. This isn't strictly true, but this is a
|
||||
* very simple induction variable detector, and it can't handle more
|
||||
* complex cases.
|
||||
*/
|
||||
if (lv->num_assignments > 1)
|
||||
continue;
|
||||
|
||||
/* All of the variables with zero assignments in the loop are loop
|
||||
* invariant, and they should have already been filtered out.
|
||||
*/
|
||||
assert(lv->num_assignments == 1);
|
||||
assert(lv->first_assignment != NULL);
|
||||
|
||||
/* The assignment to the variable in the loop must be unconditional and
|
||||
* not inside a nested loop.
|
||||
*/
|
||||
if (lv->conditional_or_nested_assignment)
|
||||
continue;
|
||||
|
||||
/* Basic loop induction variables have a single assignment in the loop
|
||||
* that has the form 'VAR = VAR + i' or 'VAR = VAR - i' where i is a
|
||||
* loop invariant.
|
||||
*/
|
||||
ir_rvalue *const inc =
|
||||
get_basic_induction_increment(lv->first_assignment, ls->var_hash);
|
||||
if (inc != NULL) {
|
||||
lv->increment = inc;
|
||||
|
||||
lv->remove();
|
||||
ls->induction_variables.push_tail(lv);
|
||||
}
|
||||
}
|
||||
|
||||
/* Search the loop terminating conditions for those of the form 'i < c'
|
||||
* where i is a loop induction variable, c is a constant, and < is any
|
||||
* relative operator. From each of these we can infer an iteration count.
|
||||
* Also figure out which terminator (if any) produces the smallest
|
||||
* iteration count--this is the limiting terminator.
|
||||
*/
|
||||
foreach_in_list(loop_terminator, t, &ls->terminators) {
|
||||
ir_if *if_stmt = t->ir;
|
||||
|
||||
/* If-statements can be either 'if (expr)' or 'if (deref)'. We only care
|
||||
* about the former here.
|
||||
*/
|
||||
ir_expression *cond = if_stmt->condition->as_expression();
|
||||
if (cond == NULL)
|
||||
continue;
|
||||
|
||||
switch (cond->operation) {
|
||||
case ir_binop_less:
|
||||
case ir_binop_gequal: {
|
||||
/* The expressions that we care about will either be of the form
|
||||
* 'counter < limit' or 'limit < counter'. Figure out which is
|
||||
* which.
|
||||
*/
|
||||
ir_rvalue *counter = cond->operands[0]->as_dereference_variable();
|
||||
ir_constant *limit = cond->operands[1]->as_constant();
|
||||
enum ir_expression_operation cmp = cond->operation;
|
||||
bool swap_compare_operands = false;
|
||||
|
||||
if (limit == NULL) {
|
||||
counter = cond->operands[1]->as_dereference_variable();
|
||||
limit = cond->operands[0]->as_constant();
|
||||
swap_compare_operands = true;
|
||||
}
|
||||
|
||||
if ((counter == NULL) || (limit == NULL))
|
||||
break;
|
||||
|
||||
ir_variable *var = counter->variable_referenced();
|
||||
|
||||
ir_rvalue *init = find_initial_value(ir, var);
|
||||
|
||||
loop_variable *lv = ls->get(var);
|
||||
if (lv != NULL && lv->is_induction_var()) {
|
||||
bool inc_before_terminator =
|
||||
incremented_before_terminator(ir, var, t->ir);
|
||||
|
||||
t->iterations = calculate_iterations(init, limit, lv->increment,
|
||||
cmp, t->continue_from_then,
|
||||
swap_compare_operands,
|
||||
inc_before_terminator);
|
||||
|
||||
if (t->iterations >= 0 &&
|
||||
(ls->limiting_terminator == NULL ||
|
||||
t->iterations < ls->limiting_terminator->iterations)) {
|
||||
ls->limiting_terminator = t;
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return visit_continue;
|
||||
}
|
||||
|
||||
ir_visitor_status
|
||||
loop_analysis::visit_enter(ir_if *ir)
|
||||
{
|
||||
(void) ir;
|
||||
|
||||
if (!this->state.is_empty())
|
||||
this->if_statement_depth++;
|
||||
|
||||
return visit_continue;
|
||||
}
|
||||
|
||||
ir_visitor_status
|
||||
loop_analysis::visit_leave(ir_if *ir)
|
||||
{
|
||||
(void) ir;
|
||||
|
||||
if (!this->state.is_empty())
|
||||
this->if_statement_depth--;
|
||||
|
||||
return visit_continue;
|
||||
}
|
||||
|
||||
ir_visitor_status
|
||||
loop_analysis::visit_enter(ir_assignment *ir)
|
||||
{
|
||||
/* If we're not somewhere inside a loop, there's nothing to do.
|
||||
*/
|
||||
if (this->state.is_empty())
|
||||
return visit_continue_with_parent;
|
||||
|
||||
this->current_assignment = ir;
|
||||
|
||||
return visit_continue;
|
||||
}
|
||||
|
||||
ir_visitor_status
|
||||
loop_analysis::visit_leave(ir_assignment *ir)
|
||||
{
|
||||
/* Since the visit_enter exits with visit_continue_with_parent for this
|
||||
* case, the loop state stack should never be empty here.
|
||||
*/
|
||||
assert(!this->state.is_empty());
|
||||
|
||||
assert(this->current_assignment == ir);
|
||||
this->current_assignment = NULL;
|
||||
|
||||
return visit_continue;
|
||||
}
|
||||
|
||||
|
||||
class examine_rhs : public ir_hierarchical_visitor {
|
||||
public:
|
||||
examine_rhs(hash_table *loop_variables)
|
||||
{
|
||||
this->only_uses_loop_constants = true;
|
||||
this->loop_variables = loop_variables;
|
||||
}
|
||||
|
||||
virtual ir_visitor_status visit(ir_dereference_variable *ir)
|
||||
{
|
||||
hash_entry *entry = _mesa_hash_table_search(this->loop_variables,
|
||||
ir->var);
|
||||
loop_variable *lv = entry ? (loop_variable *) entry->data : NULL;
|
||||
|
||||
assert(lv != NULL);
|
||||
|
||||
if (lv->is_loop_constant()) {
|
||||
return visit_continue;
|
||||
} else {
|
||||
this->only_uses_loop_constants = false;
|
||||
return visit_stop;
|
||||
}
|
||||
}
|
||||
|
||||
hash_table *loop_variables;
|
||||
bool only_uses_loop_constants;
|
||||
};
|
||||
|
||||
|
||||
bool
|
||||
all_expression_operands_are_loop_constant(ir_rvalue *ir, hash_table *variables)
|
||||
{
|
||||
examine_rhs v(variables);
|
||||
|
||||
ir->accept(&v);
|
||||
|
||||
return v.only_uses_loop_constants;
|
||||
}
|
||||
|
||||
|
||||
ir_rvalue *
|
||||
get_basic_induction_increment(ir_assignment *ir, hash_table *var_hash)
|
||||
{
|
||||
/* The RHS must be a binary expression.
|
||||
*/
|
||||
ir_expression *const rhs = ir->rhs->as_expression();
|
||||
if ((rhs == NULL)
|
||||
|| ((rhs->operation != ir_binop_add)
|
||||
&& (rhs->operation != ir_binop_sub)))
|
||||
return NULL;
|
||||
|
||||
/* One of the of operands of the expression must be the variable assigned.
|
||||
* If the operation is subtraction, the variable in question must be the
|
||||
* "left" operand.
|
||||
*/
|
||||
ir_variable *const var = ir->lhs->variable_referenced();
|
||||
|
||||
ir_variable *const op0 = rhs->operands[0]->variable_referenced();
|
||||
ir_variable *const op1 = rhs->operands[1]->variable_referenced();
|
||||
|
||||
if (((op0 != var) && (op1 != var))
|
||||
|| ((op1 == var) && (rhs->operation == ir_binop_sub)))
|
||||
return NULL;
|
||||
|
||||
ir_rvalue *inc = (op0 == var) ? rhs->operands[1] : rhs->operands[0];
|
||||
|
||||
if (inc->as_constant() == NULL) {
|
||||
ir_variable *const inc_var = inc->variable_referenced();
|
||||
if (inc_var != NULL) {
|
||||
hash_entry *entry = _mesa_hash_table_search(var_hash, inc_var);
|
||||
loop_variable *lv = entry ? (loop_variable *) entry->data : NULL;
|
||||
|
||||
if (lv == NULL || !lv->is_loop_constant()) {
|
||||
assert(lv != NULL);
|
||||
inc = NULL;
|
||||
}
|
||||
} else
|
||||
inc = NULL;
|
||||
}
|
||||
|
||||
if ((inc != NULL) && (rhs->operation == ir_binop_sub)) {
|
||||
void *mem_ctx = ralloc_parent(ir);
|
||||
|
||||
inc = new(mem_ctx) ir_expression(ir_unop_neg,
|
||||
inc->type,
|
||||
inc->clone(mem_ctx, NULL),
|
||||
NULL);
|
||||
}
|
||||
|
||||
return inc;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Detect whether an if-statement is a loop terminating condition, if so
|
||||
* add it to the list of loop terminators.
|
||||
*
|
||||
* Detects if-statements of the form
|
||||
*
|
||||
* (if (expression bool ...) (...then_instrs...break))
|
||||
*
|
||||
* or
|
||||
*
|
||||
* (if (expression bool ...) ... (...else_instrs...break))
|
||||
*/
|
||||
void
|
||||
try_add_loop_terminator(loop_variable_state *ls, ir_if *ir)
|
||||
{
|
||||
ir_instruction *inst = (ir_instruction *) ir->then_instructions.get_tail();
|
||||
ir_instruction *else_inst =
|
||||
(ir_instruction *) ir->else_instructions.get_tail();
|
||||
|
||||
if (is_break(inst) || is_break(else_inst))
|
||||
ls->insert(ir, is_break(else_inst));
|
||||
}
|
||||
|
||||
|
||||
loop_state *
|
||||
analyze_loop_variables(exec_list *instructions)
|
||||
{
|
||||
loop_state *loops = new loop_state;
|
||||
loop_analysis v(loops);
|
||||
|
||||
v.run(instructions);
|
||||
return v.loops;
|
||||
}
|
@@ -1,244 +0,0 @@
|
||||
/* -*- c++ -*- */
|
||||
/*
|
||||
* Copyright © 2010 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.
|
||||
*/
|
||||
|
||||
#ifndef LOOP_ANALYSIS_H
|
||||
#define LOOP_ANALYSIS_H
|
||||
|
||||
#include "ir.h"
|
||||
#include "util/hash_table.h"
|
||||
|
||||
/**
|
||||
* Analyze and classify all variables used in all loops in the instruction list
|
||||
*/
|
||||
extern class loop_state *
|
||||
analyze_loop_variables(exec_list *instructions);
|
||||
|
||||
static inline bool
|
||||
is_break(ir_instruction *ir)
|
||||
{
|
||||
return ir != NULL && ir->ir_type == ir_type_loop_jump &&
|
||||
((ir_loop_jump *) ir)->is_break();
|
||||
}
|
||||
|
||||
|
||||
extern bool
|
||||
unroll_loops(exec_list *instructions, loop_state *ls,
|
||||
const struct gl_shader_compiler_options *options);
|
||||
|
||||
|
||||
/**
|
||||
* Tracking for all variables used in a loop
|
||||
*/
|
||||
class loop_variable_state : public exec_node {
|
||||
public:
|
||||
class loop_variable *get(const ir_variable *);
|
||||
class loop_variable *insert(ir_variable *);
|
||||
class loop_variable *get_or_insert(ir_variable *, bool in_assignee);
|
||||
class loop_terminator *insert(ir_if *, bool continue_from_then);
|
||||
|
||||
|
||||
/**
|
||||
* Variables that have not yet been classified
|
||||
*/
|
||||
exec_list variables;
|
||||
|
||||
/**
|
||||
* Variables whose values are constant within the body of the loop
|
||||
*
|
||||
* This list contains \c loop_variable objects.
|
||||
*/
|
||||
exec_list constants;
|
||||
|
||||
/**
|
||||
* Induction variables for this loop
|
||||
*
|
||||
* This list contains \c loop_variable objects.
|
||||
*/
|
||||
exec_list induction_variables;
|
||||
|
||||
/**
|
||||
* Simple if-statements that lead to the termination of the loop
|
||||
*
|
||||
* This list contains \c loop_terminator objects.
|
||||
*
|
||||
* \sa is_loop_terminator
|
||||
*/
|
||||
exec_list terminators;
|
||||
|
||||
/**
|
||||
* If any of the terminators in \c terminators leads to termination of the
|
||||
* loop after a constant number of iterations, this is the terminator that
|
||||
* leads to termination after the smallest number of iterations. Otherwise
|
||||
* NULL.
|
||||
*/
|
||||
loop_terminator *limiting_terminator;
|
||||
|
||||
/**
|
||||
* Hash table containing all variables accessed in this loop
|
||||
*/
|
||||
hash_table *var_hash;
|
||||
|
||||
/**
|
||||
* Number of ir_loop_jump instructions that operate on this loop
|
||||
*/
|
||||
unsigned num_loop_jumps;
|
||||
|
||||
/**
|
||||
* Whether this loop contains any function calls.
|
||||
*/
|
||||
bool contains_calls;
|
||||
|
||||
loop_variable_state()
|
||||
{
|
||||
this->num_loop_jumps = 0;
|
||||
this->contains_calls = false;
|
||||
this->var_hash = _mesa_pointer_hash_table_create(NULL);
|
||||
this->limiting_terminator = NULL;
|
||||
}
|
||||
|
||||
~loop_variable_state()
|
||||
{
|
||||
_mesa_hash_table_destroy(this->var_hash, NULL);
|
||||
}
|
||||
|
||||
DECLARE_RALLOC_CXX_OPERATORS(loop_variable_state)
|
||||
};
|
||||
|
||||
|
||||
class loop_variable : public exec_node {
|
||||
public:
|
||||
/** The variable in question. */
|
||||
ir_variable *var;
|
||||
|
||||
/** Is the variable read in the loop before it is written? */
|
||||
bool read_before_write;
|
||||
|
||||
/** Are all variables in the RHS of the assignment loop constants? */
|
||||
bool rhs_clean;
|
||||
|
||||
/**
|
||||
* Is there an assignment to the variable that is conditional, or inside a
|
||||
* nested loop?
|
||||
*/
|
||||
bool conditional_or_nested_assignment;
|
||||
|
||||
/** Reference to the first assignment to the variable in the loop body. */
|
||||
ir_assignment *first_assignment;
|
||||
|
||||
/** Number of assignments to the variable in the loop body. */
|
||||
unsigned num_assignments;
|
||||
|
||||
/**
|
||||
* Increment value for a loop induction variable
|
||||
*
|
||||
* If this is a loop induction variable, the amount by which the variable
|
||||
* is incremented on each iteration through the loop.
|
||||
*
|
||||
* If this is not a loop induction variable, NULL.
|
||||
*/
|
||||
ir_rvalue *increment;
|
||||
|
||||
|
||||
inline bool is_induction_var() const
|
||||
{
|
||||
/* Induction variables always have a non-null increment, and vice
|
||||
* versa.
|
||||
*/
|
||||
return this->increment != NULL;
|
||||
}
|
||||
|
||||
|
||||
inline bool is_loop_constant() const
|
||||
{
|
||||
const bool is_const = (this->num_assignments == 0)
|
||||
|| (((this->num_assignments == 1)
|
||||
&& !this->conditional_or_nested_assignment
|
||||
&& !this->read_before_write
|
||||
&& this->rhs_clean) || this->var->data.read_only);
|
||||
|
||||
/* If the RHS of *the* assignment is clean, then there must be exactly
|
||||
* one assignment of the variable.
|
||||
*/
|
||||
assert((this->rhs_clean && (this->num_assignments == 1))
|
||||
|| !this->rhs_clean);
|
||||
|
||||
return is_const;
|
||||
}
|
||||
|
||||
void record_reference(bool in_assignee,
|
||||
bool in_conditional_code_or_nested_loop,
|
||||
ir_assignment *current_assignment);
|
||||
};
|
||||
|
||||
|
||||
class loop_terminator : public exec_node {
|
||||
public:
|
||||
loop_terminator(ir_if *ir, bool continue_from_then)
|
||||
: ir(ir), iterations(-1), continue_from_then(continue_from_then)
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* Statement which terminates the loop.
|
||||
*/
|
||||
ir_if *ir;
|
||||
|
||||
/**
|
||||
* The number of iterations after which the terminator is known to
|
||||
* terminate the loop (if that is a fixed value). Otherwise -1.
|
||||
*/
|
||||
int iterations;
|
||||
|
||||
/* Does the if continue from the then branch or the else branch */
|
||||
bool continue_from_then;
|
||||
};
|
||||
|
||||
|
||||
class loop_state {
|
||||
public:
|
||||
~loop_state();
|
||||
|
||||
/**
|
||||
* Get the loop variable state data for a particular loop
|
||||
*/
|
||||
loop_variable_state *get(const ir_loop *);
|
||||
|
||||
loop_variable_state *insert(ir_loop *ir);
|
||||
|
||||
bool loop_found;
|
||||
|
||||
private:
|
||||
loop_state();
|
||||
|
||||
/**
|
||||
* Hash table containing all loops that have been analyzed.
|
||||
*/
|
||||
hash_table *ht;
|
||||
|
||||
void *mem_ctx;
|
||||
|
||||
friend loop_state *analyze_loop_variables(exec_list *instructions);
|
||||
};
|
||||
|
||||
#endif /* LOOP_ANALYSIS_H */
|
@@ -1,590 +0,0 @@
|
||||
/*
|
||||
* Copyright © 2010 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.
|
||||
*/
|
||||
|
||||
#include "compiler/glsl_types.h"
|
||||
#include "loop_analysis.h"
|
||||
#include "ir_hierarchical_visitor.h"
|
||||
#include "main/consts_exts.h"
|
||||
|
||||
namespace {
|
||||
|
||||
class loop_unroll_visitor : public ir_hierarchical_visitor {
|
||||
public:
|
||||
loop_unroll_visitor(loop_state *state,
|
||||
const struct gl_shader_compiler_options *options)
|
||||
{
|
||||
this->state = state;
|
||||
this->progress = false;
|
||||
this->options = options;
|
||||
}
|
||||
|
||||
virtual ir_visitor_status visit_leave(ir_loop *ir);
|
||||
void simple_unroll(ir_loop *ir, int iterations);
|
||||
void complex_unroll(ir_loop *ir, int iterations,
|
||||
bool continue_from_then_branch,
|
||||
bool limiting_term_first,
|
||||
bool lt_continue_from_then_branch);
|
||||
void splice_post_if_instructions(ir_if *ir_if, exec_list *splice_dest);
|
||||
|
||||
loop_state *state;
|
||||
|
||||
bool progress;
|
||||
const struct gl_shader_compiler_options *options;
|
||||
};
|
||||
|
||||
} /* anonymous namespace */
|
||||
|
||||
class loop_unroll_count : public ir_hierarchical_visitor {
|
||||
public:
|
||||
int nodes;
|
||||
bool unsupported_variable_indexing;
|
||||
bool array_indexed_by_induction_var_with_exact_iterations;
|
||||
/* If there are nested loops, the node count will be inaccurate. */
|
||||
bool nested_loop;
|
||||
|
||||
loop_unroll_count(exec_list *list, loop_variable_state *ls,
|
||||
const struct gl_shader_compiler_options *options)
|
||||
: ls(ls), options(options)
|
||||
{
|
||||
nodes = 0;
|
||||
nested_loop = false;
|
||||
unsupported_variable_indexing = false;
|
||||
array_indexed_by_induction_var_with_exact_iterations = false;
|
||||
|
||||
run(list);
|
||||
}
|
||||
|
||||
virtual ir_visitor_status visit_enter(ir_assignment *)
|
||||
{
|
||||
nodes++;
|
||||
return visit_continue;
|
||||
}
|
||||
|
||||
virtual ir_visitor_status visit_enter(ir_expression *)
|
||||
{
|
||||
nodes++;
|
||||
return visit_continue;
|
||||
}
|
||||
|
||||
virtual ir_visitor_status visit_enter(ir_loop *)
|
||||
{
|
||||
nested_loop = true;
|
||||
return visit_continue;
|
||||
}
|
||||
|
||||
virtual ir_visitor_status visit_enter(ir_dereference_array *ir)
|
||||
{
|
||||
/* Force unroll in case of dynamic indexing with sampler arrays
|
||||
* when EmitNoIndirectSampler is set.
|
||||
*/
|
||||
if (options->EmitNoIndirectSampler) {
|
||||
if ((ir->array->type->is_array() &&
|
||||
ir->array->type->contains_sampler()) &&
|
||||
!ir->array_index->constant_expression_value(ralloc_parent(ir))) {
|
||||
unsupported_variable_indexing = true;
|
||||
return visit_continue;
|
||||
}
|
||||
}
|
||||
|
||||
/* Check for arrays variably-indexed by a loop induction variable.
|
||||
* Unrolling the loop may convert that access into constant-indexing.
|
||||
*
|
||||
* Many drivers don't support particular kinds of variable indexing,
|
||||
* and have to resort to using nir_lower_indirect_derefs to
|
||||
* handle it. This results in huge amounts of horrible code, so we'd
|
||||
* like to avoid that if possible. Here, we just note that it will
|
||||
* happen.
|
||||
*/
|
||||
if ((ir->array->type->is_array() || ir->array->type->is_matrix()) &&
|
||||
!ir->array_index->as_constant()) {
|
||||
ir_variable *array = ir->array->variable_referenced();
|
||||
loop_variable *lv = ls->get(ir->array_index->variable_referenced());
|
||||
if (array && lv && lv->is_induction_var()) {
|
||||
/* If an array is indexed by a loop induction variable, and the
|
||||
* array size is exactly the number of loop iterations, this is
|
||||
* probably a simple for-loop trying to access each element in
|
||||
* turn; the application may expect it to be unrolled.
|
||||
*/
|
||||
if (int(array->type->length) == ls->limiting_terminator->iterations)
|
||||
array_indexed_by_induction_var_with_exact_iterations = true;
|
||||
|
||||
switch (array->data.mode) {
|
||||
case ir_var_auto:
|
||||
case ir_var_temporary:
|
||||
case ir_var_const_in:
|
||||
case ir_var_function_in:
|
||||
case ir_var_function_out:
|
||||
case ir_var_function_inout:
|
||||
if (options->EmitNoIndirectTemp)
|
||||
unsupported_variable_indexing = true;
|
||||
break;
|
||||
case ir_var_uniform:
|
||||
case ir_var_shader_storage:
|
||||
if (options->EmitNoIndirectUniform)
|
||||
unsupported_variable_indexing = true;
|
||||
break;
|
||||
case ir_var_shader_in:
|
||||
if (options->EmitNoIndirectInput)
|
||||
unsupported_variable_indexing = true;
|
||||
break;
|
||||
case ir_var_shader_out:
|
||||
if (options->EmitNoIndirectOutput)
|
||||
unsupported_variable_indexing = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
return visit_continue;
|
||||
}
|
||||
|
||||
private:
|
||||
loop_variable_state *ls;
|
||||
const struct gl_shader_compiler_options *options;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Unroll a loop which does not contain any jumps. For example, if the input
|
||||
* is:
|
||||
*
|
||||
* (loop (...) ...instrs...)
|
||||
*
|
||||
* And the iteration count is 3, the output will be:
|
||||
*
|
||||
* ...instrs... ...instrs... ...instrs...
|
||||
*/
|
||||
void
|
||||
loop_unroll_visitor::simple_unroll(ir_loop *ir, int iterations)
|
||||
{
|
||||
void *const mem_ctx = ralloc_parent(ir);
|
||||
loop_variable_state *const ls = this->state->get(ir);
|
||||
|
||||
/* If there are no terminators, then the loop iteration count must be 1.
|
||||
* This is the 'do { } while (false);' case.
|
||||
*/
|
||||
assert(!ls->terminators.is_empty() || iterations == 1);
|
||||
|
||||
ir_instruction *first_ir =
|
||||
(ir_instruction *) ir->body_instructions.get_head();
|
||||
|
||||
if (!first_ir) {
|
||||
/* The loop is empty remove it and return */
|
||||
ir->remove();
|
||||
return;
|
||||
}
|
||||
|
||||
ir_if *limit_if = NULL;
|
||||
bool exit_branch_has_instructions = false;
|
||||
if (ls->limiting_terminator) {
|
||||
limit_if = ls->limiting_terminator->ir;
|
||||
ir_instruction *ir_if_last = (ir_instruction *)
|
||||
limit_if->then_instructions.get_tail();
|
||||
|
||||
if (is_break(ir_if_last)) {
|
||||
if (ir_if_last != limit_if->then_instructions.get_head())
|
||||
exit_branch_has_instructions = true;
|
||||
|
||||
splice_post_if_instructions(limit_if, &limit_if->else_instructions);
|
||||
ir_if_last->remove();
|
||||
} else {
|
||||
ir_if_last = (ir_instruction *)
|
||||
limit_if->else_instructions.get_tail();
|
||||
assert(is_break(ir_if_last));
|
||||
|
||||
if (ir_if_last != limit_if->else_instructions.get_head())
|
||||
exit_branch_has_instructions = true;
|
||||
|
||||
splice_post_if_instructions(limit_if, &limit_if->then_instructions);
|
||||
ir_if_last->remove();
|
||||
}
|
||||
}
|
||||
|
||||
/* Because 'iterations' is the number of times we pass over the *entire*
|
||||
* loop body before hitting the first break, we need to bump the number of
|
||||
* iterations if the limiting terminator is not the first instruction in
|
||||
* the loop, or it the exit branch contains instructions. This ensures we
|
||||
* execute any instructions before the terminator or in its exit branch.
|
||||
*/
|
||||
if (!ls->terminators.is_empty() &&
|
||||
(limit_if != first_ir->as_if() || exit_branch_has_instructions))
|
||||
iterations++;
|
||||
|
||||
for (int i = 0; i < iterations; i++) {
|
||||
exec_list copy_list;
|
||||
|
||||
copy_list.make_empty();
|
||||
clone_ir_list(mem_ctx, ©_list, &ir->body_instructions);
|
||||
|
||||
ir->insert_before(©_list);
|
||||
}
|
||||
|
||||
/* The loop has been replaced by the unrolled copies. Remove the original
|
||||
* loop from the IR sequence.
|
||||
*/
|
||||
ir->remove();
|
||||
|
||||
this->progress = true;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Unroll a loop whose last statement is an ir_if. If \c
|
||||
* continue_from_then_branch is true, the loop is repeated only when the
|
||||
* "then" branch of the if is taken; otherwise it is repeated only when the
|
||||
* "else" branch of the if is taken.
|
||||
*
|
||||
* For example, if the input is:
|
||||
*
|
||||
* (loop (...)
|
||||
* ...body...
|
||||
* (if (cond)
|
||||
* (...then_instrs...)
|
||||
* (...else_instrs...)))
|
||||
*
|
||||
* And the iteration count is 3, and \c continue_from_then_branch is true,
|
||||
* then the output will be:
|
||||
*
|
||||
* ...body...
|
||||
* (if (cond)
|
||||
* (...then_instrs...
|
||||
* ...body...
|
||||
* (if (cond)
|
||||
* (...then_instrs...
|
||||
* ...body...
|
||||
* (if (cond)
|
||||
* (...then_instrs...)
|
||||
* (...else_instrs...)))
|
||||
* (...else_instrs...)))
|
||||
* (...else_instrs))
|
||||
*/
|
||||
void
|
||||
loop_unroll_visitor::complex_unroll(ir_loop *ir, int iterations,
|
||||
bool second_term_then_continue,
|
||||
bool extra_iteration_required,
|
||||
bool first_term_then_continue)
|
||||
{
|
||||
void *const mem_ctx = ralloc_parent(ir);
|
||||
ir_instruction *ir_to_replace = ir;
|
||||
|
||||
/* Because 'iterations' is the number of times we pass over the *entire*
|
||||
* loop body before hitting the first break, we need to bump the number of
|
||||
* iterations if the limiting terminator is not the first instruction in
|
||||
* the loop, or it the exit branch contains instructions. This ensures we
|
||||
* execute any instructions before the terminator or in its exit branch.
|
||||
*/
|
||||
if (extra_iteration_required)
|
||||
iterations++;
|
||||
|
||||
for (int i = 0; i < iterations; i++) {
|
||||
exec_list copy_list;
|
||||
|
||||
copy_list.make_empty();
|
||||
clone_ir_list(mem_ctx, ©_list, &ir->body_instructions);
|
||||
|
||||
ir_if *ir_if = ((ir_instruction *) copy_list.get_tail())->as_if();
|
||||
assert(ir_if != NULL);
|
||||
|
||||
exec_list *const first_list = first_term_then_continue
|
||||
? &ir_if->then_instructions : &ir_if->else_instructions;
|
||||
ir_if = ((ir_instruction *) first_list->get_tail())->as_if();
|
||||
|
||||
ir_to_replace->insert_before(©_list);
|
||||
ir_to_replace->remove();
|
||||
|
||||
/* placeholder that will be removed in the next iteration */
|
||||
ir_to_replace =
|
||||
new(mem_ctx) ir_loop_jump(ir_loop_jump::jump_continue);
|
||||
|
||||
exec_list *const second_term_continue_list = second_term_then_continue
|
||||
? &ir_if->then_instructions : &ir_if->else_instructions;
|
||||
|
||||
second_term_continue_list->push_tail(ir_to_replace);
|
||||
}
|
||||
|
||||
ir_to_replace->remove();
|
||||
|
||||
this->progress = true;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Move all of the instructions which follow \c ir_if to the end of
|
||||
* \c splice_dest.
|
||||
*
|
||||
* For example, in the code snippet:
|
||||
*
|
||||
* (if (cond)
|
||||
* (...then_instructions...
|
||||
* break)
|
||||
* (...else_instructions...))
|
||||
* ...post_if_instructions...
|
||||
*
|
||||
* If \c ir_if points to the "if" instruction, and \c splice_dest points to
|
||||
* (...else_instructions...), the code snippet is transformed into:
|
||||
*
|
||||
* (if (cond)
|
||||
* (...then_instructions...
|
||||
* break)
|
||||
* (...else_instructions...
|
||||
* ...post_if_instructions...))
|
||||
*/
|
||||
void
|
||||
loop_unroll_visitor::splice_post_if_instructions(ir_if *ir_if,
|
||||
exec_list *splice_dest)
|
||||
{
|
||||
while (!ir_if->get_next()->is_tail_sentinel()) {
|
||||
ir_instruction *move_ir = (ir_instruction *) ir_if->get_next();
|
||||
|
||||
move_ir->remove();
|
||||
splice_dest->push_tail(move_ir);
|
||||
}
|
||||
}
|
||||
|
||||
static bool
|
||||
exit_branch_has_instructions(ir_if *term_if, bool lt_then_continue)
|
||||
{
|
||||
if (lt_then_continue) {
|
||||
if (term_if->else_instructions.get_head() ==
|
||||
term_if->else_instructions.get_tail())
|
||||
return false;
|
||||
} else {
|
||||
if (term_if->then_instructions.get_head() ==
|
||||
term_if->then_instructions.get_tail())
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
ir_visitor_status
|
||||
loop_unroll_visitor::visit_leave(ir_loop *ir)
|
||||
{
|
||||
loop_variable_state *const ls = this->state->get(ir);
|
||||
|
||||
/* If we've entered a loop that hasn't been analyzed, something really,
|
||||
* really bad has happened.
|
||||
*/
|
||||
if (ls == NULL) {
|
||||
assert(ls != NULL);
|
||||
return visit_continue;
|
||||
}
|
||||
|
||||
/* Limiting terminator may have iteration count of zero,
|
||||
* this is a valid case because the loop may break during
|
||||
* the first iteration.
|
||||
*/
|
||||
|
||||
/* Remove the conditional break statements associated with all terminators
|
||||
* that are associated with a fixed iteration count, except for the one
|
||||
* associated with the limiting terminator--that one needs to stay, since
|
||||
* it terminates the loop. Exception: if the loop still has a normative
|
||||
* bound, then that terminates the loop, so we don't even need the limiting
|
||||
* terminator.
|
||||
*/
|
||||
foreach_in_list_safe(loop_terminator, t, &ls->terminators) {
|
||||
if (t->iterations < 0)
|
||||
continue;
|
||||
|
||||
exec_list *branch_instructions;
|
||||
if (t != ls->limiting_terminator) {
|
||||
ir_instruction *ir_if_last = (ir_instruction *)
|
||||
t->ir->then_instructions.get_tail();
|
||||
if (is_break(ir_if_last)) {
|
||||
branch_instructions = &t->ir->else_instructions;
|
||||
} else {
|
||||
branch_instructions = &t->ir->then_instructions;
|
||||
assert(is_break((ir_instruction *)
|
||||
t->ir->else_instructions.get_tail()));
|
||||
}
|
||||
|
||||
exec_list copy_list;
|
||||
copy_list.make_empty();
|
||||
clone_ir_list(ir, ©_list, branch_instructions);
|
||||
|
||||
t->ir->insert_before(©_list);
|
||||
t->ir->remove();
|
||||
|
||||
assert(ls->num_loop_jumps > 0);
|
||||
ls->num_loop_jumps--;
|
||||
|
||||
/* Also remove it from the terminator list */
|
||||
t->remove();
|
||||
|
||||
this->progress = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (ls->limiting_terminator == NULL) {
|
||||
ir_instruction *last_ir =
|
||||
(ir_instruction *) ir->body_instructions.get_tail();
|
||||
|
||||
/* If a loop has no induction variable and the last instruction is
|
||||
* a break, unroll the loop with a count of 1. This is the classic
|
||||
*
|
||||
* do {
|
||||
* // ...
|
||||
* } while (false)
|
||||
*
|
||||
* that is used to wrap multi-line macros.
|
||||
*
|
||||
* If num_loop_jumps is not zero, last_ir cannot be NULL... there has to
|
||||
* be at least num_loop_jumps instructions in the loop.
|
||||
*/
|
||||
if (ls->num_loop_jumps == 1 && is_break(last_ir)) {
|
||||
last_ir->remove();
|
||||
|
||||
simple_unroll(ir, 1);
|
||||
}
|
||||
|
||||
/* Don't try to unroll loops where the number of iterations is not known
|
||||
* at compile-time.
|
||||
*/
|
||||
return visit_continue;
|
||||
}
|
||||
|
||||
int iterations = ls->limiting_terminator->iterations;
|
||||
|
||||
const int max_iterations = options->MaxUnrollIterations;
|
||||
|
||||
/* Don't try to unroll loops that have zillions of iterations either.
|
||||
*/
|
||||
if (iterations > max_iterations)
|
||||
return visit_continue;
|
||||
|
||||
/* Don't try to unroll nested loops and loops with a huge body.
|
||||
*/
|
||||
loop_unroll_count count(&ir->body_instructions, ls, options);
|
||||
|
||||
bool loop_too_large =
|
||||
count.nested_loop || count.nodes * iterations > max_iterations * 5;
|
||||
|
||||
if (loop_too_large && !count.unsupported_variable_indexing &&
|
||||
!count.array_indexed_by_induction_var_with_exact_iterations)
|
||||
return visit_continue;
|
||||
|
||||
/* Note: the limiting terminator contributes 1 to ls->num_loop_jumps.
|
||||
* We'll be removing the limiting terminator before we unroll.
|
||||
*/
|
||||
assert(ls->num_loop_jumps > 0);
|
||||
unsigned predicted_num_loop_jumps = ls->num_loop_jumps - 1;
|
||||
|
||||
if (predicted_num_loop_jumps > 1)
|
||||
return visit_continue;
|
||||
|
||||
if (predicted_num_loop_jumps == 0) {
|
||||
simple_unroll(ir, iterations);
|
||||
return visit_continue;
|
||||
}
|
||||
|
||||
ir_instruction *last_ir = (ir_instruction *) ir->body_instructions.get_tail();
|
||||
assert(last_ir != NULL);
|
||||
|
||||
if (is_break(last_ir)) {
|
||||
/* If the only loop-jump is a break at the end of the loop, the loop
|
||||
* will execute exactly once. Remove the break and use the simple
|
||||
* unroller with an iteration count of 1.
|
||||
*/
|
||||
last_ir->remove();
|
||||
|
||||
simple_unroll(ir, 1);
|
||||
return visit_continue;
|
||||
}
|
||||
|
||||
/* Complex unrolling can only handle two terminators. One with an unknown
|
||||
* iteration count and one with a known iteration count. We have already
|
||||
* made sure we have a known iteration count above and removed any
|
||||
* unreachable terminators with a known count. Here we make sure there
|
||||
* isn't any additional unknown terminators, or any other jumps nested
|
||||
* inside futher ifs.
|
||||
*/
|
||||
if (ls->num_loop_jumps != 2 || ls->terminators.length() != 2)
|
||||
return visit_continue;
|
||||
|
||||
ir_instruction *first_ir =
|
||||
(ir_instruction *) ir->body_instructions.get_head();
|
||||
|
||||
unsigned term_count = 0;
|
||||
bool first_term_then_continue = false;
|
||||
foreach_in_list(loop_terminator, t, &ls->terminators) {
|
||||
ir_if *ir_if = t->ir->as_if();
|
||||
assert(ir_if != NULL);
|
||||
|
||||
ir_instruction *ir_if_last =
|
||||
(ir_instruction *) ir_if->then_instructions.get_tail();
|
||||
|
||||
if (is_break(ir_if_last)) {
|
||||
splice_post_if_instructions(ir_if, &ir_if->else_instructions);
|
||||
ir_if_last->remove();
|
||||
if (term_count == 1) {
|
||||
bool ebi =
|
||||
exit_branch_has_instructions(ls->limiting_terminator->ir,
|
||||
first_term_then_continue);
|
||||
complex_unroll(ir, iterations, false,
|
||||
first_ir->as_if() != ls->limiting_terminator->ir ||
|
||||
ebi,
|
||||
first_term_then_continue);
|
||||
return visit_continue;
|
||||
}
|
||||
} else {
|
||||
ir_if_last =
|
||||
(ir_instruction *) ir_if->else_instructions.get_tail();
|
||||
|
||||
assert(is_break(ir_if_last));
|
||||
if (is_break(ir_if_last)) {
|
||||
splice_post_if_instructions(ir_if, &ir_if->then_instructions);
|
||||
ir_if_last->remove();
|
||||
if (term_count == 1) {
|
||||
bool ebi =
|
||||
exit_branch_has_instructions(ls->limiting_terminator->ir,
|
||||
first_term_then_continue);
|
||||
complex_unroll(ir, iterations, true,
|
||||
first_ir->as_if() != ls->limiting_terminator->ir ||
|
||||
ebi,
|
||||
first_term_then_continue);
|
||||
return visit_continue;
|
||||
} else {
|
||||
first_term_then_continue = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
term_count++;
|
||||
}
|
||||
|
||||
/* Did not find the break statement. It must be in a complex if-nesting,
|
||||
* so don't try to unroll.
|
||||
*/
|
||||
return visit_continue;
|
||||
}
|
||||
|
||||
|
||||
bool
|
||||
unroll_loops(exec_list *instructions, loop_state *ls,
|
||||
const struct gl_shader_compiler_options *options)
|
||||
{
|
||||
loop_unroll_visitor v(ls, options);
|
||||
|
||||
v.run(instructions);
|
||||
|
||||
return v.progress;
|
||||
}
|
@@ -154,9 +154,6 @@ files_libglsl = files(
|
||||
'link_varyings.cpp',
|
||||
'link_varyings.h',
|
||||
'list.h',
|
||||
'loop_analysis.cpp',
|
||||
'loop_analysis.h',
|
||||
'loop_unroll.cpp',
|
||||
'lower_blend_equation_advanced.cpp',
|
||||
'lower_buffer_access.cpp',
|
||||
'lower_buffer_access.h',
|
||||
|
@@ -33,7 +33,6 @@
|
||||
#include "glsl_parser_extras.h"
|
||||
#include "ir_optimization.h"
|
||||
#include "program.h"
|
||||
#include "loop_analysis.h"
|
||||
#include "standalone_scaffolding.h"
|
||||
#include "standalone.h"
|
||||
#include "string_to_uint_map.h"
|
||||
|
@@ -265,7 +265,6 @@ void initialize_context_to_defaults(struct gl_context *ctx, gl_api api)
|
||||
/* Set up default shader compiler options. */
|
||||
struct gl_shader_compiler_options options;
|
||||
memset(&options, 0, sizeof(options));
|
||||
options.MaxUnrollIterations = 32;
|
||||
options.MaxIfDepth = UINT_MAX;
|
||||
|
||||
for (int sh = 0; sh < MESA_SHADER_STAGES; ++sh)
|
||||
|
Reference in New Issue
Block a user