Skip to content

[Clang] Unify 'nvptx-arch' and 'amdgpu-arch' into 'offload-arch' #134713

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 7 commits into from
Apr 8, 2025

Conversation

jhuber6
Copy link
Contributor

@jhuber6 jhuber6 commented Apr 7, 2025

Summary:
These two tools do the same thing, we should unify them into a single
tool. We create symlinks for backward compatiblity and provide a way to
get the old vendor specific behavior with --amdgpu-only and
--nvptx-only.

Summary:
These two tools do the same thing, we should unify them into a single
tool. We create symlinks for backward compatiblity and provide a way to
get the old vendor specific behavior with `--amdgpu-only` and
`--nvptx-only`.
@llvmbot llvmbot added clang Clang issues not falling into any other category backend:AMDGPU labels Apr 7, 2025
@llvmbot
Copy link
Member

llvmbot commented Apr 7, 2025

@llvm/pr-subscribers-clang

@llvm/pr-subscribers-backend-amdgpu

Author: Joseph Huber (jhuber6)

Changes

Summary:
These two tools do the same thing, we should unify them into a single
tool. We create symlinks for backward compatiblity and provide a way to
get the old vendor specific behavior with --amdgpu-only and
--nvptx-only.


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

9 Files Affected:

  • (modified) clang/tools/CMakeLists.txt (+1-2)
  • (removed) clang/tools/amdgpu-arch/AMDGPUArch.cpp (-56)
  • (removed) clang/tools/amdgpu-arch/CMakeLists.txt (-13)
  • (removed) clang/tools/nvptx-arch/CMakeLists.txt (-12)
  • (renamed) clang/tools/offload-arch/AMDGPUArchByHIP.cpp ()
  • (renamed) clang/tools/offload-arch/AMDGPUArchByKFD.cpp ()
  • (added) clang/tools/offload-arch/CMakeLists.txt (+8)
  • (renamed) clang/tools/offload-arch/NVPTXArch.cpp (+1-25)
  • (added) clang/tools/offload-arch/OffloadArch.cpp (+78)
diff --git a/clang/tools/CMakeLists.txt b/clang/tools/CMakeLists.txt
index e3557c1328d53..9634eb12080c8 100644
--- a/clang/tools/CMakeLists.txt
+++ b/clang/tools/CMakeLists.txt
@@ -50,5 +50,4 @@ add_llvm_external_project(clang-tools-extra extra)
 # libclang may require clang-tidy in clang-tools-extra.
 add_clang_subdirectory(libclang)
 
-add_clang_subdirectory(amdgpu-arch)
-add_clang_subdirectory(nvptx-arch)
+add_clang_subdirectory(offload-arch)
diff --git a/clang/tools/amdgpu-arch/AMDGPUArch.cpp b/clang/tools/amdgpu-arch/AMDGPUArch.cpp
deleted file mode 100644
index 86f3e31f47bbc..0000000000000
--- a/clang/tools/amdgpu-arch/AMDGPUArch.cpp
+++ /dev/null
@@ -1,56 +0,0 @@
-//===- AMDGPUArch.cpp - list AMDGPU installed ----------*- C++ -*---------===//
-//
-// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
-// See https://llvm.org/LICENSE.txt for license information.
-// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
-//
-//===----------------------------------------------------------------------===//
-//
-// This file implements a tool for detecting name of AMDGPU installed in system.
-// This tool is used by AMDGPU OpenMP and HIP driver.
-//
-//===----------------------------------------------------------------------===//
-
-#include "clang/Basic/Version.h"
-#include "llvm/Support/CommandLine.h"
-
-using namespace llvm;
-
-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';
-}
-
-int printGPUsByKFD();
-int printGPUsByHIP();
-
-int main(int argc, char *argv[]) {
-  cl::HideUnrelatedOptions(AMDGPUArchCategory);
-
-  cl::SetVersionPrinter(PrintVersion);
-  cl::ParseCommandLineOptions(
-      argc, argv,
-      "A tool to detect the presence of AMDGPU devices on the system. \n\n"
-      "The tool will output each detected GPU architecture separated by a\n"
-      "newline character. If multiple GPUs of the same architecture are found\n"
-      "a string will be printed for each\n");
-
-  if (Help) {
-    cl::PrintHelpMessage();
-    return 0;
-  }
-
-#ifndef _WIN32
-  if (!printGPUsByKFD())
-    return 0;
-#endif
-
-  return printGPUsByHIP();
-}
diff --git a/clang/tools/amdgpu-arch/CMakeLists.txt b/clang/tools/amdgpu-arch/CMakeLists.txt
deleted file mode 100644
index c4c8de614565a..0000000000000
--- a/clang/tools/amdgpu-arch/CMakeLists.txt
+++ /dev/null
@@ -1,13 +0,0 @@
-# //===----------------------------------------------------------------------===//
-# //
-# // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
-# // See https://llvm.org/LICENSE.txt for details.
-# // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
-# //
-# //===----------------------------------------------------------------------===//
-
-set(LLVM_LINK_COMPONENTS Support)
-
-add_clang_tool(amdgpu-arch AMDGPUArch.cpp AMDGPUArchByKFD.cpp AMDGPUArchByHIP.cpp)
-
-target_link_libraries(amdgpu-arch PRIVATE clangBasic)
diff --git a/clang/tools/nvptx-arch/CMakeLists.txt b/clang/tools/nvptx-arch/CMakeLists.txt
deleted file mode 100644
index 8f756be2c86d0..0000000000000
--- a/clang/tools/nvptx-arch/CMakeLists.txt
+++ /dev/null
@@ -1,12 +0,0 @@
-# //===--------------------------------------------------------------------===//
-# //
-# // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
-# // See https://llvm.org/LICENSE.txt for details.
-# // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
-# //
-# //===--------------------------------------------------------------------===//
-
-set(LLVM_LINK_COMPONENTS Support)
-add_clang_tool(nvptx-arch NVPTXArch.cpp)
-
-target_link_libraries(nvptx-arch PRIVATE clangBasic)
diff --git a/clang/tools/amdgpu-arch/AMDGPUArchByHIP.cpp b/clang/tools/offload-arch/AMDGPUArchByHIP.cpp
similarity index 100%
rename from clang/tools/amdgpu-arch/AMDGPUArchByHIP.cpp
rename to clang/tools/offload-arch/AMDGPUArchByHIP.cpp
diff --git a/clang/tools/amdgpu-arch/AMDGPUArchByKFD.cpp b/clang/tools/offload-arch/AMDGPUArchByKFD.cpp
similarity index 100%
rename from clang/tools/amdgpu-arch/AMDGPUArchByKFD.cpp
rename to clang/tools/offload-arch/AMDGPUArchByKFD.cpp
diff --git a/clang/tools/offload-arch/CMakeLists.txt b/clang/tools/offload-arch/CMakeLists.txt
new file mode 100644
index 0000000000000..cb50b9c1d6dde
--- /dev/null
+++ b/clang/tools/offload-arch/CMakeLists.txt
@@ -0,0 +1,8 @@
+set(LLVM_LINK_COMPONENTS Support)
+
+add_clang_tool(offload-arch OffloadArch.cpp NVPTXArch.cpp AMDGPUArchByKFD.cpp AMDGPUArchByHIP.cpp)
+
+add_clang_symlink(amdgpu-arch offload-arch)
+add_clang_symlink(nvptx-arch offload-arch)
+
+target_link_libraries(offload-arch PRIVATE clangBasic)
diff --git a/clang/tools/nvptx-arch/NVPTXArch.cpp b/clang/tools/offload-arch/NVPTXArch.cpp
similarity index 80%
rename from clang/tools/nvptx-arch/NVPTXArch.cpp
rename to clang/tools/offload-arch/NVPTXArch.cpp
index 71a48657576e4..c7b7fcdf80500 100644
--- a/clang/tools/nvptx-arch/NVPTXArch.cpp
+++ b/clang/tools/offload-arch/NVPTXArch.cpp
@@ -21,15 +21,6 @@
 
 using namespace llvm;
 
-static cl::opt<bool> Help("h", cl::desc("Alias for -help"), cl::Hidden);
-
-static void PrintVersion(raw_ostream &OS) {
-  OS << clang::getClangToolFullVersion("nvptx-arch") << '\n';
-}
-// Mark all our options with this category, everything else (except for -version
-// and -help) will be hidden.
-static cl::OptionCategory NVPTXArchCategory("nvptx-arch options");
-
 typedef enum cudaError_enum {
   CUDA_SUCCESS = 0,
   CUDA_ERROR_NO_DEVICE = 100,
@@ -84,22 +75,7 @@ static int handleError(CUresult Err) {
   return 1;
 }
 
-int main(int argc, char *argv[]) {
-  cl::HideUnrelatedOptions(NVPTXArchCategory);
-
-  cl::SetVersionPrinter(PrintVersion);
-  cl::ParseCommandLineOptions(
-      argc, argv,
-      "A tool to detect the presence of NVIDIA devices on the system. \n\n"
-      "The tool will output each detected GPU architecture separated by a\n"
-      "newline character. If multiple GPUs of the same architecture are found\n"
-      "a string will be printed for each\n");
-
-  if (Help) {
-    cl::PrintHelpMessage();
-    return 0;
-  }
-
+int printGPUsByCUDA() {
   // Attempt to load the NVPTX driver runtime.
   if (llvm::Error Err = loadCUDA()) {
     logAllUnhandledErrors(std::move(Err), llvm::errs());
diff --git a/clang/tools/offload-arch/OffloadArch.cpp b/clang/tools/offload-arch/OffloadArch.cpp
new file mode 100644
index 0000000000000..ee69e60473c86
--- /dev/null
+++ b/clang/tools/offload-arch/OffloadArch.cpp
@@ -0,0 +1,78 @@
+//===- OffloadArch.cpp - list available GPUs ------------*- C++ -*---------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "clang/Basic/Version.h"
+#include "llvm/Support/CommandLine.h"
+#include "llvm/Support/Path.h"
+
+using namespace llvm;
+
+static cl::opt<bool> Help("h", cl::desc("Alias for -help"), cl::Hidden);
+
+// Mark all our options with this category.
+static cl::OptionCategory OffloadArchCategory("amdgpu-arch options");
+
+cl::opt<bool> Verbose("verbose", cl::desc("Enable verbose output"),
+                      cl::init(false), cl::cat(OffloadArchCategory));
+
+cl::opt<bool> AMDGPU("amdgpu-only", cl::desc("Print only AMD GPUs"),
+                     cl::init(false), cl::cat(OffloadArchCategory));
+
+cl::opt<bool> NVPTX("nvptx-only", cl::desc("Print only NVIDIA GPUs"),
+                    cl::init(false), cl::cat(OffloadArchCategory));
+
+static void PrintVersion(raw_ostream &OS) {
+  OS << clang::getClangToolFullVersion("offload-arch") << '\n';
+}
+
+int printGPUsByKFD();
+int printGPUsByHIP();
+int printGPUsByCUDA();
+
+int printAMD() {
+#ifndef _WIN32
+  if (!printGPUsByKFD())
+    return 0;
+#endif
+
+  return printGPUsByHIP();
+}
+
+int printNVIDIA() { return printGPUsByCUDA(); }
+
+int main(int argc, char *argv[]) {
+  cl::HideUnrelatedOptions(OffloadArchCategory);
+
+  cl::SetVersionPrinter(PrintVersion);
+  cl::ParseCommandLineOptions(
+      argc, argv,
+      "A tool to detect the presence of offloading devices on the system. \n\n"
+      "The tool will output each detected GPU architecture separated by a\n"
+      "newline character. If multiple GPUs of the same architecture are found\n"
+      "a string will be printed for each\n");
+
+  if (Help) {
+    cl::PrintHelpMessage();
+    return 0;
+  }
+
+  // If this was invoked from the legacy symlinks provide the same behavior.
+  bool AMDGPUOnly = AMDGPU || sys::path::filename(argv[0]) == "amdgpu-arch";
+  bool NVIDIAOnly = NVPTX || sys::path::filename(argv[0]) == "nvptx-arch";
+
+  int NVIDIAResult = 0;
+  if (!AMDGPUOnly)
+    NVIDIAResult = printNVIDIA();
+
+  int AMDResult = 0;
+  if (!NVIDIAOnly)
+    AMDResult = printAMD();
+
+  // We only failed if all cases returned an error.
+  return AMDResult && NVIDIAResult;
+}

@@ -0,0 +1,87 @@
//===- OffloadArch.cpp - list available GPUs ------------*- C++ -*---------===//
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

//===----------------------------------------------------------------------===// per #118553

@jhuber6 jhuber6 merged commit 79cb6f0 into llvm:main Apr 8, 2025
11 checks passed
jdenny-ornl added a commit to jdenny-ornl/llvm-project that referenced this pull request Apr 9, 2025
PR llvm#134713, which landed as 79cb6f0, causes this on my test
systems:

```
-- Building AMDGPU plugin for dlopened libhsa
-- Not generating AMDGPU tests, no supported devices detected. Use 'LIBOMPTARGET_FORCE_AMDGPU_TESTS' to override.
-- Building CUDA plugin for dlopened libcuda
-- Not generating NVIDIA tests, no supported devices detected. Use 'LIBOMPTARGET_FORCE_NVIDIA_TESTS' to override.
```

The problem is it cannot locate amdgpu-arch and nvptx-arch.  This
patch enables it to.

I suspect there is more cleanup to do here.  amdgpu-arch and
nvptx-arch do not appear to exist as cmake targets anymore, but there
is still cmake code here that looks for those targets.
jdenny-ornl added a commit that referenced this pull request Apr 9, 2025
PR #134713, which landed as 79cb6f0, causes this on my test
systems:

```
-- Building AMDGPU plugin for dlopened libhsa
-- Not generating AMDGPU tests, no supported devices detected. Use 'LIBOMPTARGET_FORCE_AMDGPU_TESTS' to override.
-- Building CUDA plugin for dlopened libcuda
-- Not generating NVIDIA tests, no supported devices detected. Use 'LIBOMPTARGET_FORCE_NVIDIA_TESTS' to override.
```

The problem is it cannot locate amdgpu-arch and nvptx-arch. This patch
enables it to.

I suspect there is more cleanup to do here. amdgpu-arch and nvptx-arch
do not appear to exist as cmake targets anymore, but there is still
cmake code here that looks for those targets.
AllinLeeYL pushed a commit to AllinLeeYL/llvm-project that referenced this pull request Apr 10, 2025
PR llvm#134713, which landed as 79cb6f0, causes this on my test
systems:

```
-- Building AMDGPU plugin for dlopened libhsa
-- Not generating AMDGPU tests, no supported devices detected. Use 'LIBOMPTARGET_FORCE_AMDGPU_TESTS' to override.
-- Building CUDA plugin for dlopened libcuda
-- Not generating NVIDIA tests, no supported devices detected. Use 'LIBOMPTARGET_FORCE_NVIDIA_TESTS' to override.
```

The problem is it cannot locate amdgpu-arch and nvptx-arch. This patch
enables it to.

I suspect there is more cleanup to do here. amdgpu-arch and nvptx-arch
do not appear to exist as cmake targets anymore, but there is still
cmake code here that looks for those targets.
var-const pushed a commit to ldionne/llvm-project that referenced this pull request Apr 17, 2025
PR llvm#134713, which landed as 79cb6f0, causes this on my test
systems:

```
-- Building AMDGPU plugin for dlopened libhsa
-- Not generating AMDGPU tests, no supported devices detected. Use 'LIBOMPTARGET_FORCE_AMDGPU_TESTS' to override.
-- Building CUDA plugin for dlopened libcuda
-- Not generating NVIDIA tests, no supported devices detected. Use 'LIBOMPTARGET_FORCE_NVIDIA_TESTS' to override.
```

The problem is it cannot locate amdgpu-arch and nvptx-arch. This patch
enables it to.

I suspect there is more cleanup to do here. amdgpu-arch and nvptx-arch
do not appear to exist as cmake targets anymore, but there is still
cmake code here that looks for those targets.
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.

6 participants