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