Skip to content

Commit

Permalink
[Arith] Add simplification rule for x - max(x+y, z) (#14271)
Browse files Browse the repository at this point in the history
This parallels an existing simplification rule for `x - min(x,y, z)`,
applying the same cancellation for `max`.
  • Loading branch information
Lunderberg authored Mar 10, 2023
1 parent 852f97d commit c59bc29
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/arith/rewrite_simplify.cc
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,8 @@ PrimExpr RewriteSimplifier::Impl::VisitExpr_(const SubNode* op) {

TVM_TRY_REWRITE(matches_one_of(x - min(x + y, z), x - min(y + x, z)), max(0 - y, x - z));
TVM_TRY_REWRITE(matches_one_of(x - min(z, x + y), x - min(z, y + x)), max(x - z, 0 - y));
TVM_TRY_REWRITE(matches_one_of(x - max(x + y, z), x - max(y + x, z)), min(0 - y, x - z));
TVM_TRY_REWRITE(matches_one_of(x - max(z, x + y), x - max(z, y + x)), min(x - z, 0 - y));

TVM_TRY_REWRITE(min(x, y) - min(y, x), ZeroWithTypeLike(x));
TVM_TRY_REWRITE(max(x, y) - max(y, x), ZeroWithTypeLike(x));
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 @@ -360,6 +360,10 @@ class TestSubIndex(BaseCompare):
TestCase(tvm.te.max(x, y) - tvm.te.max(y, x), 0),
TestCase(tvm.te.min(x, y) - tvm.te.min(x + 10, y + 10), -10),
TestCase(tvm.te.min(x + 10, y + 1) - tvm.te.min(x, y - 9), 10),
TestCase(x - tvm.te.max(x + y, 0), tvm.te.min(0 - y, x)),
TestCase(x - tvm.te.max(0, x + y), tvm.te.min(x, 0 - y)),
TestCase(x - tvm.te.min(x + y, 0), tvm.te.max(0 - y, x)),
TestCase(x - tvm.te.min(0, x + y), tvm.te.max(x, 0 - y)),
# DivMod patterns
# truc div
TestCase(x - tdiv(x, 3) * 3, tmod(x, 3)),
Expand Down

0 comments on commit c59bc29

Please sign in to comment.