1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
|
//===------ LinkGraphLinkingLayer.cpp - Link LinkGraphs with JITLink ------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "llvm/ExecutionEngine/Orc/LinkGraphLinkingLayer.h"
#include "llvm/ExecutionEngine/JITLink/EHFrameSupport.h"
#include "llvm/ExecutionEngine/JITLink/aarch32.h"
#include "llvm/ExecutionEngine/Orc/DebugUtils.h"
#include "llvm/ExecutionEngine/Orc/Shared/ObjectFormats.h"
#include "llvm/Support/MemoryBuffer.h"
#define DEBUG_TYPE "orc"
using namespace llvm;
using namespace llvm::jitlink;
using namespace llvm::orc;
namespace {
ExecutorAddr getJITSymbolPtrForSymbol(Symbol &Sym, const Triple &TT) {
switch (TT.getArch()) {
case Triple::arm:
case Triple::armeb:
case Triple::thumb:
case Triple::thumbeb:
if (hasTargetFlags(Sym, aarch32::ThumbSymbol)) {
// Set LSB to indicate thumb target
assert(Sym.isCallable() && "Only callable symbols can have thumb flag");
assert((Sym.getAddress().getValue() & 0x01) == 0 && "LSB is clear");
return Sym.getAddress() + 0x01;
}
return Sym.getAddress();
default:
return Sym.getAddress();
}
}
} // end anonymous namespace
namespace llvm {
namespace orc {
class LinkGraphLinkingLayer::JITLinkCtx final : public JITLinkContext {
public:
JITLinkCtx(LinkGraphLinkingLayer &Layer,
std::unique_ptr<MaterializationResponsibility> MR,
std::unique_ptr<MemoryBuffer> ObjBuffer)
: JITLinkContext(&MR->getTargetJITDylib()), Layer(Layer),
MR(std::move(MR)), ObjBuffer(std::move(ObjBuffer)) {
std::lock_guard<std::mutex> Lock(Layer.LayerMutex);
Plugins = Layer.Plugins;
}
~JITLinkCtx() {
// If there is an object buffer return function then use it to
// return ownership of the buffer.
if (Layer.ReturnObjectBuffer && ObjBuffer)
Layer.ReturnObjectBuffer(std::move(ObjBuffer));
}
JITLinkMemoryManager &getMemoryManager() override { return Layer.MemMgr; }
void notifyMaterializing(LinkGraph &G) {
for (auto &P : Plugins)
P->notifyMaterializing(*MR, G, *this,
ObjBuffer ? ObjBuffer->getMemBufferRef()
: MemoryBufferRef());
}
void notifyFailed(Error Err) override {
for (auto &P : Plugins)
Err = joinErrors(std::move(Err), P->notifyFailed(*MR));
Layer.getExecutionSession().reportError(std::move(Err));
MR->failMaterialization();
}
void lookup(const LookupMap &Symbols,
std::unique_ptr<JITLinkAsyncLookupContinuation> LC) override {
JITDylibSearchOrder LinkOrder;
MR->getTargetJITDylib().withLinkOrderDo(
[&](const JITDylibSearchOrder &LO) { LinkOrder = LO; });
auto &ES = Layer.getExecutionSession();
SymbolLookupSet LookupSet;
for (auto &KV : Symbols) {
orc::SymbolLookupFlags LookupFlags;
switch (KV.second) {
case jitlink::SymbolLookupFlags::RequiredSymbol:
LookupFlags = orc::SymbolLookupFlags::RequiredSymbol;
break;
case jitlink::SymbolLookupFlags::WeaklyReferencedSymbol:
LookupFlags = orc::SymbolLookupFlags::WeaklyReferencedSymbol;
break;
}
LookupSet.add(KV.first, LookupFlags);
}
// OnResolve -- De-intern the symbols and pass the result to the linker.
auto OnResolve = [LookupContinuation =
std::move(LC)](Expected<SymbolMap> Result) mutable {
if (!Result)
LookupContinuation->run(Result.takeError());
else {
AsyncLookupResult LR;
LR.insert_range(*Result);
LookupContinuation->run(std::move(LR));
}
};
ES.lookup(LookupKind::Static, LinkOrder, std::move(LookupSet),
SymbolState::Resolved, std::move(OnResolve),
[this](const SymbolDependenceMap &Deps) {
// Translate LookupDeps map to SymbolSourceJD.
for (auto &[DepJD, Deps] : Deps)
for (auto &DepSym : Deps)
SymbolSourceJDs[NonOwningSymbolStringPtr(DepSym)] = DepJD;
});
}
Error notifyResolved(LinkGraph &G) override {
SymbolFlagsMap ExtraSymbolsToClaim;
bool AutoClaim = Layer.AutoClaimObjectSymbols;
SymbolMap InternedResult;
for (auto *Sym : G.defined_symbols())
if (Sym->getScope() < Scope::SideEffectsOnly) {
auto Ptr = getJITSymbolPtrForSymbol(*Sym, G.getTargetTriple());
auto Flags = getJITSymbolFlagsForSymbol(*Sym);
InternedResult[Sym->getName()] = {Ptr, Flags};
if (AutoClaim && !MR->getSymbols().count(Sym->getName())) {
assert(!ExtraSymbolsToClaim.count(Sym->getName()) &&
"Duplicate symbol to claim?");
ExtraSymbolsToClaim[Sym->getName()] = Flags;
}
}
for (auto *Sym : G.absolute_symbols())
if (Sym->getScope() < Scope::SideEffectsOnly) {
auto Ptr = getJITSymbolPtrForSymbol(*Sym, G.getTargetTriple());
auto Flags = getJITSymbolFlagsForSymbol(*Sym);
InternedResult[Sym->getName()] = {Ptr, Flags};
if (AutoClaim && !MR->getSymbols().count(Sym->getName())) {
assert(!ExtraSymbolsToClaim.count(Sym->getName()) &&
"Duplicate symbol to claim?");
ExtraSymbolsToClaim[Sym->getName()] = Flags;
}
}
if (!ExtraSymbolsToClaim.empty())
if (auto Err = MR->defineMaterializing(ExtraSymbolsToClaim))
return Err;
{
// Check that InternedResult matches up with MR->getSymbols(), overriding
// flags if requested.
// This guards against faulty transformations / compilers / object caches.
// First check that there aren't any missing symbols.
size_t NumMaterializationSideEffectsOnlySymbols = 0;
SymbolNameVector MissingSymbols;
for (auto &[Sym, Flags] : MR->getSymbols()) {
auto I = InternedResult.find(Sym);
// If this is a materialization-side-effects only symbol then bump
// the counter and remove in from the result, otherwise make sure that
// it's defined.
if (Flags.hasMaterializationSideEffectsOnly())
++NumMaterializationSideEffectsOnlySymbols;
else if (I == InternedResult.end())
MissingSymbols.push_back(Sym);
else if (Layer.OverrideObjectFlags)
I->second.setFlags(Flags);
}
// If there were missing symbols then report the error.
if (!MissingSymbols.empty())
return make_error<MissingSymbolDefinitions>(
Layer.getExecutionSession().getSymbolStringPool(), G.getName(),
std::move(MissingSymbols));
// If there are more definitions than expected, add them to the
// ExtraSymbols vector.
SymbolNameVector ExtraSymbols;
if (InternedResult.size() >
MR->getSymbols().size() - NumMaterializationSideEffectsOnlySymbols) {
for (auto &KV : InternedResult)
if (!MR->getSymbols().count(KV.first))
ExtraSymbols.push_back(KV.first);
}
// If there were extra definitions then report the error.
if (!ExtraSymbols.empty())
return make_error<UnexpectedSymbolDefinitions>(
Layer.getExecutionSession().getSymbolStringPool(), G.getName(),
std::move(ExtraSymbols));
}
if (auto Err = MR->notifyResolved(InternedResult))
return Err;
return Error::success();
}
void notifyFinalized(JITLinkMemoryManager::FinalizedAlloc A) override {
if (auto Err = notifyEmitted(std::move(A))) {
Layer.getExecutionSession().reportError(std::move(Err));
MR->failMaterialization();
return;
}
if (auto Err = MR->notifyEmitted(SymbolDepGroups)) {
Layer.getExecutionSession().reportError(std::move(Err));
MR->failMaterialization();
}
}
LinkGraphPassFunction getMarkLivePass(const Triple &TT) const override {
return [this](LinkGraph &G) { return markResponsibilitySymbolsLive(G); };
}
Error modifyPassConfig(LinkGraph &LG, PassConfiguration &Config) override {
// Add passes to mark duplicate defs as should-discard, and to walk the
// link graph to build the symbol dependence graph.
Config.PrePrunePasses.push_back([this](LinkGraph &G) {
return claimOrExternalizeWeakAndCommonSymbols(G);
});
for (auto &P : Plugins)
P->modifyPassConfig(*MR, LG, Config);
Config.PreFixupPasses.push_back(
[this](LinkGraph &G) { return registerDependencies(G); });
return Error::success();
}
Error notifyEmitted(jitlink::JITLinkMemoryManager::FinalizedAlloc FA) {
Error Err = Error::success();
for (auto &P : Plugins)
Err = joinErrors(std::move(Err), P->notifyEmitted(*MR));
if (Err) {
if (FA)
Err =
joinErrors(std::move(Err), Layer.MemMgr.deallocate(std::move(FA)));
return Err;
}
if (FA)
return Layer.recordFinalizedAlloc(*MR, std::move(FA));
return Error::success();
}
private:
Error claimOrExternalizeWeakAndCommonSymbols(LinkGraph &G) {
SymbolFlagsMap NewSymbolsToClaim;
std::vector<std::pair<SymbolStringPtr, Symbol *>> NameToSym;
auto ProcessSymbol = [&](Symbol *Sym) {
if (Sym->hasName() && Sym->getLinkage() == Linkage::Weak &&
Sym->getScope() != Scope::Local) {
if (!MR->getSymbols().count(Sym->getName())) {
NewSymbolsToClaim[Sym->getName()] =
getJITSymbolFlagsForSymbol(*Sym) | JITSymbolFlags::Weak;
NameToSym.push_back(std::make_pair(Sym->getName(), Sym));
}
}
};
for (auto *Sym : G.defined_symbols())
ProcessSymbol(Sym);
for (auto *Sym : G.absolute_symbols())
ProcessSymbol(Sym);
// Attempt to claim all weak defs that we're not already responsible for.
// This may fail if the resource tracker has become defunct, but should
// always succeed otherwise.
if (auto Err = MR->defineMaterializing(std::move(NewSymbolsToClaim)))
return Err;
// Walk the list of symbols that we just tried to claim. Symbols that we're
// responsible for are marked live. Symbols that we're not responsible for
// are turned into external references.
for (auto &KV : NameToSym) {
if (MR->getSymbols().count(KV.first))
KV.second->setLive(true);
else
G.makeExternal(*KV.second);
}
return Error::success();
}
Error markResponsibilitySymbolsLive(LinkGraph &G) const {
for (auto *Sym : G.defined_symbols())
if (Sym->hasName() && MR->getSymbols().count(Sym->getName()))
Sym->setLive(true);
return Error::success();
}
Error registerDependencies(LinkGraph &G) {
struct BlockInfo {
bool InWorklist = false;
DenseSet<Symbol *> Defs;
DenseSet<Symbol *> SymbolDeps;
DenseSet<Block *> AnonEdges, AnonBackEdges;
};
DenseMap<Block *, BlockInfo> BlockInfos;
// Reserve space so that BlockInfos doesn't need to resize. This is
// essential to avoid invalidating pointers to entries below.
{
size_t NumBlocks = 0;
for (auto &Sec : G.sections())
NumBlocks += Sec.blocks_size();
BlockInfos.reserve(NumBlocks);
}
// Identify non-locally-scoped symbols defined by each block.
for (auto *Sym : G.defined_symbols()) {
if (Sym->getScope() != Scope::Local)
BlockInfos[&Sym->getBlock()].Defs.insert(Sym);
}
// Identify the symbolic and anonymous-block dependencies for each block.
for (auto *B : G.blocks()) {
auto &BI = BlockInfos[B];
for (auto &E : B->edges()) {
// External symbols are trivially depended on.
if (E.getTarget().isExternal()) {
BI.SymbolDeps.insert(&E.getTarget());
continue;
}
// Anonymous symbols aren't depended on at all (they're assumed to be
// already available).
if (E.getTarget().isAbsolute())
continue;
// If we get here then we depend on a symbol defined by some other
// block.
auto &TgtBI = BlockInfos[&E.getTarget().getBlock()];
// If that block has any definitions then use the first one as the
// "effective" dependence here (all symbols in TgtBI will become
// ready at the same time, and chosing a single symbol to represent
// the block keeps the SymbolDepGroup size small).
if (!TgtBI.Defs.empty()) {
BI.SymbolDeps.insert(*TgtBI.Defs.begin());
continue;
}
// Otherwise we've got a dependence on an anonymous block. Record it
// here for back-propagating symbol dependencies below.
BI.AnonEdges.insert(&E.getTarget().getBlock());
TgtBI.AnonBackEdges.insert(B);
}
}
// Prune anonymous blocks.
{
std::vector<Block *> BlocksToRemove;
for (auto &[B, BI] : BlockInfos) {
// Skip blocks with defs. We only care about anonyous blocks.
if (!BI.Defs.empty())
continue;
BlocksToRemove.push_back(B);
for (auto *FB : BI.AnonEdges)
BlockInfos[FB].AnonBackEdges.erase(B);
for (auto *BB : BI.AnonBackEdges)
BlockInfos[BB].AnonEdges.erase(B);
for (auto *FB : BI.AnonEdges) {
auto &FBI = BlockInfos[FB];
FBI.AnonBackEdges.insert_range(BI.AnonBackEdges);
}
for (auto *BB : BI.AnonBackEdges) {
auto &BBI = BlockInfos[BB];
BBI.SymbolDeps.insert_range(BI.SymbolDeps);
BBI.AnonEdges.insert_range(BI.AnonEdges);
}
}
for (auto *B : BlocksToRemove)
BlockInfos.erase(B);
}
// Build the initial dependence propagation worklist.
std::deque<Block *> Worklist;
for (auto &[B, BI] : BlockInfos) {
if (!BI.SymbolDeps.empty() && !BI.AnonBackEdges.empty()) {
Worklist.push_back(B);
BI.InWorklist = true;
}
}
// Propagate symbol deps through the graph.
while (!Worklist.empty()) {
auto *B = Worklist.front();
Worklist.pop_front();
auto &BI = BlockInfos[B];
BI.InWorklist = false;
for (auto *DB : BI.AnonBackEdges) {
auto &DBI = BlockInfos[DB];
for (auto *Sym : BI.SymbolDeps) {
if (DBI.SymbolDeps.insert(Sym).second && !DBI.InWorklist) {
Worklist.push_back(DB);
DBI.InWorklist = true;
}
}
}
}
// Transform our local dependence information into a list of
// SymbolDependenceGroups (in the SymbolDepGroups member), ready for use in
// the upcoming notifyFinalized call.
auto &TargetJD = MR->getTargetJITDylib();
for (auto &[B, BI] : BlockInfos) {
if (!BI.Defs.empty()) {
SymbolDepGroups.push_back(SymbolDependenceGroup());
auto &SDG = SymbolDepGroups.back();
for (auto *Def : BI.Defs)
SDG.Symbols.insert(Def->getName());
for (auto *Dep : BI.SymbolDeps) {
auto DepName = Dep->getName();
if (Dep->isDefined())
SDG.Dependencies[&TargetJD].insert(std::move(DepName));
else {
auto SourceJDItr =
SymbolSourceJDs.find(NonOwningSymbolStringPtr(DepName));
if (SourceJDItr != SymbolSourceJDs.end())
SDG.Dependencies[SourceJDItr->second].insert(std::move(DepName));
}
}
}
}
return Error::success();
}
LinkGraphLinkingLayer &Layer;
std::vector<std::shared_ptr<LinkGraphLinkingLayer::Plugin>> Plugins;
std::unique_ptr<MaterializationResponsibility> MR;
std::unique_ptr<MemoryBuffer> ObjBuffer;
DenseMap<NonOwningSymbolStringPtr, JITDylib *> SymbolSourceJDs;
std::vector<SymbolDependenceGroup> SymbolDepGroups;
};
LinkGraphLinkingLayer::Plugin::~Plugin() = default;
LinkGraphLinkingLayer::LinkGraphLinkingLayer(ExecutionSession &ES)
: LinkGraphLayer(ES), MemMgr(ES.getExecutorProcessControl().getMemMgr()) {
ES.registerResourceManager(*this);
}
LinkGraphLinkingLayer::LinkGraphLinkingLayer(ExecutionSession &ES,
JITLinkMemoryManager &MemMgr)
: LinkGraphLayer(ES), MemMgr(MemMgr) {
ES.registerResourceManager(*this);
}
LinkGraphLinkingLayer::LinkGraphLinkingLayer(
ExecutionSession &ES, std::unique_ptr<JITLinkMemoryManager> MemMgr)
: LinkGraphLayer(ES), MemMgr(*MemMgr), MemMgrOwnership(std::move(MemMgr)) {
ES.registerResourceManager(*this);
}
LinkGraphLinkingLayer::~LinkGraphLinkingLayer() {
assert(Allocs.empty() &&
"Layer destroyed with resources still attached "
"(ExecutionSession::endSession() must be called prior to "
"destruction)");
getExecutionSession().deregisterResourceManager(*this);
}
void LinkGraphLinkingLayer::emit(
std::unique_ptr<MaterializationResponsibility> R,
std::unique_ptr<LinkGraph> G) {
assert(R && "R must not be null");
assert(G && "G must not be null");
auto Ctx = std::make_unique<JITLinkCtx>(*this, std::move(R), nullptr);
Ctx->notifyMaterializing(*G);
link(std::move(G), std::move(Ctx));
}
void LinkGraphLinkingLayer::emit(
std::unique_ptr<MaterializationResponsibility> R,
std::unique_ptr<LinkGraph> G, std::unique_ptr<MemoryBuffer> ObjBuf) {
assert(R && "R must not be null");
assert(G && "G must not be null");
assert(ObjBuf && "Object must not be null");
auto Ctx =
std::make_unique<JITLinkCtx>(*this, std::move(R), std::move(ObjBuf));
Ctx->notifyMaterializing(*G);
link(std::move(G), std::move(Ctx));
}
Error LinkGraphLinkingLayer::recordFinalizedAlloc(
MaterializationResponsibility &MR, FinalizedAlloc FA) {
auto Err = MR.withResourceKeyDo(
[&](ResourceKey K) { Allocs[K].push_back(std::move(FA)); });
if (Err)
Err = joinErrors(std::move(Err), MemMgr.deallocate(std::move(FA)));
return Err;
}
Error LinkGraphLinkingLayer::handleRemoveResources(JITDylib &JD,
ResourceKey K) {
{
Error Err = Error::success();
for (auto &P : Plugins)
Err = joinErrors(std::move(Err), P->notifyRemovingResources(JD, K));
if (Err)
return Err;
}
std::vector<FinalizedAlloc> AllocsToRemove;
getExecutionSession().runSessionLocked([&] {
auto I = Allocs.find(K);
if (I != Allocs.end()) {
std::swap(AllocsToRemove, I->second);
Allocs.erase(I);
}
});
if (AllocsToRemove.empty())
return Error::success();
return MemMgr.deallocate(std::move(AllocsToRemove));
}
void LinkGraphLinkingLayer::handleTransferResources(JITDylib &JD,
ResourceKey DstKey,
ResourceKey SrcKey) {
if (Allocs.contains(SrcKey)) {
// DstKey may not be in the DenseMap yet, so the following line may resize
// the container and invalidate iterators and value references.
auto &DstAllocs = Allocs[DstKey];
auto &SrcAllocs = Allocs[SrcKey];
DstAllocs.reserve(DstAllocs.size() + SrcAllocs.size());
for (auto &Alloc : SrcAllocs)
DstAllocs.push_back(std::move(Alloc));
Allocs.erase(SrcKey);
}
for (auto &P : Plugins)
P->notifyTransferringResources(JD, DstKey, SrcKey);
}
} // End namespace orc.
} // End namespace llvm.
|