16 #define DEBUG_TYPE "orc" 29 std::unique_ptr<MemoryBuffer> ObjBuffer)
30 : Layer(Layer), MR(
std::move(MR)), ObjBuffer(
std::move(ObjBuffer)) {}
35 if (Layer.ReturnObjectBuffer)
36 Layer.ReturnObjectBuffer(std::move(ObjBuffer));
42 return ObjBuffer->getMemBufferRef();
46 Layer.getExecutionSession().reportError(std::move(Err));
47 MR.failMaterialization();
51 std::unique_ptr<JITLinkAsyncLookupContinuation> LC)
override {
54 MR.getTargetJITDylib().withSearchOrderDo(
57 auto &ES = Layer.getExecutionSession();
60 for (
auto &KV : Symbols) {
70 LookupSet.
add(ES.intern(KV.first), LookupFlags);
74 auto OnResolve = [
this, LookupContinuation = std::move(LC)](
76 auto Main = Layer.getExecutionSession().intern(
"_main");
78 LookupContinuation->run(Result.takeError());
81 for (
auto &KV : *Result)
82 LR[*KV.first] = KV.second;
83 LookupContinuation->run(std::move(LR));
88 SymbolState::Resolved, std::move(OnResolve),
90 registerDependencies(Deps);
95 auto &ES = Layer.getExecutionSession();
98 bool AutoClaim = Layer.AutoClaimObjectSymbols;
102 if (Sym->hasName() && Sym->getScope() !=
Scope::Local) {
103 auto InternedName = ES.intern(Sym->getName());
106 if (Sym->isCallable())
111 InternedResult[InternedName] =
113 if (AutoClaim && !MR.getSymbols().count(InternedName)) {
115 "Duplicate symbol to claim?");
116 ExtraSymbolsToClaim[InternedName] = Flags;
121 if (Sym->hasName()) {
122 auto InternedName = ES.intern(Sym->getName());
125 if (Sym->isCallable())
129 InternedResult[InternedName] =
131 if (AutoClaim && !MR.getSymbols().count(InternedName)) {
133 "Duplicate symbol to claim?");
134 ExtraSymbolsToClaim[InternedName] = Flags;
138 if (!ExtraSymbolsToClaim.
empty())
139 if (
auto Err = MR.defineMaterializing(ExtraSymbolsToClaim))
140 return notifyFailed(std::move(Err));
141 if (
auto Err = MR.notifyResolved(InternedResult)) {
142 Layer.getExecutionSession().reportError(std::move(Err));
143 MR.failMaterialization();
146 Layer.notifyLoaded(MR);
150 std::unique_ptr<JITLinkMemoryManager::Allocation> A)
override {
151 if (
auto Err = Layer.notifyEmitted(MR, std::move(A))) {
152 Layer.getExecutionSession().reportError(std::move(Err));
153 MR.failMaterialization();
156 if (
auto Err = MR.notifyEmitted()) {
157 Layer.getExecutionSession().reportError(std::move(Err));
158 MR.failMaterialization();
163 return [
this](
LinkGraph &
G) {
return markResponsibilitySymbolsLive(
G); };
170 [
this](
LinkGraph &
G) {
return externalizeWeakAndCommonSymbols(G); });
172 [
this](
LinkGraph &G) {
return computeNamedSymbolDependencies(G); });
174 Layer.modifyPassConfig(MR, TT, Config);
183 auto &ES = Layer.getExecutionSession();
186 if (!MR.getSymbols().count(ES.intern(Sym->getName())))
192 if (!MR.getSymbols().count(ES.intern(Sym->getName())))
200 auto &ES = Layer.getExecutionSession();
202 if (Sym->hasName() && MR.getSymbols().count(ES.intern(Sym->getName())))
208 auto &ES = MR.getTargetJITDylib().getExecutionSession();
209 auto AnonDeps = computeAnonDeps(G);
218 auto SymName = ES.intern(Sym->getName());
221 for (
auto &
E : Sym->getBlock().edges()) {
222 auto &TargetSym =
E.getTarget();
225 SymDeps.
insert(ES.intern(TargetSym.getName()));
227 assert(TargetSym.isDefined() &&
228 "Anonymous/local symbols must be defined");
229 auto I = AnonDeps.find(&TargetSym);
230 if (I != AnonDeps.end())
231 for (
auto &S : I->second)
242 auto &ES = MR.getTargetJITDylib().getExecutionSession();
249 struct WorklistEntry {
251 : Sym(Sym), SymAnonDeps(std::move(SymAnonDeps)) {}
256 std::vector<WorklistEntry> Worklist;
258 if (!Sym->hasName()) {
259 auto &SymNamedDeps = DepMap[Sym];
262 for (
auto &
E : Sym->getBlock().edges()) {
263 auto &TargetSym =
E.getTarget();
264 if (TargetSym.hasName())
265 SymNamedDeps.insert(ES.intern(TargetSym.getName()));
267 assert(TargetSym.isDefined() &&
268 "Anonymous symbols must be defined");
269 SymAnonDeps.
insert(&TargetSym);
273 if (!SymAnonDeps.
empty())
274 Worklist.push_back(WorklistEntry(Sym, std::move(SymAnonDeps)));
283 for (
auto &WLEntry : Worklist) {
284 auto *Sym = WLEntry.Sym;
285 auto &SymNamedDeps = DepMap[Sym];
286 auto &SymAnonDeps = WLEntry.SymAnonDeps;
288 for (
auto *TargetSym : SymAnonDeps) {
289 auto I = DepMap.
find(TargetSym);
290 if (
I != DepMap.
end())
291 for (
const auto &S :
I->second)
292 Changed |= SymNamedDeps.insert(S).second;
301 for (
auto &NamedDepsEntry : NamedSymbolDeps) {
302 auto &
Name = NamedDepsEntry.first;
303 auto &NameDeps = NamedDepsEntry.second;
306 for (
const auto &QueryDepsEntry : QueryDeps) {
307 JITDylib &SourceJD = *QueryDepsEntry.first;
309 auto &DepsForJD = SymbolDeps[&SourceJD];
311 for (
const auto &S : Symbols)
312 if (NameDeps.count(S))
315 if (DepsForJD.empty())
316 SymbolDeps.
erase(&SourceJD);
319 MR.addDependencies(
Name, SymbolDeps);
325 std::unique_ptr<MemoryBuffer> ObjBuffer;
329 ObjectLinkingLayer::Plugin::~Plugin() {}
331 ObjectLinkingLayer::ObjectLinkingLayer(
336 if (
auto Err = removeAllModules())
341 std::unique_ptr<MemoryBuffer>
O) {
342 assert(O &&
"Object must not be null");
343 jitLink(std::make_unique<ObjectLinkingLayerJITLinkContext>(
344 *
this, std::move(R), std::move(O)));
350 for (
auto &
P : Plugins)
351 P->modifyPassConfig(MR, TT, PassConfig);
355 for (
auto &
P : Plugins)
362 for (
auto &
P : Plugins)
363 Err =
joinErrors(std::move(Err),
P->notifyEmitted(MR));
369 std::lock_guard<std::mutex>
Lock(LayerMutex);
370 UntrackedAllocs.push_back(std::move(Alloc));
379 for (
auto &
P : Plugins)
380 Err =
joinErrors(std::move(Err),
P->notifyRemovingModule(K));
385 std::lock_guard<std::mutex>
Lock(LayerMutex);
386 auto AllocItr = TrackedAllocs.
find(K);
387 Alloc = std::move(AllocItr->second);
388 TrackedAllocs.
erase(AllocItr);
391 assert(Alloc &&
"No allocation for key K");
393 return joinErrors(std::move(Err), Alloc->deallocate());
396 Error ObjectLinkingLayer::removeAllModules() {
400 for (
auto &
P : Plugins)
401 Err =
joinErrors(std::move(Err),
P->notifyRemovingAllModules());
403 std::vector<AllocPtr> Allocs;
405 std::lock_guard<std::mutex>
Lock(LayerMutex);
406 Allocs = std::move(UntrackedAllocs);
408 for (
auto &KV : TrackedAllocs)
409 Allocs.push_back(std::move(KV.second));
411 TrackedAllocs.clear();
414 while (!Allocs.empty()) {
415 Err =
joinErrors(std::move(Err), Allocs.back()->deallocate());
424 : Registrar(Registrar) {}
429 assert(!InProcessLinks.count(&MR) &&
"Link for MR already being tracked?");
435 InProcessLinks[&MR] = { Addr, Size };
442 auto EHFrameRangeItr = InProcessLinks.find(&MR);
443 if (EHFrameRangeItr == InProcessLinks.end())
446 auto EHFrameRange = EHFrameRangeItr->second;
447 assert(EHFrameRange.Addr &&
448 "eh-frame addr to register can not be null");
450 InProcessLinks.erase(EHFrameRangeItr);
452 TrackedEHFrameRanges[
Key] = EHFrameRange;
454 UntrackedEHFrameRanges.push_back(EHFrameRange);
460 auto EHFrameRangeItr = TrackedEHFrameRanges.
find(K);
461 if (EHFrameRangeItr == TrackedEHFrameRanges.
end())
464 auto EHFrameRange = EHFrameRangeItr->second;
465 assert(EHFrameRange.Addr &&
"Tracked eh-frame range must not be null");
467 TrackedEHFrameRanges.
erase(EHFrameRangeItr);
474 std::vector<EHFrameRange> EHFrameRanges =
475 std::move(UntrackedEHFrameRanges);
476 EHFrameRanges.reserve(EHFrameRanges.size() + TrackedEHFrameRanges.
size());
478 for (
auto &KV : TrackedEHFrameRanges)
479 EHFrameRanges.push_back(KV.second);
481 TrackedEHFrameRanges.clear();
485 while (!EHFrameRanges.empty()) {
486 auto EHFrameRange = EHFrameRanges.back();
487 assert(EHFrameRange.Addr &&
"Untracked eh-frame range must not be null");
488 EHFrameRanges.pop_back();
~ObjectLinkingLayerJITLinkContext()
void notifyResolved(LinkGraph &G) override
Called by JITLink once all defined symbols in the graph have been assigned their final memory locatio...
This class represents lattice values for constants.
amdgpu Simplify well known AMD library false FunctionCallee Value const Twine & Name
std::vector< std::pair< JITDylib *, JITDylibLookupFlags > > JITDylibSearchOrder
A list of (JITDylib*, JITDylibLookupFlags) pairs to be used as a search order during symbol lookup...
void makeExternal(Symbol &Sym)
Turn a defined symbol into an external one.
void jitLink(std::unique_ptr< JITLinkContext > Ctx)
Basic JITLink implementation.
EHFrameRegistrationPlugin(jitlink::EHFrameRegistrar &Registrar)
void notifyFailed(Error Err) override
Notify this context that linking failed.
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
virtual Error deregisterEHFrames(JITTargetAddress EHFrameSectionAddr, size_t EHFrameSectionSize)=0
An ObjectLayer implementation built on JITLink.
Tagged union holding either a T or a Error.
void modifyPassConfig(MaterializationResponsibility &MR, const Triple &TT, jitlink::PassConfiguration &PassConfig) override
Tracks responsibility for materialization, and mediates interactions between MaterializationUnits and...
Manages allocations of JIT memory.
std::function< Error(LinkGraph &)> LinkGraphPassFunction
A function for mutating LinkGraphs.
ExecutionSession & getExecutionSession()
Returns the execution session for this layer.
SymbolLookupFlags
Lookup flags that apply to each symbol in a lookup.
uint64_t JITTargetAddress
Represents an address in the target process's address space.
Supports registration/deregistration of EH-frames in a target process.
Error notifyRemovingAllModules() override
iterator find(const_arg_type_t< KeyT > Val)
LinkGraphPassList PostFixupPasses
Post-fixup passes.
bool erase(const KeyT &Val)
LinkGraphPassFunction createEHFrameRecorderPass(const Triple &TT, StoreFrameRangeFunction StoreFrameRange)
Creates a pass that records the address and size of the EH frame section.
VModuleKey getVModuleKey() const
Returns the VModuleKey for this instance.
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
std::pair< iterator, bool > insert(const ValueT &V)
Flags for symbols in the JIT.
void add(SymbolStringPtr Name, SymbolLookupFlags Flags=SymbolLookupFlags::RequiredSymbol)
Add an element to the set.
LinkGraphPassList PostPrunePasses
Post-prune passes.
Error notifyRemovingModule(VModuleKey K) override
LinkGraphPassFunction getMarkLivePass(const Triple &TT) const override
Returns the mark-live pass to be used for this link.
~ObjectLinkingLayer()
Destruct an ObjectLinkingLayer.
Triple - Helper class for working with autoconf configuration names.
LinkGraphPassList PrePrunePasses
Pre-prune passes.
iterator_range< external_symbol_iterator > absolute_symbols()
static ErrorSuccess success()
Create a success value.
virtual Error registerEHFrames(JITTargetAddress EHFrameSectionAddr, size_t EHFrameSectionSize)=0
void notifyFinalized(std::unique_ptr< JITLinkMemoryManager::Allocation > A) override
Called by JITLink to notify the context that the object has been finalized (i.e.
A set of symbols to look up, each associated with a SymbolLookupFlags value.
MemoryBufferRef getObjectBuffer() const override
Returns a StringRef for the object buffer.
Error notifyEmitted(MaterializationResponsibility &MR) override
void lookup(const LookupMap &Symbols, std::unique_ptr< JITLinkAsyncLookupContinuation > LC) override
Called by JITLink to resolve external symbols.
An ExecutionSession represents a running JIT program.
ObjectLinkingLayerJITLinkContext(ObjectLinkingLayer &Layer, MaterializationResponsibility MR, std::unique_ptr< MemoryBuffer > ObjBuffer)
Represents a symbol that has been evaluated to an address already.
uint8_t uint64_t VModuleKey
VModuleKey provides a unique identifier (allocated and managed by ExecutionSessions) for a module add...
void reportError(Error Err)
Report a error for this execution session.
Error modifyPassConfig(const Triple &TT, PassConfiguration &Config) override
Called by JITLink to modify the pass pipeline prior to linking.
Holds context for a single jitLink invocation.
Error joinErrors(Error E1, Error E2)
Concatenate errors.
void emit(MaterializationResponsibility R, std::unique_ptr< MemoryBuffer > O) override
Emit the object.
size_type count(const_arg_type_t< KeyT > Val) const
Return 1 if the specified key is in the map, 0 otherwise.
LLVM_NODISCARD bool empty() const
ValueT lookup(const_arg_type_t< KeyT > Val) const
lookup - Return the entry for the specified key, or a default constructed value if no such entry exis...
An LinkGraph pass configuration, consisting of a list of pre-prune, post-prune, and post-fixup passes...
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
Lightweight error class with error context and mandatory checking.
Interface for Layers that accept object files.
JITLinkMemoryManager & getMemoryManager() override
Return the MemoryManager to be used for this link.
iterator_range< defined_symbol_iterator > defined_symbols()
A symbol table that supports asynchoronous symbol queries.