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

Fixes to bounds inference on shift_left #5477

Merged
merged 22 commits into from
Dec 4, 2020
Merged
Changes from 13 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
2c308ac
Add shift_left fix for signed integers by possibly negative values + …
rootjalex Nov 22, 2020
54f5f74
add required condition on shift_left integer fix
rootjalex Nov 22, 2020
04311b6
add type check to shift_left minimum condition
rootjalex Nov 22, 2020
a3f3323
fix constant folding of shifts with |b| >= type.bits() for types that…
rootjalex Nov 22, 2020
d9230a6
make regression tests use scoped bindings
rootjalex Nov 23, 2020
084976e
change condition in case int24/int48 proposal happens soon
rootjalex Nov 23, 2020
a81a878
Merge branch 'master' of https://github.com/halide/Halide into ajr-sh…
rootjalex Nov 24, 2020
1d14dc6
revert changes based on overflow expectations
rootjalex Nov 25, 2020
9898a11
add more regression tests
rootjalex Nov 25, 2020
9d5b35d
clarify comment
rootjalex Nov 25, 2020
0594730
add shift_left min handler for b only UB
rootjalex Nov 25, 2020
030d36d
fix clang-tidy complaint
rootjalex Nov 25, 2020
c4b7b8a
relax shift_left of non-negative value constraint
rootjalex Nov 25, 2020
e8bcdf2
pull case outside of unnecessary preconditions
rootjalex Nov 26, 2020
343b5b0
fix clang-format complaint
rootjalex Nov 26, 2020
86e362e
fix broken precondition
rootjalex Nov 26, 2020
b725708
add typecheck to possibly save a can_prove() call
rootjalex Nov 26, 2020
3055665
add easy-out type check to precondition
rootjalex Nov 30, 2020
1a8930e
Add descriptive comment to bug fix + add another early-exit precondition
rootjalex Nov 30, 2020
89b0e19
Merge branch 'master' into rootjalex/shift_left_fix
steven-johnson Dec 3, 2020
153871f
Merge branch 'master' into rootjalex/shift_left_fix
steven-johnson Dec 3, 2020
983e492
Merge branch 'master' into rootjalex/shift_left_fix
steven-johnson Dec 3, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
53 changes: 52 additions & 1 deletion src/Bounds.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1235,8 +1235,26 @@ class Bounds : public IRVisitor {
!b_interval.min.type().is_uint() &&
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it the case that b_interval.min and b_interval.max are always the same type? (i.e., this implies that b_interval.max is also a non-uint?)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe that this should be the case, but @abadams might contradict me. I have been operating under the assumption that that invariant is maintained.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Would be nice to have that documented somewhere, though that's orthogonal to this PR.)

I ask mainly because code lower down seemed to be optimized based on that assumption, but it wasn't clear to me whether deliberate or not.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, all of the changes I have made to bounds inference assumes that the type of the min, type of the max, and type of the operation are all equal.

can_prove(b_interval.min < 0 &&
b_interval.min > -t.bits())) {
interval.min = a_interval.min >> abs(b_interval.min);
if (a_interval.min.type().is_uint() || can_prove(a_interval.min >= 0)) {
rootjalex marked this conversation as resolved.
Show resolved Hide resolved
interval.min = a_interval.min >> abs(b_interval.min);
} else if (can_prove(a_interval.min < 0)) {
rootjalex marked this conversation as resolved.
Show resolved Hide resolved
if (b_interval.has_upper_bound()) {
rootjalex marked this conversation as resolved.
Show resolved Hide resolved
if (can_prove(b_interval.max <= 0)) {
// If b is strictly non-positive, then the magnitude can only decrease.
interval.min = a_interval.min;
} else {
// If b could be positive, then the magnitude might increase.
interval.min = min(a_interval.min, a_interval.min << b_interval.max);
}
}
}
// TODO: Are there any other cases we can handle here?
} else if (a_interval.has_lower_bound() &&
can_prove(a_interval.min >= 0)) {
// A positive value shifted cannot change sign.
interval.min = make_zero(t);
}

if (a_interval.has_upper_bound() &&
b_interval.has_upper_bound() &&
can_prove(b_interval.max >= 0 &&
Expand Down Expand Up @@ -3131,6 +3149,39 @@ void bounds_test() {
check(scope, x & 123, 0, 10); // Doesn't have to be a precise bitmask
check(scope, (x - 1) & 4095, 0, 4095); // LHS could be -1

// Regression tests on shifts (produced by z3).
{
ScopedBinding<Interval> xb(scope, "x", Interval(-123, Interval::pos_inf()));
ScopedBinding<Interval> yb(scope, "y", Interval(-6, 0));
// -123 << 0 = -123
check(scope, x << y, -123, Interval::pos_inf());
}
{
ScopedBinding<Interval> xb(scope, "x", Interval(-123, Interval::pos_inf()));
ScopedBinding<Interval> yb(scope, "y", Interval(-6, Interval::pos_inf()));
// A negative value can increase in magnitude if the rhs is positive.
check(scope, x << y, Interval::neg_inf(), Interval::pos_inf());
}
{
ScopedBinding<Interval> xb(scope, "x", Interval(-123, Interval::pos_inf()));
Var c("c");
ScopedBinding<Interval> yb(scope, "y", Interval(-6, c));
// Can't prove anything about the upper bound of y.
check(scope, x << y, min((-123) << c, -123), Interval::pos_inf());
}
{
ScopedBinding<Interval> xb(scope, "x", Interval(-123, Interval::pos_inf()));
ScopedBinding<Interval> yb(scope, "y", Interval(-6, 4));
// -123 << 4 = -1968
check(scope, x << y, -1968, Interval::pos_inf());
}
{
ScopedBinding<Interval> xb(scope, "x", Interval(24, Interval::pos_inf()));
ScopedBinding<Interval> yb(scope, "y", Interval(Interval::neg_inf(), -1));
// Cannot change sign, only can decrease magnitude.
check(scope, x << y, 0, Interval::pos_inf());
}

// If we clamp something unbounded as one type, the bounds should
// propagate through casts whenever the cast can be proved to not
// overflow.
Expand Down