diff --git a/sycl/plugins/level_zero/pi_level_zero.cpp b/sycl/plugins/level_zero/pi_level_zero.cpp index efbf971d6945b..51084bba12967 100644 --- a/sycl/plugins/level_zero/pi_level_zero.cpp +++ b/sycl/plugins/level_zero/pi_level_zero.cpp @@ -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 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) { @@ -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; } diff --git a/sycl/plugins/level_zero/pi_level_zero.hpp b/sycl/plugins/level_zero/pi_level_zero.hpp index 4570d73f9cfa8..fb1ff0fecc7e0 100644 --- a/sycl/plugins/level_zero/pi_level_zero.hpp +++ b/sycl/plugins/level_zero/pi_level_zero.hpp @@ -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;