From 880ddd43879ed3aa5e9fb58ebf38ede9d5783501 Mon Sep 17 00:00:00 2001 From: "Jonathan R. Madsen" Date: Mon, 16 Oct 2023 21:07:57 -0500 Subject: [PATCH] rocprofiler-register support - Update CMakeLists.txt - find_package for rocprofiler-register - this is an optional package until rocprofiler-register is added to the CI - define HSA_VERSION_{MAJOR,MINOR,PATCH} ppdefs - Update runtime.cpp - include - if rocprofiler-register succeeds, do not support v1 unless explicitly requested Change-Id: I8f48bbf3f6b52fb91ddade2f198491a1256035fe [ROCm/ROCR-Runtime commit: f9cf1852e5b431eae270f8ad98f26d0d5211aa9e] --- .../runtime/hsa-runtime/CMakeLists.txt | 9 ++++ .../hsa-runtime/core/runtime/runtime.cpp | 49 +++++++++++++++++++ 2 files changed, 58 insertions(+) diff --git a/projects/rocr-runtime/runtime/hsa-runtime/CMakeLists.txt b/projects/rocr-runtime/runtime/hsa-runtime/CMakeLists.txt index 01d1fc2c0e..f92550f978 100644 --- a/projects/rocr-runtime/runtime/hsa-runtime/CMakeLists.txt +++ b/projects/rocr-runtime/runtime/hsa-runtime/CMakeLists.txt @@ -281,6 +281,15 @@ endif() target_link_libraries ( ${CORE_RUNTIME_TARGET} PRIVATE hsakmt::hsakmt PkgConfig::drm) target_link_libraries ( ${CORE_RUNTIME_TARGET} PRIVATE elf::elf dl pthread rt ) +find_package(rocprofiler-register) +if(rocprofiler-register_FOUND) + target_compile_definitions(${CORE_RUNTIME_TARGET} PRIVATE HSA_ROCPROFILER_REGISTER=1 + HSA_VERSION_MAJOR=${VERSION_MAJOR} + HSA_VERSION_MINOR=${VERSION_MINOR} + HSA_VERSION_PATCH=${VERSION_PATCH}) + target_link_libraries(${CORE_RUNTIME_TARGET} PRIVATE rocprofiler-register::rocprofiler-register) +endif() + ## Set the VERSION and SOVERSION values set_property ( TARGET ${CORE_RUNTIME_TARGET} PROPERTY VERSION "${SO_VERSION_STRING}" ) set_property ( TARGET ${CORE_RUNTIME_TARGET} PROPERTY SOVERSION "${VERSION_MAJOR}" ) diff --git a/projects/rocr-runtime/runtime/hsa-runtime/core/runtime/runtime.cpp b/projects/rocr-runtime/runtime/hsa-runtime/core/runtime/runtime.cpp index 0def3224cf..3ea2a7e74b 100644 --- a/projects/rocr-runtime/runtime/hsa-runtime/core/runtime/runtime.cpp +++ b/projects/rocr-runtime/runtime/hsa-runtime/core/runtime/runtime.cpp @@ -45,6 +45,7 @@ #include #include #include +#include #include #include #include @@ -52,6 +53,10 @@ #include #include +#if defined(HSA_ROCPROFILER_REGISTER) && HSA_ROCPROFILER_REGISTER > 0 +#include +#endif + #include "core/common/shared.h" #include "core/inc/hsa_ext_interface.h" #include "core/inc/amd_cpu_agent.h" @@ -66,8 +71,22 @@ #include "core/inc/exceptions.h" #include "inc/hsa_ven_amd_aqlprofile.h" +#ifndef HSA_VERSION_MAJOR #define HSA_VERSION_MAJOR 1 +#endif +#ifndef HSA_VERSION_MINOR #define HSA_VERSION_MINOR 1 +#endif +#ifndef HSA_VERSION_PATCH +#define HSA_VERSION_PATCH 0 +#endif + +#if defined(HSA_ROCPROFILER_REGISTER) && HSA_ROCPROFILER_REGISTER > 0 +#define ROCP_REG_VERSION \ + ROCPROFILER_REGISTER_COMPUTE_VERSION_3(HSA_VERSION_MAJOR, HSA_VERSION_MINOR, HSA_VERSION_PATCH) + +ROCPROFILER_REGISTER_DEFINE_IMPORT(hsa, ROCP_REG_VERSION) +#endif const char rocrbuildid[] __attribute__((used)) = "ROCR BUILD ID: " STRING(ROCR_BUILD_ID); @@ -1720,6 +1739,36 @@ void Runtime::LoadTools() { typedef Agent* (*tool_wrap_t)(Agent*); typedef void (*tool_add_t)(Runtime*); +#if defined(HSA_ROCPROFILER_REGISTER) && HSA_ROCPROFILER_REGISTER > 0 + auto* profiler_api_table_ = static_cast(&hsa_api_table_); + auto lib_id = rocprofiler_register_library_indentifier_t{}; + auto success = + rocprofiler_register_library_api_table("hsa", &ROCPROFILER_REGISTER_IMPORT_FUNC(hsa), + ROCP_REG_VERSION, &profiler_api_table_, 1, &lib_id); + + bool allow_v1_registration = false; + if(os::IsEnvVarSet("HSA_TOOLS_ROCPROFILER_V1_TOOLS")) + { + // assume true if env variable is set + allow_v1_registration = true; + auto allow_v1_value = os::GetEnvVar("HSA_TOOLS_ROCPROFILER_V1_TOOLS"); + // support using numbers, off, false, no, n, or f + if (!allow_v1_value.empty()) { + if (allow_v1_value.find_first_not_of("0123456789") == std::string::npos) { + allow_v1_registration = (std::stoi(allow_v1_value) != 0); + } else if (std::regex_match( + allow_v1_value, + std::regex{"^(off|false|no|n|f)$", std::regex_constants::icase})) { + allow_v1_registration = false; + } + } + } + + // if rocprofiler library supports registration and v1 support not explicitly requested, do not + // use old method + if (success != 0 && !allow_v1_registration) return; +#endif + std::vector failed; //Get loaded libs and filter to tool libraries.