Skip to content

Commit 45e1b14

Browse files
authored
[SYCL][L0] Fix memory leak in PiDeviceCache and ZeCommandList (#2974)
Release resources in pi_platforms and pi_devices while tearing down. Upon tear-down, platforms are destroyed. Each platform contains a piDeviceCache. When destroying each pi_platform, we have to destroy zeCommandList, too. Signed-off-by: Byoungro So [email protected]
1 parent 6cfc3ad commit 45e1b14

File tree

2 files changed

+16
-9
lines changed

2 files changed

+16
-9
lines changed

sycl/plugins/level_zero/pi_level_zero.cpp

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,20 @@ ze_result_t ZeCall::doCall(ze_result_t ZeResult, const char *CallStr,
406406
return mapError(Result);
407407
#define ZE_CALL_NOCHECK(Call) ZeCall().doCall(Call, #Call, false)
408408

409+
// Destroy all the command lists associated with this device.
410+
// This is required when destructing the _pi_device object.
411+
// During the piTearDown process, platforms and root devices are
412+
// destroyed automatically regardless of their reference counts.
413+
// So, this destructor should explicitly call zeCommandListDestroy
414+
// to avoid memory leaks.
415+
_pi_device::~_pi_device() {
416+
std::lock_guard<std::mutex> Lock(ZeCommandListCacheMutex);
417+
for (ze_command_list_handle_t &ZeCommandList : ZeCommandListCache) {
418+
if (ZeCommandList)
419+
zeCommandListDestroy(ZeCommandList);
420+
}
421+
}
422+
409423
// This helper function increments the reference counter of the Queue
410424
// without guarding with a lock.
411425
// It is the caller's responsibility to make sure the lock is acquired
@@ -1245,17 +1259,9 @@ pi_result piDeviceRelease(pi_device Device) {
12451259
if (Device->RefCount <= 0)
12461260
die("piDeviceRelease: the device has been already released");
12471261

1248-
// TODO: OpenCL says root-device ref-count remains unchanged (1),
1249-
// but when would we free the device's data?
1262+
// Root devices are destroyed during the piTearDown process.
12501263
if (Device->IsSubDevice) {
12511264
if (--(Device->RefCount) == 0) {
1252-
// Destroy all the command lists associated with this device.
1253-
Device->ZeCommandListCacheMutex.lock();
1254-
for (ze_command_list_handle_t &ZeCommandList :
1255-
Device->ZeCommandListCache) {
1256-
zeCommandListDestroy(ZeCommandList);
1257-
}
1258-
Device->ZeCommandListCacheMutex.unlock();
12591265
delete Device;
12601266
}
12611267
}

sycl/plugins/level_zero/pi_level_zero.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ struct _pi_device : _pi_object {
142142
// NOTE: one must additionally call initialize() to complete
143143
// PI device creation.
144144
}
145+
~_pi_device();
145146

146147
// Keep the ordinal of a "compute" commands group, where we send all
147148
// commands currently.

0 commit comments

Comments
 (0)