From 7170ae043c25bd4d04b98d155cd358be88621bab Mon Sep 17 00:00:00 2001 From: Igor Chorazewicz Date: Mon, 7 Jul 2025 19:55:36 +0000 Subject: [PATCH 1/5] [SYCL][UR] Make v2 adapter default for BMG and newer Whenever we detect ANY device newer than BMG on the platform we will use V2, otherwise we will use V1. The default behavior can still be overwritten by setting SYCL_UR_USE_LEVEL_ZERO_V2=1 to use V2 adapter or SYCL_UR_USE_LEVEL_ZERO_V2=0 to use V1 adapter. --- .../source/adapters/level_zero/adapter.cpp | 61 +++++++++++++++++++ .../source/loader/ur_adapter_registry.hpp | 16 ----- 2 files changed, 61 insertions(+), 16 deletions(-) diff --git a/unified-runtime/source/adapters/level_zero/adapter.cpp b/unified-runtime/source/adapters/level_zero/adapter.cpp index 0809ec97ede6f..44bba4d31fd66 100644 --- a/unified-runtime/source/adapters/level_zero/adapter.cpp +++ b/unified-runtime/source/adapters/level_zero/adapter.cpp @@ -258,6 +258,51 @@ ur_result_t adapterStateInit() { return UR_RESULT_SUCCESS; } +static bool isBMGorNewer() { + auto urResult = checkDeviceIntelGPUIpVersionOrNewer(0x05004000); + if (urResult != UR_RESULT_SUCCESS && + urResult != UR_RESULT_ERROR_UNSUPPORTED_VERSION) { + UR_LOG(ERR, "Intel GPU IP Version check failed: {}\n", urResult); + throw urResult; + } + + return urResult == UR_RESULT_SUCCESS; +} + +// returns a pair indicating whether to use the V1 adapter and a string +// indicating the reason for the decision. +static std::pair shouldUseV1Adapter() { + auto specificAdapterVersionRequested = + ur_getenv("UR_LOADER_USE_LEVEL_ZERO_V2").has_value() || + ur_getenv("SYCL_UR_USE_LEVEL_ZERO_V2").has_value(); + + auto v2Requested = getenv_tobool("UR_LOADER_USE_LEVEL_ZERO_V2", false); + v2Requested |= getenv_tobool("SYCL_UR_USE_LEVEL_ZERO_V2", false); + + std::string reason = + specificAdapterVersionRequested + ? "Specific adapter version requested by UR_LOADER_USE_LEVEL_ZERO_V2 " + "or SYCL_UR_USE_LEVEL_ZERO_V2" + : "Using default adapter version based on device IP version"; + + if (v2Requested) { + return {false, reason}; + } + + if (!v2Requested && specificAdapterVersionRequested) { + // v1 specifically requested + return {true, reason}; + } + + // default: only enable for devices older than BMG + return {!isBMGorNewer(), reason}; +} + +static std::pair shouldUseV2Adapter() { + auto [useV1, reason] = shouldUseV1Adapter(); + return {!useV1, reason}; +} + /* This constructor initializes the `ur_adapter_handle_t_` object and sets up the environment for Level Zero (L0) initialization. @@ -476,6 +521,22 @@ ur_adapter_handle_t_::ur_adapter_handle_t_() return; } +#ifdef UR_ADAPTER_LEVEL_ZERO_V2 + auto [useV2, reason] = shouldUseV2Adapter(); + if (!useV2) { + UR_LOG(INFO, "Skipping L0 V2 adapter: {}", reason); + result = std::move(platforms); + return; + } +#else + auto [useV1, reason] = shouldUseV1Adapter(); + if (!useV1) { + UR_LOG(INFO, "Skipping L0 V1 adapter: {}", reason); + result = std::move(platforms); + return; + } +#endif + // Check if the user has enabled the default L0 SysMan initialization. const int UrSysmanZesinitEnable = [&UserForcedSysManInit] { const char *UrRet = std::getenv("UR_L0_ENABLE_ZESINIT_DEFAULT"); diff --git a/unified-runtime/source/loader/ur_adapter_registry.hpp b/unified-runtime/source/loader/ur_adapter_registry.hpp index 05c3c3e9ceeca..cabed246a6da2 100644 --- a/unified-runtime/source/loader/ur_adapter_registry.hpp +++ b/unified-runtime/source/loader/ur_adapter_registry.hpp @@ -355,22 +355,6 @@ class AdapterRegistry { } for (const auto &adapterName : adapterNames) { - // Skip legacy L0 adapter if the v2 adapter is requested, and vice versa. - if (std::string(adapterName).find("level_zero") != std::string::npos) { - auto v2Requested = getenv_tobool("UR_LOADER_USE_LEVEL_ZERO_V2", false); - v2Requested |= getenv_tobool("SYCL_UR_USE_LEVEL_ZERO_V2", false); - auto v2Adapter = - std::string(adapterName).find("v2") != std::string::npos; - - if (v2Requested != v2Adapter) { - UR_LOG(INFO, "The adapter '{}' is skipped because {} {}.", - adapterName, - "UR_LOADER_USE_LEVEL_ZERO_V2 or SYCL_UR_USE_LEVEL_ZERO_V2", - v2Requested ? "is set" : "is not set"); - continue; - } - } - std::vector loadPaths; // Adapter search order: From abb0daf9bf4b7ad4c46f6e558e47c85fef8c7dbc Mon Sep 17 00:00:00 2001 From: Igor Chorazewicz Date: Thu, 10 Jul 2025 15:36:30 +0000 Subject: [PATCH 2/5] fix warning --- unified-runtime/source/adapters/level_zero/adapter.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/unified-runtime/source/adapters/level_zero/adapter.cpp b/unified-runtime/source/adapters/level_zero/adapter.cpp index 44bba4d31fd66..f40352c19d2b9 100644 --- a/unified-runtime/source/adapters/level_zero/adapter.cpp +++ b/unified-runtime/source/adapters/level_zero/adapter.cpp @@ -298,7 +298,7 @@ static std::pair shouldUseV1Adapter() { return {!isBMGorNewer(), reason}; } -static std::pair shouldUseV2Adapter() { +[[maybe_unused]] static std::pair shouldUseV2Adapter() { auto [useV1, reason] = shouldUseV1Adapter(); return {!useV1, reason}; } From 30fa2d89c40c68544a4d3d0db00b22cd0304eb43 Mon Sep 17 00:00:00 2001 From: Igor Chorazewicz Date: Thu, 10 Jul 2025 17:26:15 +0000 Subject: [PATCH 3/5] Use single var to enable both adapters --- sycl/test-e2e/format.py | 2 ++ sycl/test-e2e/lit.cfg.py | 2 ++ 2 files changed, 4 insertions(+) diff --git a/sycl/test-e2e/format.py b/sycl/test-e2e/format.py index 44da0ef1c52c4..b8a710f7093e8 100644 --- a/sycl/test-e2e/format.py +++ b/sycl/test-e2e/format.py @@ -333,6 +333,8 @@ def get_extra_env(sycl_devices): dev_features = test.config.sycl_dev_features[full_dev_name] if "level_zero_v2_adapter" in dev_features: expanded += " env UR_LOADER_USE_LEVEL_ZERO_V2=1" + else: + expanded += " env UR_LOADER_USE_LEVEL_ZERO_V2=0" expanded += " ONEAPI_DEVICE_SELECTOR={} {}".format( parsed_dev_name, test.config.run_launcher diff --git a/sycl/test-e2e/lit.cfg.py b/sycl/test-e2e/lit.cfg.py index 2f91e68f12f76..a871f2ec5ae57 100644 --- a/sycl/test-e2e/lit.cfg.py +++ b/sycl/test-e2e/lit.cfg.py @@ -962,6 +962,8 @@ def get_sycl_ls_verbose(sycl_device, env): if "v2" in full_name: env["UR_LOADER_ENABLE_LEVEL_ZERO_V2"] = "1" + else: + env["UR_LOADER_ENABLE_LEVEL_ZERO_V2"] = "0" env["ONEAPI_DEVICE_SELECTOR"] = sycl_device if sycl_device.startswith("cuda:"): From 081b512dfccc57fd92853486bd6cb0880fd0359c Mon Sep 17 00:00:00 2001 From: Igor Chorazewicz Date: Fri, 11 Jul 2025 22:25:11 +0000 Subject: [PATCH 4/5] Skip adapters with no platforms in urAdapterGet --- .../scripts/templates/ldrddi.cpp.mako | 25 +- .../source/adapters/level_zero/adapter.cpp | 250 +++++++++--------- .../source/adapters/level_zero/adapter.hpp | 6 +- .../source/adapters/level_zero/device.cpp | 8 +- .../source/adapters/level_zero/platform.cpp | 15 +- unified-runtime/source/loader/ur_ldrddi.cpp | 24 +- 6 files changed, 151 insertions(+), 177 deletions(-) diff --git a/unified-runtime/scripts/templates/ldrddi.cpp.mako b/unified-runtime/scripts/templates/ldrddi.cpp.mako index d70586aa77f79..8678cc0e18ce8 100644 --- a/unified-runtime/scripts/templates/ldrddi.cpp.mako +++ b/unified-runtime/scripts/templates/ldrddi.cpp.mako @@ -51,24 +51,21 @@ namespace ur_loader %if func_basename == "AdapterGet": auto context = getContext(); - size_t adapterIndex = 0; - if( nullptr != ${obj['params'][1]['name']} && ${obj['params'][0]['name']} !=0) - { - for( auto& platform : context->platforms ) - { - if(platform.initStatus != ${X}_RESULT_SUCCESS) - continue; - platform.dditable.${th.get_table_name(n, tags, obj)}.${th.make_pfn_name(n, tags, obj)}( 1, &${obj['params'][1]['name']}[adapterIndex], nullptr ); - adapterIndex++; - if (adapterIndex == NumEntries) { - break; - } - } + uint32_t numAdapters = 0; + for (auto &platform : context->platforms) { + if (platform.initStatus != ${X}_RESULT_SUCCESS) + continue; + + uint32_t adapter; + ur_adapter_handle_t *adapterHandle = numAdapters < NumEntries ? &${obj['params'][1]['name']}[numAdapters] : nullptr; + platform.dditable.${th.get_table_name(n, tags, obj)}.${th.make_pfn_name(n, tags, obj)}( 1, adapterHandle, &adapter ); + + numAdapters += adapter; } if( ${obj['params'][2]['name']} != nullptr ) { - *${obj['params'][2]['name']} = static_cast(context->platforms.size()); + *${obj['params'][2]['name']} = numAdapters; } return ${X}_RESULT_SUCCESS; diff --git a/unified-runtime/source/adapters/level_zero/adapter.cpp b/unified-runtime/source/adapters/level_zero/adapter.cpp index f40352c19d2b9..1768f532afaf0 100644 --- a/unified-runtime/source/adapters/level_zero/adapter.cpp +++ b/unified-runtime/source/adapters/level_zero/adapter.cpp @@ -54,21 +54,21 @@ class ur_legacy_sink : public logger::Sink { }; // Find the corresponding ZesDevice Handle for a given ZeDevice -ur_result_t getZesDeviceHandle(zes_uuid_t coreDeviceUuid, +ur_result_t getZesDeviceHandle(ur_adapter_handle_t_ *adapter, + zes_uuid_t coreDeviceUuid, zes_device_handle_t *ZesDevice, uint32_t *SubDeviceId, ze_bool_t *SubDevice) { uint32_t ZesDriverCount = 0; std::vector ZesDrivers; std::vector ZesDevices; ze_result_t ZesResult = ZE_RESULT_ERROR_INVALID_ARGUMENT; - ZE2UR_CALL(GlobalAdapter->getSysManDriversFunctionPtr, - (&ZesDriverCount, nullptr)); + ZE2UR_CALL(adapter->getSysManDriversFunctionPtr, (&ZesDriverCount, nullptr)); ZesDrivers.resize(ZesDriverCount); - ZE2UR_CALL(GlobalAdapter->getSysManDriversFunctionPtr, + ZE2UR_CALL(adapter->getSysManDriversFunctionPtr, (&ZesDriverCount, ZesDrivers.data())); for (uint32_t I = 0; I < ZesDriverCount; ++I) { ZesResult = ZE_CALL_NOCHECK( - GlobalAdapter->getDeviceByUUIdFunctionPtr, + adapter->getDeviceByUUIdFunctionPtr, (ZesDrivers[I], coreDeviceUuid, ZesDevice, SubDevice, SubDeviceId)); if (ZesResult == ZE_RESULT_SUCCESS) { return UR_RESULT_SUCCESS; @@ -147,7 +147,7 @@ ur_result_t checkDeviceIntelGPUIpVersionOrNewer(uint32_t ipVersion) { * for the devices into the platform. * 10. The function handles exceptions and returns the appropriate result. */ -ur_result_t initPlatforms(PlatformVec &platforms, +ur_result_t initPlatforms(ur_adapter_handle_t_ *adapter, PlatformVec &platforms, ze_result_t ZesResult) noexcept try { std::vector ZeDrivers; std::vector ZeDriverGetHandles; @@ -162,20 +162,20 @@ ur_result_t initPlatforms(PlatformVec &platforms, ZeDriverGetHandles.resize(ZeDriverGetCount); ZE2UR_CALL(zeDriverGet, (&ZeDriverGetCount, ZeDriverGetHandles.data())); } - if (ZeDriverGetCount == 0 && GlobalAdapter->ZeInitDriversCount == 0) { + if (ZeDriverGetCount == 0 && adapter->ZeInitDriversCount == 0) { UR_LOG(ERR, "\nNo Valid L0 Drivers found.\n"); return UR_RESULT_SUCCESS; } - if (GlobalAdapter->InitDriversSupported) { - ZeInitDriversHandles.resize(GlobalAdapter->ZeInitDriversCount); - ZeDrivers.resize(GlobalAdapter->ZeInitDriversCount); - ZE2UR_CALL(GlobalAdapter->initDriversFunctionPtr, - (&GlobalAdapter->ZeInitDriversCount, ZeInitDriversHandles.data(), - &GlobalAdapter->InitDriversDesc)); + if (adapter->InitDriversSupported) { + ZeInitDriversHandles.resize(adapter->ZeInitDriversCount); + ZeDrivers.resize(adapter->ZeInitDriversCount); + ZE2UR_CALL(adapter->initDriversFunctionPtr, + (&adapter->ZeInitDriversCount, ZeInitDriversHandles.data(), + &adapter->InitDriversDesc)); ZeDrivers.assign(ZeInitDriversHandles.begin(), ZeInitDriversHandles.end()); - if (ZeDriverGetCount > 0 && GlobalAdapter->ZeInitDriversCount > 0) { - for (uint32_t X = 0; X < GlobalAdapter->ZeInitDriversCount; ++X) { + if (ZeDriverGetCount > 0 && adapter->ZeInitDriversCount > 0) { + for (uint32_t X = 0; X < adapter->ZeInitDriversCount; ++X) { for (uint32_t Y = 0; Y < ZeDriverGetCount; ++Y) { ZeStruct ZeDriverGetProperties; ZeStruct ZeInitDriverProperties; @@ -234,9 +234,10 @@ ur_result_t initPlatforms(PlatformVec &platforms, ur_zes_device_handle_data_t ZesDeviceData; zes_uuid_t ZesUUID; std::memcpy(&ZesUUID, &device_properties.uuid, sizeof(zes_uuid_t)); - if (getZesDeviceHandle( - ZesUUID, &ZesDeviceData.ZesDevice, &ZesDeviceData.SubDeviceId, - &ZesDeviceData.SubDevice) == UR_RESULT_SUCCESS) { + if (getZesDeviceHandle(adapter, ZesUUID, &ZesDeviceData.ZesDevice, + &ZesDeviceData.SubDeviceId, + &ZesDeviceData.SubDevice) == + UR_RESULT_SUCCESS) { platforms.back()->ZedeviceToZesDeviceMap.insert( std::make_pair(ZeDevices[D], std::move(ZesDeviceData))); } @@ -342,9 +343,9 @@ Behavior Summary: */ ur_adapter_handle_t_::ur_adapter_handle_t_() : handle_base(), logger(logger::get_logger("level_zero")), RefCount(0) { - ZeInitDriversResult = ZE_RESULT_ERROR_UNINITIALIZED; - ZeInitResult = ZE_RESULT_ERROR_UNINITIALIZED; - ZesResult = ZE_RESULT_ERROR_UNINITIALIZED; + auto ZeInitDriversResult = ZE_RESULT_ERROR_UNINITIALIZED; + auto ZeInitResult = ZE_RESULT_ERROR_UNINITIALIZED; + auto ZesResult = ZE_RESULT_ERROR_UNINITIALIZED; #ifdef UR_STATIC_LEVEL_ZERO // Given static linking of the L0 Loader, we must delay the loader's @@ -370,7 +371,6 @@ ur_adapter_handle_t_::ur_adapter_handle_t_() setEnvVar("ZEL_ENABLE_BASIC_LEAK_CHECKER", "1"); } - PlatformCache.Compute = [](Result &result) { uint32_t UserForcedSysManInit = 0; // Check if the user has disabled the default L0 Env initialization. const int UrSysManEnvInitEnabled = [&UserForcedSysManInit] { @@ -386,14 +386,12 @@ ur_adapter_handle_t_::ur_adapter_handle_t_() // not exist in older loader runtimes. #ifndef UR_STATIC_LEVEL_ZERO #ifdef _WIN32 - GlobalAdapter->processHandle = GetModuleHandle(NULL); + processHandle = GetModuleHandle(NULL); #else - GlobalAdapter->processHandle = nullptr; + processHandle = nullptr; #endif #endif - // initialize level zero only once. - 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) { @@ -427,10 +425,10 @@ ur_adapter_handle_t_::ur_adapter_handle_t_() } UR_LOG(DEBUG, "\nzeInit with flags value of {}\n", static_cast(L0InitFlags)); - GlobalAdapter->ZeInitResult = ZE_CALL_NOCHECK(zeInit, (L0InitFlags)); - if (GlobalAdapter->ZeInitResult != ZE_RESULT_SUCCESS) { + ZeInitResult = ZE_CALL_NOCHECK(zeInit, (L0InitFlags)); + if (ZeInitResult != ZE_RESULT_SUCCESS) { const char *ErrorString = "Unknown"; - zeParseError(GlobalAdapter->ZeInitResult, ErrorString); + zeParseError(ZeInitResult, ErrorString); UR_LOG(ERR, "\nzeInit failed with {}\n", ErrorString); } @@ -474,125 +472,103 @@ ur_adapter_handle_t_::ur_adapter_handle_t_() if (useInitDrivers) { #ifdef UR_STATIC_LEVEL_ZERO - GlobalAdapter->initDriversFunctionPtr = zeInitDrivers; + initDriversFunctionPtr = zeInitDrivers; #else - GlobalAdapter->initDriversFunctionPtr = + initDriversFunctionPtr = (ze_pfnInitDrivers_t)ur_loader::LibLoader::getFunctionPtr( - GlobalAdapter->processHandle, "zeInitDrivers"); + processHandle, "zeInitDrivers"); #endif - if (GlobalAdapter->initDriversFunctionPtr) { + if (initDriversFunctionPtr) { UR_LOG(DEBUG, "\nzeInitDrivers with flags value of {}\n", - static_cast(GlobalAdapter->InitDriversDesc.flags)); - GlobalAdapter->ZeInitDriversResult = - ZE_CALL_NOCHECK(GlobalAdapter->initDriversFunctionPtr, - (&GlobalAdapter->ZeInitDriversCount, nullptr, - &GlobalAdapter->InitDriversDesc)); - if (GlobalAdapter->ZeInitDriversResult == ZE_RESULT_SUCCESS) { - GlobalAdapter->InitDriversSupported = true; + static_cast(InitDriversDesc.flags)); + ZeInitDriversResult = + ZE_CALL_NOCHECK(initDriversFunctionPtr, + (&ZeInitDriversCount, nullptr, &InitDriversDesc)); + if (ZeInitDriversResult == ZE_RESULT_SUCCESS) { + InitDriversSupported = true; } else { const char *ErrorString = "Unknown"; - zeParseError(GlobalAdapter->ZeInitDriversResult, ErrorString); + zeParseError(ZeInitDriversResult, ErrorString); UR_LOG(ERR, "\nzeInitDrivers failed with {}\n", ErrorString); } } } - if (GlobalAdapter->ZeInitResult == ZE_RESULT_SUCCESS || - GlobalAdapter->ZeInitDriversResult == ZE_RESULT_SUCCESS) { - GlobalAdapter->ZeResult = ZE_RESULT_SUCCESS; - } else { - GlobalAdapter->ZeResult = ZE_RESULT_ERROR_UNINITIALIZED; + if (ZeInitResult != ZE_RESULT_SUCCESS && + ZeInitDriversResult != ZE_RESULT_SUCCESS) { + // Absorb the ZE_RESULT_ERROR_UNINITIALIZED and just return 0 Platforms. + UR_LOG(ERR, "Level Zero Uninitialized\n"); + return; } - } - 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 (*GlobalAdapter->ZeResult == ZE_RESULT_ERROR_UNINITIALIZED) { - UR_LOG(ERR, "Level Zero Uninitialized\n"); - result = std::move(platforms); - return; - } - if (*GlobalAdapter->ZeResult != ZE_RESULT_SUCCESS) { - UR_LOG(ERR, "Level Zero initialization failure\n"); - result = ze2urResult(*GlobalAdapter->ZeResult); - return; - } + PlatformVec platforms; #ifdef UR_ADAPTER_LEVEL_ZERO_V2 - auto [useV2, reason] = shouldUseV2Adapter(); - if (!useV2) { - UR_LOG(INFO, "Skipping L0 V2 adapter: {}", reason); - result = std::move(platforms); - return; - } + auto [useV2, reason] = shouldUseV2Adapter(); + if (!useV2) { + UR_LOG(INFO, "Skipping L0 V2 adapter: {}", reason); + return; + } #else - auto [useV1, reason] = shouldUseV1Adapter(); - if (!useV1) { - UR_LOG(INFO, "Skipping L0 V1 adapter: {}", reason); - result = std::move(platforms); - return; - } + auto [useV1, reason] = shouldUseV1Adapter(); + if (!useV1) { + UR_LOG(INFO, "Skipping L0 V1 adapter: {}", reason); + return; + } #endif - // Check if the user has enabled the default L0 SysMan initialization. - const int UrSysmanZesinitEnable = [&UserForcedSysManInit] { - const char *UrRet = std::getenv("UR_L0_ENABLE_ZESINIT_DEFAULT"); - if (!UrRet) - return 0; - UserForcedSysManInit &= 2; - return std::atoi(UrRet); - }(); - - bool ZesInitNeeded = UrSysmanZesinitEnable && !UrSysManEnvInitEnabled; - // Unless the user has forced the SysMan init, we will check the device - // version to see if the zesInit is needed. - if (UserForcedSysManInit == 0 && - checkDeviceIntelGPUIpVersionOrNewer(0x05004000) == UR_RESULT_SUCCESS) { - if (UrSysManEnvInitEnabled) { - setEnvVar("ZES_ENABLE_SYSMAN", "0"); + // Check if the user has enabled the default L0 SysMan initialization. + const int UrSysmanZesinitEnable = [&UserForcedSysManInit] { + const char *UrRet = std::getenv("UR_L0_ENABLE_ZESINIT_DEFAULT"); + if (!UrRet) + return 0; + UserForcedSysManInit &= 2; + return std::atoi(UrRet); + }(); + + bool ZesInitNeeded = UrSysmanZesinitEnable && !UrSysManEnvInitEnabled; + // Unless the user has forced the SysMan init, we will check the device + // version to see if the zesInit is needed. + if (UserForcedSysManInit == 0 && checkDeviceIntelGPUIpVersionOrNewer( + 0x05004000) == UR_RESULT_SUCCESS) { + if (UrSysManEnvInitEnabled) { + setEnvVar("ZES_ENABLE_SYSMAN", "0"); + } + ZesInitNeeded = true; } - ZesInitNeeded = true; - } - if (ZesInitNeeded) { + if (ZesInitNeeded) { #ifdef UR_STATIC_LEVEL_ZERO - GlobalAdapter->getDeviceByUUIdFunctionPtr = zesDriverGetDeviceByUuidExp; - GlobalAdapter->getSysManDriversFunctionPtr = zesDriverGet; - GlobalAdapter->sysManInitFunctionPtr = zesInit; + getDeviceByUUIdFunctionPtr = zesDriverGetDeviceByUuidExp; + getSysManDriversFunctionPtr = zesDriverGet; + sysManInitFunctionPtr = zesInit; #else - GlobalAdapter->getDeviceByUUIdFunctionPtr = - (zes_pfnDriverGetDeviceByUuidExp_t) - ur_loader::LibLoader::getFunctionPtr( - GlobalAdapter->processHandle, "zesDriverGetDeviceByUuidExp"); - GlobalAdapter->getSysManDriversFunctionPtr = - (zes_pfnDriverGet_t)ur_loader::LibLoader::getFunctionPtr( - GlobalAdapter->processHandle, "zesDriverGet"); - GlobalAdapter->sysManInitFunctionPtr = - (zes_pfnInit_t)ur_loader::LibLoader::getFunctionPtr( - GlobalAdapter->processHandle, "zesInit"); + getDeviceByUUIdFunctionPtr = (zes_pfnDriverGetDeviceByUuidExp_t) + ur_loader::LibLoader::getFunctionPtr(processHandle, + "zesDriverGetDeviceByUuidExp"); + getSysManDriversFunctionPtr = + (zes_pfnDriverGet_t)ur_loader::LibLoader::getFunctionPtr( + processHandle, "zesDriverGet"); + sysManInitFunctionPtr = + (zes_pfnInit_t)ur_loader::LibLoader::getFunctionPtr(processHandle, + "zesInit"); #endif - } - if (GlobalAdapter->getDeviceByUUIdFunctionPtr && - GlobalAdapter->getSysManDriversFunctionPtr && - GlobalAdapter->sysManInitFunctionPtr) { - ze_init_flags_t L0ZesInitFlags = 0; - UR_LOG(DEBUG, "\nzesInit with flags value of {}\n", - static_cast(L0ZesInitFlags)); - GlobalAdapter->ZesResult = ZE_CALL_NOCHECK( - GlobalAdapter->sysManInitFunctionPtr, (L0ZesInitFlags)); - } else { - GlobalAdapter->ZesResult = ZE_RESULT_ERROR_UNINITIALIZED; - } + } + if (getDeviceByUUIdFunctionPtr && getSysManDriversFunctionPtr && + sysManInitFunctionPtr) { + ze_init_flags_t L0ZesInitFlags = 0; + UR_LOG(DEBUG, "\nzesInit with flags value of {}\n", + static_cast(L0ZesInitFlags)); + ZesResult = ZE_CALL_NOCHECK(sysManInitFunctionPtr, (L0ZesInitFlags)); + } else { + ZesResult = ZE_RESULT_ERROR_UNINITIALIZED; + } - ur_result_t err = initPlatforms(platforms, GlobalAdapter->ZesResult); - if (err == UR_RESULT_SUCCESS) { - result = std::move(platforms); - } else { - result = err; - } - }; + ur_result_t err = initPlatforms(this, platforms, ZesResult); + if (err == UR_RESULT_SUCCESS) { + Platforms = std::move(platforms); + } else { + throw err; + } } void globalAdapterOnDemandCleanup() { @@ -623,15 +599,25 @@ ur_result_t urAdapterGet( /// ::urAdapterGet shall only retrieve that number of platforms. ur_adapter_handle_t *Adapters, /// [out][optional] returns the total number of adapters available. - uint32_t *NumAdapters) { + uint32_t *NumAdapters) try { static std::mutex AdapterConstructionMutex{}; - if (NumEntries > 0 && Adapters) { - std::lock_guard Lock{AdapterConstructionMutex}; + // We need to initialize the adapter even if user only queries + // the number of adapters to decided whether to use V1 or V2. + std::lock_guard Lock{AdapterConstructionMutex}; + + if (!GlobalAdapter) { + GlobalAdapter = new ur_adapter_handle_t_(); + } - if (!GlobalAdapter) { - GlobalAdapter = new ur_adapter_handle_t_(); + if (GlobalAdapter->Platforms.size() == 0) { + if (NumAdapters) { + *NumAdapters = 0; } + return UR_RESULT_ERROR_UNSUPPORTED_VERSION; + } + + if (NumEntries && Adapters) { *Adapters = GlobalAdapter; if (GlobalAdapter->RefCount.retain() == 0) { @@ -644,6 +630,10 @@ ur_result_t urAdapterGet( } return UR_RESULT_SUCCESS; +} catch (ur_result_t result) { + return result; +} catch (...) { + return UR_RESULT_ERROR_UNKNOWN; } ur_result_t urAdapterRelease([[maybe_unused]] ur_adapter_handle_t Adapter) { diff --git a/unified-runtime/source/adapters/level_zero/adapter.hpp b/unified-runtime/source/adapters/level_zero/adapter.hpp index 890e39d296672..0c0ce9a9f12d4 100644 --- a/unified-runtime/source/adapters/level_zero/adapter.hpp +++ b/unified-runtime/source/adapters/level_zero/adapter.hpp @@ -37,11 +37,7 @@ struct ur_adapter_handle_t_ : ur::handle_base { uint32_t ZeInitDriversCount = 0; bool InitDriversSupported = false; - ze_result_t ZeInitDriversResult; - ze_result_t ZeInitResult; - ze_result_t ZesResult; - std::optional ZeResult; - ZeCache> PlatformCache; + PlatformVec Platforms; logger::Logger &logger; HMODULE processHandle = nullptr; diff --git a/unified-runtime/source/adapters/level_zero/device.cpp b/unified-runtime/source/adapters/level_zero/device.cpp index 897dc5e659718..167c7cb856b10 100644 --- a/unified-runtime/source/adapters/level_zero/device.cpp +++ b/unified-runtime/source/adapters/level_zero/device.cpp @@ -1583,12 +1583,8 @@ ur_result_t urDeviceCreateWithNativeHandle( // a valid Level Zero device. ur_device_handle_t Dev = nullptr; - if (const auto *platforms = GlobalAdapter->PlatformCache->get_value()) { - for (const auto &p : *platforms) { - Dev = p->getDeviceFromNativeHandle(ZeDevice); - } - } else { - return GlobalAdapter->PlatformCache->get_error(); + for (const auto &p : GlobalAdapter->Platforms) { + Dev = p->getDeviceFromNativeHandle(ZeDevice); } if (Dev == nullptr) diff --git a/unified-runtime/source/adapters/level_zero/platform.cpp b/unified-runtime/source/adapters/level_zero/platform.cpp index 50a2ce1080964..47f2e519bb723 100644 --- a/unified-runtime/source/adapters/level_zero/platform.cpp +++ b/unified-runtime/source/adapters/level_zero/platform.cpp @@ -28,20 +28,15 @@ ur_result_t urPlatformGet( uint32_t *NumPlatforms) { // 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 = GlobalAdapter->PlatformCache->get_value(); - cached_platforms) { - uint32_t nplatforms = (uint32_t)cached_platforms->size(); - if (NumPlatforms) { - *NumPlatforms = nplatforms; - } + uint32_t nplatforms = (uint32_t)GlobalAdapter->Platforms.size(); + if (NumPlatforms) { + *NumPlatforms = nplatforms; + } if (Platforms) { for (uint32_t i = 0; i < std::min(nplatforms, NumEntries); ++i) { - Platforms[i] = cached_platforms->at(i).get(); + Platforms[i] = GlobalAdapter->Platforms.at(i).get(); } } - } else { - return GlobalAdapter->PlatformCache->get_error(); - } return UR_RESULT_SUCCESS; } diff --git a/unified-runtime/source/loader/ur_ldrddi.cpp b/unified-runtime/source/loader/ur_ldrddi.cpp index 2ddf9f21c95cf..d8dfaf8e8a9d4 100644 --- a/unified-runtime/source/loader/ur_ldrddi.cpp +++ b/unified-runtime/source/loader/ur_ldrddi.cpp @@ -34,21 +34,21 @@ __urdlllocal ur_result_t UR_APICALL urAdapterGet( auto context = getContext(); - size_t adapterIndex = 0; - if (nullptr != phAdapters && NumEntries != 0) { - for (auto &platform : context->platforms) { - if (platform.initStatus != UR_RESULT_SUCCESS) - continue; - platform.dditable.Adapter.pfnGet(1, &phAdapters[adapterIndex], nullptr); - adapterIndex++; - if (adapterIndex == NumEntries) { - break; - } - } + uint32_t numAdapters = 0; + for (auto &platform : context->platforms) { + if (platform.initStatus != UR_RESULT_SUCCESS) + continue; + + uint32_t adapter; + ur_adapter_handle_t *adapterHandle = + numAdapters < NumEntries ? &phAdapters[numAdapters] : nullptr; + platform.dditable.Adapter.pfnGet(1, adapterHandle, &adapter); + + numAdapters += adapter; } if (pNumAdapters != nullptr) { - *pNumAdapters = static_cast(context->platforms.size()); + *pNumAdapters = numAdapters; } return UR_RESULT_SUCCESS; From 0466f0d4d906265c8516e2a3be0b15889a68b4fe Mon Sep 17 00:00:00 2001 From: Igor Chorazewicz Date: Mon, 14 Jul 2025 16:19:09 +0000 Subject: [PATCH 5/5] Remove XFAIL --- sycl/test-e2e/Regression/reduction_resource_leak_dw.cpp | 2 -- 1 file changed, 2 deletions(-) diff --git a/sycl/test-e2e/Regression/reduction_resource_leak_dw.cpp b/sycl/test-e2e/Regression/reduction_resource_leak_dw.cpp index b904baf169966..ccf79b6c4d27e 100644 --- a/sycl/test-e2e/Regression/reduction_resource_leak_dw.cpp +++ b/sycl/test-e2e/Regression/reduction_resource_leak_dw.cpp @@ -1,6 +1,4 @@ // REQUIRES: level_zero, level_zero_dev_kit -// XFAIL: windows && run-mode -// XFAIL-TRACKER: https://github.com/intel/llvm/issues/16418 // // RUN: %{build} %level_zero_options -o %t.out // RUN: %{l0_leak_check} %{run} %t.out 2>&1 | FileCheck %s