Skip to content

Commit 4bf87d2

Browse files
authored
[SYCL] Do not track allocation if we don't own the handle (#8792)
If we don't own level zero handle of the pi_mem then we can't control deallocation of that memory so there is no point of keeping track of the memory allocation for deferred memory release. More over currently level zero context is leaked because we increase its ref count at the point where we start tracking memory but don't decrement that ref count in USMFreeHelper. This PR fixes that problem.
1 parent cb9486d commit 4bf87d2

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

sycl/plugins/level_zero/pi_level_zero.cpp

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3344,7 +3344,11 @@ pi_result piextMemCreateWithNativeHandle(pi_native_handle NativeHandle,
33443344
pi_platform Plt = Context->getPlatform();
33453345
std::unique_lock<pi_shared_mutex> ContextsLock(Plt->ContextsMutex,
33463346
std::defer_lock);
3347-
if (IndirectAccessTrackingEnabled) {
3347+
// If we don't own the native handle then we can't control deallocation of
3348+
// that memory so there is no point of keeping track of the memory
3349+
// allocation for deferred memory release in the mode when indirect access
3350+
// tracking is enabled.
3351+
if (IndirectAccessTrackingEnabled && ownNativeHandle) {
33483352
// We need to keep track of all memory allocations in the context
33493353
ContextsLock.lock();
33503354
// Retain context to be sure that it is released after all memory
@@ -7342,6 +7346,11 @@ pi_result piextUSMHostAlloc(void **ResultPtr, pi_context Context,
73427346
// mutex.
73437347
static pi_result USMFreeHelper(pi_context Context, void *Ptr,
73447348
bool OwnZeMemHandle) {
7349+
if (!OwnZeMemHandle) {
7350+
// Memory should not be freed
7351+
return PI_SUCCESS;
7352+
}
7353+
73457354
if (IndirectAccessTrackingEnabled) {
73467355
auto It = Context->MemAllocs.find(Ptr);
73477356
if (It == std::end(Context->MemAllocs)) {
@@ -7357,11 +7366,6 @@ static pi_result USMFreeHelper(pi_context Context, void *Ptr,
73577366
Context->MemAllocs.erase(It);
73587367
}
73597368

7360-
if (!OwnZeMemHandle) {
7361-
// Memory should not be freed
7362-
return PI_SUCCESS;
7363-
}
7364-
73657369
if (!UseUSMAllocator) {
73667370
pi_result Res = USMFreeImpl(Context, Ptr);
73677371
if (IndirectAccessTrackingEnabled)

0 commit comments

Comments
 (0)