@@ -3065,8 +3065,7 @@ pi_result piMemRetain(pi_mem Mem) {
3065
3065
// If indirect access tracking is not enabled then this functions just performs
3066
3066
// zeMemFree. If indirect access tracking is enabled then reference counting is
3067
3067
// 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) {
3070
3069
pi_platform Plt = Context->getPlatform ();
3071
3070
std::unique_lock<pi_shared_mutex> ContextsLock (Plt->ContextsMutex ,
3072
3071
std::defer_lock);
@@ -3086,8 +3085,7 @@ static pi_result ZeMemFreeHelper(pi_context Context, void *Ptr,
3086
3085
Context->MemAllocs .erase (It);
3087
3086
}
3088
3087
3089
- if (OwnZeMemHandle)
3090
- ZE_CALL (zeMemFree, (Context->ZeContext , Ptr));
3088
+ ZE_CALL (zeMemFree, (Context->ZeContext , Ptr));
3091
3089
3092
3090
if (IndirectAccessTrackingEnabled)
3093
3091
PI_CALL (ContextReleaseHelper (Context));
@@ -3096,7 +3094,7 @@ static pi_result ZeMemFreeHelper(pi_context Context, void *Ptr,
3096
3094
}
3097
3095
3098
3096
static pi_result USMFreeHelper (pi_context Context, void *Ptr,
3099
- bool OwnZeMemHandle);
3097
+ bool OwnZeMemHandle = true );
3100
3098
3101
3099
pi_result piMemRelease (pi_mem Mem) {
3102
3100
PI_ASSERT (Mem, PI_ERROR_INVALID_MEM_OBJECT);
@@ -7050,10 +7048,8 @@ static pi_result USMHostAllocImpl(void **ResultPtr, pi_context Context,
7050
7048
return PI_SUCCESS;
7051
7049
}
7052
7050
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));
7057
7053
return PI_SUCCESS;
7058
7054
}
7059
7055
@@ -7112,8 +7108,8 @@ void *USMMemoryAllocBase::allocate(size_t Size, size_t Alignment) {
7112
7108
return Ptr;
7113
7109
}
7114
7110
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);
7117
7113
if (Res != PI_SUCCESS) {
7118
7114
throw UsmAllocationException (Res);
7119
7115
}
@@ -7361,8 +7357,13 @@ static pi_result USMFreeHelper(pi_context Context, void *Ptr,
7361
7357
Context->MemAllocs .erase (It);
7362
7358
}
7363
7359
7360
+ if (!OwnZeMemHandle) {
7361
+ // Memory should not be freed
7362
+ return PI_SUCCESS;
7363
+ }
7364
+
7364
7365
if (!UseUSMAllocator) {
7365
- pi_result Res = USMFreeImpl (Context, Ptr, OwnZeMemHandle );
7366
+ pi_result Res = USMFreeImpl (Context, Ptr);
7366
7367
if (IndirectAccessTrackingEnabled)
7367
7368
PI_CALL (ContextReleaseHelper (Context));
7368
7369
return Res;
@@ -7381,7 +7382,7 @@ static pi_result USMFreeHelper(pi_context Context, void *Ptr,
7381
7382
// If memory type is host release from host pool
7382
7383
if (ZeMemoryAllocationProperties.type == ZE_MEMORY_TYPE_HOST) {
7383
7384
try {
7384
- Context->HostMemAllocContext ->deallocate (Ptr, OwnZeMemHandle );
7385
+ Context->HostMemAllocContext ->deallocate (Ptr);
7385
7386
} catch (const UsmAllocationException &Ex) {
7386
7387
return Ex.getError ();
7387
7388
} catch (...) {
@@ -7409,16 +7410,16 @@ static pi_result USMFreeHelper(pi_context Context, void *Ptr,
7409
7410
PI_ASSERT (Device, PI_ERROR_INVALID_DEVICE);
7410
7411
7411
7412
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) {
7415
7416
try {
7416
7417
auto It = AllocContextMap.find (Device->ZeDevice );
7417
7418
if (It == AllocContextMap.end ())
7418
7419
return PI_ERROR_INVALID_VALUE;
7419
7420
7420
7421
// The right context is found, deallocate the pointer
7421
- It->second .deallocate (Ptr, OwnZeMemHandle );
7422
+ It->second .deallocate (Ptr);
7422
7423
} catch (const UsmAllocationException &Ex) {
7423
7424
return Ex.getError ();
7424
7425
}
@@ -7444,7 +7445,7 @@ static pi_result USMFreeHelper(pi_context Context, void *Ptr,
7444
7445
}
7445
7446
}
7446
7447
7447
- pi_result Res = USMFreeImpl (Context, Ptr, OwnZeMemHandle );
7448
+ pi_result Res = USMFreeImpl (Context, Ptr);
7448
7449
if (SharedReadOnlyAllocsIterator != Context->SharedReadOnlyAllocs .end ()) {
7449
7450
Context->SharedReadOnlyAllocs .erase (SharedReadOnlyAllocsIterator);
7450
7451
}
@@ -7459,7 +7460,7 @@ pi_result piextUSMFree(pi_context Context, void *Ptr) {
7459
7460
std::scoped_lock<pi_shared_mutex> Lock (
7460
7461
IndirectAccessTrackingEnabled ? Plt->ContextsMutex : Context->Mutex );
7461
7462
7462
- return USMFreeHelper (Context, Ptr, true /* OwnZeMemHandle */ );
7463
+ return USMFreeHelper (Context, Ptr);
7463
7464
}
7464
7465
7465
7466
pi_result piextKernelSetArgPointer (pi_kernel Kernel, pi_uint32 ArgIndex,
@@ -8379,11 +8380,11 @@ pi_result _pi_buffer::free() {
8379
8380
std::scoped_lock<pi_shared_mutex> Lock (
8380
8381
IndirectAccessTrackingEnabled ? Plt->ContextsMutex : Context->Mutex );
8381
8382
8382
- PI_CALL (USMFreeHelper (Context, ZeHandle, true ));
8383
+ PI_CALL (USMFreeHelper (Context, ZeHandle));
8383
8384
break ;
8384
8385
}
8385
8386
case allocation_t ::free_native:
8386
- PI_CALL (ZeMemFreeHelper (Context, ZeHandle, true ));
8387
+ PI_CALL (ZeMemFreeHelper (Context, ZeHandle));
8387
8388
break ;
8388
8389
case allocation_t ::unimport:
8389
8390
ZeUSMImport.doZeUSMRelease (Context->getPlatform ()->ZeDriver , ZeHandle);
0 commit comments