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] RewriteSimplifier: min/max, logical, select #2768

Merged
merged 3 commits into from
Mar 14, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
14 changes: 10 additions & 4 deletions src/arithmetic/rewrite_simplify.cc
Original file line number Diff line number Diff line change
Expand Up @@ -734,14 +734,16 @@ Mutate_(const Min* op, const Expr& self) {
c1.Eval()->value + 1 == c2.Eval()->value);
TVM_TRY_REWRITE_IF(min(((x + c1) / c2) * c2, max(x, c2)), max(x, c2),
c2.Eval()->value > 0 &&
c1.Eval()->value + 1 == c2.Eval()->value);
c1.Eval()->value + 1 == c2.Eval()->value &&
CanProveGreaterEqual(x.Eval(), 0));

TVM_TRY_REWRITE_IF(min(x, ((x + c1) / c2) * c2), x,
c2.Eval()->value > 0 &&
c1.Eval()->value + 1 == c2.Eval()->value);
TVM_TRY_REWRITE_IF(min(max(x, c2), ((x + c1) / c2) * c2), max(x, c2),
c2.Eval()->value > 0 &&
c1.Eval()->value + 1 == c2.Eval()->value);
c1.Eval()->value + 1 == c2.Eval()->value &&
CanProveGreaterEqual(x.Eval(), 0));

TVM_TRY_REWRITE(min(max(x, y), min(x, y)), min(x, y));
TVM_TRY_REWRITE(min(max(x, y), min(y, x)), min(x, y));
tqchen marked this conversation as resolved.
Show resolved Hide resolved
Expand Down Expand Up @@ -1060,6 +1062,8 @@ Mutate_(const LT* op, const Expr& self) {

TVM_TRY_REWRITE_IF(x * c1 < y * c1, x < y,
c1.Eval()->value > 0);
tqchen marked this conversation as resolved.
Show resolved Hide resolved
TVM_TRY_REWRITE_IF(x * c1 < y * c1, y < x,
c1.Eval()->value < 0);

// require c1 > 0 to work for any div mode
TVM_TRY_REWRITE_IF(x * c2 < c1, x < (c1 - 1) / c2 + 1,
Expand Down Expand Up @@ -1127,6 +1131,8 @@ Mutate_(const Not* op, const Expr& self) {
TVM_TRY_REWRITE(!(x > y), x <= y);
TVM_TRY_REWRITE(!(x == y), x != y);
TVM_TRY_REWRITE(!(x != y), x == y);
tqchen marked this conversation as resolved.
Show resolved Hide resolved
TVM_TRY_REWRITE(!(x || y), (!x) && (!y));
TVM_TRY_REWRITE(!(x && y), (!x) || (!y));
Copy link
Contributor

Choose a reason for hiding this comment

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

It would be better to use TVM_TRY_RECURSIVE_REWRITE here, because otherwise nots are not propagated all the way down in some expressions (like ((max(max(i, x), y) <= 9) && ((x == i) || (y == i)))).

Copy link
Member Author

Choose a reason for hiding this comment

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

updated

return ret;
}

Expand Down Expand Up @@ -1224,8 +1230,8 @@ Mutate_(const Or* op, const Expr& self) {
TVM_TRY_REWRITE_IF(c2 <= x || x <= c1, ctrue,
c2.Eval()->value <= c1.Eval()->value + 1);

TVM_TRY_REWRITE(x == c1 || x != c2, x != c1 || c1 == c2);
TVM_TRY_REWRITE(x != c2 || x == c1, x != c1 || c1 == c2);
TVM_TRY_REWRITE(x != c1 || x == c2, x != c1 || c1 == c2);
TVM_TRY_REWRITE(x == c2 || x != c1, x != c1 || c1 == c2);
return ret;
}

Expand Down
4 changes: 3 additions & 1 deletion tests/python/unittest/test_arith_rewrite_simplify.py
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,7 @@ def test_min_index_simplify():
ck.verify(tvm.min(3 - x, 2 - x), 2 - x)

ck.verify(tvm.min((x + 3) / 4 * 4, x), x)
ck.analyzer.update(x, tvm.arith.ConstIntBound(0, 1000))
ck.verify(tvm.min((x + 3) / 4 * 4, tvm.max(x, 4)), tvm.max(x, 4))
ck.verify(tvm.min(x, (x + 3) / 4 * 4), x)
ck.verify(tvm.min(tvm.max(x, 4), (x + 3) / 4 * 4), tvm.max(x, 4))
Copy link
Contributor

Choose a reason for hiding this comment

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

I think the boundary of x should be reset after the check of this rule.

Expand Down Expand Up @@ -417,6 +418,7 @@ def test_cmp_simplify():
ck.verify(100 < x + 1, tvm.expr.LT(99, x))
ck.verify(1 < 100 - x, tvm.expr.LT(x, 99))
ck.verify(x * 3 < y * 3, x < y)
ck.verify(x * (-3) < y * (-3), y < x)
ck.verify(x * 3 >= y * 3, y <= x)

ck.verify(x * 4 >= 2, tvm.expr.LE(1, x))
Expand Down Expand Up @@ -482,7 +484,7 @@ def test_logical_simplify():
ck.verify(tvm.expr.Or(1 < x, x <= 1), tvm.const(True, "bool"))
ck.verify(tvm.expr.Or(x <= 1, 2 <= x), tvm.const(True, "bool"))
ck.verify(tvm.expr.Or(2 <= x, x <= 1), tvm.const(True, "bool"))
ck.verify(tvm.expr.Or(x != 1, x == 2), x != 2)
ck.verify(tvm.expr.Or(x != 1, x == 2), x != 1)


if __name__ == "__main__":
Expand Down