Skip to content
This repository was archived by the owner on Feb 5, 2019. It is now read-only.

Commit cea401e

Browse files
committed
[ORC] Replace SymbolResolvers in the new ORC layers with search orders on VSOs.
A search order is a list of VSOs to be searched linearly to find symbols. Each VSO now has a search order that will be used when fixing up definitions in that VSO. Each VSO's search order defaults to just that VSO itself. This is a first step towards removing symbol resolvers from ORC altogether. In practice symbol resolvers tended to be used to implement a search order anyway, sometimes with additional programatic generation of symbols. Now that VSOs support programmatic generation of definitions via fallback generators, search orders provide a cleaner way to achieve the desired effect (while removing a lot of boilerplate). git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@337593 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent 70215f5 commit cea401e

File tree

15 files changed

+376
-303
lines changed

15 files changed

+376
-303
lines changed

include/llvm/ExecutionEngine/Orc/CompileOnDemandLayer.h

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "llvm/ExecutionEngine/Orc/IndirectionUtils.h"
2424
#include "llvm/ExecutionEngine/Orc/LambdaResolver.h"
2525
#include "llvm/ExecutionEngine/Orc/Layer.h"
26+
#include "llvm/ExecutionEngine/Orc/Legacy.h"
2627
#include "llvm/ExecutionEngine/Orc/OrcError.h"
2728
#include "llvm/ExecutionEngine/RuntimeDyld.h"
2829
#include "llvm/IR/Attributes.h"
@@ -67,21 +68,11 @@ class CompileOnDemandLayer2 : public IRLayer {
6768
using IndirectStubsManagerBuilder =
6869
std::function<std::unique_ptr<IndirectStubsManager>()>;
6970

70-
/// Retrieve symbol resolver for the given VModuleKey.
71-
using GetSymbolResolverFunction =
72-
std::function<std::shared_ptr<SymbolResolver>(VModuleKey K)>;
73-
74-
/// Set the symbol resolver for the given VModuleKey.
75-
using SetSymbolResolverFunction =
76-
std::function<void(VModuleKey K, std::shared_ptr<SymbolResolver> R)>;
77-
7871
using GetAvailableContextFunction = std::function<LLVMContext &()>;
7972

8073
CompileOnDemandLayer2(ExecutionSession &ES, IRLayer &BaseLayer,
8174
JITCompileCallbackManager &CCMgr,
8275
IndirectStubsManagerBuilder BuildIndirectStubsManager,
83-
GetSymbolResolverFunction GetSymbolResolver,
84-
SetSymbolResolverFunction SetSymbolResolver,
8576
GetAvailableContextFunction GetAvailableContext);
8677

8778
Error add(VSO &V, VModuleKey K, std::unique_ptr<Module> M) override;
@@ -96,17 +87,14 @@ class CompileOnDemandLayer2 : public IRLayer {
9687
IndirectStubsManager &getStubsManager(const VSO &V);
9788

9889
void emitExtractedFunctionsModule(MaterializationResponsibility R,
99-
std::unique_ptr<Module> M,
100-
std::shared_ptr<SymbolResolver> Resolver);
90+
std::unique_ptr<Module> M);
10191

10292
mutable std::mutex CODLayerMutex;
10393

10494
IRLayer &BaseLayer;
10595
JITCompileCallbackManager &CCMgr;
10696
IndirectStubsManagerBuilder BuildIndirectStubsManager;
10797
StubManagersMap StubsMgrs;
108-
GetSymbolResolverFunction GetSymbolResolver;
109-
SetSymbolResolverFunction SetSymbolResolver;
11098
GetAvailableContextFunction GetAvailableContext;
11199
};
112100

include/llvm/ExecutionEngine/Orc/Core.h

Lines changed: 43 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,11 @@ using SymbolDependenceMap = std::map<VSO *, SymbolNameSet>;
6666
/// Render a SymbolDependendeMap.
6767
raw_ostream &operator<<(raw_ostream &OS, const SymbolDependenceMap &Deps);
6868

69+
/// A list of VSO pointers.
70+
using VSOList = std::vector<VSO *>;
71+
72+
raw_ostream &operator<<(raw_ostream &OS, const VSOList &VSOs);
73+
6974
/// Used to notify a VSO that the given set of symbols failed to materialize.
7075
class FailedToMaterialize : public ErrorInfo<FailedToMaterialize> {
7176
public:
@@ -471,74 +476,6 @@ class AsynchronousSymbolQuery {
471476
size_t NotYetReadyCount;
472477
};
473478

474-
/// SymbolResolver is a composable interface for looking up symbol flags
475-
/// and addresses using the AsynchronousSymbolQuery type. It will
476-
/// eventually replace the LegacyJITSymbolResolver interface as the
477-
/// stardard ORC symbol resolver type.
478-
///
479-
/// FIXME: SymbolResolvers should go away and be replaced with VSOs with
480-
/// defenition generators.
481-
class SymbolResolver {
482-
public:
483-
virtual ~SymbolResolver() = default;
484-
485-
/// Returns the flags for each symbol in Symbols that can be found,
486-
/// along with the set of symbol that could not be found.
487-
virtual SymbolNameSet lookupFlags(SymbolFlagsMap &Flags,
488-
const SymbolNameSet &Symbols) = 0;
489-
490-
/// For each symbol in Symbols that can be found, assigns that symbols
491-
/// value in Query. Returns the set of symbols that could not be found.
492-
virtual SymbolNameSet lookup(std::shared_ptr<AsynchronousSymbolQuery> Query,
493-
SymbolNameSet Symbols) = 0;
494-
495-
private:
496-
virtual void anchor();
497-
};
498-
499-
/// Implements SymbolResolver with a pair of supplied function objects
500-
/// for convenience. See createSymbolResolver.
501-
template <typename LookupFlagsFn, typename LookupFn>
502-
class LambdaSymbolResolver final : public SymbolResolver {
503-
public:
504-
template <typename LookupFlagsFnRef, typename LookupFnRef>
505-
LambdaSymbolResolver(LookupFlagsFnRef &&LookupFlags, LookupFnRef &&Lookup)
506-
: LookupFlags(std::forward<LookupFlagsFnRef>(LookupFlags)),
507-
Lookup(std::forward<LookupFnRef>(Lookup)) {}
508-
509-
SymbolNameSet lookupFlags(SymbolFlagsMap &Flags,
510-
const SymbolNameSet &Symbols) final {
511-
return LookupFlags(Flags, Symbols);
512-
}
513-
514-
SymbolNameSet lookup(std::shared_ptr<AsynchronousSymbolQuery> Query,
515-
SymbolNameSet Symbols) final {
516-
return Lookup(std::move(Query), std::move(Symbols));
517-
}
518-
519-
private:
520-
LookupFlagsFn LookupFlags;
521-
LookupFn Lookup;
522-
};
523-
524-
/// Creates a SymbolResolver implementation from the pair of supplied
525-
/// function objects.
526-
template <typename LookupFlagsFn, typename LookupFn>
527-
std::unique_ptr<LambdaSymbolResolver<
528-
typename std::remove_cv<
529-
typename std::remove_reference<LookupFlagsFn>::type>::type,
530-
typename std::remove_cv<
531-
typename std::remove_reference<LookupFn>::type>::type>>
532-
createSymbolResolver(LookupFlagsFn &&LookupFlags, LookupFn &&Lookup) {
533-
using LambdaSymbolResolverImpl = LambdaSymbolResolver<
534-
typename std::remove_cv<
535-
typename std::remove_reference<LookupFlagsFn>::type>::type,
536-
typename std::remove_cv<
537-
typename std::remove_reference<LookupFn>::type>::type>;
538-
return llvm::make_unique<LambdaSymbolResolverImpl>(
539-
std::forward<LookupFlagsFn>(LookupFlags), std::forward<LookupFn>(Lookup));
540-
}
541-
542479
/// A symbol table that supports asynchoronous symbol queries.
543480
///
544481
/// Represents a virtual shared object. Instances can not be copied or moved, so
@@ -578,6 +515,42 @@ class VSO {
578515
this->FallbackDefinitionGenerator = std::move(FallbackDefinitionGenerator);
579516
}
580517

518+
/// Set the search order to be used when fixing up definitions in VSO.
519+
/// This will replace the previous search order, and apply to any symbol
520+
/// resolutions made for definitions in this VSO after the call to
521+
/// setSearchOrder (even if the definition itself was added before the
522+
/// call).
523+
///
524+
/// If SearchThisVSOFirst is set, which by default it is, then this VSO will
525+
/// add itself to the beginning of the SearchOrder (Clients should *not*
526+
/// put this VSO in the list in this case, to avoid redundant lookups).
527+
///
528+
/// If SearchThisVSOFirst is false then the search order will be used as
529+
/// given. The main motivation for this feature is to support deliberate
530+
/// shadowing of symbols in this VSO by a facade VSO. For example, the
531+
/// facade may resolve function names to stubs, and the stubs may compile
532+
/// lazily by looking up symbols in this dylib. Adding the facade dylib
533+
/// as the first in the search order (instead of this dylib) ensures that
534+
/// definitions within this dylib resolve to the lazy-compiling stubs,
535+
/// rather than immediately materializing the definitions in this dylib.
536+
void setSearchOrder(VSOList NewSearchOrder, bool SearchThisVSOFirst = true);
537+
538+
/// Add the given VSO to the search order for definitions in this VSO.
539+
void addToSearchOrder(VSO &V);
540+
541+
/// Replace OldV with NewV in the search order if OldV is present. Otherwise
542+
/// this operation is a no-op.
543+
void replaceInSearchOrder(VSO &OldV, VSO &NewV);
544+
545+
/// Remove the given VSO from the search order for this VSO if it is
546+
/// present. Otherwise this operation is a no-op.
547+
void removeFromSearchOrder(VSO &V);
548+
549+
/// Do something with the search order (run under the session lock).
550+
template <typename Func> void withSearchOrderDo(Func F) {
551+
ES.runSessionLocked([&]() { F(SearchOrder); });
552+
}
553+
581554
/// Define all symbols provided by the materialization unit to be part
582555
/// of the given VSO.
583556
template <typename UniquePtrToMaterializationUnit>
@@ -649,8 +622,7 @@ class VSO {
649622
LLVM_MARK_AS_BITMASK_ENUM(NotifyFullyReady)
650623
};
651624

652-
VSO(ExecutionSessionBase &ES, std::string Name)
653-
: ES(ES), VSOName(std::move(Name)) {}
625+
VSO(ExecutionSessionBase &ES, std::string Name);
654626

655627
Error defineImpl(MaterializationUnit &MU);
656628

@@ -692,6 +664,7 @@ class VSO {
692664
UnmaterializedInfosMap UnmaterializedInfos;
693665
MaterializingInfosMap MaterializingInfos;
694666
FallbackDefinitionGeneratorFunction FallbackDefinitionGenerator;
667+
VSOList SearchOrder;
695668

696669
// FIXME: Remove this (and runOutstandingMUs) once the linking layer works
697670
// with callbacks from asynchronous queries.
@@ -729,8 +702,6 @@ Expected<SymbolMap> blockingLookup(ExecutionSessionBase &ES,
729702
SymbolNameSet Names, bool WaiUntilReady,
730703
MaterializationResponsibility *MR = nullptr);
731704

732-
using VSOList = std::vector<VSO *>;
733-
734705
/// Look up the given names in the given VSOs.
735706
/// VSOs will be searched in order and no VSO pointer may be null.
736707
/// All symbols must be found within the given VSOs or an error

include/llvm/ExecutionEngine/Orc/LLJIT.h

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -81,13 +81,10 @@ class LLJIT {
8181
LLJIT(std::unique_ptr<ExecutionSession> ES, std::unique_ptr<TargetMachine> TM,
8282
DataLayout DL);
8383

84-
std::shared_ptr<SymbolResolver> takeSymbolResolver(VModuleKey K);
85-
RTDyldObjectLinkingLayer2::Resources getRTDyldResources(VModuleKey K);
84+
std::shared_ptr<RuntimeDyld::MemoryManager> getMemoryManager(VModuleKey K);
8685

8786
std::string mangle(StringRef UnmangledName);
8887

89-
std::unique_ptr<SymbolResolver> createResolverFor(VSO &V);
90-
9188
Error applyDataLayout(Module &M);
9289

9390
void recordCtorDtors(Module &M);
@@ -98,12 +95,9 @@ class LLJIT {
9895
std::unique_ptr<TargetMachine> TM;
9996
DataLayout DL;
10097

101-
std::map<VSO *, VSOList> VSOLookupOrder;
102-
10398
RTDyldObjectLinkingLayer2 ObjLinkingLayer;
10499
IRCompileLayer2 CompileLayer;
105100

106-
std::map<VModuleKey, std::shared_ptr<orc::SymbolResolver>> Resolvers;
107101
CtorDtorRunner2 CtorRunner, DtorRunner;
108102
};
109103

@@ -136,10 +130,6 @@ class LLLazyJIT : public LLJIT {
136130
std::unique_ptr<JITCompileCallbackManager> CCMgr,
137131
std::function<std::unique_ptr<IndirectStubsManager>()> ISMBuilder);
138132

139-
std::shared_ptr<SymbolResolver> getSymbolResolver(VModuleKey K);
140-
141-
void setSymbolResolver(VModuleKey K, std::shared_ptr<SymbolResolver> R);
142-
143133
std::unique_ptr<JITCompileCallbackManager> CCMgr;
144134
std::function<std::unique_ptr<IndirectStubsManager>()> ISMBuilder;
145135

include/llvm/ExecutionEngine/Orc/Legacy.h

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,74 @@
2020
namespace llvm {
2121
namespace orc {
2222

23+
/// SymbolResolver is a composable interface for looking up symbol flags
24+
/// and addresses using the AsynchronousSymbolQuery type. It will
25+
/// eventually replace the LegacyJITSymbolResolver interface as the
26+
/// stardard ORC symbol resolver type.
27+
///
28+
/// FIXME: SymbolResolvers should go away and be replaced with VSOs with
29+
/// defenition generators.
30+
class SymbolResolver {
31+
public:
32+
virtual ~SymbolResolver() = default;
33+
34+
/// Returns the flags for each symbol in Symbols that can be found,
35+
/// along with the set of symbol that could not be found.
36+
virtual SymbolNameSet lookupFlags(SymbolFlagsMap &Flags,
37+
const SymbolNameSet &Symbols) = 0;
38+
39+
/// For each symbol in Symbols that can be found, assigns that symbols
40+
/// value in Query. Returns the set of symbols that could not be found.
41+
virtual SymbolNameSet lookup(std::shared_ptr<AsynchronousSymbolQuery> Query,
42+
SymbolNameSet Symbols) = 0;
43+
44+
private:
45+
virtual void anchor();
46+
};
47+
48+
/// Implements SymbolResolver with a pair of supplied function objects
49+
/// for convenience. See createSymbolResolver.
50+
template <typename LookupFlagsFn, typename LookupFn>
51+
class LambdaSymbolResolver final : public SymbolResolver {
52+
public:
53+
template <typename LookupFlagsFnRef, typename LookupFnRef>
54+
LambdaSymbolResolver(LookupFlagsFnRef &&LookupFlags, LookupFnRef &&Lookup)
55+
: LookupFlags(std::forward<LookupFlagsFnRef>(LookupFlags)),
56+
Lookup(std::forward<LookupFnRef>(Lookup)) {}
57+
58+
SymbolNameSet lookupFlags(SymbolFlagsMap &Flags,
59+
const SymbolNameSet &Symbols) final {
60+
return LookupFlags(Flags, Symbols);
61+
}
62+
63+
SymbolNameSet lookup(std::shared_ptr<AsynchronousSymbolQuery> Query,
64+
SymbolNameSet Symbols) final {
65+
return Lookup(std::move(Query), std::move(Symbols));
66+
}
67+
68+
private:
69+
LookupFlagsFn LookupFlags;
70+
LookupFn Lookup;
71+
};
72+
73+
/// Creates a SymbolResolver implementation from the pair of supplied
74+
/// function objects.
75+
template <typename LookupFlagsFn, typename LookupFn>
76+
std::unique_ptr<LambdaSymbolResolver<
77+
typename std::remove_cv<
78+
typename std::remove_reference<LookupFlagsFn>::type>::type,
79+
typename std::remove_cv<
80+
typename std::remove_reference<LookupFn>::type>::type>>
81+
createSymbolResolver(LookupFlagsFn &&LookupFlags, LookupFn &&Lookup) {
82+
using LambdaSymbolResolverImpl = LambdaSymbolResolver<
83+
typename std::remove_cv<
84+
typename std::remove_reference<LookupFlagsFn>::type>::type,
85+
typename std::remove_cv<
86+
typename std::remove_reference<LookupFn>::type>::type>;
87+
return llvm::make_unique<LambdaSymbolResolverImpl>(
88+
std::forward<LookupFlagsFn>(LookupFlags), std::forward<LookupFn>(Lookup));
89+
}
90+
2391
class JITSymbolResolverAdapter : public JITSymbolResolver {
2492
public:
2593
JITSymbolResolverAdapter(ExecutionSession &ES, SymbolResolver &R,

include/llvm/ExecutionEngine/Orc/NullResolver.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
#ifndef LLVM_EXECUTIONENGINE_ORC_NULLRESOLVER_H
1616
#define LLVM_EXECUTIONENGINE_ORC_NULLRESOLVER_H
1717

18-
#include "llvm/ExecutionEngine/Orc/Core.h"
18+
#include "llvm/ExecutionEngine/Orc/Legacy.h"
1919
#include "llvm/ExecutionEngine/RuntimeDyld.h"
2020

2121
namespace llvm {

include/llvm/ExecutionEngine/Orc/RTDyldObjectLinkingLayer.h

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,17 +46,13 @@ class RTDyldObjectLinkingLayer2 : public ObjectLayer {
4646
/// Functor for receiving finalization notifications.
4747
using NotifyFinalizedFunction = std::function<void(VModuleKey)>;
4848

49-
struct Resources {
50-
std::shared_ptr<RuntimeDyld::MemoryManager> MemMgr;
51-
std::shared_ptr<SymbolResolver> Resolver;
52-
};
53-
54-
using ResourcesGetterFunction = std::function<Resources(VModuleKey)>;
49+
using GetMemoryManagerFunction =
50+
std::function<std::shared_ptr<RuntimeDyld::MemoryManager>(VModuleKey)>;
5551

5652
/// Construct an ObjectLinkingLayer with the given NotifyLoaded,
5753
/// and NotifyFinalized functors.
5854
RTDyldObjectLinkingLayer2(
59-
ExecutionSession &ES, ResourcesGetterFunction GetResources,
55+
ExecutionSession &ES, GetMemoryManagerFunction GetMemoryManager,
6056
NotifyLoadedFunction NotifyLoaded = NotifyLoadedFunction(),
6157
NotifyFinalizedFunction NotifyFinalized = NotifyFinalizedFunction());
6258

@@ -81,7 +77,7 @@ class RTDyldObjectLinkingLayer2 : public ObjectLayer {
8177

8278
private:
8379
mutable std::mutex RTDyldLayerMutex;
84-
ResourcesGetterFunction GetResources;
80+
GetMemoryManagerFunction GetMemoryManager;
8581
NotifyLoadedFunction NotifyLoaded;
8682
NotifyFinalizedFunction NotifyFinalized;
8783
bool ProcessAllSections;

0 commit comments

Comments
 (0)