diff options
Diffstat (limited to 'llvm')
-rw-r--r-- | llvm/include/llvm/ExecutionEngine/Orc/Core.h | 31 | ||||
-rw-r--r-- | llvm/include/llvm/ExecutionEngine/Orc/ExecutionUtils.h | 32 | ||||
-rw-r--r-- | llvm/lib/ExecutionEngine/Orc/Core.cpp | 49 | ||||
-rw-r--r-- | llvm/lib/ExecutionEngine/Orc/ExecutionUtils.cpp | 20 | ||||
-rw-r--r-- | llvm/tools/lli/lli.cpp | 4 | ||||
-rw-r--r-- | llvm/unittests/ExecutionEngine/Orc/CoreAPIsTest.cpp | 19 |
6 files changed, 80 insertions, 75 deletions
diff --git a/llvm/include/llvm/ExecutionEngine/Orc/Core.h b/llvm/include/llvm/ExecutionEngine/Orc/Core.h index 24cdeeae42e..67b16894f6c 100644 --- a/llvm/include/llvm/ExecutionEngine/Orc/Core.h +++ b/llvm/include/llvm/ExecutionEngine/Orc/Core.h @@ -395,15 +395,22 @@ reexports(JITDylib &SourceJD, SymbolAliasMap Aliases) { Expected<SymbolAliasMap> buildSimpleReexportsAliasMap(JITDylib &SourceJD, const SymbolNameSet &Symbols); -class ReexportsFallbackDefinitionGenerator { +/// ReexportsGenerator can be used with JITDylib::setGenerator to automatically +/// re-export a subset of the source JITDylib's symbols in the target. +class ReexportsGenerator { public: using SymbolPredicate = std::function<bool(SymbolStringPtr)>; - ReexportsFallbackDefinitionGenerator(JITDylib &BackingJD, - SymbolPredicate Allow); + + /// Create a reexports generator. If an Allow predicate is passed, only + /// symbols for which the predicate returns true will be reexported. If no + /// Allow predicate is passed, all symbols will be exported. + ReexportsGenerator(JITDylib &SourceJD, + SymbolPredicate Allow = SymbolPredicate()); + SymbolNameSet operator()(JITDylib &JD, const SymbolNameSet &Names); private: - JITDylib &BackingJD; + JITDylib &SourceJD; SymbolPredicate Allow; }; @@ -478,7 +485,7 @@ class JITDylib { friend class ExecutionSession; friend class MaterializationResponsibility; public: - using FallbackDefinitionGeneratorFunction = std::function<SymbolNameSet( + using GeneratorFunction = std::function<SymbolNameSet( JITDylib &Parent, const SymbolNameSet &Names)>; using AsynchronousSymbolQuerySet = @@ -495,12 +502,12 @@ public: /// Get a reference to the ExecutionSession for this JITDylib. ExecutionSession &getExecutionSession() const { return ES; } - /// Set a fallback defenition generator. If set, lookup and lookupFlags will - /// pass the unresolved symbols set to the fallback definition generator, - /// allowing it to add a new definition to the JITDylib. - void setFallbackDefinitionGenerator( - FallbackDefinitionGeneratorFunction FallbackDefinitionGenerator) { - this->FallbackDefinitionGenerator = std::move(FallbackDefinitionGenerator); + /// Set a definition generator. If set, whenever a symbol fails to resolve + /// within this JITDylib, lookup and lookupFlags will pass the unresolved + /// symbols set to the definition generator. The generator can optionally + /// add a definition for the unresolved symbols to the dylib. + void setGenerator(GeneratorFunction DefGenerator) { + this->DefGenerator = std::move(DefGenerator); } /// Set the search order to be used when fixing up definitions in JITDylib. @@ -667,7 +674,7 @@ private: SymbolMap Symbols; UnmaterializedInfosMap UnmaterializedInfos; MaterializingInfosMap MaterializingInfos; - FallbackDefinitionGeneratorFunction FallbackDefinitionGenerator; + GeneratorFunction DefGenerator; JITDylibList SearchOrder; }; diff --git a/llvm/include/llvm/ExecutionEngine/Orc/ExecutionUtils.h b/llvm/include/llvm/ExecutionEngine/Orc/ExecutionUtils.h index 52250662a95..662ed7b78e4 100644 --- a/llvm/include/llvm/ExecutionEngine/Orc/ExecutionUtils.h +++ b/llvm/include/llvm/ExecutionEngine/Orc/ExecutionUtils.h @@ -212,32 +212,30 @@ public: /// If an instance of this class is attached to a JITDylib as a fallback /// definition generator, then any symbol found in the given DynamicLibrary that /// passes the 'Allow' predicate will be added to the JITDylib. -class DynamicLibraryFallbackGenerator { +class DynamicLibrarySearchGenerator { public: using SymbolPredicate = std::function<bool(SymbolStringPtr)>; - static bool AllowAll(SymbolStringPtr Name) { return true; } - - /// Create a DynamicLibraryFallbackGenerator that searches for symbols in the + /// Create a DynamicLibrarySearchGenerator 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 = AllowAll); + /// If the Allow predicate is given then only symbols matching the predicate + /// will be searched for in the DynamicLibrary. If the predicate is not given + /// then all symbols will be searched for. + DynamicLibrarySearchGenerator(sys::DynamicLibrary Dylib, const DataLayout &DL, + SymbolPredicate Allow = SymbolPredicate()); /// 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<DynamicLibraryFallbackGenerator> + /// a DynamicLibrarySearchGenerator that will search it for symbol definitions + /// in the library. On failure returns the reason the library failed to load. + static Expected<DynamicLibrarySearchGenerator> Load(const char *FileName, const DataLayout &DL, - SymbolPredicate Allow = AllowAll); + SymbolPredicate Allow = SymbolPredicate()); - /// Creates a DynamicLibraryFallbackGenerator that searches for symbols in + /// Creates a DynamicLibrarySearchGenerator that searches for symbols in /// the current process. - static Expected<DynamicLibraryFallbackGenerator> - CreateForCurrentProcess(const DataLayout &DL, - SymbolPredicate Allow = AllowAll) { + static Expected<DynamicLibrarySearchGenerator> + GetForCurrentProcess(const DataLayout &DL, + SymbolPredicate Allow = SymbolPredicate()) { return Load(nullptr, DL, std::move(Allow)); } diff --git a/llvm/lib/ExecutionEngine/Orc/Core.cpp b/llvm/lib/ExecutionEngine/Orc/Core.cpp index c9cfacef61b..3fa28a5af6f 100644 --- a/llvm/lib/ExecutionEngine/Orc/Core.cpp +++ b/llvm/lib/ExecutionEngine/Orc/Core.cpp @@ -686,26 +686,26 @@ buildSimpleReexportsAliasMap(JITDylib &SourceJD, const SymbolNameSet &Symbols) { return Result; } -ReexportsFallbackDefinitionGenerator::ReexportsFallbackDefinitionGenerator( - JITDylib &BackingJD, SymbolPredicate Allow) - : BackingJD(BackingJD), Allow(std::move(Allow)) {} +ReexportsGenerator::ReexportsGenerator(JITDylib &SourceJD, + SymbolPredicate Allow) + : SourceJD(SourceJD), Allow(std::move(Allow)) {} -SymbolNameSet ReexportsFallbackDefinitionGenerator:: -operator()(JITDylib &JD, const SymbolNameSet &Names) { +SymbolNameSet ReexportsGenerator::operator()(JITDylib &JD, + const SymbolNameSet &Names) { orc::SymbolNameSet Added; orc::SymbolAliasMap AliasMap; - auto Flags = BackingJD.lookupFlags(Names); + auto Flags = SourceJD.lookupFlags(Names); for (auto &KV : Flags) { - if (!Allow(KV.first)) + if (Allow && !Allow(KV.first)) continue; AliasMap[KV.first] = SymbolAliasMapEntry(KV.first, KV.second); Added.insert(KV.first); } if (!Added.empty()) - cantFail(JD.define(reexports(BackingJD, AliasMap))); + cantFail(JD.define(reexports(SourceJD, AliasMap))); return Added; } @@ -1117,10 +1117,10 @@ SymbolFlagsMap JITDylib::lookupFlags(const SymbolNameSet &Names) { return ES.runSessionLocked([&, this]() { SymbolFlagsMap Result; auto Unresolved = lookupFlagsImpl(Result, Names); - if (FallbackDefinitionGenerator && !Unresolved.empty()) { - auto FallbackDefs = FallbackDefinitionGenerator(*this, Unresolved); - if (!FallbackDefs.empty()) { - auto Unresolved2 = lookupFlagsImpl(Result, FallbackDefs); + if (DefGenerator && !Unresolved.empty()) { + auto NewDefs = DefGenerator(*this, Unresolved); + if (!NewDefs.empty()) { + auto Unresolved2 = lookupFlagsImpl(Result, NewDefs); (void)Unresolved2; assert(Unresolved2.empty() && "All fallback defs should have been found by lookupFlagsImpl"); @@ -1156,14 +1156,13 @@ void JITDylib::lodgeQuery(std::shared_ptr<AsynchronousSymbolQuery> &Q, assert(Q && "Query can not be null"); lodgeQueryImpl(Q, Unresolved, MatchNonExportedInJD, MatchNonExported, MUs); - if (FallbackDefinitionGenerator && !Unresolved.empty()) { - auto FallbackDefs = FallbackDefinitionGenerator(*this, Unresolved); - if (!FallbackDefs.empty()) { - for (auto &D : FallbackDefs) + if (DefGenerator && !Unresolved.empty()) { + auto NewDefs = DefGenerator(*this, Unresolved); + if (!NewDefs.empty()) { + for (auto &D : NewDefs) Unresolved.erase(D); - lodgeQueryImpl(Q, FallbackDefs, MatchNonExportedInJD, MatchNonExported, - MUs); - assert(FallbackDefs.empty() && + lodgeQueryImpl(Q, NewDefs, MatchNonExportedInJD, MatchNonExported, MUs); + assert(NewDefs.empty() && "All fallback defs should have been found by lookupImpl"); } } @@ -1250,15 +1249,15 @@ SymbolNameSet JITDylib::legacyLookup(std::shared_ptr<AsynchronousSymbolQuery> Q, SymbolNameSet Unresolved = std::move(Names); ES.runSessionLocked([&, this]() { ActionFlags = lookupImpl(Q, MUs, Unresolved); - if (FallbackDefinitionGenerator && !Unresolved.empty()) { + if (DefGenerator && !Unresolved.empty()) { assert(ActionFlags == None && "ActionFlags set but unresolved symbols remain?"); - auto FallbackDefs = FallbackDefinitionGenerator(*this, Unresolved); - if (!FallbackDefs.empty()) { - for (auto &D : FallbackDefs) + auto NewDefs = DefGenerator(*this, Unresolved); + if (!NewDefs.empty()) { + for (auto &D : NewDefs) Unresolved.erase(D); - ActionFlags = lookupImpl(Q, MUs, FallbackDefs); - assert(FallbackDefs.empty() && + ActionFlags = lookupImpl(Q, MUs, NewDefs); + assert(NewDefs.empty() && "All fallback defs should have been found by lookupImpl"); } } diff --git a/llvm/lib/ExecutionEngine/Orc/ExecutionUtils.cpp b/llvm/lib/ExecutionEngine/Orc/ExecutionUtils.cpp index 6a180106240..667237373ca 100644 --- a/llvm/lib/ExecutionEngine/Orc/ExecutionUtils.cpp +++ b/llvm/lib/ExecutionEngine/Orc/ExecutionUtils.cpp @@ -178,21 +178,22 @@ Error LocalCXXRuntimeOverrides2::enable(JITDylib &JD, return JD.define(absoluteSymbols(std::move(RuntimeInterposes))); } -DynamicLibraryFallbackGenerator::DynamicLibraryFallbackGenerator( +DynamicLibrarySearchGenerator::DynamicLibrarySearchGenerator( sys::DynamicLibrary Dylib, const DataLayout &DL, SymbolPredicate Allow) : Dylib(std::move(Dylib)), Allow(std::move(Allow)), GlobalPrefix(DL.getGlobalPrefix()) {} -Expected<DynamicLibraryFallbackGenerator> DynamicLibraryFallbackGenerator::Load( - const char *FileName, const DataLayout &DL, SymbolPredicate Allow) { +Expected<DynamicLibrarySearchGenerator> +DynamicLibrarySearchGenerator::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<StringError>(std::move(ErrMsg), inconvertibleErrorCode()); - return DynamicLibraryFallbackGenerator(std::move(Lib), DL, std::move(Allow)); + return DynamicLibrarySearchGenerator(std::move(Lib), DL, std::move(Allow)); } -SymbolNameSet DynamicLibraryFallbackGenerator:: +SymbolNameSet DynamicLibrarySearchGenerator:: operator()(JITDylib &JD, const SymbolNameSet &Names) { orc::SymbolNameSet Added; orc::SymbolMap NewSymbols; @@ -200,7 +201,10 @@ operator()(JITDylib &JD, const SymbolNameSet &Names) { bool HasGlobalPrefix = (GlobalPrefix != '\0'); for (auto &Name : Names) { - if (!Allow(Name) || (*Name).empty()) + if ((*Name).empty()) + continue; + + if (Allow && !Allow(Name)) continue; if (HasGlobalPrefix && (*Name).front() != GlobalPrefix) @@ -215,8 +219,8 @@ operator()(JITDylib &JD, const SymbolNameSet &Names) { } } - // Add any new symbols to JD. Since the fallback generator is only called for - // symbols that are not already defined, this will never trigger a duplicate + // Add any new symbols to JD. Since the generator is only called for symbols + // that are not already defined, this will never trigger a duplicate // definition error, so we can wrap this call in a 'cantFail'. if (!NewSymbols.empty()) cantFail(JD.define(absoluteSymbols(std::move(NewSymbols)))); diff --git a/llvm/tools/lli/lli.cpp b/llvm/tools/lli/lli.cpp index 4794fe532a5..d633fe6f800 100644 --- a/llvm/tools/lli/lli.cpp +++ b/llvm/tools/lli/lli.cpp @@ -793,8 +793,8 @@ int runOrcLazyJIT(const char *ProgName) { } return Dump(std::move(TSM), R); }); - J->getMainJITDylib().setFallbackDefinitionGenerator(ExitOnErr( - orc::DynamicLibraryFallbackGenerator::CreateForCurrentProcess(DL))); + J->getMainJITDylib().setGenerator( + ExitOnErr(orc::DynamicLibrarySearchGenerator::GetForCurrentProcess(DL))); orc::MangleAndInterner Mangle(J->getExecutionSession(), DL); orc::LocalCXXRuntimeOverrides2 CXXRuntimeOverrides; diff --git a/llvm/unittests/ExecutionEngine/Orc/CoreAPIsTest.cpp b/llvm/unittests/ExecutionEngine/Orc/CoreAPIsTest.cpp index c8fa6ef5297..1ccc4755957 100644 --- a/llvm/unittests/ExecutionEngine/Orc/CoreAPIsTest.cpp +++ b/llvm/unittests/ExecutionEngine/Orc/CoreAPIsTest.cpp @@ -342,17 +342,15 @@ TEST_F(CoreAPIsStandardTest, TestThatReExportsDontUnnecessarilyMaterialize) { EXPECT_FALSE(BarMaterialized) << "Bar should not have been materialized"; } -TEST_F(CoreAPIsStandardTest, TestReexportsFallbackGenerator) { - // Test that a re-exports fallback generator can dynamically generate - // reexports. +TEST_F(CoreAPIsStandardTest, TestReexportsGenerator) { + // Test that a re-exports generator can dynamically generate reexports. auto &JD2 = ES.createJITDylib("JD2"); cantFail(JD2.define(absoluteSymbols({{Foo, FooSym}, {Bar, BarSym}}))); auto Filter = [this](SymbolStringPtr Name) { return Name != Bar; }; - JD.setFallbackDefinitionGenerator( - ReexportsFallbackDefinitionGenerator(JD2, Filter)); + JD.setGenerator(ReexportsGenerator(JD2, Filter)); auto Flags = JD.lookupFlags({Foo, Bar, Baz}); EXPECT_EQ(Flags.size(), 1U) << "Unexpected number of results"; @@ -679,14 +677,13 @@ TEST_F(CoreAPIsStandardTest, DefineMaterializingSymbol) { << "Expected Bar == BarSym"; } -TEST_F(CoreAPIsStandardTest, FallbackDefinitionGeneratorTest) { +TEST_F(CoreAPIsStandardTest, GeneratorTest) { cantFail(JD.define(absoluteSymbols({{Foo, FooSym}}))); - JD.setFallbackDefinitionGenerator( - [&](JITDylib &JD2, const SymbolNameSet &Names) { - cantFail(JD2.define(absoluteSymbols({{Bar, BarSym}}))); - return SymbolNameSet({Bar}); - }); + JD.setGenerator([&](JITDylib &JD2, const SymbolNameSet &Names) { + cantFail(JD2.define(absoluteSymbols({{Bar, BarSym}}))); + return SymbolNameSet({Bar}); + }); auto Result = cantFail(ES.lookup({&JD}, {Foo, Bar})); |