Skip to content

Fix amdgpu-arch for dll name on Windows #101350

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 7, 2025
Merged

Conversation

yxsamliu
Copy link
Collaborator

Recently HIP runtime changed dll name to amdhip64_n.dll on Windows, where n is ROCm major version number.

Fix amdgpu-arch to search for amdhip64_n.dll on Windows.

@yxsamliu yxsamliu requested review from Artem-B and jhuber6 July 31, 2024 15:18
@llvmbot llvmbot added clang Clang issues not falling into any other category backend:AMDGPU labels Jul 31, 2024
@llvmbot
Copy link
Member

llvmbot commented Jul 31, 2024

@llvm/pr-subscribers-backend-amdgpu

@llvm/pr-subscribers-clang

Author: Yaxun (Sam) Liu (yxsamliu)

Changes

Recently HIP runtime changed dll name to amdhip64_n.dll on Windows, where n is ROCm major version number.

Fix amdgpu-arch to search for amdhip64_n.dll on Windows.


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

2 Files Affected:

  • (modified) clang/tools/amdgpu-arch/AMDGPUArch.cpp (+3)
  • (modified) clang/tools/amdgpu-arch/AMDGPUArchByHIP.cpp (+108-4)
diff --git a/clang/tools/amdgpu-arch/AMDGPUArch.cpp b/clang/tools/amdgpu-arch/AMDGPUArch.cpp
index 7ae57b7877e1f..fefd4f08d5ed2 100644
--- a/clang/tools/amdgpu-arch/AMDGPUArch.cpp
+++ b/clang/tools/amdgpu-arch/AMDGPUArch.cpp
@@ -21,6 +21,9 @@ static cl::opt<bool> Help("h", cl::desc("Alias for -help"), cl::Hidden);
 // Mark all our options with this category.
 static cl::OptionCategory AMDGPUArchCategory("amdgpu-arch options");
 
+cl::opt<bool> Verbose("verbose", cl::desc("Enable verbose output"),
+                      cl::init(false), cl::cat(AMDGPUArchCategory));
+
 static void PrintVersion(raw_ostream &OS) {
   OS << clang::getClangToolFullVersion("amdgpu-arch") << '\n';
 }
diff --git a/clang/tools/amdgpu-arch/AMDGPUArchByHIP.cpp b/clang/tools/amdgpu-arch/AMDGPUArchByHIP.cpp
index 7338872dbf32f..f9beb5046568c 100644
--- a/clang/tools/amdgpu-arch/AMDGPUArchByHIP.cpp
+++ b/clang/tools/amdgpu-arch/AMDGPUArchByHIP.cpp
@@ -11,9 +11,21 @@
 //
 //===----------------------------------------------------------------------===//
 
+#include "llvm/Support/CommandLine.h"
 #include "llvm/Support/DynamicLibrary.h"
 #include "llvm/Support/Error.h"
+#include "llvm/Support/FileSystem.h"
+#include "llvm/Support/Path.h"
+#include "llvm/Support/Process.h"
+#include "llvm/Support/Program.h"
 #include "llvm/Support/raw_ostream.h"
+#include <algorithm>
+#include <string>
+#include <vector>
+
+#ifdef _WIN32
+#include <windows.h>
+#endif
 
 using namespace llvm;
 
@@ -31,16 +43,108 @@ typedef hipError_t (*hipGetDeviceCount_t)(int *);
 typedef hipError_t (*hipDeviceGet_t)(int *, int);
 typedef hipError_t (*hipGetDeviceProperties_t)(hipDeviceProp_t *, int);
 
-int printGPUsByHIP() {
+extern cl::opt<bool> Verbose;
+
 #ifdef _WIN32
-  constexpr const char *DynamicHIPPath = "amdhip64.dll";
+std::vector<std::string> getSearchPaths() {
+  std::vector<std::string> Paths;
+
+  // Get the directory of the current executable
+  if (auto MainExe = sys::fs::getMainExecutable(nullptr, nullptr);
+      !MainExe.empty())
+    Paths.push_back(sys::path::parent_path(MainExe).str());
+
+  // Get the system directory
+  char SystemDirectory[MAX_PATH];
+  if (GetSystemDirectoryA(SystemDirectory, MAX_PATH) > 0) {
+    Paths.push_back(SystemDirectory);
+  }
+
+  // Get the Windows directory
+  char WindowsDirectory[MAX_PATH];
+  if (GetWindowsDirectoryA(WindowsDirectory, MAX_PATH) > 0) {
+    Paths.push_back(WindowsDirectory);
+  }
+
+  // Get the current working directory
+  SmallVector<char, 256> CWD;
+  if (sys::fs::current_path(CWD))
+    Paths.push_back(std::string(CWD.begin(), CWD.end()));
+
+  // Get the PATH environment variable
+  if (auto PathEnv = llvm::sys::Process::GetEnv("PATH")) {
+    SmallVector<StringRef, 16> PathList;
+    StringRef(*PathEnv).split(PathList, sys::EnvPathSeparator);
+    for (auto &Path : PathList)
+      Paths.push_back(Path.str());
+  }
+
+  return Paths;
+}
+
+// Custom comparison function for dll name
+bool compareVersions(const std::string &a, const std::string &b) {
+  // Extract version numbers
+  int versionA = std::stoi(a.substr(a.find_last_of('_') + 1));
+  int versionB = std::stoi(b.substr(b.find_last_of('_') + 1));
+  return versionA > versionB;
+}
+
+#endif
+
+// On Windows, prefer amdhip64_n.dll where n is ROCm major version and greater
+// value of n takes precedence. If amdhip64_n.dll is not found, fall back to
+// amdhip64.dll. The reason is that a normal driver installation only has
+// amdhip64_n.dll but we do not know what n is since this progrm may be used
+// with a future version of HIP runtime.
+//
+// On Linux, always use default libamdhip64.so.
+std::pair<std::string, bool> findNewestHIPDLL() {
+#ifdef _WIN32
+  const char *HipDLLPrefix = "amdhip64_";
+  const char *HipDLLSuffix = ".dll";
+
+  std::vector<std::string> SearchPaths = getSearchPaths();
+  std::vector<std::string> DLLNames;
+
+  for (const auto &Dir : SearchPaths) {
+    std::error_code EC;
+    for (sys::fs::directory_iterator DirIt(Dir, EC), DirEnd;
+         DirIt != DirEnd && !EC; DirIt.increment(EC)) {
+      StringRef Filename = sys::path::filename(DirIt->path());
+      if (Filename.starts_with(HipDLLPrefix) &&
+          Filename.ends_with(HipDLLSuffix))
+        DLLNames.push_back(sys::path::convert_to_slash(DirIt->path()));
+    }
+    if (!DLLNames.empty())
+      break;
+  }
+
+  if (DLLNames.empty())
+    return {"amdhip64.dll", true};
+
+  std::sort(DLLNames.begin(), DLLNames.end(), compareVersions);
+  return {DLLNames[0], false};
 #else
-  constexpr const char *DynamicHIPPath = "libamdhip64.so";
+  // On Linux, fallback to default shared object
+  return {"libamdhip64.so", true};
 #endif
+}
+
+int printGPUsByHIP() {
+  auto [DynamicHIPPath, IsFallback] = findNewestHIPDLL();
+
+  if (Verbose) {
+    if (IsFallback)
+      outs() << "Using default HIP runtime: " << DynamicHIPPath << "\n";
+    else
+      outs() << "Found HIP runtime: " << DynamicHIPPath << "\n";
+  }
 
   std::string ErrMsg;
   auto DynlibHandle = std::make_unique<llvm::sys::DynamicLibrary>(
-      llvm::sys::DynamicLibrary::getPermanentLibrary(DynamicHIPPath, &ErrMsg));
+      llvm::sys::DynamicLibrary::getPermanentLibrary(DynamicHIPPath.c_str(),
+                                                     &ErrMsg));
   if (!DynlibHandle->isValid()) {
     llvm::errs() << "Failed to load " << DynamicHIPPath << ": " << ErrMsg
                  << '\n';

@yxsamliu
Copy link
Collaborator Author

ping

@Artem-B
Copy link
Member

Artem-B commented Aug 23, 2024

This is very Windows-specific.
@rnk -- would you have time to take a look?

@yxsamliu yxsamliu requested a review from kzhuravl August 26, 2024 13:14
@yxsamliu
Copy link
Collaborator Author

ping

@yxsamliu
Copy link
Collaborator Author

yxsamliu commented Mar 6, 2025

ping

Recently HIP runtime changed dll name to amdhip64_n.dll on Windows, where
n is ROCm major version number.

Fix amdgpu-arch to search for amdhip64_n.dll on Windows.
@yxsamliu yxsamliu merged commit 6fa1bfa into llvm:main Mar 7, 2025
11 checks passed
@dyung
Copy link
Collaborator

dyung commented Mar 7, 2025

@yxsamliu this seems to be causing a build failure on a Windows bot https://lab.llvm.org/buildbot/#/builders/46/builds/13184

FAILED: tools/clang/tools/amdgpu-arch/CMakeFiles/amdgpu-arch.dir/AMDGPUArchByHIP.cpp.obj 
C:\bin\ccache.exe C:\PROGRA~2\MICROS~1\2019\BUILDT~1\VC\Tools\MSVC\1429~1.301\bin\HostX64\x64\cl.exe  /nologo /TP -DCLANG_BUILD_STATIC -DGTEST_HAS_RTTI=0 -DLLVM_BUILD_STATIC -DUNICODE -D_CRT_NONSTDC_NO_DEPRECATE -D_CRT_NONSTDC_NO_WARNINGS -D_CRT_SECURE_NO_DEPRECATE -D_CRT_SECURE_NO_WARNINGS -D_GLIBCXX_ASSERTIONS -D_HAS_EXCEPTIONS=0 -D_SCL_SECURE_NO_DEPRECATE -D_SCL_SECURE_NO_WARNINGS -D_UNICODE -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS -Itools\clang\tools\amdgpu-arch -IZ:\b\llvm-clang-x86_64-sie-win\llvm-project\clang\tools\amdgpu-arch -IZ:\b\llvm-clang-x86_64-sie-win\llvm-project\clang\include -Itools\clang\include -Iinclude -IZ:\b\llvm-clang-x86_64-sie-win\llvm-project\llvm\include /DWIN32 /D_WINDOWS   /Zc:inline /Zc:preprocessor /Zc:__cplusplus /Oi /bigobj /permissive- /W4 -wd4141 -wd4146 -wd4244 -wd4267 -wd4291 -wd4351 -wd4456 -wd4457 -wd4458 -wd4459 -wd4503 -wd4624 -wd4722 -wd4100 -wd4127 -wd4512 -wd4505 -wd4610 -wd4510 -wd4702 -wd4245 -wd4706 -wd4310 -wd4701 -wd4703 -wd4389 -wd4611 -wd4805 -wd4204 -wd4577 -wd4091 -wd4592 -wd4319 -wd4709 -wd5105 -wd4324 -wd4251 -wd4275 -w14062 -we4238 /Gw /O2 /Ob2  -MD  /EHs-c- /GR- -UNDEBUG -std:c++17 /showIncludes /Fotools\clang\tools\amdgpu-arch\CMakeFiles\amdgpu-arch.dir\AMDGPUArchByHIP.cpp.obj /Fdtools\clang\tools\amdgpu-arch\CMakeFiles\amdgpu-arch.dir\ /FS -c Z:\b\llvm-clang-x86_64-sie-win\llvm-project\clang\tools\amdgpu-arch\AMDGPUArchByHIP.cpp
Z:\b\llvm-clang-x86_64-sie-win\llvm-project\clang\tools\amdgpu-arch\AMDGPUArchByHIP.cpp(104): error C2039: 'parse': is not a member of 'llvm::VersionTuple'
Z:\b\llvm-clang-x86_64-sie-win\llvm-project\llvm\include\llvm/Support/VersionTuple.h(29): note: see declaration of 'llvm::VersionTuple'
Z:\b\llvm-clang-x86_64-sie-win\llvm-project\clang\tools\amdgpu-arch\AMDGPUArchByHIP.cpp(104): error C3861: 'parse': identifier not found

@vvereschaka
Copy link
Contributor

@yxsamliu also the build failures on https://lab.llvm.org/buildbot/#/builders/193/builds/6129

FAILED: tools/clang/tools/amdgpu-arch/CMakeFiles/amdgpu-arch.dir/AMDGPUArchByHIP.cpp.obj 
ccache C:\PROGRA~1\MICROS~2\2022\COMMUN~1\VC\Tools\MSVC\1439~1.335\bin\Hostx64\x64\cl.exe  /nologo /TP -DCLANG_BUILD_STATIC -DGTEST_HAS_RTTI=0 -DLLVM_BUILD_STATIC -DUNICODE -D_CRT_NONSTDC_NO_DEPRECATE -D_CRT_NONSTDC_NO_WARNINGS -D_CRT_SECURE_NO_DEPRECATE -D_CRT_SECURE_NO_WARNINGS -D_GLIBCXX_ASSERTIONS -D_HAS_EXCEPTIONS=0 -D_SCL_SECURE_NO_DEPRECATE -D_SCL_SECURE_NO_WARNINGS -D_UNICODE -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS -IC:\buildbot\as-builder-2\x-aarch64\build\tools\clang\tools\amdgpu-arch -IC:\buildbot\as-builder-2\x-aarch64\llvm-project\clang\tools\amdgpu-arch -IC:\buildbot\as-builder-2\x-aarch64\llvm-project\clang\include -IC:\buildbot\as-builder-2\x-aarch64\build\tools\clang\include -IC:\buildbot\as-builder-2\x-aarch64\build\include -IC:\buildbot\as-builder-2\x-aarch64\llvm-project\llvm\include -external:IC:\buildbot\fs\zlib-win32\include -external:W0 -D__OPTIMIZE__ /Zc:inline /Zc:preprocessor /Zc:__cplusplus /Oi /bigobj /permissive- /W4 -wd4141 -wd4146 -wd4244 -wd4267 -wd4291 -wd4351 -wd4456 -wd4457 -wd4458 -wd4459 -wd4503 -wd4624 -wd4722 -wd4100 -wd4127 -wd4512 -wd4505 -wd4610 -wd4510 -wd4702 -wd4245 -wd4706 -wd4310 -wd4701 -wd4703 -wd4389 -wd4611 -wd4805 -wd4204 -wd4577 -wd4091 -wd4592 -wd4319 -wd4709 -wd5105 -wd4324 -wd4251 -wd4275 -w14062 -we4238 /Gw /O2 /Ob2  -MD  /EHs-c- /GR- -UNDEBUG -std:c++17 /showIncludes /Fotools\clang\tools\amdgpu-arch\CMakeFiles\amdgpu-arch.dir\AMDGPUArchByHIP.cpp.obj /Fdtools\clang\tools\amdgpu-arch\CMakeFiles\amdgpu-arch.dir\ /FS -c C:\buildbot\as-builder-2\x-aarch64\llvm-project\clang\tools\amdgpu-arch\AMDGPUArchByHIP.cpp
C:\buildbot\as-builder-2\x-aarch64\llvm-project\clang\tools\amdgpu-arch\AMDGPUArchByHIP.cpp(104): error C2039: 'parse': is not a member of 'llvm::VersionTuple'
C:\buildbot\as-builder-2\x-aarch64\llvm-project\llvm\include\llvm/Support/VersionTuple.h(29): note: see declaration of 'llvm::VersionTuple'
C:\buildbot\as-builder-2\x-aarch64\llvm-project\clang\tools\amdgpu-arch\AMDGPUArchByHIP.cpp(104): error C3861: 'parse': identifier not found

@yxsamliu
Copy link
Collaborator Author

yxsamliu commented Mar 7, 2025 via email

yxsamliu added a commit that referenced this pull request Mar 7, 2025
This reverts commit 6fa1bfa.

Revert it due to breaking buildbot:

Z:\b\llvm-clang-x86_64-sie-win\llvm-project\clang\tools\amdgpu-arch\AMDGPUArchByHIP.cpp(104):
 error C2039: 'parse': is not a member of 'llvm::VersionTuple'

https://lab.llvm.org/buildbot/#/builders/46/builds/13184
jph-13 pushed a commit to jph-13/llvm-project that referenced this pull request Mar 21, 2025
Recently HIP runtime changed dll name to amdhip64_n.dll on Windows,
where n is ROCm major version number.

Fix amdgpu-arch to search for amdhip64_n.dll on Windows.
jph-13 pushed a commit to jph-13/llvm-project that referenced this pull request Mar 21, 2025
This reverts commit 6fa1bfa.

Revert it due to breaking buildbot:

Z:\b\llvm-clang-x86_64-sie-win\llvm-project\clang\tools\amdgpu-arch\AMDGPUArchByHIP.cpp(104):
 error C2039: 'parse': is not a member of 'llvm::VersionTuple'

https://lab.llvm.org/buildbot/#/builders/46/builds/13184
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backend:AMDGPU clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants