LLVM 23.0.0git
EPCGenericJITLinkMemoryManager.cpp
Go to the documentation of this file.
1//===---- EPCGenericJITLinkMemoryManager.cpp -- Mem management via EPC ----===//
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
15#include <limits>
16
17using namespace llvm::jitlink;
18
19namespace llvm {
20namespace orc {
21
24public:
25
26 // FIXME: The C++98 initializer is an attempt to work around compile failures
27 // due to http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#1397.
28 // We should be able to switch this back to member initialization once that
29 // issue is fixed.
38
40
42 ExecutorAddr AllocAddr, SegInfoMap Segs)
43 : Parent(Parent), G(G), AllocAddr(AllocAddr), Segs(std::move(Segs)) {}
44
45 void finalize(OnFinalizedFunction OnFinalize) override {
47 for (auto &KV : Segs) {
48 assert(KV.second.ContentSize <= std::numeric_limits<size_t>::max());
50 KV.first,
51 KV.second.Addr,
52 alignTo(KV.second.ContentSize + KV.second.ZeroFillSize,
53 Parent.EPC.getPageSize()),
54 {KV.second.WorkingMem, static_cast<size_t>(KV.second.ContentSize)}});
55 }
56
57 // Transfer allocation actions.
58 std::swap(FR.Actions, G.allocActions());
59
60 Parent.EPC.callSPSWrapperAsync<
62 Parent.SAs.Initialize,
63 [OnFinalize = std::move(OnFinalize), AllocAddr = this->AllocAddr](
64 Error SerializationErr,
65 Expected<ExecutorAddr> InitializeKey) mutable {
66 // FIXME: Release abandoned alloc.
67 if (SerializationErr) {
68 cantFail(InitializeKey.takeError());
69 OnFinalize(std::move(SerializationErr));
70 } else if (!InitializeKey)
71 OnFinalize(InitializeKey.takeError());
72 else
73 OnFinalize(FinalizedAlloc(AllocAddr));
74 },
75 Parent.SAs.Allocator, std::move(FR));
76 }
77
78 void abandon(OnAbandonedFunction OnAbandoned) override {
79 // FIXME: Return memory to pool instead.
80 Parent.EPC.callSPSWrapperAsync<
82 Parent.SAs.Release,
83 [OnAbandoned = std::move(OnAbandoned)](Error SerializationErr,
84 Error DeallocateErr) mutable {
85 if (SerializationErr) {
86 cantFail(std::move(DeallocateErr));
87 OnAbandoned(std::move(SerializationErr));
88 } else
89 OnAbandoned(std::move(DeallocateErr));
90 },
91 Parent.SAs.Allocator, ArrayRef<ExecutorAddr>(AllocAddr));
92 }
93
94private:
96 LinkGraph &G;
97 ExecutorAddr AllocAddr;
98 SegInfoMap Segs;
99};
100
104 auto &ES = JD.getExecutionSession();
105 SymbolAddrs SAs;
106 if (auto Err = lookupAndRecordAddrs(
108 {
109 {ES.intern(SNs.AllocatorName), &SAs.Allocator},
110 {ES.intern(SNs.ReserveName), &SAs.Reserve},
111 {ES.intern(SNs.InitializeName), &SAs.Initialize},
112 {ES.intern(SNs.DeinitializeName), &SAs.Deinitialize},
113 {ES.intern(SNs.ReleaseName), &SAs.Release},
114 }))
115 return Err;
116 return std::make_unique<EPCGenericJITLinkMemoryManager>(
117 ES.getExecutorProcessControl(), SAs);
118}
119
125
127 LinkGraph &G,
128 OnAllocatedFunction OnAllocated) {
129 BasicLayout BL(G);
130
131 auto Pages = BL.getContiguousPageBasedLayoutSizes(EPC.getPageSize());
132 if (!Pages)
133 return OnAllocated(Pages.takeError());
134
136 SAs.Reserve,
137 [this, BL = std::move(BL), OnAllocated = std::move(OnAllocated)](
138 Error SerializationErr, Expected<ExecutorAddr> AllocAddr) mutable {
139 if (SerializationErr) {
140 cantFail(AllocAddr.takeError());
141 return OnAllocated(std::move(SerializationErr));
142 }
143 if (!AllocAddr)
144 return OnAllocated(AllocAddr.takeError());
145
146 completeAllocation(*AllocAddr, std::move(BL), std::move(OnAllocated));
147 },
148 SAs.Allocator, Pages->total());
149}
150
152 std::vector<FinalizedAlloc> Allocs, OnDeallocatedFunction OnDeallocated) {
154 SAs.Release,
155 [OnDeallocated = std::move(OnDeallocated)](Error SerErr,
156 Error DeallocErr) mutable {
157 if (SerErr) {
158 cantFail(std::move(DeallocErr));
159 OnDeallocated(std::move(SerErr));
160 } else
161 OnDeallocated(std::move(DeallocErr));
162 },
163 SAs.Allocator, Allocs);
164 for (auto &A : Allocs)
165 A.release();
166}
167
168void EPCGenericJITLinkMemoryManager::completeAllocation(
169 ExecutorAddr AllocAddr, BasicLayout BL, OnAllocatedFunction OnAllocated) {
170
172
173 ExecutorAddr NextSegAddr = AllocAddr;
174 for (auto &KV : BL.segments()) {
175 const auto &AG = KV.first;
176 auto &Seg = KV.second;
177
178 Seg.Addr = NextSegAddr;
179 KV.second.WorkingMem = BL.getGraph().allocateBuffer(Seg.ContentSize).data();
180 NextSegAddr += ExecutorAddrDiff(
181 alignTo(Seg.ContentSize + Seg.ZeroFillSize, EPC.getPageSize()));
182
183 auto &SegInfo = SegInfos[AG];
184 SegInfo.ContentSize = Seg.ContentSize;
185 SegInfo.ZeroFillSize = Seg.ZeroFillSize;
186 SegInfo.Addr = Seg.Addr;
187 SegInfo.WorkingMem = Seg.WorkingMem;
188 }
189
190 if (auto Err = BL.apply())
191 return OnAllocated(std::move(Err));
192
193 OnAllocated(std::make_unique<InFlightAlloc>(*this, BL.getGraph(), AllocAddr,
194 std::move(SegInfos)));
195}
196
197} // end namespace orc
198} // end namespace llvm
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
#define G(x, y, z)
Definition MD5.cpp:55
Lightweight error class with error context and mandatory checking.
Definition Error.h:159
Tagged union holding either a T or a Error.
Definition Error.h:485
A specialized small-map for AllocGroups.
void finalize(OnFinalizedFunction OnFinalize) override
Called to transfer working memory to the target and apply finalization.
void abandon(OnAbandonedFunction OnAbandoned) override
Called prior to finalization if the allocation should be abandoned.
InFlightAlloc(EPCGenericJITLinkMemoryManager &Parent, LinkGraph &G, ExecutorAddr AllocAddr, SegInfoMap Segs)
void deallocate(std::vector< FinalizedAlloc > Allocs, OnDeallocatedFunction OnDeallocated) override
Deallocate a list of allocation objects.
void allocate(const jitlink::JITLinkDylib *JD, jitlink::LinkGraph &G, OnAllocatedFunction OnAllocated) override
Start the allocation process.
static Expected< std::unique_ptr< EPCGenericJITLinkMemoryManager > > Create(JITDylib &JD, rt::SimpleExecutorMemoryManagerSymbolNames SNs=rt::orc_rt_SimpleNativeMemoryMapSPSSymbols)
Create an EPCGenericJITLinkMemoryManager using the given implementation symbol names.
EPCGenericJITLinkMemoryManager(ExecutorProcessControl &EPC, SymbolAddrs SAs)
Create an EPCGenericJITLinkMemoryManager instance from a given set of function addrs.
An ExecutionSession represents a running JIT program.
Definition Core.h:1355
JITDylib & getBootstrapJITDylib()
Returns a reference to the bootstrap JITDylib.
Definition Core.h:1416
Represents an address in the executor process.
unsigned getPageSize() const
Get the page size for the target process.
Represents a JIT'd dynamic library.
Definition Core.h:919
ExecutionSession & getExecutionSession() const
Get a reference to the ExecutionSession for this JITDylib.
Definition Core.h:938
shared::SPSExpected< shared::SPSExecutorAddr >(shared::SPSExecutorAddr, uint64_t) SPSSimpleExecutorMemoryManagerReserveSignature
Definition OrcRTBridge.h:91
shared::SPSExpected< shared::SPSExecutorAddr >(shared::SPSExecutorAddr, shared::SPSFinalizeRequest) SPSSimpleExecutorMemoryManagerInitializeSignature
Definition OrcRTBridge.h:94
shared::SPSError( shared::SPSExecutorAddr, shared::SPSSequence< shared::SPSExecutorAddr >) SPSSimpleExecutorMemoryManagerReleaseSignature
Definition OrcRTBridge.h:99
JITDylibSearchOrder makeJITDylibSearchOrder(ArrayRef< JITDylib * > JDs, JITDylibLookupFlags Flags=JITDylibLookupFlags::MatchExportedSymbolsOnly)
Convenience function for creating a search order from an ArrayRef of JITDylib*, all with the same fla...
Definition Core.h:182
uint64_t ExecutorAddrDiff
LLVM_ABI void lookupAndRecordAddrs(unique_function< void(Error)> OnRecorded, ExecutionSession &ES, LookupKind K, const JITDylibSearchOrder &SearchOrder, std::vector< std::pair< SymbolStringPtr, ExecutorAddr * > > Pairs, SymbolLookupFlags LookupFlags=SymbolLookupFlags::RequiredSymbol)
Record addresses of the given symbols in the given ExecutorAddrs.
This is an optimization pass for GlobalISel generic memory operations.
constexpr uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
Definition Alignment.h:144
void cantFail(Error Err, const char *Msg=nullptr)
Report a fatal error if Err is a failure value.
Definition Error.h:769
ArrayRef(const T &OneElt) -> ArrayRef< T >
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1916
Implement std::hash so that hash_code can be used in STL containers.
Definition BitVector.h:870
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition BitVector.h:872
Symbol addresses for memory management implementation.
Symbol names for memory management implementation.
Definition OrcRTBridge.h:70
std::vector< SegFinalizeRequest > Segments