[llvm] [AMDGPU][OpenMP] Using HSA API calls for APU detection. (PR #90186)

Thorsten Blaß via llvm-commits llvm-commits at lists.llvm.org
Tue Apr 30 03:18:57 PDT 2024


https://github.com/ThorBl updated https://github.com/llvm/llvm-project/pull/90186

>From 8353018c943bab9f65f81d0d9c3e8d6b37c60ad0 Mon Sep 17 00:00:00 2001
From: Thorsten Blass <Thorsten.Blass at amd.com>
Date: Fri, 26 Apr 2024 05:40:46 -0400
Subject: [PATCH 1/2] Using HSA API calls for APU detection.

A simplified APU detection algorithm will be enabled if ROCm 6.1 is available at the compiler's build time. This algorithm is more reliable and simplified, as it doesn't rely on string comparison of the GFX name. With this new algorithm, the system can effectively distinguish between an MI300A and an MI300X without the need for further checking.
---
 offload/plugins-nextgen/amdgpu/CMakeLists.txt |  4 +++-
 .../amdgpu/dynamic_hip/hip_version.h          | 16 +++++++++++++
 offload/plugins-nextgen/amdgpu/src/rtl.cpp    | 23 ++++++++++++++++++-
 3 files changed, 41 insertions(+), 2 deletions(-)
 create mode 100644 offload/plugins-nextgen/amdgpu/dynamic_hip/hip_version.h

diff --git a/offload/plugins-nextgen/amdgpu/CMakeLists.txt b/offload/plugins-nextgen/amdgpu/CMakeLists.txt
index f5f7096137c20f..5fb4a37b6604d2 100644
--- a/offload/plugins-nextgen/amdgpu/CMakeLists.txt
+++ b/offload/plugins-nextgen/amdgpu/CMakeLists.txt
@@ -40,7 +40,9 @@ if(hsa-runtime64_FOUND AND NOT LIBOMPTARGET_FORCE_DLOPEN_LIBHSA)
   target_link_libraries(omptarget.rtl.amdgpu PRIVATE hsa-runtime64::hsa-runtime64)
 else()
   libomptarget_say("Building AMDGPU plugin for dlopened libhsa")
-  target_include_directories(omptarget.rtl.amdgpu PRIVATE dynamic_hsa)
+  target_include_directories(omptarget.rtl.amdgpu 
+                            PRIVATE dynamic_hsa 
+                            PRIVATE dynamic_hip)
   target_sources(omptarget.rtl.amdgpu PRIVATE dynamic_hsa/hsa.cpp)
 endif()
 
diff --git a/offload/plugins-nextgen/amdgpu/dynamic_hip/hip_version.h b/offload/plugins-nextgen/amdgpu/dynamic_hip/hip_version.h
new file mode 100644
index 00000000000000..496331cf86ec8f
--- /dev/null
+++ b/offload/plugins-nextgen/amdgpu/dynamic_hip/hip_version.h
@@ -0,0 +1,16 @@
+#ifndef HIP_VERSION
+#define HIP_VERSION
+
+#define HIP_VERSION_MAJOR 0
+#define HIP_VERSION_MINOR 0
+#define HIP_VERSION_PATCH 0
+#define HIP_VERSION_GITHASH ""
+#define HIP_VERSION_BUILD_ID 0
+#define HIP_VERSION_BUILD_NAME ""
+#define HIP_VERSION                                                            \
+  (HIP_VERSION_MAJOR * 10000000 + HIP_VERSION_MINOR * 100000 +                 \
+   HIP_VERSION_PATCH)
+
+#define __HIP_HAS_GET_PCH 0
+
+#endif /* HIP_VERSION */
diff --git a/offload/plugins-nextgen/amdgpu/src/rtl.cpp b/offload/plugins-nextgen/amdgpu/src/rtl.cpp
index 00650b801b4202..58323dbd7233d2 100644
--- a/offload/plugins-nextgen/amdgpu/src/rtl.cpp
+++ b/offload/plugins-nextgen/amdgpu/src/rtl.cpp
@@ -57,6 +57,12 @@
 #endif
 
 #if defined(__has_include)
+#if __has_include("hip/hip_version.h")
+#include "hip/hip_version.h"
+#else
+#include "hip_version.h"
+#endif
+
 #if __has_include("hsa/hsa.h")
 #include "hsa/hsa.h"
 #include "hsa/hsa_ext_amd.h"
@@ -2809,7 +2815,21 @@ struct AMDGPUDeviceTy : public GenericDeviceTy, AMDGenericDeviceTy {
 
   /// Detect if current architecture is an APU.
   Error checkIfAPU() {
-    // TODO: replace with ROCr API once it becomes available.
+#if (HIP_VERSION_MAJOR >= 6 && HIP_VERSION_MINOR >= 1)
+
+    uint8_t MemoryProperties[8];
+
+    hsa_status_t Stat = hsa_agent_get_info(
+        getAgent(), (hsa_agent_info_t)HSA_AMD_AGENT_INFO_MEMORY_PROPERTIES,
+        MemoryProperties);
+
+    if (auto Err = Plugin::check(Stat, "Error: Unable to fetch the memory "
+                                       "properties of the GPU. (%s)\n"))
+      return Err;
+
+    IsAPU = hsa_flag_isset64(MemoryProperties,
+                             HSA_AMD_MEMORY_PROPERTY_AGENT_IS_APU);
+#else
     llvm::StringRef StrGfxName(ComputeUnitKind);
     IsAPU = llvm::StringSwitch<bool>(StrGfxName)
                 .Case("gfx940", true)
@@ -2832,6 +2852,7 @@ struct AMDGPUDeviceTy : public GenericDeviceTy, AMDGenericDeviceTy {
       IsAPU = true;
       return Plugin::success();
     }
+#endif
     return Plugin::success();
   }
 

>From 127c59ed32513abe0c9dcd71ea5da5c736a5820d Mon Sep 17 00:00:00 2001
From: Thorsten Blass <Thorsten.Blass at amd.com>
Date: Tue, 30 Apr 2024 06:00:02 -0400
Subject: [PATCH 2/2] Removed dependency on HIP.

---
 offload/plugins-nextgen/amdgpu/CMakeLists.txt    |  4 +---
 .../amdgpu/dynamic_hip/hip_version.h             | 16 ----------------
 offload/plugins-nextgen/amdgpu/src/rtl.cpp       |  2 +-
 3 files changed, 2 insertions(+), 20 deletions(-)
 delete mode 100644 offload/plugins-nextgen/amdgpu/dynamic_hip/hip_version.h

diff --git a/offload/plugins-nextgen/amdgpu/CMakeLists.txt b/offload/plugins-nextgen/amdgpu/CMakeLists.txt
index 5fb4a37b6604d2..f5f7096137c20f 100644
--- a/offload/plugins-nextgen/amdgpu/CMakeLists.txt
+++ b/offload/plugins-nextgen/amdgpu/CMakeLists.txt
@@ -40,9 +40,7 @@ if(hsa-runtime64_FOUND AND NOT LIBOMPTARGET_FORCE_DLOPEN_LIBHSA)
   target_link_libraries(omptarget.rtl.amdgpu PRIVATE hsa-runtime64::hsa-runtime64)
 else()
   libomptarget_say("Building AMDGPU plugin for dlopened libhsa")
-  target_include_directories(omptarget.rtl.amdgpu 
-                            PRIVATE dynamic_hsa 
-                            PRIVATE dynamic_hip)
+  target_include_directories(omptarget.rtl.amdgpu PRIVATE dynamic_hsa)
   target_sources(omptarget.rtl.amdgpu PRIVATE dynamic_hsa/hsa.cpp)
 endif()
 
diff --git a/offload/plugins-nextgen/amdgpu/dynamic_hip/hip_version.h b/offload/plugins-nextgen/amdgpu/dynamic_hip/hip_version.h
deleted file mode 100644
index 496331cf86ec8f..00000000000000
--- a/offload/plugins-nextgen/amdgpu/dynamic_hip/hip_version.h
+++ /dev/null
@@ -1,16 +0,0 @@
-#ifndef HIP_VERSION
-#define HIP_VERSION
-
-#define HIP_VERSION_MAJOR 0
-#define HIP_VERSION_MINOR 0
-#define HIP_VERSION_PATCH 0
-#define HIP_VERSION_GITHASH ""
-#define HIP_VERSION_BUILD_ID 0
-#define HIP_VERSION_BUILD_NAME ""
-#define HIP_VERSION                                                            \
-  (HIP_VERSION_MAJOR * 10000000 + HIP_VERSION_MINOR * 100000 +                 \
-   HIP_VERSION_PATCH)
-
-#define __HIP_HAS_GET_PCH 0
-
-#endif /* HIP_VERSION */
diff --git a/offload/plugins-nextgen/amdgpu/src/rtl.cpp b/offload/plugins-nextgen/amdgpu/src/rtl.cpp
index 58323dbd7233d2..8c6e22d94c5659 100644
--- a/offload/plugins-nextgen/amdgpu/src/rtl.cpp
+++ b/offload/plugins-nextgen/amdgpu/src/rtl.cpp
@@ -2815,7 +2815,7 @@ struct AMDGPUDeviceTy : public GenericDeviceTy, AMDGenericDeviceTy {
 
   /// Detect if current architecture is an APU.
   Error checkIfAPU() {
-#if (HIP_VERSION_MAJOR >= 6 && HIP_VERSION_MINOR >= 1)
+#if (HSA_AMD_INTERFACE_VERSION_MAJOR >= 1 && HSA_AMD_INTERFACE_VERSION_MINOR >= 5)
 
     uint8_t MemoryProperties[8];
 



More information about the llvm-commits mailing list