Disable assert in runtime


#1

Hi there,

In current tvm, src/codegen/llvm/codegen_cpu.cc, it generate assert stmt. It would be nice to have a build config option to disable these runtime TVM asserts.

Below is just a example of disable it with environment variable.
It will be better to use build config to control it.

Please let me know your thought.
Thanks,
-W

diff --git a/src/codegen/llvm/codegen_cpu.cc b/src/codegen/llvm/codegen_cpu.cc
index fcad0f7b..5842e2ed 100644
--- a/src/codegen/llvm/codegen_cpu.cc
+++ b/src/codegen/llvm/codegen_cpu.cc
@@ -705,25 +705,32 @@ llvm::Value* CodeGenCPU::CreateIntrinsic(const Call* op) {
 }
 
 void CodeGenCPU::VisitStmt_(const AssertStmt* op) {
-  using llvm::BasicBlock;
-  llvm::Value* cond = MakeValue(op->condition);
-  std::ostringstream os;
-  os << "Assert fail: " << op->condition;
-  if (op->message.as<StringImm>()) {
-    os << ", " << op->message.as<StringImm>()->value;
+#ifndef NDEBUG
+  bool use_tvm_asserts = true;
+#else
+  bool use_tvm_asserts = (std::getenv("TVM_USE_ASSERT_STMT") != nullptr);
+#endif  // !NDEBUG
+  if (use_tvm_asserts) {
+    using llvm::BasicBlock;
+    llvm::Value* cond = MakeValue(op->condition);
+    std::ostringstream os;
+    os << "Assert fail: " << op->condition;
+    if (op->message.as<StringImm>()) {
+      os << ", " << op->message.as<StringImm>()->value;
+    }
+    llvm::Value* msg = GetConstString(os.str());
+    BasicBlock* fail_block = BasicBlock::Create(
+        *ctx_, "assert_fail", function_);
+    BasicBlock* end_block = BasicBlock::Create(
+        *ctx_, "assert_end", function_);
+    builder_->CreateCondBr(cond, end_block, fail_block, md_very_likely_branch_);
+    // fail condition.
+    builder_->SetInsertPoint(fail_block);
+    builder_->CreateCall(RuntimeTVMAPISetLastError(), {msg});
+    builder_->CreateRet(ConstInt32(-1));
+    // otherwise set it to be new end.
+    builder_->SetInsertPoint(end_block);
   }
-  llvm::Value* msg = GetConstString(os.str());
-  BasicBlock* fail_block = BasicBlock::Create(
-      *ctx_, "assert_fail", function_);
-  BasicBlock* end_block = BasicBlock::Create(
-      *ctx_, "assert_end", function_);
-  builder_->CreateCondBr(cond, end_block, fail_block, md_very_likely_branch_);
-  // fail condition.
-  builder_->SetInsertPoint(fail_block);
-  builder_->CreateCall(RuntimeTVMAPISetLastError(), {msg});
-  builder_->CreateRet(ConstInt32(-1));
-  // otherwise set it to be new end.
-  builder_->SetInsertPoint(end_block);
   CodeGenLLVM::VisitStmt_(op);
 }

#2

This is a reasonable feature that I agree we could put into build config option. Contributions are welcomed!


#3

Thanks for the feedback. Will send PR when I got chance.

Thanks,
-W


#4

This would be a really useful feature! Looking forward to it!