Skip to content

[UR] [L0] Update UR to link the Loader as static #17104

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Mar 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions .github/workflows/ur-build-hw.yml
Original file line number Diff line number Diff line change
Expand Up @@ -136,12 +136,18 @@ jobs:
run: cmake --install ${{github.workspace}}/build

- name: Test adapter specific
run: ctest -C ${{matrix.build_type}} --test-dir ${{github.workspace}}/build --output-on-failure -L "adapter-specific" -E "memcheck" --timeout 600
env:
ZE_ENABLE_LOADER_DEBUG_TRACE: 1
ZE_DEBUG: 1
run: ctest -C ${{matrix.build_type}} --test-dir ${{github.workspace}}/build --output-on-failure -L "adapter-specific" -E "memcheck" --timeout 600 -VV
# Don't run adapter specific tests when building multiple adapters
if: ${{ matrix.adapter.other_name == '' }}

- name: Test adapters
run: env UR_CTS_ADAPTER_PLATFORM="${{matrix.adapter.platform}}" ctest -C ${{matrix.build_type}} --test-dir ${{github.workspace}}/build --output-on-failure -L "conformance" --timeout 600
env:
ZE_ENABLE_LOADER_DEBUG_TRACE: 1
ZE_DEBUG: 1
run: env UR_CTS_ADAPTER_PLATFORM="${{matrix.adapter.platform}}" ctest -C ${{matrix.build_type}} --test-dir ${{github.workspace}}/build --output-on-failure -L "conformance" --timeout 600 -VV
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we move UR_CTS_ADAPTER_PLATFORM to env: section as well?

Copy link
Contributor Author

@nrspruit nrspruit Feb 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that would be best added in a separate PR to improve the settings in the UR workflows. The additional L0 envs were for seeing when the loader is init in case of failures.


- name: Get information about platform
if: ${{ always() }}
Expand Down
3 changes: 2 additions & 1 deletion unified-runtime/cmake/FetchLevelZero.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,13 @@ if (NOT DEFINED LEVEL_ZERO_LIBRARY OR NOT DEFINED LEVEL_ZERO_INCLUDE_DIR)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-c++98-compat-extra-semi")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-unknown-warning-option")
endif()
set(BUILD_STATIC ON)

if (UR_LEVEL_ZERO_LOADER_REPO STREQUAL "")
set(UR_LEVEL_ZERO_LOADER_REPO "https://github.com/oneapi-src/level-zero.git")
endif()
if (UR_LEVEL_ZERO_LOADER_TAG STREQUAL "")
set(UR_LEVEL_ZERO_LOADER_TAG v1.19.2)
set(UR_LEVEL_ZERO_LOADER_TAG v1.21.1)
endif()

# Disable due to a bug https://github.com/oneapi-src/level-zero/issues/104
Expand Down
5 changes: 3 additions & 2 deletions unified-runtime/source/adapters/level_zero/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,14 @@ if(UR_BUILD_ADAPTER_L0)
)
install_ur_library(ur_adapter_level_zero)

target_compile_definitions(ur_adapter_level_zero PUBLIC UR_STATIC_LEVEL_ZERO)
if(UR_STATIC_ADAPTER_L0)
target_compile_definitions(ur_adapter_level_zero PUBLIC UR_STATIC_ADAPTER_LEVEL_ZERO)

# 'utils' target from 'level-zero-loader' includes path which is prefixed
# in the source directory, this breaks the installation of 'utils' target.
set_target_properties(utils PROPERTIES INTERFACE_INCLUDE_DIRECTORIES "")
install(TARGETS ur_umf LevelZeroLoader LevelZeroLoader-Headers ComputeRuntimeLevelZero-Headers ze_loader utils
set_target_properties(level_zero_utils PROPERTIES INTERFACE_INCLUDE_DIRECTORIES "")
install(TARGETS ur_umf LevelZeroLoader LevelZeroLoader-Headers ComputeRuntimeLevelZero-Headers ze_loader level_zero_utils
EXPORT ${PROJECT_NAME}-targets
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
Expand Down
40 changes: 28 additions & 12 deletions unified-runtime/source/adapters/level_zero/adapter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,12 @@ ur_adapter_handle_t_::ur_adapter_handle_t_()
ZeInitResult = ZE_RESULT_ERROR_UNINITIALIZED;
ZesResult = ZE_RESULT_ERROR_UNINITIALIZED;

#ifdef UR_STATIC_LEVEL_ZERO
// Given static linking of the L0 Loader, we must delay the loader's
// destruction of its context until after the UR Adapter is destroyed.
zelSetDelayLoaderContextTeardown();
#endif

if (UrL0Debug & UR_L0_DEBUG_BASIC) {
logger.setLegacySink(std::make_unique<ur_legacy_sink>());
};
Expand Down Expand Up @@ -335,10 +341,12 @@ ur_adapter_handle_t_::ur_adapter_handle_t_()
// Dynamically load the new L0 apis separately.
// This must be done to avoid attempting to use symbols that do
// not exist in older loader runtimes.
#ifndef UR_STATIC_LEVEL_ZERO
#ifdef _WIN32
HMODULE processHandle = GetModuleHandle(NULL);
GlobalAdapter->processHandle = GetModuleHandle(NULL);
#else
HMODULE processHandle = nullptr;
GlobalAdapter->processHandle = nullptr;
#endif
#endif

// initialize level zero only once.
Expand Down Expand Up @@ -412,9 +420,13 @@ ur_adapter_handle_t_::ur_adapter_handle_t_()
}

if (useInitDrivers) {
#ifdef UR_STATIC_LEVEL_ZERO
GlobalAdapter->initDriversFunctionPtr = zeInitDrivers;
#else
GlobalAdapter->initDriversFunctionPtr =
(ze_pfnInitDrivers_t)ur_loader::LibLoader::getFunctionPtr(
processHandle, "zeInitDrivers");
GlobalAdapter->processHandle, "zeInitDrivers");
#endif
if (GlobalAdapter->initDriversFunctionPtr) {
logger::debug("\nzeInitDrivers with flags value of {}\n",
static_cast<int>(GlobalAdapter->InitDriversDesc.flags));
Expand Down Expand Up @@ -455,14 +467,6 @@ ur_adapter_handle_t_::ur_adapter_handle_t_()

return;
}
// Dynamically load the new L0 SysMan separate init and new EXP apis
// separately. This must be done to avoid attempting to use symbols that do
// not exist in older loader runtimes.
#ifdef _WIN32
GlobalAdapter->processHandle = GetModuleHandle(NULL);
#else
GlobalAdapter->processHandle = nullptr;
#endif

// Check if the user has enabled the default L0 SysMan initialization.
const int UrSysmanZesinitEnable = [&UserForcedSysManInit] {
Expand All @@ -484,6 +488,11 @@ ur_adapter_handle_t_::ur_adapter_handle_t_()
ZesInitNeeded = true;
}
if (ZesInitNeeded) {
#ifdef UR_STATIC_LEVEL_ZERO
GlobalAdapter->getDeviceByUUIdFunctionPtr = zesDriverGetDeviceByUuidExp;
GlobalAdapter->getSysManDriversFunctionPtr = zesDriverGet;
GlobalAdapter->sysManInitFunctionPtr = zesInit;
#else
GlobalAdapter->getDeviceByUUIdFunctionPtr =
(zes_pfnDriverGetDeviceByUuidExp_t)
ur_loader::LibLoader::getFunctionPtr(
Expand All @@ -494,6 +503,7 @@ ur_adapter_handle_t_::ur_adapter_handle_t_()
GlobalAdapter->sysManInitFunctionPtr =
(zes_pfnInit_t)ur_loader::LibLoader::getFunctionPtr(
GlobalAdapter->processHandle, "zesInit");
#endif
}
if (GlobalAdapter->getDeviceByUUIdFunctionPtr &&
GlobalAdapter->getSysManDriversFunctionPtr &&
Expand Down Expand Up @@ -668,7 +678,13 @@ ur_result_t urAdapterRelease(ur_adapter_handle_t) {
if (GlobalAdapter) {
std::lock_guard<std::mutex> Lock{GlobalAdapter->Mutex};
if (--GlobalAdapter->RefCount == 0) {
return adapterStateTeardown();
auto result = adapterStateTeardown();
#ifdef UR_STATIC_LEVEL_ZERO
// Given static linking of the L0 Loader, we must delay the loader's
// destruction of its context until after the UR Adapter is destroyed.
zelLoaderContextTeardown();
#endif
return result;
}
}

Expand Down
47 changes: 34 additions & 13 deletions unified-runtime/source/adapters/level_zero/platform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -354,52 +354,67 @@ ur_result_t ur_platform_handle_t_::initialize() {
// Check if mutable command list extension is supported and initialize
// function pointers.
if (MutableCommandListSpecExtensionSupported) {
#ifdef UR_STATIC_LEVEL_ZERO
ZeMutableCmdListExt.zexCommandListGetNextCommandIdExp =
zeCommandListGetNextCommandIdExp;
ZeMutableCmdListExt.zexCommandListUpdateMutableCommandsExp =
zeCommandListUpdateMutableCommandsExp;
ZeMutableCmdListExt.zexCommandListUpdateMutableCommandSignalEventExp =
zeCommandListUpdateMutableCommandSignalEventExp;
ZeMutableCmdListExt.zexCommandListUpdateMutableCommandWaitEventsExp =
zeCommandListUpdateMutableCommandWaitEventsExp;
ZeMutableCmdListExt.zexCommandListUpdateMutableCommandKernelsExp =
zeCommandListUpdateMutableCommandKernelsExp;
ZeMutableCmdListExt.zexCommandListGetNextCommandIdWithKernelsExp =
zeCommandListGetNextCommandIdWithKernelsExp;
#else
ZeMutableCmdListExt.zexCommandListGetNextCommandIdExp =
(ze_pfnCommandListGetNextCommandIdExp_t)
ur_loader::LibLoader::getFunctionPtr(
GlobalAdapter->processHandle,
"zeCommandListGetNextCommandIdExp");
ZeMutableCmdListExt.Supported |=
ZeMutableCmdListExt.zexCommandListGetNextCommandIdExp != nullptr;
ZeMutableCmdListExt.zexCommandListUpdateMutableCommandsExp =
(ze_pfnCommandListUpdateMutableCommandsExp_t)
ur_loader::LibLoader::getFunctionPtr(
GlobalAdapter->processHandle,
"zeCommandListUpdateMutableCommandsExp");
ZeMutableCmdListExt.Supported |=
ZeMutableCmdListExt.zexCommandListUpdateMutableCommandsExp != nullptr;
ZeMutableCmdListExt.zexCommandListUpdateMutableCommandSignalEventExp =
(ze_pfnCommandListUpdateMutableCommandSignalEventExp_t)
ur_loader::LibLoader::getFunctionPtr(
GlobalAdapter->processHandle,
"zeCommandListUpdateMutableCommandSignalEventExp");
ZeMutableCmdListExt.Supported |=
ZeMutableCmdListExt.zexCommandListUpdateMutableCommandSignalEventExp !=
nullptr;
ZeMutableCmdListExt.zexCommandListUpdateMutableCommandWaitEventsExp =
(ze_pfnCommandListUpdateMutableCommandWaitEventsExp_t)
ur_loader::LibLoader::getFunctionPtr(
GlobalAdapter->processHandle,
"zeCommandListUpdateMutableCommandWaitEventsExp");
ZeMutableCmdListExt.Supported |=
ZeMutableCmdListExt.zexCommandListUpdateMutableCommandWaitEventsExp !=
nullptr;
ZeMutableCmdListExt.zexCommandListUpdateMutableCommandKernelsExp =
(ze_pfnCommandListUpdateMutableCommandKernelsExp_t)
ur_loader::LibLoader::getFunctionPtr(
GlobalAdapter->processHandle,
"zeCommandListUpdateMutableCommandKernelsExp");
ZeMutableCmdListExt.Supported |=
ZeMutableCmdListExt.zexCommandListUpdateMutableCommandKernelsExp !=
nullptr;
ZeMutableCmdListExt.zexCommandListGetNextCommandIdWithKernelsExp =
(ze_pfnCommandListGetNextCommandIdWithKernelsExp_t)
ur_loader::LibLoader::getFunctionPtr(
GlobalAdapter->processHandle,
"zeCommandListGetNextCommandIdWithKernelsExp");
#endif
ZeMutableCmdListExt.Supported |=
ZeMutableCmdListExt.zexCommandListGetNextCommandIdExp != nullptr;
ZeMutableCmdListExt.Supported |=
ZeMutableCmdListExt.zexCommandListGetNextCommandIdWithKernelsExp !=
nullptr;
ZeMutableCmdListExt.Supported |=
ZeMutableCmdListExt.zexCommandListUpdateMutableCommandKernelsExp !=
nullptr;
ZeMutableCmdListExt.Supported |=
ZeMutableCmdListExt.zexCommandListUpdateMutableCommandWaitEventsExp !=
nullptr;
ZeMutableCmdListExt.Supported |=
ZeMutableCmdListExt.zexCommandListUpdateMutableCommandSignalEventExp !=
nullptr;
ZeMutableCmdListExt.Supported |=
ZeMutableCmdListExt.zexCommandListUpdateMutableCommandsExp != nullptr;
ZeMutableCmdListExt.LoaderExtension = true;
} else {
ZeMutableCmdListExt.Supported |=
Expand Down Expand Up @@ -455,12 +470,18 @@ ur_result_t ur_platform_handle_t_::initialize() {
// Check if ImmediateAppendCommandList is supported and initialize the
// function pointer.
if (ZeImmediateCommandListAppendExtensionFound) {
#ifdef UR_STATIC_LEVEL_ZERO
ZeCommandListImmediateAppendExt
.zeCommandListImmediateAppendCommandListsExp =
zeCommandListImmediateAppendCommandListsExp;
#else
ZeCommandListImmediateAppendExt
.zeCommandListImmediateAppendCommandListsExp =
(ze_pfnCommandListImmediateAppendCommandListsExp_t)
ur_loader::LibLoader::getFunctionPtr(
GlobalAdapter->processHandle,
"zeCommandListImmediateAppendCommandListsExp");
#endif
ZeCommandListImmediateAppendExt.Supported =
ZeCommandListImmediateAppendExt
.zeCommandListImmediateAppendCommandListsExp != nullptr;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ TEST_P(urLevelZeroKernelNativeHandleTest, OwnedHandleRelease) {
moduleDesc.pBuildFlags = "";
ze_module_handle_t module;

// Initialize Level Zero driver is required if this test is linked statically
// with Level Zero loader, the driver will not be init otherwise.
zeInit(ZE_INIT_FLAG_GPU_ONLY);

ASSERT_EQ(
zeModuleCreate(native_context, native_device, &moduleDesc, &module, NULL),
ZE_RESULT_SUCCESS);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,14 @@
#include <unordered_set>
#include <vector>

struct CommandListCacheTest : public uur::urContextTest {};
struct CommandListCacheTest : public uur::urContextTest {
void SetUp() override {
// Initialize Level Zero driver is required if this test is linked
// statically with Level Zero loader, the driver will not be init otherwise.
zeInit(ZE_INIT_FLAG_GPU_ONLY);
urContextTest::SetUp();
}
};

UUR_INSTANTIATE_DEVICE_TEST_SUITE(CommandListCacheTest);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@

struct urEnqueueKernelLaunchTest : uur::urKernelExecutionTest {
void SetUp() override {
// Initialize Level Zero driver is required if this test is linked
// statically with Level Zero loader, the driver will not be init otherwise.
zeInit(ZE_INIT_FLAG_GPU_ONLY);
program_name = "fill";
UUR_RETURN_ON_FATAL_FAILURE(urKernelExecutionTest::SetUp());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,10 @@ struct EventPoolTest : public uur::urQueueTestWithParam<ProviderParams> {

auto params = getParam();

// Initialize Level Zero driver is required if this test is linked
// statically with Level Zero loader, the driver will not be init otherwise.
zeInit(ZE_INIT_FLAG_GPU_ONLY);

mockVec.push_back(device);

cache = std::unique_ptr<event_pool_cache>(new event_pool_cache(
Expand Down