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 track bounds through wrapping casts in simplifier #7813

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
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
18 changes: 5 additions & 13 deletions src/Simplify_Cast.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,12 @@ Expr Simplify::visit(const Cast *op, ExprInfo *bounds) {
if (bounds) {
// If either the min value or the max value can't be represented
// in the destination type, or the min/max value is undefined,
// the bounds need to be cleared (one-sided for no_overflow,
// both sides for overflow types).
if ((bounds->min_defined && !op->type.can_represent(bounds->min)) || !bounds->min_defined) {
// the bounds need to be cleared.
if ((bounds->min_defined && !op->type.can_represent(bounds->min)) ||
!bounds->min_defined ||
(bounds->max_defined && !op->type.can_represent(bounds->max)) ||
!bounds->max_defined) {
bounds->min_defined = false;
if (!no_overflow(op->type)) {
// If the type overflows, this invalidates the max too.
bounds->max_defined = false;
}
}
if ((bounds->max_defined && !op->type.can_represent(bounds->max)) || !bounds->max_defined) {
if (!no_overflow(op->type)) {
// If the type overflows, this invalidates the min too.
bounds->min_defined = false;
}
bounds->max_defined = false;
}
if (!op->type.can_represent(bounds->alignment.modulus) ||
Expand Down
6 changes: 6 additions & 0 deletions test/correctness/simplify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

using namespace Halide;
using namespace Halide::Internal;
using namespace Halide::ConciseCasts;

#define internal_assert _halide_user_assert

Expand Down Expand Up @@ -1316,6 +1317,11 @@ void check_bounds() {
check(max(x * 4 + 63, y) - max(y - 3, x * 4), clamp(x * 4 - y, -63, -3) + 66);
check(max(x * 4, y - 3) - max(x * 4 + 63, y), clamp(y - x * 4, 3, 63) + -66);
check(max(y - 3, x * 4) - max(x * 4 + 63, y), clamp(y - x * 4, 3, 63) + -66);

// Check the simplifier doesn't make constant-bounds-based simplifications
// through potentially-wrapping casts.
check(max(i32(select(x == 0, u32(0), u32(Expr(0x80000000U)))), -1),
max(i32(select(x == 0, u32(0), u32(Expr(0x80000000U)))), -1));
}

void check_boolean() {
Expand Down