16#define DEBUG_TYPE "orc"
20namespace rt_bootstrap {
23 assert(Slabs.empty() &&
"shutdown not called?");
32 std::lock_guard<std::mutex> Lock(M);
33 assert(!Slabs.count(MB.base()) &&
"Duplicate allocation addr");
34 Slabs[MB.base()].Size =
Size;
46 "finalization request",
52 std::vector<sys::MemoryBlock> MBsToReset;
54 for (
auto &MB : MBsToReset)
64 RR.
End = std::max(RR.
End, Seg.Addr + Seg.Size);
69 formatv(
"Segment {0:x} content size ({1:x} bytes) "
70 "exceeds segment size ({2:x} bytes)",
71 Seg.Addr.getValue(), Seg.Content.size(), Seg.Size),
76 formatv(
"Segment {0:x} -- {1:x} crosses boundary of "
77 "allocation {2:x} -- {3:x}",
81 char *Mem = Seg.Addr.toPtr<
char *>();
82 if (!Seg.Content.empty())
83 memcpy(Mem, Seg.Content.data(), Seg.Content.size());
84 memset(Mem + Seg.Content.size(), 0, Seg.Size - Seg.Content.size());
85 assert(Seg.Size <= std::numeric_limits<size_t>::max());
92 MBsToReset.push_back(MB);
98 auto DeallocActions = runFinalizeActions(FR.
Actions);
100 return DeallocActions.takeError();
103 std::lock_guard<std::mutex> Lock(M);
104 auto Region = createRegionInfo(RR,
"In initialize");
106 return Region.takeError();
107 Region->DeallocActions = std::move(*DeallocActions);
117 const std::vector<ExecutorAddr> &InitKeys) {
121 std::vector<shared::WrapperFunctionCall> DeallocActions;
123 std::scoped_lock<std::mutex> Lock(M);
124 auto Slab = getSlabInfo(KeyAddr,
"In deinitialize");
126 Err =
joinErrors(std::move(Err), Slab.takeError());
130 auto RI = getRegionInfo(*Slab, KeyAddr,
"In deinitialize");
132 Err =
joinErrors(std::move(Err), RI.takeError());
136 DeallocActions = std::move(RI->DeallocActions);
140 runDeallocActions(std::move(DeallocActions)));
147 const std::vector<ExecutorAddr> &Bases) {
152 std::vector<shared::WrapperFunctionCall> DeallocActions;
156 std::scoped_lock<std::mutex> Lock(M);
158 auto SlabI = Slabs.find(
Base.toPtr<
void *>());
159 if (SlabI == Slabs.end()) {
163 " is not part of any reserved "
169 auto &Slab = SlabI->second;
171 for (
auto &[Addr,
Region] : Slab.Regions)
174 MB = {
Base.toPtr<
void *>(), Slab.Size};
179 Err =
joinErrors(std::move(Err), runDeallocActions(DeallocActions));
190 std::vector<ExecutorAddr> Bases;
192 std::scoped_lock<std::mutex> Lock(M);
193 for (
auto &[
Base, Slab] : Slabs)
220 M[sps_ci::MemMgrInitialize::Name] =
222 M[sps_ci::MemMgrDeinitialize::Name] =
230 auto MakeBadSlabError = [&]() {
232 Context +
", address " +
formatv(
"{0:x}",
A) +
233 " is not part of any reserved address range",
237 auto I = Slabs.upper_bound(
A.toPtr<
void *>());
238 if (
I == Slabs.begin())
239 return MakeBadSlabError();
243 return MakeBadSlabError();
249SimpleExecutorMemoryManager::getSlabInfo(ExecutorAddrRange R,
251 auto MakeBadSlabError = [&]() {
253 Context +
", range " +
formatv(
"{0:x}", R) +
254 " is not part of any reserved address range",
258 auto I = Slabs.upper_bound(R.Start.toPtr<
void *>());
259 if (
I == Slabs.begin())
260 return MakeBadSlabError();
264 return MakeBadSlabError();
269Expected<SimpleExecutorMemoryManager::RegionInfo &>
270SimpleExecutorMemoryManager::createRegionInfo(ExecutorAddrRange R,
273 auto Slab = getSlabInfo(R,
Context);
275 return Slab.takeError();
277 auto MakeBadRegionError = [&](ExecutorAddrRange
Other,
bool Prev) {
280 (Prev ?
"previous" :
"following") +
285 auto I = Slab->Regions.upper_bound(
R.Start);
286 if (
I != Slab->Regions.begin()) {
287 auto J = std::prev(
I);
288 ExecutorAddrRange PrevRange(J->first, J->second.Size);
289 if (PrevRange.overlaps(R))
290 return MakeBadRegionError(PrevRange,
true);
292 if (
I != Slab->Regions.end()) {
293 ExecutorAddrRange NextRange(
I->first,
I->second.Size);
294 if (NextRange.overlaps(R))
295 return MakeBadRegionError(NextRange,
false);
298 auto &RInfo = Slab->Regions[
R.Start];
299 RInfo.Size =
R.size();
303Expected<SimpleExecutorMemoryManager::RegionInfo &>
304SimpleExecutorMemoryManager::getRegionInfo(SlabInfo &Slab, ExecutorAddr
A,
306 auto I = Slab.Regions.find(
A);
307 if (
I == Slab.Regions.end())
310 " does not correspond to the start of any initialized region",
316Expected<SimpleExecutorMemoryManager::RegionInfo &>
317SimpleExecutorMemoryManager::getRegionInfo(ExecutorAddr
A, StringRef
Context) {
318 auto Slab = getSlabInfo(
A,
Context);
320 return Slab.takeError();
322 return getRegionInfo(*Slab,
A,
Context);
325llvm::orc::shared::CWrapperFunctionBuffer
326SimpleExecutorMemoryManager::reserveWrapper(
const char *ArgData,
328 return shared::WrapperFunction<rt::sps_ci::MemMgrReserve::SPSSig>::handle(
335llvm::orc::shared::CWrapperFunctionBuffer
336SimpleExecutorMemoryManager::initializeWrapper(
const char *ArgData,
338 return shared::WrapperFunction<rt::sps_ci::MemMgrInitialize::SPSSig>::handle(
345llvm::orc::shared::CWrapperFunctionBuffer
346SimpleExecutorMemoryManager::deinitializeWrapper(
const char *ArgData,
348 return shared::WrapperFunction<rt::sps_ci::MemMgrDeinitialize::SPSSig>::
349 handle(ArgData, ArgSize,
355llvm::orc::shared::CWrapperFunctionBuffer
356SimpleExecutorMemoryManager::releaseWrapper(
const char *ArgData,
358 return shared::WrapperFunction<rt::sps_ci::MemMgrRelease::SPSSig>::handle(
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
#define LLVM_UNLIKELY(EXPR)
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.
static ErrorSuccess success()
Create a success value.
Tagged union holding either a T or a Error.
StringMap - This is an unconventional map that is specialized for handling keys that are "strings",...
Represent a constant reference to a string, i.e.
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)
~SimpleExecutorMemoryManager() override
Error shutdown() override
Expected< ExecutorAddr > reserve(uint64_t Size)
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.
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....
LLVM_ABI const char * SimpleExecutorMemoryManagerInitializeWrapperName
LLVM_ABI const char * SimpleExecutorMemoryManagerReserveWrapperName
LLVM_ABI const char * SimpleExecutorMemoryManagerReleaseWrapperName
LLVM_ABI const char * SimpleExecutorMemoryManagerDeinitializeWrapperName
LLVM_ABI const char * SimpleExecutorMemoryManagerInstanceName
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.
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...
auto formatv(bool Validate, const char *Fmt, Ts &&...Vals)
auto reverse(ContainerTy &&C)
Error joinErrors(Error E1, Error E2)
Concatenate errors.
Error make_error(ArgTs &&... Args)
Make a Error instance representing failure using the given error info type.
OutputIt copy(R &&Range, OutputIt Out)
LLVM_ABI Error errorCodeToError(std::error_code EC)
Helper for converting an std::error_code to a Error.
Represents an address range in the exceutor process.
ExecutorAddrDiff size() const
bool contains(ExecutorAddr Addr) const
std::vector< SegFinalizeRequest > Segments
shared::AllocActions Actions