-
Notifications
You must be signed in to change notification settings - Fork 3.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Return empty CSourceModule when no lowered_funcs exists in Relay mod #4847
Changes from 1 commit
d6301d6
adbe5a2
ca6e419
2b955fc
cbaa76c
3e83a11
a4f0d96
f1e8fed
d6656e2
9441c52
ccded94
bb55935
41450a5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -356,6 +356,40 @@ TVM_REGISTER_GLOBAL("codegen.build_llvm") | |
*rv = runtime::Module(n); | ||
}); | ||
|
||
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); | ||
|
||
// create a default data layout | ||
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 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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it is not one elegant solution to create LLVM module using ir string concatenation. You could use the api like this: auto triple = tm->getTargetTriple();
auto ctx = std::make_shared<llvm::LLVMContext>();
std::string module_name = "dummy";
std::unique_ptr<llvm::Module> module(new llvm::Module(module_name, *ctx));
module->setTargetTriple(triple.str());
module->setDataLayout(tm->createDataLayout()); Does this work well too? You could refer more detail in my previous implementation of codegen_blob.cc There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @FrozenGene Thank you very much! It looks much better. I've updated my PR accordingly. |
||
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; | ||
} | ||
n->Init(std::move(module), ctx); | ||
|
||
*rv = runtime::Module(n); | ||
}); | ||
|
||
TVM_REGISTER_GLOBAL("target.llvm_lookup_intrinsic_id") | ||
.set_body([](TVMArgs args, TVMRetValue* rv) { | ||
*rv = static_cast<int64_t>(LookupLLVMIntrinsic(args[0])); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry for later response. Minor comment: The logic here doesn't only create default data layout but also create default target triple. We should update the comment.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @FrozenGene I've updated the comments based on your comments :)