Skip to content

[SYCL] Do not track allocation if we don't own the handle #8792

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 28, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions sycl/plugins/level_zero/pi_level_zero.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3344,7 +3344,11 @@ pi_result piextMemCreateWithNativeHandle(pi_native_handle NativeHandle,
pi_platform Plt = Context->getPlatform();
std::unique_lock<pi_shared_mutex> ContextsLock(Plt->ContextsMutex,
std::defer_lock);
if (IndirectAccessTrackingEnabled) {
// If we don't own the native handle 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 in the mode when indirect access
// tracking is enabled.
if (IndirectAccessTrackingEnabled && ownNativeHandle) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a source code comment why we check the ownership

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added source code comment.

// We need to keep track of all memory allocations in the context
ContextsLock.lock();
// Retain context to be sure that it is released after all memory
Expand Down Expand Up @@ -7342,6 +7346,11 @@ pi_result piextUSMHostAlloc(void **ResultPtr, pi_context Context,
// mutex.
static pi_result USMFreeHelper(pi_context Context, void *Ptr,
bool OwnZeMemHandle) {
if (!OwnZeMemHandle) {
// Memory should not be freed
return PI_SUCCESS;
}

if (IndirectAccessTrackingEnabled) {
auto It = Context->MemAllocs.find(Ptr);
if (It == std::end(Context->MemAllocs)) {
Expand All @@ -7357,11 +7366,6 @@ static pi_result USMFreeHelper(pi_context Context, void *Ptr,
Context->MemAllocs.erase(It);
}

if (!OwnZeMemHandle) {
// Memory should not be freed
return PI_SUCCESS;
}

if (!UseUSMAllocator) {
pi_result Res = USMFreeImpl(Context, Ptr);
if (IndirectAccessTrackingEnabled)
Expand Down