1//===------ LinkGraphLinkingLayer.cpp - Link LinkGraphs with JITLink ------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "llvm/ExecutionEngine/Orc/LinkGraphLinkingLayer.h"
10
11#include "llvm/ADT/SCCIterator.h"
12#include "llvm/ExecutionEngine/JITLink/aarch32.h"
13#include "llvm/ExecutionEngine/Orc/DebugUtils.h"
14#include "llvm/Support/MemoryBuffer.h"
15
16#define DEBUG_TYPE "orc"
17
18using namespace llvm;
19using namespace llvm::jitlink;
20using namespace llvm::orc;
21
22namespace llvm {
23
24struct BlockDepInfo;
25
26using BlockDepInfoMap = DenseMap<jitlink::Block *, BlockDepInfo>;
27
28struct BlockDepInfo {
29 using SymbolDefList = SmallVector<jitlink::Symbol *>;
30 using SymbolDepSet = DenseSet<jitlink::Symbol *>;
31 using AnonBlockDepSet = DenseSet<jitlink::Block *>;
32
33 BlockDepInfoMap *Graph = nullptr;
34 SymbolDefList SymbolDefs;
35 SymbolDepSet SymbolDeps;
36 AnonBlockDepSet AnonBlockDeps;
37 BlockDepInfo *SCCRoot = nullptr;
38 std::optional<size_t> DepGroupIndex;
39};
40
41template <> struct GraphTraits<BlockDepInfo *> {
42 using NodeRef = BlockDepInfo *;
43
44 class ChildIteratorType {
45 using impl_iterator = BlockDepInfo::AnonBlockDepSet::iterator;
46
47 public:
48 ChildIteratorType(NodeRef Parent, impl_iterator I)
49 : Parent(Parent), I(std::move(I)) {}
50
51 friend bool operator==(const ChildIteratorType &LHS,
52 const ChildIteratorType &RHS) {
53 return LHS.I == RHS.I;
54 }
55 friend bool operator!=(const ChildIteratorType &LHS,
56 const ChildIteratorType &RHS) {
57 return LHS.I != RHS.I;
58 }
59
60 ChildIteratorType &operator++() {
61 ++I;
62 return *this;
63 }
64 ChildIteratorType operator++(int) {
65 auto Tmp = *this;
66 ++I;
67 return Tmp;
68 }
69 NodeRef operator*() {
70 assert(Parent->Graph && "No pointer to BlockDepInfoMap");
71 return &(*Parent->Graph)[*I];
72 }
73
74 private:
75 NodeRef Parent;
76 BlockDepInfo::AnonBlockDepSet::iterator I;
77 };
78
79 static NodeRef getEntryNode(NodeRef N) { return N; }
80
81 static ChildIteratorType child_begin(NodeRef N) {
82 return ChildIteratorType(N, N->AnonBlockDeps.begin());
83 }
84 static ChildIteratorType child_end(NodeRef N) {
85 return ChildIteratorType(N, N->AnonBlockDeps.end());
86 }
87};
88
89} // namespace llvm
90
91namespace {
92
93ExecutorAddr getJITSymbolPtrForSymbol(Symbol &Sym, const Triple &TT) {
94 switch (TT.getArch()) {
95 case Triple::arm:
96 case Triple::armeb:
97 case Triple::thumb:
98 case Triple::thumbeb:
99 if (hasTargetFlags(Sym, Flags: aarch32::ThumbSymbol)) {
100 // Set LSB to indicate thumb target
101 assert(Sym.isCallable() && "Only callable symbols can have thumb flag");
102 assert((Sym.getAddress().getValue() & 0x01) == 0 && "LSB is clear");
103 return Sym.getAddress() + 0x01;
104 }
105 return Sym.getAddress();
106 default:
107 return Sym.getAddress();
108 }
109}
110
111} // end anonymous namespace
112
113namespace llvm {
114namespace orc {
115
116class LinkGraphLinkingLayer::JITLinkCtx final : public JITLinkContext {
117public:
118 JITLinkCtx(LinkGraphLinkingLayer &Layer,
119 std::unique_ptr<MaterializationResponsibility> MR,
120 std::unique_ptr<MemoryBuffer> ObjBuffer)
121 : JITLinkContext(&MR->getTargetJITDylib()), Layer(Layer),
122 MR(std::move(MR)), ObjBuffer(std::move(ObjBuffer)) {
123 std::lock_guard<std::mutex> Lock(Layer.LayerMutex);
124 Plugins = Layer.Plugins;
125 }
126
127 ~JITLinkCtx() override {
128 // If there is an object buffer return function then use it to
129 // return ownership of the buffer.
130 if (Layer.ReturnObjectBuffer && ObjBuffer)
131 Layer.ReturnObjectBuffer(std::move(ObjBuffer));
132 }
133
134 JITLinkMemoryManager &getMemoryManager() override { return Layer.MemMgr; }
135
136 void notifyMaterializing(LinkGraph &G) {
137 for (auto &P : Plugins)
138 P->notifyMaterializing(MR&: *MR, G, Ctx&: *this,
139 InputObject: ObjBuffer ? ObjBuffer->getMemBufferRef()
140 : MemoryBufferRef());
141 }
142
143 void notifyFailed(Error Err) override {
144 for (auto &P : Plugins)
145 Err = joinErrors(E1: std::move(Err), E2: P->notifyFailed(MR&: *MR));
146 Layer.getExecutionSession().reportError(Err: std::move(Err));
147 MR->failMaterialization();
148 }
149
150 void lookup(const LookupMap &Symbols,
151 std::unique_ptr<JITLinkAsyncLookupContinuation> LC) override {
152
153 JITDylibSearchOrder LinkOrder;
154 MR->getTargetJITDylib().withLinkOrderDo(
155 F: [&](const JITDylibSearchOrder &LO) { LinkOrder = LO; });
156
157 auto &ES = Layer.getExecutionSession();
158
159 SymbolLookupSet LookupSet;
160 for (auto &KV : Symbols) {
161 orc::SymbolLookupFlags LookupFlags;
162 switch (KV.second) {
163 case jitlink::SymbolLookupFlags::RequiredSymbol:
164 LookupFlags = orc::SymbolLookupFlags::RequiredSymbol;
165 break;
166 case jitlink::SymbolLookupFlags::WeaklyReferencedSymbol:
167 LookupFlags = orc::SymbolLookupFlags::WeaklyReferencedSymbol;
168 break;
169 }
170 LookupSet.add(Name: KV.first, Flags: LookupFlags);
171 }
172
173 // OnResolve -- De-intern the symbols and pass the result to the linker.
174 auto OnResolve = [LookupContinuation =
175 std::move(LC)](Expected<SymbolMap> Result) mutable {
176 if (!Result)
177 LookupContinuation->run(LR: Result.takeError());
178 else {
179 AsyncLookupResult LR;
180 LR.insert_range(R&: *Result);
181 LookupContinuation->run(LR: std::move(LR));
182 }
183 };
184
185 ES.lookup(K: LookupKind::Static, SearchOrder: LinkOrder, Symbols: std::move(LookupSet),
186 RequiredState: SymbolState::Resolved, NotifyComplete: std::move(OnResolve),
187 RegisterDependencies: [this](const SymbolDependenceMap &Deps) {
188 // Translate LookupDeps map to SymbolSourceJD.
189 for (auto &[DepJD, Deps] : Deps)
190 for (auto &DepSym : Deps)
191 SymbolSourceJDs[NonOwningSymbolStringPtr(DepSym)] = DepJD;
192 });
193 }
194
195 Error notifyResolved(LinkGraph &G) override {
196
197 SymbolFlagsMap ExtraSymbolsToClaim;
198 bool AutoClaim = Layer.AutoClaimObjectSymbols;
199
200 SymbolMap InternedResult;
201 for (auto *Sym : G.defined_symbols())
202 if (Sym->getScope() < Scope::SideEffectsOnly) {
203 auto Ptr = getJITSymbolPtrForSymbol(Sym&: *Sym, TT: G.getTargetTriple());
204 auto Flags = getJITSymbolFlagsForSymbol(Sym&: *Sym);
205 InternedResult[Sym->getName()] = {Ptr, Flags};
206 if (AutoClaim && !MR->getSymbols().count(Val: Sym->getName())) {
207 assert(!ExtraSymbolsToClaim.count(Sym->getName()) &&
208 "Duplicate symbol to claim?");
209 ExtraSymbolsToClaim[Sym->getName()] = Flags;
210 }
211 }
212
213 for (auto *Sym : G.absolute_symbols())
214 if (Sym->getScope() < Scope::SideEffectsOnly) {
215 auto Ptr = getJITSymbolPtrForSymbol(Sym&: *Sym, TT: G.getTargetTriple());
216 auto Flags = getJITSymbolFlagsForSymbol(Sym&: *Sym);
217 InternedResult[Sym->getName()] = {Ptr, Flags};
218 if (AutoClaim && !MR->getSymbols().count(Val: Sym->getName())) {
219 assert(!ExtraSymbolsToClaim.count(Sym->getName()) &&
220 "Duplicate symbol to claim?");
221 ExtraSymbolsToClaim[Sym->getName()] = Flags;
222 }
223 }
224
225 if (!ExtraSymbolsToClaim.empty())
226 if (auto Err = MR->defineMaterializing(SymbolFlags: ExtraSymbolsToClaim))
227 return Err;
228
229 {
230
231 // Check that InternedResult matches up with MR->getSymbols(), overriding
232 // flags if requested.
233 // This guards against faulty transformations / compilers / object caches.
234
235 // First check that there aren't any missing symbols.
236 size_t NumMaterializationSideEffectsOnlySymbols = 0;
237 SymbolNameVector MissingSymbols;
238 for (auto &[Sym, Flags] : MR->getSymbols()) {
239
240 auto I = InternedResult.find(Val: Sym);
241
242 // If this is a materialization-side-effects only symbol then bump
243 // the counter and remove in from the result, otherwise make sure that
244 // it's defined.
245 if (Flags.hasMaterializationSideEffectsOnly())
246 ++NumMaterializationSideEffectsOnlySymbols;
247 else if (I == InternedResult.end())
248 MissingSymbols.push_back(x: Sym);
249 else if (Layer.OverrideObjectFlags)
250 I->second.setFlags(Flags);
251 }
252
253 // If there were missing symbols then report the error.
254 if (!MissingSymbols.empty())
255 return make_error<MissingSymbolDefinitions>(
256 Args: Layer.getExecutionSession().getSymbolStringPool(), Args: G.getName(),
257 Args: std::move(MissingSymbols));
258
259 // If there are more definitions than expected, add them to the
260 // ExtraSymbols vector.
261 SymbolNameVector ExtraSymbols;
262 if (InternedResult.size() >
263 MR->getSymbols().size() - NumMaterializationSideEffectsOnlySymbols) {
264 for (auto &KV : InternedResult)
265 if (!MR->getSymbols().count(Val: KV.first))
266 ExtraSymbols.push_back(x: KV.first);
267 }
268
269 // If there were extra definitions then report the error.
270 if (!ExtraSymbols.empty())
271 return make_error<UnexpectedSymbolDefinitions>(
272 Args: Layer.getExecutionSession().getSymbolStringPool(), Args: G.getName(),
273 Args: std::move(ExtraSymbols));
274 }
275
276 if (auto Err = MR->notifyResolved(Symbols: InternedResult))
277 return Err;
278
279 return Error::success();
280 }
281
282 void notifyFinalized(JITLinkMemoryManager::FinalizedAlloc A) override {
283 if (auto Err = notifyEmitted(FA: std::move(A))) {
284 Layer.getExecutionSession().reportError(Err: std::move(Err));
285 MR->failMaterialization();
286 return;
287 }
288
289 if (auto Err = MR->notifyEmitted(EmittedDeps: SymbolDepGroups)) {
290 Layer.getExecutionSession().reportError(Err: std::move(Err));
291 MR->failMaterialization();
292 }
293 }
294
295 LinkGraphPassFunction getMarkLivePass(const Triple &TT) const override {
296 return [this](LinkGraph &G) { return markResponsibilitySymbolsLive(G); };
297 }
298
299 Error modifyPassConfig(LinkGraph &LG, PassConfiguration &Config) override {
300 // Add passes to mark duplicate defs as should-discard, and to walk the
301 // link graph to build the symbol dependence graph.
302 Config.PrePrunePasses.push_back(x: [this](LinkGraph &G) {
303 return claimOrExternalizeWeakAndCommonSymbols(G);
304 });
305
306 for (auto &P : Plugins)
307 P->modifyPassConfig(MR&: *MR, G&: LG, Config);
308
309 Config.PreFixupPasses.push_back(
310 x: [this](LinkGraph &G) { return registerDependencies(G); });
311
312 return Error::success();
313 }
314
315 Error notifyEmitted(jitlink::JITLinkMemoryManager::FinalizedAlloc FA) {
316 Error Err = Error::success();
317 for (auto &P : Plugins)
318 Err = joinErrors(E1: std::move(Err), E2: P->notifyEmitted(MR&: *MR));
319
320 if (Err) {
321 if (FA)
322 Err =
323 joinErrors(E1: std::move(Err), E2: Layer.MemMgr.deallocate(Alloc: std::move(FA)));
324 return Err;
325 }
326
327 if (FA)
328 return Layer.recordFinalizedAlloc(MR&: *MR, FA: std::move(FA));
329
330 return Error::success();
331 }
332
333private:
334 Error claimOrExternalizeWeakAndCommonSymbols(LinkGraph &G) {
335 SymbolFlagsMap NewSymbolsToClaim;
336 std::vector<std::pair<SymbolStringPtr, Symbol *>> NameToSym;
337
338 auto ProcessSymbol = [&](Symbol *Sym) {
339 if (Sym->hasName() && Sym->getLinkage() == Linkage::Weak &&
340 Sym->getScope() != Scope::Local) {
341 if (!MR->getSymbols().count(Val: Sym->getName())) {
342 NewSymbolsToClaim[Sym->getName()] =
343 getJITSymbolFlagsForSymbol(Sym&: *Sym) | JITSymbolFlags::Weak;
344 NameToSym.push_back(x: std::make_pair(x: Sym->getName(), y&: Sym));
345 }
346 }
347 };
348
349 for (auto *Sym : G.defined_symbols())
350 ProcessSymbol(Sym);
351 for (auto *Sym : G.absolute_symbols())
352 ProcessSymbol(Sym);
353
354 // Attempt to claim all weak defs that we're not already responsible for.
355 // This may fail if the resource tracker has become defunct, but should
356 // always succeed otherwise.
357 if (auto Err = MR->defineMaterializing(SymbolFlags: std::move(NewSymbolsToClaim)))
358 return Err;
359
360 // Walk the list of symbols that we just tried to claim. Symbols that we're
361 // responsible for are marked live. Symbols that we're not responsible for
362 // are turned into external references.
363 for (auto &KV : NameToSym) {
364 if (MR->getSymbols().count(Val: KV.first))
365 KV.second->setLive(true);
366 else
367 G.makeExternal(Sym&: *KV.second);
368 }
369
370 return Error::success();
371 }
372
373 Error markResponsibilitySymbolsLive(LinkGraph &G) const {
374 for (auto *Sym : G.defined_symbols())
375 if (Sym->hasName() && MR->getSymbols().count(Val: Sym->getName()))
376 Sym->setLive(true);
377 return Error::success();
378 }
379
380 Error registerDependencies(LinkGraph &G) {
381 auto &TargetJD = MR->getTargetJITDylib();
382 for (auto &[Defs, Deps] : calculateDepGroups(G)) {
383 SymbolDepGroups.push_back(x: SymbolDependenceGroup());
384 auto &SDG = SymbolDepGroups.back();
385 for (auto *Def : Defs)
386 SDG.Symbols.insert(V: Def->getName());
387 for (auto *Dep : Deps) {
388 if (Dep->isDefined())
389 SDG.Dependencies[&TargetJD].insert(V: Dep->getName());
390 else {
391 auto I =
392 SymbolSourceJDs.find(Val: NonOwningSymbolStringPtr(Dep->getName()));
393 if (I != SymbolSourceJDs.end()) {
394 auto &SymJD = *I->second;
395 SDG.Dependencies[&SymJD].insert(V: Dep->getName());
396 }
397 }
398 }
399 }
400 return Error::success();
401 }
402
403 LinkGraphLinkingLayer &Layer;
404 std::vector<std::shared_ptr<LinkGraphLinkingLayer::Plugin>> Plugins;
405 std::unique_ptr<MaterializationResponsibility> MR;
406 std::unique_ptr<MemoryBuffer> ObjBuffer;
407 DenseMap<NonOwningSymbolStringPtr, JITDylib *> SymbolSourceJDs;
408 std::vector<SymbolDependenceGroup> SymbolDepGroups;
409};
410
411LinkGraphLinkingLayer::Plugin::~Plugin() = default;
412
413LinkGraphLinkingLayer::LinkGraphLinkingLayer(ExecutionSession &ES,
414 JITLinkMemoryManager &MemMgr)
415 : LinkGraphLayer(ES), MemMgr(MemMgr) {
416 ES.registerResourceManager(RM&: *this);
417}
418
419LinkGraphLinkingLayer::LinkGraphLinkingLayer(
420 ExecutionSession &ES, std::unique_ptr<JITLinkMemoryManager> MemMgr)
421 : LinkGraphLayer(ES), MemMgr(*MemMgr), MemMgrOwnership(std::move(MemMgr)) {
422 ES.registerResourceManager(RM&: *this);
423}
424
425LinkGraphLinkingLayer::~LinkGraphLinkingLayer() {
426 assert(Allocs.empty() &&
427 "Layer destroyed with resources still attached "
428 "(ExecutionSession::endSession() must be called prior to "
429 "destruction)");
430 getExecutionSession().deregisterResourceManager(RM&: *this);
431}
432
433void LinkGraphLinkingLayer::emit(
434 std::unique_ptr<MaterializationResponsibility> R,
435 std::unique_ptr<LinkGraph> G) {
436 assert(R && "R must not be null");
437 assert(G && "G must not be null");
438 auto Ctx = std::make_unique<JITLinkCtx>(args&: *this, args: std::move(R), args: nullptr);
439 Ctx->notifyMaterializing(G&: *G);
440 link(G: std::move(G), Ctx: std::move(Ctx));
441}
442
443void LinkGraphLinkingLayer::emit(
444 std::unique_ptr<MaterializationResponsibility> R,
445 std::unique_ptr<LinkGraph> G, std::unique_ptr<MemoryBuffer> ObjBuf) {
446 assert(R && "R must not be null");
447 assert(G && "G must not be null");
448 assert(ObjBuf && "Object must not be null");
449 auto Ctx =
450 std::make_unique<JITLinkCtx>(args&: *this, args: std::move(R), args: std::move(ObjBuf));
451 Ctx->notifyMaterializing(G&: *G);
452 link(G: std::move(G), Ctx: std::move(Ctx));
453}
454
455SmallVector<LinkGraphLinkingLayer::SymbolDepGroup>
456LinkGraphLinkingLayer::calculateDepGroups(LinkGraph &G) {
457
458 // Step 1.
459 // Build initial map entries and symbol def lists.
460 BlockDepInfoMap BlockDepInfos;
461 for (auto *Sym : G.defined_symbols())
462 if (Sym->getScope() != Scope::Local)
463 BlockDepInfos[&Sym->getBlock()].SymbolDefs.push_back(Elt: Sym);
464
465 // Step 2.
466 // Complete the BlockDepInfos "graph" by adding symbol and block dependencies
467 // for each block.
468 {
469 SmallVector<Block *> Worklist;
470 Worklist.reserve(N: BlockDepInfos.size());
471
472 // Build worklist, link each BlockDepInfo "node" back to the BlockInfos map
473 // "graph" for our GraphTraits specialization above. This will allow us to
474 // walk the SCCs of the anonymous-block-dependence graph.
475 for (auto &[B, BDInfo] : BlockDepInfos) {
476 BDInfo.Graph = &BlockDepInfos;
477 Worklist.push_back(Elt: B);
478 }
479
480 // Calculate the relevant symbol and block dependencies for each block:
481 // 1. Absolute symbols are ignored.
482 // 2. External symbols are included in a block's symbol dep set.
483 // 3. Blocks that do not define any symbols are included in the anonymous
484 // block dependence sets.
485 // 4. For blocks that do define symbols we add only the first defined
486 // symbol to the symbol dep set (since all symbols for the block will
487 // have the same dependencies).
488 while (!Worklist.empty()) {
489 auto *B = Worklist.pop_back_val();
490 BlockDepInfo *BDInfo = nullptr; // Populated lazily.
491
492 for (auto &E : B->edges()) {
493 if (E.getTarget().isAbsolute()) // skip: absolutes are assumed ready
494 continue;
495
496 if (!BDInfo) // Populate -- we'll need it below.
497 BDInfo = &BlockDepInfos[B];
498
499 if (E.getTarget().isExternal()) { // include and continue
500 BDInfo->SymbolDeps.insert(V: &E.getTarget());
501 continue;
502 }
503
504 // Target must be defined.
505 auto *TgtB = &E.getTarget().getBlock();
506 auto I = BlockDepInfos.find(Val: TgtB);
507
508 if (I != BlockDepInfos.end()) {
509 // TgtB is in BlockInfos. Record a symbol dependence (if it defines
510 // any symbols) or anonymous block dependence.
511 auto &TgtBInfo = I->second;
512 if (!TgtBInfo.SymbolDefs.empty())
513 BDInfo->SymbolDeps.insert(V: TgtBInfo.SymbolDefs.front());
514 else
515 BDInfo->AnonBlockDeps.insert(V: TgtB);
516 } else {
517 // TgtB not in BlockInfos. It must be anonymous. We need to:
518 // 1. Record the dependence.
519 // 2. Add BlockInfos and Worklist entries for TgtB.
520 // 3. Reset BInfo, since step (2) may have invalidated the pointer.
521 BDInfo->AnonBlockDeps.insert(V: TgtB);
522 Worklist.push_back(Elt: TgtB);
523 BlockDepInfos[TgtB].Graph = &BlockDepInfos;
524 BDInfo = nullptr;
525 continue;
526 }
527 }
528 }
529 }
530
531 // Step 3.
532 // Convert block deps to SCC deps.
533 SmallVector<SymbolDepGroup> DGs;
534 for (auto &[B, BDInfo] : BlockDepInfos) {
535 for (auto &SCC : make_range(x: scc_begin(G: &BDInfo), y: scc_end(G: &BDInfo))) {
536
537 auto &SCCRootInfo = *SCC.front();
538
539 // Continue if already visited. The loop over the SCC elements below
540 // deletes the SCCs below as it goes, so this early continue just saves
541 // us looking at a bunch of empty sets below that.
542 if (SCCRootInfo.SCCRoot)
543 continue;
544 SCCRootInfo.SCCRoot = &SCCRootInfo;
545
546 // Collect all symbol defs, deps, and anonymous block deps, and remove
547 // the links to already visited SCCs.
548 auto SCCSymbolDefs = std::move(SCCRootInfo.SymbolDefs);
549 auto SCCSymbolDeps = std::move(SCCRootInfo.SymbolDeps);
550 auto SCCAnonBlockDeps = std::move(SCCRootInfo.AnonBlockDeps);
551 for (auto *SCCBInfo : make_range(x: std::next(x: SCC.begin()), y: SCC.end())) {
552 SCCBInfo->SCCRoot = &SCCRootInfo;
553 SCCSymbolDefs.append(RHS: SCCBInfo->SymbolDefs);
554 SCCBInfo->SymbolDefs.clear();
555 SCCSymbolDeps.insert(I: SCCBInfo->SymbolDeps.begin(),
556 E: SCCBInfo->SymbolDeps.end());
557 SCCBInfo->SymbolDeps.clear();
558 SCCAnonBlockDeps.insert(I: SCCBInfo->AnonBlockDeps.begin(),
559 E: SCCBInfo->AnonBlockDeps.end());
560 SCCBInfo->AnonBlockDeps.clear();
561 }
562
563 // Identify DepGroups emitted for previously visited SCCs that this
564 // SCC depends on.
565 DenseSet<size_t> SrcDepGroups;
566 for (auto *DepB : SCCAnonBlockDeps) {
567 assert(BlockDepInfos.count(DepB) && "Unrecognized block");
568 auto &DepBRootInfo = *BlockDepInfos[DepB].SCCRoot;
569 if (DepBRootInfo.DepGroupIndex)
570 SrcDepGroups.insert(V: *DepBRootInfo.DepGroupIndex);
571 }
572
573 // If this SCC doesn't depend on any existing dep groups then check
574 // whether it has direct symbol deps of its own.
575 if (SrcDepGroups.empty()) {
576
577 // If this SCC has its own symbol deps then add a dep-group and
578 // continue.
579 if (!SCCSymbolDeps.empty()) {
580 SCCRootInfo.DepGroupIndex = DGs.size();
581 DGs.push_back(Elt: {});
582 DGs.back().Defs = std::move(SCCSymbolDefs);
583 DGs.back().Deps = std::move(SCCSymbolDeps);
584 }
585 // Otherwise just continue.
586 continue;
587 }
588
589 // Special case: If we only depend on one dep group and this SCC
590 // doesn't have any symbol deps of its own then just merge this SCC's
591 // defs into the existing dep group and continue.
592 if (SrcDepGroups.size() == 1 && SCCSymbolDeps.empty()) {
593 SCCRootInfo.DepGroupIndex = *SrcDepGroups.begin();
594 DGs[*SCCRootInfo.DepGroupIndex].Defs.append(RHS: SCCSymbolDefs);
595 continue;
596 }
597
598 // General case: This SCC depends on multiple dep groups, and/or has
599 // its own symbol deps. Build a new dep group for it.
600 SCCRootInfo.DepGroupIndex = DGs.size();
601 DGs.push_back(Elt: {});
602 auto &DG = DGs.back();
603 DG.Defs = std::move(SCCSymbolDefs);
604 for (auto &DGIndex : SrcDepGroups)
605 DG.Deps.insert(I: DGs[DGIndex].Deps.begin(), E: DGs[DGIndex].Deps.end());
606 DG.Deps.insert(I: SCCSymbolDeps.begin(), E: SCCSymbolDeps.end());
607 }
608 }
609
610 // Remove self-reference from each dep group, and filter out any dep groups
611 // whose resulting deps or defs are empty.
612 for (size_t I = 0; I != DGs.size();) {
613 auto &DG = DGs[I];
614
615 // Remove self-deps.
616 for (auto &Def : DG.Defs)
617 DG.Deps.erase(V: Def);
618
619 // Remove groups with empty defs or deps.
620 if (DG.Defs.empty() || DG.Deps.empty()) {
621 std::swap(a&: DG, b&: DGs.back());
622 DGs.pop_back();
623 } else
624 ++I;
625 }
626
627 return DGs;
628}
629
630Error LinkGraphLinkingLayer::recordFinalizedAlloc(
631 MaterializationResponsibility &MR, FinalizedAlloc FA) {
632 auto Err = MR.withResourceKeyDo(
633 F: [&](ResourceKey K) { Allocs[K].push_back(x: std::move(FA)); });
634
635 if (Err)
636 Err = joinErrors(E1: std::move(Err), E2: MemMgr.deallocate(Alloc: std::move(FA)));
637
638 return Err;
639}
640
641Error LinkGraphLinkingLayer::handleRemoveResources(JITDylib &JD,
642 ResourceKey K) {
643
644 {
645 Error Err = Error::success();
646 for (auto &P : Plugins)
647 Err = joinErrors(E1: std::move(Err), E2: P->notifyRemovingResources(JD, K));
648 if (Err)
649 return Err;
650 }
651
652 std::vector<FinalizedAlloc> AllocsToRemove;
653 getExecutionSession().runSessionLocked(F: [&] {
654 auto I = Allocs.find(Val: K);
655 if (I != Allocs.end()) {
656 std::swap(x&: AllocsToRemove, y&: I->second);
657 Allocs.erase(I);
658 }
659 });
660
661 if (AllocsToRemove.empty())
662 return Error::success();
663
664 return MemMgr.deallocate(Allocs: std::move(AllocsToRemove));
665}
666
667void LinkGraphLinkingLayer::handleTransferResources(JITDylib &JD,
668 ResourceKey DstKey,
669 ResourceKey SrcKey) {
670 if (Allocs.contains(Val: SrcKey)) {
671 // DstKey may not be in the DenseMap yet, so the following line may resize
672 // the container and invalidate iterators and value references.
673 auto &DstAllocs = Allocs[DstKey];
674 auto &SrcAllocs = Allocs[SrcKey];
675 DstAllocs.reserve(n: DstAllocs.size() + SrcAllocs.size());
676 for (auto &Alloc : SrcAllocs)
677 DstAllocs.push_back(x: std::move(Alloc));
678
679 Allocs.erase(Val: SrcKey);
680 }
681
682 for (auto &P : Plugins)
683 P->notifyTransferringResources(JD, DstKey, SrcKey);
684}
685
686} // End namespace orc.
687} // End namespace llvm.
688