From 152cee3737fe38e2e583573d0bb5c5cc274acfea Mon Sep 17 00:00:00 2001 From: Aidan Belton-Schure Date: Mon, 25 Nov 2024 15:34:47 +0000 Subject: [PATCH] SWDEV-443561 - Add tools dispatch table Change-Id: I3445554e486ab7b94592571f52c1530cb918d021 --- .../include/hip/amd_detail/hip_api_trace.hpp | 30 +++++++++++++++++ hipamd/src/hip_api_trace.cpp | 32 +++++++++++++++++-- hipamd/src/hip_context.cpp | 23 +++++++++++++ hipamd/src/hip_table_interface.cpp | 1 + hipamd/src/hip_table_interface_c.cpp | 1 + 5 files changed, 85 insertions(+), 2 deletions(-) diff --git a/hipamd/include/hip/amd_detail/hip_api_trace.hpp b/hipamd/include/hip/amd_detail/hip_api_trace.hpp index efab82a610..b0d5b83658 100644 --- a/hipamd/include/hip/amd_detail/hip_api_trace.hpp +++ b/hipamd/include/hip/amd_detail/hip_api_trace.hpp @@ -52,6 +52,7 @@ // DO NOT REMOVE IT. #define HIP_API_TABLE_MAJOR_VERSION 0 #define HIP_COMPILER_API_TABLE_MAJOR_VERSION 0 +#define HIP_TOOLS_API_TABLE_MAJOR_VERSION 0 #define HIP_RUNTIME_API_TABLE_MAJOR_VERSION 0 // The step version number should be changed whenever the size of the API table struct(s) change. @@ -61,9 +62,11 @@ // - Reset any of the *_STEP_VERSION defines to zero if the corresponding *_MAJOR_VERSION increases #define HIP_API_TABLE_STEP_VERSION 0 #define HIP_COMPILER_API_TABLE_STEP_VERSION 0 +#define HIP_TOOLS_API_TABLE_STEP_VERSION 0 #define HIP_RUNTIME_API_TABLE_STEP_VERSION 8 // HIP API interface +// HIP compiler dispatch functions typedef hipError_t (*t___hipPopCallConfiguration)(dim3* gridDim, dim3* blockDim, size_t* sharedMem, hipStream_t* stream); typedef hipError_t (*t___hipPushCallConfiguration)(dim3 gridDim, dim3 blockDim, size_t sharedMem, @@ -83,6 +86,10 @@ typedef void (*t___hipRegisterVar)(void** modules, void* var, char* hostVar, char* deviceVar, int ext, size_t size, int constant, int global); typedef void (*t___hipUnregisterFatBinary)(void** modules); +// HIP tools dispatch functions +typedef void (*t___hipReportDevices)(size_t numDevices, const hipUUID* uuids); + +// HIP runtime dispatch functions typedef const char* (*t_hipApiName)(uint32_t id); typedef hipError_t (*t_hipArray3DCreate)(hipArray_t* array, const HIP_ARRAY3D_DESCRIPTOR* pAllocateArray); @@ -1558,3 +1565,26 @@ struct HipDispatchTable { // 5) ADD "DO NOT EDIT ABOVE!" COMMENT // ******************************************************************************************* // }; + +// HIP Tools dispatch table +struct HipToolsDispatchTable { + // HIP_TOOLS_API_TABLE_STEP_VERSION == 0 + size_t size; + t___hipReportDevices __hipReportDevices_fn; + + // DO NOT EDIT ABOVE! + // HIP_TOOLS_API_TABLE_STEP_VERSION == 1 + + // ******************************************************************************************* // + // + // READ BELOW + // + // ******************************************************************************************* // + // KEEP AT END OF STRUCT + // 1) DO NOT REORDER ANY EXIST MEMBERS + // 2) INCREASE STEP VERSION DEFINE BEFORE ADDING NEW MEMBERS + // 3) INSERT NEW MEMBERS UNDER APPROPRIATE STEP VERSION COMMENT + // 4) GENERATE COMMENT FOR NEXT STEP VERSION + // 5) ADD "DO NOT EDIT ABOVE!" COMMENT + // ******************************************************************************************* // +}; diff --git a/hipamd/src/hip_api_trace.cpp b/hipamd/src/hip_api_trace.cpp index c9d8303a1b..506b23d5f7 100644 --- a/hipamd/src/hip_api_trace.cpp +++ b/hipamd/src/hip_api_trace.cpp @@ -36,6 +36,7 @@ ROCPROFILER_REGISTER_DEFINE_IMPORT(hip, HIP_ROCP_REG_VERSION) ROCPROFILER_REGISTER_DEFINE_IMPORT(hip_compiler, HIP_ROCP_REG_VERSION) +ROCPROFILER_REGISTER_DEFINE_IMPORT(hip_tools, HIP_ROCP_REG_VERSION) #elif !defined(HIP_ROCPROFILER_REGISTER) #define HIP_ROCPROFILER_REGISTER 0 #endif @@ -829,6 +830,11 @@ void UpdateDispatchTable(HipCompilerDispatchTable* ptrCompilerDispatchTable) { ptrCompilerDispatchTable->__hipUnregisterFatBinary_fn = hip::__hipUnregisterFatBinary; } +void UpdateDispatchTable(HipToolsDispatchTable* ptrToolsDispatchTable) { + ptrToolsDispatchTable->size = sizeof(HipToolsDispatchTable); + ptrToolsDispatchTable->__hipReportDevices_fn = nullptr; +} + void UpdateDispatchTable(HipDispatchTable* ptrDispatchTable) { ptrDispatchTable->size = sizeof(HipDispatchTable); ptrDispatchTable->hipApiName_fn = hip::hipApiName; @@ -1331,9 +1337,10 @@ constexpr auto ComputeTableSize(size_t num_funcs) { HIP_DEFINE_DISPATCH_TABLE_INFO(HipDispatchTable, hip) HIP_DEFINE_DISPATCH_TABLE_INFO(HipCompilerDispatchTable, hip_compiler) +HIP_DEFINE_DISPATCH_TABLE_INFO(HipToolsDispatchTable, hip_tools) #endif -template void ToolInit(Tp* table) { +template void ToolsInit(Tp* table) { #if HIP_ROCPROFILER_REGISTER > 0 auto table_array = std::array{static_cast(table)}; auto lib_id = rocprofiler_register_library_indentifier_t{}; @@ -1360,7 +1367,7 @@ template Tp& GetDispatchTableImpl() { UpdateDispatchTable(&dispatch_table); // Profiler Registration, may wrap the function pointers - ToolInit(&dispatch_table); + ToolsInit(&dispatch_table); return dispatch_table; } @@ -1385,6 +1392,10 @@ GetHipCompilerDispatchTable() { static auto* _v = &GetDispatchTableImpl(); return _v; } +const HipToolsDispatchTable* GetHipToolsDispatchTable() { + static auto* _v = &GetDispatchTableImpl(); + return _v; +} } // namespace hip #if !defined(_WIN32) @@ -1441,6 +1452,23 @@ static_assert(HIP_COMPILER_API_TABLE_MAJOR_VERSION == 0 && HIP_COMPILER_API_TABL "If you get this error, add new HIP_ENFORCE_ABI(...) code for the new function " "pointers and then update this check so it is true"); +// These ensure that function pointers are not re-ordered +// HIP_TOOLS_API_TABLE_STEP_VERSION == 0 +HIP_ENFORCE_ABI(HipToolsDispatchTable, __hipReportDevices_fn, 0) +// HIP_TOOLS_API_TABLE_STEP_VERSION == 1 + +// if HIP_ENFORCE_ABI entries are added for each new function pointer in the table, the number below +// will be +1 of the number in the last HIP_ENFORCE_ABI line. E.g.: +// +// HIP_ENFORCE_ABI(, , 8) +// +// HIP_ENFORCE_ABI_VERSIONING(
, 9) <- 8 + 1 = 9 +HIP_ENFORCE_ABI_VERSIONING(HipToolsDispatchTable, 1) + +static_assert(HIP_TOOLS_API_TABLE_MAJOR_VERSION == 0 && HIP_TOOLS_API_TABLE_STEP_VERSION == 0, + "If you get this error, add new HIP_ENFORCE_ABI(...) code for the new function " + "pointers and then update this check so it is true"); + // These ensure that function pointers are not re-ordered // HIP_RUNTIME_API_TABLE_STEP_VERSION == 0 HIP_ENFORCE_ABI(HipDispatchTable, hipApiName_fn, 0) diff --git a/hipamd/src/hip_context.cpp b/hipamd/src/hip_context.cpp index b68fd60b31..befb40259b 100644 --- a/hipamd/src/hip_context.cpp +++ b/hipamd/src/hip_context.cpp @@ -25,6 +25,11 @@ #include "rocclr/utils/flags.hpp" #include "rocclr/utils/versions.hpp" +#include +namespace hip { +const HipToolsDispatchTable* GetHipToolsDispatchTable(); +} // namespace hip + namespace hip { std::once_flag g_ihipInitialized; @@ -65,6 +70,24 @@ void init(bool* status) { amd::RuntimeTearDown::RegisterObject(device); } + if (hip::GetHipToolsDispatchTable()->__hipReportDevices_fn != nullptr) { + size_t numDevices = g_devices.size(); + + std::vector uuids(numDevices); + + int i = 0; + for (const auto& dev : g_devices) { + auto* deviceHandle = dev->devices()[0]; + const auto& info = deviceHandle->info(); + memcpy(uuids[i].bytes, info.uuid_, sizeof(info.uuid_)); + // if assert fails, the memcpy bytes param needs to be addressed + static_assert(sizeof(info.uuid_) == sizeof(uuids[0].bytes), "error ABI issue"); + ++i; + } + + hip::GetHipToolsDispatchTable()->__hipReportDevices_fn(numDevices, uuids.data()); + } + amd::Context* hContext = new amd::Context(devices, amd::Context::Info()); if (!hContext) { *status = false; diff --git a/hipamd/src/hip_table_interface.cpp b/hipamd/src/hip_table_interface.cpp index 3d142ff5a2..77092e1fa1 100644 --- a/hipamd/src/hip_table_interface.cpp +++ b/hipamd/src/hip_table_interface.cpp @@ -23,6 +23,7 @@ namespace hip { const HipDispatchTable* GetHipDispatchTable(); const HipCompilerDispatchTable* GetHipCompilerDispatchTable(); +const HipToolsDispatchTable* GetHipToolsDispatchTable(); } // namespace hip extern "C" hipError_t __hipPopCallConfiguration(dim3* gridDim, dim3* blockDim, size_t* sharedMem, diff --git a/hipamd/src/hip_table_interface_c.cpp b/hipamd/src/hip_table_interface_c.cpp index 5f765300af..5cca3c6069 100644 --- a/hipamd/src/hip_table_interface_c.cpp +++ b/hipamd/src/hip_table_interface_c.cpp @@ -23,6 +23,7 @@ namespace hip { const HipDispatchTable* GetHipDispatchTable(); const HipCompilerDispatchTable* GetHipCompilerDispatchTable(); +const HipToolsDispatchTable* GetHipToolsDispatchTable(); } // namespace hip #ifdef _WIN32