Take zero extent loops as NoOp and remove it and add unittest for the same (#3724)
authorUmang Yadav <umangyadav95@gmail.com>
Wed, 7 Aug 2019 15:51:18 +0000 (11:51 -0400)
committerTianqi Chen <tqchen@users.noreply.github.com>
Wed, 7 Aug 2019 15:51:18 +0000 (08:51 -0700)
src/pass/remove_no_op.cc
tests/python/unittest/test_pass_remove_no_op.py

index c367bd6..c7797f4 100644 (file)
@@ -70,6 +70,9 @@ class NoOpRemover : public IRMutator {
   Stmt Mutate_(const For* op, const Stmt& s) final {
     Stmt stmt = IRMutator::Mutate_(op, s);
     op = stmt.as<For>();
+    if (is_zero(op->extent)) {
+      return Evaluate::make(0);
+    }
     return is_no_op(op->body) ? MakeEvaluate({op->min, op->extent}) : stmt;
   }
   Stmt Mutate_(const Allocate* op, const Stmt& s) final {
index b6fb171..d1ea450 100644 (file)
@@ -39,7 +39,10 @@ def test_remove_no_op():
                            i + 1)
     stmt2 = tvm.make.Block(stmt, store)
     assert(tvm.ir_pass.RemoveNoOp(stmt2) == store)
-
+    # remove zero extent loop
+    stmt3 = tvm.make.For(i, 0, 0, 0, 0, store)
+    ret = tvm.ir_pass.RemoveNoOp(stmt3)
+    assert(isinstance(ret, tvm.stmt.Evaluate))
 
 if __name__ == "__main__":
     test_remove_no_op()