14#define DEBUG_TYPE "jitlink"
26 for (
auto &Sec : G.sections()) {
28 if (Sec.blocks().empty() ||
29 Sec.getMemLifetime() == orc::MemLifetime::NoAlloc)
32 auto &Seg = Segments[{Sec.getMemProt(), Sec.getMemLifetime()}];
33 for (
auto *
B : Sec.blocks())
35 Seg.ContentBlocks.push_back(
B);
37 Seg.ZeroFillBlocks.push_back(
B);
41 auto CompareBlocks = [](
const Block *LHS,
const Block *RHS) {
43 if (LHS->getSection().getOrdinal() != RHS->getSection().getOrdinal())
44 return LHS->getSection().getOrdinal() < RHS->getSection().getOrdinal();
45 if (LHS->getAddress() != RHS->getAddress())
46 return LHS->getAddress() < RHS->getAddress();
47 return LHS->getSize() < RHS->getSize();
50 LLVM_DEBUG(
dbgs() <<
"Generated BasicLayout for " <<
G.getName() <<
":\n");
51 for (
auto &KV : Segments) {
52 auto &Seg = KV.second;
57 for (
auto *
B : Seg.ContentBlocks) {
59 Seg.ContentSize +=
B->getSize();
60 Seg.Alignment = std::max(Seg.Alignment,
Align(
B->getAlignment()));
63 uint64_t SegEndOffset = Seg.ContentSize;
64 for (
auto *
B : Seg.ZeroFillBlocks) {
66 SegEndOffset +=
B->getSize();
67 Seg.Alignment = std::max(Seg.Alignment,
Align(
B->getAlignment()));
69 Seg.ZeroFillSize = SegEndOffset - Seg.ContentSize;
72 dbgs() <<
" Seg " << KV.first
73 <<
": content-size=" <<
formatv(
"{0:x}", Seg.ContentSize)
74 <<
", zero-fill-size=" <<
formatv(
"{0:x}", Seg.ZeroFillSize)
75 <<
", align=" <<
formatv(
"{0:x}", Seg.Alignment.value()) <<
"\n";
86 auto &Seg = KV.second;
92 uint64_t SegSize =
alignTo(Seg.ContentSize + Seg.ZeroFillSize,
PageSize);
103 for (
auto &KV : Segments) {
104 auto &Seg = KV.second;
106 assert(!(Seg.ContentBlocks.empty() && Seg.ZeroFillBlocks.empty()) &&
107 "Empty section recorded?");
109 for (
auto *
B : Seg.ContentBlocks) {
112 Seg.NextWorkingMemOffset =
alignToBlock(Seg.NextWorkingMemOffset, *
B);
115 B->setAddress(Seg.Addr);
116 Seg.Addr +=
B->getSize();
120 memcpy(Seg.WorkingMem + Seg.NextWorkingMemOffset,
B->getContent().data(),
122 B->setMutableContent(
123 {Seg.WorkingMem + Seg.NextWorkingMemOffset,
B->getSize()});
124 Seg.NextWorkingMemOffset +=
B->getSize();
127 for (
auto *
B : Seg.ZeroFillBlocks) {
131 B->setAddress(Seg.Addr);
132 Seg.Addr +=
B->getSize();
135 Seg.ContentBlocks.clear();
136 Seg.ZeroFillBlocks.clear();
143 return G.allocActions();
147 std::shared_ptr<orc::SymbolStringPool> SSP,
153 "AllocGroup has changed. Section names below must be updated");
155 "__---.standard",
"__R--.standard",
"__-W-.standard",
"__RW-.standard",
156 "__--X.standard",
"__R-X.standard",
"__-WX.standard",
"__RWX.standard",
157 "__---.finalize",
"__R--.finalize",
"__-W-.finalize",
"__RW-.finalize",
158 "__--X.finalize",
"__R-X.finalize",
"__-WX.finalize",
"__RWX.finalize"};
160 unsigned PointerSize = TT.getArchPointerBitWidth() / 8;
161 auto G = std::make_unique<LinkGraph>(
"", std::move(SSP), std::move(TT),
167 for (
auto &KV : Segments) {
169 auto &Seg = KV.second;
172 "NoAlloc segments are not supported by SimpleSegmentAlloc");
175 AGSectionNames[
static_cast<unsigned>(AG.getMemProt()) |
176 static_cast<bool>(AG.getMemLifetime()) << 3];
178 auto &Sec = G->createSection(AGSectionName, AG.getMemProt());
179 Sec.setMemLifetime(AG.getMemLifetime());
181 if (Seg.ContentSize != 0) {
185 G->createMutableContentBlock(Sec, G->allocateBuffer(Seg.ContentSize),
186 NextAddr, Seg.ContentAlign.value(), 0);
187 ContentBlocks[AG] = &
B;
188 NextAddr += Seg.ContentSize;
195 [G = std::move(G), ContentBlocks = std::move(ContentBlocks),
196 OnCreated = std::move(OnCreated)](
199 OnCreated(Alloc.takeError());
202 std::move(ContentBlocks),
210 std::promise<MSVCPExpected<SimpleSegmentAlloc>> AllocP;
211 auto AllocF = AllocP.get_future();
212 Create(MemMgr, std::move(SSP), std::move(TT), JD, std::move(Segments),
214 AllocP.set_value(std::move(Result));
226 auto I = ContentBlocks.find(AG);
227 if (
I != ContentBlocks.end()) {
228 auto &
B = *
I->second;
229 return {
B.getAddress(),
B.getAlreadyMutableContent()};
235 std::unique_ptr<LinkGraph>
G,
237 std::unique_ptr<JITLinkMemoryManager::InFlightAlloc>
Alloc)
247 : MemMgr(MemMgr), G(&G), BL(
std::
move(BL)),
248 StandardSegments(
std::
move(StandardSegments)),
249 FinalizationSegments(
std::
move(FinalizationSegments)) {}
252 assert(!G &&
"InFlight alloc neither abandoned nor finalized");
258 if (
auto Err = applyProtections()) {
259 OnFinalized(std::move(Err));
264 auto DeallocActions = runFinalizeActions(G->allocActions());
265 if (!DeallocActions) {
266 OnFinalized(DeallocActions.takeError());
284 OnFinalized(MemMgr.createFinalizedAlloc(std::move(StandardSegments),
285 std::move(*DeallocActions)));
302 OnAbandoned(std::move(Err));
306 Error applyProtections() {
308 const auto &AG = KV.first;
309 auto &Seg = KV.second;
311 auto Prot = toSysMemoryProtectionFlags(AG.getMemProt());
314 alignTo(Seg.ContentSize + Seg.ZeroFillSize, MemMgr.PageSize);
337 "Could not create InProcessMemoryManager: Page size " +
338 Twine(*PageSize) +
" is not a power of 2",
341 return std::make_unique<InProcessMemoryManager>(*PageSize);
354 OnAllocated(SegsSizes.takeError());
360 if (SegsSizes->total() > std::numeric_limits<size_t>::max()) {
362 "Total requested size " +
formatv(
"{0:x}", SegsSizes->total()) +
363 " for graph " +
G.getName() +
" exceeds address space"));
395 StandardSegsMem = {Slab.
base(),
396 static_cast<size_t>(SegsSizes->StandardSegs)};
397 FinalizeSegsMem = {(
void *)((
char *)Slab.
base() + SegsSizes->StandardSegs),
398 static_cast<size_t>(SegsSizes->FinalizeSegs)};
405 dbgs() <<
"InProcessMemoryManager allocated:\n";
406 if (SegsSizes->StandardSegs)
407 dbgs() <<
formatv(
" [ {0:x16} -- {1:x16} ]", NextStandardSegAddr,
409 <<
" to stardard segs\n";
411 dbgs() <<
" no standard segs\n";
412 if (SegsSizes->FinalizeSegs)
413 dbgs() <<
formatv(
" [ {0:x16} -- {1:x16} ]", NextFinalizeSegAddr,
415 <<
" to finalize segs\n";
417 dbgs() <<
" no finalize segs\n";
423 auto &Seg = KV.second;
426 ? NextStandardSegAddr
427 : NextFinalizeSegAddr;
429 Seg.WorkingMem = SegAddr.toPtr<
char *>();
432 SegAddr +=
alignTo(Seg.ContentSize + Seg.ZeroFillSize, PageSize);
435 if (
auto Err = BL.
apply()) {
436 OnAllocated(std::move(Err));
440 OnAllocated(std::make_unique<IPInFlightAlloc>(*
this,
G, std::move(BL),
441 std::move(StandardSegsMem),
442 std::move(FinalizeSegsMem)));
447 std::vector<sys::MemoryBlock> StandardSegmentsList;
448 std::vector<std::vector<orc::shared::WrapperFunctionCall>> DeallocActionsList;
451 std::lock_guard<std::mutex> Lock(FinalizedAllocsMutex);
452 for (
auto &
Alloc : Allocs) {
453 auto *FA =
Alloc.release().toPtr<FinalizedAllocInfo *>();
454 StandardSegmentsList.push_back(std::move(FA->StandardSegments));
455 DeallocActionsList.push_back(std::move(FA->DeallocActions));
456 FA->~FinalizedAllocInfo();
457 FinalizedAllocInfos.Deallocate(FA);
463 while (!DeallocActionsList.empty()) {
464 auto &DeallocActions = DeallocActionsList.back();
465 auto &StandardSegments = StandardSegmentsList.back();
468 while (!DeallocActions.empty()) {
469 if (
auto Err = DeallocActions.back().runWithSPSRetErrorMerged())
470 DeallocErr =
joinErrors(std::move(DeallocErr), std::move(Err));
471 DeallocActions.pop_back();
478 DeallocActionsList.pop_back();
479 StandardSegmentsList.pop_back();
482 OnDeallocated(std::move(DeallocErr));
486InProcessMemoryManager::createFinalizedAlloc(
488 std::vector<orc::shared::WrapperFunctionCall> DeallocActions) {
489 std::lock_guard<std::mutex> Lock(FinalizedAllocsMutex);
490 auto *FA = FinalizedAllocInfos.Allocate<FinalizedAllocInfo>();
491 new (FA) FinalizedAllocInfo(
492 {std::move(StandardSegments), std::move(DeallocActions)});
for(const MachineOperand &MO :llvm::drop_begin(OldMI.operands(), Desc.getNumOperands()))
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
#define LLVM_LIKELY(EXPR)
static cl::opt< int > PageSize("imp-null-check-page-size", cl::desc("The page size of the target in bytes"), cl::init(4096), cl::Hidden)
Provides a library for accessing information about this process and other processes on the operating ...
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.
Error takeError()
Take ownership of the stored error.
Represent a constant reference to a string, i.e.
Manages the enabling and disabling of subtarget specific features.
Triple - Helper class for working with autoconf configuration names.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
BasicLayout simplifies the implementation of JITLinkMemoryManagers.
LLVM_ABI orc::shared::AllocActions & graphAllocActions()
Returns a reference to the AllocActions in the graph.
LLVM_ABI Error apply()
Apply the layout to the graph.
LLVM_ABI Expected< ContiguousPageBasedLayoutSizes > getContiguousPageBasedLayoutSizes(uint64_t PageSize)
Returns the total number of required to allocate all segments (with each segment padded out to page s...
iterator_range< SegmentMap::iterator > segments()
Returns an iterator over the segments of the layout.
LLVM_ABI BasicLayout(LinkGraph &G)
An Addressable with content and edges.
~IPInFlightAlloc() override
void abandon(OnAbandonedFunction OnAbandoned) override
Called prior to finalization if the allocation should be abandoned.
IPInFlightAlloc(InProcessMemoryManager &MemMgr, LinkGraph &G, BasicLayout BL, sys::MemoryBlock StandardSegments, sys::MemoryBlock FinalizationSegments)
void finalize(OnFinalizedFunction OnFinalized) override
Called to transfer working memory to the target and apply finalization.
static Expected< std::unique_ptr< InProcessMemoryManager > > Create()
Attempts to auto-detect the host page size.
void deallocate(std::vector< FinalizedAlloc > Alloc, OnDeallocatedFunction OnDeallocated) override
Deallocate a list of allocation objects.
void allocate(const JITLinkDylib *JD, LinkGraph &G, OnAllocatedFunction OnAllocated) override
Start the allocation process.
InProcessMemoryManager(uint64_t PageSize)
Create an instance using the given page size.
Represents a JITDylib as seen by JITLink.
Represents a finalized allocation.
Represents an allocation which has not been finalized yet.
unique_function< void(Error)> OnAbandonedFunction
unique_function< void(Expected< FinalizedAlloc >)> OnFinalizedFunction
Manages allocations of JIT memory.
virtual void allocate(const JITLinkDylib *JD, LinkGraph &G, OnAllocatedFunction OnAllocated)=0
Start the allocation process.
unique_function< void(AllocResult)> OnAllocatedFunction
Called when allocation has been completed.
unique_function< void(Error)> OnDeallocatedFunction
Called when deallocation has completed.
virtual ~JITLinkMemoryManager()
Expected< std::unique_ptr< InFlightAlloc > > AllocResult
Typedef for the argument to be passed to OnAllocatedFunction.
A utility class for making simple allocations using JITLinkMemoryManager.
unique_function< void(Expected< SimpleSegmentAlloc >)> OnCreatedFunction
LLVM_ABI ~SimpleSegmentAlloc()
orc::AllocGroupSmallMap< Segment > SegmentMap
LLVM_ABI SimpleSegmentAlloc(SimpleSegmentAlloc &&)
LLVM_ABI SegmentInfo getSegInfo(orc::AllocGroup AG)
Returns the SegmentInfo for the given group.
LLVM_ABI SimpleSegmentAlloc & operator=(SimpleSegmentAlloc &&)
static LLVM_ABI void Create(JITLinkMemoryManager &MemMgr, std::shared_ptr< orc::SymbolStringPool > SSP, Triple TT, const JITLinkDylib *JD, SegmentMap Segments, OnCreatedFunction OnCreated)
A specialized small-map for AllocGroups.
A pair of memory protections and allocation policies.
static constexpr unsigned NumGroups
Represents an address in the executor process.
uint64_t getValue() const
static ExecutorAddr fromPtr(T *Ptr, UnwrapFn &&Unwrap=UnwrapFn())
Create an ExecutorAddr from the given pointer.
This class encapsulates the notion of a memory block which has an address and a size.
size_t allocatedSize() const
The size as it was allocated.
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....
static LLVM_ABI Expected< unsigned > getPageSize()
Get the process's page size.
LLVM_ABI const char * getGenericEdgeKindName(Edge::Kind K)
Returns the string name of the given generic edge kind, or "unknown" otherwise.
uint64_t alignToBlock(uint64_t Addr, const Block &B)
std::vector< AllocActionCallPair > AllocActions
A vector of allocation actions to be run for this allocation.
@ NoAlloc
NoAlloc memory should not be allocated by the JITLinkMemoryManager at all.
@ Standard
Standard memory should be allocated by the allocator and then deallocated when the deallocate method ...
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...
constexpr bool isPowerOf2_64(uint64_t Value)
Return true if the argument is a power of two > 0 (64 bit edition.)
auto formatv(bool Validate, const char *Fmt, Ts &&...Vals)
Error joinErrors(Error E1, Error E2)
Concatenate errors.
void sort(IteratorTy Start, IteratorTy End)
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
constexpr uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
Error make_error(ArgTs &&... Args)
Make a Error instance representing failure using the given error info type.
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
LLVM_ABI Error errorCodeToError(std::error_code EC)
Helper for converting an std::error_code to a Error.
Implement std::hash so that hash_code can be used in STL containers.
This struct is a compact representation of a valid (non-zero power of two) alignment.
A convenience class that further groups segments based on memory deallocation policy.
Describes the segment working memory and executor address.