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

Additional canonicalization added for AddNode #5846

Merged
merged 2 commits into from
Jun 18, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion python/tvm/tir/expr.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def __add__(self, other):
return _generic.add(self, other)

def __radd__(self, other):
return self.__add__(other)
return _generic.add(other, self)

def __sub__(self, other):
return _generic.subtract(self, other)
Expand Down
1 change: 1 addition & 0 deletions src/arith/rewrite_simplify.cc
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ PrimExpr RewriteSimplifier::Impl::VisitExpr_(const AddNode* op) {
// canonicalization rule
// will try rewrite again after canonicalization.
TVM_TRY_RECURSIVE_REWRITE(x + (c1 - y), (x - y) + c1);
TVM_TRY_RECURSIVE_REWRITE((c1 - y) + x, (x - y) + c1);
TVM_TRY_RECURSIVE_REWRITE(x + c1 + y, (x + y) + c1);
TVM_TRY_RECURSIVE_REWRITE(x + (c1 + y), (x + y) + c1);
TVM_TRY_RECURSIVE_REWRITE(x + max(y, z), max(y, z) + x);
Expand Down
10 changes: 10 additions & 0 deletions tests/python/unittest/test_arith_rewrite_simplify.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,16 @@ def test_add_index_simplify():
ck.verify(y * x + 10 * x, x * (y + 10))
ck.verify(x * y + 10 * x, x * (y + 10))

ck.verify((2 * z) + tvm.te.min(x, y - (2 * z)), tvm.te.min(x + (z * 2), y))
ck.verify(y * x + x, x * (y + 1))
ck.verify(x * y + x, x * (y + 1))
ck.verify((x + 10) + 13, x + 23)
ck.verify((x + 10) + (13 + z), x + z + 23)
ck.verify(x * y + 10 * x, x * (y + 10))
ck.verify(y * x + x * 3, x * (y + 3))
ck.verify(x + 3 + y, x + y + 3)
ck.verify((3 - y) + x, x - y + 3)


# canonicalization
ck.verify(x + 2 + 3 + 4 + x, x * 2 + 9);
Expand Down