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 : LinkGraphLayer(ES), MemMgr(ES.getExecutorProcessControl().getMemMgr()) {
417 ES.registerResourceManager(RM&: *this);
418}
419
420LinkGraphLinkingLayer::LinkGraphLinkingLayer(ExecutionSession &ES,
421 JITLinkMemoryManager &MemMgr)
422 : LinkGraphLayer(ES), MemMgr(MemMgr) {
423 ES.registerResourceManager(RM&: *this);
424}
425
426LinkGraphLinkingLayer::LinkGraphLinkingLayer(
427 ExecutionSession &ES, std::unique_ptr<JITLinkMemoryManager> MemMgr)
428 : LinkGraphLayer(ES), MemMgr(*MemMgr), MemMgrOwnership(std::move(MemMgr)) {
429 ES.registerResourceManager(RM&: *this);
430}
431
432LinkGraphLinkingLayer::~LinkGraphLinkingLayer() {
433 assert(Allocs.empty() &&
434 "Layer destroyed with resources still attached "
435 "(ExecutionSession::endSession() must be called prior to "
436 "destruction)");
437 getExecutionSession().deregisterResourceManager(RM&: *this);
438}
439
440void LinkGraphLinkingLayer::emit(
441 std::unique_ptr<MaterializationResponsibility> R,
442 std::unique_ptr<LinkGraph> G) {
443 assert(R && "R must not be null");
444 assert(G && "G must not be null");
445 auto Ctx = std::make_unique<JITLinkCtx>(args&: *this, args: std::move(R), args: nullptr);
446 Ctx->notifyMaterializing(G&: *G);
447 link(G: std::move(G), Ctx: std::move(Ctx));
448}
449
450void LinkGraphLinkingLayer::emit(
451 std::unique_ptr<MaterializationResponsibility> R,
452 std::unique_ptr<LinkGraph> G, std::unique_ptr<MemoryBuffer> ObjBuf) {
453 assert(R && "R must not be null");
454 assert(G && "G must not be null");
455 assert(ObjBuf && "Object must not be null");
456 auto Ctx =
457 std::make_unique<JITLinkCtx>(args&: *this, args: std::move(R), args: std::move(ObjBuf));
458 Ctx->notifyMaterializing(G&: *G);
459 link(G: std::move(G), Ctx: std::move(Ctx));
460}
461
462SmallVector<LinkGraphLinkingLayer::SymbolDepGroup>
463LinkGraphLinkingLayer::calculateDepGroups(LinkGraph &G) {
464
465 // Step 1.
466 // Build initial map entries and symbol def lists.
467 BlockDepInfoMap BlockDepInfos;
468 for (auto *Sym : G.defined_symbols())
469 if (Sym->getScope() != Scope::Local)
470 BlockDepInfos[&Sym->getBlock()].SymbolDefs.push_back(Elt: Sym);
471
472 // Step 2.
473 // Complete the BlockDepInfos "graph" by adding symbol and block dependencies
474 // for each block.
475 {
476 SmallVector<Block *> Worklist;
477 Worklist.reserve(N: BlockDepInfos.size());
478
479 // Build worklist, link each BlockDepInfo "node" back to the BlockInfos map
480 // "graph" for our GraphTraits specialization above. This will allow us to
481 // walk the SCCs of the anonymous-block-dependence graph.
482 for (auto &[B, BDInfo] : BlockDepInfos) {
483 BDInfo.Graph = &BlockDepInfos;
484 Worklist.push_back(Elt: B);
485 }
486
487 // Calculate the relevant symbol and block dependencies for each block:
488 // 1. Absolute symbols are ignored.
489 // 2. External symbols are included in a block's symbol dep set.
490 // 3. Blocks that do not define any symbols are included in the anonymous
491 // block dependence sets.
492 // 4. For blocks that do define symbols we add only the first defined
493 // symbol to the symbol dep set (since all symbols for the block will
494 // have the same dependencies).
495 while (!Worklist.empty()) {
496 auto *B = Worklist.pop_back_val();
497 BlockDepInfo *BDInfo = nullptr; // Populated lazily.
498
499 for (auto &E : B->edges()) {
500 if (E.getTarget().isAbsolute()) // skip: absolutes are assumed ready
501 continue;
502
503 if (!BDInfo) // Populate -- we'll need it below.
504 BDInfo = &BlockDepInfos[B];
505
506 if (E.getTarget().isExternal()) { // include and continue
507 BDInfo->SymbolDeps.insert(V: &E.getTarget());
508 continue;
509 }
510
511 // Target must be defined.
512 auto *TgtB = &E.getTarget().getBlock();
513 auto I = BlockDepInfos.find(Val: TgtB);
514
515 if (I != BlockDepInfos.end()) {
516 // TgtB is in BlockInfos. Record a symbol dependence (if it defines
517 // any symbols) or anonymous block dependence.
518 auto &TgtBInfo = I->second;
519 if (!TgtBInfo.SymbolDefs.empty())
520 BDInfo->SymbolDeps.insert(V: TgtBInfo.SymbolDefs.front());
521 else
522 BDInfo->AnonBlockDeps.insert(V: TgtB);
523 } else {
524 // TgtB not in BlockInfos. It must be anonymous. We need to:
525 // 1. Record the dependence.
526 // 2. Add BlockInfos and Worklist entries for TgtB.
527 // 3. Reset BInfo, since step (2) may have invalidated the pointer.
528 BDInfo->AnonBlockDeps.insert(V: TgtB);
529 Worklist.push_back(Elt: TgtB);
530 BlockDepInfos[TgtB].Graph = &BlockDepInfos;
531 BDInfo = nullptr;
532 continue;
533 }
534 }
535 }
536 }
537
538 // Step 3.
539 // Convert block deps to SCC deps.
540 SmallVector<SymbolDepGroup> DGs;
541 for (auto &[B, BDInfo] : BlockDepInfos) {
542 for (auto &SCC : make_range(x: scc_begin(G: &BDInfo), y: scc_end(G: &BDInfo))) {
543
544 auto &SCCRootInfo = *SCC.front();
545
546 // Continue if already visited. The loop over the SCC elements below
547 // deletes the SCCs below as it goes, so this early continue just saves
548 // us looking at a bunch of empty sets below that.
549 if (SCCRootInfo.SCCRoot)
550 continue;
551 SCCRootInfo.SCCRoot = &SCCRootInfo;
552
553 // Collect all symbol defs, deps, and anonymous block deps, and remove
554 // the links to already visited SCCs.
555 auto SCCSymbolDefs = std::move(SCCRootInfo.SymbolDefs);
556 auto SCCSymbolDeps = std::move(SCCRootInfo.SymbolDeps);
557 auto SCCAnonBlockDeps = std::move(SCCRootInfo.AnonBlockDeps);
558 for (auto *SCCBInfo : make_range(x: std::next(x: SCC.begin()), y: SCC.end())) {
559 SCCBInfo->SCCRoot = &SCCRootInfo;
560 SCCSymbolDefs.append(RHS: SCCBInfo->SymbolDefs);
561 SCCBInfo->SymbolDefs.clear();
562 SCCSymbolDeps.insert(I: SCCBInfo->SymbolDeps.begin(),
563 E: SCCBInfo->SymbolDeps.end());
564 SCCBInfo->SymbolDeps.clear();
565 SCCAnonBlockDeps.insert(I: SCCBInfo->AnonBlockDeps.begin(),
566 E: SCCBInfo->AnonBlockDeps.end());
567 SCCBInfo->AnonBlockDeps.clear();
568 }
569
570 // Identify DepGroups emitted for previously visited SCCs that this
571 // SCC depends on.
572 DenseSet<size_t> SrcDepGroups;
573 for (auto *DepB : SCCAnonBlockDeps) {
574 assert(BlockDepInfos.count(DepB) && "Unrecognized block");
575 auto &DepBRootInfo = *BlockDepInfos[DepB].SCCRoot;
576 if (DepBRootInfo.DepGroupIndex)
577 SrcDepGroups.insert(V: *DepBRootInfo.DepGroupIndex);
578 }
579
580 // If this SCC doesn't depend on any existing dep groups then check
581 // whether it has direct symbol deps of its own.
582 if (SrcDepGroups.empty()) {
583
584 // If this SCC has its own symbol deps then add a dep-group and
585 // continue.
586 if (!SCCSymbolDeps.empty()) {
587 SCCRootInfo.DepGroupIndex = DGs.size();
588 DGs.push_back(Elt: {});
589 DGs.back().Defs = std::move(SCCSymbolDefs);
590 DGs.back().Deps = std::move(SCCSymbolDeps);
591 }
592 // Otherwise just continue.
593 continue;
594 }
595
596 // Special case: If we only depend on one dep group and this SCC
597 // doesn't have any symbol deps of its own then just merge this SCC's
598 // defs into the existing dep group and continue.
599 if (SrcDepGroups.size() == 1 && SCCSymbolDeps.empty()) {
600 SCCRootInfo.DepGroupIndex = *SrcDepGroups.begin();
601 DGs[*SCCRootInfo.DepGroupIndex].Defs.append(RHS: SCCSymbolDefs);
602 continue;
603 }
604
605 // General case: This SCC depends on multiple dep groups, and/or has
606 // its own symbol deps. Build a new dep group for it.
607 SCCRootInfo.DepGroupIndex = DGs.size();
608 DGs.push_back(Elt: {});
609 auto &DG = DGs.back();
610 DG.Defs = std::move(SCCSymbolDefs);
611 for (auto &DGIndex : SrcDepGroups)
612 DG.Deps.insert(I: DGs[DGIndex].Deps.begin(), E: DGs[DGIndex].Deps.end());
613 DG.Deps.insert(I: SCCSymbolDeps.begin(), E: SCCSymbolDeps.end());
614 }
615 }
616
617 // Remove self-reference from each dep group, and filter out any dep groups
618 // whose resulting deps or defs are empty.
619 for (size_t I = 0; I != DGs.size();) {
620 auto &DG = DGs[I];
621
622 // Remove self-deps.
623 for (auto &Def : DG.Defs)
624 DG.Deps.erase(V: Def);
625
626 // Remove groups with empty defs or deps.
627 if (DG.Defs.empty() || DG.Deps.empty()) {
628 std::swap(a&: DG, b&: DGs.back());
629 DGs.pop_back();
630 } else
631 ++I;
632 }
633
634 return DGs;
635}
636
637Error LinkGraphLinkingLayer::recordFinalizedAlloc(
638 MaterializationResponsibility &MR, FinalizedAlloc FA) {
639 auto Err = MR.withResourceKeyDo(
640 F: [&](ResourceKey K) { Allocs[K].push_back(x: std::move(FA)); });
641
642 if (Err)
643 Err = joinErrors(E1: std::move(Err), E2: MemMgr.deallocate(Alloc: std::move(FA)));
644
645 return Err;
646}
647
648Error LinkGraphLinkingLayer::handleRemoveResources(JITDylib &JD,
649 ResourceKey K) {
650
651 {
652 Error Err = Error::success();
653 for (auto &P : Plugins)
654 Err = joinErrors(E1: std::move(Err), E2: P->notifyRemovingResources(JD, K));
655 if (Err)
656 return Err;
657 }
658
659 std::vector<FinalizedAlloc> AllocsToRemove;
660 getExecutionSession().runSessionLocked(F: [&] {
661 auto I = Allocs.find(Val: K);
662 if (I != Allocs.end()) {
663 std::swap(x&: AllocsToRemove, y&: I->second);
664 Allocs.erase(I);
665 }
666 });
667
668 if (AllocsToRemove.empty())
669 return Error::success();
670
671 return MemMgr.deallocate(Allocs: std::move(AllocsToRemove));
672}
673
674void LinkGraphLinkingLayer::handleTransferResources(JITDylib &JD,
675 ResourceKey DstKey,
676 ResourceKey SrcKey) {
677 if (Allocs.contains(Val: SrcKey)) {
678 // DstKey may not be in the DenseMap yet, so the following line may resize
679 // the container and invalidate iterators and value references.
680 auto &DstAllocs = Allocs[DstKey];
681 auto &SrcAllocs = Allocs[SrcKey];
682 DstAllocs.reserve(n: DstAllocs.size() + SrcAllocs.size());
683 for (auto &Alloc : SrcAllocs)
684 DstAllocs.push_back(x: std::move(Alloc));
685
686 Allocs.erase(Val: SrcKey);
687 }
688
689 for (auto &P : Plugins)
690 P->notifyTransferringResources(JD, DstKey, SrcKey);
691}
692
693} // End namespace orc.
694} // End namespace llvm.
695