FrozenGene commented on a change in pull request #4847: Return empty 
CSourceModule when no lowered_funcs exists in Relay mod
URL: https://github.com/apache/incubator-tvm/pull/4847#discussion_r386479510
 
 

 ##########
 File path: src/target/llvm/llvm_module.cc
 ##########
 @@ -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);
 
 Review comment:
   I think it is not one elegant solution to create LLVM module using ir string 
concatenation. You could use the api like this:
   ```cpp
     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

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to