Skip to content
Open
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
39 changes: 34 additions & 5 deletions src/Monotonic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,11 +139,12 @@ class DerivativeBounds : public IRVisitor {
ConstantInterval rb = result;

// This is essentially the product rule: a*rb + b*ra
// but only implemented for the case where a or b is constant.
if (auto b = as_const_int_or_uint(op->b)) {
result = ra * (*b);
} else if (auto a = as_const_int_or_uint(op->a)) {
result = rb * (*a);
// but only implemented for the case where a or b doesn't
// depend on the var.
if (rb.is_single_point(0)) {
result = ra * constant_integer_bounds(op->b);
} else if (ra.is_single_point(0)) {
result = rb * constant_integer_bounds(op->a);
} else {
result = ConstantInterval::everything();
}
Expand Down Expand Up @@ -179,6 +180,34 @@ class DerivativeBounds : public IRVisitor {
}
return;
}
op->b.accept(this);
if (result.is_single_point(0)) {
// The denominator doesn't depend on the var. If it's known
// to be nonzero, the quotient moves in the same direction as
// the numerator, or the opposite one, by at most as much.
ConstantInterval rb = constant_integer_bounds(op->b);
bool negative = rb < 0;
if (negative) {
rb = -rb;
}
if (rb > 0) {
op->a.accept(this);
ConstantInterval ra = result;
result = ConstantInterval::everything();
if (ra.min_defined) {
result.min_defined = true;
result.min = std::min<int64_t>(div_imp(ra.min, rb.min), 0);
}
if (ra.max_defined && ra.max != INT64_MIN) {
result.max_defined = true;
result.max = std::max<int64_t>(div_imp(ra.max - 1, rb.min) + 1, 0);
}
if (negative) {
result = -result;
}
return;
}
}
}
result = ConstantInterval::everything();
}
Expand Down
10 changes: 9 additions & 1 deletion test/correctness/is_monotonic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,15 @@ int main() {
check_decreasing(x <= y);
check_decreasing(x < y);
check_unknown(x * y);

check_unknown(x / y);

// Scaling by something of known sign that doesn't depend on x.
check_increasing(x * max(y, 1));
check_increasing(max(y, 0) * x);
check_increasing(x / max(y, 1));
check_decreasing(x * min(y, -1));
check_decreasing(x / min(y, -1));
check_decreasing((4 - x) / max(y, 1));
// Not constant despite having constant args, because there's a side-effect.
check_unknown(Call::make(Int(32), "foo", {Expr(3)}, Call::Extern));

Expand Down
19 changes: 19 additions & 0 deletions test/correctness/sliding_window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,25 @@ int main(int argc, char **argv) {
}
}

{
// Sliding over a division by a param of known sign.
count = 0;
Param<int> k;
Func f, g;
f(x) = call_counter(x, 0);
Expr i = x / max(k, 1);
g(x) = f(i) + f(i + 1);
f.store_root().compute_at(g, x);

k.set(3);
g.realize({100});
// f spans x in [0, 34], so 35 calls when slid.
if (count != 35) {
printf("f was called %d times instead of %d times\n", count, 35);
return 1;
}
}

printf("Success!\n");
return 0;
}
Loading