diff --git a/llvm/include/llvm/ExecutionEngine/Orc/COFFPlatform.h b/llvm/include/llvm/ExecutionEngine/Orc/COFFPlatform.h --- a/llvm/include/llvm/ExecutionEngine/Orc/COFFPlatform.h +++ b/llvm/include/llvm/ExecutionEngine/Orc/COFFPlatform.h @@ -46,8 +46,6 @@ const char *VCRuntimePath = nullptr, Optional RuntimeAliases = None); - Error bootstrap(JITDylib &PlatformJD); - ExecutionSession &getExecutionSession() const { return ES; } ObjectLinkingLayer &getObjectLinkingLayer() const { return ObjLinkingLayer; } diff --git a/llvm/include/llvm/ExecutionEngine/Orc/LLJIT.h b/llvm/include/llvm/ExecutionEngine/Orc/LLJIT.h --- a/llvm/include/llvm/ExecutionEngine/Orc/LLJIT.h +++ b/llvm/include/llvm/ExecutionEngine/Orc/LLJIT.h @@ -447,6 +447,9 @@ public LLLazyJITBuilderSetters {}; +/// Configure the LLJIT instance to use orc runtime support. +Error setUpOrcPlatform(LLJIT& J); + /// Configure the LLJIT instance to scrape modules for llvm.global_ctors and /// llvm.global_dtors variables and (if present) build initialization and /// deinitialization functions. Platform specific initialization configurations diff --git a/llvm/lib/ExecutionEngine/Orc/COFFPlatform.cpp b/llvm/lib/ExecutionEngine/Orc/COFFPlatform.cpp --- a/llvm/lib/ExecutionEngine/Orc/COFFPlatform.cpp +++ b/llvm/lib/ExecutionEngine/Orc/COFFPlatform.cpp @@ -413,33 +413,35 @@ Err = std::move(E2); return; } -} -Error COFFPlatform::bootstrap(JITDylib &PlatformJD) { - for (auto &Lib : DylibsToPreload) - if (auto Err = LoadDynLibrary(PlatformJD, Lib)) - return Err; + for (auto& Lib : DylibsToPreload) + if (auto E2 = LoadDynLibrary(PlatformJD, Lib)) { + Err = std::move(E2); + return; + } if (StaticVCRuntime) - if (auto Err = VCRuntimeBootstrap->initializeStaticVCRuntime(PlatformJD)) - return Err; + if (auto E2 = VCRuntimeBootstrap->initializeStaticVCRuntime(PlatformJD)) { + Err = std::move(E2); + return; + } // Associate wrapper function tags with JIT-side function implementations. - if (auto Err = associateRuntimeSupportFunctions(PlatformJD)) { - return Err; + if (auto E2 = associateRuntimeSupportFunctions(PlatformJD)) { + Err = std::move(E2); + return; } // Lookup addresses of runtime functions callable by the platform, // call the platform bootstrap function to initialize the platform-state // object in the executor. - if (auto Err = bootstrapCOFFRuntime(PlatformJD)) { - return Err; + if (auto E2 = bootstrapCOFFRuntime(PlatformJD)) { + Err = std::move(E2); + return; } Bootstrapping.store(false); JDBootstrapStates.clear(); - - return Error::success(); } Expected diff --git a/llvm/lib/ExecutionEngine/Orc/LLJIT.cpp b/llvm/lib/ExecutionEngine/Orc/LLJIT.cpp --- a/llvm/lib/ExecutionEngine/Orc/LLJIT.cpp +++ b/llvm/lib/ExecutionEngine/Orc/LLJIT.cpp @@ -81,6 +81,53 @@ return WrapperFn; } +class ORCPlatformSupport : public LLJIT::PlatformSupport { +public: + ORCPlatformSupport(orc::LLJIT &J) : J(J) {} + + Error initialize(orc::JITDylib &JD) override { + using llvm::orc::shared::SPSExecutorAddr; + using llvm::orc::shared::SPSString; + using SPSDLOpenSig = SPSExecutorAddr(SPSString, int32_t); + enum dlopen_mode : int32_t { + ORC_RT_RTLD_LAZY = 0x1, + ORC_RT_RTLD_NOW = 0x2, + ORC_RT_RTLD_LOCAL = 0x4, + ORC_RT_RTLD_GLOBAL = 0x8 + }; + + if (auto WrapperAddr = J.lookup("__orc_rt_jit_dlopen_wrapper")) { + return J.getExecutionSession().callSPSWrapper( + *WrapperAddr, DSOHandles[&JD], JD.getName(), + int32_t(ORC_RT_RTLD_LAZY)); + } else + return WrapperAddr.takeError(); + } + + Error deinitialize(orc::JITDylib &JD) override { + using llvm::orc::shared::SPSExecutorAddr; + using SPSDLCloseSig = int32_t(SPSExecutorAddr); + + if (auto WrapperAddr = J.lookup("__orc_rt_jit_dlclose_wrapper")) { + int32_t result; + auto E = J.getExecutionSession().callSPSWrapper( + *WrapperAddr, result, DSOHandles[&JD]); + if (E) + return E; + else if (result) + return make_error("dlclose failed", + inconvertibleErrorCode()); + DSOHandles.erase(&JD); + } else + return WrapperAddr.takeError(); + return Error::success(); + } + +private: + orc::LLJIT &J; + DenseMap DSOHandles; +}; + class GenericLLVMIRPlatformSupport; /// orc::Platform component of Generic LLVM IR Platform support. @@ -880,6 +927,13 @@ return Error::success(); } +Error setUpOrcPlatform(LLJIT& J) { + LLVM_DEBUG( + { dbgs() << "Setting up orc platform support for LLJIT\n"; }); + J.setPlatformSupport(std::make_unique(J)); + return Error::success(); +} + void setUpGenericLLVMIRPlatform(LLJIT &J) { LLVM_DEBUG( { dbgs() << "Setting up GenericLLVMIRPlatform support for LLJIT\n"; }); diff --git a/llvm/tools/lli/lli.cpp b/llvm/tools/lli/lli.cpp --- a/llvm/tools/lli/lli.cpp +++ b/llvm/tools/lli/lli.cpp @@ -373,53 +373,6 @@ } }; -class ORCPlatformSupport : public orc::LLJIT::PlatformSupport { -public: - ORCPlatformSupport(orc::LLJIT &J) : J(J) {} - - Error initialize(orc::JITDylib &JD) override { - using llvm::orc::shared::SPSExecutorAddr; - using llvm::orc::shared::SPSString; - using SPSDLOpenSig = SPSExecutorAddr(SPSString, int32_t); - enum dlopen_mode : int32_t { - ORC_RT_RTLD_LAZY = 0x1, - ORC_RT_RTLD_NOW = 0x2, - ORC_RT_RTLD_LOCAL = 0x4, - ORC_RT_RTLD_GLOBAL = 0x8 - }; - - if (auto WrapperAddr = J.lookup("__orc_rt_jit_dlopen_wrapper")) { - return J.getExecutionSession().callSPSWrapper( - *WrapperAddr, DSOHandles[&JD], JD.getName(), - int32_t(ORC_RT_RTLD_LAZY)); - } else - return WrapperAddr.takeError(); - } - - Error deinitialize(orc::JITDylib &JD) override { - using llvm::orc::shared::SPSExecutorAddr; - using SPSDLCloseSig = int32_t(SPSExecutorAddr); - - if (auto WrapperAddr = J.lookup("__orc_rt_jit_dlclose_wrapper")) { - int32_t result; - auto E = J.getExecutionSession().callSPSWrapper( - *WrapperAddr, result, DSOHandles[&JD]); - if (E) - return E; - else if (result) - return make_error("dlclose failed", - inconvertibleErrorCode()); - DSOHandles.erase(&JD); - } else - return WrapperAddr.takeError(); - return Error::success(); - } - -private: - orc::LLJIT &J; - DenseMap DSOHandles; -}; - // On Mingw and Cygwin, an external symbol named '__main' is called from the // generated 'main' function to allow static initialization. To avoid linking // problems with remote targets (because lli's remote target support does not @@ -969,10 +922,7 @@ } switch (P) { case LLJITPlatform::ORC: - Builder.setPlatformSetUp([](llvm::orc::LLJIT &J) -> llvm::Error { - J.setPlatformSupport(std::make_unique(J)); - return Error::success(); - }); + Builder.setPlatformSetUp(orc::setUpOrcPlatform); break; case LLJITPlatform::GenericIR: // Nothing to do: LLJITBuilder will use this by default. diff --git a/llvm/tools/llvm-jitlink/llvm-jitlink.cpp b/llvm/tools/llvm-jitlink/llvm-jitlink.cpp --- a/llvm/tools/llvm-jitlink/llvm-jitlink.cpp +++ b/llvm/tools/llvm-jitlink/llvm-jitlink.cpp @@ -1239,16 +1239,9 @@ }; if (auto P = COFFPlatform::Create(ES, ObjLayer, *MainJD, OrcRuntime.c_str(), - std::move(LoadDynLibrary))) { - // Set platform early to register jitdylib of dynamic libraries. - auto &CP = **P; + std::move(LoadDynLibrary))) ES.setPlatform(std::move(*P)); - - if (auto E2 = CP.bootstrap(*MainJD)) { - Err = std::move(E2); - return; - } - } else { + else { Err = P.takeError(); return; }