Skip to content

Commit

Permalink
[ARITH][BACKPORT-0.6] fix a min/max simplify bug (apache#5749)
Browse files Browse the repository at this point in the history
* fix a min/max simplify bug

* fix cpplint

* turn into oposite when c1val<0 and add more case

* fix c1=0

Co-authored-by: xqdan <danxiaoqiang@huawei.com>
  • Loading branch information
2 people authored and trevor-m committed Jun 18, 2020
1 parent 4c0e8cd commit f4dd60c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/arith/rewrite_simplify.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1025,8 +1025,11 @@ PrimExpr RewriteSimplifier::Impl::VisitExpr_(const MinNode* op) {
if (min(x * c1, c2).Match(ret)) {
int64_t c1val = c1.Eval()->value;
int64_t c2val = c2.Eval()->value;
if (c1val == 0) {
return c2val < 0 ? c2.Eval() : c1.Eval();
}
if (c2val % c1val == 0) {
if (c2val / c1val >= 0) {
if (c1val > 0) {
return (min(x, c2val / c1val) * c1val).Eval();
} else {
return (max(x, c2val / c1val) * c1val).Eval();
Expand Down Expand Up @@ -1185,8 +1188,11 @@ PrimExpr RewriteSimplifier::Impl::VisitExpr_(const MaxNode* op) {
if (max(x * c1, c2).Match(ret)) {
int64_t c1val = c1.Eval()->value;
int64_t c2val = c2.Eval()->value;
if (c1val == 0) {
return c2val > 0 ? c2.Eval() : c1.Eval();
}
if (c2val % c1val == 0) {
if (c2val / c1val >= 0) {
if (c1val > 0) {
return (max(x, c2val / c1val) * c1val).Eval();
} else {
return (min(x, c2val / c1val) * c1val).Eval();
Expand Down
12 changes: 12 additions & 0 deletions tests/python/unittest/test_arith_rewrite_simplify.py
Original file line number Diff line number Diff line change
Expand Up @@ -529,7 +529,13 @@ 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.min(0 - x * 2, 0), tvm.te.max(x, 0) * -2)
ck.verify(tvm.te.min(3 - x, 2), 3 - tvm.te.max(x, 1))
ck.verify(tvm.te.min(x * (-2), -4), tvm.te.max(x, 2) * -2)
ck.verify(tvm.te.min(x * (-2), 4), tvm.te.max(x, -2) * -2)
ck.verify(tvm.te.min(x * (0), 4), 0)
ck.verify(tvm.te.min(x * (0), -4), -4)

# DivMod rules
# truc div
Expand Down Expand Up @@ -610,6 +616,12 @@ def test_max_index_simplify():

ck.verify(tvm.te.max(x * 3, 9), tvm.te.max(x, 3) * 3)
ck.verify(tvm.te.max(3 - x, 1), 3 - tvm.te.min(x, 2))
ck.verify(tvm.te.max(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.max(x * (-2), -4), tvm.te.min(x, 2) * -2)
ck.verify(tvm.te.max(x * (-2), 4), tvm.te.min(x, -2) * -2)
ck.verify(tvm.te.max(x * (0), 4), 4)
ck.verify(tvm.te.max(x * (0), -4), 0)

# DivMod rules
# truc div
Expand Down

0 comments on commit f4dd60c

Please sign in to comment.