Skip to content

Commit aab825a

Browse files
lhamesakadutta
authored andcommitted
[orc-rt] Rename SimpleNativeMemoryMap finalize & deallocate. NFCI. (llvm#163114)
This commit renames the "finalize" operation to "initialize", and "deallocate" to "deinitialize". The new names are chosen to better fit the point of view of the ORC-runtime and executor-process: After memory is *reserved* it can be *initialized* with some content, and *deinitialized* to return that memory to the reserved region. This seems more understandable to me than the original scheme, which named these operations after the controller-side JITLinkMemoryManager operations that they partially implemented. I.e. SimpleNativeMemoryMap::finalize implemented the final step of JITLinkMemoryManager::finalize, initializing the memory in the executor; and SimpleNativeMemoryMap::deallocate implemented the final step of JITLinkMemoryManager::deallocate, running dealloc actions and releasing the finalized region. The proper way to think of the relationship between these operations now is that: 1. The final step of finalization is to initialize the memory in the executor. 2. The final step of deallocation is to deinitialize the memory in the executor.
1 parent 82e968e commit aab825a

File tree

3 files changed

+125
-117
lines changed

3 files changed

+125
-117
lines changed

orc-rt/include/orc-rt/SimpleNativeMemoryMap.h

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,13 @@ namespace orc_rt {
3131
///
3232
/// Intances can:
3333
/// 1. Reserve address space.
34-
/// 2. Finalize memory regions within reserved memory (copying content,
34+
/// 2. Initialize memory regions within reserved memory (copying content,
3535
/// applying permissions, running finalize actions, and recording
3636
/// deallocate actions).
37-
/// 3. Deallocate memory regions within reserved memory (running
37+
/// 3. Deinitialize memory regions within reserved memory (running
3838
/// deallocate actions and making memory available for future
39-
/// finalize calls (if the system permits this).
40-
/// 4. Release address space, deallocating any not-yet-deallocated finalized
39+
/// initialize calls (if the system permits this).
40+
/// 4. Release address space, deinitializing any remaining initialized
4141
/// regions, and returning the address space to the system for reuse (if
4242
/// the system permits).
4343
class SimpleNativeMemoryMap : public ResourceManager {
@@ -58,7 +58,7 @@ class SimpleNativeMemoryMap : public ResourceManager {
5858
void releaseMultiple(OnReleaseCompleteFn &&OnComplete,
5959
std::vector<void *> Addrs);
6060

61-
struct FinalizeRequest {
61+
struct InitializeRequest {
6262
struct Segment {
6363
AllocGroup AG;
6464
char *Address = nullptr;
@@ -72,19 +72,19 @@ class SimpleNativeMemoryMap : public ResourceManager {
7272

7373
/// Writes content into the requested ranges, applies permissions, and
7474
/// performs allocation actions.
75-
using OnFinalizeCompleteFn = move_only_function<void(Expected<void *>)>;
76-
void finalize(OnFinalizeCompleteFn &&OnComplete, FinalizeRequest FR);
75+
using OnInitializeCompleteFn = move_only_function<void(Expected<void *>)>;
76+
void initialize(OnInitializeCompleteFn &&OnComplete, InitializeRequest FR);
7777

7878
/// Runs deallocation actions and resets memory permissions for the requested
7979
/// memory.
80-
using OnDeallocateCompleteFn = move_only_function<void(Error)>;
81-
void deallocate(OnDeallocateCompleteFn &&OnComplete, void *Base);
80+
using OnDeinitializeCompleteFn = move_only_function<void(Error)>;
81+
void deinitialize(OnDeinitializeCompleteFn &&OnComplete, void *Base);
8282

83-
/// Convenience method to deallocate multiple regions with one call. This can
84-
/// be used to save on interprocess communication at the cost of less
83+
/// Convenience method to deinitialize multiple regions with one call. This
84+
/// can be used to save on interprocess communication at the cost of less
8585
/// expressive errors.
86-
void deallocateMultiple(OnDeallocateCompleteFn &&OnComplete,
87-
std::vector<void *> Bases);
86+
void deinitializeMultiple(OnDeinitializeCompleteFn &&OnComplete,
87+
std::vector<void *> Bases);
8888

8989
void detach(ResourceManager::OnCompleteFn OnComplete) override;
9090
void shutdown(ResourceManager::OnCompleteFn OnComplete) override;
@@ -98,8 +98,9 @@ class SimpleNativeMemoryMap : public ResourceManager {
9898

9999
void releaseNext(OnReleaseCompleteFn &&OnComplete, std::vector<void *> Addrs,
100100
bool AnyError, Error LastErr);
101-
void deallocateNext(OnDeallocateCompleteFn &&OnComplete,
102-
std::vector<void *> Bases, bool AnyError, Error LastErr);
101+
void deinitializeNext(OnDeinitializeCompleteFn &&OnComplete,
102+
std::vector<void *> Bases, bool AnyError,
103+
Error LastErr);
103104
void shutdownNext(OnCompleteFn OnComplete, std::vector<void *> Bases);
104105
Error makeBadSlabError(void *Base, const char *Op);
105106
SlabInfo *findSlabInfoFor(void *Base);
@@ -121,12 +122,12 @@ orc_rt_SimpleNativeMemoryMap_releaseMultiple_sps_wrapper(
121122
orc_rt_SessionRef Session, void *CallCtx,
122123
orc_rt_WrapperFunctionReturn Return, orc_rt_WrapperFunctionBuffer ArgBytes);
123124

124-
ORC_RT_SPS_INTERFACE void orc_rt_SimpleNativeMemoryMap_finalize_sps_wrapper(
125+
ORC_RT_SPS_INTERFACE void orc_rt_SimpleNativeMemoryMap_initialize_sps_wrapper(
125126
orc_rt_SessionRef Session, void *CallCtx,
126127
orc_rt_WrapperFunctionReturn Return, orc_rt_WrapperFunctionBuffer ArgBytes);
127128

128129
ORC_RT_SPS_INTERFACE void
129-
orc_rt_SimpleNativeMemoryMap_deallocateMultiple_sps_wrapper(
130+
orc_rt_SimpleNativeMemoryMap_deinitializeMultiple_sps_wrapper(
130131
orc_rt_SessionRef Session, void *CallCtx,
131132
orc_rt_WrapperFunctionReturn Return, orc_rt_WrapperFunctionBuffer ArgBytes);
132133

orc-rt/lib/executor/SimpleNativeMemoryMap.cpp

Lines changed: 48 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
//
99
// SimpleNativeMemoryMap and related APIs.
1010
//
11-
// TODO: We don't reset / uncommit pages on deallocate, or on failure during
12-
// finalize. We should do that to reduce memory pressure.
11+
// TODO: We don't reset / uncommit pages on deinitialize, or on failure during
12+
// initialize. We should do that to reduce memory pressure.
1313
//
1414
//===----------------------------------------------------------------------===//
1515

@@ -29,14 +29,16 @@ namespace orc_rt {
2929
struct SPSSimpleNativeMemoryMapSegment;
3030

3131
template <>
32-
class SPSSerializationTraits<SPSSimpleNativeMemoryMapSegment,
33-
SimpleNativeMemoryMap::FinalizeRequest::Segment> {
32+
class SPSSerializationTraits<
33+
SPSSimpleNativeMemoryMapSegment,
34+
SimpleNativeMemoryMap::InitializeRequest::Segment> {
3435
using SPSType =
3536
SPSTuple<SPSAllocGroup, SPSExecutorAddr, uint64_t, SPSSequence<char>>;
3637

3738
public:
38-
static bool deserialize(SPSInputBuffer &IB,
39-
SimpleNativeMemoryMap::FinalizeRequest::Segment &S) {
39+
static bool
40+
deserialize(SPSInputBuffer &IB,
41+
SimpleNativeMemoryMap::InitializeRequest::Segment &S) {
4042
AllocGroup AG;
4143
ExecutorAddr Address;
4244
uint64_t Size;
@@ -50,17 +52,17 @@ class SPSSerializationTraits<SPSSimpleNativeMemoryMapSegment,
5052
}
5153
};
5254

53-
struct SPSSimpleNativeMemoryMapFinalizeRequest;
55+
struct SPSSimpleNativeMemoryMapInitializeRequest;
5456

5557
template <>
56-
class SPSSerializationTraits<SPSSimpleNativeMemoryMapFinalizeRequest,
57-
SimpleNativeMemoryMap::FinalizeRequest> {
58+
class SPSSerializationTraits<SPSSimpleNativeMemoryMapInitializeRequest,
59+
SimpleNativeMemoryMap::InitializeRequest> {
5860
using SPSType = SPSTuple<SPSSequence<SPSSimpleNativeMemoryMapSegment>,
5961
SPSSequence<SPSAllocActionPair>>;
6062

6163
public:
6264
static bool deserialize(SPSInputBuffer &IB,
63-
SimpleNativeMemoryMap::FinalizeRequest &FR) {
65+
SimpleNativeMemoryMap::InitializeRequest &FR) {
6466
return SPSType::AsArgList::deserialize(IB, FR.Segments, FR.AAPs);
6567
}
6668
};
@@ -121,13 +123,13 @@ void SimpleNativeMemoryMap::releaseMultiple(OnReleaseCompleteFn &&OnComplete,
121123
releaseNext(std::move(OnComplete), std::move(Addrs), false, Error::success());
122124
}
123125

124-
void SimpleNativeMemoryMap::finalize(OnFinalizeCompleteFn &&OnComplete,
125-
FinalizeRequest FR) {
126+
void SimpleNativeMemoryMap::initialize(OnInitializeCompleteFn &&OnComplete,
127+
InitializeRequest FR) {
126128

127129
void *Base = nullptr;
128130

129-
// TODO: Record finalize segments for release.
130-
// std::vector<std::pair<void*, size_t>> FinalizeSegments;
131+
// TODO: Record initialize segments for release.
132+
// std::vector<std::pair<void*, size_t>> InitializeSegments;
131133

132134
// Check segment validity before proceeding.
133135
for (auto &S : FR.Segments) {
@@ -166,9 +168,10 @@ void SimpleNativeMemoryMap::finalize(OnFinalizeCompleteFn &&OnComplete,
166168
}
167169

168170
if (!Base)
169-
return OnComplete(make_error<StringError>(
170-
"SimpleNativeMemoryMap finalize error: finalization requires at least "
171-
"one standard-lifetime segment"));
171+
return OnComplete(
172+
make_error<StringError>("SimpleNativeMemoryMap initialize error: "
173+
"finalization requires at least "
174+
"one standard-lifetime segment"));
172175

173176
auto DeallocActions = runFinalizeActions(std::move(FR.AAPs));
174177
if (!DeallocActions)
@@ -182,25 +185,26 @@ void SimpleNativeMemoryMap::finalize(OnFinalizeCompleteFn &&OnComplete,
182185
OnComplete(Base);
183186
}
184187

185-
void SimpleNativeMemoryMap::deallocate(OnDeallocateCompleteFn &&OnComplete,
186-
void *Base) {
188+
void SimpleNativeMemoryMap::deinitialize(OnDeinitializeCompleteFn &&OnComplete,
189+
void *Base) {
187190
std::vector<AllocAction> DAAs;
188191

189192
{
190193
std::unique_lock<std::mutex> Lock(M);
191194
auto *SI = findSlabInfoFor(Base);
192195
if (!SI) {
193196
Lock.unlock();
194-
return OnComplete(makeBadSlabError(Base, "finalize"));
197+
return OnComplete(makeBadSlabError(Base, "deinitialize"));
195198
}
196199

197200
auto I = SI->DeallocActions.find(Base);
198201
if (I == SI->DeallocActions.end()) {
199202
Lock.unlock();
200203
std::ostringstream ErrMsg;
201-
ErrMsg << "SimpleNativeMemoryMap deallocate error: no deallocate actions "
202-
"registered for segment base address "
203-
<< Base;
204+
ErrMsg
205+
<< "SimpleNativeMemoryMap deinitialize error: no deallocate actions "
206+
"registered for segment base address "
207+
<< Base;
204208
return OnComplete(make_error<StringError>(ErrMsg.str()));
205209
}
206210

@@ -212,10 +216,10 @@ void SimpleNativeMemoryMap::deallocate(OnDeallocateCompleteFn &&OnComplete,
212216
OnComplete(Error::success());
213217
}
214218

215-
void SimpleNativeMemoryMap::deallocateMultiple(
216-
OnDeallocateCompleteFn &&OnComplete, std::vector<void *> Bases) {
217-
deallocateNext(std::move(OnComplete), std::move(Bases), false,
218-
Error::success());
219+
void SimpleNativeMemoryMap::deinitializeMultiple(
220+
OnDeinitializeCompleteFn &&OnComplete, std::vector<void *> Bases) {
221+
deinitializeNext(std::move(OnComplete), std::move(Bases), false,
222+
Error::success());
219223
}
220224

221225
void SimpleNativeMemoryMap::detach(ResourceManager::OnCompleteFn OnComplete) {
@@ -268,9 +272,9 @@ void SimpleNativeMemoryMap::releaseNext(OnReleaseCompleteFn &&OnComplete,
268272
NextAddr);
269273
}
270274

271-
void SimpleNativeMemoryMap::deallocateNext(OnDeallocateCompleteFn &&OnComplete,
272-
std::vector<void *> Addrs,
273-
bool AnyError, Error LastErr) {
275+
void SimpleNativeMemoryMap::deinitializeNext(
276+
OnDeinitializeCompleteFn &&OnComplete, std::vector<void *> Addrs,
277+
bool AnyError, Error LastErr) {
274278
// TODO: Log error?
275279
if (LastErr) {
276280
consumeError(std::move(LastErr));
@@ -282,17 +286,17 @@ void SimpleNativeMemoryMap::deallocateNext(OnDeallocateCompleteFn &&OnComplete,
282286
return OnComplete(Error::success());
283287

284288
return OnComplete(
285-
make_error<StringError>("Failed to deallocate some addresses"));
289+
make_error<StringError>("Failed to deinitialize some addresses"));
286290
}
287291

288292
void *NextAddr = Addrs.back();
289293
Addrs.pop_back();
290294

291-
deallocate(
295+
deinitialize(
292296
[this, OnComplete = std::move(OnComplete), AnyError = AnyError,
293297
Addrs = std::move(Addrs)](Error Err) mutable {
294-
deallocateNext(std::move(OnComplete), std::move(Addrs), AnyError,
295-
std::move(Err));
298+
deinitializeNext(std::move(OnComplete), std::move(Addrs), AnyError,
299+
std::move(Err));
296300
},
297301
NextAddr);
298302
}
@@ -346,15 +350,15 @@ Error SimpleNativeMemoryMap::recordDeallocActions(
346350
auto *SI = findSlabInfoFor(Base);
347351
if (!SI) {
348352
Lock.unlock();
349-
return makeBadSlabError(Base, "deallocate");
353+
return makeBadSlabError(Base, "deinitialize");
350354
}
351355

352356
auto I = SI->DeallocActions.find(Base);
353357
if (I != SI->DeallocActions.end()) {
354358
Lock.unlock();
355359
std::ostringstream ErrMsg;
356-
ErrMsg << "SimpleNativeMemoryMap finalize error: segment base address "
357-
"reused in subsequent finalize call";
360+
ErrMsg << "SimpleNativeMemoryMap initialize error: segment base address "
361+
"reused in subsequent initialize call";
358362
return make_error<StringError>(ErrMsg.str());
359363
}
360364

@@ -383,27 +387,27 @@ orc_rt_SimpleNativeMemoryMap_releaseMultiple_sps_wrapper(
383387
&SimpleNativeMemoryMap::releaseMultiple));
384388
}
385389

386-
ORC_RT_SPS_INTERFACE void orc_rt_SimpleNativeMemoryMap_finalize_sps_wrapper(
390+
ORC_RT_SPS_INTERFACE void orc_rt_SimpleNativeMemoryMap_initialize_sps_wrapper(
387391
orc_rt_SessionRef Session, void *CallCtx,
388392
orc_rt_WrapperFunctionReturn Return,
389393
orc_rt_WrapperFunctionBuffer ArgBytes) {
390394
using Sig = SPSExpected<SPSExecutorAddr>(
391-
SPSExecutorAddr, SPSSimpleNativeMemoryMapFinalizeRequest);
392-
SPSWrapperFunction<Sig>::handle(
393-
Session, CallCtx, Return, ArgBytes,
394-
WrapperFunction::handleWithAsyncMethod(&SimpleNativeMemoryMap::finalize));
395+
SPSExecutorAddr, SPSSimpleNativeMemoryMapInitializeRequest);
396+
SPSWrapperFunction<Sig>::handle(Session, CallCtx, Return, ArgBytes,
397+
WrapperFunction::handleWithAsyncMethod(
398+
&SimpleNativeMemoryMap::initialize));
395399
}
396400

397401
ORC_RT_SPS_INTERFACE void
398-
orc_rt_SimpleNativeMemoryMap_deallocateMultiple_sps_wrapper(
402+
orc_rt_SimpleNativeMemoryMap_deinitializeMultiple_sps_wrapper(
399403
orc_rt_SessionRef Session, void *CallCtx,
400404
orc_rt_WrapperFunctionReturn Return,
401405
orc_rt_WrapperFunctionBuffer ArgBytes) {
402406
using Sig = SPSError(SPSExecutorAddr, SPSSequence<SPSExecutorAddr>);
403407
SPSWrapperFunction<Sig>::handle(
404408
Session, CallCtx, Return, ArgBytes,
405409
WrapperFunction::handleWithAsyncMethod(
406-
&SimpleNativeMemoryMap::deallocateMultiple));
410+
&SimpleNativeMemoryMap::deinitializeMultiple));
407411
}
408412

409413
} // namespace orc_rt

0 commit comments

Comments
 (0)