Skip to content

Commit 5acd471

Browse files
argentitelhames
authored andcommitted
[ORC] Add a shared-memory based orc::MemoryMapper.
This is an implementation of orc::MemoryMapper that maps shared memory pages in both executor and controller process and writes directly to them avoiding transferring content over EPC. All allocations are properly deinitialized automatically on the executor side at shutdown by the ExecutorSharedMemoryMapperService. Reviewed By: lhames Differential Revision: https://reviews.llvm.org/D128544
1 parent 6e6c1ef commit 5acd471

File tree

9 files changed

+858
-0
lines changed

9 files changed

+858
-0
lines changed

llvm/include/llvm/ExecutionEngine/Orc/MemoryMapper.h

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,47 @@ class InProcessMemoryMapper final : public MemoryMapper {
109109
AllocationMap Allocations;
110110
};
111111

112+
class SharedMemoryMapper final : public MemoryMapper {
113+
public:
114+
struct SymbolAddrs {
115+
ExecutorAddr Instance;
116+
ExecutorAddr Reserve;
117+
ExecutorAddr Initialize;
118+
ExecutorAddr Deinitialize;
119+
ExecutorAddr Release;
120+
};
121+
122+
SharedMemoryMapper(ExecutorProcessControl &EPC, SymbolAddrs SAs)
123+
: EPC(EPC), SAs(SAs) {}
124+
125+
void reserve(size_t NumBytes, OnReservedFunction OnReserved) override;
126+
127+
char *prepare(ExecutorAddr Addr, size_t ContentSize) override;
128+
129+
void initialize(AllocInfo &AI, OnInitializedFunction OnInitialized) override;
130+
131+
void deinitialize(ArrayRef<ExecutorAddr> Allocations,
132+
OnDeinitializedFunction OnDeInitialized) override;
133+
134+
void release(ArrayRef<ExecutorAddr> Reservations,
135+
OnReleasedFunction OnRelease) override;
136+
137+
~SharedMemoryMapper() override;
138+
139+
private:
140+
struct Reservation {
141+
void *LocalAddr;
142+
size_t Size;
143+
};
144+
145+
ExecutorProcessControl &EPC;
146+
SymbolAddrs SAs;
147+
148+
std::mutex Mutex;
149+
150+
std::map<ExecutorAddr, Reservation> Reservations;
151+
};
152+
112153
} // namespace orc
113154
} // end namespace llvm
114155

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@ extern const char *SimpleExecutorMemoryManagerReserveWrapperName;
3131
extern const char *SimpleExecutorMemoryManagerFinalizeWrapperName;
3232
extern const char *SimpleExecutorMemoryManagerDeallocateWrapperName;
3333

34+
extern const char *ExecutorSharedMemoryMapperServiceInstanceName;
35+
extern const char *ExecutorSharedMemoryMapperServiceReserveWrapperName;
36+
extern const char *ExecutorSharedMemoryMapperServiceInitializeWrapperName;
37+
extern const char *ExecutorSharedMemoryMapperServiceDeinitializeWrapperName;
38+
extern const char *ExecutorSharedMemoryMapperServiceReleaseWrapperName;
39+
3440
extern const char *MemoryWriteUInt8sWrapperName;
3541
extern const char *MemoryWriteUInt16sWrapperName;
3642
extern const char *MemoryWriteUInt32sWrapperName;
@@ -58,6 +64,21 @@ using SPSSimpleExecutorMemoryManagerFinalizeSignature =
5864
using SPSSimpleExecutorMemoryManagerDeallocateSignature = shared::SPSError(
5965
shared::SPSExecutorAddr, shared::SPSSequence<shared::SPSExecutorAddr>);
6066

67+
// ExecutorSharedMemoryMapperService
68+
using SPSExecutorSharedMemoryMapperServiceReserveSignature =
69+
shared::SPSExpected<
70+
shared::SPSTuple<shared::SPSExecutorAddr, shared::SPSString>>(
71+
shared::SPSExecutorAddr, uint64_t);
72+
using SPSExecutorSharedMemoryMapperServiceInitializeSignature =
73+
shared::SPSExpected<shared::SPSExecutorAddr>(shared::SPSExecutorAddr,
74+
shared::SPSExecutorAddr,
75+
shared::SPSFinalizeRequest);
76+
using SPSExecutorSharedMemoryMapperServiceDeinitializeSignature =
77+
shared::SPSError(shared::SPSExecutorAddr,
78+
shared::SPSSequence<shared::SPSExecutorAddr>);
79+
using SPSExecutorSharedMemoryMapperServiceReleaseSignature = shared::SPSError(
80+
shared::SPSExecutorAddr, shared::SPSSequence<shared::SPSExecutorAddr>);
81+
6182
using SPSRunAsMainSignature = int64_t(shared::SPSExecutorAddr,
6283
shared::SPSSequence<shared::SPSString>);
6384

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
//===----------- ExecutorSharedMemoryMapperService.h ------------*- C++ -*-===//
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+
9+
#ifndef LLVM_EXECUTIONENGINE_ORC_TARGETPROCESS_EXECUTORSHAREDMEMORYMAPPERSERVICE
10+
#define LLVM_EXECUTIONENGINE_ORC_TARGETPROCESS_EXECUTORSHAREDMEMORYMAPPERSERVICE
11+
12+
#include "llvm/ADT/DenseMap.h"
13+
#include "llvm/ExecutionEngine/Orc/Shared/TargetProcessControlTypes.h"
14+
#include "llvm/ExecutionEngine/Orc/TargetProcess/ExecutorBootstrapService.h"
15+
16+
#include <atomic>
17+
#include <mutex>
18+
19+
#if defined(_WIN32)
20+
#include <windows.h>
21+
#endif
22+
23+
namespace llvm {
24+
namespace orc {
25+
namespace rt_bootstrap {
26+
27+
class ExecutorSharedMemoryMapperService final
28+
: public ExecutorBootstrapService {
29+
public:
30+
~ExecutorSharedMemoryMapperService(){};
31+
32+
Expected<std::pair<ExecutorAddr, std::string>> reserve(uint64_t Size);
33+
Expected<ExecutorAddr> initialize(ExecutorAddr Reservation,
34+
tpctypes::FinalizeRequest &FR);
35+
36+
Error deinitialize(const std::vector<ExecutorAddr> &Bases);
37+
Error release(const std::vector<ExecutorAddr> &Bases);
38+
39+
Error shutdown() override;
40+
void addBootstrapSymbols(StringMap<ExecutorAddr> &M) override;
41+
42+
private:
43+
struct Allocation {
44+
std::vector<shared::WrapperFunctionCall> DeinitializationActions;
45+
};
46+
using AllocationMap = DenseMap<ExecutorAddr, Allocation>;
47+
48+
struct Reservation {
49+
size_t Size;
50+
std::vector<ExecutorAddr> Allocations;
51+
#if defined(_WIN32)
52+
HANDLE SharedMemoryFile;
53+
#endif
54+
};
55+
using ReservationMap = DenseMap<void *, Reservation>;
56+
57+
static llvm::orc::shared::CWrapperFunctionResult
58+
reserveWrapper(const char *ArgData, size_t ArgSize);
59+
60+
static llvm::orc::shared::CWrapperFunctionResult
61+
initializeWrapper(const char *ArgData, size_t ArgSize);
62+
63+
static llvm::orc::shared::CWrapperFunctionResult
64+
deinitializeWrapper(const char *ArgData, size_t ArgSize);
65+
66+
static llvm::orc::shared::CWrapperFunctionResult
67+
releaseWrapper(const char *ArgData, size_t ArgSize);
68+
69+
std::atomic<int> SharedMemoryCount{0};
70+
std::mutex Mutex;
71+
ReservationMap Reservations;
72+
AllocationMap Allocations;
73+
};
74+
75+
} // namespace rt_bootstrap
76+
} // namespace orc
77+
} // namespace llvm
78+
#endif // LLVM_EXECUTIONENGINE_ORC_TARGETPROCESS_EXECUTORSHAREDMEMORYMAPPERSERVICE

0 commit comments

Comments
 (0)