<div dir="ltr">Hi Alex, Larry,<div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">As for documentation: I will (hopefully soon) start another thread to discuss how to describe parts of ORC better.</blockquote><div><br></div><div>Yep. ORC could definitely use better documentation, and the interface is more mature now (and less of a moving target) so it's a good time to do this.</div><div><br></div><div>At a high level I think we want two documents: An "ORC Overview/Manual" describing the design and components, and a tutorial series showing how to use them for a simple-but-real use case. I started work on the tutorial series over the weekend, and committed my work-in-progress first chapter today as r270487.</div><div><br></div><div><div class="gmail_extra">Any help (feedback, patches, etc.) will be most welcome, especially because some of the hurdles and gotchas will be more fresh in your mind than they are mine.</div><div class="gmail_extra"><br></div><div class="gmail_extra">Thanks to both of you for asking about this - it's a good motivator to start working on it. :)</div><div class="gmail_extra"><br></div><div class="gmail_extra">Cheers,</div><div class="gmail_extra">Lang.</div><div class="gmail_extra"><br><div class="gmail_quote">On Sun, May 22, 2016 at 12:23 PM, Alex Denisov <span dir="ltr"><<a href="mailto:1101.debian@gmail.com" target="_blank">1101.debian@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">>> llvm::sys::DynamicLibrary::LoadLibraryPermanently(nullptr)<br>
<br>
This is one is a bit tricky and hard to find.<br>
I spent quiet some time digging into MC and ORC JIT execution engines trying to find what makes them work.<br>
The problem is that this trick (LoadLibraryPermanently) happens inside of EngineBuilder, despite that the functionality belongs to a JIT engine itself, not to the builder.<br>
<br>
I think this part can be improved by moving the method call into an engine itself, or by documenting the steps required to create custom engine on top of ORC.<br>
<br>
As for documentation: I will (hopefully soon) start another thread to discuss how to describe parts of ORC better.<br>
<div class=""><div class="h5"><br>
> On 20 May 2016, at 19:30, Lang Hames via llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org">llvm-dev@lists.llvm.org</a>> wrote:<br>
><br>
> Hi Larry,<br>
><br>
> Thanks so much! This seems to do the trick. I would have spun my wheels for a long time before discovering all of this, wow.<br>
><br>
> No worries. :)<br>
> I'll try to keep this in mind and make sure I address it in future Kaleidoscope tutorial chapters - these issues tripped me up the first time I encountered them too.<br>
><br>
> Do I even want to know what additional chickens need to be sacrificed to get this to work on Windows?<br>
><br>
> I don't build on Windows myself, so I'm not certain. I know there are people using ORC on Windows though, and evidently at least simple code works. I only know of one big gotcha, which is that COFF files don't properly handle the SF_Exported symbol flag, so whenever you go searching for a symbol in the JIT on windows you have to call findSymbol with ExportedSymbolsOnly=false. Other than that I don't think usage should differ too much between platforms.<br>
><br>
> On the feature support side: the JIT linker for Windows (RuntimeDyldCOFF) is less mature, so you may run into unimplemented features there. While I don't have much time to work on Windows support myself, we definitely want to improve support for it so patches, bug reports and feature requests are all very welcome.<br>
><br>
> In case it's useful too, here's a link to the LLILC JIT file, which uses a simple ORC setup on Windows: <a href="https://github.com/dotnet/llilc/blob/dd12743f9cdb5418f1c39b2cd756da1e8396a922/lib/Jit/LLILCJit.cpp#L299" rel="noreferrer" target="_blank">https://github.com/dotnet/llilc/blob/dd12743f9cdb5418f1c39b2cd756da1e8396a922/lib/Jit/LLILCJit.cpp#L299</a><br>
><br>
> Cheers,<br>
> Lang.<br>
><br>
> On Thu, May 19, 2016 at 11:57 AM, Larry Gritz <<a href="mailto:lg@larrygritz.com">lg@larrygritz.com</a>> wrote:<br>
> Thanks so much! This seems to do the trick. I would have spun my wheels for a long time before discovering all of this, wow.<br>
><br>
> Do I even want to know what additional chickens need to be sacrificed to get this to work on Windows?<br>
><br>
> -- lg<br>
><br>
>> On May 18, 2016, at 1:52 PM, Lang Hames <<a href="mailto:lhames@gmail.com">lhames@gmail.com</a>> wrote:<br>
>><br>
>> Hi Larry,<br>
>><br>
>> You're basically there, but you're hitting a couple of subtle issues:<br>
>><br>
>> (1) On both platforms you'll want to call llvm::sys::DynamicLibrary::LoadLibraryPermanently(nullptr) at program startup. This makes exported symbols in the main program searchable by RTDyldMemoryManager::getSymbolAddressInProcess (important for making 'sqr' findable on any platform).<br>
>><br>
>> (2) On Linux (if I understand correctly) symbols aren't exported from the main process by default, so even if you've called DynamicLibrary::LoadLibraryPermanently, sqr won't show up. To fix this you can add -Wl,export-dynamic to your link line. This will flip the default and export symbols from the main binary, allowing getSymbolAddressInProcess to find sqr. On MacOS this isn't necessary.<br>
>><br>
>> (3) On MacOS, C names are mangled by prepending an '_'. To maintain consistency with statically linked programs, the JIT uses these mangled names so you need to search for '_myfunc', rather than 'myfunc'. I usually add a 'mangle' function that uses the DataLayout, then search for 'mangle(<name>)', which does the right thing on all platforms.<br>
>><br>
>> I've attached a new version of your code with these changes included. Hope this helps!<br>
>><br>
>> - Lang.<br>
>><br>
>><br>
>> On Tue, May 17, 2016 at 12:13 PM, Larry Gritz via llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org">llvm-dev@lists.llvm.org</a>> wrote:<br>
>> When using ORC JIT, I'm having trouble with external function resolution (that is, of a function defined in the app, with C linkage).<br>
>><br>
>> I add a declaration for the function to my IR, and when I use MCJIT, it finds it and all is well, But when I use ORC JIT (I *think* correctly, at least it closely matches what I see in the tutorial), I get an LLVM error, "Program used external function 'sqr' which could not be resolved."<br>
>><br>
>> (Excuse my coming to the ORC party late, I've been stuck on an older LLVM for my DSL, and am only now jumping forward several versions at once, with some growing pains.)<br>
>><br>
>> I've boiled it down to the minimal example below. When I build with orc=0, thus using MCJIT, it works fine and I get the expected output. When I build with orc=1, it fails as I described.<br>
>><br>
>> I'm having this trouble with LLVM 3.7 and 3.8, on both Linux and OSX.<br>
>><br>
>> I figure I'm probably just getting some part of the magic incantation wrong. Can anybody help a poor guy out and spot the error of my ways?<br>
>><br>
>> -----<br>
>><br>
>> #include <llvm/ADT/STLExtras.h><br>
>> #include <llvm/Bitcode/ReaderWriter.h><br>
>> #include <llvm/ExecutionEngine/ExecutionEngine.h><br>
>> #include <llvm/ExecutionEngine/Orc/CompileUtils.h><br>
>> #include <llvm/ExecutionEngine/RuntimeDyld.h><br>
>> #include <llvm/ExecutionEngine/Orc/IRCompileLayer.h><br>
>> #include <llvm/ExecutionEngine/Orc/LambdaResolver.h><br>
>> #include <llvm/ExecutionEngine/Orc/ObjectLinkingLayer.h><br>
>> #include <llvm/IR/DataLayout.h><br>
>> #include <llvm/IR/IRBuilder.h><br>
>> #include <llvm/IR/LegacyPassManager.h><br>
>> #include <llvm/IR/LLVMContext.h><br>
>> #include <llvm/IR/Mangler.h><br>
>> #include <llvm/IR/Module.h><br>
>> #include <llvm/IR/Verifier.h><br>
>> #include <llvm/Support/TargetSelect.h><br>
>> #include <llvm/Target/TargetMachine.h><br>
>> #include <llvm/Transforms/IPO.h><br>
>> #include <llvm/Transforms/IPO/PassManagerBuilder.h><br>
>> #include <llvm/Transforms/Scalar.h><br>
>> #include <llvm/Transforms/Utils/UnifyFunctionExitNodes.h><br>
>><br>
>> template <typename T><br>
>> inline std::vector<T> singletonSet (T t)<br>
>> {<br>
>> std::vector<T> Vec;<br>
>> Vec.push_back(std::move(t));<br>
>> return Vec;<br>
>> }<br>
>><br>
>><br>
>> ///<br>
>> /// THIS is the function I want my IR to call<br>
>> ///<br>
>> extern "C" {<br>
>> float sqr (float x) { return x*x; }<br>
>> }<br>
>><br>
>><br>
>> void<br>
>> simple ()<br>
>> {<br>
>> llvm::InitializeAllTargets();<br>
>> llvm::InitializeAllTargetMCs();<br>
>> llvm::InitializeAllAsmPrinters();<br>
>> llvm::InitializeAllAsmParsers();<br>
>> llvm::LLVMContext Context;<br>
>> std::unique_ptr<llvm::TargetMachine> TM (llvm::EngineBuilder().selectTarget());<br>
>> std::unique_ptr<llvm::DataLayout> DL;<br>
>> DL.reset (new llvm::DataLayout (TM->createDataLayout()));<br>
>> std::unique_ptr<llvm::ExecutionEngine> EE;<br>
>> typedef llvm::orc::ObjectLinkingLayer<> ObjLayerT;<br>
>> typedef llvm::orc::IRCompileLayer<ObjLayerT> CompileLayerT;<br>
>> typedef CompileLayerT::ModuleSetHandleT ModuleHandleT;<br>
>> ObjLayerT Objlayer;<br>
>> CompileLayerT Compilelayer (Objlayer, llvm::orc::SimpleCompiler(*TM));<br>
>> std::unique_ptr<llvm::Module> M (new llvm::Module("module", Context));<br>
>> M->setDataLayout (*DL);<br>
>><br>
>> // Declare stub for external function sqr<br>
>> auto type_float = llvm::Type::getFloatTy (Context);<br>
>> llvm::Type* one_float[] = { type_float };<br>
>> llvm::FunctionType *functype_ff = llvm::FunctionType::get (type_float, one_float, false);<br>
>> llvm::Function::Create (functype_ff, llvm::Function::ExternalLinkage,<br>
>> "sqr", M.get());<br>
>><br>
>> // Create myfunc and generate its IR, which just calls sqr on its argument<br>
>> llvm::Function *myfunc = llvm::Function::Create (functype_ff,<br>
>> llvm::Function::ExternalLinkage,<br>
>> "myfunc", M.get());<br>
>> llvm::IRBuilder<> builder (Context);<br>
>> auto block = llvm::BasicBlock::Create (Context, "", myfunc);<br>
>> builder.SetInsertPoint (block);<br>
>> llvm::Value *a = llvm::cast<llvm::Value>(myfunc->arg_begin());<br>
>> llvm::Value *asq = builder.CreateCall (M->getFunction ("sqr"), a);<br>
>> builder.CreateRet (asq);<br>
>><br>
>> // Set up compilation<br>
>> if (orc) {<br>
>> auto Resolver = llvm::orc::createLambdaResolver(<br>
>> // External lookup functor<br>
>> [&](const std::string &name) {<br>
>> if (auto Sym = Compilelayer.findSymbol(name, true))<br>
>> return llvm::RuntimeDyld::SymbolInfo(Sym.getAddress(), Sym.getFlags());<br>
>> // If not found as a symbol, look up in current process.<br>
>> // Why doesn't this work?<br>
>> if (auto Addr = llvm::RTDyldMemoryManager::getSymbolAddressInProcess(name))<br>
>> return llvm::RuntimeDyld::SymbolInfo(Addr, llvm::JITSymbolFlags::Exported);<br>
>> return llvm::RuntimeDyld::SymbolInfo(nullptr);<br>
>> },<br>
>> // Dylib lookup functor<br>
>> [&](const std::string &name) { return nullptr; }<br>
>> );<br>
>> Compilelayer.addModuleSet (singletonSet(std::move(M)),<br>
>> llvm::make_unique<llvm::SectionMemoryManager>(),<br>
>> std::move(Resolver));<br>
>> } else {<br>
>> // MCJIT<br>
>> std::string engine_errors;<br>
>> llvm::EngineBuilder engine_builder (std::move(M));<br>
>> engine_builder.setEngineKind (llvm::EngineKind::JIT)<br>
>> .setOptLevel (llvm::CodeGenOpt::Default) // Aggressive?<br>
>> .setErrorStr (&engine_errors);<br>
>> EE.reset (engine_builder.create());<br>
>> EE->finalizeObject ();<br>
>> }<br>
>><br>
>> // Ask for a callable function<br>
>> typedef float (*FuncFloatFloat)(float);<br>
>> FuncFloatFloat my_executable_function = NULL;<br>
>> if (orc) {<br>
>> auto ExprSymbol = Compilelayer.findSymbol ("myfunc", true);<br>
>> my_executable_function = (FuncFloatFloat) ExprSymbol.getAddress ();<br>
>> } else {<br>
>> my_executable_function = (FuncFloatFloat) EE->getFunctionAddress ("myfunc");<br>
>> }<br>
>><br>
>> assert (my_executable_function);<br>
>> printf ("myfunc(42.0f) = %g\n", (*my_executable_function)(42.0f));<br>
>> }<br>
>><br>
>><br>
>><br>
>> --<br>
>> Larry Gritz<br>
>> <a href="mailto:lg@larrygritz.com">lg@larrygritz.com</a><br>
>><br>
>><br>
>> _______________________________________________<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/mailman/listinfo/llvm-dev</a><br>
>><br>
>> <larrys_jit.cpp><br>
><br>
> --<br>
> Larry Gritz<br>
> <a href="mailto:lg@larrygritz.com">lg@larrygritz.com</a><br>
><br>
><br>
><br>
> _______________________________________________<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/mailman/listinfo/llvm-dev</a><br>
<br>
</div></div></blockquote></div><br></div></div></div>