Skip to content

Commit 4374ca3

Browse files
authored
[ORC] Align ExecutorSimpleMemoryManager w/ orc_rt::SimpleNativeMemoryMap (#163693)
Teach ExecutorSimpleMemoryManager to handle slab reserve/release operations, plus separate initialize/deinitialize for regions within the slab. The release operation automatically deinitializes any regions within each slab that have not already been released. EPCGenericJITLinkMemoryManager is updated to use the reserve (allocate), initialize (finalize), and relesae (deallocate) operations. This brings ExecutorSimpleMemoryManager into alignment with the orc_rt::SimpleNativeMemoryMap class, allowing SimpleNativeMemoryMap to be used as a backend for EPCGenericJITLinkMemoryManager. A future commit will introduce a new MemoryMapper class that will make SimpleNativeMemoryMap usable as a backend for MapperJITLinkMemoryManager. This work will make it easier to re-use in-tree APIs and tools with the new ORC runtime.
1 parent 20fdd53 commit 4374ca3

File tree

11 files changed

+351
-205
lines changed

11 files changed

+351
-205
lines changed

llvm/include/llvm/ExecutionEngine/Orc/EPCGenericJITLinkMemoryManager.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,9 @@ class LLVM_ABI EPCGenericJITLinkMemoryManager
3232
struct SymbolAddrs {
3333
ExecutorAddr Allocator;
3434
ExecutorAddr Reserve;
35-
ExecutorAddr Finalize;
36-
ExecutorAddr Deallocate;
35+
ExecutorAddr Initialize;
36+
ExecutorAddr Deinitialize;
37+
ExecutorAddr Release;
3738
};
3839

3940
/// Create an EPCGenericJITLinkMemoryManager instance from a given set of

llvm/include/llvm/ExecutionEngine/Orc/EPCGenericRTDyldMemoryManager.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ class LLVM_ABI EPCGenericRTDyldMemoryManager
3131
struct SymbolAddrs {
3232
ExecutorAddr Instance;
3333
ExecutorAddr Reserve;
34-
ExecutorAddr Finalize;
35-
ExecutorAddr Deallocate;
34+
ExecutorAddr Initialize;
35+
ExecutorAddr Release;
3636
ExecutorAddr RegisterEHFrame;
3737
ExecutorAddr DeregisterEHFrame;
3838
};

llvm/include/llvm/ExecutionEngine/Orc/Shared/OrcRTBridge.h

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,9 @@ LLVM_ABI extern const char *SimpleExecutorDylibManagerResolveWrapperName;
2929

3030
LLVM_ABI extern const char *SimpleExecutorMemoryManagerInstanceName;
3131
LLVM_ABI extern const char *SimpleExecutorMemoryManagerReserveWrapperName;
32-
LLVM_ABI extern const char *SimpleExecutorMemoryManagerFinalizeWrapperName;
33-
LLVM_ABI extern const char *SimpleExecutorMemoryManagerDeallocateWrapperName;
32+
LLVM_ABI extern const char *SimpleExecutorMemoryManagerInitializeWrapperName;
33+
LLVM_ABI extern const char *SimpleExecutorMemoryManagerDeinitializeWrapperName;
34+
LLVM_ABI extern const char *SimpleExecutorMemoryManagerReleaseWrapperName;
3435

3536
LLVM_ABI extern const char *ExecutorSharedMemoryMapperServiceInstanceName;
3637
LLVM_ABI extern const char *ExecutorSharedMemoryMapperServiceReserveWrapperName;
@@ -73,9 +74,12 @@ using SPSSimpleExecutorDylibManagerResolveSignature = shared::SPSExpected<
7374
using SPSSimpleExecutorMemoryManagerReserveSignature =
7475
shared::SPSExpected<shared::SPSExecutorAddr>(shared::SPSExecutorAddr,
7576
uint64_t);
76-
using SPSSimpleExecutorMemoryManagerFinalizeSignature =
77-
shared::SPSError(shared::SPSExecutorAddr, shared::SPSFinalizeRequest);
78-
using SPSSimpleExecutorMemoryManagerDeallocateSignature = shared::SPSError(
77+
using SPSSimpleExecutorMemoryManagerInitializeSignature =
78+
shared::SPSExpected<shared::SPSExecutorAddr>(shared::SPSExecutorAddr,
79+
shared::SPSFinalizeRequest);
80+
using SPSSimpleExecutorMemoryManagerDeinitializeSignature = shared::SPSError(
81+
shared::SPSExecutorAddr, shared::SPSSequence<shared::SPSExecutorAddr>);
82+
using SPSSimpleExecutorMemoryManagerReleaseSignature = shared::SPSError(
7983
shared::SPSExecutorAddr, shared::SPSSequence<shared::SPSExecutorAddr>);
8084

8185
// ExecutorSharedMemoryMapperService
@@ -93,6 +97,18 @@ using SPSExecutorSharedMemoryMapperServiceDeinitializeSignature =
9397
using SPSExecutorSharedMemoryMapperServiceReleaseSignature = shared::SPSError(
9498
shared::SPSExecutorAddr, shared::SPSSequence<shared::SPSExecutorAddr>);
9599

100+
// SimpleNativeMemoryMap APIs.
101+
using SPSSimpleRemoteMemoryMapReserveSignature =
102+
shared::SPSExpected<shared::SPSExecutorAddr>(shared::SPSExecutorAddr,
103+
uint64_t);
104+
using SPSSimpleRemoteMemoryMapInitializeSignature =
105+
shared::SPSExpected<shared::SPSExecutorAddr>(shared::SPSExecutorAddr,
106+
shared::SPSFinalizeRequest);
107+
using SPSSimpleRemoteMemoryMapDeinitializeSignature = shared::SPSError(
108+
shared::SPSExecutorAddr, shared::SPSSequence<shared::SPSExecutorAddr>);
109+
using SPSSimpleRemoteMemoryMapReleaseSignature = shared::SPSError(
110+
shared::SPSExecutorAddr, shared::SPSSequence<shared::SPSExecutorAddr>);
111+
96112
using SPSRunAsMainSignature = int64_t(shared::SPSExecutorAddr,
97113
shared::SPSSequence<shared::SPSString>);
98114
using SPSRunAsVoidFunctionSignature = int32_t(shared::SPSExecutorAddr);

llvm/include/llvm/ExecutionEngine/Orc/TargetProcess/SimpleExecutorMemoryManager.h

Lines changed: 41 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,34 +34,65 @@ class LLVM_ABI SimpleExecutorMemoryManager : public ExecutorBootstrapService {
3434
public:
3535
virtual ~SimpleExecutorMemoryManager();
3636

37-
Expected<ExecutorAddr> allocate(uint64_t Size);
38-
Error finalize(tpctypes::FinalizeRequest &FR);
39-
Error deallocate(const std::vector<ExecutorAddr> &Bases);
37+
Expected<ExecutorAddr> reserve(uint64_t Size);
38+
Expected<ExecutorAddr> initialize(tpctypes::FinalizeRequest &FR);
39+
Error deinitialize(const std::vector<ExecutorAddr> &InitKeys);
40+
Error release(const std::vector<ExecutorAddr> &Bases);
4041

4142
Error shutdown() override;
4243
void addBootstrapSymbols(StringMap<ExecutorAddr> &M) override;
4344

4445
private:
45-
struct Allocation {
46+
struct RegionInfo {
4647
size_t Size = 0;
47-
std::vector<shared::WrapperFunctionCall> DeallocationActions;
48+
std::vector<shared::WrapperFunctionCall> DeallocActions;
4849
};
4950

50-
using AllocationsMap = DenseMap<void *, Allocation>;
51+
struct SlabInfo {
52+
using RegionMap = std::map<ExecutorAddr, RegionInfo>;
53+
size_t Size = 0;
54+
RegionMap Regions;
55+
};
56+
57+
using SlabMap = std::map<void *, SlabInfo>;
58+
59+
/// Get a reference to the slab information for the slab containing the given
60+
/// address.
61+
Expected<SlabInfo &> getSlabInfo(ExecutorAddr A, StringRef Context);
62+
63+
/// Get a reference to the slab information for the slab *covering* the given
64+
/// range. The given range must be a subrange of e(possibly equal to) the
65+
/// range of the slab itself.
66+
Expected<SlabInfo &> getSlabInfo(ExecutorAddrRange R, StringRef Context);
5167

52-
Error deallocateImpl(void *Base, Allocation &A);
68+
/// Create a RegionInfo for the given range, which must not overlap any
69+
/// existing region.
70+
Expected<RegionInfo &> createRegionInfo(ExecutorAddrRange R,
71+
StringRef Context);
72+
73+
/// Get a reference to the region information for the given address. This
74+
/// address must represent the start of an existing initialized region.
75+
Expected<RegionInfo &> getRegionInfo(SlabInfo &Slab, ExecutorAddr A,
76+
StringRef Context);
77+
78+
/// Get a reference to the region information for the given address. This
79+
/// address must represent the start of an existing initialized region.
80+
Expected<RegionInfo &> getRegionInfo(ExecutorAddr A, StringRef Context);
5381

5482
static llvm::orc::shared::CWrapperFunctionResult
5583
reserveWrapper(const char *ArgData, size_t ArgSize);
5684

5785
static llvm::orc::shared::CWrapperFunctionResult
58-
finalizeWrapper(const char *ArgData, size_t ArgSize);
86+
initializeWrapper(const char *ArgData, size_t ArgSize);
87+
88+
static llvm::orc::shared::CWrapperFunctionResult
89+
deinitializeWrapper(const char *ArgData, size_t ArgSize);
5990

6091
static llvm::orc::shared::CWrapperFunctionResult
61-
deallocateWrapper(const char *ArgData, size_t ArgSize);
92+
releaseWrapper(const char *ArgData, size_t ArgSize);
6293

6394
std::mutex M;
64-
AllocationsMap Allocations;
95+
SlabMap Slabs;
6596
};
6697

6798
} // end namespace rt_bootstrap

llvm/lib/ExecutionEngine/Orc/EPCGenericJITLinkMemoryManager.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -57,16 +57,17 @@ class EPCGenericJITLinkMemoryManager::InFlightAlloc
5757
std::swap(FR.Actions, G.allocActions());
5858

5959
Parent.EPC.callSPSWrapperAsync<
60-
rt::SPSSimpleExecutorMemoryManagerFinalizeSignature>(
61-
Parent.SAs.Finalize,
60+
rt::SPSSimpleExecutorMemoryManagerInitializeSignature>(
61+
Parent.SAs.Initialize,
6262
[OnFinalize = std::move(OnFinalize), AllocAddr = this->AllocAddr](
63-
Error SerializationErr, Error FinalizeErr) mutable {
63+
Error SerializationErr,
64+
Expected<ExecutorAddr> InitializeKey) mutable {
6465
// FIXME: Release abandoned alloc.
6566
if (SerializationErr) {
66-
cantFail(std::move(FinalizeErr));
67+
cantFail(InitializeKey.takeError());
6768
OnFinalize(std::move(SerializationErr));
68-
} else if (FinalizeErr)
69-
OnFinalize(std::move(FinalizeErr));
69+
} else if (!InitializeKey)
70+
OnFinalize(InitializeKey.takeError());
7071
else
7172
OnFinalize(FinalizedAlloc(AllocAddr));
7273
},
@@ -76,8 +77,8 @@ class EPCGenericJITLinkMemoryManager::InFlightAlloc
7677
void abandon(OnAbandonedFunction OnAbandoned) override {
7778
// FIXME: Return memory to pool instead.
7879
Parent.EPC.callSPSWrapperAsync<
79-
rt::SPSSimpleExecutorMemoryManagerDeallocateSignature>(
80-
Parent.SAs.Deallocate,
80+
rt::SPSSimpleExecutorMemoryManagerReleaseSignature>(
81+
Parent.SAs.Release,
8182
[OnAbandoned = std::move(OnAbandoned)](Error SerializationErr,
8283
Error DeallocateErr) mutable {
8384
if (SerializationErr) {
@@ -123,9 +124,8 @@ void EPCGenericJITLinkMemoryManager::allocate(const JITLinkDylib *JD,
123124

124125
void EPCGenericJITLinkMemoryManager::deallocate(
125126
std::vector<FinalizedAlloc> Allocs, OnDeallocatedFunction OnDeallocated) {
126-
EPC.callSPSWrapperAsync<
127-
rt::SPSSimpleExecutorMemoryManagerDeallocateSignature>(
128-
SAs.Deallocate,
127+
EPC.callSPSWrapperAsync<rt::SPSSimpleExecutorMemoryManagerReleaseSignature>(
128+
SAs.Release,
129129
[OnDeallocated = std::move(OnDeallocated)](Error SerErr,
130130
Error DeallocErr) mutable {
131131
if (SerErr) {

llvm/lib/ExecutionEngine/Orc/EPCGenericRTDyldMemoryManager.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ EPCGenericRTDyldMemoryManager::CreateWithDefaultBootstrapSymbols(
2525
if (auto Err = EPC.getBootstrapSymbols(
2626
{{SAs.Instance, rt::SimpleExecutorMemoryManagerInstanceName},
2727
{SAs.Reserve, rt::SimpleExecutorMemoryManagerReserveWrapperName},
28-
{SAs.Finalize, rt::SimpleExecutorMemoryManagerFinalizeWrapperName},
29-
{SAs.Deallocate,
30-
rt::SimpleExecutorMemoryManagerDeallocateWrapperName},
28+
{SAs.Initialize,
29+
rt::SimpleExecutorMemoryManagerInitializeWrapperName},
30+
{SAs.Release, rt::SimpleExecutorMemoryManagerReleaseWrapperName},
3131
{SAs.RegisterEHFrame, rt::RegisterEHFrameSectionAllocActionName},
3232
{SAs.DeregisterEHFrame,
3333
rt::DeregisterEHFrameSectionAllocActionName}}))
@@ -48,7 +48,7 @@ EPCGenericRTDyldMemoryManager::~EPCGenericRTDyldMemoryManager() {
4848

4949
Error Err = Error::success();
5050
if (auto Err2 = EPC.callSPSWrapper<
51-
rt::SPSSimpleExecutorMemoryManagerDeallocateSignature>(
51+
rt::SPSSimpleExecutorMemoryManagerReleaseSignature>(
5252
SAs.Reserve, Err, SAs.Instance, FinalizedAllocs)) {
5353
// FIXME: Report errors through EPC once that functionality is available.
5454
logAllUnhandledErrors(std::move(Err2), errs(), "");
@@ -267,20 +267,20 @@ bool EPCGenericRTDyldMemoryManager::finalizeMemory(std::string *ErrMsg) {
267267

268268
// We'll also need to make an extra allocation for the eh-frame wrapper call
269269
// arguments.
270-
Error FinalizeErr = Error::success();
270+
Expected<ExecutorAddr> InitializeKey((ExecutorAddr()));
271271
if (auto Err = EPC.callSPSWrapper<
272-
rt::SPSSimpleExecutorMemoryManagerFinalizeSignature>(
273-
SAs.Finalize, FinalizeErr, SAs.Instance, std::move(FR))) {
272+
rt::SPSSimpleExecutorMemoryManagerInitializeSignature>(
273+
SAs.Initialize, InitializeKey, SAs.Instance, std::move(FR))) {
274274
std::lock_guard<std::mutex> Lock(M);
275275
this->ErrMsg = toString(std::move(Err));
276276
dbgs() << "Serialization error: " << this->ErrMsg << "\n";
277277
if (ErrMsg)
278278
*ErrMsg = this->ErrMsg;
279279
return true;
280280
}
281-
if (FinalizeErr) {
281+
if (!InitializeKey) {
282282
std::lock_guard<std::mutex> Lock(M);
283-
this->ErrMsg = toString(std::move(FinalizeErr));
283+
this->ErrMsg = toString(InitializeKey.takeError());
284284
dbgs() << "Finalization error: " << this->ErrMsg << "\n";
285285
if (ErrMsg)
286286
*ErrMsg = this->ErrMsg;

llvm/lib/ExecutionEngine/Orc/Shared/OrcRTBridge.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,12 @@ const char *SimpleExecutorMemoryManagerInstanceName =
2323
"__llvm_orc_SimpleExecutorMemoryManager_Instance";
2424
const char *SimpleExecutorMemoryManagerReserveWrapperName =
2525
"__llvm_orc_SimpleExecutorMemoryManager_reserve_wrapper";
26-
const char *SimpleExecutorMemoryManagerFinalizeWrapperName =
27-
"__llvm_orc_SimpleExecutorMemoryManager_finalize_wrapper";
28-
const char *SimpleExecutorMemoryManagerDeallocateWrapperName =
29-
"__llvm_orc_SimpleExecutorMemoryManager_deallocate_wrapper";
26+
const char *SimpleExecutorMemoryManagerInitializeWrapperName =
27+
"__llvm_orc_SimpleExecutorMemoryManager_initialize_wrapper";
28+
const char *SimpleExecutorMemoryManagerDeinitializeWrapperName =
29+
"__llvm_orc_SimpleExecutorMemoryManager_deinitialize_wrapper";
30+
const char *SimpleExecutorMemoryManagerReleaseWrapperName =
31+
"__llvm_orc_SimpleExecutorMemoryManager_release_wrapper";
3032

3133
const char *ExecutorSharedMemoryMapperServiceInstanceName =
3234
"__llvm_orc_ExecutorSharedMemoryMapperService_Instance";

llvm/lib/ExecutionEngine/Orc/SimpleRemoteEPC.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -216,9 +216,9 @@ SimpleRemoteEPC::createDefaultMemoryManager(SimpleRemoteEPC &SREPC) {
216216
if (auto Err = SREPC.getBootstrapSymbols(
217217
{{SAs.Allocator, rt::SimpleExecutorMemoryManagerInstanceName},
218218
{SAs.Reserve, rt::SimpleExecutorMemoryManagerReserveWrapperName},
219-
{SAs.Finalize, rt::SimpleExecutorMemoryManagerFinalizeWrapperName},
220-
{SAs.Deallocate,
221-
rt::SimpleExecutorMemoryManagerDeallocateWrapperName}}))
219+
{SAs.Initialize,
220+
rt::SimpleExecutorMemoryManagerInitializeWrapperName},
221+
{SAs.Release, rt::SimpleExecutorMemoryManagerReleaseWrapperName}}))
222222
return std::move(Err);
223223

224224
return std::make_unique<EPCGenericJITLinkMemoryManager>(SREPC, SAs);

0 commit comments

Comments
 (0)