Skip to content

Commit

Permalink
1) fixed constant folding for mod operation in CanonicalSimplify 2) a…
Browse files Browse the repository at this point in the history
…dded a unit test (apache#2487)
  • Loading branch information
derisavi authored and tqchen committed Jan 24, 2019
1 parent 12aca82 commit 859bda8
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/arithmetic/canonical.cc
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ class Canonical::Internal : public IRMutator {
return Binary_(op, e, a.value, b.value);
}
if (is_const(a.value) && is_const(b.value)) {
return ComputeExpr<Mul>(a.value, b.value);
return ComputeExpr<Mod>(a.value, b.value);
} else if (is_const(b.value)) {
return SumModConst(a.AsSum(), b.value);
} else {
Expand Down
10 changes: 10 additions & 0 deletions tests/cpp/ir_simplify_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,16 @@ TEST(IRSIMPLIFY, Mul) {
CHECK(is_zero(es));
}

TEST(IRSIMPLIFY, Mod) {
auto x = tvm::Integer(10);
auto y = tvm::Integer(12);
// Mod::make is used instead of % to avoid constant folding during
// calling operator%(x,y). Mod::make doesn't try constant folding,
// and therefore, the constant folding will be attempted in CanonicalSimplify
auto mod = tvm::ir::CanonicalSimplify(tvm::ir::Mod::make(x, y));
auto es = tvm::ir::CanonicalSimplify(mod - x);
CHECK(is_zero(es));
}
int main(int argc, char ** argv) {
testing::InitGoogleTest(&argc, argv);
testing::FLAGS_gtest_death_test_style = "threadsafe";
Expand Down

0 comments on commit 859bda8

Please sign in to comment.