LLVM 20.0.0git
LazyObjectLinkingLayer.cpp
Go to the documentation of this file.
1//===---------- LazyReexports.cpp - Utilities for lazy reexports ----------===//
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
10
14
15using namespace llvm;
16using namespace llvm::jitlink;
17
18namespace {
19
20constexpr StringRef FnBodySuffix = "$orc_fnbody";
21
22} // anonymous namespace
23
24namespace llvm::orc {
25
27 : public ObjectLinkingLayer::Plugin {
28public:
32 // We need to insert this before the mark-live pass to ensure that we don't
33 // delete the bodies (their names won't match the responsibility set until
34 // after this pass completes.
35 Config.PrePrunePasses.insert(
36 Config.PrePrunePasses.begin(),
37 [&MR](LinkGraph &G) { return renameFunctionBodies(G, MR); });
38 }
39
41 return Error::success();
42 }
43
45 return Error::success();
46 }
47
49 ResourceKey SrcKey) override {}
50
51private:
52 static Error renameFunctionBodies(LinkGraph &G,
55 for (auto &[Name, Flags] : MR.getSymbols())
56 if ((*Name).ends_with(FnBodySuffix))
57 SymsToRename[(*Name).drop_back(FnBodySuffix.size())] =
59
60 for (auto *Sym : G.defined_symbols()) {
61 if (!Sym->hasName())
62 continue;
63 auto I = SymsToRename.find(*Sym->getName());
64 if (I == SymsToRename.end())
65 continue;
66 Sym->setName(G.intern(G.allocateName(*I->second)));
67 }
68
69 return Error::success();
70 }
71};
72
75 : ObjectLayer(BaseLayer.getExecutionSession()), BaseLayer(BaseLayer),
76 LRMgr(LRMgr) {
77 BaseLayer.addPlugin(std::make_unique<RenamerPlugin>());
78}
79
81 std::unique_ptr<MemoryBuffer> O,
83
84 // Object files with initializer symbols can't be lazy.
85 if (I.InitSymbol)
86 return BaseLayer.add(std::move(RT), std::move(O), std::move(I));
87
88 auto &ES = getExecutionSession();
89 SymbolAliasMap LazySymbols;
90 for (auto &[Name, Flags] : I.SymbolFlags)
91 if (Flags.isCallable())
92 LazySymbols[Name] = {ES.intern((*Name + FnBodySuffix).str()), Flags};
93
94 for (auto &[Name, AI] : LazySymbols) {
95 I.SymbolFlags.erase(Name);
96 I.SymbolFlags[AI.Aliasee] = AI.AliasFlags;
97 }
98
99 if (auto Err = BaseLayer.add(RT, std::move(O), std::move(I)))
100 return Err;
101
102 auto &JD = RT->getJITDylib();
103 return JD.define(lazyReexports(LRMgr, std::move(LazySymbols)), std::move(RT));
104}
105
107 std::unique_ptr<MaterializationResponsibility> MR,
108 std::unique_ptr<MemoryBuffer> Obj) {
109 return BaseLayer.emit(std::move(MR), std::move(Obj));
110}
111
112} // namespace llvm::orc
std::string Name
RelaxConfig Config
Definition: ELF_riscv.cpp:506
Symbol * Sym
Definition: ELF_riscv.cpp:479
#define I(x, y, z)
Definition: MD5.cpp:58
#define G(x, y, z)
Definition: MD5.cpp:56
iterator find(const_arg_type_t< KeyT > Val)
Definition: DenseMap.h:156
iterator end()
Definition: DenseMap.h:84
Lightweight error class with error context and mandatory checking.
Definition: Error.h:160
static ErrorSuccess success()
Create a success value.
Definition: Error.h:337
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:51
Represents a JIT'd dynamic library.
Definition: Core.h:897
void modifyPassConfig(MaterializationResponsibility &MR, jitlink::LinkGraph &LG, jitlink::PassConfiguration &Config) override
Error notifyFailed(MaterializationResponsibility &MR) override
Error notifyRemovingResources(JITDylib &JD, ResourceKey K) override
void notifyTransferringResources(JITDylib &JD, ResourceKey DstKey, ResourceKey SrcKey) override
void emit(std::unique_ptr< MaterializationResponsibility > R, std::unique_ptr< MemoryBuffer > O) override
Emit should materialize the given IR.
LazyObjectLinkingLayer(ObjectLinkingLayer &BaseLayer, LazyReexportsManager &LRMgr)
llvm::Error add(llvm::orc::ResourceTrackerSP RT, std::unique_ptr< MemoryBuffer > O, MaterializationUnit::Interface I) override
Add an object file to the JITDylib targeted by the given tracker.
LinkGraphLinkingLayer & addPlugin(std::shared_ptr< Plugin > P)
Add a plugin.
Tracks responsibility for materialization, and mediates interactions between MaterializationUnits and...
Definition: Core.h:571
const SymbolFlagsMap & getSymbols() const
Returns the symbol flags map for this responsibility instance.
Definition: Core.h:605
Non-owning SymbolStringPool entry pointer.
Interface for Layers that accept object files.
Definition: Layer.h:133
ExecutionSession & getExecutionSession()
Returns the execution session for this layer.
Definition: Layer.h:141
An ObjectLayer implementation built on JITLink.
void emit(std::unique_ptr< MaterializationResponsibility > R, std::unique_ptr< MemoryBuffer > O) override
Emit an object file.
virtual Error add(ResourceTrackerSP RT, std::unique_ptr< MemoryBuffer > O, MaterializationUnit::Interface I)
Adds a MaterializationUnit for the object file in the given memory buffer to the JITDylib for the giv...
Definition: Layer.cpp:170
std::unique_ptr< LazyReexportsMaterializationUnit > lazyReexports(LazyCallThroughManager &LCTManager, RedirectableSymbolManager &RSManager, JITDylib &SourceJD, SymbolAliasMap CallableAliases, ImplSymbolMap *SrcJDLoc=nullptr)
Define lazy-reexports based on the given SymbolAliasMap.
uintptr_t ResourceKey
Definition: Core.h:74
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18