Skip to content
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

Fix zeInit Compatibility when zeInitDrivers is undefined #232

Merged
merged 2 commits into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 4 additions & 0 deletions scripts/templates/libapi.cpp.mako
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ ${th.make_func_name(n, tags, obj)}(
return result;
});

if (result != ${X}_RESULT_SUCCESS) {
return result;
}

if(ze_lib::context->inTeardown) {
return ${X}_RESULT_ERROR_UNINITIALIZED;
}
Expand Down
11 changes: 11 additions & 0 deletions scripts/templates/nullddi.cpp.mako
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,16 @@ ${tbl['export']['name']}(

${x}_result_t result = ${X}_RESULT_SUCCESS;

% if tbl['name'] == 'Global' and n == 'ze':
pDdiTable->pfnInit = driver::zeInit;

auto missing_api = getenv_string( "ZEL_TEST_MISSING_API" );
if (std::strcmp(missing_api.c_str(), "zeInitDrivers") == 0) {
pDdiTable->pfnInitDrivers = nullptr;
} else {
pDdiTable->pfnInitDrivers = driver::zeInitDrivers;
}
%else:
%for obj in tbl['functions']:
%if 'condition' in obj:
#if ${th.subt(n, tags, obj['condition'])}
Expand All @@ -142,6 +152,7 @@ ${tbl['export']['name']}(
%endif

%endfor
%endif
return result;
}

Expand Down
8 changes: 6 additions & 2 deletions source/drivers/null/ze_nullddi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5142,8 +5142,12 @@ zeGetGlobalProcAddrTable(

pDdiTable->pfnInit = driver::zeInit;

pDdiTable->pfnInitDrivers = driver::zeInitDrivers;

auto missing_api = getenv_string( "ZEL_TEST_MISSING_API" );
if (std::strcmp(missing_api.c_str(), "zeInitDrivers") == 0) {
pDdiTable->pfnInitDrivers = nullptr;
} else {
pDdiTable->pfnInitDrivers = driver::zeInitDrivers;
}
return result;
}

Expand Down
4 changes: 4 additions & 0 deletions source/lib/ze_libapi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,10 @@ zeInitDrivers(
return result;
});

if (result != ZE_RESULT_SUCCESS) {
return result;
}

if(ze_lib::context->inTeardown) {
return ZE_RESULT_ERROR_UNINITIALIZED;
}
Expand Down
8 changes: 8 additions & 0 deletions source/loader/ze_loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,14 @@ namespace loader
debug_trace_message(message, "");
}
}
// If zeInitDrivers is not supported by this driver, but zeInitDrivers is called first, then return uninitialized.
if (desc && !loader::context->initDriversSupport) {
std::string message = "zeInitDrivers called first, but not supported by driver, returning uninitialized.";
debug_trace_message(message, "");
return ZE_RESULT_ERROR_UNINITIALIZED;
}


bool return_first_driver_result=false;
std::string initName = "zeInit";
driver_vector_t *drivers = &zeDrivers;
Expand Down
4 changes: 3 additions & 1 deletion test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,6 @@ set_property(TEST tests_both_succeed PROPERTY ENVIRONMENT "ZE_ENABLE_NULL_DRIVER
add_test(NAME tests_both_gpu COMMAND tests --gtest_filter=*GivenLevelZeroLoaderPresentWhenCallingzeInitThenZeInitDriversThenBothCallsSucceedWithGPUTypes*)
set_property(TEST tests_both_gpu PROPERTY ENVIRONMENT "ZE_ENABLE_NULL_DRIVER=1")
add_test(NAME tests_both_npu COMMAND tests --gtest_filter=*GivenLevelZeroLoaderPresentWhenCallingzeInitThenZeInitDriversThenBothCallsSucceedWithNPUTypes*)
set_property(TEST tests_both_npu PROPERTY ENVIRONMENT "ZE_ENABLE_NULL_DRIVER=1")
set_property(TEST tests_both_npu PROPERTY ENVIRONMENT "ZE_ENABLE_NULL_DRIVER=1")
add_test(NAME tests_missing_api COMMAND tests --gtest_filter=*GivenLevelZeroLoaderPresentWhenCallingZeInitDriversThenzeInitWithzeInitDriversUnsupportedOnTheDriverThenzeInitSucceeds*)
set_property(TEST tests_missing_api PROPERTY ENVIRONMENT "ZE_ENABLE_NULL_DRIVER=1")
13 changes: 13 additions & 0 deletions test/loader_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,19 @@ TEST(
EXPECT_EQ(ZE_RESULT_SUCCESS, zeInit(ZE_INIT_FLAG_GPU_ONLY));
}

TEST(
LoaderInit,
GivenLevelZeroLoaderPresentWhenCallingZeInitDriversThenzeInitWithzeInitDriversUnsupportedOnTheDriverThenzeInitSucceeds) {

uint32_t pCount = 0;
ze_init_driver_type_desc_t desc = {ZE_STRUCTURE_TYPE_INIT_DRIVER_TYPE_DESC};
desc.flags = UINT32_MAX;
desc.pNext = nullptr;
putenv_safe( const_cast<char *>( "ZEL_TEST_MISSING_API=zeInitDrivers" ) );
EXPECT_EQ(ZE_RESULT_ERROR_UNINITIALIZED, zeInitDrivers(&pCount, nullptr, &desc));
EXPECT_EQ(ZE_RESULT_SUCCESS, zeInit(0));
}

TEST(
LoaderInit,
GivenLevelZeroLoaderPresentWhenCallingZeInitDriversThenzeInitThenBothCallsSucceedWithNPUTypes) {
Expand Down
Loading