Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't lift loop vars outside of their loops in sliding window #7896

Merged
merged 3 commits into from
Oct 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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