Skip to content

Commit

Permalink
Don't lift loop vars outside of their loops in sliding window (#7896)
Browse files Browse the repository at this point in the history
Sliding window, when operating in the mode that shifts the consumer's
loop min backwards a few iterations to cover the warmup, was capable of
inappropriately lifting for loop vars inside that loop but outside the
produce node of the slid Func.

Fixes #7891
  • Loading branch information
abadams authored Oct 18, 2023
1 parent 5c97c3c commit eb66c06
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/SlidingWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,9 @@ class SlidingWindowOnFunctionAndLoop : public IRMutator {
set<int> &slid_dimensions;
Scope<Expr> scope;

// Loops between the loop being slid over and the produce node
Scope<> enclosing_loops;

map<string, Expr> replacements;

using IRMutator::visit;
Expand Down Expand Up @@ -433,7 +436,9 @@ class SlidingWindowOnFunctionAndLoop : public IRMutator {
new_loop_min_eq = simplify(new_loop_min_eq);
Interval solve_result = solve_for_inner_interval(new_loop_min_eq, new_loop_min_name);
internal_assert(!new_loop_min.defined());
if (solve_result.has_upper_bound() && !equal(solve_result.max, loop_min)) {
if (solve_result.has_upper_bound() &&
!equal(solve_result.max, loop_min) &&
!expr_uses_vars(solve_result.max, enclosing_loops)) {
new_loop_min = simplify(solve_result.max);

// We have a new loop min, so we an assume every iteration has
Expand Down Expand Up @@ -558,6 +563,7 @@ class SlidingWindowOnFunctionAndLoop : public IRMutator {
// the var we're sliding over.
Expr min = expand_expr(op->min, scope);
Expr extent = expand_expr(op->extent, scope);
ScopedBinding<> bind(enclosing_loops, op->name);
if (is_const_one(extent)) {
// Just treat it like a let
Stmt s = LetStmt::make(op->name, min, op->body);
Expand Down
24 changes: 24 additions & 0 deletions test/correctness/fuzz_schedule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,30 @@ int main(int argc, char **argv) {
check_blur_output(buf, correct);
}

// https://github.com/halide/Halide/issues/7891
{
Func input("input");
Func local_sum("local_sum");
Func blurry("blurry");
Var x("x"), y("y");
input(x, y) = 2 * x + 5 * y;
RDom r(-2, 5, -2, 5);
local_sum(x, y) = 0;
local_sum(x, y) += input(x + r.x, y + r.y);
blurry(x, y) = cast<int32_t>(local_sum(x, y) / 25);
Var yo, yi, xo, xi, xio, xii, xiio, xiii;
blurry.split(y, yo, yi, 4, TailStrategy::Auto)
.split(x, xo, xi, 1, TailStrategy::Auto)
.split(xi, xio, xii, 4, TailStrategy::GuardWithIf)
.split(xii, xiio, xiii, 1, TailStrategy::RoundUp);
local_sum.compute_at(blurry, xiio);
input.compute_at(blurry, xiio);
input.store_root();
Pipeline p({blurry});
Buffer<int> buf = p.realize({32, 32});
check_blur_output(buf, correct);
}

// https://github.com/halide/Halide/issues/7892
{
Func input("input");
Expand Down

0 comments on commit eb66c06

Please sign in to comment.