Skip to content

Commit a5157d6

Browse files
committed
[ORC] Simplify naming for JITDylib definition generators.
Renames: JITDylib's setFallbackDefinitionGenerator method to setGenerator. DynamicLibraryFallbackGenerator class to DynamicLibrarySearchGenerator. ReexportsFallbackDefinitionGenerator to ReexportsGenerator. llvm-svn: 344489
1 parent 3c01508 commit a5157d6

File tree

6 files changed

+80
-75
lines changed

6 files changed

+80
-75
lines changed

llvm/include/llvm/ExecutionEngine/Orc/Core.h

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -395,15 +395,22 @@ reexports(JITDylib &SourceJD, SymbolAliasMap Aliases) {
395395
Expected<SymbolAliasMap>
396396
buildSimpleReexportsAliasMap(JITDylib &SourceJD, const SymbolNameSet &Symbols);
397397

398-
class ReexportsFallbackDefinitionGenerator {
398+
/// ReexportsGenerator can be used with JITDylib::setGenerator to automatically
399+
/// re-export a subset of the source JITDylib's symbols in the target.
400+
class ReexportsGenerator {
399401
public:
400402
using SymbolPredicate = std::function<bool(SymbolStringPtr)>;
401-
ReexportsFallbackDefinitionGenerator(JITDylib &BackingJD,
402-
SymbolPredicate Allow);
403+
404+
/// Create a reexports generator. If an Allow predicate is passed, only
405+
/// symbols for which the predicate returns true will be reexported. If no
406+
/// Allow predicate is passed, all symbols will be exported.
407+
ReexportsGenerator(JITDylib &SourceJD,
408+
SymbolPredicate Allow = SymbolPredicate());
409+
403410
SymbolNameSet operator()(JITDylib &JD, const SymbolNameSet &Names);
404411

405412
private:
406-
JITDylib &BackingJD;
413+
JITDylib &SourceJD;
407414
SymbolPredicate Allow;
408415
};
409416

@@ -478,7 +485,7 @@ class JITDylib {
478485
friend class ExecutionSession;
479486
friend class MaterializationResponsibility;
480487
public:
481-
using FallbackDefinitionGeneratorFunction = std::function<SymbolNameSet(
488+
using GeneratorFunction = std::function<SymbolNameSet(
482489
JITDylib &Parent, const SymbolNameSet &Names)>;
483490

484491
using AsynchronousSymbolQuerySet =
@@ -495,12 +502,12 @@ class JITDylib {
495502
/// Get a reference to the ExecutionSession for this JITDylib.
496503
ExecutionSession &getExecutionSession() const { return ES; }
497504

498-
/// Set a fallback defenition generator. If set, lookup and lookupFlags will
499-
/// pass the unresolved symbols set to the fallback definition generator,
500-
/// allowing it to add a new definition to the JITDylib.
501-
void setFallbackDefinitionGenerator(
502-
FallbackDefinitionGeneratorFunction FallbackDefinitionGenerator) {
503-
this->FallbackDefinitionGenerator = std::move(FallbackDefinitionGenerator);
505+
/// Set a definition generator. If set, whenever a symbol fails to resolve
506+
/// within this JITDylib, lookup and lookupFlags will pass the unresolved
507+
/// symbols set to the definition generator. The generator can optionally
508+
/// add a definition for the unresolved symbols to the dylib.
509+
void setGenerator(GeneratorFunction DefGenerator) {
510+
this->DefGenerator = std::move(DefGenerator);
504511
}
505512

506513
/// Set the search order to be used when fixing up definitions in JITDylib.
@@ -667,7 +674,7 @@ class JITDylib {
667674
SymbolMap Symbols;
668675
UnmaterializedInfosMap UnmaterializedInfos;
669676
MaterializingInfosMap MaterializingInfos;
670-
FallbackDefinitionGeneratorFunction FallbackDefinitionGenerator;
677+
GeneratorFunction DefGenerator;
671678
JITDylibList SearchOrder;
672679
};
673680

llvm/include/llvm/ExecutionEngine/Orc/ExecutionUtils.h

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -212,32 +212,30 @@ class LocalCXXRuntimeOverrides2 : public LocalCXXRuntimeOverridesBase {
212212
/// If an instance of this class is attached to a JITDylib as a fallback
213213
/// definition generator, then any symbol found in the given DynamicLibrary that
214214
/// passes the 'Allow' predicate will be added to the JITDylib.
215-
class DynamicLibraryFallbackGenerator {
215+
class DynamicLibrarySearchGenerator {
216216
public:
217217
using SymbolPredicate = std::function<bool(SymbolStringPtr)>;
218218

219-
static bool AllowAll(SymbolStringPtr Name) { return true; }
220-
221-
/// Create a DynamicLibraryFallbackGenerator that searches for symbols in the
219+
/// Create a DynamicLibrarySearchGenerator that searches for symbols in the
222220
/// given sys::DynamicLibrary.
223-
/// Only symbols that match the 'Allow' predicate will be searched for.
224-
DynamicLibraryFallbackGenerator(sys::DynamicLibrary Dylib,
225-
const DataLayout &DL,
226-
SymbolPredicate Allow = AllowAll);
221+
/// If the Allow predicate is given then only symbols matching the predicate
222+
/// will be searched for in the DynamicLibrary. If the predicate is not given
223+
/// then all symbols will be searched for.
224+
DynamicLibrarySearchGenerator(sys::DynamicLibrary Dylib, const DataLayout &DL,
225+
SymbolPredicate Allow = SymbolPredicate());
227226

228227
/// Permanently loads the library at the given path and, on success, returns
229-
/// a DynamicLibraryFallbackGenerator that will search it for symbol
230-
/// definitions matching the Allow predicate.
231-
/// On failure returns the reason the library failed to load.
232-
static Expected<DynamicLibraryFallbackGenerator>
228+
/// a DynamicLibrarySearchGenerator that will search it for symbol definitions
229+
/// in the library. On failure returns the reason the library failed to load.
230+
static Expected<DynamicLibrarySearchGenerator>
233231
Load(const char *FileName, const DataLayout &DL,
234-
SymbolPredicate Allow = AllowAll);
232+
SymbolPredicate Allow = SymbolPredicate());
235233

236-
/// Creates a DynamicLibraryFallbackGenerator that searches for symbols in
234+
/// Creates a DynamicLibrarySearchGenerator that searches for symbols in
237235
/// the current process.
238-
static Expected<DynamicLibraryFallbackGenerator>
239-
CreateForCurrentProcess(const DataLayout &DL,
240-
SymbolPredicate Allow = AllowAll) {
236+
static Expected<DynamicLibrarySearchGenerator>
237+
GetForCurrentProcess(const DataLayout &DL,
238+
SymbolPredicate Allow = SymbolPredicate()) {
241239
return Load(nullptr, DL, std::move(Allow));
242240
}
243241

llvm/lib/ExecutionEngine/Orc/Core.cpp

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -686,26 +686,26 @@ buildSimpleReexportsAliasMap(JITDylib &SourceJD, const SymbolNameSet &Symbols) {
686686
return Result;
687687
}
688688

689-
ReexportsFallbackDefinitionGenerator::ReexportsFallbackDefinitionGenerator(
690-
JITDylib &BackingJD, SymbolPredicate Allow)
691-
: BackingJD(BackingJD), Allow(std::move(Allow)) {}
689+
ReexportsGenerator::ReexportsGenerator(JITDylib &SourceJD,
690+
SymbolPredicate Allow)
691+
: SourceJD(SourceJD), Allow(std::move(Allow)) {}
692692

693-
SymbolNameSet ReexportsFallbackDefinitionGenerator::
694-
operator()(JITDylib &JD, const SymbolNameSet &Names) {
693+
SymbolNameSet ReexportsGenerator::operator()(JITDylib &JD,
694+
const SymbolNameSet &Names) {
695695
orc::SymbolNameSet Added;
696696
orc::SymbolAliasMap AliasMap;
697697

698-
auto Flags = BackingJD.lookupFlags(Names);
698+
auto Flags = SourceJD.lookupFlags(Names);
699699

700700
for (auto &KV : Flags) {
701-
if (!Allow(KV.first))
701+
if (Allow && !Allow(KV.first))
702702
continue;
703703
AliasMap[KV.first] = SymbolAliasMapEntry(KV.first, KV.second);
704704
Added.insert(KV.first);
705705
}
706706

707707
if (!Added.empty())
708-
cantFail(JD.define(reexports(BackingJD, AliasMap)));
708+
cantFail(JD.define(reexports(SourceJD, AliasMap)));
709709

710710
return Added;
711711
}
@@ -1117,10 +1117,10 @@ SymbolFlagsMap JITDylib::lookupFlags(const SymbolNameSet &Names) {
11171117
return ES.runSessionLocked([&, this]() {
11181118
SymbolFlagsMap Result;
11191119
auto Unresolved = lookupFlagsImpl(Result, Names);
1120-
if (FallbackDefinitionGenerator && !Unresolved.empty()) {
1121-
auto FallbackDefs = FallbackDefinitionGenerator(*this, Unresolved);
1122-
if (!FallbackDefs.empty()) {
1123-
auto Unresolved2 = lookupFlagsImpl(Result, FallbackDefs);
1120+
if (DefGenerator && !Unresolved.empty()) {
1121+
auto NewDefs = DefGenerator(*this, Unresolved);
1122+
if (!NewDefs.empty()) {
1123+
auto Unresolved2 = lookupFlagsImpl(Result, NewDefs);
11241124
(void)Unresolved2;
11251125
assert(Unresolved2.empty() &&
11261126
"All fallback defs should have been found by lookupFlagsImpl");
@@ -1156,14 +1156,13 @@ void JITDylib::lodgeQuery(std::shared_ptr<AsynchronousSymbolQuery> &Q,
11561156
assert(Q && "Query can not be null");
11571157

11581158
lodgeQueryImpl(Q, Unresolved, MatchNonExportedInJD, MatchNonExported, MUs);
1159-
if (FallbackDefinitionGenerator && !Unresolved.empty()) {
1160-
auto FallbackDefs = FallbackDefinitionGenerator(*this, Unresolved);
1161-
if (!FallbackDefs.empty()) {
1162-
for (auto &D : FallbackDefs)
1159+
if (DefGenerator && !Unresolved.empty()) {
1160+
auto NewDefs = DefGenerator(*this, Unresolved);
1161+
if (!NewDefs.empty()) {
1162+
for (auto &D : NewDefs)
11631163
Unresolved.erase(D);
1164-
lodgeQueryImpl(Q, FallbackDefs, MatchNonExportedInJD, MatchNonExported,
1165-
MUs);
1166-
assert(FallbackDefs.empty() &&
1164+
lodgeQueryImpl(Q, NewDefs, MatchNonExportedInJD, MatchNonExported, MUs);
1165+
assert(NewDefs.empty() &&
11671166
"All fallback defs should have been found by lookupImpl");
11681167
}
11691168
}
@@ -1250,15 +1249,15 @@ SymbolNameSet JITDylib::legacyLookup(std::shared_ptr<AsynchronousSymbolQuery> Q,
12501249
SymbolNameSet Unresolved = std::move(Names);
12511250
ES.runSessionLocked([&, this]() {
12521251
ActionFlags = lookupImpl(Q, MUs, Unresolved);
1253-
if (FallbackDefinitionGenerator && !Unresolved.empty()) {
1252+
if (DefGenerator && !Unresolved.empty()) {
12541253
assert(ActionFlags == None &&
12551254
"ActionFlags set but unresolved symbols remain?");
1256-
auto FallbackDefs = FallbackDefinitionGenerator(*this, Unresolved);
1257-
if (!FallbackDefs.empty()) {
1258-
for (auto &D : FallbackDefs)
1255+
auto NewDefs = DefGenerator(*this, Unresolved);
1256+
if (!NewDefs.empty()) {
1257+
for (auto &D : NewDefs)
12591258
Unresolved.erase(D);
1260-
ActionFlags = lookupImpl(Q, MUs, FallbackDefs);
1261-
assert(FallbackDefs.empty() &&
1259+
ActionFlags = lookupImpl(Q, MUs, NewDefs);
1260+
assert(NewDefs.empty() &&
12621261
"All fallback defs should have been found by lookupImpl");
12631262
}
12641263
}

llvm/lib/ExecutionEngine/Orc/ExecutionUtils.cpp

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -178,29 +178,33 @@ Error LocalCXXRuntimeOverrides2::enable(JITDylib &JD,
178178
return JD.define(absoluteSymbols(std::move(RuntimeInterposes)));
179179
}
180180

181-
DynamicLibraryFallbackGenerator::DynamicLibraryFallbackGenerator(
181+
DynamicLibrarySearchGenerator::DynamicLibrarySearchGenerator(
182182
sys::DynamicLibrary Dylib, const DataLayout &DL, SymbolPredicate Allow)
183183
: Dylib(std::move(Dylib)), Allow(std::move(Allow)),
184184
GlobalPrefix(DL.getGlobalPrefix()) {}
185185

186-
Expected<DynamicLibraryFallbackGenerator> DynamicLibraryFallbackGenerator::Load(
187-
const char *FileName, const DataLayout &DL, SymbolPredicate Allow) {
186+
Expected<DynamicLibrarySearchGenerator>
187+
DynamicLibrarySearchGenerator::Load(const char *FileName, const DataLayout &DL,
188+
SymbolPredicate Allow) {
188189
std::string ErrMsg;
189190
auto Lib = sys::DynamicLibrary::getPermanentLibrary(FileName, &ErrMsg);
190191
if (!Lib.isValid())
191192
return make_error<StringError>(std::move(ErrMsg), inconvertibleErrorCode());
192-
return DynamicLibraryFallbackGenerator(std::move(Lib), DL, std::move(Allow));
193+
return DynamicLibrarySearchGenerator(std::move(Lib), DL, std::move(Allow));
193194
}
194195

195-
SymbolNameSet DynamicLibraryFallbackGenerator::
196+
SymbolNameSet DynamicLibrarySearchGenerator::
196197
operator()(JITDylib &JD, const SymbolNameSet &Names) {
197198
orc::SymbolNameSet Added;
198199
orc::SymbolMap NewSymbols;
199200

200201
bool HasGlobalPrefix = (GlobalPrefix != '\0');
201202

202203
for (auto &Name : Names) {
203-
if (!Allow(Name) || (*Name).empty())
204+
if ((*Name).empty())
205+
continue;
206+
207+
if (Allow && !Allow(Name))
204208
continue;
205209

206210
if (HasGlobalPrefix && (*Name).front() != GlobalPrefix)
@@ -215,8 +219,8 @@ operator()(JITDylib &JD, const SymbolNameSet &Names) {
215219
}
216220
}
217221

218-
// Add any new symbols to JD. Since the fallback generator is only called for
219-
// symbols that are not already defined, this will never trigger a duplicate
222+
// Add any new symbols to JD. Since the generator is only called for symbols
223+
// that are not already defined, this will never trigger a duplicate
220224
// definition error, so we can wrap this call in a 'cantFail'.
221225
if (!NewSymbols.empty())
222226
cantFail(JD.define(absoluteSymbols(std::move(NewSymbols))));

llvm/tools/lli/lli.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -793,8 +793,8 @@ int runOrcLazyJIT(const char *ProgName) {
793793
}
794794
return Dump(std::move(TSM), R);
795795
});
796-
J->getMainJITDylib().setFallbackDefinitionGenerator(ExitOnErr(
797-
orc::DynamicLibraryFallbackGenerator::CreateForCurrentProcess(DL)));
796+
J->getMainJITDylib().setGenerator(
797+
ExitOnErr(orc::DynamicLibrarySearchGenerator::GetForCurrentProcess(DL)));
798798

799799
orc::MangleAndInterner Mangle(J->getExecutionSession(), DL);
800800
orc::LocalCXXRuntimeOverrides2 CXXRuntimeOverrides;

llvm/unittests/ExecutionEngine/Orc/CoreAPIsTest.cpp

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -342,17 +342,15 @@ TEST_F(CoreAPIsStandardTest, TestThatReExportsDontUnnecessarilyMaterialize) {
342342
EXPECT_FALSE(BarMaterialized) << "Bar should not have been materialized";
343343
}
344344

345-
TEST_F(CoreAPIsStandardTest, TestReexportsFallbackGenerator) {
346-
// Test that a re-exports fallback generator can dynamically generate
347-
// reexports.
345+
TEST_F(CoreAPIsStandardTest, TestReexportsGenerator) {
346+
// Test that a re-exports generator can dynamically generate reexports.
348347

349348
auto &JD2 = ES.createJITDylib("JD2");
350349
cantFail(JD2.define(absoluteSymbols({{Foo, FooSym}, {Bar, BarSym}})));
351350

352351
auto Filter = [this](SymbolStringPtr Name) { return Name != Bar; };
353352

354-
JD.setFallbackDefinitionGenerator(
355-
ReexportsFallbackDefinitionGenerator(JD2, Filter));
353+
JD.setGenerator(ReexportsGenerator(JD2, Filter));
356354

357355
auto Flags = JD.lookupFlags({Foo, Bar, Baz});
358356
EXPECT_EQ(Flags.size(), 1U) << "Unexpected number of results";
@@ -679,14 +677,13 @@ TEST_F(CoreAPIsStandardTest, DefineMaterializingSymbol) {
679677
<< "Expected Bar == BarSym";
680678
}
681679

682-
TEST_F(CoreAPIsStandardTest, FallbackDefinitionGeneratorTest) {
680+
TEST_F(CoreAPIsStandardTest, GeneratorTest) {
683681
cantFail(JD.define(absoluteSymbols({{Foo, FooSym}})));
684682

685-
JD.setFallbackDefinitionGenerator(
686-
[&](JITDylib &JD2, const SymbolNameSet &Names) {
687-
cantFail(JD2.define(absoluteSymbols({{Bar, BarSym}})));
688-
return SymbolNameSet({Bar});
689-
});
683+
JD.setGenerator([&](JITDylib &JD2, const SymbolNameSet &Names) {
684+
cantFail(JD2.define(absoluteSymbols({{Bar, BarSym}})));
685+
return SymbolNameSet({Bar});
686+
});
690687

691688
auto Result = cantFail(ES.lookup({&JD}, {Foo, Bar}));
692689

0 commit comments

Comments
 (0)