From 01ca0b3d4983cfa2d65cf7803f5a7993ec068ee7 Mon Sep 17 00:00:00 2001 From: "Neil R. Spruit" Date: Thu, 24 Oct 2024 16:40:22 -0700 Subject: [PATCH 1/7] 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 42aa0dde..b73d815c 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 ca1590318318846a09a2c65f521cc2c814ad94e5 Mon Sep 17 00:00:00 2001 From: "Neil R. Spruit" Date: Thu, 24 Oct 2024 17:25:06 -0700 Subject: [PATCH 2/7] 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; From e3aaf6815226b746e8195abbafd7b53a683a20a7 Mon Sep 17 00:00:00 2001 From: "Neil R. Spruit" Date: Fri, 15 Nov 2024 11:29:02 -0800 Subject: [PATCH 3/7] Add support for older ddi tables - Added support for older dii tables such that the version of the apis supported by that dynamic loader are requested and not newer versions. - Added check for backwards compatability for older init apis. Signed-off-by: Neil R. Spruit --- source/CMakeLists.txt | 2 +- source/lib/linux/lib_init.cpp | 2 - source/lib/ze_lib.cpp | 128 ++++++++++++++++++++++------ source/lib/ze_lib.h | 17 +++- source/lib/ze_libddi.cpp | 61 +++++++------ source/lib/zel_tracing_libddi.cpp | 6 +- source/lib/zes_libddi.cpp | 52 +++++------ source/lib/zet_libddi.cpp | 41 +++++---- source/loader/linux/loader_init.cpp | 2 - source/loader/ze_loader_api.cpp | 1 - 10 files changed, 193 insertions(+), 119 deletions(-) diff --git a/source/CMakeLists.txt b/source/CMakeLists.txt index 458546d6..92cc7734 100644 --- a/source/CMakeLists.txt +++ b/source/CMakeLists.txt @@ -12,7 +12,7 @@ configure_file( @ONLY) include(GNUInstallDirs) -if (DYNAMIC_LOAD_LOADER) +if (BUILD_STATIC) message(STATUS "Building loader as static library") add_library(${TARGET_LOADER_NAME} STATIC diff --git a/source/lib/linux/lib_init.cpp b/source/lib/linux/lib_init.cpp index 66500f26..399736c7 100644 --- a/source/lib/linux/lib_init.cpp +++ b/source/lib/linux/lib_init.cpp @@ -12,11 +12,9 @@ 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() { - printf("Context destroyed context lib dynamic\n"); delete context; } #endif diff --git a/source/lib/ze_lib.cpp b/source/lib/ze_lib.cpp index c7707830..126d0bbc 100644 --- a/source/lib/ze_lib.cpp +++ b/source/lib/ze_lib.cpp @@ -22,13 +22,7 @@ namespace ze_lib /////////////////////////////////////////////////////////////////////////////// context_t::context_t() - { - #ifdef DYNAMIC_LOAD_LOADER - printf("Context created static\n"); - #else - printf("Context created dynamic\n"); - #endif - }; + {}; /////////////////////////////////////////////////////////////////////////////// context_t::~context_t() @@ -41,10 +35,17 @@ namespace ze_lib ze_lib::destruction = true; }; + ////////////////////////////////////////////////////////////////////////// + void debug_trace_message(const std::string &message, const std::string &extra) + { + std::cerr << message << " " << extra << std::endl; + } + ////////////////////////////////////////////////////////////////////////// __zedlllocal ze_result_t context_t::Init(ze_init_flags_t flags, bool sysmanOnly, ze_init_driver_type_desc_t* desc) { ze_result_t result; + ze_api_version_t version = ZE_API_VERSION_CURRENT; #ifdef DYNAMIC_LOAD_LOADER std::string loaderLibraryPath; #ifdef _WIN32 @@ -54,47 +55,78 @@ namespace ze_lib loader = LOAD_DRIVER_LIBRARY(loaderFullLibraryPath.c_str()); if( NULL == loader ) { - printf("loader load failed\n"); + std::string message = "ze_lib Context Init() Loader Library Load Failed"; + debug_trace_message(message, ""); 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 ) { + std::string message = "ze_lib Context Init() Loader Init Failed"; + debug_trace_message(message, ""); return result; } typedef HMODULE (ZE_APICALL *getTracing_t)(); auto getTracing = reinterpret_cast( GET_FUNCTION_PTR(loader, "zeLoaderGetTracingHandle") ); if (getTracing == nullptr) { - printf("missing getTracing\n"); + std::string message = "ze_lib Context Init() zeLoaderGetTracingHandle missing"; + debug_trace_message(message, ""); 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"); + std::string message = "ze_lib Context Init() zelLoaderTracingLayerInit missing"; + debug_trace_message(message, ""); 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"); + std::string message = "ze_lib Context Init() zelLoaderGetContext missing"; + debug_trace_message(message, ""); + } + + size_t size = 0; + result = zelLoaderGetVersions(&size, nullptr); + if (ZE_RESULT_SUCCESS != result) { + std::string message = "ze_lib Context Init() zelLoaderGetVersions Failed"; + debug_trace_message(message, ""); + return result; + } + + std::vector versions(size); + result = zelLoaderGetVersions(&size, versions.data()); + if (ZE_RESULT_SUCCESS != result) { + std::string message = "ze_lib Context Init() zelLoaderGetVersions Failed to read component versions"; + debug_trace_message(message, ""); + return result; + } + bool zeInitDriversSupport = true; + const std::string loader_name = "loader"; + for (auto &component : versions) { + if (loader_name == component.component_name) { + version = component.spec_version; + if(component.component_lib_version.major == 1) { + if (component.component_lib_version.minor < 18) { + std::string message = "ze_lib Context Init() Version Does not support zeInitDrivers"; + debug_trace_message(message, ""); + zeInitDriversSupport = false; + } + } else { + std::string message = "ze_lib Context Init() Loader version is too new"; + debug_trace_message(message, ""); + return ZE_RESULT_ERROR_UNSUPPORTED_VERSION; + } + } } - printf("done loading functions\n"); #else result = zeLoaderInit(); if( ZE_RESULT_SUCCESS == result ) { @@ -128,22 +160,38 @@ namespace ze_lib // Init the ZE DDI Tables if( ZE_RESULT_SUCCESS == result ) { - result = zeDdiTableInit(); + result = zeDdiTableInit(version); + if (result != ZE_RESULT_SUCCESS) { + std::string message = "ze_lib Context Init() zeDdiTableInit failed"; + debug_trace_message(message, ""); + } } // Init the ZET DDI Tables if( ZE_RESULT_SUCCESS == result ) { - result = zetDdiTableInit(); + result = zetDdiTableInit(version); + if( ZE_RESULT_SUCCESS != result ) { + std::string message = "ze_lib Context Init() zetDdiTableInit failed"; + debug_trace_message(message, ""); + } } // Init the ZES DDI Tables if( ZE_RESULT_SUCCESS == result ) { - result = zesDdiTableInit(); + result = zesDdiTableInit(version); + if (result != ZE_RESULT_SUCCESS) { + std::string message = "ze_lib Context Init() zesDdiTableInit failed"; + debug_trace_message(message, ""); + } } // Init the Tracing API DDI Tables if( ZE_RESULT_SUCCESS == result ) { - result = zelTracingDdiTableInit(); + result = zelTracingDdiTableInit(version); + if (result != ZE_RESULT_SUCCESS) { + std::string message = "ze_lib Context Init() zelTracingDdiTableInit failed"; + debug_trace_message(message, ""); + } } // Init the stored ddi tables for the tracing layer if( ZE_RESULT_SUCCESS == result ) @@ -163,10 +211,34 @@ namespace ze_lib // 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); + if (zeInitDriversSupport) { + 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) { + std::string message = "ze_lib Context Init() zelLoaderDriverCheck missing"; + debug_trace_message(message, ""); + return ZE_RESULT_ERROR_UNINITIALIZED; + } + result = loaderDriverCheck(flags, desc, &ze_lib::context->initialzeDdiTable.Global, &ze_lib::context->initialzesDdiTable.Global, &requireDdiReinit, sysmanOnly); + } else { + typedef ze_result_t (ZE_APICALL *zelLoaderDriverCheck_t)(ze_init_flags_t flags, 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) { + std::string message = "ze_lib Context Init() zelLoaderDriverCheck missing"; + debug_trace_message(message, ""); + return ZE_RESULT_ERROR_UNINITIALIZED; + } + result = loaderDriverCheck(flags, &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 (result != ZE_RESULT_SUCCESS) { + std::string message = "ze_lib Context Init() zelLoaderDriverCheck failed"; + debug_trace_message(message, ""); + } // 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 && 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. @@ -174,12 +246,12 @@ namespace ze_lib // reInit the ZE DDI Tables if( ZE_RESULT_SUCCESS == result ) { - result = zeDdiTableInit(); + result = zeDdiTableInit(version); } // reInit the ZET DDI Tables if( ZE_RESULT_SUCCESS == result ) { - result = zetDdiTableInit(); + result = zetDdiTableInit(version); } // If ze/zet ddi tables have been reinit and no longer use the intercept layer, then handles passed to zelLoaderTranslateHandleInternal do not require translation. // Setting intercept_enabled==false changes the behavior of zelLoaderTranslateHandleInternal to avoid translation. @@ -191,7 +263,7 @@ namespace ze_lib // reInit the ZES DDI Tables if( ZE_RESULT_SUCCESS == result ) { - result = zesDdiTableInit(); + result = zesDdiTableInit(version); } } } diff --git a/source/lib/ze_lib.h b/source/lib/ze_lib.h index 73589936..93af3e5d 100644 --- a/source/lib/ze_lib.h +++ b/source/lib/ze_lib.h @@ -34,22 +34,31 @@ namespace ze_lib context_t(); ~context_t(); + /////////////////////////////////////////////////////////////////////////////// + template + ze_result_t getTableWithCheck(T getTable, ze_api_version_t version, TableType* table) { + if (getTable == nullptr) { + return ZE_RESULT_ERROR_UNINITIALIZED; + } + return getTable(version, table); + } + std::once_flag initOnce; std::once_flag initOnceDrivers; std::once_flag initOnceSysMan; ze_result_t Init(ze_init_flags_t flags, bool sysmanOnly, ze_init_driver_type_desc_t* desc); - ze_result_t zeDdiTableInit(); + ze_result_t zeDdiTableInit(ze_api_version_t version); std::atomic zeDdiTable = {nullptr}; - ze_result_t zetDdiTableInit(); + ze_result_t zetDdiTableInit(ze_api_version_t version); std::atomic zetDdiTable = {nullptr}; - ze_result_t zesDdiTableInit(); + ze_result_t zesDdiTableInit(ze_api_version_t version); std::atomic zesDdiTable = {nullptr}; - ze_result_t zelTracingDdiTableInit(); + ze_result_t zelTracingDdiTableInit(ze_api_version_t version); zel_tracing_dditable_t zelTracingDdiTable = {}; std::atomic pTracingZeDdiTable = {nullptr}; ze_dditable_t initialzeDdiTable; diff --git a/source/lib/ze_libddi.cpp b/source/lib/ze_libddi.cpp index b73d815c..bf8db69d 100644 --- a/source/lib/ze_libddi.cpp +++ b/source/lib/ze_libddi.cpp @@ -17,15 +17,14 @@ namespace ze_lib /////////////////////////////////////////////////////////////////////////////// #ifdef DYNAMIC_LOAD_LOADER - __zedlllocal ze_result_t context_t::zeDdiTableInit() + __zedlllocal ze_result_t context_t::zeDdiTableInit(ze_api_version_t version) { ze_result_t result = ZE_RESULT_SUCCESS; - printf("calling static loader ddi init\n"); if( ZE_RESULT_SUCCESS == result ) { auto getTable = reinterpret_cast( GET_FUNCTION_PTR(loader, "zeGetGlobalProcAddrTable") ); - result = getTable( ZE_API_VERSION_CURRENT, &initialzeDdiTable.Global ); + result = getTableWithCheck(getTable, version, &initialzeDdiTable.Global ); } if( ZE_RESULT_SUCCESS == result ) @@ -33,7 +32,7 @@ namespace ze_lib // Optional auto getTable = reinterpret_cast( GET_FUNCTION_PTR(loader, "zeGetRTASBuilderExpProcAddrTable") ); - getTable( ZE_API_VERSION_CURRENT, &initialzeDdiTable.RTASBuilderExp ); + getTableWithCheck(getTable, version, &initialzeDdiTable.RTASBuilderExp ); } if( ZE_RESULT_SUCCESS == result ) @@ -41,14 +40,14 @@ namespace ze_lib // Optional auto getTable = reinterpret_cast( GET_FUNCTION_PTR(loader, "zeGetRTASParallelOperationExpProcAddrTable") ); - getTable( ZE_API_VERSION_CURRENT, &initialzeDdiTable.RTASParallelOperationExp ); + getTableWithCheck(getTable, version, &initialzeDdiTable.RTASParallelOperationExp ); } if( ZE_RESULT_SUCCESS == result ) { auto getTable = reinterpret_cast( GET_FUNCTION_PTR(loader, "zeGetDriverProcAddrTable") ); - result = getTable( ZE_API_VERSION_CURRENT, &initialzeDdiTable.Driver ); + result = getTableWithCheck(getTable, version, &initialzeDdiTable.Driver ); } if( ZE_RESULT_SUCCESS == result ) @@ -56,14 +55,14 @@ namespace ze_lib // Optional auto getTable = reinterpret_cast( GET_FUNCTION_PTR(loader, "zeGetDriverExpProcAddrTable") ); - getTable( ZE_API_VERSION_CURRENT, &initialzeDdiTable.DriverExp ); + getTableWithCheck(getTable, version, &initialzeDdiTable.DriverExp ); } if( ZE_RESULT_SUCCESS == result ) { auto getTable = reinterpret_cast( GET_FUNCTION_PTR(loader, "zeGetDeviceProcAddrTable") ); - result = getTable( ZE_API_VERSION_CURRENT, &initialzeDdiTable.Device ); + result = getTableWithCheck(getTable, version, &initialzeDdiTable.Device ); } if( ZE_RESULT_SUCCESS == result ) @@ -71,28 +70,28 @@ namespace ze_lib // Optional auto getTable = reinterpret_cast( GET_FUNCTION_PTR(loader, "zeGetDeviceExpProcAddrTable") ); - getTable( ZE_API_VERSION_CURRENT, &initialzeDdiTable.DeviceExp ); + getTableWithCheck(getTable, version, &initialzeDdiTable.DeviceExp ); } if( ZE_RESULT_SUCCESS == result ) { auto getTable = reinterpret_cast( GET_FUNCTION_PTR(loader, "zeGetContextProcAddrTable") ); - result = getTable( ZE_API_VERSION_CURRENT, &initialzeDdiTable.Context ); + result = getTableWithCheck(getTable, version, &initialzeDdiTable.Context ); } if( ZE_RESULT_SUCCESS == result ) { auto getTable = reinterpret_cast( GET_FUNCTION_PTR(loader, "zeGetCommandQueueProcAddrTable") ); - result = getTable( ZE_API_VERSION_CURRENT, &initialzeDdiTable.CommandQueue ); + result = getTableWithCheck(getTable, version, &initialzeDdiTable.CommandQueue ); } if( ZE_RESULT_SUCCESS == result ) { auto getTable = reinterpret_cast( GET_FUNCTION_PTR(loader, "zeGetCommandListProcAddrTable") ); - result = getTable( ZE_API_VERSION_CURRENT, &initialzeDdiTable.CommandList ); + result = getTableWithCheck(getTable, version, &initialzeDdiTable.CommandList ); } if( ZE_RESULT_SUCCESS == result ) @@ -100,14 +99,14 @@ namespace ze_lib // Optional auto getTable = reinterpret_cast( GET_FUNCTION_PTR(loader, "zeGetCommandListExpProcAddrTable") ); - getTable( ZE_API_VERSION_CURRENT, &initialzeDdiTable.CommandListExp ); + getTableWithCheck(getTable, version, &initialzeDdiTable.CommandListExp ); } if( ZE_RESULT_SUCCESS == result ) { auto getTable = reinterpret_cast( GET_FUNCTION_PTR(loader, "zeGetEventProcAddrTable") ); - result = getTable( ZE_API_VERSION_CURRENT, &initialzeDdiTable.Event ); + result = getTableWithCheck(getTable, version, &initialzeDdiTable.Event ); } if( ZE_RESULT_SUCCESS == result ) @@ -115,28 +114,28 @@ namespace ze_lib // Optional auto getTable = reinterpret_cast( GET_FUNCTION_PTR(loader, "zeGetEventExpProcAddrTable") ); - getTable( ZE_API_VERSION_CURRENT, &initialzeDdiTable.EventExp ); + getTableWithCheck(getTable, version, &initialzeDdiTable.EventExp ); } if( ZE_RESULT_SUCCESS == result ) { auto getTable = reinterpret_cast( GET_FUNCTION_PTR(loader, "zeGetEventPoolProcAddrTable") ); - result = getTable( ZE_API_VERSION_CURRENT, &initialzeDdiTable.EventPool ); + result = getTableWithCheck(getTable, version, &initialzeDdiTable.EventPool ); } if( ZE_RESULT_SUCCESS == result ) { auto getTable = reinterpret_cast( GET_FUNCTION_PTR(loader, "zeGetFenceProcAddrTable") ); - result = getTable( ZE_API_VERSION_CURRENT, &initialzeDdiTable.Fence ); + result = getTableWithCheck(getTable, version, &initialzeDdiTable.Fence ); } if( ZE_RESULT_SUCCESS == result ) { auto getTable = reinterpret_cast( GET_FUNCTION_PTR(loader, "zeGetImageProcAddrTable") ); - result = getTable( ZE_API_VERSION_CURRENT, &initialzeDdiTable.Image ); + result = getTableWithCheck(getTable, version, &initialzeDdiTable.Image ); } if( ZE_RESULT_SUCCESS == result ) @@ -144,14 +143,14 @@ namespace ze_lib // Optional auto getTable = reinterpret_cast( GET_FUNCTION_PTR(loader, "zeGetImageExpProcAddrTable") ); - getTable( ZE_API_VERSION_CURRENT, &initialzeDdiTable.ImageExp ); + getTableWithCheck(getTable, version, &initialzeDdiTable.ImageExp ); } if( ZE_RESULT_SUCCESS == result ) { auto getTable = reinterpret_cast( GET_FUNCTION_PTR(loader, "zeGetKernelProcAddrTable") ); - result = getTable( ZE_API_VERSION_CURRENT, &initialzeDdiTable.Kernel ); + result = getTableWithCheck(getTable, version, &initialzeDdiTable.Kernel ); } if( ZE_RESULT_SUCCESS == result ) @@ -159,14 +158,14 @@ namespace ze_lib // Optional auto getTable = reinterpret_cast( GET_FUNCTION_PTR(loader, "zeGetKernelExpProcAddrTable") ); - getTable( ZE_API_VERSION_CURRENT, &initialzeDdiTable.KernelExp ); + getTableWithCheck(getTable, version, &initialzeDdiTable.KernelExp ); } if( ZE_RESULT_SUCCESS == result ) { auto getTable = reinterpret_cast( GET_FUNCTION_PTR(loader, "zeGetMemProcAddrTable") ); - result = getTable( ZE_API_VERSION_CURRENT, &initialzeDdiTable.Mem ); + result = getTableWithCheck(getTable, version, &initialzeDdiTable.Mem ); } if( ZE_RESULT_SUCCESS == result ) @@ -174,42 +173,42 @@ namespace ze_lib // Optional auto getTable = reinterpret_cast( GET_FUNCTION_PTR(loader, "zeGetMemExpProcAddrTable") ); - getTable( ZE_API_VERSION_CURRENT, &initialzeDdiTable.MemExp ); + getTableWithCheck(getTable, version, &initialzeDdiTable.MemExp ); } if( ZE_RESULT_SUCCESS == result ) { auto getTable = reinterpret_cast( GET_FUNCTION_PTR(loader, "zeGetModuleProcAddrTable") ); - result = getTable( ZE_API_VERSION_CURRENT, &initialzeDdiTable.Module ); + result = getTableWithCheck(getTable, version, &initialzeDdiTable.Module ); } if( ZE_RESULT_SUCCESS == result ) { auto getTable = reinterpret_cast( GET_FUNCTION_PTR(loader, "zeGetModuleBuildLogProcAddrTable") ); - result = getTable( ZE_API_VERSION_CURRENT, &initialzeDdiTable.ModuleBuildLog ); + result = getTableWithCheck(getTable, version, &initialzeDdiTable.ModuleBuildLog ); } if( ZE_RESULT_SUCCESS == result ) { auto getTable = reinterpret_cast( GET_FUNCTION_PTR(loader, "zeGetPhysicalMemProcAddrTable") ); - result = getTable( ZE_API_VERSION_CURRENT, &initialzeDdiTable.PhysicalMem ); + result = getTableWithCheck(getTable, version, &initialzeDdiTable.PhysicalMem ); } if( ZE_RESULT_SUCCESS == result ) { auto getTable = reinterpret_cast( GET_FUNCTION_PTR(loader, "zeGetSamplerProcAddrTable") ); - result = getTable( ZE_API_VERSION_CURRENT, &initialzeDdiTable.Sampler ); + result = getTableWithCheck(getTable, version, &initialzeDdiTable.Sampler ); } if( ZE_RESULT_SUCCESS == result ) { auto getTable = reinterpret_cast( GET_FUNCTION_PTR(loader, "zeGetVirtualMemProcAddrTable") ); - result = getTable( ZE_API_VERSION_CURRENT, &initialzeDdiTable.VirtualMem ); + result = getTableWithCheck(getTable, version, &initialzeDdiTable.VirtualMem ); } if( ZE_RESULT_SUCCESS == result ) @@ -217,7 +216,7 @@ namespace ze_lib // Optional auto getTable = reinterpret_cast( GET_FUNCTION_PTR(loader, "zeGetFabricEdgeExpProcAddrTable") ); - getTable( ZE_API_VERSION_CURRENT, &initialzeDdiTable.FabricEdgeExp ); + getTableWithCheck(getTable, version, &initialzeDdiTable.FabricEdgeExp ); } if( ZE_RESULT_SUCCESS == result ) @@ -225,13 +224,13 @@ namespace ze_lib // Optional auto getTable = reinterpret_cast( GET_FUNCTION_PTR(loader, "zeGetFabricVertexExpProcAddrTable") ); - getTable( ZE_API_VERSION_CURRENT, &initialzeDdiTable.FabricVertexExp ); + getTableWithCheck(getTable, version, &initialzeDdiTable.FabricVertexExp ); } return result; } #else - __zedlllocal ze_result_t context_t::zeDdiTableInit() + __zedlllocal ze_result_t context_t::zeDdiTableInit(ze_api_version_t version) { ze_result_t result = ZE_RESULT_SUCCESS; diff --git a/source/lib/zel_tracing_libddi.cpp b/source/lib/zel_tracing_libddi.cpp index cb481951..f20b75bb 100644 --- a/source/lib/zel_tracing_libddi.cpp +++ b/source/lib/zel_tracing_libddi.cpp @@ -15,7 +15,7 @@ namespace ze_lib /////////////////////////////////////////////////////////////////////////////// #ifdef DYNAMIC_LOAD_LOADER - __zedlllocal ze_result_t context_t::zelTracingDdiTableInit() + __zedlllocal ze_result_t context_t::zelTracingDdiTableInit(ze_api_version_t version) { ze_result_t result = ZE_RESULT_SUCCESS; @@ -23,13 +23,13 @@ namespace ze_lib { auto getTable = reinterpret_cast( GET_FUNCTION_PTR(loader, "zelGetTracerApiProcAddrTable") ); - result = getTable( ZE_API_VERSION_CURRENT, &zelTracingDdiTable.Tracer); + result = getTableWithCheck(getTable, version, &zelTracingDdiTable.Tracer); } return result; } #else - __zedlllocal ze_result_t context_t::zelTracingDdiTableInit() + __zedlllocal ze_result_t context_t::zelTracingDdiTableInit(ze_api_version_t version) { ze_result_t result; result = zelGetTracerApiProcAddrTable( ZE_API_VERSION_CURRENT, &zelTracingDdiTable.Tracer); diff --git a/source/lib/zes_libddi.cpp b/source/lib/zes_libddi.cpp index 4159a931..7c51ee89 100644 --- a/source/lib/zes_libddi.cpp +++ b/source/lib/zes_libddi.cpp @@ -17,7 +17,7 @@ namespace ze_lib /////////////////////////////////////////////////////////////////////////////// #ifdef DYNAMIC_LOAD_LOADER - __zedlllocal ze_result_t context_t::zesDdiTableInit() + __zedlllocal ze_result_t context_t::zesDdiTableInit(ze_api_version_t version) { ze_result_t result = ZE_RESULT_SUCCESS; @@ -26,14 +26,14 @@ namespace ze_lib // Optional auto getTable = reinterpret_cast( GET_FUNCTION_PTR(loader, "zesGetGlobalProcAddrTable") ); - getTable( ZE_API_VERSION_CURRENT, &initialzesDdiTable.Global ); + getTableWithCheck(getTable, version, &initialzesDdiTable.Global ); } if( ZE_RESULT_SUCCESS == result ) { auto getTable = reinterpret_cast( GET_FUNCTION_PTR(loader, "zesGetDeviceProcAddrTable") ); - result = getTable( ZE_API_VERSION_CURRENT, &initialzesDdiTable.Device ); + result = getTableWithCheck(getTable, version, &initialzesDdiTable.Device ); } if( ZE_RESULT_SUCCESS == result ) @@ -41,14 +41,14 @@ namespace ze_lib // Optional auto getTable = reinterpret_cast( GET_FUNCTION_PTR(loader, "zesGetDeviceExpProcAddrTable") ); - getTable( ZE_API_VERSION_CURRENT, &initialzesDdiTable.DeviceExp ); + getTableWithCheck(getTable, version, &initialzesDdiTable.DeviceExp ); } if( ZE_RESULT_SUCCESS == result ) { auto getTable = reinterpret_cast( GET_FUNCTION_PTR(loader, "zesGetDriverProcAddrTable") ); - result = getTable( ZE_API_VERSION_CURRENT, &initialzesDdiTable.Driver ); + result = getTableWithCheck(getTable, version, &initialzesDdiTable.Driver ); } if( ZE_RESULT_SUCCESS == result ) @@ -56,42 +56,42 @@ namespace ze_lib // Optional auto getTable = reinterpret_cast( GET_FUNCTION_PTR(loader, "zesGetDriverExpProcAddrTable") ); - getTable( ZE_API_VERSION_CURRENT, &initialzesDdiTable.DriverExp ); + getTableWithCheck(getTable, version, &initialzesDdiTable.DriverExp ); } if( ZE_RESULT_SUCCESS == result ) { auto getTable = reinterpret_cast( GET_FUNCTION_PTR(loader, "zesGetDiagnosticsProcAddrTable") ); - result = getTable( ZE_API_VERSION_CURRENT, &initialzesDdiTable.Diagnostics ); + result = getTableWithCheck(getTable, version, &initialzesDdiTable.Diagnostics ); } if( ZE_RESULT_SUCCESS == result ) { auto getTable = reinterpret_cast( GET_FUNCTION_PTR(loader, "zesGetEngineProcAddrTable") ); - result = getTable( ZE_API_VERSION_CURRENT, &initialzesDdiTable.Engine ); + result = getTableWithCheck(getTable, version, &initialzesDdiTable.Engine ); } if( ZE_RESULT_SUCCESS == result ) { auto getTable = reinterpret_cast( GET_FUNCTION_PTR(loader, "zesGetFabricPortProcAddrTable") ); - result = getTable( ZE_API_VERSION_CURRENT, &initialzesDdiTable.FabricPort ); + result = getTableWithCheck(getTable, version, &initialzesDdiTable.FabricPort ); } if( ZE_RESULT_SUCCESS == result ) { auto getTable = reinterpret_cast( GET_FUNCTION_PTR(loader, "zesGetFanProcAddrTable") ); - result = getTable( ZE_API_VERSION_CURRENT, &initialzesDdiTable.Fan ); + result = getTableWithCheck(getTable, version, &initialzesDdiTable.Fan ); } if( ZE_RESULT_SUCCESS == result ) { auto getTable = reinterpret_cast( GET_FUNCTION_PTR(loader, "zesGetFirmwareProcAddrTable") ); - result = getTable( ZE_API_VERSION_CURRENT, &initialzesDdiTable.Firmware ); + result = getTableWithCheck(getTable, version, &initialzesDdiTable.Firmware ); } if( ZE_RESULT_SUCCESS == result ) @@ -99,28 +99,28 @@ namespace ze_lib // Optional auto getTable = reinterpret_cast( GET_FUNCTION_PTR(loader, "zesGetFirmwareExpProcAddrTable") ); - getTable( ZE_API_VERSION_CURRENT, &initialzesDdiTable.FirmwareExp ); + getTableWithCheck(getTable, version, &initialzesDdiTable.FirmwareExp ); } if( ZE_RESULT_SUCCESS == result ) { auto getTable = reinterpret_cast( GET_FUNCTION_PTR(loader, "zesGetFrequencyProcAddrTable") ); - result = getTable( ZE_API_VERSION_CURRENT, &initialzesDdiTable.Frequency ); + result = getTableWithCheck(getTable, version, &initialzesDdiTable.Frequency ); } if( ZE_RESULT_SUCCESS == result ) { auto getTable = reinterpret_cast( GET_FUNCTION_PTR(loader, "zesGetLedProcAddrTable") ); - result = getTable( ZE_API_VERSION_CURRENT, &initialzesDdiTable.Led ); + result = getTableWithCheck(getTable, version, &initialzesDdiTable.Led ); } if( ZE_RESULT_SUCCESS == result ) { auto getTable = reinterpret_cast( GET_FUNCTION_PTR(loader, "zesGetMemoryProcAddrTable") ); - result = getTable( ZE_API_VERSION_CURRENT, &initialzesDdiTable.Memory ); + result = getTableWithCheck(getTable, version, &initialzesDdiTable.Memory ); } if( ZE_RESULT_SUCCESS == result ) @@ -128,35 +128,35 @@ namespace ze_lib // Optional auto getTable = reinterpret_cast( GET_FUNCTION_PTR(loader, "zesGetOverclockProcAddrTable") ); - getTable( ZE_API_VERSION_CURRENT, &initialzesDdiTable.Overclock ); + getTableWithCheck(getTable, version, &initialzesDdiTable.Overclock ); } if( ZE_RESULT_SUCCESS == result ) { auto getTable = reinterpret_cast( GET_FUNCTION_PTR(loader, "zesGetPerformanceFactorProcAddrTable") ); - result = getTable( ZE_API_VERSION_CURRENT, &initialzesDdiTable.PerformanceFactor ); + result = getTableWithCheck(getTable, version, &initialzesDdiTable.PerformanceFactor ); } if( ZE_RESULT_SUCCESS == result ) { auto getTable = reinterpret_cast( GET_FUNCTION_PTR(loader, "zesGetPowerProcAddrTable") ); - result = getTable( ZE_API_VERSION_CURRENT, &initialzesDdiTable.Power ); + result = getTableWithCheck(getTable, version, &initialzesDdiTable.Power ); } if( ZE_RESULT_SUCCESS == result ) { auto getTable = reinterpret_cast( GET_FUNCTION_PTR(loader, "zesGetPsuProcAddrTable") ); - result = getTable( ZE_API_VERSION_CURRENT, &initialzesDdiTable.Psu ); + result = getTableWithCheck(getTable, version, &initialzesDdiTable.Psu ); } if( ZE_RESULT_SUCCESS == result ) { auto getTable = reinterpret_cast( GET_FUNCTION_PTR(loader, "zesGetRasProcAddrTable") ); - result = getTable( ZE_API_VERSION_CURRENT, &initialzesDdiTable.Ras ); + result = getTableWithCheck(getTable, version, &initialzesDdiTable.Ras ); } if( ZE_RESULT_SUCCESS == result ) @@ -164,28 +164,28 @@ namespace ze_lib // Optional auto getTable = reinterpret_cast( GET_FUNCTION_PTR(loader, "zesGetRasExpProcAddrTable") ); - getTable( ZE_API_VERSION_CURRENT, &initialzesDdiTable.RasExp ); + getTableWithCheck(getTable, version, &initialzesDdiTable.RasExp ); } if( ZE_RESULT_SUCCESS == result ) { auto getTable = reinterpret_cast( GET_FUNCTION_PTR(loader, "zesGetSchedulerProcAddrTable") ); - result = getTable( ZE_API_VERSION_CURRENT, &initialzesDdiTable.Scheduler ); + result = getTableWithCheck(getTable, version, &initialzesDdiTable.Scheduler ); } if( ZE_RESULT_SUCCESS == result ) { auto getTable = reinterpret_cast( GET_FUNCTION_PTR(loader, "zesGetStandbyProcAddrTable") ); - result = getTable( ZE_API_VERSION_CURRENT, &initialzesDdiTable.Standby ); + result = getTableWithCheck(getTable, version, &initialzesDdiTable.Standby ); } if( ZE_RESULT_SUCCESS == result ) { auto getTable = reinterpret_cast( GET_FUNCTION_PTR(loader, "zesGetTemperatureProcAddrTable") ); - result = getTable( ZE_API_VERSION_CURRENT, &initialzesDdiTable.Temperature ); + result = getTableWithCheck(getTable, version, &initialzesDdiTable.Temperature ); } if( ZE_RESULT_SUCCESS == result ) @@ -193,13 +193,13 @@ namespace ze_lib // Optional auto getTable = reinterpret_cast( GET_FUNCTION_PTR(loader, "zesGetVFManagementExpProcAddrTable") ); - getTable( ZE_API_VERSION_CURRENT, &initialzesDdiTable.VFManagementExp ); + getTableWithCheck(getTable, version, &initialzesDdiTable.VFManagementExp ); } return result; } #else - __zedlllocal ze_result_t context_t::zesDdiTableInit() + __zedlllocal ze_result_t context_t::zesDdiTableInit(ze_api_version_t version) { ze_result_t result = ZE_RESULT_SUCCESS; diff --git a/source/lib/zet_libddi.cpp b/source/lib/zet_libddi.cpp index 5a6595b6..e3b90f65 100644 --- a/source/lib/zet_libddi.cpp +++ b/source/lib/zet_libddi.cpp @@ -14,10 +14,9 @@ namespace ze_lib { - /////////////////////////////////////////////////////////////////////////////// #ifdef DYNAMIC_LOAD_LOADER - __zedlllocal ze_result_t context_t::zetDdiTableInit() + __zedlllocal ze_result_t context_t::zetDdiTableInit(ze_api_version_t version) { ze_result_t result = ZE_RESULT_SUCCESS; @@ -26,7 +25,7 @@ namespace ze_lib // Optional auto getTable = reinterpret_cast( GET_FUNCTION_PTR(loader, "zetGetMetricDecoderExpProcAddrTable") ); - getTable( ZE_API_VERSION_CURRENT, &initialzetDdiTable.MetricDecoderExp ); + getTableWithCheck(getTable, version, &initialzetDdiTable.MetricDecoderExp ); } if( ZE_RESULT_SUCCESS == result ) @@ -34,7 +33,7 @@ namespace ze_lib // Optional auto getTable = reinterpret_cast( GET_FUNCTION_PTR(loader, "zetGetMetricProgrammableExpProcAddrTable") ); - getTable( ZE_API_VERSION_CURRENT, &initialzetDdiTable.MetricProgrammableExp ); + getTableWithCheck(getTable, version, &initialzetDdiTable.MetricProgrammableExp ); } if( ZE_RESULT_SUCCESS == result ) @@ -42,14 +41,14 @@ namespace ze_lib // Optional auto getTable = reinterpret_cast( GET_FUNCTION_PTR(loader, "zetGetMetricTracerExpProcAddrTable") ); - getTable( ZE_API_VERSION_CURRENT, &initialzetDdiTable.MetricTracerExp ); + getTableWithCheck(getTable, version, &initialzetDdiTable.MetricTracerExp ); } if( ZE_RESULT_SUCCESS == result ) { auto getTable = reinterpret_cast( GET_FUNCTION_PTR(loader, "zetGetDeviceProcAddrTable") ); - result = getTable( ZE_API_VERSION_CURRENT, &initialzetDdiTable.Device ); + result = getTableWithCheck(getTable, version, &initialzetDdiTable.Device ); } if( ZE_RESULT_SUCCESS == result ) @@ -57,49 +56,49 @@ namespace ze_lib // Optional auto getTable = reinterpret_cast( GET_FUNCTION_PTR(loader, "zetGetDeviceExpProcAddrTable") ); - getTable( ZE_API_VERSION_CURRENT, &initialzetDdiTable.DeviceExp ); + getTableWithCheck(getTable, version, &initialzetDdiTable.DeviceExp ); } if( ZE_RESULT_SUCCESS == result ) { auto getTable = reinterpret_cast( GET_FUNCTION_PTR(loader, "zetGetContextProcAddrTable") ); - result = getTable( ZE_API_VERSION_CURRENT, &initialzetDdiTable.Context ); + result = getTableWithCheck(getTable, version, &initialzetDdiTable.Context ); } if( ZE_RESULT_SUCCESS == result ) { auto getTable = reinterpret_cast( GET_FUNCTION_PTR(loader, "zetGetCommandListProcAddrTable") ); - result = getTable( ZE_API_VERSION_CURRENT, &initialzetDdiTable.CommandList ); + result = getTableWithCheck(getTable, version, &initialzetDdiTable.CommandList ); } if( ZE_RESULT_SUCCESS == result ) { auto getTable = reinterpret_cast( GET_FUNCTION_PTR(loader, "zetGetKernelProcAddrTable") ); - result = getTable( ZE_API_VERSION_CURRENT, &initialzetDdiTable.Kernel ); + result = getTableWithCheck(getTable, version, &initialzetDdiTable.Kernel ); } if( ZE_RESULT_SUCCESS == result ) { auto getTable = reinterpret_cast( GET_FUNCTION_PTR(loader, "zetGetModuleProcAddrTable") ); - result = getTable( ZE_API_VERSION_CURRENT, &initialzetDdiTable.Module ); + result = getTableWithCheck(getTable, version, &initialzetDdiTable.Module ); } if( ZE_RESULT_SUCCESS == result ) { auto getTable = reinterpret_cast( GET_FUNCTION_PTR(loader, "zetGetDebugProcAddrTable") ); - result = getTable( ZE_API_VERSION_CURRENT, &initialzetDdiTable.Debug ); + result = getTableWithCheck(getTable, version, &initialzetDdiTable.Debug ); } if( ZE_RESULT_SUCCESS == result ) { auto getTable = reinterpret_cast( GET_FUNCTION_PTR(loader, "zetGetMetricProcAddrTable") ); - result = getTable( ZE_API_VERSION_CURRENT, &initialzetDdiTable.Metric ); + result = getTableWithCheck(getTable, version, &initialzetDdiTable.Metric ); } if( ZE_RESULT_SUCCESS == result ) @@ -107,14 +106,14 @@ namespace ze_lib // Optional auto getTable = reinterpret_cast( GET_FUNCTION_PTR(loader, "zetGetMetricExpProcAddrTable") ); - getTable( ZE_API_VERSION_CURRENT, &initialzetDdiTable.MetricExp ); + getTableWithCheck(getTable, version, &initialzetDdiTable.MetricExp ); } if( ZE_RESULT_SUCCESS == result ) { auto getTable = reinterpret_cast( GET_FUNCTION_PTR(loader, "zetGetMetricGroupProcAddrTable") ); - result = getTable( ZE_API_VERSION_CURRENT, &initialzetDdiTable.MetricGroup ); + result = getTableWithCheck(getTable, version, &initialzetDdiTable.MetricGroup ); } if( ZE_RESULT_SUCCESS == result ) @@ -122,41 +121,41 @@ namespace ze_lib // Optional auto getTable = reinterpret_cast( GET_FUNCTION_PTR(loader, "zetGetMetricGroupExpProcAddrTable") ); - getTable( ZE_API_VERSION_CURRENT, &initialzetDdiTable.MetricGroupExp ); + getTableWithCheck(getTable, version, &initialzetDdiTable.MetricGroupExp ); } if( ZE_RESULT_SUCCESS == result ) { auto getTable = reinterpret_cast( GET_FUNCTION_PTR(loader, "zetGetMetricQueryProcAddrTable") ); - result = getTable( ZE_API_VERSION_CURRENT, &initialzetDdiTable.MetricQuery ); + result = getTableWithCheck(getTable, version, &initialzetDdiTable.MetricQuery ); } if( ZE_RESULT_SUCCESS == result ) { auto getTable = reinterpret_cast( GET_FUNCTION_PTR(loader, "zetGetMetricQueryPoolProcAddrTable") ); - result = getTable( ZE_API_VERSION_CURRENT, &initialzetDdiTable.MetricQueryPool ); + result = getTableWithCheck(getTable, version, &initialzetDdiTable.MetricQueryPool ); } if( ZE_RESULT_SUCCESS == result ) { auto getTable = reinterpret_cast( GET_FUNCTION_PTR(loader, "zetGetMetricStreamerProcAddrTable") ); - result = getTable( ZE_API_VERSION_CURRENT, &initialzetDdiTable.MetricStreamer ); + result = getTableWithCheck(getTable, version, &initialzetDdiTable.MetricStreamer ); } if( ZE_RESULT_SUCCESS == result ) { auto getTable = reinterpret_cast( GET_FUNCTION_PTR(loader, "zetGetTracerExpProcAddrTable") ); - result = getTable( ZE_API_VERSION_CURRENT, &initialzetDdiTable.TracerExp ); + result = getTableWithCheck(getTable, version, &initialzetDdiTable.TracerExp ); } return result; } #else - __zedlllocal ze_result_t context_t::zetDdiTableInit() + __zedlllocal ze_result_t context_t::zetDdiTableInit(ze_api_version_t version) { ze_result_t result = ZE_RESULT_SUCCESS; diff --git a/source/loader/linux/loader_init.cpp b/source/loader/linux/loader_init.cpp index e3d322ac..aa9f57ba 100644 --- a/source/loader/linux/loader_init.cpp +++ b/source/loader/linux/loader_init.cpp @@ -12,12 +12,10 @@ 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 diff --git a/source/loader/ze_loader_api.cpp b/source/loader/ze_loader_api.cpp index 4ab76f77..3c1c8902 100644 --- a/source/loader/ze_loader_api.cpp +++ b/source/loader/ze_loader_api.cpp @@ -56,7 +56,6 @@ zeLoaderGetTracingHandle() /// - ::Pointer to the Loader's Context ZE_DLLEXPORT loader::context_t *ZE_APICALL zelLoaderGetContext() { - printf("zelLoaderGetContext\n"); return loader::context; } From 5f46576b73927a9cac30978d456b20b18afefe5a Mon Sep 17 00:00:00 2001 From: "Neil R. Spruit" Date: Fri, 15 Nov 2024 11:33:47 -0800 Subject: [PATCH 4/7] Add workflows for static builds Signed-off-by: Neil R. Spruit --- .github/workflows/build-multi-static.yml | 159 +++++++++++++++++++++++ .github/workflows/build-quick-static.yml | 49 +++++++ 2 files changed, 208 insertions(+) create mode 100644 .github/workflows/build-multi-static.yml create mode 100644 .github/workflows/build-quick-static.yml diff --git a/.github/workflows/build-multi-static.yml b/.github/workflows/build-multi-static.yml new file mode 100644 index 00000000..0921e8ad --- /dev/null +++ b/.github/workflows/build-multi-static.yml @@ -0,0 +1,159 @@ +on: + push: + branches: [ master ] + pull_request: + branches: [ master ] + workflow_dispatch: + +permissions: read-all + +defaults: + run: + shell: bash + +jobs: + config: + if: github.repository_owner == 'oneapi-src' + runs-on: ubuntu-latest + outputs: + short-sha: ${{ steps.const.outputs.short-sha }} + ref-slug: ${{ steps.const.outputs.ref-slug }} + steps: + - uses: actions/checkout@v4 + with: + clean: true + ref: ${{ github.event.pull_request.head.sha }} + - name: Set constants + id: const + run: | + cat >> ${GITHUB_OUTPUT} <- + ${{ matrix.os.name == 'windows' && 'windows' || + format('{0}-{1}.{2}', + matrix.os.name, + matrix.os.vmaj, + matrix.os.vmin + ) + }} + CCACHE_DIR: ${{ github.workspace }}/ccache + run: | + cat >> ${GITHUB_OUTPUT} <> ${GITHUB_ENV} + - name: "Registry login: ghcr.io" + run: | + echo ${{ secrets.GITHUB_TOKEN }} | + docker login -u sys-lzdev --password-stdin ghcr.io + - name: Build image + run: | + docker info + docker build \ + ${{ runner.os == 'Windows' && ' \ + --memory 16G ' || ' ' + }}\ + ${{ matrix.os.vmaj != '' && format(' \ + --build-arg VMAJ={0} \ + --build-arg VMIN={1} ', matrix.os.vmaj, matrix.os.vmin) || ' ' + }}\ + --pull \ + --tag ${DOCKER_IMAGE}:${{ needs.config.outputs.ref-slug }} \ + - < .github/docker/${{ matrix.os.name }}.Dockerfile + - name: Build + id: build + run: | + mkdir build + docker run \ + --rm \ + --interactive \ + -v '${{ github.workspace }}':${MOUNT_TARGET} \ + -w ${MOUNT_TARGET}/build \ + -e CCACHE_BASEDIR=${MOUNT_TARGET} \ + -e CCACHE_DIR=${MOUNT_TARGET}/ccache \ + -v '${{ steps.const.outputs.ccache-dir }}':${MOUNT_TARGET}/ccache \ + ${DOCKER_IMAGE}:${{ needs.config.outputs.ref-slug }} \ + bash -e -x <<-EOF + + cmake \ + ${{ matrix.os.name != 'windows' && ' \ + -G Ninja ' || ' ' + }}\ + ${{ matrix.arch == 'arm64' && ' \ + -D CMAKE_C_COMPILER=aarch64-linux-gnu-gcc \ + -D CMAKE_CXX_COMPILER=aarch64-linux-gnu-g++ \ + -D CMAKE_SYSTEM_PROCESSOR=aarch64 ' || ' ' + }}\ + -D CMAKE_C_COMPILER_LAUNCHER=ccache \ + -D CMAKE_CXX_COMPILER_LAUNCHER=ccache \ + -D CMAKE_BUILD_TYPE=Release \ + -D CMAKE_INSTALL_PREFIX=${{ matrix.target == 'install' && '../level-zero-install' || matrix.target == 'package' && '/usr' || '' }} \ + -D CPACK_OUTPUT_FILE_PREFIX=${MOUNT_TARGET}/level-zero-package \ + .. + + cmake --build . ${PARALLEL} --target ${{ matrix.target }} ${{ matrix.os.name == 'windows' && '--config Release' || '' }} + + ccache --show-stats + + EOF diff --git a/.github/workflows/build-quick-static.yml b/.github/workflows/build-quick-static.yml new file mode 100644 index 00000000..33c75000 --- /dev/null +++ b/.github/workflows/build-quick-static.yml @@ -0,0 +1,49 @@ +on: + push: + branches: [ master ] + pull_request: + branches: [ master ] + workflow_dispatch: + +permissions: read-all + +jobs: + build-linux: + if: github.repository_owner == 'oneapi-src' + runs-on: [ubuntu-latest] + steps: + - uses: actions/checkout@v3 + - uses: hendrikmuhs/ccache-action@v1 + - name: Build Loader on Latest Ubuntu + run: | + mkdir build + cd build + cmake \ + -D CMAKE_C_COMPILER_LAUNCHER=ccache \ + -D CMAKE_CXX_COMPILER_LAUNCHER=ccache \ + -D CMAKE_BUILD_TYPE=Release \ + -D BUILD_L0_LOADER_TESTS=1 \ + .. + make -j$(nproc) + - env: + ZE_ENABLE_LOADER_DEBUG_TRACE: '1' + ZEL_LIBRARY_PATH: '${{ github.workspace }}/build/lib' + working-directory: build + run: ctest -V + + build-windows: + if: github.repository_owner == 'oneapi-src' + runs-on: [windows-latest] + steps: + - uses: actions/checkout@v3 + - name: Build Loader on Latest Windows + run: | + mkdir build + cd build + cmake -D BUILD_L0_LOADER_TESTS=1 -D BUILD_STATIC=1 .. + cmake --build . --config Release + - env: + ZE_ENABLE_LOADER_DEBUG_TRACE: '1' + ZEL_LIBRARY_PATH: '${{ github.workspace }}/build/bin/Release' + working-directory: build + run: ctest -C Release -V From 922da0e914eaa7eef423af42c517a4f7850661f8 Mon Sep 17 00:00:00 2001 From: "Neil R. Spruit" Date: Fri, 15 Nov 2024 11:57:17 -0800 Subject: [PATCH 5/7] Fix windows build for dllmain Signed-off-by: Neil R. Spruit --- source/lib/windows/lib_init.cpp | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/source/lib/windows/lib_init.cpp b/source/lib/windows/lib_init.cpp index 07f7fa46..db47b9a9 100644 --- a/source/lib/windows/lib_init.cpp +++ b/source/lib/windows/lib_init.cpp @@ -13,16 +13,7 @@ namespace ze_lib { -#ifdef DYNAMIC_LOAD_LOADER - export "C" BOOL APIENTRY DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved) { - if (fdwReason == DLL_PROCESS_DETACH) { - delete context; - } else if (fdwReason == DLL_PROCESS_ATTACH) { - context = new context_t; - } - return TRUE; - } -#else +#ifndef DYNAMIC_LOAD_LOADER extern "C" BOOL APIENTRY DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved) { if (fdwReason == DLL_PROCESS_DETACH) { delete context; From a9842e32eb44b1feab7f7d3617b7d4dd2a8bee08 Mon Sep 17 00:00:00 2001 From: "Neil R. Spruit" Date: Fri, 15 Nov 2024 13:31:49 -0800 Subject: [PATCH 6/7] Fix windows tests build and multi workflow Signed-off-by: Neil R. Spruit --- .github/workflows/build-multi-static.yml | 1 + test/CMakeLists.txt | 4 ++++ 2 files changed, 5 insertions(+) diff --git a/.github/workflows/build-multi-static.yml b/.github/workflows/build-multi-static.yml index 0921e8ad..c510c5cd 100644 --- a/.github/workflows/build-multi-static.yml +++ b/.github/workflows/build-multi-static.yml @@ -148,6 +148,7 @@ jobs: -D CMAKE_C_COMPILER_LAUNCHER=ccache \ -D CMAKE_CXX_COMPILER_LAUNCHER=ccache \ -D CMAKE_BUILD_TYPE=Release \ + -D BUILD_STATIC=1 \ -D CMAKE_INSTALL_PREFIX=${{ matrix.target == 'install' && '../level-zero-install' || matrix.target == 'package' && '/usr' || '' }} \ -D CPACK_OUTPUT_FILE_PREFIX=${MOUNT_TARGET}/level-zero-package \ .. diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 2df908a0..0fb63e4c 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -16,7 +16,11 @@ target_link_libraries( # For some reason the MSVC runtime libraries used by googletest and test # binaries don't match, so force the test binary to use the dynamic runtime. if(MSVC) + if (BUILD_STATIC) + target_compile_options(tests PRIVATE "/MT$<$:d>") + else() target_compile_options(tests PRIVATE "/MD$<$:d>") + endif() endif() add_test(NAME tests_api COMMAND tests --gtest_filter=*GivenLevelZeroLoaderPresentWhenCallingzeGetLoaderVersionsAPIThenValidVersionIsReturned*) From acf49cf2cf2191c1d7a8b0038809c7d491052968 Mon Sep 17 00:00:00 2001 From: "Neil R. Spruit" Date: Tue, 10 Dec 2024 12:51:52 -0800 Subject: [PATCH 7/7] Update Logging and Version check with more details and fix for static loader build Signed-off-by: Neil R. Spruit --- CMakeLists.txt | 19 +++++-- source/lib/ze_lib.cpp | 68 ++++++++++++------------- source/lib/ze_lib.h | 116 +++++++++++++++++++++++++++++++++++++++--- 3 files changed, 156 insertions(+), 47 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index ca0746fa..1ecd2385 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -53,13 +53,22 @@ endif() include(FetchContent) if(BUILD_L0_LOADER_TESTS) - FetchContent_Declare( - googletest - URL https://github.com/google/googletest/archive/refs/tags/v1.14.0.zip - ) + FetchContent_Declare( + googletest + GIT_REPOSITORY https://github.com/google/googletest.git + GIT_TAG v1.14.0 + ) + add_library(GTest::GTest INTERFACE IMPORTED) + target_link_libraries(GTest::GTest INTERFACE gtest_main) # For Windows: Prevent overriding the parent project's compiler/linker settings - set(gtest_force_shared_crt ON CACHE BOOL "" FORCE) + if(MSVC) + if (BUILD_STATIC) + set(gtest_force_shared_crt OFF CACHE BOOL "" FORCE) + else() + set(gtest_force_shared_crt ON CACHE BOOL "" FORCE) + endif() + endif() FetchContent_MakeAvailable(googletest) diff --git a/source/lib/ze_lib.cpp b/source/lib/ze_lib.cpp index 126d0bbc..f569a5bd 100644 --- a/source/lib/ze_lib.cpp +++ b/source/lib/ze_lib.cpp @@ -22,7 +22,9 @@ namespace ze_lib /////////////////////////////////////////////////////////////////////////////// context_t::context_t() - {}; + { + debugTraceEnabled = getenv_tobool( "ZE_ENABLE_LOADER_DEBUG_TRACE" ); + }; /////////////////////////////////////////////////////////////////////////////// context_t::~context_t() @@ -35,12 +37,6 @@ namespace ze_lib ze_lib::destruction = true; }; - ////////////////////////////////////////////////////////////////////////// - void debug_trace_message(const std::string &message, const std::string &extra) - { - std::cerr << message << " " << extra << std::endl; - } - ////////////////////////////////////////////////////////////////////////// __zedlllocal ze_result_t context_t::Init(ze_init_flags_t flags, bool sysmanOnly, ze_init_driver_type_desc_t* desc) { @@ -55,8 +51,8 @@ namespace ze_lib loader = LOAD_DRIVER_LIBRARY(loaderFullLibraryPath.c_str()); if( NULL == loader ) { - std::string message = "ze_lib Context Init() Loader Library Load Failed"; - debug_trace_message(message, ""); + std::string message = "ze_lib Context Init() Loader Library Load Failed with "; + debug_trace_message(message, to_string(ZE_RESULT_ERROR_UNINITIALIZED)); return ZE_RESULT_ERROR_UNINITIALIZED; } @@ -65,16 +61,16 @@ namespace ze_lib GET_FUNCTION_PTR(loader, "zeLoaderInit") ); result = loaderInit(); if( ZE_RESULT_SUCCESS != result ) { - std::string message = "ze_lib Context Init() Loader Init Failed"; - debug_trace_message(message, ""); + std::string message = "ze_lib Context Init() Loader Init Failed with "; + debug_trace_message(message, to_string(result)); return result; } typedef HMODULE (ZE_APICALL *getTracing_t)(); auto getTracing = reinterpret_cast( GET_FUNCTION_PTR(loader, "zeLoaderGetTracingHandle") ); if (getTracing == nullptr) { - std::string message = "ze_lib Context Init() zeLoaderGetTracingHandle missing"; - debug_trace_message(message, ""); + std::string message = "ze_lib Context Init() zeLoaderGetTracingHandle missing, returning "; + debug_trace_message(message, to_string(ZE_RESULT_ERROR_UNINITIALIZED)); return ZE_RESULT_ERROR_UNINITIALIZED; } tracing_lib = getTracing(); @@ -82,8 +78,8 @@ namespace ze_lib auto loaderTracingLayerInit = reinterpret_cast( GET_FUNCTION_PTR(loader, "zelLoaderTracingLayerInit") ); if (loaderTracingLayerInit == nullptr) { - std::string message = "ze_lib Context Init() zelLoaderTracingLayerInit missing"; - debug_trace_message(message, ""); + std::string message = "ze_lib Context Init() zelLoaderTracingLayerInit missing, returning "; + debug_trace_message(message, to_string(ZE_RESULT_ERROR_UNINITIALIZED)); return ZE_RESULT_ERROR_UNINITIALIZED; } typedef loader::context_t * (ZE_APICALL *zelLoaderGetContext_t)(); @@ -97,16 +93,16 @@ namespace ze_lib size_t size = 0; result = zelLoaderGetVersions(&size, nullptr); if (ZE_RESULT_SUCCESS != result) { - std::string message = "ze_lib Context Init() zelLoaderGetVersions Failed"; - debug_trace_message(message, ""); + std::string message = "ze_lib Context Init() zelLoaderGetVersions Failed with"; + debug_trace_message(message, to_string(result)); return result; } std::vector versions(size); result = zelLoaderGetVersions(&size, versions.data()); if (ZE_RESULT_SUCCESS != result) { - std::string message = "ze_lib Context Init() zelLoaderGetVersions Failed to read component versions"; - debug_trace_message(message, ""); + std::string message = "ze_lib Context Init() zelLoaderGetVersions Failed to read component versions with "; + debug_trace_message(message, to_string(result)); return result; } bool zeInitDriversSupport = true; @@ -121,12 +117,14 @@ namespace ze_lib zeInitDriversSupport = false; } } else { - std::string message = "ze_lib Context Init() Loader version is too new"; - debug_trace_message(message, ""); + std::string message = "ze_lib Context Init() Loader version is too new, returning "; + debug_trace_message(message, to_string(ZE_RESULT_ERROR_UNSUPPORTED_VERSION)); return ZE_RESULT_ERROR_UNSUPPORTED_VERSION; } } } + std::string version_message = "Loader API Version to be requested is v" + std::to_string(ZE_MAJOR_VERSION(version)) + "." + std::to_string(ZE_MINOR_VERSION(version)); + debug_trace_message(version_message, ""); #else result = zeLoaderInit(); if( ZE_RESULT_SUCCESS == result ) { @@ -162,8 +160,8 @@ namespace ze_lib { result = zeDdiTableInit(version); if (result != ZE_RESULT_SUCCESS) { - std::string message = "ze_lib Context Init() zeDdiTableInit failed"; - debug_trace_message(message, ""); + std::string message = "ze_lib Context Init() zeDdiTableInit failed with "; + debug_trace_message(message, to_string(result)); } } // Init the ZET DDI Tables @@ -171,8 +169,8 @@ namespace ze_lib { result = zetDdiTableInit(version); if( ZE_RESULT_SUCCESS != result ) { - std::string message = "ze_lib Context Init() zetDdiTableInit failed"; - debug_trace_message(message, ""); + std::string message = "ze_lib Context Init() zetDdiTableInit failed with "; + debug_trace_message(message, to_string(result)); } } // Init the ZES DDI Tables @@ -180,8 +178,8 @@ namespace ze_lib { result = zesDdiTableInit(version); if (result != ZE_RESULT_SUCCESS) { - std::string message = "ze_lib Context Init() zesDdiTableInit failed"; - debug_trace_message(message, ""); + std::string message = "ze_lib Context Init() zesDdiTableInit failed with "; + debug_trace_message(message, to_string(result)); } } // Init the Tracing API DDI Tables @@ -189,8 +187,8 @@ namespace ze_lib { result = zelTracingDdiTableInit(version); if (result != ZE_RESULT_SUCCESS) { - std::string message = "ze_lib Context Init() zelTracingDdiTableInit failed"; - debug_trace_message(message, ""); + std::string message = "ze_lib Context Init() zelTracingDdiTableInit failed with "; + debug_trace_message(message, to_string(result)); } } // Init the stored ddi tables for the tracing layer @@ -216,8 +214,8 @@ namespace ze_lib auto loaderDriverCheck = reinterpret_cast( GET_FUNCTION_PTR(loader, "zelLoaderDriverCheck") ); if (loaderDriverCheck == nullptr) { - std::string message = "ze_lib Context Init() zelLoaderDriverCheck missing"; - debug_trace_message(message, ""); + std::string message = "ze_lib Context Init() zelLoaderDriverCheck missing, returning "; + debug_trace_message(message, to_string(ZE_RESULT_ERROR_UNINITIALIZED)); return ZE_RESULT_ERROR_UNINITIALIZED; } result = loaderDriverCheck(flags, desc, &ze_lib::context->initialzeDdiTable.Global, &ze_lib::context->initialzesDdiTable.Global, &requireDdiReinit, sysmanOnly); @@ -226,8 +224,8 @@ namespace ze_lib auto loaderDriverCheck = reinterpret_cast( GET_FUNCTION_PTR(loader, "zelLoaderDriverCheck") ); if (loaderDriverCheck == nullptr) { - std::string message = "ze_lib Context Init() zelLoaderDriverCheck missing"; - debug_trace_message(message, ""); + std::string message = "ze_lib Context Init() zelLoaderDriverCheck missing, returning "; + debug_trace_message(message, to_string(ZE_RESULT_ERROR_UNINITIALIZED)); return ZE_RESULT_ERROR_UNINITIALIZED; } result = loaderDriverCheck(flags, &ze_lib::context->initialzeDdiTable.Global, &ze_lib::context->initialzesDdiTable.Global, &requireDdiReinit, sysmanOnly); @@ -236,8 +234,8 @@ namespace ze_lib result = zelLoaderDriverCheck(flags, desc, &ze_lib::context->initialzeDdiTable.Global, &ze_lib::context->initialzesDdiTable.Global, &requireDdiReinit, sysmanOnly); #endif if (result != ZE_RESULT_SUCCESS) { - std::string message = "ze_lib Context Init() zelLoaderDriverCheck failed"; - debug_trace_message(message, ""); + std::string message = "ze_lib Context Init() zelLoaderDriverCheck failed with "; + debug_trace_message(message, to_string(result)); } // 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 && loaderContextAccessAllowed) { diff --git a/source/lib/ze_lib.h b/source/lib/ze_lib.h index 93af3e5d..3308536c 100644 --- a/source/lib/ze_lib.h +++ b/source/lib/ze_lib.h @@ -20,6 +20,8 @@ #include #include #include +#include +#include namespace ze_lib { @@ -34,14 +36,113 @@ namespace ze_lib context_t(); ~context_t(); - /////////////////////////////////////////////////////////////////////////////// - template - ze_result_t getTableWithCheck(T getTable, ze_api_version_t version, TableType* table) { - if (getTable == nullptr) { - return ZE_RESULT_ERROR_UNINITIALIZED; - } - return getTable(version, table); + ////////////////////////////////////////////////////////////////////////// + std::string to_string(const ze_result_t result) { + if (result == ZE_RESULT_SUCCESS) { + return "ZE_RESULT_SUCCESS"; + } else if (result == ZE_RESULT_NOT_READY) { + return "ZE_RESULT_NOT_READY"; + } else if (result == ZE_RESULT_ERROR_UNINITIALIZED) { + return "ZE_RESULT_ERROR_UNINITIALIZED"; + } else if (result == ZE_RESULT_ERROR_DEVICE_LOST) { + return "ZE_RESULT_ERROR_DEVICE_LOST"; + } else if (result == ZE_RESULT_ERROR_INVALID_ARGUMENT) { + return "ZE_RESULT_ERROR_INVALID_ARGUMENT"; + } else if (result == ZE_RESULT_ERROR_OUT_OF_HOST_MEMORY) { + return "ZE_RESULT_ERROR_OUT_OF_HOST_MEMORY"; + } else if (result == ZE_RESULT_ERROR_OUT_OF_DEVICE_MEMORY) { + return "ZE_RESULT_ERROR_OUT_OF_DEVICE_MEMORY"; + } else if (result == ZE_RESULT_ERROR_MODULE_BUILD_FAILURE) { + return "ZE_RESULT_ERROR_MODULE_BUILD_FAILURE"; + } else if (result == ZE_RESULT_ERROR_MODULE_LINK_FAILURE) { + return "ZE_RESULT_ERROR_MODULE_LINK_FAILURE"; + } else if (result == ZE_RESULT_ERROR_INSUFFICIENT_PERMISSIONS) { + return "ZE_RESULT_ERROR_INSUFFICIENT_PERMISSIONS"; + } else if (result == ZE_RESULT_ERROR_NOT_AVAILABLE) { + return "ZE_RESULT_ERROR_NOT_AVAILABLE"; + } else if (result == ZE_RESULT_ERROR_DEPENDENCY_UNAVAILABLE) { + return "ZE_RESULT_ERROR_DEPENDENCY_UNAVAILABLE"; + } else if (result == ZE_RESULT_WARNING_DROPPED_DATA) { + return "ZE_RESULT_WARNING_DROPPED_DATA"; + } else if (result == ZE_RESULT_ERROR_UNSUPPORTED_VERSION) { + return "ZE_RESULT_ERROR_UNSUPPORTED_VERSION"; + } else if (result == ZE_RESULT_ERROR_UNSUPPORTED_FEATURE) { + return "ZE_RESULT_ERROR_UNSUPPORTED_FEATURE"; + } else if (result == ZE_RESULT_ERROR_INVALID_NULL_HANDLE) { + return "ZE_RESULT_ERROR_INVALID_NULL_HANDLE"; + } else if (result == ZE_RESULT_ERROR_HANDLE_OBJECT_IN_USE) { + return "ZE_RESULT_ERROR_HANDLE_OBJECT_IN_USE"; + } else if (result == ZE_RESULT_ERROR_INVALID_NULL_POINTER) { + return "ZE_RESULT_ERROR_INVALID_NULL_POINTER"; + } else if (result == ZE_RESULT_ERROR_INVALID_SIZE) { + return "ZE_RESULT_ERROR_INVALID_SIZE"; + } else if (result == ZE_RESULT_ERROR_UNSUPPORTED_SIZE) { + return "ZE_RESULT_ERROR_UNSUPPORTED_SIZE"; + } else if (result == ZE_RESULT_ERROR_UNSUPPORTED_ALIGNMENT) { + return "ZE_RESULT_ERROR_UNSUPPORTED_ALIGNMENT"; + } else if (result == ZE_RESULT_ERROR_INVALID_SYNCHRONIZATION_OBJECT) { + return "ZE_RESULT_ERROR_INVALID_SYNCHRONIZATION_OBJECT"; + } else if (result == ZE_RESULT_ERROR_INVALID_ENUMERATION) { + return "ZE_RESULT_ERROR_INVALID_ENUMERATION"; + } else if (result == ZE_RESULT_ERROR_UNSUPPORTED_ENUMERATION) { + return "ZE_RESULT_ERROR_UNSUPPORTED_ENUMERATION"; + } else if (result == ZE_RESULT_ERROR_UNSUPPORTED_IMAGE_FORMAT) { + return "ZE_RESULT_ERROR_UNSUPPORTED_IMAGE_FORMAT"; + } else if (result == ZE_RESULT_ERROR_INVALID_NATIVE_BINARY) { + return "ZE_RESULT_ERROR_INVALID_NATIVE_BINARY"; + } else if (result == ZE_RESULT_ERROR_INVALID_GLOBAL_NAME) { + return "ZE_RESULT_ERROR_INVALID_GLOBAL_NAME"; + } else if (result == ZE_RESULT_ERROR_INVALID_KERNEL_NAME) { + return "ZE_RESULT_ERROR_INVALID_KERNEL_NAME"; + } else if (result == ZE_RESULT_ERROR_INVALID_FUNCTION_NAME) { + return "ZE_RESULT_ERROR_INVALID_FUNCTION_NAME"; + } else if (result == ZE_RESULT_ERROR_INVALID_GROUP_SIZE_DIMENSION) { + return "ZE_RESULT_ERROR_INVALID_GROUP_SIZE_DIMENSION"; + } else if (result == ZE_RESULT_ERROR_INVALID_GLOBAL_WIDTH_DIMENSION) { + return "ZE_RESULT_ERROR_INVALID_GLOBAL_WIDTH_DIMENSION"; + } else if (result == ZE_RESULT_ERROR_INVALID_KERNEL_ARGUMENT_INDEX) { + return "ZE_RESULT_ERROR_INVALID_KERNEL_ARGUMENT_INDEX"; + } else if (result == ZE_RESULT_ERROR_INVALID_KERNEL_ARGUMENT_SIZE) { + return "ZE_RESULT_ERROR_INVALID_KERNEL_ARGUMENT_SIZE"; + } else if (result == ZE_RESULT_ERROR_INVALID_KERNEL_ATTRIBUTE_VALUE) { + return "ZE_RESULT_ERROR_INVALID_KERNEL_ATTRIBUTE_VALUE"; + } else if (result == ZE_RESULT_ERROR_INVALID_MODULE_UNLINKED) { + return "ZE_RESULT_ERROR_INVALID_MODULE_UNLINKED"; + } else if (result == ZE_RESULT_ERROR_INVALID_COMMAND_LIST_TYPE) { + return "ZE_RESULT_ERROR_INVALID_COMMAND_LIST_TYPE"; + } else if (result == ZE_RESULT_ERROR_OVERLAPPING_REGIONS) { + return "ZE_RESULT_ERROR_OVERLAPPING_REGIONS"; + } else if (result == ZE_RESULT_ERROR_UNKNOWN) { + return "ZE_RESULT_ERROR_UNKNOWN"; + } else { + return std::to_string(static_cast(result)); } + } + + ////////////////////////////////////////////////////////////////////////// + void debug_trace_message(std::string message, std::string result) { + if (debugTraceEnabled){ + std::string debugTracePrefix = "ZE_LOADER_DEBUG_TRACE:"; + std::cerr << debugTracePrefix << message << result << std::endl; + } + } + + /////////////////////////////////////////////////////////////////////////////// + template + ze_result_t getTableWithCheck(T getTable, ze_api_version_t version, TableType* table) { + ze_result_t result = ZE_RESULT_ERROR_UNINITIALIZED; + if (getTable == nullptr) { + std::string message = "getTableWithCheck Failed for " + std::string(typeid(TableType).name()) + " with "; + debug_trace_message(message, to_string(result)); + return result; + } + result = getTable(version, table); + if (result != ZE_RESULT_SUCCESS) { + std::string message = "getTableWithCheck Failed for " + std::string(typeid(TableType).name()) + " with "; + debug_trace_message(message, to_string(result)); + } + return result; + } std::once_flag initOnce; std::once_flag initOnceDrivers; @@ -71,6 +172,7 @@ namespace ze_lib bool inTeardown = false; bool zesInuse = false; bool zeInuse = false; + bool debugTraceEnabled = false; }; extern context_t *context;