Skip to content

Commit c8b0eb3

Browse files
committed
Add Support for optional ddi loading and ULTs
Signed-off-by: Neil R. Spruit <[email protected]>
1 parent d6f2c9f commit c8b0eb3

File tree

9 files changed

+228
-17
lines changed

9 files changed

+228
-17
lines changed

.github/workflows/build-quick-static.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ jobs:
3535
-D CMAKE_C_COMPILER_LAUNCHER=ccache \
3636
-D CMAKE_CXX_COMPILER_LAUNCHER=ccache \
3737
-D CMAKE_BUILD_TYPE=Release \
38+
-D BUILD_L0_LOADER_TESTS=1 \
3839
-D BUILD_STATIC=0 \
3940
..
4041
make -j$(nproc)

scripts/templates/ze_loader_internal.h.mako

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ namespace loader
7171
ze_driver_handle_t zerDriverHandle = nullptr;
7272
ze_api_version_t versionRequested = ZE_API_VERSION_CURRENT;
7373
bool ddiInitialized = false;
74+
bool customDriver = false;
7475
ze_result_t zeddiInitResult = ZE_RESULT_ERROR_UNINITIALIZED;
7576
ze_result_t zetddiInitResult = ZE_RESULT_ERROR_UNINITIALIZED;
7677
ze_result_t zesddiInitResult = ZE_RESULT_ERROR_UNINITIALIZED;

source/loader/driver_discovery.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,13 @@
1313

1414
namespace loader {
1515

16-
using DriverLibraryPath = std::string;
16+
struct DriverLibraryPath {
17+
std::string path;
18+
bool customDriver;
19+
20+
DriverLibraryPath(const std::string& p, bool isCustom = false)
21+
: path(p), customDriver(isCustom) {}
22+
};
1723

1824
std::vector<DriverLibraryPath> discoverEnabledDrivers();
1925

source/loader/linux/driver_discovery_lin.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,17 +113,19 @@ std::vector<DriverLibraryPath> discoverEnabledDrivers() {
113113
// ZE_ENABLE_ALT_DRIVERS is for development/debug only
114114
altDrivers = getenv("ZE_ENABLE_ALT_DRIVERS");
115115
if (altDrivers == nullptr) {
116+
// Standard drivers - not custom
116117
for (auto path : knownDriverNames) {
117118
if (libraryExistsInSearchPaths(path)) {
118-
enabledDrivers.emplace_back(path);
119+
enabledDrivers.emplace_back(path, false);
119120
}
120121
}
121122
} else {
123+
// Alternative drivers from environment variable - these are custom
122124
std::stringstream ss(altDrivers);
123125
while (ss.good()) {
124126
std::string substr;
125127
getline(ss, substr, ',');
126-
enabledDrivers.emplace_back(substr);
128+
enabledDrivers.emplace_back(substr, true);
127129
}
128130
}
129131
return enabledDrivers;

source/loader/windows/driver_discovery_win.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,18 @@ std::vector<DriverLibraryPath> discoverEnabledDrivers() {
3333
// ZE_ENABLE_ALT_DRIVERS is for development/debug only
3434
envBufferSize = GetEnvironmentVariable("ZE_ENABLE_ALT_DRIVERS", &altDrivers[0], envBufferSize);
3535
if (!envBufferSize) {
36+
// Standard drivers discovered from registry - not custom
3637
auto displayDrivers = discoverDriversBasedOnDisplayAdapters(GUID_DEVCLASS_DISPLAY);
3738
auto computeDrivers = discoverDriversBasedOnDisplayAdapters(GUID_DEVCLASS_COMPUTEACCELERATOR);
3839
enabledDrivers.insert(enabledDrivers.end(), displayDrivers.begin(), displayDrivers.end());
3940
enabledDrivers.insert(enabledDrivers.end(), computeDrivers.begin(), computeDrivers.end());
4041
} else {
42+
// Alternative drivers from environment variable - these are custom
4143
std::stringstream ss(altDrivers.c_str());
4244
while (ss.good()) {
4345
std::string substr;
4446
getline(ss, substr, ',');
45-
enabledDrivers.emplace_back(substr);
47+
enabledDrivers.emplace_back(substr, true);
4648
}
4749
}
4850

@@ -110,7 +112,7 @@ DriverLibraryPath readDriverPathForDisplayAdapter(DEVINST dnDevNode) {
110112

111113
if (CR_SUCCESS != configErr) {
112114
assert(false && "CM_Open_DevNode_Key failed");
113-
return "";
115+
return DriverLibraryPath("", false);
114116
}
115117

116118
DWORD regValueType = {};
@@ -133,7 +135,7 @@ DriverLibraryPath readDriverPathForDisplayAdapter(DEVINST dnDevNode) {
133135
regOpStatus = RegCloseKey(hkey);
134136
assert((ERROR_SUCCESS == regOpStatus) && "RegCloseKey failed");
135137

136-
return driverPath;
138+
return DriverLibraryPath(driverPath, false);
137139
}
138140

139141
std::wstring readDisplayAdaptersDeviceIdsList(const GUID rguid) {
@@ -193,11 +195,12 @@ std::vector<DriverLibraryPath> discoverDriversBasedOnDisplayAdapters(const GUID
193195

194196
auto driverPath = readDriverPathForDisplayAdapter(devinst);
195197

196-
if (driverPath.empty()) {
198+
if (driverPath.path.empty()) {
197199
continue;
198200
}
199201

200-
bool alreadyOnTheList = (enabledDrivers.end() != std::find(enabledDrivers.begin(), enabledDrivers.end(), driverPath));
202+
bool alreadyOnTheList = (enabledDrivers.end() != std::find_if(enabledDrivers.begin(), enabledDrivers.end(),
203+
[&driverPath](const DriverLibraryPath& d) { return d.path == driverPath.path; }));
201204
if (alreadyOnTheList) {
202205
continue;
203206
}

source/loader/ze_loader.cpp

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -510,19 +510,21 @@ namespace loader
510510
std::string message = "init driver " + driver.name + " found GPU Supported Driver.";
511511
debug_trace_message(message, "");
512512
}
513+
loadDriver = true;
513514
}
514-
loadDriver = true;
515515
}
516516
if ((!desc && (flags == 0 || flags & ZE_INIT_FLAG_VPU_ONLY)) || (desc && desc->flags & ZE_INIT_DRIVER_TYPE_FLAG_NPU)) {
517517
if (driver.name.find("vpu") != std::string::npos || driver.name.find("npu") != std::string::npos) {
518518
if (debugTraceEnabled) {
519519
std::string message = "init driver " + driver.name + " found VPU/NPU Supported Driver.";
520520
debug_trace_message(message, "");
521521
}
522+
loadDriver = true;
522523
}
523-
loadDriver = true;
524524
}
525525

526+
loadDriver = !driver.handle && driver.customDriver ? true : loadDriver;
527+
526528
if (loadDriver && !driver.handle) {
527529
auto handle = LOAD_DRIVER_LIBRARY( driver.name.c_str() );
528530
if( NULL != handle )
@@ -584,6 +586,14 @@ namespace loader
584586
driver.ddiInitialized = true;
585587
}
586588

589+
if (!driver.handle && !driver.ddiInitialized) {
590+
if (debugTraceEnabled) {
591+
std::string message = "init driver " + driver.name + " does not match the requested flags or desc, skipping driver.";
592+
debug_trace_message(message, "");
593+
}
594+
return ZE_RESULT_ERROR_UNINITIALIZED;
595+
}
596+
587597
return ZE_RESULT_SUCCESS;
588598
}
589599

@@ -682,14 +692,14 @@ namespace loader
682692
}
683693
}
684694

685-
for( auto name : discoveredDrivers )
695+
for( auto driverInfo : discoveredDrivers )
686696
{
687697
if (discoveredDrivers.size() == 1) {
688-
auto handle = LOAD_DRIVER_LIBRARY( name.c_str() );
698+
auto handle = LOAD_DRIVER_LIBRARY( driverInfo.path.c_str() );
689699
if( NULL != handle )
690700
{
691701
if (debugTraceEnabled) {
692-
std::string message = "Loading Driver " + name + " succeeded";
702+
std::string message = "Loading Driver " + driverInfo.path + " succeeded";
693703
#if !defined(_WIN32) && !defined(ANDROID)
694704
// TODO: implement same message for windows, move dlinfo to ze_util.h as a macro
695705
struct link_map *dlinfo_map;
@@ -701,17 +711,19 @@ namespace loader
701711
}
702712
allDrivers.emplace_back();
703713
allDrivers.rbegin()->handle = handle;
704-
allDrivers.rbegin()->name = name;
714+
allDrivers.rbegin()->name = driverInfo.path;
715+
allDrivers.rbegin()->customDriver = driverInfo.customDriver;
705716
} else if (debugTraceEnabled) {
706717
GET_LIBRARY_ERROR(loadLibraryErrorValue);
707-
std::string errorMessage = "Load Library of " + name + " failed with ";
718+
std::string errorMessage = "Load Library of " + driverInfo.path + " failed with ";
708719
debug_trace_message(errorMessage, loadLibraryErrorValue);
709720
loadLibraryErrorValue.clear();
710721
}
711722
} else {
712723
allDrivers.emplace_back();
713724
allDrivers.rbegin()->handle = nullptr;
714-
allDrivers.rbegin()->name = name;
725+
allDrivers.rbegin()->name = driverInfo.path;
726+
allDrivers.rbegin()->customDriver = driverInfo.customDriver;
715727
}
716728
}
717729
if(allDrivers.size()==0){

source/loader/ze_loader_internal.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ namespace loader
6262
ze_driver_handle_t zerDriverHandle = nullptr;
6363
ze_api_version_t versionRequested = ZE_API_VERSION_CURRENT;
6464
bool ddiInitialized = false;
65+
bool customDriver = false;
6566
ze_result_t zeddiInitResult = ZE_RESULT_ERROR_UNINITIALIZED;
6667
ze_result_t zetddiInitResult = ZE_RESULT_ERROR_UNINITIALIZED;
6768
ze_result_t zesddiInitResult = ZE_RESULT_ERROR_UNINITIALIZED;

test/CMakeLists.txt

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ add_executable(
1111
# Only include driver_ordering_unit_tests for static builds or non-Windows platforms
1212
# as it requires internal loader symbols that are not exported in Windows DLLs
1313
if(BUILD_STATIC OR NOT WIN32)
14-
target_sources(tests PRIVATE driver_ordering_unit_tests.cpp)
14+
target_sources(tests PRIVATE driver_ordering_unit_tests.cpp init_driver_unit_tests.cpp)
1515
endif()
1616

1717
# For static builds, we need to include the loader source files directly
@@ -69,6 +69,37 @@ if(BUILD_STATIC)
6969
endif()
7070
endif()
7171

72+
# Create fake driver copies for init_driver_unit_tests
73+
if(NOT BUILD_STATIC OR NOT WIN32)
74+
if(MSVC)
75+
add_custom_command(TARGET tests POST_BUILD
76+
COMMAND ${CMAKE_COMMAND} -E copy_if_different
77+
$<TARGET_FILE_DIR:tests>/ze_null.dll
78+
$<TARGET_FILE_DIR:tests>/ze_fake_gpu.dll
79+
COMMAND ${CMAKE_COMMAND} -E copy_if_different
80+
$<TARGET_FILE_DIR:tests>/ze_null_test1.dll
81+
$<TARGET_FILE_DIR:tests>/ze_fake_npu.dll
82+
COMMAND ${CMAKE_COMMAND} -E copy_if_different
83+
$<TARGET_FILE_DIR:tests>/ze_null_test2.dll
84+
$<TARGET_FILE_DIR:tests>/ze_fake_vpu.dll
85+
COMMENT "Copying null drivers to fake driver names for init_driver_unit_tests"
86+
)
87+
else()
88+
add_custom_command(TARGET tests POST_BUILD
89+
COMMAND ${CMAKE_COMMAND} -E copy_if_different
90+
${CMAKE_BINARY_DIR}/lib/libze_null.so.1
91+
${CMAKE_BINARY_DIR}/lib/libze_fake_gpu.so.1
92+
COMMAND ${CMAKE_COMMAND} -E copy_if_different
93+
${CMAKE_BINARY_DIR}/lib/libze_null_test1.so.1
94+
${CMAKE_BINARY_DIR}/lib/libze_fake_npu.so.1
95+
COMMAND ${CMAKE_COMMAND} -E copy_if_different
96+
${CMAKE_BINARY_DIR}/lib/libze_null_test2.so.1
97+
${CMAKE_BINARY_DIR}/lib/libze_fake_vpu.so.1
98+
COMMENT "Copying null drivers to fake driver names for init_driver_unit_tests"
99+
)
100+
endif()
101+
endif()
102+
72103
add_test(NAME tests_api COMMAND tests --gtest_filter=*GivenLevelZeroLoaderPresentWhenCallingzeGetLoaderVersionsAPIThenValidVersionIsReturned*)
73104
set_property(TEST tests_api PROPERTY ENVIRONMENT "ZE_ENABLE_LOADER_DEBUG_TRACE=1;ZE_ENABLE_NULL_DRIVER=1")
74105
add_test(NAME tests_init_gpu_all COMMAND tests --gtest_filter=*GivenLevelZeroLoaderPresentWhenCallingZeInitDriversWithGPUTypeThenExpectPassWithGPUorAllOnly*)
@@ -596,6 +627,9 @@ set_property(TEST driver_ordering_trim_function PROPERTY ENVIRONMENT "ZE_ENABLE_
596627
add_test(NAME driver_ordering_parse_driver_order COMMAND tests --gtest_filter=DriverOrderingHelperFunctionsTest.ParseDriverOrder_*)
597628
set_property(TEST driver_ordering_parse_driver_order PROPERTY ENVIRONMENT "ZE_ENABLE_LOADER_DEBUG_TRACE=1;ZE_ENABLE_NULL_DRIVER=1")
598629

630+
# Init Driver Unit Tests
631+
add_test(NAME init_driver_unit_tests COMMAND tests --gtest_filter=InitDriverUnitTest.*)
632+
set_property(TEST init_driver_unit_tests PROPERTY ENVIRONMENT "ZE_ENABLE_LOADER_DEBUG_TRACE=1;ZE_ENABLE_NULL_DRIVER=1")
599633

600634
# These tests are currently not supported on Windows. The reason is that the std::cerr is not being redirected to a pipe in Windows to be then checked against the expected output.
601635
if(NOT MSVC)

0 commit comments

Comments
 (0)