Skip to content

Commit

Permalink
Delete Adapter in atexit after refcnt is 0 due to multi DLLMain
Browse files Browse the repository at this point in the history
- In Windows, SYCL and UMF both define DLLMain such that a DLLMain
  for only the adapter's is not possible. To fix this, the
  L0 adapter inits the global adapter at variable init and
  registers an atexit teardown after refcnt == 0.

Signed-off-by: Neil R. Spruit <neil.r.spruit@intel.com>
  • Loading branch information
nrspruit committed Mar 7, 2024
1 parent aa69079 commit 826d697
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 63 deletions.
9 changes: 2 additions & 7 deletions source/adapters/level_zero/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (C) 2022 Intel Corporation
# Copyright (C) 2022-2024 Intel Corporation
# Part of the Unified-Runtime Project, under the Apache License v2.0 with LLVM Exceptions.
# See LICENSE.TXT
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Expand Down Expand Up @@ -122,12 +122,7 @@ add_ur_adapter(${TARGET_NAME}
${CMAKE_CURRENT_SOURCE_DIR}/../../ur/ur.cpp
)

if(WIN32)
target_sources(ur_adapter_level_zero
PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/adapter_lib_init_windows.cpp
)
else()
if(!WIN32)
target_sources(ur_adapter_level_zero
PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/adapter_lib_init_linux.cpp
Expand Down
56 changes: 38 additions & 18 deletions source/adapters/level_zero/adapter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,13 @@
#include "adapter.hpp"
#include "ur_level_zero.hpp"

ur_adapter_handle_t_ *Adapter;
// Due to multiple DLLMain definitions with SYCL, Global Adapter is init at
// variable creation.
#if defined(_WIN32)
ur_adapter_handle_t_ *GlobalAdapter = new ur_adapter_handle_t_();
#else
ur_adapter_handle_t_ *GlobalAdapter;
#endif

ur_result_t initPlatforms(PlatformVec &platforms) noexcept try {
uint32_t ZeDriverCount = 0;
Expand Down Expand Up @@ -53,7 +59,7 @@ ur_adapter_handle_t_::ur_adapter_handle_t_() {
}

// initialize level zero only once.
if (Adapter->ZeResult == std::nullopt) {
if (GlobalAdapter->ZeResult == std::nullopt) {
// Setting these environment variables before running zeInit will enable
// the validation layer in the Level Zero loader.
if (UrL0Debug & UR_L0_DEBUG_VALIDATION) {
Expand All @@ -72,20 +78,21 @@ ur_adapter_handle_t_::ur_adapter_handle_t_() {
// We must only initialize the driver once, even if urPlatformGet() is
// called multiple times. Declaring the return value as "static" ensures
// it's only called once.
Adapter->ZeResult = ZE_CALL_NOCHECK(zeInit, (ZE_INIT_FLAG_GPU_ONLY));
GlobalAdapter->ZeResult =
ZE_CALL_NOCHECK(zeInit, (ZE_INIT_FLAG_GPU_ONLY));
}
assert(Adapter->ZeResult !=
assert(GlobalAdapter->ZeResult !=
std::nullopt); // verify that level-zero is initialized
PlatformVec platforms;

// Absorb the ZE_RESULT_ERROR_UNINITIALIZED and just return 0 Platforms.
if (*Adapter->ZeResult == ZE_RESULT_ERROR_UNINITIALIZED) {
if (*GlobalAdapter->ZeResult == ZE_RESULT_ERROR_UNINITIALIZED) {
result = std::move(platforms);
return;
}
if (*Adapter->ZeResult != ZE_RESULT_SUCCESS) {
if (*GlobalAdapter->ZeResult != ZE_RESULT_SUCCESS) {
urPrint("zeInit: Level Zero initialization failure\n");
result = ze2urResult(*Adapter->ZeResult);
result = ze2urResult(*GlobalAdapter->ZeResult);
return;
}

Expand All @@ -98,6 +105,14 @@ ur_adapter_handle_t_::ur_adapter_handle_t_() {
};
}

#if defined(_WIN32)
void globalAdapterWindowsCleanup() {
if (GlobalAdapter) {
delete GlobalAdapter;
}
}
#endif

ur_result_t adapterStateTeardown() {
bool LeakFound = false;

Expand Down Expand Up @@ -183,6 +198,11 @@ ur_result_t adapterStateTeardown() {
}
if (LeakFound)
return UR_RESULT_ERROR_INVALID_MEM_OBJECT;
// Due to multiple DLLMain definitions with SYCL, register to cleanup the
// Global Adapter after refcnt is 0
#if defined(_WIN32)
std::atexit(globalAdapterWindowsCleanup);
#endif

return UR_RESULT_SUCCESS;
}
Expand All @@ -202,12 +222,12 @@ UR_APIEXPORT ur_result_t UR_APICALL urAdapterGet(
///< adapters available.
) {
if (NumEntries > 0 && Adapters) {
if (Adapter) {
std::lock_guard<std::mutex> Lock{Adapter->Mutex};
if (Adapter->RefCount++ == 0) {
if (GlobalAdapter) {
std::lock_guard<std::mutex> Lock{GlobalAdapter->Mutex};
if (GlobalAdapter->RefCount++ == 0) {
adapterStateInit();
}
*Adapters = Adapter;
*Adapters = GlobalAdapter;
} else {
return UR_RESULT_ERROR_UNINITIALIZED;
}
Expand All @@ -222,9 +242,9 @@ UR_APIEXPORT ur_result_t UR_APICALL urAdapterGet(

UR_APIEXPORT ur_result_t UR_APICALL urAdapterRelease(ur_adapter_handle_t) {
// Check first if the Adapter pointer is valid
if (Adapter) {
std::lock_guard<std::mutex> Lock{Adapter->Mutex};
if (--Adapter->RefCount == 0) {
if (GlobalAdapter) {
std::lock_guard<std::mutex> Lock{GlobalAdapter->Mutex};
if (--GlobalAdapter->RefCount == 0) {
return adapterStateTeardown();
}
}
Expand All @@ -233,9 +253,9 @@ UR_APIEXPORT ur_result_t UR_APICALL urAdapterRelease(ur_adapter_handle_t) {
}

UR_APIEXPORT ur_result_t UR_APICALL urAdapterRetain(ur_adapter_handle_t) {
if (Adapter) {
std::lock_guard<std::mutex> Lock{Adapter->Mutex};
Adapter->RefCount++;
if (GlobalAdapter) {
std::lock_guard<std::mutex> Lock{GlobalAdapter->Mutex};
GlobalAdapter->RefCount++;
} else {
return UR_RESULT_ERROR_UNINITIALIZED;
}
Expand Down Expand Up @@ -267,7 +287,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urAdapterGetInfo(ur_adapter_handle_t,
case UR_ADAPTER_INFO_BACKEND:
return ReturnValue(UR_ADAPTER_BACKEND_LEVEL_ZERO);
case UR_ADAPTER_INFO_REFERENCE_COUNT:
return ReturnValue(Adapter->RefCount.load());
return ReturnValue(GlobalAdapter->RefCount.load());
default:
return UR_RESULT_ERROR_INVALID_ENUMERATION;
}
Expand Down
2 changes: 1 addition & 1 deletion source/adapters/level_zero/adapter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ struct ur_adapter_handle_t_ {
ZeCache<Result<PlatformVec>> PlatformCache;
};

extern ur_adapter_handle_t_ *Adapter;
extern ur_adapter_handle_t_ *GlobalAdapter;
9 changes: 5 additions & 4 deletions source/adapters/level_zero/adapter_lib_init_linux.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@
#include "ur_level_zero.hpp"

void __attribute__((constructor)) createAdapterHandle() {
if (!Adapter) {
Adapter = new ur_adapter_handle_t_();
if (!GlobalAdapter) {
GlobalAdapter = new ur_adapter_handle_t_();
}
}

void __attribute__((destructor)) deleteAdapterHandle() {
if (Adapter) {
delete Adapter;
if (GlobalAdapter) {
delete GlobalAdapter;
GlobalAdapter = nullptr;
}
}
27 changes: 0 additions & 27 deletions source/adapters/level_zero/adapter_lib_init_windows.cpp

This file was deleted.

4 changes: 2 additions & 2 deletions source/adapters/level_zero/device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1442,7 +1442,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urDeviceCreateWithNativeHandle(
// a valid Level Zero device.

ur_device_handle_t Dev = nullptr;
if (const auto *platforms = Adapter->PlatformCache->get_value()) {
if (const auto *platforms = GlobalAdapter->PlatformCache->get_value()) {
for (const auto &p : *platforms) {
Dev = p->getDeviceFromNativeHandle(ZeDevice);
if (Dev) {
Expand All @@ -1453,7 +1453,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urDeviceCreateWithNativeHandle(
}
}
} else {
return Adapter->PlatformCache->get_error();
return GlobalAdapter->PlatformCache->get_error();
}

if (Dev == nullptr)
Expand Down
6 changes: 3 additions & 3 deletions source/adapters/level_zero/platform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urPlatformGet(
) {
// Platform handles are cached for reuse. This is to ensure consistent
// handle pointers across invocations and to improve retrieval performance.
if (const auto *cached_platforms = Adapter->PlatformCache->get_value();
if (const auto *cached_platforms = GlobalAdapter->PlatformCache->get_value();
cached_platforms) {
uint32_t nplatforms = (uint32_t)cached_platforms->size();
if (NumPlatforms) {
Expand All @@ -41,7 +41,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urPlatformGet(
}
}
} else {
return Adapter->PlatformCache->get_error();
return GlobalAdapter->PlatformCache->get_error();
}

return UR_RESULT_SUCCESS;
Expand Down Expand Up @@ -133,7 +133,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urPlatformCreateWithNativeHandle(
auto ZeDriver = ur_cast<ze_driver_handle_t>(NativePlatform);

uint32_t NumPlatforms = 0;
ur_adapter_handle_t AdapterHandle = Adapter;
ur_adapter_handle_t AdapterHandle = GlobalAdapter;
UR_CALL(urPlatformGet(&AdapterHandle, 1, 0, nullptr, &NumPlatforms));

if (NumPlatforms) {
Expand Down
2 changes: 1 addition & 1 deletion source/adapters/level_zero/queue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -569,7 +569,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urQueueCreateWithNativeHandle(
// Maybe this is not completely correct.
uint32_t NumEntries = 1;
ur_platform_handle_t Platform{};
ur_adapter_handle_t AdapterHandle = Adapter;
ur_adapter_handle_t AdapterHandle = GlobalAdapter;
UR_CALL(urPlatformGet(&AdapterHandle, 1, NumEntries, &Platform, nullptr));

ur_device_handle_t UrDevice = Device;
Expand Down

0 comments on commit 826d697

Please sign in to comment.