Skip to content

Commit

Permalink
Fix zeInit Compatibility when zeInitDrivers is undefined
Browse files Browse the repository at this point in the history
- Handle when zeinitDrivers is called first and the driver does not
  support the api yet. Included a unit test to cover these types of
cases going forward.

Signed-off-by: Neil R. Spruit <neil.r.spruit@intel.com>
  • Loading branch information
nrspruit committed Nov 9, 2024
1 parent 3cd7e37 commit 93c081a
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 3 deletions.
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
9 changes: 9 additions & 0 deletions source/loader/ze_loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,15 @@ 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;
}
printf("check_drivers\n");


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

0 comments on commit 93c081a

Please sign in to comment.