diff --git a/sycl/plugins/level_zero/pi_level_zero.cpp b/sycl/plugins/level_zero/pi_level_zero.cpp index e87dba352d6b5..df1a02c40e8a9 100644 --- a/sycl/plugins/level_zero/pi_level_zero.cpp +++ b/sycl/plugins/level_zero/pi_level_zero.cpp @@ -406,6 +406,20 @@ ze_result_t ZeCall::doCall(ze_result_t ZeResult, const char *CallStr, return mapError(Result); #define ZE_CALL_NOCHECK(Call) ZeCall().doCall(Call, #Call, false) +// Destroy all the command lists associated with this device. +// This is required when destructing the _pi_device object. +// During the piTearDown process, platforms and root devices are +// destroyed automatically regardless of their reference counts. +// So, this destructor should explicitly call zeCommandListDestroy +// to avoid memory leaks. +_pi_device::~_pi_device() { + std::lock_guard Lock(ZeCommandListCacheMutex); + for (ze_command_list_handle_t &ZeCommandList : ZeCommandListCache) { + if (ZeCommandList) + zeCommandListDestroy(ZeCommandList); + } +} + // This helper function increments the reference counter of the Queue // without guarding with a lock. // It is the caller's responsibility to make sure the lock is acquired @@ -1162,17 +1176,9 @@ pi_result piDeviceRelease(pi_device Device) { if (Device->RefCount <= 0) die("piDeviceRelease: the device has been already released"); - // TODO: OpenCL says root-device ref-count remains unchanged (1), - // but when would we free the device's data? + // Root devices are destroyed during the piTearDown process. if (Device->IsSubDevice) { if (--(Device->RefCount) == 0) { - // Destroy all the command lists associated with this device. - Device->ZeCommandListCacheMutex.lock(); - for (ze_command_list_handle_t &ZeCommandList : - Device->ZeCommandListCache) { - zeCommandListDestroy(ZeCommandList); - } - Device->ZeCommandListCacheMutex.unlock(); delete Device; } } diff --git a/sycl/plugins/level_zero/pi_level_zero.hpp b/sycl/plugins/level_zero/pi_level_zero.hpp index 2d8133f9369e4..8559887254f0d 100644 --- a/sycl/plugins/level_zero/pi_level_zero.hpp +++ b/sycl/plugins/level_zero/pi_level_zero.hpp @@ -142,6 +142,7 @@ struct _pi_device : _pi_object { // NOTE: one must additionally call initialize() to complete // PI device creation. } + ~_pi_device(); // Keep the ordinal of a "compute" commands group, where we send all // commands currently.