Skip to content

Commit 1b1f1c7

Browse files
argentitelhames
authored andcommitted
Re-re-apply 5acd471, Add a shared-memory based orc::MemoryMapper...
...with more fixes. The original patch was reverted in 3e9cc54 due to bot failures caused by a missing dependence on librt. That issue was fixed in 32d8d23, but that commit also broke sanitizer bots due to a bug in SimplePackedSerialization: empty ArrayRef<char>s triggered a zero-byte memcpy from a null source. The ArrayRef<char> serialization issue was fixed in 67220c2, and this patch has also been updated with a new custom SharedMemorySegFinalizeRequest message that should avoid serializing empty ArrayRefs in the first place. https://reviews.llvm.org/D128544
1 parent 1cf6b93 commit 1b1f1c7

File tree

11 files changed

+931
-1
lines changed

11 files changed

+931
-1
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>(
74+
shared::SPSExecutorAddr, shared::SPSExecutorAddr,
75+
shared::SPSSharedMemoryFinalizeRequest);
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

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

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,17 @@ struct FinalizeRequest {
8282
shared::AllocActions Actions;
8383
};
8484

85+
struct SharedMemorySegFinalizeRequest {
86+
WireProtectionFlags Prot;
87+
ExecutorAddr Addr;
88+
uint64_t Size;
89+
};
90+
91+
struct SharedMemoryFinalizeRequest {
92+
std::vector<SharedMemorySegFinalizeRequest> Segments;
93+
shared::AllocActions Actions;
94+
};
95+
8596
template <typename T> struct UIntWrite {
8697
UIntWrite() = default;
8798
UIntWrite(ExecutorAddr Addr, T Value) : Addr(Addr), Value(Value) {}
@@ -131,6 +142,13 @@ using SPSSegFinalizeRequest =
131142
using SPSFinalizeRequest = SPSTuple<SPSSequence<SPSSegFinalizeRequest>,
132143
SPSSequence<SPSAllocActionCallPair>>;
133144

145+
using SPSSharedMemorySegFinalizeRequest =
146+
SPSTuple<SPSMemoryProtectionFlags, SPSExecutorAddr, uint64_t>;
147+
148+
using SPSSharedMemoryFinalizeRequest =
149+
SPSTuple<SPSSequence<SPSSharedMemorySegFinalizeRequest>,
150+
SPSSequence<SPSAllocActionCallPair>>;
151+
134152
template <typename T>
135153
using SPSMemoryAccessUIntWrite = SPSTuple<SPSExecutorAddr, T>;
136154

@@ -204,6 +222,48 @@ class SPSSerializationTraits<SPSFinalizeRequest, tpctypes::FinalizeRequest> {
204222
}
205223
};
206224

225+
template <>
226+
class SPSSerializationTraits<SPSSharedMemorySegFinalizeRequest,
227+
tpctypes::SharedMemorySegFinalizeRequest> {
228+
using SFRAL = SPSSharedMemorySegFinalizeRequest::AsArgList;
229+
230+
public:
231+
static size_t size(const tpctypes::SharedMemorySegFinalizeRequest &SFR) {
232+
return SFRAL::size(SFR.Prot, SFR.Addr, SFR.Size);
233+
}
234+
235+
static bool serialize(SPSOutputBuffer &OB,
236+
const tpctypes::SharedMemorySegFinalizeRequest &SFR) {
237+
return SFRAL::serialize(OB, SFR.Prot, SFR.Addr, SFR.Size);
238+
}
239+
240+
static bool deserialize(SPSInputBuffer &IB,
241+
tpctypes::SharedMemorySegFinalizeRequest &SFR) {
242+
return SFRAL::deserialize(IB, SFR.Prot, SFR.Addr, SFR.Size);
243+
}
244+
};
245+
246+
template <>
247+
class SPSSerializationTraits<SPSSharedMemoryFinalizeRequest,
248+
tpctypes::SharedMemoryFinalizeRequest> {
249+
using FRAL = SPSSharedMemoryFinalizeRequest::AsArgList;
250+
251+
public:
252+
static size_t size(const tpctypes::SharedMemoryFinalizeRequest &FR) {
253+
return FRAL::size(FR.Segments, FR.Actions);
254+
}
255+
256+
static bool serialize(SPSOutputBuffer &OB,
257+
const tpctypes::SharedMemoryFinalizeRequest &FR) {
258+
return FRAL::serialize(OB, FR.Segments, FR.Actions);
259+
}
260+
261+
static bool deserialize(SPSInputBuffer &IB,
262+
tpctypes::SharedMemoryFinalizeRequest &FR) {
263+
return FRAL::deserialize(IB, FR.Segments, FR.Actions);
264+
}
265+
};
266+
207267
template <typename T>
208268
class SPSSerializationTraits<SPSMemoryAccessUIntWrite<T>,
209269
tpctypes::UIntWrite<T>> {
@@ -244,7 +304,6 @@ class SPSSerializationTraits<SPSMemoryAccessBufferWrite,
244304
}
245305
};
246306

247-
248307
} // end namespace shared
249308
} // end namespace orc
250309
} // end namespace llvm
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::SharedMemoryFinalizeRequest &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

llvm/lib/ExecutionEngine/Orc/CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
if( CMAKE_HOST_UNIX AND HAVE_LIBRT )
2+
set(rt_lib rt)
3+
endif()
4+
15
add_llvm_component_library(LLVMOrcJIT
26
CompileOnDemandLayer.cpp
37
CompileUtils.cpp
@@ -45,6 +49,7 @@ add_llvm_component_library(LLVMOrcJIT
4549

4650
LINK_LIBS
4751
${LLVM_PTHREAD_LIB}
52+
${rt_lib}
4853

4954
LINK_COMPONENTS
5055
Core

0 commit comments

Comments
 (0)