<div dir="ltr">Hi Markus,<div><br></div><div>What happens if you set the data layout on pModule before creating the engine?</div><div><br></div><div>- Lang.</div></div><div class="gmail_extra"><br><div class="gmail_quote">On Mon, Sep 19, 2016 at 5:34 AM, via llvm-dev <span dir="ltr"><<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hi,<br>
<br>
I want to use a function defined in c(++)-code from code generated by llvm. For this I use ExecutionEngine.<wbr>addGlobalMapping(). I started with the JIT execution engine and everything worked, then I switched to the interpreter engine and it stopped working, but only if compiled on a Linux system. More precisely it works if I use<br>
<br>
    llvm 3.8.1 + gcc (Linux) + JIT<br>
    llvm 3.8.0 + mingw-gcc (Windows) + JIT<br>
    llvm 3.8.0 + mingw-gcc (Windows) + Interpreter<br>
<br>
But it does not work for llvm 3.8.1 + gcc (Linux) + Interpreter. In that case I get the error message "LLVM ERROR: Tried to execute an unknown external function: testFunction." Am I using ExecutionEngine.<wbr>addGlobalMapping() in the wrong way, even it works in three out of four scenarios?<br>
<br>
#include <llvm/Support/TargetSelect.h><br>
#include <llvm/Analysis/Passes.h><br>
#include <llvm/Transforms/Scalar.h><br>
#include <llvm/Transforms/IPO/<wbr>PassManagerBuilder.h><br>
#include <llvm/Transforms/IPO.h><br>
#include <llvm/IR/Verifier.h><br>
#include <llvm/ExecutionEngine/<wbr>ExecutionEngine.h><br>
#include <llvm/ExecutionEngine/<wbr>Interpreter.h><br>
#include <llvm/ExecutionEngine/MCJIT.h><br>
#include <llvm/ExecutionEngine/<wbr>GenericValue.h><br>
#include <llvm/IR/LegacyPassManager.h><br>
#include <llvm/IR/IRBuilder.h><br>
<br>
extern "C" double testFunction(){<br>
  return 42.0;<br>
}<br>
<br>
int main() {<br>
  // initialization needed for JIT<br>
  llvm::InitializeNativeTarget()<wbr>;<br>
  llvm::<wbr>InitializeNativeTargetAsmPrint<wbr>er();<br>
  llvm::<wbr>InitializeNativeTargetAsmParse<wbr>r();<br>
<br>
  llvm::LLVMContext &context(llvm::<wbr>getGlobalContext());<br>
  llvm::Module *pModule = new llvm::Module("a module", context);<br>
<br>
  // Here the EngineKind-flag decides if JIT or interpreter is used.<br>
  auto pExecutionEngine = llvm::EngineBuilder(std::<wbr>unique_ptr<llvm::Module>(<wbr>pModule)<br>
    ).setEngineKind(llvm::<wbr>EngineKind::Interpreter).<wbr>create();<br>
  pModule->setDataLayout(<wbr>pExecutionEngine-><wbr>getDataLayout());<br>
<br>
  // declaration of the c function.<br>
  std::vector<llvm::Type *> noArgTypes;<br>
  llvm::FunctionType* ft = llvm::FunctionType::get(llvm::<wbr>Type::getDoubleTy(context),<wbr>noArgTypes, false);<br>
  auto pFunction = llvm::Function::Create(ft, llvm::Function::<wbr>ExternalLinkage, "testFunction",pModule);<br>
  pExecutionEngine-><wbr>addGlobalMapping(pFunction,<wbr>reinterpret_cast<void*>(&<wbr>testFunction));<br>
<br>
  // generation of llvm code<br>
  auto pBlock = llvm::BasicBlock::Create(<wbr>context, "evaluation");<br>
  llvm::IRBuilder<> builder(context);<br>
  builder.SetInsertPoint(pBlock)<wbr>;<br>
<br>
  // code for call of the c function.<br>
  auto pFunction2 = pModule->getFunction("<wbr>testFunction");<br>
  auto temp = builder.CreateCall(pFunction2, std::vector<llvm::Value*>(), "calltmp");<br>
  builder.CreateRet(temp);<br>
<br>
  // generation of the llvm function calling the c function<br>
  llvm::FunctionType* ftWrapper = llvm::FunctionType::get(llvm::<wbr>Type::getDoubleTy(context),<wbr>noArgTypes, false);<br>
  auto pWrapperFunction = llvm::Function::Create(<wbr>ftWrapper, llvm::Function::<wbr>ExternalLinkage, "AFunction",pModule);<br>
  pWrapperFunction-><wbr>getBasicBlockList().push_back(<wbr>pBlock);<br>
<br>
  // calling the generated llvm function<br>
  pExecutionEngine-><wbr>finalizeObject();<br>
  pExecutionEngine->runFunction(<wbr>pWrapperFunction,std::vector<<wbr>llvm::GenericValue>());<br>
}<br>
<br>
Thanks for any help,<br>
Markus<br>
______________________________<wbr>_________________<br>
LLVM Developers mailing list<br>
<a href="mailto:llvm-dev@lists.llvm.org">llvm-dev@lists.llvm.org</a><br>
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/<wbr>mailman/listinfo/llvm-dev</a><br>
</blockquote></div><br></div>