LLVM 24.0.0git
SimpleExecutorMemoryManager.cpp
Go to the documentation of this file.
1//===- SimpleExecuorMemoryManagare.cpp - Simple executor-side memory mgmt -===//
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
11#include "llvm/ADT/ScopeExit.h"
14
15#define DEBUG_TYPE "orc"
16
17namespace llvm {
18namespace orc {
19namespace rt_bootstrap {
20
22 assert(Slabs.empty() && "shutdown not called?");
23}
24
26 std::error_code EC;
29 if (EC)
30 return errorCodeToError(EC);
31 std::lock_guard<std::mutex> Lock(M);
32 assert(!Slabs.count(MB.base()) && "Duplicate allocation addr");
33 Slabs[MB.base()].Size = Size;
34 return ExecutorAddr::fromPtr(MB.base());
35}
36
39 if (FR.Segments.empty()) {
40 if (FR.Actions.empty())
41 return make_error<StringError>("Finalization request is empty",
43 else
44 return make_error<StringError>("Finalization actions attached to empty "
45 "finalization request",
47 }
48
49 ExecutorAddrRange RR(FR.Segments.front().Addr, FR.Segments.front().Addr);
50
51 std::vector<sys::MemoryBlock> MBsToReset;
52 llvm::scope_exit ResetMBs([&]() {
53 for (auto &MB : MBsToReset)
57 RR.size());
58 });
59
60 // Copy content and apply permissions.
61 for (auto &Seg : FR.Segments) {
62 RR.Start = std::min(RR.Start, Seg.Addr);
63 RR.End = std::max(RR.End, Seg.Addr + Seg.Size);
64
65 // Check segment ranges.
66 if (LLVM_UNLIKELY(Seg.Size < Seg.Content.size()))
68 formatv("Segment {0:x} content size ({1:x} bytes) "
69 "exceeds segment size ({2:x} bytes)",
70 Seg.Addr.getValue(), Seg.Content.size(), Seg.Size),
72 ExecutorAddr SegEnd = Seg.Addr + ExecutorAddrDiff(Seg.Size);
73 if (LLVM_UNLIKELY(Seg.Addr < RR.Start || SegEnd > RR.End))
75 formatv("Segment {0:x} -- {1:x} crosses boundary of "
76 "allocation {2:x} -- {3:x}",
77 Seg.Addr, SegEnd, RR.Start, RR.End),
79
80 char *Mem = Seg.Addr.toPtr<char *>();
81 if (!Seg.Content.empty())
82 memcpy(Mem, Seg.Content.data(), Seg.Content.size());
83 memset(Mem + Seg.Content.size(), 0, Seg.Size - Seg.Content.size());
84 assert(Seg.Size <= std::numeric_limits<size_t>::max());
85
86 sys::MemoryBlock MB(Mem, Seg.Size);
88 MB, toSysMemoryProtectionFlags(Seg.RAG.Prot)))
89 return errorCodeToError(EC);
90
91 MBsToReset.push_back(MB);
92
93 if ((Seg.RAG.Prot & MemProt::Exec) == MemProt::Exec)
95 }
96
97 auto DeallocActions = runFinalizeActions(FR.Actions);
98 if (!DeallocActions)
99 return DeallocActions.takeError();
100
101 {
102 std::lock_guard<std::mutex> Lock(M);
103 auto Region = createRegionInfo(RR, "In initialize");
104 if (!Region)
105 return Region.takeError();
106 Region->DeallocActions = std::move(*DeallocActions);
107 }
108
109 // Successful initialization.
110 ResetMBs.release();
111
112 return RR.Start;
113}
114
116 const std::vector<ExecutorAddr> &InitKeys) {
117 Error Err = Error::success();
118
119 for (auto &KeyAddr : llvm::reverse(InitKeys)) {
120 std::vector<shared::WrapperFunctionCall> DeallocActions;
121 {
122 std::scoped_lock<std::mutex> Lock(M);
123 auto Slab = getSlabInfo(KeyAddr, "In deinitialize");
124 if (!Slab) {
125 Err = joinErrors(std::move(Err), Slab.takeError());
126 continue;
127 }
128
129 auto RI = getRegionInfo(*Slab, KeyAddr, "In deinitialize");
130 if (!RI) {
131 Err = joinErrors(std::move(Err), RI.takeError());
132 continue;
133 }
134
135 DeallocActions = std::move(RI->DeallocActions);
136 }
137
138 Err = joinErrors(std::move(Err),
139 runDeallocActions(std::move(DeallocActions)));
140 }
141
142 return Err;
143}
144
146 const std::vector<ExecutorAddr> &Bases) {
147 Error Err = Error::success();
148
149 // TODO: Prohibit new initializations within the slabs being removed?
150 for (auto &Base : llvm::reverse(Bases)) {
151 std::vector<shared::WrapperFunctionCall> DeallocActions;
153
154 {
155 std::scoped_lock<std::mutex> Lock(M);
156
157 auto SlabI = Slabs.find(Base.toPtr<void *>());
158 if (SlabI == Slabs.end()) {
159 Err = joinErrors(
160 std::move(Err),
161 make_error<StringError>("In release, " + formatv("{0:x}", Base) +
162 " is not part of any reserved "
163 "address range",
165 continue;
166 }
167
168 auto &Slab = SlabI->second;
169
170 for (auto &[Addr, Region] : Slab.Regions)
171 llvm::copy(Region.DeallocActions, back_inserter(DeallocActions));
172
173 MB = {Base.toPtr<void *>(), Slab.Size};
174
175 Slabs.erase(SlabI);
176 }
177
178 Err = joinErrors(std::move(Err), runDeallocActions(DeallocActions));
179 if (auto EC = sys::Memory::releaseMappedMemory(MB))
180 Err = joinErrors(std::move(Err), errorCodeToError(EC));
181 }
182
183 return Err;
184}
185
187
188 // TODO: Prevent new allocations during shutdown.
189 std::vector<ExecutorAddr> Bases;
190 {
191 std::scoped_lock<std::mutex> Lock(M);
192 for (auto &[Base, Slab] : Slabs)
193 Bases.push_back(ExecutorAddr::fromPtr(Base));
194 }
195
196 return release(Bases);
197}
198
201 namespace sps_ci = rt::sps_ci;
202 M[sps_ci::SimpleNativeMemoryMapInstanceName] = ExecutorAddr::fromPtr(this);
203 M[sps_ci::MemMgrReserve::Name] = ExecutorAddr::fromPtr(reserveWrapper);
204 M[sps_ci::MemMgrInitialize::Name] = ExecutorAddr::fromPtr(initializeWrapper);
205 M[sps_ci::MemMgrDeinitialize::Name] =
206 ExecutorAddr::fromPtr(deinitializeWrapper);
207 M[sps_ci::MemMgrRelease::Name] = ExecutorAddr::fromPtr(releaseWrapper);
208}
209
211SimpleExecutorMemoryManager::getSlabInfo(ExecutorAddr A, StringRef Context) {
212 auto MakeBadSlabError = [&]() {
214 Context + ", address " + formatv("{0:x}", A) +
215 " is not part of any reserved address range",
217 };
218
219 auto I = Slabs.upper_bound(A.toPtr<void *>());
220 if (I == Slabs.begin())
221 return MakeBadSlabError();
222 --I;
223 if (!ExecutorAddrRange(ExecutorAddr::fromPtr(I->first), I->second.Size)
224 .contains(A))
225 return MakeBadSlabError();
226
227 return I->second;
228}
229
231SimpleExecutorMemoryManager::getSlabInfo(ExecutorAddrRange R,
232 StringRef Context) {
233 auto MakeBadSlabError = [&]() {
235 Context + ", range " + formatv("{0:x}", R) +
236 " is not part of any reserved address range",
238 };
239
240 auto I = Slabs.upper_bound(R.Start.toPtr<void *>());
241 if (I == Slabs.begin())
242 return MakeBadSlabError();
243 --I;
244 if (!ExecutorAddrRange(ExecutorAddr::fromPtr(I->first), I->second.Size)
245 .contains(R))
246 return MakeBadSlabError();
247
248 return I->second;
249}
250
251Expected<SimpleExecutorMemoryManager::RegionInfo &>
252SimpleExecutorMemoryManager::createRegionInfo(ExecutorAddrRange R,
253 StringRef Context) {
254
255 auto Slab = getSlabInfo(R, Context);
256 if (!Slab)
257 return Slab.takeError();
258
259 auto MakeBadRegionError = [&](ExecutorAddrRange Other, bool Prev) {
260 return make_error<StringError>(Context + ", region " + formatv("{0:x}", R) +
261 " overlaps " +
262 (Prev ? "previous" : "following") +
263 " region " + formatv("{0:x}", Other),
265 };
266
267 auto I = Slab->Regions.upper_bound(R.Start);
268 if (I != Slab->Regions.begin()) {
269 auto J = std::prev(I);
270 ExecutorAddrRange PrevRange(J->first, J->second.Size);
271 if (PrevRange.overlaps(R))
272 return MakeBadRegionError(PrevRange, true);
273 }
274 if (I != Slab->Regions.end()) {
275 ExecutorAddrRange NextRange(I->first, I->second.Size);
276 if (NextRange.overlaps(R))
277 return MakeBadRegionError(NextRange, false);
278 }
279
280 auto &RInfo = Slab->Regions[R.Start];
281 RInfo.Size = R.size();
282 return RInfo;
283}
284
285Expected<SimpleExecutorMemoryManager::RegionInfo &>
286SimpleExecutorMemoryManager::getRegionInfo(SlabInfo &Slab, ExecutorAddr A,
287 StringRef Context) {
288 auto I = Slab.Regions.find(A);
289 if (I == Slab.Regions.end())
291 Context + ", address " + formatv("{0:x}", A) +
292 " does not correspond to the start of any initialized region",
294
295 return I->second;
296}
297
298Expected<SimpleExecutorMemoryManager::RegionInfo &>
299SimpleExecutorMemoryManager::getRegionInfo(ExecutorAddr A, StringRef Context) {
300 auto Slab = getSlabInfo(A, Context);
301 if (!Slab)
302 return Slab.takeError();
303
304 return getRegionInfo(*Slab, A, Context);
305}
306
307llvm::orc::shared::CWrapperFunctionBuffer
308SimpleExecutorMemoryManager::reserveWrapper(const char *ArgData,
309 size_t ArgSize) {
310 return shared::WrapperFunction<rt::sps_ci::MemMgrReserve::SPSSig>::handle(
311 ArgData, ArgSize,
314 .release();
315}
316
317llvm::orc::shared::CWrapperFunctionBuffer
318SimpleExecutorMemoryManager::initializeWrapper(const char *ArgData,
319 size_t ArgSize) {
320 return shared::WrapperFunction<rt::sps_ci::MemMgrInitialize::SPSSig>::handle(
321 ArgData, ArgSize,
324 .release();
325}
326
327llvm::orc::shared::CWrapperFunctionBuffer
328SimpleExecutorMemoryManager::deinitializeWrapper(const char *ArgData,
329 size_t ArgSize) {
330 return shared::WrapperFunction<rt::sps_ci::MemMgrDeinitialize::SPSSig>::
331 handle(ArgData, ArgSize,
334 .release();
335}
336
337llvm::orc::shared::CWrapperFunctionBuffer
338SimpleExecutorMemoryManager::releaseWrapper(const char *ArgData,
339 size_t ArgSize) {
340 return shared::WrapperFunction<rt::sps_ci::MemMgrRelease::SPSSig>::handle(
341 ArgData, ArgSize,
344 .release();
345}
346
347} // namespace rt_bootstrap
348} // end namespace orc
349} // end namespace llvm
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
unsigned uint64_t
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
#define LLVM_UNLIKELY(EXPR)
Definition Compiler.h:344
#define I(x, y, z)
Definition MD5.cpp:57
This file defines the scope_exit class, which executes user-defined cleanup logic at scope exit.
Lightweight error class with error context and mandatory checking.
Definition Error.h:159
static ErrorSuccess success()
Create a success value.
Definition Error.h:336
Tagged union holding either a T or a Error.
Definition Error.h:485
StringMap - This is an unconventional map that is specialized for handling keys that are "strings",...
Definition StringMap.h:129
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
Represents an address in the executor process.
static ExecutorAddr fromPtr(T *Ptr, UnwrapFn &&Unwrap=UnwrapFn())
Create an ExecutorAddr from the given pointer.
std::enable_if_t< std::is_pointer< T >::value, T > toPtr(WrapFn &&Wrap=WrapFn()) const
Cast this ExecutorAddr to a pointer of the given type.
Error deinitialize(const std::vector< ExecutorAddr > &InitKeys)
Error release(const std::vector< ExecutorAddr > &Bases)
Expected< ExecutorAddr > initialize(tpctypes::FinalizeRequest &FR)
void addBootstrapSymbols(StringMap< ExecutorAddr > &M) override
This class encapsulates the notion of a memory block which has an address and a size.
Definition Memory.h:33
static LLVM_ABI std::error_code protectMappedMemory(const MemoryBlock &Block, unsigned Flags)
This method sets the protection flags for a block of memory to the state specified by /p Flags.
static LLVM_ABI std::error_code releaseMappedMemory(MemoryBlock &Block)
This method releases a block of memory that was allocated with the allocateMappedMemory method.
static LLVM_ABI void InvalidateInstructionCache(const void *Addr, size_t Len)
InvalidateInstructionCache - Before the JIT can run a block of code that has been emitted it must inv...
static LLVM_ABI MemoryBlock allocateMappedMemory(size_t NumBytes, const MemoryBlock *const NearBlock, unsigned Flags, std::error_code &EC)
This method allocates a block of memory that is suitable for loading dynamically generated code (e....
MethodWrapperHandler< RetT, ClassT, ArgTs... > makeMethodWrapperHandler(RetT(ClassT::*Method)(ArgTs...))
Create a MethodWrapperHandler object from the given method pointer.
uint64_t ExecutorAddrDiff
sys::Memory::ProtectionFlags toSysMemoryProtectionFlags(MemProt MP)
Convert a MemProt value to a corresponding sys::Memory::ProtectionFlags value.
Definition MemoryFlags.h:44
This is an optimization pass for GlobalISel generic memory operations.
LLVM_ABI std::error_code inconvertibleErrorCode()
The value returned by this function can be returned from convertToErrorCode for Error values where no...
Definition Error.cpp:94
auto formatv(bool Validate, const char *Fmt, Ts &&...Vals)
auto reverse(ContainerTy &&C)
Definition STLExtras.h:407
Error joinErrors(Error E1, Error E2)
Concatenate errors.
Definition Error.h:442
Error make_error(ArgTs &&... Args)
Make a Error instance representing failure using the given error info type.
Definition Error.h:340
@ Other
Any other memory.
Definition ModRef.h:68
OutputIt copy(R &&Range, OutputIt Out)
Definition STLExtras.h:1885
LLVM_ABI Error errorCodeToError(std::error_code EC)
Helper for converting an std::error_code to a Error.
Definition Error.cpp:107
Represents an address range in the exceutor process.
ExecutorAddrDiff size() const
bool contains(ExecutorAddr Addr) const
std::vector< SegFinalizeRequest > Segments