Skip to content

Commit 00ae1e7

Browse files
authored
[SYCL] Remove OwnZeMemHandle from USMAllocator (#7853)
OwnZeMemHandle is used to track whether an allocation should be freed by the runtime or not. There is no point in passing this flag to USMAllocator and then to USMFreeHelper since the allocation cannot come from the USMAllocator (it can only be from zeMemAlloc*). I've thought about moving the memory allocation code to UR first, but there are a lot of dependencies: we would have to move the implementation of context, device, event, and queue at least. It seemed that doing this small change first made more sense.
1 parent 01511a3 commit 00ae1e7

File tree

4 files changed

+32
-32
lines changed

4 files changed

+32
-32
lines changed

sycl/plugins/level_zero/pi_level_zero.cpp

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3065,8 +3065,7 @@ pi_result piMemRetain(pi_mem Mem) {
30653065
// If indirect access tracking is not enabled then this functions just performs
30663066
// zeMemFree. If indirect access tracking is enabled then reference counting is
30673067
// performed.
3068-
static pi_result ZeMemFreeHelper(pi_context Context, void *Ptr,
3069-
bool OwnZeMemHandle = true) {
3068+
static pi_result ZeMemFreeHelper(pi_context Context, void *Ptr) {
30703069
pi_platform Plt = Context->getPlatform();
30713070
std::unique_lock<pi_shared_mutex> ContextsLock(Plt->ContextsMutex,
30723071
std::defer_lock);
@@ -3086,8 +3085,7 @@ static pi_result ZeMemFreeHelper(pi_context Context, void *Ptr,
30863085
Context->MemAllocs.erase(It);
30873086
}
30883087

3089-
if (OwnZeMemHandle)
3090-
ZE_CALL(zeMemFree, (Context->ZeContext, Ptr));
3088+
ZE_CALL(zeMemFree, (Context->ZeContext, Ptr));
30913089

30923090
if (IndirectAccessTrackingEnabled)
30933091
PI_CALL(ContextReleaseHelper(Context));
@@ -3096,7 +3094,7 @@ static pi_result ZeMemFreeHelper(pi_context Context, void *Ptr,
30963094
}
30973095

30983096
static pi_result USMFreeHelper(pi_context Context, void *Ptr,
3099-
bool OwnZeMemHandle);
3097+
bool OwnZeMemHandle = true);
31003098

31013099
pi_result piMemRelease(pi_mem Mem) {
31023100
PI_ASSERT(Mem, PI_ERROR_INVALID_MEM_OBJECT);
@@ -7050,10 +7048,8 @@ static pi_result USMHostAllocImpl(void **ResultPtr, pi_context Context,
70507048
return PI_SUCCESS;
70517049
}
70527050

7053-
static pi_result USMFreeImpl(pi_context Context, void *Ptr,
7054-
bool OwnZeMemHandle) {
7055-
if (OwnZeMemHandle)
7056-
ZE_CALL(zeMemFree, (Context->ZeContext, Ptr));
7051+
static pi_result USMFreeImpl(pi_context Context, void *Ptr) {
7052+
ZE_CALL(zeMemFree, (Context->ZeContext, Ptr));
70577053
return PI_SUCCESS;
70587054
}
70597055

@@ -7112,8 +7108,8 @@ void *USMMemoryAllocBase::allocate(size_t Size, size_t Alignment) {
71127108
return Ptr;
71137109
}
71147110

7115-
void USMMemoryAllocBase::deallocate(void *Ptr, bool OwnZeMemHandle) {
7116-
auto Res = USMFreeImpl(Context, Ptr, OwnZeMemHandle);
7111+
void USMMemoryAllocBase::deallocate(void *Ptr) {
7112+
auto Res = USMFreeImpl(Context, Ptr);
71177113
if (Res != PI_SUCCESS) {
71187114
throw UsmAllocationException(Res);
71197115
}
@@ -7361,8 +7357,13 @@ static pi_result USMFreeHelper(pi_context Context, void *Ptr,
73617357
Context->MemAllocs.erase(It);
73627358
}
73637359

7360+
if (!OwnZeMemHandle) {
7361+
// Memory should not be freed
7362+
return PI_SUCCESS;
7363+
}
7364+
73647365
if (!UseUSMAllocator) {
7365-
pi_result Res = USMFreeImpl(Context, Ptr, OwnZeMemHandle);
7366+
pi_result Res = USMFreeImpl(Context, Ptr);
73667367
if (IndirectAccessTrackingEnabled)
73677368
PI_CALL(ContextReleaseHelper(Context));
73687369
return Res;
@@ -7381,7 +7382,7 @@ static pi_result USMFreeHelper(pi_context Context, void *Ptr,
73817382
// If memory type is host release from host pool
73827383
if (ZeMemoryAllocationProperties.type == ZE_MEMORY_TYPE_HOST) {
73837384
try {
7384-
Context->HostMemAllocContext->deallocate(Ptr, OwnZeMemHandle);
7385+
Context->HostMemAllocContext->deallocate(Ptr);
73857386
} catch (const UsmAllocationException &Ex) {
73867387
return Ex.getError();
73877388
} catch (...) {
@@ -7409,16 +7410,16 @@ static pi_result USMFreeHelper(pi_context Context, void *Ptr,
74097410
PI_ASSERT(Device, PI_ERROR_INVALID_DEVICE);
74107411

74117412
auto DeallocationHelper =
7412-
[Context, Device, Ptr,
7413-
OwnZeMemHandle](std::unordered_map<ze_device_handle_t, USMAllocContext>
7414-
&AllocContextMap) {
7413+
[Context, Device,
7414+
Ptr](std::unordered_map<ze_device_handle_t, USMAllocContext>
7415+
&AllocContextMap) {
74157416
try {
74167417
auto It = AllocContextMap.find(Device->ZeDevice);
74177418
if (It == AllocContextMap.end())
74187419
return PI_ERROR_INVALID_VALUE;
74197420

74207421
// The right context is found, deallocate the pointer
7421-
It->second.deallocate(Ptr, OwnZeMemHandle);
7422+
It->second.deallocate(Ptr);
74227423
} catch (const UsmAllocationException &Ex) {
74237424
return Ex.getError();
74247425
}
@@ -7444,7 +7445,7 @@ static pi_result USMFreeHelper(pi_context Context, void *Ptr,
74447445
}
74457446
}
74467447

7447-
pi_result Res = USMFreeImpl(Context, Ptr, OwnZeMemHandle);
7448+
pi_result Res = USMFreeImpl(Context, Ptr);
74487449
if (SharedReadOnlyAllocsIterator != Context->SharedReadOnlyAllocs.end()) {
74497450
Context->SharedReadOnlyAllocs.erase(SharedReadOnlyAllocsIterator);
74507451
}
@@ -7459,7 +7460,7 @@ pi_result piextUSMFree(pi_context Context, void *Ptr) {
74597460
std::scoped_lock<pi_shared_mutex> Lock(
74607461
IndirectAccessTrackingEnabled ? Plt->ContextsMutex : Context->Mutex);
74617462

7462-
return USMFreeHelper(Context, Ptr, true /* OwnZeMemHandle */);
7463+
return USMFreeHelper(Context, Ptr);
74637464
}
74647465

74657466
pi_result piextKernelSetArgPointer(pi_kernel Kernel, pi_uint32 ArgIndex,
@@ -8379,11 +8380,11 @@ pi_result _pi_buffer::free() {
83798380
std::scoped_lock<pi_shared_mutex> Lock(
83808381
IndirectAccessTrackingEnabled ? Plt->ContextsMutex : Context->Mutex);
83818382

8382-
PI_CALL(USMFreeHelper(Context, ZeHandle, true));
8383+
PI_CALL(USMFreeHelper(Context, ZeHandle));
83838384
break;
83848385
}
83858386
case allocation_t::free_native:
8386-
PI_CALL(ZeMemFreeHelper(Context, ZeHandle, true));
8387+
PI_CALL(ZeMemFreeHelper(Context, ZeHandle));
83878388
break;
83888389
case allocation_t::unimport:
83898390
ZeUSMImport.doZeUSMRelease(Context->getPlatform()->ZeDriver, ZeHandle);

sycl/plugins/level_zero/pi_level_zero.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ class USMMemoryAllocBase : public SystemMemory {
112112
: Context{Ctx}, Device{Dev} {}
113113
void *allocate(size_t Size) override final;
114114
void *allocate(size_t Size, size_t Alignment) override final;
115-
void deallocate(void *Ptr, bool OwnZeMemHandle) override final;
115+
void deallocate(void *Ptr) override final;
116116
};
117117

118118
// Allocation routines for shared memory type

sycl/plugins/unified_runtime/ur/usm_allocator.cpp

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ class USMAllocContext::USMAllocImpl {
290290

291291
void *allocate(size_t Size, size_t Alignment, bool &FromPool);
292292
void *allocate(size_t Size, bool &FromPool);
293-
void deallocate(void *Ptr, bool &ToPool, bool OwnZeMemHandle);
293+
void deallocate(void *Ptr, bool &ToPool);
294294

295295
SystemMemory &getMemHandle() { return *MemHandle; }
296296

@@ -332,7 +332,7 @@ Slab::Slab(Bucket &Bkt)
332332

333333
Slab::~Slab() {
334334
unregSlab(*this);
335-
bucket.getMemHandle().deallocate(MemPtr, true /* OwnZeMemHandle */);
335+
bucket.getMemHandle().deallocate(MemPtr);
336336
}
337337

338338
// Return the index of the first available chunk, -1 otherwize
@@ -737,8 +737,7 @@ Bucket &USMAllocContext::USMAllocImpl::findBucket(size_t Size) {
737737
return *(*It);
738738
}
739739

740-
void USMAllocContext::USMAllocImpl::deallocate(void *Ptr, bool &ToPool,
741-
bool OwnZeMemHandle) {
740+
void USMAllocContext::USMAllocImpl::deallocate(void *Ptr, bool &ToPool) {
742741
auto *SlabPtr = AlignPtrDown(Ptr, SlabMinSize());
743742

744743
// Lock the map on read
@@ -748,7 +747,7 @@ void USMAllocContext::USMAllocImpl::deallocate(void *Ptr, bool &ToPool,
748747
auto Slabs = getKnownSlabs().equal_range(SlabPtr);
749748
if (Slabs.first == Slabs.second) {
750749
Lk.unlock();
751-
getMemHandle().deallocate(Ptr, OwnZeMemHandle);
750+
getMemHandle().deallocate(Ptr);
752751
return;
753752
}
754753

@@ -779,7 +778,7 @@ void USMAllocContext::USMAllocImpl::deallocate(void *Ptr, bool &ToPool,
779778
// There is a rare case when we have a pointer from system allocation next
780779
// to some slab with an entry in the map. So we find a slab
781780
// but the range checks fail.
782-
getMemHandle().deallocate(Ptr, OwnZeMemHandle);
781+
getMemHandle().deallocate(Ptr);
783782
}
784783

785784
USMAllocContext::USMAllocContext(std::unique_ptr<SystemMemory> MemHandle,
@@ -813,9 +812,9 @@ void *USMAllocContext::allocate(size_t size, size_t alignment) {
813812
return Ptr;
814813
}
815814

816-
void USMAllocContext::deallocate(void *ptr, bool OwnZeMemHandle) {
815+
void USMAllocContext::deallocate(void *ptr) {
817816
bool ToPool;
818-
pImpl->deallocate(ptr, ToPool, OwnZeMemHandle);
817+
pImpl->deallocate(ptr, ToPool);
819818

820819
if (pImpl->getParams().PoolTrace > 2) {
821820
auto MT = pImpl->getParams().memoryTypeName;

sycl/plugins/unified_runtime/ur/usm_allocator.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class SystemMemory {
1717
public:
1818
virtual void *allocate(size_t size) = 0;
1919
virtual void *allocate(size_t size, size_t aligned) = 0;
20-
virtual void deallocate(void *ptr, bool OwnZeMemHandle) = 0;
20+
virtual void deallocate(void *ptr) = 0;
2121
virtual ~SystemMemory() = default;
2222
};
2323

@@ -68,7 +68,7 @@ class USMAllocContext {
6868

6969
void *allocate(size_t size);
7070
void *allocate(size_t size, size_t alignment);
71-
void deallocate(void *ptr, bool OwnZeMemHandle);
71+
void deallocate(void *ptr);
7272

7373
private:
7474
std::unique_ptr<USMAllocImpl> pImpl;

0 commit comments

Comments
 (0)