From 12790479c40fbba2b498002db3b3830047030046 Mon Sep 17 00:00:00 2001 From: FrozenGene Date: Fri, 15 Nov 2019 01:23:19 +0800 Subject: [PATCH 1/6] Add disable assert --- include/tvm/build_module.h | 4 ++++ python/tvm/build_module.py | 3 ++- src/codegen/build_module.cc | 1 + src/codegen/llvm/codegen_cpu.cc | 36 +++++++++++++++++---------------- src/codegen/llvm/codegen_cpu.h | 2 ++ 5 files changed, 28 insertions(+), 18 deletions(-) diff --git a/include/tvm/build_module.h b/include/tvm/build_module.h index 7114a4550331..a83288ce3662 100644 --- a/include/tvm/build_module.h +++ b/include/tvm/build_module.h @@ -229,6 +229,9 @@ class BuildConfigNode : public Node { /*! \brief Whether to disable loop vectorization. */ bool disable_vectorize = false; + /*! \brief Whether to disable assert stmt generation. */ + bool disable_assert = false; + void VisitAttrs(AttrVisitor* v) { v->Visit("data_alignment", &data_alignment); v->Visit("offset_factor", &offset_factor); @@ -244,6 +247,7 @@ class BuildConfigNode : public Node { v->Visit("instrument_bound_checkers", &instrument_bound_checkers); v->Visit("disable_select_rewriting", &disable_select_rewriting); v->Visit("disable_vectorize", &disable_vectorize); + v->Visit("disable_assert", &disable_assert); } static constexpr const char* _type_key = "BuildConfig"; diff --git a/python/tvm/build_module.py b/python/tvm/build_module.py index 217318ebfa84..f96e28323595 100644 --- a/python/tvm/build_module.py +++ b/python/tvm/build_module.py @@ -144,7 +144,8 @@ class BuildConfig(NodeBase): "dump_pass_ir": False, "instrument_bound_checkers": False, "disable_select_rewriting": False, - "disable_vectorize": False + "disable_vectorize": False, + "disable_assert": False } _dump_ir = DumpIR() diff --git a/src/codegen/build_module.cc b/src/codegen/build_module.cc index 3f279f8772df..ac991d4bfea3 100644 --- a/src/codegen/build_module.cc +++ b/src/codegen/build_module.cc @@ -672,6 +672,7 @@ TVM_STATIC_IR_FUNCTOR(IRPrinter, vtable) p->stream << "instrument_bound_checkers=" << op->instrument_bound_checkers << ", "; p->stream << "disable_select_rewriting=" << op->disable_select_rewriting; p->stream << "disable_vectorize=" << op->disable_vectorize; + p->stream << "disable_assert=" << op->disable_assert; p->stream << ")"; }); diff --git a/src/codegen/llvm/codegen_cpu.cc b/src/codegen/llvm/codegen_cpu.cc index 0ba0c584a590..ebd9661c38d4 100644 --- a/src/codegen/llvm/codegen_cpu.cc +++ b/src/codegen/llvm/codegen_cpu.cc @@ -847,25 +847,27 @@ 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()) { - os << ", " << op->message.as()->value; - } - llvm::Value* msg = GetConstString(os.str()); - BasicBlock* fail_block = BasicBlock::Create( + if (!BuildConfig::Current()->disable_assert) { + using llvm::BasicBlock; + llvm::Value *cond = MakeValue(op->condition); + std::ostringstream os; + os << "Assert fail: " << op->condition; + if (op->message.as()) { + os << ", " << op->message.as()->value; + } + llvm::Value *msg = GetConstString(os.str()); + BasicBlock *fail_block = BasicBlock::Create( *ctx_, "assert_fail", function_); - BasicBlock* end_block = BasicBlock::Create( + 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); + 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); } diff --git a/src/codegen/llvm/codegen_cpu.h b/src/codegen/llvm/codegen_cpu.h index 52e6f6c6ef90..581f5d4f987b 100644 --- a/src/codegen/llvm/codegen_cpu.h +++ b/src/codegen/llvm/codegen_cpu.h @@ -29,8 +29,10 @@ #include #include #include +#include #include "codegen_llvm.h" + namespace tvm { namespace codegen { From 7c3e98d9a0b9acf361f12d67513decc7ab5a5d5d Mon Sep 17 00:00:00 2001 From: FrozenGene Date: Fri, 15 Nov 2019 01:30:30 +0800 Subject: [PATCH 2/6] Remove blank --- src/codegen/llvm/codegen_cpu.h | 1 - 1 file changed, 1 deletion(-) diff --git a/src/codegen/llvm/codegen_cpu.h b/src/codegen/llvm/codegen_cpu.h index 581f5d4f987b..8b6242bd643b 100644 --- a/src/codegen/llvm/codegen_cpu.h +++ b/src/codegen/llvm/codegen_cpu.h @@ -32,7 +32,6 @@ #include #include "codegen_llvm.h" - namespace tvm { namespace codegen { From 1a135c60480d5dad395238aa0a68deb6f147737c Mon Sep 17 00:00:00 2001 From: FrozenGene Date: Fri, 15 Nov 2019 01:35:13 +0800 Subject: [PATCH 3/6] Solve CPP Lint of system header --- src/codegen/llvm/codegen_cpu.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/codegen/llvm/codegen_cpu.h b/src/codegen/llvm/codegen_cpu.h index 8b6242bd643b..a1eaf419e274 100644 --- a/src/codegen/llvm/codegen_cpu.h +++ b/src/codegen/llvm/codegen_cpu.h @@ -24,12 +24,12 @@ #ifndef TVM_CODEGEN_LLVM_CODEGEN_CPU_H_ #define TVM_CODEGEN_LLVM_CODEGEN_CPU_H_ +#include #include #include #include #include #include -#include #include "codegen_llvm.h" namespace tvm { From 91f24fdd1724c8fa05bf99f83391cb7dea056750 Mon Sep 17 00:00:00 2001 From: FrozenGene Date: Fri, 15 Nov 2019 14:20:05 +0800 Subject: [PATCH 4/6] Add SkipAssert Pass --- include/tvm/ir_pass.h | 7 +++++ src/codegen/codegen.cc | 10 ++++++- src/codegen/llvm/codegen_cpu.cc | 38 +++++++++++++------------- src/codegen/llvm/codegen_cpu.h | 1 - src/pass/skip_assert.cc | 47 +++++++++++++++++++++++++++++++++ 5 files changed, 81 insertions(+), 22 deletions(-) create mode 100644 src/pass/skip_assert.cc diff --git a/include/tvm/ir_pass.h b/include/tvm/ir_pass.h index 76d7d61f1e3d..5c5c4bb2f452 100644 --- a/include/tvm/ir_pass.h +++ b/include/tvm/ir_pass.h @@ -563,6 +563,13 @@ LoweredFunc LowerCustomDatatypes(LoweredFunc f, const std::string& target); */ LoweredFunc InferFragment(LoweredFunc f); +/*! + * \brief skip assert stmt generation + * \param f The function to be transformed. + * \return Transformed function. + */ +LoweredFunc SkipAssert(LoweredFunc f); + /*! * \brief Verify if memory accesses are legal for a specific target device type. * diff --git a/src/codegen/codegen.cc b/src/codegen/codegen.cc index ed9484b211b0..836d04c3d7e3 100644 --- a/src/codegen/codegen.cc +++ b/src/codegen/codegen.cc @@ -26,6 +26,7 @@ #include #include #include +#include #include #include #include @@ -40,12 +41,19 @@ runtime::Module Build(const Array& funcs, if (pos != std::string::npos) { mode = mode.substr(0, pos); } + Array transformed_funcs; + for (const auto& x : funcs) { + if(BuildConfig::Current()->disable_assert) { + auto func = ir::SkipAssert(x); + transformed_funcs.push_back(func); + } + } std::string build_f_name = "codegen.build_" + mode; // the build function. const PackedFunc* bf = runtime::Registry::Get(build_f_name); CHECK(bf != nullptr) << "Target " << target << " is not enabled"; - runtime::Module m = (*bf)(funcs, target); + runtime::Module m = transformed_funcs.empty() ? (*bf)(funcs, target) : (*bf)(transformed_funcs, target); return m; } diff --git a/src/codegen/llvm/codegen_cpu.cc b/src/codegen/llvm/codegen_cpu.cc index ebd9661c38d4..87f6d99c51d5 100644 --- a/src/codegen/llvm/codegen_cpu.cc +++ b/src/codegen/llvm/codegen_cpu.cc @@ -847,27 +847,25 @@ llvm::Value* CodeGenCPU::CreateIntrinsic(const Call* op) { } void CodeGenCPU::VisitStmt_(const AssertStmt* op) { - if (!BuildConfig::Current()->disable_assert) { - using llvm::BasicBlock; - llvm::Value *cond = MakeValue(op->condition); - std::ostringstream os; - os << "Assert fail: " << op->condition; - if (op->message.as()) { - os << ", " << op->message.as()->value; - } - llvm::Value *msg = GetConstString(os.str()); - BasicBlock *fail_block = BasicBlock::Create( + using llvm::BasicBlock; + llvm::Value* cond = MakeValue(op->condition); + std::ostringstream os; + os << "Assert fail: " << op->condition; + if (op->message.as()) { + os << ", " << op->message.as()->value; + } + llvm::Value* msg = GetConstString(os.str()); + BasicBlock* fail_block = BasicBlock::Create( *ctx_, "assert_fail", function_); - BasicBlock *end_block = BasicBlock::Create( + 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); - } + 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); } @@ -956,4 +954,4 @@ void CodeGenCPU::VisitStmt_(const For* op) { } // namespace codegen } // namespace tvm -#endif // TVM_LLVM_VERSION +#endif // TVM_LLVM_VERSION \ No newline at end of file diff --git a/src/codegen/llvm/codegen_cpu.h b/src/codegen/llvm/codegen_cpu.h index a1eaf419e274..52e6f6c6ef90 100644 --- a/src/codegen/llvm/codegen_cpu.h +++ b/src/codegen/llvm/codegen_cpu.h @@ -24,7 +24,6 @@ #ifndef TVM_CODEGEN_LLVM_CODEGEN_CPU_H_ #define TVM_CODEGEN_LLVM_CODEGEN_CPU_H_ -#include #include #include #include diff --git a/src/pass/skip_assert.cc b/src/pass/skip_assert.cc new file mode 100644 index 000000000000..5f310a61dfe3 --- /dev/null +++ b/src/pass/skip_assert.cc @@ -0,0 +1,47 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +#include +#include +#include + +namespace tvm { +namespace ir { + +class AssertSkipper : public IRMutator { + public: + Stmt Mutate_(const AssertStmt* op, const Stmt& s) final { + Stmt stmt = IRMutator::Mutate_(op, s); + op = stmt.as(); + return op->body; + } +}; + +Stmt SkipAssert(Stmt stmt) { + return AssertSkipper().Mutate(stmt); +} + +LoweredFunc SkipAssert(LoweredFunc f) { + auto n = make_node(*f.operator->()); + n->body = SkipAssert(f->body); + return LoweredFunc(n); +} + +} // namespace ir +} // namespace tvm From 9b5571c49fcef0ab0f2d31b341b4067a8d76df66 Mon Sep 17 00:00:00 2001 From: FrozenGene Date: Fri, 15 Nov 2019 14:21:08 +0800 Subject: [PATCH 5/6] Add blank line of codegen_cpu.cc --- src/codegen/llvm/codegen_cpu.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/codegen/llvm/codegen_cpu.cc b/src/codegen/llvm/codegen_cpu.cc index 87f6d99c51d5..0ba0c584a590 100644 --- a/src/codegen/llvm/codegen_cpu.cc +++ b/src/codegen/llvm/codegen_cpu.cc @@ -954,4 +954,4 @@ void CodeGenCPU::VisitStmt_(const For* op) { } // namespace codegen } // namespace tvm -#endif // TVM_LLVM_VERSION \ No newline at end of file +#endif // TVM_LLVM_VERSION From 91b3aaa06309b0dfa96c727accc324f4b29a78f3 Mon Sep 17 00:00:00 2001 From: FrozenGene Date: Fri, 15 Nov 2019 14:26:44 +0800 Subject: [PATCH 6/6] cpp lint --- src/codegen/codegen.cc | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/codegen/codegen.cc b/src/codegen/codegen.cc index 836d04c3d7e3..4ea37ba7317b 100644 --- a/src/codegen/codegen.cc +++ b/src/codegen/codegen.cc @@ -43,7 +43,7 @@ runtime::Module Build(const Array& funcs, } Array transformed_funcs; for (const auto& x : funcs) { - if(BuildConfig::Current()->disable_assert) { + if (BuildConfig::Current()->disable_assert) { auto func = ir::SkipAssert(x); transformed_funcs.push_back(func); } @@ -53,7 +53,9 @@ runtime::Module Build(const Array& funcs, const PackedFunc* bf = runtime::Registry::Get(build_f_name); CHECK(bf != nullptr) << "Target " << target << " is not enabled"; - runtime::Module m = transformed_funcs.empty() ? (*bf)(funcs, target) : (*bf)(transformed_funcs, target); + runtime::Module m = transformed_funcs.empty() ? + (*bf)(funcs, target) : + (*bf)(transformed_funcs, target); return m; }