diff --git a/source/adapters/level_zero/adapter.cpp b/source/adapters/level_zero/adapter.cpp index 1bc81ed1dd..77c0edfb45 100644 --- a/source/adapters/level_zero/adapter.cpp +++ b/source/adapters/level_zero/adapter.cpp @@ -11,8 +11,6 @@ #include "adapter.hpp" #include "ur_level_zero.hpp" -ur_adapter_handle_t_ Adapter{}; - ur_result_t initPlatforms(PlatformVec &platforms) noexcept try { uint32_t ZeDriverCount = 0; ZE2UR_CALL(zeDriverGet, (&ZeDriverCount, nullptr)); @@ -36,43 +34,45 @@ ur_result_t initPlatforms(PlatformVec &platforms) noexcept try { return exceptionToResult(std::current_exception()); } -ur_result_t adapterStateInit() { - static std::once_flag ZeCallCountInitialized; - try { - std::call_once(ZeCallCountInitialized, []() { - if (UrL0LeaksDebug) { - ZeCallCount = new std::map; - } - }); - } catch (const std::bad_alloc &) { - return UR_RESULT_ERROR_OUT_OF_HOST_MEMORY; - } catch (...) { - return UR_RESULT_ERROR_UNKNOWN; - } +ur_result_t adapterStateInit() { return UR_RESULT_SUCCESS; } - // initialize level zero only once. - if (Adapter.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) { - setEnvVar("ZE_ENABLE_VALIDATION_LAYER", "1"); - setEnvVar("ZE_ENABLE_PARAMETER_VALIDATION", "1"); - } +ur_adapter_handle_t_::ur_adapter_handle_t_() { - if (getenv("SYCL_ENABLE_PCI") != nullptr) { - urPrint("WARNING: SYCL_ENABLE_PCI is deprecated and no longer needed.\n"); + Adapter.PlatformCache.Compute = [](Result &result) { + static std::once_flag ZeCallCountInitialized; + try { + std::call_once(ZeCallCountInitialized, []() { + if (UrL0LeaksDebug) { + ZeCallCount = new std::map; + } + }); + } catch (...) { + result = exceptionToResult(std::current_exception()); + return; } - // TODO: We can still safely recover if something goes wrong during the - // init. Implement handling segfault using sigaction. + // initialize level zero only once. + if (Adapter.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) { + setEnvVar("ZE_ENABLE_VALIDATION_LAYER", "1"); + setEnvVar("ZE_ENABLE_PARAMETER_VALIDATION", "1"); + } - // 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)); - } + if (getenv("SYCL_ENABLE_PCI") != nullptr) { + urPrint( + "WARNING: SYCL_ENABLE_PCI is deprecated and no longer needed.\n"); + } - Adapter.PlatformCache.Compute = [](Result &result) { + // TODO: We can still safely recover if something goes wrong during the + // init. Implement handling segfault using sigaction. + + // 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)); + } assert(Adapter.ZeResult != std::nullopt); // verify that level-zero is initialized PlatformVec platforms; @@ -95,9 +95,10 @@ ur_result_t adapterStateInit() { result = err; } }; - return UR_RESULT_SUCCESS; } +ur_adapter_handle_t_ Adapter{}; + ur_result_t adapterStateTeardown() { bool LeakFound = false; diff --git a/source/adapters/level_zero/adapter.hpp b/source/adapters/level_zero/adapter.hpp index 86dbde162d..0942db852a 100644 --- a/source/adapters/level_zero/adapter.hpp +++ b/source/adapters/level_zero/adapter.hpp @@ -17,6 +17,7 @@ using PlatformVec = std::vector>; struct ur_adapter_handle_t_ { + ur_adapter_handle_t_(); std::atomic RefCount = 0; std::mutex Mutex; diff --git a/source/adapters/level_zero/platform.cpp b/source/adapters/level_zero/platform.cpp index bc86ce7692..cca3b2173d 100644 --- a/source/adapters/level_zero/platform.cpp +++ b/source/adapters/level_zero/platform.cpp @@ -29,7 +29,8 @@ 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 = Adapter.PlatformCache->get_value(); + cached_platforms) { uint32_t nplatforms = (uint32_t)cached_platforms->size(); if (NumPlatforms) { *NumPlatforms = nplatforms;