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

[ARITH][BACKPORT-0.6] fix a min/max simplify bug #5749

Merged
merged 4 commits into from
Jun 9, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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: 6 additions & 2 deletions src/arith/rewrite_simplify.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1026,7 +1026,9 @@ PrimExpr RewriteSimplifier::Impl::VisitExpr_(const MinNode* op) {
int64_t c1val = c1.Eval()->value;
int64_t c2val = c2.Eval()->value;
if (c2val % c1val == 0) {
if (c2val / c1val >= 0) {
if (c2val == 0) {
return c1val > 0 ? (min(x, 0) * c1val).Eval() : (max(x, 0) * c1val).Eval();
} else if (c2val / c1val > 0) {
xqdan marked this conversation as resolved.
Show resolved Hide resolved
return (min(x, c2val / c1val) * c1val).Eval();
} else {
return (max(x, c2val / c1val) * c1val).Eval();
Expand Down Expand Up @@ -1186,7 +1188,9 @@ PrimExpr RewriteSimplifier::Impl::VisitExpr_(const MaxNode* op) {
int64_t c1val = c1.Eval()->value;
int64_t c2val = c2.Eval()->value;
if (c2val % c1val == 0) {
if (c2val / c1val >= 0) {
if (c2val == 0) {
return c1val > 0 ? (max(x, 0) * c1val).Eval() : (min(x, 0) * c1val).Eval();
} else if (c2val / c1val > 0) {
return (max(x, c2val / c1val) * c1val).Eval();
} else {
return (min(x, c2val / c1val) * c1val).Eval();
Expand Down
4 changes: 4 additions & 0 deletions tests/python/unittest/test_arith_rewrite_simplify.py
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,10 @@ def test_min_index_simplify():
ck.verify(tvm.te.min(tvm.te.min(x, 11), 10), tvm.te.min(x, 10))

ck.verify(tvm.te.min(x * 3, 9), tvm.te.min(x, 3) * 3)
ck.verify(tvm.te.min(x * 2, 0), tvm.te.min(x, 0) * 2)
ck.verify(tvm.te.max(x * 2, 0), tvm.te.max(x, 0) * 2)
ck.verify(tvm.te.min(0 - x * 2, 0), tvm.te.max(x, 0) * -2)
ck.verify(tvm.te.max(0 - x * 2, 0), tvm.te.min(x, 0) * -2)
ck.verify(tvm.te.min(3 - x, 2), 3 - tvm.te.max(x, 1))

# DivMod rules
Expand Down