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

[Bugfix] Fix other div zero errors also in rewrite_simplify #8983

Merged
merged 9 commits into from
Sep 16, 2021
Merged
Show file tree
Hide file tree
Changes from 7 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
3 changes: 3 additions & 0 deletions src/arith/rewrite_simplify.cc
Original file line number Diff line number Diff line change
Expand Up @@ -645,6 +645,7 @@ PrimExpr RewriteSimplifier::Impl::VisitExpr_(const ModNode* op) {
if (truncmod(ramp(b1, c1, lanes), broadcast(c2, lanes)).Match(ret)) {
int64_t c1val = c1.Eval()->value;
int64_t c2val = c2.Eval()->value;
ICHECK(c2val != 0) << "division by zero";
if (c1val % c2val == 0) {
return broadcast(truncmod(b1, c2), lanes).Eval();
}
Expand Down Expand Up @@ -724,6 +725,7 @@ PrimExpr RewriteSimplifier::Impl::VisitExpr_(const FloorDivNode* op) {
if (floordiv(ramp(b1, c1, lanes), broadcast(c2, lanes)).Match(ret)) {
int64_t c1val = c1.Eval()->value;
int64_t c2val = c2.Eval()->value;
ICHECK(c2val != 0) << "division by zero";
if (c1val % c2val == 0) {
return ramp(floordiv(b1, c2), floordiv(c1, c2), lanes).Eval();
}
Expand Down Expand Up @@ -852,6 +854,7 @@ PrimExpr RewriteSimplifier::Impl::VisitExpr_(const FloorModNode* op) {
if (floormod(ramp(b1, c1, lanes), broadcast(c2, lanes)).Match(ret)) {
int64_t c1val = c1.Eval()->value;
int64_t c2val = c2.Eval()->value;
ICHECK(c2val != 0) << "division by zero";
if (c1val % c2val == 0) {
return broadcast(floormod(b1, c2), lanes).Eval();
}
Expand Down
16 changes: 15 additions & 1 deletion tests/python/unittest/test_arith_rewrite_simplify.py
Original file line number Diff line number Diff line change
Expand Up @@ -934,9 +934,23 @@ def test_shift_left_simplify():

def test_div_zero_simplify():
ck = RewriteChecker()
ramp = tvm.tir.Ramp(1, 1, 2)
broadcast = tvm.tir.Broadcast(0, 2)

with pytest.raises(tvm.error.TVMError) as cm:
ck.analyzer.rewrite_simplify(tvm.tir.Div(tvm.tir.Ramp(1, 1, 2), tvm.tir.Broadcast(0, 2)))
ck.analyzer.rewrite_simplify(tvm.tir.Div(ramp, broadcast))
assert "division by zero" in str(cm.execption)

with pytest.raises(tvm.error.TVMError) as cm:
ck.analyzer.rewrite_simplify(tvm.tir.Mod(ramp, broadcast))
assert "division by zero" in str(cm.execption)

with pytest.raises(tvm.error.TVMError) as cm:
ck.analyzer.rewrite_simplify(tvm.tir.FloorDiv(ramp, broadcast))
assert "division by zero" in str(cm.execption)

with pytest.raises(tvm.error.TVMError) as cm:
ck.analyzer.rewrite_simplify(tvm.tir.FloorMod(ramp, broadcast))
assert "division by zero" in str(cm.execption)


Expand Down