[llvm] r251933 - [Orc] Directly emit machine code for the x86 resolver block and trampolines.
Rafael EspĂndola via llvm-commits
llvm-commits at lists.llvm.org
Tue Nov 3 08:28:54 PST 2015
Hi lang,
Sorry I reverted this as it broke the build:
/home/espindola/llvm/llvm/examples/Kaleidoscope/Orc/fully_lazy/toy.cpp:1314:3:
error: too many template arguments for class template
'JITCompileCallbackManager'
JITCompileCallbackManager<LazyEmitLayerT, OrcX86_64> CompileCallbacks;
On 3 November 2015 at 11:10, Lang Hames via llvm-commits
<llvm-commits at lists.llvm.org> wrote:
> Author: lhames
> Date: Tue Nov 3 10:10:18 2015
> New Revision: 251933
>
> URL: http://llvm.org/viewvc/llvm-project?rev=251933&view=rev
> Log:
> [Orc] Directly emit machine code for the x86 resolver block and trampolines.
>
> Bypassing LLVM for this has a number of benefits:
>
> 1) Laziness support becomes asm-syntax agnostic (previously lazy jitting didn't
> work on Windows as the resolver block was in Darwin asm).
>
> 2) For cross-process JITs, it allows resolver blocks and trampolines to be
> emitted directly in the target process, reducing cross process traffic.
>
> 3) It should be marginally faster.
>
>
> Modified:
> llvm/trunk/include/llvm-c/OrcBindings.h
> llvm/trunk/include/llvm/ExecutionEngine/Orc/CompileOnDemandLayer.h
> llvm/trunk/include/llvm/ExecutionEngine/Orc/IndirectionUtils.h
> llvm/trunk/include/llvm/ExecutionEngine/Orc/OrcTargetSupport.h
> llvm/trunk/lib/ExecutionEngine/Orc/OrcCBindings.cpp
> llvm/trunk/lib/ExecutionEngine/Orc/OrcCBindingsStack.cpp
> llvm/trunk/lib/ExecutionEngine/Orc/OrcCBindingsStack.h
> llvm/trunk/lib/ExecutionEngine/Orc/OrcTargetSupport.cpp
> llvm/trunk/tools/lli/OrcLazyJIT.cpp
> llvm/trunk/tools/lli/OrcLazyJIT.h
> llvm/trunk/unittests/ExecutionEngine/Orc/CompileOnDemandLayerTest.cpp
> llvm/trunk/unittests/ExecutionEngine/Orc/OrcCAPITest.cpp
>
> Modified: llvm/trunk/include/llvm-c/OrcBindings.h
> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm-c/OrcBindings.h?rev=251933&r1=251932&r2=251933&view=diff
> ==============================================================================
> --- llvm/trunk/include/llvm-c/OrcBindings.h (original)
> +++ llvm/trunk/include/llvm-c/OrcBindings.h Tue Nov 3 10:10:18 2015
> @@ -47,8 +47,7 @@ typedef uint64_t (*LLVMOrcLazyCompileCal
> * client should not attempt to dispose of the Target Machine, or it will result
> * in a double-free.
> */
> -LLVMOrcJITStackRef LLVMOrcCreateInstance(LLVMTargetMachineRef TM,
> - LLVMContextRef Context);
> +LLVMOrcJITStackRef LLVMOrcCreateInstance(LLVMTargetMachineRef TM);
>
> /**
> * Mangle the given symbol.
>
> Modified: llvm/trunk/include/llvm/ExecutionEngine/Orc/CompileOnDemandLayer.h
> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ExecutionEngine/Orc/CompileOnDemandLayer.h?rev=251933&r1=251932&r2=251933&view=diff
> ==============================================================================
> --- llvm/trunk/include/llvm/ExecutionEngine/Orc/CompileOnDemandLayer.h (original)
> +++ llvm/trunk/include/llvm/ExecutionEngine/Orc/CompileOnDemandLayer.h Tue Nov 3 10:10:18 2015
> @@ -242,7 +242,7 @@ private:
> // Create a callback, associate it with the stub for the function,
> // and set the compile action to compile the partition containing the
> // function.
> - auto CCInfo = CompileCallbackMgr.getCompileCallback(SrcM.getContext());
> + auto CCInfo = CompileCallbackMgr.getCompileCallback();
> StubInits[mangle(F.getName(), DL)] =
> std::make_pair(CCInfo.getAddress(),
> JITSymbolBase::flagsFromGlobalValue(F));
>
> Modified: llvm/trunk/include/llvm/ExecutionEngine/Orc/IndirectionUtils.h
> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ExecutionEngine/Orc/IndirectionUtils.h?rev=251933&r1=251932&r2=251933&view=diff
> ==============================================================================
> --- llvm/trunk/include/llvm/ExecutionEngine/Orc/IndirectionUtils.h (original)
> +++ llvm/trunk/include/llvm/ExecutionEngine/Orc/IndirectionUtils.h Tue Nov 3 10:10:18 2015
> @@ -27,8 +27,7 @@
> namespace llvm {
> namespace orc {
>
> -/// @brief Base class for JITLayer independent aspects of
> -/// JITCompileCallbackManager.
> +/// @brief Target-independent base class JITCompileCallbackManager.
> class JITCompileCallbackManagerBase {
> public:
>
> @@ -54,13 +53,8 @@ public:
> /// @brief Construct a JITCompileCallbackManagerBase.
> /// @param ErrorHandlerAddress The address of an error handler in the target
> /// process to be used if a compile callback fails.
> - /// @param NumTrampolinesPerBlock Number of trampolines to emit if there is no
> - /// available trampoline when getCompileCallback is
> - /// called.
> - JITCompileCallbackManagerBase(TargetAddress ErrorHandlerAddress,
> - unsigned NumTrampolinesPerBlock)
> - : ErrorHandlerAddress(ErrorHandlerAddress),
> - NumTrampolinesPerBlock(NumTrampolinesPerBlock) {}
> + JITCompileCallbackManagerBase(TargetAddress ErrorHandlerAddress)
> + : ErrorHandlerAddress(ErrorHandlerAddress) {}
>
> virtual ~JITCompileCallbackManagerBase() {}
>
> @@ -90,7 +84,7 @@ public:
> }
>
> /// @brief Reserve a compile callback.
> - virtual CompileCallbackInfo getCompileCallback(LLVMContext &Context) = 0;
> + virtual CompileCallbackInfo getCompileCallback() = 0;
>
> /// @brief Get a CompileCallbackInfo for an existing callback.
> CompileCallbackInfo getCompileCallbackInfo(TargetAddress TrampolineAddr) {
> @@ -113,7 +107,6 @@ public:
>
> protected:
> TargetAddress ErrorHandlerAddress;
> - unsigned NumTrampolinesPerBlock;
>
> typedef std::map<TargetAddress, CompileFtor> TrampolineMapT;
> TrampolineMapT ActiveTrampolines;
> @@ -124,69 +117,54 @@ private:
> };
>
> /// @brief Manage compile callbacks.
> -template <typename JITLayerT, typename TargetT>
> +template <typename TargetT>
> class JITCompileCallbackManager : public JITCompileCallbackManagerBase {
> public:
>
> /// @brief Construct a JITCompileCallbackManager.
> - /// @param JIT JIT layer to emit callback trampolines, etc. into.
> - /// @param Context LLVMContext to use for trampoline & resolve block modules.
> /// @param ErrorHandlerAddress The address of an error handler in the target
> /// process to be used if a compile callback fails.
> - /// @param NumTrampolinesPerBlock Number of trampolines to allocate whenever
> - /// there is no existing callback trampoline.
> - /// (Trampolines are allocated in blocks for
> - /// efficiency.)
> - JITCompileCallbackManager(JITLayerT &JIT, RuntimeDyld::MemoryManager &MemMgr,
> - LLVMContext &Context,
> - TargetAddress ErrorHandlerAddress,
> - unsigned NumTrampolinesPerBlock)
> - : JITCompileCallbackManagerBase(ErrorHandlerAddress,
> - NumTrampolinesPerBlock),
> - JIT(JIT), MemMgr(MemMgr) {
> - emitResolverBlock(Context);
> + JITCompileCallbackManager(TargetAddress ErrorHandlerAddress)
> + : JITCompileCallbackManagerBase(ErrorHandlerAddress) {
> +
> + /// Set up the resolver block.
> + std::error_code EC;
> + ResolverBlock =
> + sys::OwningMemoryBlock(
> + sys::Memory::allocateMappedMemory(TargetT::ResolverCodeSize, nullptr,
> + sys::Memory::MF_READ |
> + sys::Memory::MF_WRITE, EC));
> + assert(!EC && "Failed to allocate resolver block");
> +
> + TargetT::writeResolverCode(static_cast<uint8_t*>(ResolverBlock.base()),
> + &reenter, this);
> +
> + EC = sys::Memory::protectMappedMemory(ResolverBlock.getMemoryBlock(),
> + sys::Memory::MF_READ |
> + sys::Memory::MF_EXEC);
> + assert(!EC && "Failed to mprotect resolver block");
> }
>
> /// @brief Get/create a compile callback with the given signature.
> - CompileCallbackInfo getCompileCallback(LLVMContext &Context) final {
> - TargetAddress TrampolineAddr = getAvailableTrampolineAddr(Context);
> + CompileCallbackInfo getCompileCallback() final {
> + TargetAddress TrampolineAddr = getAvailableTrampolineAddr();
> auto &Compile = this->ActiveTrampolines[TrampolineAddr];
> return CompileCallbackInfo(TrampolineAddr, Compile);
> }
>
> private:
>
> - std::vector<std::unique_ptr<Module>>
> - SingletonSet(std::unique_ptr<Module> M) {
> - std::vector<std::unique_ptr<Module>> Ms;
> - Ms.push_back(std::move(M));
> - return Ms;
> - }
> -
> - void emitResolverBlock(LLVMContext &Context) {
> - std::unique_ptr<Module> M(new Module("resolver_block_module",
> - Context));
> - TargetT::insertResolverBlock(*M, *this);
> - auto NonResolver =
> - createLambdaResolver(
> - [](const std::string &Name) -> RuntimeDyld::SymbolInfo {
> - llvm_unreachable("External symbols in resolver block?");
> - },
> - [](const std::string &Name) -> RuntimeDyld::SymbolInfo {
> - llvm_unreachable("Dylib symbols in resolver block?");
> - });
> - auto H = JIT.addModuleSet(SingletonSet(std::move(M)), &MemMgr,
> - std::move(NonResolver));
> - JIT.emitAndFinalize(H);
> - auto ResolverBlockSymbol =
> - JIT.findSymbolIn(H, TargetT::ResolverBlockName, false);
> - assert(ResolverBlockSymbol && "Failed to insert resolver block");
> - ResolverBlockAddr = ResolverBlockSymbol.getAddress();
> + static TargetAddress reenter(void *CCMgr, void *TrampolineId) {
> + JITCompileCallbackManager *Mgr =
> + static_cast<JITCompileCallbackManager*>(CCMgr);
> + return Mgr->executeCompileCallback(
> + static_cast<TargetAddress>(
> + reinterpret_cast<uintptr_t>(TrampolineId)));
> }
>
> - TargetAddress getAvailableTrampolineAddr(LLVMContext &Context) {
> + TargetAddress getAvailableTrampolineAddr() {
> if (this->AvailableTrampolines.empty())
> - grow(Context);
> + grow();
> assert(!this->AvailableTrampolines.empty() &&
> "Failed to grow available trampolines.");
> TargetAddress TrampolineAddr = this->AvailableTrampolines.back();
> @@ -194,35 +172,41 @@ private:
> return TrampolineAddr;
> }
>
> - void grow(LLVMContext &Context) {
> + void grow() {
> assert(this->AvailableTrampolines.empty() && "Growing prematurely?");
> - std::unique_ptr<Module> M(new Module("trampoline_block", Context));
> - auto GetLabelName =
> - TargetT::insertCompileCallbackTrampolines(*M, ResolverBlockAddr,
> - this->NumTrampolinesPerBlock,
> - this->ActiveTrampolines.size());
> - auto NonResolver =
> - createLambdaResolver(
> - [](const std::string &Name) -> RuntimeDyld::SymbolInfo {
> - llvm_unreachable("External symbols in trampoline block?");
> - },
> - [](const std::string &Name) -> RuntimeDyld::SymbolInfo {
> - llvm_unreachable("Dylib symbols in trampoline block?");
> - });
> - auto H = JIT.addModuleSet(SingletonSet(std::move(M)), &MemMgr,
> - std::move(NonResolver));
> - JIT.emitAndFinalize(H);
> - for (unsigned I = 0; I < this->NumTrampolinesPerBlock; ++I) {
> - std::string Name = GetLabelName(I);
> - auto TrampolineSymbol = JIT.findSymbolIn(H, Name, false);
> - assert(TrampolineSymbol && "Failed to emit trampoline.");
> - this->AvailableTrampolines.push_back(TrampolineSymbol.getAddress());
> - }
> +
> + std::error_code EC;
> + auto TrampolineBlock =
> + sys::OwningMemoryBlock(
> + sys::Memory::allocateMappedMemory(TargetT::PageSize, nullptr,
> + sys::Memory::MF_READ |
> + sys::Memory::MF_WRITE, EC));
> + assert(!EC && "Failed to allocate trampoline block");
> +
> +
> + unsigned NumTrampolines =
> + (TargetT::PageSize - TargetT::PointerSize) / TargetT::TrampolineSize;
> +
> + uint8_t *TrampolineMem = static_cast<uint8_t*>(TrampolineBlock.base());
> + TargetT::writeTrampolines(TrampolineMem, ResolverBlock.base(),
> + NumTrampolines);
> +
> + for (unsigned I = 0; I < NumTrampolines; ++I)
> + this->AvailableTrampolines.push_back(
> + static_cast<TargetAddress>(
> + reinterpret_cast<uintptr_t>(
> + TrampolineMem + (I * TargetT::TrampolineSize))));
> +
> + EC = sys::Memory::protectMappedMemory(TrampolineBlock.getMemoryBlock(),
> + sys::Memory::MF_READ |
> + sys::Memory::MF_EXEC);
> + assert(!EC && "Failed to mprotect trampoline block");
> +
> + TrampolineBlocks.push_back(std::move(TrampolineBlock));
> }
>
> - JITLayerT &JIT;
> - RuntimeDyld::MemoryManager &MemMgr;
> - TargetAddress ResolverBlockAddr;
> + sys::OwningMemoryBlock ResolverBlock;
> + std::vector<sys::OwningMemoryBlock> TrampolineBlocks;
> };
>
> /// @brief Base class for managing collections of named indirect stubs.
>
> Modified: llvm/trunk/include/llvm/ExecutionEngine/Orc/OrcTargetSupport.h
> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ExecutionEngine/Orc/OrcTargetSupport.h?rev=251933&r1=251932&r2=251933&view=diff
> ==============================================================================
> --- llvm/trunk/include/llvm/ExecutionEngine/Orc/OrcTargetSupport.h (original)
> +++ llvm/trunk/include/llvm/ExecutionEngine/Orc/OrcTargetSupport.h Tue Nov 3 10:10:18 2015
> @@ -26,29 +26,24 @@ namespace orc {
>
> class OrcX86_64 {
> public:
> - static const char *ResolverBlockName;
> + static const unsigned PageSize = 4096;
> + static const unsigned PointerSize = 8;
> + static const unsigned TrampolineSize = 8;
> + static const unsigned ResolverCodeSize = 0x78;
>
> - /// @brief Insert module-level inline callback asm into module M for the
> - /// symbols managed by JITResolveCallbackHandler J.
> - static void insertResolverBlock(Module &M,
> - JITCompileCallbackManagerBase &JCBM);
> + typedef TargetAddress (*JITReentryFn)(void *CallbackMgr,
> + void *TrampolineId);
>
> - /// @brief Get a label name from the given index.
> - typedef std::function<std::string(unsigned)> LabelNameFtor;
> + /// @brief Write the resolver code into the given memory. The user is be
> + /// responsible for allocating the memory and setting permissions.
> + static void writeResolverCode(uint8_t *ResolveMem, JITReentryFn Reentry,
> + void *CallbackMgr);
>
> - /// @brief Insert the requested number of trampolines into the given module.
> - /// @param M Module to insert the call block into.
> - /// @param NumCalls Number of calls to create in the call block.
> - /// @param StartIndex Optional argument specifying the index suffix to start
> - /// with.
> - /// @return A functor that provides the symbol name for each entry in the call
> - /// block.
> - ///
> - static LabelNameFtor insertCompileCallbackTrampolines(
> - Module &M,
> - TargetAddress TrampolineAddr,
> - unsigned NumCalls,
> - unsigned StartIndex = 0);
> + /// @brief Write the requsted number of trampolines into the given memory,
> + /// which must be big enough to hold 1 pointer, plus NumTrampolines
> + /// trampolines.
> + static void writeTrampolines(uint8_t *TrampolineMem, void *ResolverAddr,
> + unsigned NumTrampolines);
>
> /// @brief Provide information about stub blocks generated by the
> /// makeIndirectStubsBlock function.
>
> Modified: llvm/trunk/lib/ExecutionEngine/Orc/OrcCBindings.cpp
> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/ExecutionEngine/Orc/OrcCBindings.cpp?rev=251933&r1=251932&r2=251933&view=diff
> ==============================================================================
> --- llvm/trunk/lib/ExecutionEngine/Orc/OrcCBindings.cpp (original)
> +++ llvm/trunk/lib/ExecutionEngine/Orc/OrcCBindings.cpp Tue Nov 3 10:10:18 2015
> @@ -12,20 +12,18 @@
>
> using namespace llvm;
>
> -LLVMOrcJITStackRef LLVMOrcCreateInstance(LLVMTargetMachineRef TM,
> - LLVMContextRef Context) {
> +LLVMOrcJITStackRef LLVMOrcCreateInstance(LLVMTargetMachineRef TM) {
> TargetMachine *TM2(unwrap(TM));
> - LLVMContext &Ctx = *unwrap(Context);
>
> Triple T(TM2->getTargetTriple());
>
> - auto CallbackMgrBuilder = OrcCBindingsStack::createCallbackManagerBuilder(T);
> + auto CompileCallbackMgr = OrcCBindingsStack::createCompileCallbackMgr(T);
> auto IndirectStubsMgrBuilder =
> OrcCBindingsStack::createIndirectStubsMgrBuilder(T);
>
> OrcCBindingsStack *JITStack =
> - new OrcCBindingsStack(*TM2, Ctx, CallbackMgrBuilder,
> - IndirectStubsMgrBuilder);
> + new OrcCBindingsStack(*TM2, std::move(CompileCallbackMgr),
> + IndirectStubsMgrBuilder);
>
> return wrap(JITStack);
> }
>
> Modified: llvm/trunk/lib/ExecutionEngine/Orc/OrcCBindingsStack.cpp
> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/ExecutionEngine/Orc/OrcCBindingsStack.cpp?rev=251933&r1=251932&r2=251933&view=diff
> ==============================================================================
> --- llvm/trunk/lib/ExecutionEngine/Orc/OrcCBindingsStack.cpp (original)
> +++ llvm/trunk/lib/ExecutionEngine/Orc/OrcCBindingsStack.cpp Tue Nov 3 10:10:18 2015
> @@ -17,19 +17,14 @@
>
> using namespace llvm;
>
> -OrcCBindingsStack::CallbackManagerBuilder
> -OrcCBindingsStack::createCallbackManagerBuilder(Triple T) {
> +std::unique_ptr<OrcCBindingsStack::CompileCallbackMgr>
> +OrcCBindingsStack::createCompileCallbackMgr(Triple T) {
> switch (T.getArch()) {
> default: return nullptr;
>
> case Triple::x86_64: {
> - typedef orc::JITCompileCallbackManager<CompileLayerT,
> - orc::OrcX86_64> CCMgrT;
> - return [](CompileLayerT &CompileLayer, RuntimeDyld::MemoryManager &MemMgr,
> - LLVMContext &Context) {
> - return llvm::make_unique<CCMgrT>(CompileLayer, MemMgr, Context, 0,
> - 64);
> - };
> + typedef orc::JITCompileCallbackManager<orc::OrcX86_64> CCMgrT;
> + return llvm::make_unique<CCMgrT>(0);
> }
> }
> }
>
> Modified: llvm/trunk/lib/ExecutionEngine/Orc/OrcCBindingsStack.h
> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/ExecutionEngine/Orc/OrcCBindingsStack.h?rev=251933&r1=251932&r2=251933&view=diff
> ==============================================================================
> --- llvm/trunk/lib/ExecutionEngine/Orc/OrcCBindingsStack.h (original)
> +++ llvm/trunk/lib/ExecutionEngine/Orc/OrcCBindingsStack.h Tue Nov 3 10:10:18 2015
> @@ -34,10 +34,7 @@ public:
> typedef orc::IRCompileLayer<ObjLayerT> CompileLayerT;
> typedef orc::CompileOnDemandLayer<CompileLayerT, CompileCallbackMgr> CODLayerT;
>
> - typedef std::function<
> - std::unique_ptr<CompileCallbackMgr>(CompileLayerT&,
> - RuntimeDyld::MemoryManager&,
> - LLVMContext&)>
> + typedef std::function<std::unique_ptr<CompileCallbackMgr>()>
> CallbackManagerBuilder;
>
> typedef CODLayerT::IndirectStubsManagerBuilderT IndirectStubsManagerBuilder;
> @@ -86,19 +83,18 @@ public:
>
> typedef unsigned ModuleHandleT;
>
> - static CallbackManagerBuilder createCallbackManagerBuilder(Triple T);
> + static std::unique_ptr<CompileCallbackMgr> createCompileCallbackMgr(Triple T);
> static IndirectStubsManagerBuilder createIndirectStubsMgrBuilder(Triple T);
>
> - OrcCBindingsStack(TargetMachine &TM, LLVMContext &Context,
> - CallbackManagerBuilder &BuildCallbackMgr,
> + OrcCBindingsStack(TargetMachine &TM,
> + std::unique_ptr<CompileCallbackMgr> CCMgr,
> IndirectStubsManagerBuilder IndirectStubsMgrBuilder)
> - : Context(Context), DL(TM.createDataLayout()),
> + : DL(TM.createDataLayout()), CCMgr(std::move(CCMgr)),
> ObjectLayer(),
> CompileLayer(ObjectLayer, orc::SimpleCompiler(TM)),
> - CCMgr(BuildCallbackMgr(CompileLayer, CCMgrMemMgr, Context)),
> CODLayer(CompileLayer,
> [](Function &F) { std::set<Function*> S; S.insert(&F); return S; },
> - *CCMgr, std::move(IndirectStubsMgrBuilder), false),
> + *this->CCMgr, std::move(IndirectStubsMgrBuilder), false),
> IndirectStubsMgr(IndirectStubsMgrBuilder()),
> CXXRuntimeOverrides([this](const std::string &S) { return mangle(S); }) {}
>
> @@ -127,7 +123,7 @@ public:
> orc::TargetAddress
> createLazyCompileCallback(LLVMOrcLazyCompileCallbackFn Callback,
> void *CallbackCtx) {
> - auto CCInfo = CCMgr->getCompileCallback(Context);
> + auto CCInfo = CCMgr->getCompileCallback();
> CCInfo.setCompileAction(
> [=]() -> orc::TargetAddress {
> return Callback(wrap(this), CallbackCtx);
> @@ -264,13 +260,12 @@ private:
> return NewHandle;
> }
>
> - LLVMContext &Context;
> DataLayout DL;
> SectionMemoryManager CCMgrMemMgr;
>
> + std::unique_ptr<CompileCallbackMgr> CCMgr;
> ObjLayerT ObjectLayer;
> CompileLayerT CompileLayer;
> - std::unique_ptr<CompileCallbackMgr> CCMgr;
> CODLayerT CODLayer;
>
> std::unique_ptr<orc::IndirectStubsManagerBase> IndirectStubsMgr;
>
> Modified: llvm/trunk/lib/ExecutionEngine/Orc/OrcTargetSupport.cpp
> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/ExecutionEngine/Orc/OrcTargetSupport.cpp?rev=251933&r1=251932&r2=251933&view=diff
> ==============================================================================
> --- llvm/trunk/lib/ExecutionEngine/Orc/OrcTargetSupport.cpp (original)
> +++ llvm/trunk/lib/ExecutionEngine/Orc/OrcTargetSupport.cpp Tue Nov 3 10:10:18 2015
> @@ -12,136 +12,88 @@
> #include "llvm/Support/Process.h"
> #include <array>
>
> -using namespace llvm::orc;
> -
> -namespace {
> -
> -uint64_t executeCompileCallback(JITCompileCallbackManagerBase *JCBM,
> - TargetAddress CallbackID) {
> - return JCBM->executeCompileCallback(CallbackID);
> -}
> -
> -}
> -
> namespace llvm {
> namespace orc {
>
> -const char* OrcX86_64::ResolverBlockName = "orc_resolver_block";
> +void OrcX86_64::writeResolverCode(uint8_t *ResolverMem, JITReentryFn ReentryFn,
> + void *CallbackMgr) {
>
> -void OrcX86_64::insertResolverBlock(
> - Module &M, JITCompileCallbackManagerBase &JCBM) {
> + const uint8_t ResolverCode[] = {
> + // resolver_entry:
> + 0x55, // 0x00: pushq %rbp
> + 0x48, 0x89, 0xe5, // 0x01: movq %rsp, %rbp
> + 0x50, // 0x04: pushq %rax
> + 0x53, // 0x05: pushq %rbx
> + 0x51, // 0x06: pushq %rcx
> + 0x52, // 0x07: pushq %rdx
> + 0x56, // 0x08: pushq %rsi
> + 0x57, // 0x09: pushq %rdi
> + 0x41, 0x50, // 0x0a: pushq %r8
> + 0x41, 0x51, // 0x0c: pushq %r9
> + 0x41, 0x52, // 0x0e: pushq %r10
> + 0x41, 0x53, // 0x10: pushq %r11
> + 0x41, 0x54, // 0x12: pushq %r12
> + 0x41, 0x55, // 0x14: pushq %r13
> + 0x41, 0x56, // 0x16: pushq %r14
> + 0x41, 0x57, // 0x18: pushq %r15
> + 0x48, 0x81, 0xec, 0x08, 0x02, 0x00, 0x00, // 0x1a: subq 20, %rsp
> + 0x48, 0x0f, 0xae, 0x04, 0x24, // 0x21: fxsave64 (%rsp)
> + 0x48, 0x8d, 0x3d, 0x43, 0x00, 0x00, 0x00, // 0x26: leaq 67(%rip), %rdi
> + 0x48, 0x8b, 0x3f, // 0x2d: movq (%rdi), %rdi
> + 0x48, 0x8b, 0x75, 0x08, // 0x30: movq 8(%rbp), %rsi
> + 0x48, 0x83, 0xee, 0x06, // 0x34: subq $6, %rsi
> + 0x48, 0xb8, // 0x38: movabsq $0, %rax
> +
> + // 0x3a: JIT re-entry fn addr:
> + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
> +
> + 0xff, 0xd0, // 0x42: callq *%rax
> + 0x48, 0x89, 0x45, 0x08, // 0x44: movq %rax, 8(%rbp)
> + 0x48, 0x0f, 0xae, 0x0c, 0x24, // 0x48: fxrstor64 (%rsp)
> + 0x48, 0x81, 0xc4, 0x08, 0x02, 0x00, 0x00, // 0x4d: addq 20, %rsp
> + 0x41, 0x5f, // 0x54: popq %r15
> + 0x41, 0x5e, // 0x56: popq %r14
> + 0x41, 0x5d, // 0x58: popq %r13
> + 0x41, 0x5c, // 0x5a: popq %r12
> + 0x41, 0x5b, // 0x5c: popq %r11
> + 0x41, 0x5a, // 0x5e: popq %r10
> + 0x41, 0x59, // 0x60: popq %r9
> + 0x41, 0x58, // 0x62: popq %r8
> + 0x5f, // 0x64: popq %rdi
> + 0x5e, // 0x65: popq %rsi
> + 0x5a, // 0x66: popq %rdx
> + 0x59, // 0x67: popq %rcx
> + 0x5b, // 0x68: popq %rbx
> + 0x58, // 0x69: popq %rax
> + 0x5d, // 0x6a: popq %rbp
> + 0xc3, // 0x6b: retq
> + 0x00, 0x00, 0x00, 0x00, // 0x6c: <padding>
>
> - // Trampoline code-sequence length, used to get trampoline address from return
> - // address.
> - const unsigned X86_64_TrampolineLength = 6;
> -
> - // List of x86-64 GPRs to save. Note - RBP saved separately below.
> - std::array<const char *, 14> GPRs = {{
> - "rax", "rbx", "rcx", "rdx",
> - "rsi", "rdi", "r8", "r9",
> - "r10", "r11", "r12", "r13",
> - "r14", "r15"
> - }};
> -
> - // Address of the executeCompileCallback function.
> - uint64_t CallbackAddr =
> - static_cast<uint64_t>(
> - reinterpret_cast<uintptr_t>(executeCompileCallback));
> -
> - std::ostringstream AsmStream;
> - Triple TT(M.getTargetTriple());
> -
> - // Switch to text section.
> - if (TT.getOS() == Triple::Darwin)
> - AsmStream << ".section __TEXT,__text,regular,pure_instructions\n"
> - << ".align 4, 0x90\n";
> - else
> - AsmStream << ".text\n"
> - << ".align 16, 0x90\n";
> -
> - // Bake in a pointer to the callback manager immediately before the
> - // start of the resolver function.
> - AsmStream << "jit_callback_manager_addr:\n"
> - << " .quad " << &JCBM << "\n";
> -
> - // Start the resolver function.
> - AsmStream << ResolverBlockName << ":\n"
> - << " pushq %rbp\n"
> - << " movq %rsp, %rbp\n";
> -
> - // Store the GPRs.
> - for (const auto &GPR : GPRs)
> - AsmStream << " pushq %" << GPR << "\n";
> -
> - // Store floating-point state with FXSAVE.
> - // Note: We need to keep the stack 16-byte aligned, so if we've emitted an odd
> - // number of 64-bit pushes so far (GPRs.size() plus 1 for RBP) then add
> - // an extra 64 bits of padding to the FXSave area.
> - unsigned Padding = (GPRs.size() + 1) % 2 ? 8 : 0;
> - unsigned FXSaveSize = 512 + Padding;
> - AsmStream << " subq $" << FXSaveSize << ", %rsp\n"
> - << " fxsave64 (%rsp)\n"
> -
> - // Load callback manager address, compute trampoline address, call JIT.
> - << " lea jit_callback_manager_addr(%rip), %rdi\n"
> - << " movq (%rdi), %rdi\n"
> - << " movq 0x8(%rbp), %rsi\n"
> - << " subq $" << X86_64_TrampolineLength << ", %rsi\n"
> - << " movabsq $" << CallbackAddr << ", %rax\n"
> - << " callq *%rax\n"
> -
> - // Replace the return to the trampoline with the return address of the
> - // compiled function body.
> - << " movq %rax, 0x8(%rbp)\n"
> -
> - // Restore the floating point state.
> - << " fxrstor64 (%rsp)\n"
> - << " addq $" << FXSaveSize << ", %rsp\n";
> -
> - for (const auto &GPR : make_range(GPRs.rbegin(), GPRs.rend()))
> - AsmStream << " popq %" << GPR << "\n";
> -
> - // Restore original RBP and return to compiled function body.
> - AsmStream << " popq %rbp\n"
> - << " retq\n";
> + // 0x70: Callback mgr address.
> + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
> + };
>
> - M.appendModuleInlineAsm(AsmStream.str());
> + const unsigned ReentryFnAddrOffset = 0x3a;
> + const unsigned CallbackMgrAddrOffset = 0x70;
> +
> + memcpy(ResolverMem, ResolverCode, sizeof(ResolverCode));
> + memcpy(ResolverMem + ReentryFnAddrOffset, &ReentryFn, sizeof(ReentryFn));
> + memcpy(ResolverMem + CallbackMgrAddrOffset, &CallbackMgr,
> + sizeof(CallbackMgr));
> }
>
> -OrcX86_64::LabelNameFtor
> -OrcX86_64::insertCompileCallbackTrampolines(Module &M,
> - TargetAddress ResolverBlockAddr,
> - unsigned NumCalls,
> - unsigned StartIndex) {
> - const char *ResolverBlockPtrName = "Lorc_resolve_block_addr";
> -
> - std::ostringstream AsmStream;
> - Triple TT(M.getTargetTriple());
> -
> - if (TT.getOS() == Triple::Darwin)
> - AsmStream << ".section __TEXT,__text,regular,pure_instructions\n"
> - << ".align 4, 0x90\n";
> - else
> - AsmStream << ".text\n"
> - << ".align 16, 0x90\n";
> -
> - AsmStream << ResolverBlockPtrName << ":\n"
> - << " .quad " << ResolverBlockAddr << "\n";
> -
> - auto GetLabelName =
> - [=](unsigned I) {
> - std::ostringstream LabelStream;
> - LabelStream << "orc_jcc_" << (StartIndex + I);
> - return LabelStream.str();
> - };
> +void OrcX86_64::writeTrampolines(uint8_t *TrampolineMem, void *ResolverAddr,
> + unsigned NumTrampolines) {
> +
> + unsigned OffsetToPtr = NumTrampolines * TrampolineSize;
>
> - for (unsigned I = 0; I < NumCalls; ++I)
> - AsmStream << GetLabelName(I) << ":\n"
> - << " callq *" << ResolverBlockPtrName << "(%rip)\n";
> + memcpy(TrampolineMem + OffsetToPtr, &ResolverAddr, sizeof(void*));
>
> - M.appendModuleInlineAsm(AsmStream.str());
> + uint64_t *Trampolines = reinterpret_cast<uint64_t*>(TrampolineMem);
> + uint64_t CallIndirPCRel = 0xf1c40000000015ff;
>
> - return GetLabelName;
> + for (unsigned I = 0; I < NumTrampolines; ++I, OffsetToPtr -= TrampolineSize)
> + Trampolines[I] = CallIndirPCRel | ((OffsetToPtr - 6) << 16);
> }
>
> std::error_code OrcX86_64::emitIndirectStubsBlock(IndirectStubsInfo &StubsInfo,
>
> Modified: llvm/trunk/tools/lli/OrcLazyJIT.cpp
> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/lli/OrcLazyJIT.cpp?rev=251933&r1=251932&r2=251933&view=diff
> ==============================================================================
> --- llvm/trunk/tools/lli/OrcLazyJIT.cpp (original)
> +++ llvm/trunk/tools/lli/OrcLazyJIT.cpp Tue Nov 3 10:10:18 2015
> @@ -46,19 +46,14 @@ namespace {
> cl::init(true), cl::Hidden);
> }
>
> -OrcLazyJIT::CallbackManagerBuilder
> -OrcLazyJIT::createCallbackMgrBuilder(Triple T) {
> +std::unique_ptr<OrcLazyJIT::CompileCallbackMgr>
> +OrcLazyJIT::createCompileCallbackMgr(Triple T) {
> switch (T.getArch()) {
> default: return nullptr;
>
> case Triple::x86_64: {
> - typedef orc::JITCompileCallbackManager<IRDumpLayerT,
> - orc::OrcX86_64> CCMgrT;
> - return [](IRDumpLayerT &IRDumpLayer, RuntimeDyld::MemoryManager &MemMgr,
> - LLVMContext &Context) {
> - return llvm::make_unique<CCMgrT>(IRDumpLayer, MemMgr, Context, 0,
> - 64);
> - };
> + typedef orc::JITCompileCallbackManager<orc::OrcX86_64> CCMgrT;
> + return llvm::make_unique<CCMgrT>(0);
> }
> }
> }
> @@ -146,13 +141,12 @@ int llvm::runOrcLazyJIT(std::unique_ptr<
> EngineBuilder EB;
> EB.setOptLevel(getOptLevel());
> auto TM = std::unique_ptr<TargetMachine>(EB.selectTarget());
> - auto &Context = getGlobalContext();
> - auto CallbackMgrBuilder =
> - OrcLazyJIT::createCallbackMgrBuilder(Triple(TM->getTargetTriple()));
> + auto CompileCallbackMgr =
> + OrcLazyJIT::createCompileCallbackMgr(Triple(TM->getTargetTriple()));
>
> // If we couldn't build the factory function then there must not be a callback
> // manager for this target. Bail out.
> - if (!CallbackMgrBuilder) {
> + if (!CompileCallbackMgr) {
> errs() << "No callback manager available for target '"
> << TM->getTargetTriple().str() << "'.\n";
> return 1;
> @@ -169,7 +163,7 @@ int llvm::runOrcLazyJIT(std::unique_ptr<
> }
>
> // Everything looks good. Build the JIT.
> - OrcLazyJIT J(std::move(TM), Context, CallbackMgrBuilder,
> + OrcLazyJIT J(std::move(TM), std::move(CompileCallbackMgr),
> std::move(IndirectStubsMgrBuilder),
> OrcInlineStubs);
>
>
> Modified: llvm/trunk/tools/lli/OrcLazyJIT.h
> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/lli/OrcLazyJIT.h?rev=251933&r1=251932&r2=251933&view=diff
> ==============================================================================
> --- llvm/trunk/tools/lli/OrcLazyJIT.h (original)
> +++ llvm/trunk/tools/lli/OrcLazyJIT.h Tue Nov 3 10:10:18 2015
> @@ -23,7 +23,6 @@
> #include "llvm/ExecutionEngine/Orc/IRTransformLayer.h"
> #include "llvm/ExecutionEngine/Orc/ObjectLinkingLayer.h"
> #include "llvm/ExecutionEngine/RTDyldMemoryManager.h"
> -#include "llvm/IR/LLVMContext.h"
>
> namespace llvm {
>
> @@ -41,21 +40,16 @@ public:
> IndirectStubsManagerBuilder;
> typedef CODLayerT::ModuleSetHandleT ModuleHandleT;
>
> - typedef std::function<
> - std::unique_ptr<CompileCallbackMgr>(IRDumpLayerT&,
> - RuntimeDyld::MemoryManager&,
> - LLVMContext&)>
> - CallbackManagerBuilder;
> -
> - OrcLazyJIT(std::unique_ptr<TargetMachine> TM, LLVMContext &Context,
> - CallbackManagerBuilder &BuildCallbackMgr,
> + OrcLazyJIT(std::unique_ptr<TargetMachine> TM,
> + std::unique_ptr<CompileCallbackMgr> CCMgr,
> IndirectStubsManagerBuilder IndirectStubsMgrBuilder,
> bool InlineStubs)
> - : TM(std::move(TM)), DL(this->TM->createDataLayout()), ObjectLayer(),
> + : TM(std::move(TM)), DL(this->TM->createDataLayout()),
> + CCMgr(std::move(CCMgr)),
> + ObjectLayer(),
> CompileLayer(ObjectLayer, orc::SimpleCompiler(*this->TM)),
> IRDumpLayer(CompileLayer, createDebugDumper()),
> - CCMgr(BuildCallbackMgr(IRDumpLayer, CCMgrMemMgr, Context)),
> - CODLayer(IRDumpLayer, extractSingleFunction, *CCMgr,
> + CODLayer(IRDumpLayer, extractSingleFunction, *this->CCMgr,
> std::move(IndirectStubsMgrBuilder), InlineStubs),
> CXXRuntimeOverrides(
> [this](const std::string &S) { return mangle(S); }) {}
> @@ -68,8 +62,7 @@ public:
> DtorRunner.runViaLayer(CODLayer);
> }
>
> - static CallbackManagerBuilder createCallbackMgrBuilder(Triple T);
> -
> + static std::unique_ptr<CompileCallbackMgr> createCompileCallbackMgr(Triple T);
> static IndirectStubsManagerBuilder createIndirectStubsMgrBuilder(Triple T);
>
> ModuleHandleT addModule(std::unique_ptr<Module> M) {
> @@ -155,10 +148,10 @@ private:
> DataLayout DL;
> SectionMemoryManager CCMgrMemMgr;
>
> + std::unique_ptr<CompileCallbackMgr> CCMgr;
> ObjLayerT ObjectLayer;
> CompileLayerT CompileLayer;
> IRDumpLayerT IRDumpLayer;
> - std::unique_ptr<CompileCallbackMgr> CCMgr;
> CODLayerT CODLayer;
>
> orc::LocalCXXRuntimeOverrides CXXRuntimeOverrides;
>
> Modified: llvm/trunk/unittests/ExecutionEngine/Orc/CompileOnDemandLayerTest.cpp
> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/ExecutionEngine/Orc/CompileOnDemandLayerTest.cpp?rev=251933&r1=251932&r2=251933&view=diff
> ==============================================================================
> --- llvm/trunk/unittests/ExecutionEngine/Orc/CompileOnDemandLayerTest.cpp (original)
> +++ llvm/trunk/unittests/ExecutionEngine/Orc/CompileOnDemandLayerTest.cpp Tue Nov 3 10:10:18 2015
> @@ -19,11 +19,11 @@ namespace {
> class DummyCallbackManager : public orc::JITCompileCallbackManagerBase {
> public:
> DummyCallbackManager()
> - : JITCompileCallbackManagerBase(0, 0), NextStubAddress(0),
> + : JITCompileCallbackManagerBase(0), NextStubAddress(0),
> UniversalCompile([]() { return 0; }) {
> }
>
> - CompileCallbackInfo getCompileCallback(LLVMContext &Context) override {
> + CompileCallbackInfo getCompileCallback() override {
> return CompileCallbackInfo(++NextStubAddress, UniversalCompile);
> }
> public:
>
> Modified: llvm/trunk/unittests/ExecutionEngine/Orc/OrcCAPITest.cpp
> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/ExecutionEngine/Orc/OrcCAPITest.cpp?rev=251933&r1=251932&r2=251933&view=diff
> ==============================================================================
> --- llvm/trunk/unittests/ExecutionEngine/Orc/OrcCAPITest.cpp (original)
> +++ llvm/trunk/unittests/ExecutionEngine/Orc/OrcCAPITest.cpp Tue Nov 3 10:10:18 2015
> @@ -82,7 +82,7 @@ TEST_F(OrcCAPIExecutionTest, TestEagerIR
> return;
>
> LLVMOrcJITStackRef JIT =
> - LLVMOrcCreateInstance(wrap(TM.get()), LLVMGetGlobalContext());
> + LLVMOrcCreateInstance(wrap(TM.get()));
>
> std::unique_ptr<Module> M = createTestModule(TM->getTargetTriple());
>
> @@ -106,7 +106,7 @@ TEST_F(OrcCAPIExecutionTest, TestLazyIRC
> return;
>
> LLVMOrcJITStackRef JIT =
> - LLVMOrcCreateInstance(wrap(TM.get()), LLVMGetGlobalContext());
> + LLVMOrcCreateInstance(wrap(TM.get()));
>
> std::unique_ptr<Module> M = createTestModule(TM->getTargetTriple());
>
> @@ -130,7 +130,7 @@ TEST_F(OrcCAPIExecutionTest, TestDirectC
> return;
>
> LLVMOrcJITStackRef JIT =
> - LLVMOrcCreateInstance(wrap(TM.get()), LLVMGetGlobalContext());
> + LLVMOrcCreateInstance(wrap(TM.get()));
>
> LLVMOrcGetMangledSymbol(JIT, &testFuncName, "testFunc");
>
>
>
> _______________________________________________
> llvm-commits mailing list
> llvm-commits at lists.llvm.org
> http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits
More information about the llvm-commits
mailing list