Skip to content

Commit 38450c4

Browse files
author
omarahmed1111
committed
fix some calls to release objects
1 parent f2dd1ec commit 38450c4

3 files changed

Lines changed: 35 additions & 22 deletions

File tree

source/adapters/level_zero/adapter_lib_init_windows.cpp

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,25 @@
1313

1414
#include <windows.h>
1515

16+
// On windows, UR is destructed before sycl-rt have destructed the objects, so
17+
// we need to clear the leftover memory before UR destruction.
18+
ur_result_t deleteCacheOnDestruction() {
19+
std::lock_guard<std::mutex> Lock{GlobalAdapter->Mutex};
20+
const auto *platforms = GlobalAdapter->PlatformCache->get_value();
21+
for (const auto &p : *platforms) {
22+
std::scoped_lock<ur_shared_mutex> ContextsLock(p->ContextsMutex);
23+
while (!p->Contexts.empty()) {
24+
ur_context_handle_t &ctx = p->Contexts.front();
25+
ctx->deleteCachedObjectsOnDestruction();
26+
uint32_t RefCount = ctx->RefCount.load();
27+
while (RefCount--) {
28+
UR_CALL(urContextRelease(ctx));
29+
}
30+
deleteFromCachedList<ur_context_handle_t>(ctx, p->Contexts);
31+
}
32+
}
33+
}
34+
1635
BOOL WINAPI DllMain(HINSTANCE hinstDLL, // handle to DLL module
1736
DWORD fdwReason, // reason for calling function
1837
LPVOID lpReserved) // reserved
@@ -21,18 +40,7 @@ BOOL WINAPI DllMain(HINSTANCE hinstDLL, // handle to DLL module
2140
case DLL_PROCESS_ATTACH:
2241
break;
2342
case DLL_PROCESS_DETACH: {
24-
std::lock_guard<std::mutex> Lock{GlobalAdapter->Mutex};
25-
const auto *platforms = GlobalAdapter->PlatformCache->get_value();
26-
for (const auto &p : *platforms) {
27-
std::scoped_lock<ur_shared_mutex> ContextsLock(p->ContextsMutex);
28-
while (!p->Contexts.empty()) {
29-
ur_context_handle_t &ctx = p->Contexts.front();
30-
ctx->deleteCachedObjectsOnDestruction();
31-
UR_CALL(urContextRelease(ctx));
32-
deleteFromCachedList<ur_context_handle_t>(ctx, p->Contexts);
33-
}
34-
}
35-
break;
43+
return deleteCacheOnDestruction() == UR_RESULT_SUCCESS;
3644
}
3745
case DLL_THREAD_ATTACH:
3846
break;

source/adapters/level_zero/context.cpp

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -368,13 +368,12 @@ ur_result_t ContextReleaseHelper(ur_context_handle_t Context) {
368368
Contexts.erase(It);
369369
};
370370

371-
#ifdef _WIN32
372371
DeleteFromContextsCache();
373-
#else
372+
374373
if (IndirectAccessTrackingEnabled) {
375374
DeleteFromContextsCache();
376375
}
377-
#endif
376+
378377
ze_context_handle_t DestroyZeContext =
379378
Context->OwnNativeHandle ? Context->ZeContext : nullptr;
380379

@@ -857,3 +856,14 @@ ur_context_handle_t_::getDevices() const {
857856
ze_context_handle_t ur_context_handle_t_::getZeHandle() const {
858857
return ZeContext;
859858
}
859+
860+
void ur_context_handle_t_::deleteCachedObjectsOnDestruction() {
861+
while (!KernelsCache.empty()) {
862+
ur_kernel_handle_t &kernel = KernelsCache.front();
863+
uint32_t RefCount = kernel->RefCount.load();
864+
while (RefCount--) {
865+
UR_CALL_THROWS(urKernelRelease(kernel));
866+
}
867+
deleteFromCachedList(kernel, KernelsCache);
868+
}
869+
}

source/adapters/level_zero/context.hpp

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include <zes_api.h>
2424

2525
#include "common.hpp"
26+
#include "kernel.hpp"
2627
#include "queue.hpp"
2728

2829
#include <umf_helpers.hpp>
@@ -311,13 +312,7 @@ struct ur_context_handle_t_ : _ur_object {
311312
// Get handle to the L0 context
312313
ze_context_handle_t getZeHandle() const;
313314

314-
void deleteCachedObjectsOnDestruction() {
315-
while (!KernelsCache.empty()) {
316-
ur_kernel_handle_t &kernel = KernelsCache.front();
317-
UR_CALL_THROWS(urKernelRelease(kernel));
318-
deleteFromCachedList(kernel, KernelsCache);
319-
}
320-
}
315+
void deleteCachedObjectsOnDestruction();
321316

322317
private:
323318
// Get the cache of events for a provided scope and profiling mode.

0 commit comments

Comments
 (0)