Skip to content

Commit

Permalink
Avoid using IR str concat to create LLVM module
Browse files Browse the repository at this point in the history
  • Loading branch information
kumasento committed Mar 2, 2020
1 parent 9441c52 commit ccded94
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 22 deletions.
2 changes: 1 addition & 1 deletion src/relay/backend/build_module.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
28 changes: 7 additions & 21 deletions src/target/llvm/llvm_module.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<LLVMModuleNode>();

// 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::LLVMContext>();
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<llvm::Module> 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);
Expand Down

0 comments on commit ccded94

Please sign in to comment.