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

via llvm-commits llvm-commits at lists.llvm.org
Fri Apr 26 02:43:50 PDT 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-backend-amdgpu

Author: Thorsten Blaß (ThorBl)

<details>
<summary>Changes</summary>

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.

---
Full diff: https://github.com/llvm/llvm-project/pull/90186.diff


3 Files Affected:

- (modified) offload/plugins-nextgen/amdgpu/CMakeLists.txt (+3-1) 
- (added) offload/plugins-nextgen/amdgpu/dynamic_hip/hip_version.h (+16) 
- (modified) offload/plugins-nextgen/amdgpu/src/rtl.cpp (+22-1) 


``````````diff
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();
   }
 

``````````

</details>


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


More information about the llvm-commits mailing list