From fc141913c82dff29719cf344e27f4fe1df4fe2fb Mon Sep 17 00:00:00 2001 From: "Neil R. Spruit" Date: Thu, 24 Oct 2024 16:40:22 -0700 Subject: [PATCH 1/2] Static Loader Support Signed-off-by: Neil R. Spruit --- source/CMakeLists.txt | 21 ++++++++++--- source/lib/linux/lib_init.cpp | 14 +++++---- source/lib/ze_lib.cpp | 49 +++++++++++++++++++++++------ source/lib/ze_libddi.cpp | 2 +- source/loader/linux/loader_init.cpp | 6 ++-- source/loader/ze_loader_api.cpp | 11 +++++++ source/loader/ze_loader_api.h | 8 +++++ 7 files changed, 88 insertions(+), 23 deletions(-) diff --git a/source/CMakeLists.txt b/source/CMakeLists.txt index c9d05dda..458546d6 100644 --- a/source/CMakeLists.txt +++ b/source/CMakeLists.txt @@ -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) + 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) diff --git a/source/lib/linux/lib_init.cpp b/source/lib/linux/lib_init.cpp index 7fbf65ca..66500f26 100644 --- a/source/lib/linux/lib_init.cpp +++ b/source/lib/linux/lib_init.cpp @@ -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 } \ No newline at end of file diff --git a/source/lib/ze_lib.cpp b/source/lib/ze_lib.cpp index fc0e1880..ff564dd2 100644 --- a/source/lib/ze_lib.cpp +++ b/source/lib/ze_lib.cpp @@ -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 }; /////////////////////////////////////////////////////////////////////////////// @@ -46,19 +53,32 @@ 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( 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( - 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( + GET_FUNCTION_PTR(loader, "zeLoaderGetTracingHandle") ); + 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( + GET_FUNCTION_PTR(loader, "zelLoaderDriverCheck") ); + typedef ze_result_t (ZE_APICALL *zelLoaderTracingLayerInit_t)(std::atomic &zeDdiTable); + auto loaderTracingLayerInit = reinterpret_cast( + GET_FUNCTION_PTR(loader, "zelLoaderTracingLayerInit") ); + typedef loader::context_t * (ZE_APICALL *zelLoaderGetContext_t)(); + auto loaderGetContext = reinterpret_cast( + GET_FUNCTION_PTR(loader, "zelLoaderGetContext") ); #else result = zeLoaderInit(); if( ZE_RESULT_SUCCESS == result ) { @@ -75,6 +95,9 @@ namespace ze_lib } // Given zesInit, then zesDrivers needs to be used as the sysmanInstanceDrivers; +#ifdef DYNAMIC_LOAD_LOADER + loader::context = loaderGetContext(); +#endif if (sysmanOnly) { loader::context->sysmanInstanceDrivers = &loader::context->zesDrivers; } @@ -104,7 +127,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 @@ -114,7 +141,11 @@ 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 a user has already called the core apis, then ddi table reinit is not possible due to handles already being read by the user. @@ -165,7 +196,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( GET_FUNCTION_PTR(ze_lib::context->loader, "zelLoaderGetVersionsInternal") ); diff --git a/source/lib/ze_libddi.cpp b/source/lib/ze_libddi.cpp index 5a6c8a81..0b3ac375 100644 --- a/source/lib/ze_libddi.cpp +++ b/source/lib/ze_libddi.cpp @@ -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( diff --git a/source/loader/linux/loader_init.cpp b/source/loader/linux/loader_init.cpp index 12b2c15e..e3d322ac 100644 --- a/source/loader/linux/loader_init.cpp +++ b/source/loader/linux/loader_init.cpp @@ -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 } \ No newline at end of file diff --git a/source/loader/ze_loader_api.cpp b/source/loader/ze_loader_api.cpp index a94daa9c..4ab76f77 100644 --- a/source/loader/ze_loader_api.cpp +++ b/source/loader/ze_loader_api.cpp @@ -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. /// diff --git a/source/loader/ze_loader_api.h b/source/loader/ze_loader_api.h index 4459602f..1b719ed9 100644 --- a/source/loader/ze_loader_api.h +++ b/source/loader/ze_loader_api.h @@ -51,6 +51,14 @@ zelLoaderTracingLayerInit(std::atomic &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 From dcca4f42756a63edb480af9ed3b95ea601b1b1d0 Mon Sep 17 00:00:00 2001 From: "Neil R. Spruit" Date: Thu, 24 Oct 2024 17:25:06 -0700 Subject: [PATCH 2/2] handle older loader Signed-off-by: Neil R. Spruit --- source/lib/ze_lib.cpp | 27 ++++++++++++++++++++++++--- source/loader/ze_loader_internal.h | 1 + 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/source/lib/ze_lib.cpp b/source/lib/ze_lib.cpp index ff564dd2..c7707830 100644 --- a/source/lib/ze_lib.cpp +++ b/source/lib/ze_lib.cpp @@ -69,16 +69,32 @@ namespace ze_lib typedef HMODULE (ZE_APICALL *getTracing_t)(); auto getTracing = reinterpret_cast( 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( 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 &zeDdiTable); auto loaderTracingLayerInit = reinterpret_cast( GET_FUNCTION_PTR(loader, "zelLoaderTracingLayerInit") ); + if (loaderTracingLayerInit == nullptr) { + printf("missing loaderTracingLayerInit\n"); + return ZE_RESULT_ERROR_UNINITIALIZED; + } typedef loader::context_t * (ZE_APICALL *zelLoaderGetContext_t)(); auto loaderGetContext = reinterpret_cast( 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 ) { @@ -95,10 +111,15 @@ namespace ze_lib } // Given zesInit, then zesDrivers needs to be used as the sysmanInstanceDrivers; + bool loaderContextAccessAllowed = true; #ifdef DYNAMIC_LOAD_LOADER - loader::context = loaderGetContext(); + if (loaderGetContext == nullptr) { + loaderContextAccessAllowed = false; + } else { + loader::context = loaderGetContext(); + } #endif - if (sysmanOnly) { + if (sysmanOnly && loaderContextAccessAllowed) { loader::context->sysmanInstanceDrivers = &loader::context->zesDrivers; } @@ -147,7 +168,7 @@ namespace ze_lib 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 diff --git a/source/loader/ze_loader_internal.h b/source/loader/ze_loader_internal.h index 087adef9..6de49357 100644 --- a/source/loader/ze_loader_internal.h +++ b/source/loader/ze_loader_internal.h @@ -131,6 +131,7 @@ namespace loader bool tracingLayerEnabled = false; dditable_t tracing_dditable = {}; std::shared_ptr zel_logger; + uint32_t init_counter = 0; }; extern context_t *context;