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

[DO_NOT_MERGE] Static L0 Loader Support #224

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
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
21 changes: 16 additions & 5 deletions source/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,22 @@ configure_file(
@ONLY)

include(GNUInstallDirs)
add_library(${TARGET_LOADER_NAME}
SHARED
""
${CMAKE_CURRENT_BINARY_DIR}/ZeLoaderVersion.rc
)
if (DYNAMIC_LOAD_LOADER)
Copy link
Contributor

Choose a reason for hiding this comment

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

Recommend the top level cmake option be named after the result rather than the behavior, like BUILD_STATIC

Copy link
Contributor Author

Choose a reason for hiding this comment

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

good point, that would differentiate.

message(STATUS "Building loader as static library")
add_library(${TARGET_LOADER_NAME}
STATIC
""
${CMAKE_CURRENT_BINARY_DIR}/ZeLoaderVersion.rc
)
add_definitions(-DDYNAMIC_LOAD_LOADER="1")
else()
message(STATUS "Building loader as dynamic library")
add_library(${TARGET_LOADER_NAME}
SHARED
""
${CMAKE_CURRENT_BINARY_DIR}/ZeLoaderVersion.rc
)
endif()

add_subdirectory(lib)
add_subdirectory(loader)
Expand Down
14 changes: 8 additions & 6 deletions source/lib/linux/lib_init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@

namespace ze_lib
{

#ifndef DYNAMIC_LOAD_LOADER
void __attribute__((constructor)) createLibContext() {
printf("Context created context lib dynamic\n");
context = new context_t;
}

void __attribute__((destructor)) deleteLibContext() {
delete context;
}
}
void __attribute__((destructor)) deleteLibContext() {
printf("Context destroyed context lib dynamic\n");
delete context;
}
#endif

}
74 changes: 63 additions & 11 deletions source/lib/ze_lib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,26 @@
*
*/
#include "ze_lib.h"
#ifndef DYNAMIC_LOAD_LOADER
#include "../loader/ze_loader_api.h"
#endif

namespace ze_lib
{
///////////////////////////////////////////////////////////////////////////////
#ifndef DYNAMIC_LOAD_LOADER
context_t *context;
#else
context_t *context = new context_t;
#endif
bool destruction = false;

///////////////////////////////////////////////////////////////////////////////
context_t::context_t()
{
#ifdef DYNAMIC_LOAD_LOADER
printf("Context created static\n");
#else
printf("Context created dynamic\n");
#endif
};

///////////////////////////////////////////////////////////////////////////////
Expand All @@ -46,19 +53,48 @@ namespace ze_lib
std::string loaderFullLibraryPath = create_library_path(MAKE_LIBRARY_NAME( "ze_loader", L0_LOADER_VERSION), loaderLibraryPath.c_str());
loader = LOAD_DRIVER_LIBRARY(loaderFullLibraryPath.c_str());

if( NULL == loader )
if( NULL == loader ) {
printf("loader load failed\n");
return ZE_RESULT_ERROR_UNINITIALIZED;
}

typedef ze_result_t (ZE_APICALL *loaderInit_t)();
auto loaderInit = reinterpret_cast<loaderInit_t>(
GET_FUNCTION_PTR(loader, "zeLoaderInit") );
printf("calling loader init static\n");
result = loaderInit();
if( ZE_RESULT_SUCCESS == result ) {
typedef HMODULE (ZE_APICALL *getTracing_t)();
auto getTracing = reinterpret_cast<getTracing_t>(
GET_FUNCTION_PTR(loader, "zeLoaderGetTracingHandle") );
tracing_lib = getTracing();
if( ZE_RESULT_SUCCESS != result ) {
return result;
}
typedef HMODULE (ZE_APICALL *getTracing_t)();
auto getTracing = reinterpret_cast<getTracing_t>(
GET_FUNCTION_PTR(loader, "zeLoaderGetTracingHandle") );
if (getTracing == nullptr) {
printf("missing getTracing\n");
return ZE_RESULT_ERROR_UNINITIALIZED;
}
tracing_lib = getTracing();
typedef ze_result_t (ZE_APICALL *zelLoaderDriverCheck_t)(ze_init_flags_t flags, ze_init_driver_type_desc_t* desc, ze_global_dditable_t *globalInitStored, zes_global_dditable_t *sysmanGlobalInitStored, bool *requireDdiReinit, bool sysmanOnly);
auto loaderDriverCheck = reinterpret_cast<zelLoaderDriverCheck_t>(
GET_FUNCTION_PTR(loader, "zelLoaderDriverCheck") );
if (loaderDriverCheck == nullptr) {
printf("missing loaderDriverCheck\n");
return ZE_RESULT_ERROR_UNINITIALIZED;
}
typedef ze_result_t (ZE_APICALL *zelLoaderTracingLayerInit_t)(std::atomic<ze_dditable_t *> &zeDdiTable);
auto loaderTracingLayerInit = reinterpret_cast<zelLoaderTracingLayerInit_t>(
GET_FUNCTION_PTR(loader, "zelLoaderTracingLayerInit") );
if (loaderTracingLayerInit == nullptr) {
printf("missing loaderTracingLayerInit\n");
Copy link
Contributor

Choose a reason for hiding this comment

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

I think these should be going to stderr

Copy link
Contributor Author

Choose a reason for hiding this comment

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

agreed, I was going to convert the printfs to debug trace logs for the ones we determine useful.

return ZE_RESULT_ERROR_UNINITIALIZED;
}
typedef loader::context_t * (ZE_APICALL *zelLoaderGetContext_t)();
auto loaderGetContext = reinterpret_cast<zelLoaderGetContext_t>(
GET_FUNCTION_PTR(loader, "zelLoaderGetContext") );
if (loaderGetContext == nullptr) {
printf("missing zelLoaderGetContext\n");
}
printf("done loading functions\n");
#else
result = zeLoaderInit();
if( ZE_RESULT_SUCCESS == result ) {
Expand All @@ -75,7 +111,15 @@ namespace ze_lib
}

// Given zesInit, then zesDrivers needs to be used as the sysmanInstanceDrivers;
if (sysmanOnly) {
bool loaderContextAccessAllowed = true;
#ifdef DYNAMIC_LOAD_LOADER
if (loaderGetContext == nullptr) {
loaderContextAccessAllowed = false;
} else {
loader::context = loaderGetContext();
}
#endif
if (sysmanOnly && loaderContextAccessAllowed) {
loader::context->sysmanInstanceDrivers = &loader::context->zesDrivers;
}

Expand Down Expand Up @@ -104,7 +148,11 @@ namespace ze_lib
// Init the stored ddi tables for the tracing layer
if( ZE_RESULT_SUCCESS == result )
{
#ifdef DYNAMIC_LOAD_LOADER
result = loaderTracingLayerInit(this->pTracingZeDdiTable);
#else
result = zelLoaderTracingLayerInit(this->pTracingZeDdiTable);
#endif
}
// End DDI Table Inits

Expand All @@ -114,9 +162,13 @@ namespace ze_lib
// Check which drivers support the ze_driver_flag_t specified
// No need to check if only initializing sysman
bool requireDdiReinit = false;
#ifdef DYNAMIC_LOAD_LOADER
result = loaderDriverCheck(flags, desc, &ze_lib::context->initialzeDdiTable.Global, &ze_lib::context->initialzesDdiTable.Global, &requireDdiReinit, sysmanOnly);
#else
result = zelLoaderDriverCheck(flags, desc, &ze_lib::context->initialzeDdiTable.Global, &ze_lib::context->initialzesDdiTable.Global, &requireDdiReinit, sysmanOnly);
#endif
// If a driver was removed from the driver list, then the ddi tables need to be reinit to allow for passthru directly to the driver.
if (requireDdiReinit) {
if (requireDdiReinit && loaderContextAccessAllowed) {
// If a user has already called the core apis, then ddi table reinit is not possible due to handles already being read by the user.
if (!sysmanOnly && !ze_lib::context->zeInuse) {
// reInit the ZE DDI Tables
Expand Down Expand Up @@ -165,7 +217,7 @@ zelLoaderGetVersions(
{
#ifdef DYNAMIC_LOAD_LOADER
if(nullptr == ze_lib::context->loader)
return ZE_RESULT_ERROR;
return ZE_RESULT_ERROR_UNINITIALIZED;
typedef ze_result_t (ZE_APICALL *zelLoaderGetVersions_t)(size_t *num_elems, zel_component_version_t *versions);
auto getVersions = reinterpret_cast<zelLoaderGetVersions_t>(
GET_FUNCTION_PTR(ze_lib::context->loader, "zelLoaderGetVersionsInternal") );
Expand Down
2 changes: 1 addition & 1 deletion source/lib/ze_libddi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ namespace ze_lib
__zedlllocal ze_result_t context_t::zeDdiTableInit()
{
ze_result_t result = ZE_RESULT_SUCCESS;

printf("calling static loader ddi init\n");
if( ZE_RESULT_SUCCESS == result )
{
auto getTable = reinterpret_cast<ze_pfnGetGlobalProcAddrTable_t>(
Expand Down
6 changes: 4 additions & 2 deletions source/loader/linux/loader_init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@

namespace loader
{

#ifndef DYNAMIC_LOAD_LOADER
void __attribute__((constructor)) createLoaderContext() {
printf("loader created context lib dynamic\n");
context = new context_t;
}

void __attribute__((destructor)) deleteLoaderContext() {
printf("loader destroyed context lib dynamic\n");
delete context;
}

#endif
}
11 changes: 11 additions & 0 deletions source/loader/ze_loader_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,17 @@ zeLoaderGetTracingHandle()
return loader::context->tracingLayer;
}

///////////////////////////////////////////////////////////////////////////////
/// @brief Get pointer to Loader Context
///
/// @returns
/// - ::Pointer to the Loader's Context
ZE_DLLEXPORT loader::context_t *ZE_APICALL
zelLoaderGetContext() {
printf("zelLoaderGetContext\n");
return loader::context;
}

///////////////////////////////////////////////////////////////////////////////
/// @brief Internal function for Setting the ddi table for the Tracing Layer.
///
Expand Down
8 changes: 8 additions & 0 deletions source/loader/ze_loader_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,14 @@ zelLoaderTracingLayerInit(std::atomic<ze_dditable_t *> &zeDdiTable);
ZE_DLLEXPORT HMODULE ZE_APICALL
zeLoaderGetTracingHandle();

///////////////////////////////////////////////////////////////////////////////
/// @brief Get pointer to Loader Context
///
/// @returns
/// - ::handle to tracing library
ZE_DLLEXPORT loader::context_t *ZE_APICALL
zelLoaderGetContext();


///////////////////////////////////////////////////////////////////////////////
/// @brief Exported function for getting version
Expand Down
1 change: 1 addition & 0 deletions source/loader/ze_loader_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ namespace loader
bool tracingLayerEnabled = false;
dditable_t tracing_dditable = {};
std::shared_ptr<Logger> zel_logger;
uint32_t init_counter = 0;
};

extern context_t *context;
Expand Down
Loading