Skip to content

Commit

Permalink
[Fix][Arith] Fix canonical simplification of LE
Browse files Browse the repository at this point in the history
PR apache#15471 enhances the simplification for LE, while missed a case
where the upper bound `kPosInf` is divisible by a factor. Therefore,
prior to this PR, when simplifying `x * 1024 + y < z * 7168`, it will
fails with the error message
```
InternalError: Check failed: value < 1LL << (dtype.bits() - 1)
(8589934591 vs. 2147483648) : ValueError: Literal value 8589934591
exceeds maximum of int32
```

This is just because the upper bound 7 here divides `kPosInf` the
maximum value of int64, which passes an "if" condition in apache#15471
unexpectedly.

This PR fixes the issue.
  • Loading branch information
MasterJH5574 committed Mar 12, 2024
1 parent 62fc412 commit f5e3882
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/arith/canonical_simplify.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1422,6 +1422,7 @@ PrimExpr CanonicalSimplifier::Impl::VisitExpr_(const LTNode* op) {
divisible.CopyOnWrite()->DivideBy(gcd);
return Rewriter::VisitExpr(divisible->Normalize() < make_zero(dtype));
} else if (extra->args.size() == 1 &&
extra->args[0]->upper_factor != ConstIntBoundNode::kPosInf &&
extra->args[0]->upper_factor % (gcd * extra->args[0]->lower_factor) == 0) {
// Case 2. xn == yn % m, where m % d == 0
divisible.CopyOnWrite()->DivideBy(gcd);
Expand Down
5 changes: 5 additions & 0 deletions tests/python/arith/test_arith_canonical_simplify.py
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,11 @@ def test_simplify_le():
)
ck.verify(tx // 2 % 8 + vec < 8, tx % 16 // 2 + vec < 8)

# Case 3. No failure
x, y, z = te.var("x"), te.var("y"), te.var("z")
ck.analyzer.bind(y, tvm.ir.Range(0, 1024))
ck.verify(x * 1024 + y < z * 7168, x - z * 7 < 0)


if __name__ == "__main__":
tvm.testing.main()

0 comments on commit f5e3882

Please sign in to comment.