From ccded94d8ff35b838e30de544e89611d02b62d42 Mon Sep 17 00:00:00 2001 From: Vincent Zhao Date: Mon, 2 Mar 2020 19:31:55 +0000 Subject: [PATCH] Avoid using IR str concat to create LLVM module --- src/relay/backend/build_module.cc | 2 +- src/target/llvm/llvm_module.cc | 28 +++++++--------------------- 2 files changed, 8 insertions(+), 22 deletions(-) diff --git a/src/relay/backend/build_module.cc b/src/relay/backend/build_module.cc index 624ad17b7fd2..80919bc30baf 100644 --- a/src/relay/backend/build_module.cc +++ b/src/relay/backend/build_module.cc @@ -452,7 +452,7 @@ class RelayBuildModule : public runtime::ModuleNode { if (target_host.defined() && target_host->target_name == "llvm") { // If we can decide the target is LLVM, we then create an empty LLVM module. - ret_.mod = (*pf)(target_host->str()); + ret_.mod = (*pf)(target_host->str(), "empty_module"); } else { // If we cannot decide the target is LLVM, we create an empty CSourceModule. // The code content is initialized with ";" to prevent complaining diff --git a/src/target/llvm/llvm_module.cc b/src/target/llvm/llvm_module.cc index 450120d69da3..fde3e8494a42 100644 --- a/src/target/llvm/llvm_module.cc +++ b/src/target/llvm/llvm_module.cc @@ -359,32 +359,18 @@ TVM_REGISTER_GLOBAL("codegen.build_llvm") TVM_REGISTER_GLOBAL("codegen.LLVMModuleCreate") .set_body([](TVMArgs args, TVMRetValue *rv) { auto n = make_object(); - - // parse target triple from the first argument auto target = args[0].operator std::string(); - std::string triple, mcpu, mattr; - llvm::TargetOptions opt; - ParseLLVMTargetOptions(target, &triple, &mcpu, &mattr, &opt); + auto module_name = args[1].operator std::string(); // create a default data layout + InitializeLLVM(); auto tm = GetLLVMTargetMachine(target); - llvm::DataLayout layout(tm->createDataLayout()); - - // initialize an IR code snippet from a simple template - std::string ir_str; - ir_str += "target triple = \"" + triple + "\"\n"; - ir_str += "target datalayout = \"" + layout.getStringRepresentation() + "\""; - - // use parseIR to create a LLVM Module. + auto triple = tm->getTargetTriple(); auto ctx = std::make_shared(); - llvm::SMDiagnostic err; - auto mem_buf = llvm::MemoryBuffer::getMemBuffer(ir_str); - auto module = llvm::parseIR(mem_buf->getMemBufferRef(), err, *ctx); - if (module == nullptr) { - std::string msg = std::string(err.getMessage()); - LOG(FATAL) << "Failed to create a LLVM module from the generated IR code:" - << std::endl << ir_str << std::endl << "Error message: " << msg; - } + std::unique_ptr module(new llvm::Module(module_name, *ctx)); + module->setTargetTriple(triple.str()); + module->setDataLayout(tm->createDataLayout()); + n->Init(std::move(module), ctx); *rv = runtime::Module(n);