Skip to content
Merged
Show file tree
Hide file tree
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
32 changes: 23 additions & 9 deletions sycl/plugins/level_zero/pi_level_zero.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,26 @@ pi_result _pi_context::initialize() {
return PI_SUCCESS;
}

pi_result _pi_context::finalize() {
// This function is called when pi_context is deallocated, piContextRelase.
// There could be some memory that may have not been deallocated.
// For example, zeEventPool could be still alive.
std::lock_guard<std::mutex> NumEventsLiveInEventPoolGuard(
NumEventsLiveInEventPoolMutex, std::adopt_lock);
if (ZeEventPool && NumEventsLiveInEventPool[ZeEventPool])
zeEventPoolDestroy(ZeEventPool);

// Destroy the command list used for initializations
ZE_CALL(zeCommandListDestroy(ZeCommandListInit));

// Destruction of some members of pi_context uses L0 context
// and therefore it must be valid at that point.
// Technically it should be placed to the destructor of pi_context
// but this makes API error handling more complex.
ZE_CALL(zeContextDestroy(ZeContext));
return PI_SUCCESS;
}

pi_result
_pi_queue::resetCommandListFenceEntry(ze_command_list_handle_t ZeCommandList,
bool MakeAvailable) {
Expand Down Expand Up @@ -1810,16 +1830,10 @@ pi_result piContextRelease(pi_context Context) {

assert(Context);
if (--(Context->RefCount) == 0) {
auto ZeContext = Context->ZeContext;
// Destroy the command list used for initializations
ZE_CALL(zeCommandListDestroy(Context->ZeCommandListInit));
// Clean up any live memory associated with Context
pi_result Result = Context->finalize();
delete Context;

// Destruction of some members of pi_context uses L0 context
// and therefore it must be valid at that point.
// Technically it should be placed to the destructor of pi_context
// but this makes API error handling more complex.
ZE_CALL(zeContextDestroy(ZeContext));
return Result;
}
return PI_SUCCESS;
}
Expand Down
3 changes: 3 additions & 0 deletions sycl/plugins/level_zero/pi_level_zero.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,9 @@ struct _pi_context : _pi_object {
// Initialize the PI context.
pi_result initialize();

// Finalize the PI context
pi_result finalize();

// A L0 context handle is primarily used during creation and management of
// resources that may be used by multiple devices.
ze_context_handle_t ZeContext;
Expand Down