From 47d0a37704ebc529ad2ab8a46e12a06e88df3207 Mon Sep 17 00:00:00 2001 From: Lang Hames Date: Mon, 1 Oct 2018 00:59:28 +0000 Subject: [PATCH] [ORC] Add convenience methods for creating DynamicLibraryFallbackGenerators for libraries on disk, and for the current process. Avoids more boilerplate during JIT construction. llvm-svn: 343430 --- .../llvm/ExecutionEngine/Orc/ExecutionUtils.h | 26 +++++++++++++++++++++- llvm/lib/ExecutionEngine/Orc/ExecutionUtils.cpp | 9 ++++++++ llvm/tools/lli/lli.cpp | 14 ++---------- 3 files changed, 36 insertions(+), 13 deletions(-) diff --git a/llvm/include/llvm/ExecutionEngine/Orc/ExecutionUtils.h b/llvm/include/llvm/ExecutionEngine/Orc/ExecutionUtils.h index 5c58d4b..5225066 100644 --- a/llvm/include/llvm/ExecutionEngine/Orc/ExecutionUtils.h +++ b/llvm/include/llvm/ExecutionEngine/Orc/ExecutionUtils.h @@ -215,8 +215,32 @@ public: class DynamicLibraryFallbackGenerator { public: using SymbolPredicate = std::function; + + static bool AllowAll(SymbolStringPtr Name) { return true; } + + /// Create a DynamicLibraryFallbackGenerator that searches for symbols in the + /// given sys::DynamicLibrary. + /// Only symbols that match the 'Allow' predicate will be searched for. DynamicLibraryFallbackGenerator(sys::DynamicLibrary Dylib, - const DataLayout &DL, SymbolPredicate Allow); + const DataLayout &DL, + SymbolPredicate Allow = AllowAll); + + /// Permanently loads the library at the given path and, on success, returns + /// a DynamicLibraryFallbackGenerator that will search it for symbol + /// definitions matching the Allow predicate. + /// On failure returns the reason the library failed to load. + static Expected + Load(const char *FileName, const DataLayout &DL, + SymbolPredicate Allow = AllowAll); + + /// Creates a DynamicLibraryFallbackGenerator that searches for symbols in + /// the current process. + static Expected + CreateForCurrentProcess(const DataLayout &DL, + SymbolPredicate Allow = AllowAll) { + return Load(nullptr, DL, std::move(Allow)); + } + SymbolNameSet operator()(JITDylib &JD, const SymbolNameSet &Names); private: diff --git a/llvm/lib/ExecutionEngine/Orc/ExecutionUtils.cpp b/llvm/lib/ExecutionEngine/Orc/ExecutionUtils.cpp index e9e2745..29c3f4e 100644 --- a/llvm/lib/ExecutionEngine/Orc/ExecutionUtils.cpp +++ b/llvm/lib/ExecutionEngine/Orc/ExecutionUtils.cpp @@ -174,6 +174,15 @@ DynamicLibraryFallbackGenerator::DynamicLibraryFallbackGenerator( : Dylib(std::move(Dylib)), Allow(std::move(Allow)), GlobalPrefix(DL.getGlobalPrefix()) {} +Expected DynamicLibraryFallbackGenerator::Load( + const char *FileName, const DataLayout &DL, SymbolPredicate Allow) { + std::string ErrMsg; + auto Lib = sys::DynamicLibrary::getPermanentLibrary(FileName, &ErrMsg); + if (!Lib.isValid()) + return make_error(std::move(ErrMsg), inconvertibleErrorCode()); + return DynamicLibraryFallbackGenerator(std::move(Lib), DL, std::move(Allow)); +} + SymbolNameSet DynamicLibraryFallbackGenerator:: operator()(JITDylib &JD, const SymbolNameSet &Names) { orc::SymbolNameSet Added; diff --git a/llvm/tools/lli/lli.cpp b/llvm/tools/lli/lli.cpp index b23733e..4794fe5 100644 --- a/llvm/tools/lli/lli.cpp +++ b/llvm/tools/lli/lli.cpp @@ -752,15 +752,6 @@ static orc::IRTransformLayer2::TransformFunction createDebugDumper() { int runOrcLazyJIT(const char *ProgName) { // Start setting up the JIT environment. - // First add lli's symbols into the JIT's search space. - std::string ErrMsg; - sys::DynamicLibrary LibLLI = - sys::DynamicLibrary::getPermanentLibrary(nullptr, &ErrMsg); - if (!LibLLI.isValid()) { - errs() << "Error loading lli symbols: " << ErrMsg << ".\n"; - return 1; - } - // Parse the main module. orc::ThreadSafeContext TSCtx(llvm::make_unique()); SMDiagnostic Err; @@ -802,9 +793,8 @@ int runOrcLazyJIT(const char *ProgName) { } return Dump(std::move(TSM), R); }); - J->getMainJITDylib().setFallbackDefinitionGenerator( - orc::DynamicLibraryFallbackGenerator( - std::move(LibLLI), DL, [](orc::SymbolStringPtr) { return true; })); + J->getMainJITDylib().setFallbackDefinitionGenerator(ExitOnErr( + orc::DynamicLibraryFallbackGenerator::CreateForCurrentProcess(DL))); orc::MangleAndInterner Mangle(J->getExecutionSession(), DL); orc::LocalCXXRuntimeOverrides2 CXXRuntimeOverrides; -- 2.7.4