Skip to content

Commit

Permalink
Fix failure to pop loop var info
Browse files Browse the repository at this point in the history
  • Loading branch information
abadams committed Jun 1, 2024
1 parent e42dfbe commit 883a9da
Showing 1 changed file with 13 additions and 17 deletions.
30 changes: 13 additions & 17 deletions src/Simplify_Stmts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,12 @@ Stmt Simplify::visit(const For *op) {
(in_vector_loop ||
op->for_type == ForType::Vectorized));

bool bounds_tracked = false;
Expr extent_positive = mutate(0 < new_extent, nullptr);
if (is_const_zero(extent_positive)) {
// This loop never runs
return Evaluate::make(0);
}

ExprInfo loop_var_info;
// Deduce bounds for the loop var that are true for any code than runs
// inside the loop body. Code in the inner loop only runs if the extent is
Expand All @@ -226,24 +231,19 @@ Stmt Simplify::visit(const For *op) {
loop_var_info.bounds =
ConstantInterval::make_union(min_info.bounds,
min_info.bounds + max(extent_info.bounds, 1) - 1);

if (loop_var_info.bounds.max_defined ||
loop_var_info.bounds.min_defined) {
bounds_tracked = true;
bounds_and_alignment_info.push(op->name, loop_var_info);
}

Expr extent_positive = mutate(0 < new_extent, nullptr);
if (is_const_zero(extent_positive)) {
return Evaluate::make(0);
}

Stmt new_body;
{
ScopedBinding<ExprInfo> bind_if((loop_var_info.bounds.max_defined ||
loop_var_info.bounds.min_defined),
bounds_and_alignment_info,
op->name,
loop_var_info);

// If we're in the loop, the extent must be greater than 0.
ScopedFact fact = scoped_truth(extent_positive);
new_body = mutate(op->body);
}

if (in_unreachable) {
// We found that the body of this loop is unreachable when recursively
// mutating it, so we can remove the loop. Additionally, if we know the
Expand All @@ -254,10 +254,6 @@ Stmt Simplify::visit(const For *op) {
return Evaluate::make(0);
}

if (bounds_tracked) {
bounds_and_alignment_info.pop(op->name);
}

if (const Acquire *acquire = new_body.as<Acquire>()) {
if (is_no_op(acquire->body)) {
// Rewrite iterated no-op acquires as a single acquire.
Expand Down

0 comments on commit 883a9da

Please sign in to comment.