<div dir="ltr">Hi Markus,<div><br></div><div>I don't know the Interpreter code well (and it's very poorly maintained) but from a quick inspection it doesn't seem to be inspecting the global mapping during external function resolution at all. The reason the code works on windows anyway is that (I would guess) it's an exported symbol in your program, and the Interpreter *does* look for those. On Linux, where symbols are not exported from the main binary by default, this fails.</div><div><br></div><div>So: global mappings do not work in the interpreter at the moment.</div><div><br></div><div>Is there any particular reason for you to use the interpreter? Given that it is poorly maintained, I would encourage you to use MCJIT instead (or, even better, ORC: <a href="http://llvm.org/docs/tutorial/BuildingAJIT1.html">http://llvm.org/docs/tutorial/BuildingAJIT1.html</a>).</div><div><br></div><div>Cheers,</div><div>Lang.</div><div><br></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Mon, Oct 24, 2016 at 12:09 AM,  <span dir="ltr"><<a href="mailto:markus.parker@arcor.de" target="_blank">markus.parker@arcor.de</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"> Hi Lang,<br>
<span class=""><br>
> What happens if you set the data layout on pModule before creating the<br>
> engine?<br>
<br>
</span>The output of pExecutionEngine-><wbr>getDataLayout().<wbr>getStringRepresentation() is an empty string if I do not set the layout on pModule before creating the interpreter engine. If I set the layout before, the output is the specified layout. But unfortunately, the error is still there.<br>
<br>
Markus<br>
<div class="HOEnZb"><div class="h5"><br>
><br>
> - Lang.<br>
><br>
> On Mon, Sep 19, 2016 at 5:34 AM, via llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org">llvm-dev@lists.llvm.org</a>><br>
> wrote:<br>
><br>
> > Hi,<br>
> ><br>
> > I want to use a function defined in c(++)-code from code generated by<br>
> > llvm. For this I use ExecutionEngine.<wbr>addGlobalMapping(). I started with<br>
> > the JIT execution engine and everything worked, then I switched to the<br>
> > interpreter engine and it stopped working, but only if compiled on a<br>
> Linux<br>
> > 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<br>
> > case I get the error message "LLVM ERROR: Tried to execute an unknown<br>
> > external function: testFunction." Am I using<br>
> ExecutionEngine.<wbr>addGlobalMapping()<br>
> > 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::<br>
> > 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::<br>
> > Type::getDoubleTy(context),<wbr>noArgTypes, false);<br>
> >   auto pFunction = llvm::Function::Create(ft,<br>
> llvm::Function::<wbr>ExternalLinkage,<br>
> > "testFunction",pModule);<br>
> >   pExecutionEngine-><wbr>addGlobalMapping(pFunction,<wbr>reinterpret_cast<void*>(&<br>
> > 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*>(),<br>
> > "calltmp");<br>
> >   builder.CreateRet(temp);<br>
> ><br>
> >   // generation of the llvm function calling the c function<br>
> >   llvm::FunctionType* ftWrapper = llvm::FunctionType::get(llvm::<br>
> > Type::getDoubleTy(context),<wbr>noArgTypes, false);<br>
> >   auto pWrapperFunction = llvm::Function::Create(<wbr>ftWrapper,<br>
> > 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<<br>
> > 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>
> ><br>
><br>
</div></div></blockquote></div><br></div>