LLVM  15.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 
17 using namespace llvm::jitlink;
18 
19 namespace llvm {
20 namespace orc {
21 
24 public:
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.
30  struct SegInfo {
31  SegInfo() : WorkingMem(nullptr), ContentSize(0), ZeroFillSize(0) {}
32 
33  char *WorkingMem;
37  };
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());
51  toSysMemoryProtectionFlags(KV.first.getMemProt())),
52  KV.second.Addr,
53  alignTo(KV.second.ContentSize + KV.second.ZeroFillSize,
54  Parent.EPC.getPageSize()),
55  {KV.second.WorkingMem, static_cast<size_t>(KV.second.ContentSize)}});
56  }
57 
58  // Transfer allocation actions.
59  std::swap(FR.Actions, G.allocActions());
60 
61  Parent.EPC.callSPSWrapperAsync<
63  Parent.SAs.Finalize,
64  [OnFinalize = std::move(OnFinalize), AllocAddr = this->AllocAddr](
65  Error SerializationErr, Error FinalizeErr) mutable {
66  // FIXME: Release abandoned alloc.
67  if (SerializationErr) {
68  cantFail(std::move(FinalizeErr));
69  OnFinalize(std::move(SerializationErr));
70  } else if (FinalizeErr)
71  OnFinalize(std::move(FinalizeErr));
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.Deallocate,
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 
94 private:
96  LinkGraph &G;
97  ExecutorAddr AllocAddr;
98  SegInfoMap Segs;
99 };
100 
101 void EPCGenericJITLinkMemoryManager::allocate(const JITLinkDylib *JD,
102  LinkGraph &G,
103  OnAllocatedFunction OnAllocated) {
104  BasicLayout BL(G);
105 
106  auto Pages = BL.getContiguousPageBasedLayoutSizes(EPC.getPageSize());
107  if (!Pages)
108  return OnAllocated(Pages.takeError());
109 
111  SAs.Reserve,
112  [this, BL = std::move(BL), OnAllocated = std::move(OnAllocated)](
113  Error SerializationErr, Expected<ExecutorAddr> AllocAddr) mutable {
114  if (SerializationErr) {
115  cantFail(AllocAddr.takeError());
116  return OnAllocated(std::move(SerializationErr));
117  }
118  if (!AllocAddr)
119  return OnAllocated(AllocAddr.takeError());
120 
121  completeAllocation(*AllocAddr, std::move(BL), std::move(OnAllocated));
122  },
123  SAs.Allocator, Pages->total());
124 }
125 
126 void EPCGenericJITLinkMemoryManager::deallocate(
127  std::vector<FinalizedAlloc> Allocs, OnDeallocatedFunction OnDeallocated) {
128  EPC.callSPSWrapperAsync<
130  SAs.Deallocate,
131  [OnDeallocated = std::move(OnDeallocated)](Error SerErr,
132  Error DeallocErr) mutable {
133  if (SerErr) {
134  cantFail(std::move(DeallocErr));
135  OnDeallocated(std::move(SerErr));
136  } else
137  OnDeallocated(std::move(DeallocErr));
138  },
139  SAs.Allocator, Allocs);
140  for (auto &A : Allocs)
141  A.release();
142 }
143 
144 void EPCGenericJITLinkMemoryManager::completeAllocation(
145  ExecutorAddr AllocAddr, BasicLayout BL, OnAllocatedFunction OnAllocated) {
146 
147  InFlightAlloc::SegInfoMap SegInfos;
148 
149  ExecutorAddr NextSegAddr = AllocAddr;
150  for (auto &KV : BL.segments()) {
151  const auto &AG = KV.first;
152  auto &Seg = KV.second;
153 
154  Seg.Addr = NextSegAddr;
155  KV.second.WorkingMem = BL.getGraph().allocateBuffer(Seg.ContentSize).data();
156  NextSegAddr += ExecutorAddrDiff(
157  alignTo(Seg.ContentSize + Seg.ZeroFillSize, EPC.getPageSize()));
158 
159  auto &SegInfo = SegInfos[AG];
160  SegInfo.ContentSize = Seg.ContentSize;
161  SegInfo.ZeroFillSize = Seg.ZeroFillSize;
162  SegInfo.Addr = ExecutorAddr(Seg.Addr);
163  SegInfo.WorkingMem = Seg.WorkingMem;
164  }
165 
166  if (auto Err = BL.apply())
167  return OnAllocated(std::move(Err));
168 
169  OnAllocated(std::make_unique<InFlightAlloc>(*this, BL.getGraph(), AllocAddr,
170  std::move(SegInfos)));
171 }
172 
173 } // end namespace orc
174 } // end namespace llvm
llvm::orc::ExecutorAddr
Represents an address in the executor process.
Definition: ExecutorAddress.h:30
llvm::orc::tpctypes::toWireProtectionFlags
WireProtectionFlags toWireProtectionFlags(sys::Memory::ProtectionFlags PF)
Convert from sys::Memory::ProtectionFlags.
Definition: TargetProcessControlTypes.h:42
llvm::alignTo
uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
Definition: Alignment.h:148
llvm::orc::EPCGenericJITLinkMemoryManager::InFlightAlloc::SegInfo::WorkingMem
char * WorkingMem
Definition: EPCGenericJITLinkMemoryManager.cpp:33
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:17
llvm::unique_function
unique_function is a type-erasing functor similar to std::function.
Definition: FunctionExtras.h:56
llvm::orc::EPCGenericJITLinkMemoryManager::InFlightAlloc::SegInfo
Definition: EPCGenericJITLinkMemoryManager.cpp:30
llvm::orc::rt::SPSSimpleExecutorMemoryManagerFinalizeSignature
shared::SPSError(shared::SPSExecutorAddr, shared::SPSFinalizeRequest) SPSSimpleExecutorMemoryManagerFinalizeSignature
Definition: OrcRTBridge.h:57
llvm::Expected
Tagged union holding either a T or a Error.
Definition: APFloat.h:41
llvm::orc::tpctypes::SegFinalizeRequest
Definition: TargetProcessControlTypes.h:73
EPCGenericJITLinkMemoryManager.h
llvm::orc::tpctypes::FinalizeRequest::Actions
shared::AllocActions Actions
Definition: TargetProcessControlTypes.h:82
llvm::ARCISD::BL
@ BL
Definition: ARCISelLowering.h:34
llvm::orc::tpctypes::FinalizeRequest
Definition: TargetProcessControlTypes.h:80
llvm::orc::EPCGenericJITLinkMemoryManager
Definition: EPCGenericJITLinkMemoryManager.h:27
llvm::orc::rt::SPSSimpleExecutorMemoryManagerReserveSignature
shared::SPSExpected< shared::SPSExecutorAddr >(shared::SPSExecutorAddr, uint64_t) SPSSimpleExecutorMemoryManagerReserveSignature
Definition: OrcRTBridge.h:55
llvm::orc::ExecutorAddrDiff
uint64_t ExecutorAddrDiff
Definition: ExecutorAddress.h:27
G
const DataFlowGraph & G
Definition: RDFGraph.cpp:200
uint64_t
move
compiles ldr LCPI1_0 ldr ldr mov lsr tst moveq r1 ldr LCPI1_1 and r0 bx lr It would be better to do something like to fold the shift into the conditional move
Definition: README.txt:546
llvm::orc::EPCGenericJITLinkMemoryManager::InFlightAlloc::SegInfo::ContentSize
uint64_t ContentSize
Definition: EPCGenericJITLinkMemoryManager.cpp:35
LookupAndRecordAddrs.h
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::move
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:1665
std::swap
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition: BitVector.h:853
llvm::orc::tpctypes::FinalizeRequest::Segments
std::vector< SegFinalizeRequest > Segments
Definition: TargetProcessControlTypes.h:81
llvm::orc::EPCGenericJITLinkMemoryManager::InFlightAlloc::abandon
void abandon(OnAbandonedFunction OnAbandoned) override
Called prior to finalization if the allocation should be abandoned.
Definition: EPCGenericJITLinkMemoryManager.cpp:78
llvm::ArrayRef
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: APInt.h:32
llvm::cantFail
void cantFail(Error Err, const char *Msg=nullptr)
Report a fatal error if Err is a failure value.
Definition: Error.h:745
llvm::orc::EPCGenericJITLinkMemoryManager::InFlightAlloc::SegInfo::ZeroFillSize
uint64_t ZeroFillSize
Definition: EPCGenericJITLinkMemoryManager.cpp:36
std
Definition: BitVector.h:851
llvm::Error
Lightweight error class with error context and mandatory checking.
Definition: Error.h:155
llvm::orc::EPCGenericJITLinkMemoryManager::InFlightAlloc::finalize
void finalize(OnFinalizedFunction OnFinalize) override
Called to transfer working memory to the target and apply finalization.
Definition: EPCGenericJITLinkMemoryManager.cpp:45
llvm::orc::EPCGenericJITLinkMemoryManager::InFlightAlloc
Definition: EPCGenericJITLinkMemoryManager.cpp:22
llvm::max
Align max(MaybeAlign Lhs, Align Rhs)
Definition: Alignment.h:340
llvm::orc::EPCGenericJITLinkMemoryManager::InFlightAlloc::SegInfo::SegInfo
SegInfo()
Definition: EPCGenericJITLinkMemoryManager.cpp:31
llvm::orc::EPCGenericJITLinkMemoryManager::InFlightAlloc::InFlightAlloc
InFlightAlloc(EPCGenericJITLinkMemoryManager &Parent, LinkGraph &G, ExecutorAddr AllocAddr, SegInfoMap Segs)
Definition: EPCGenericJITLinkMemoryManager.cpp:41
OrcRTBridge.h
llvm::orc::rt::SPSSimpleExecutorMemoryManagerDeallocateSignature
shared::SPSError(shared::SPSExecutorAddr, shared::SPSSequence< shared::SPSExecutorAddr >) SPSSimpleExecutorMemoryManagerDeallocateSignature
Definition: OrcRTBridge.h:59
llvm::orc::EPCGenericJITLinkMemoryManager::InFlightAlloc::SegInfo::Addr
ExecutorAddr Addr
Definition: EPCGenericJITLinkMemoryManager.cpp:34