[clang] [Clang] Unify 'nvptx-arch' and 'amdgpu-arch' into 'offload-arch' (PR #134713)

Joseph Huber via cfe-commits cfe-commits at lists.llvm.org
Mon Apr 7 18:51:25 PDT 2025


https://github.com/jhuber6 updated https://github.com/llvm/llvm-project/pull/134713

>From e44db82f3abe7c1d23c2b49094c92a890127ffc7 Mon Sep 17 00:00:00 2001
From: Joseph Huber <huberjn at outlook.com>
Date: Mon, 7 Apr 2025 14:37:22 -0500
Subject: [PATCH 1/6] [Clang] Unify 'nvptx-arch' and 'amdgpu-arch' into
 'offload-arch'

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`.
---
 clang/tools/CMakeLists.txt                    |  3 +-
 clang/tools/amdgpu-arch/AMDGPUArch.cpp        | 56 -------------
 clang/tools/amdgpu-arch/CMakeLists.txt        | 13 ----
 clang/tools/nvptx-arch/CMakeLists.txt         | 12 ---
 .../AMDGPUArchByHIP.cpp                       |  0
 .../AMDGPUArchByKFD.cpp                       |  0
 clang/tools/offload-arch/CMakeLists.txt       |  8 ++
 .../NVPTXArch.cpp                             | 26 +------
 clang/tools/offload-arch/OffloadArch.cpp      | 78 +++++++++++++++++++
 9 files changed, 88 insertions(+), 108 deletions(-)
 delete mode 100644 clang/tools/amdgpu-arch/AMDGPUArch.cpp
 delete mode 100644 clang/tools/amdgpu-arch/CMakeLists.txt
 delete mode 100644 clang/tools/nvptx-arch/CMakeLists.txt
 rename clang/tools/{amdgpu-arch => offload-arch}/AMDGPUArchByHIP.cpp (100%)
 rename clang/tools/{amdgpu-arch => offload-arch}/AMDGPUArchByKFD.cpp (100%)
 create mode 100644 clang/tools/offload-arch/CMakeLists.txt
 rename clang/tools/{nvptx-arch => offload-arch}/NVPTXArch.cpp (80%)
 create mode 100644 clang/tools/offload-arch/OffloadArch.cpp

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;
+}

>From 6759040365587830ff59f574ea36487c202f0908 Mon Sep 17 00:00:00 2001
From: Joseph Huber <huberjn at outlook.com>
Date: Mon, 7 Apr 2025 15:30:19 -0500
Subject: [PATCH 2/6] stem

---
 clang/tools/offload-arch/OffloadArch.cpp | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/clang/tools/offload-arch/OffloadArch.cpp b/clang/tools/offload-arch/OffloadArch.cpp
index ee69e60473c86..fc44ac1cce0fc 100644
--- a/clang/tools/offload-arch/OffloadArch.cpp
+++ b/clang/tools/offload-arch/OffloadArch.cpp
@@ -62,8 +62,8 @@ int main(int argc, char *argv[]) {
   }
 
   // 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";
+  bool AMDGPUOnly = AMDGPU || sys::path::stem(argv[0]) == "amdgpu-arch";
+  bool NVIDIAOnly = NVPTX || sys::path::stem(argv[0]) == "nvptx-arch";
 
   int NVIDIAResult = 0;
   if (!AMDGPUOnly)

>From 18960193ee4098c860cab90659bc86865454dc58 Mon Sep 17 00:00:00 2001
From: Joseph Huber <huberjn at outlook.com>
Date: Mon, 7 Apr 2025 19:44:37 -0500
Subject: [PATCH 3/6] Comments

---
 clang/tools/offload-arch/OffloadArch.cpp | 17 +++++++++--------
 1 file changed, 9 insertions(+), 8 deletions(-)

diff --git a/clang/tools/offload-arch/OffloadArch.cpp b/clang/tools/offload-arch/OffloadArch.cpp
index fc44ac1cce0fc..20a69bee88d05 100644
--- a/clang/tools/offload-arch/OffloadArch.cpp
+++ b/clang/tools/offload-arch/OffloadArch.cpp
@@ -17,14 +17,14 @@ 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));
+static 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));
+static 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 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';
@@ -62,8 +62,9 @@ int main(int argc, char *argv[]) {
   }
 
   // If this was invoked from the legacy symlinks provide the same behavior.
-  bool AMDGPUOnly = AMDGPU || sys::path::stem(argv[0]) == "amdgpu-arch";
-  bool NVIDIAOnly = NVPTX || sys::path::stem(argv[0]) == "nvptx-arch";
+  bool AMDGPUOnly =
+      AMDGPU || sys::path::stem(argv[0]).starts_with("amdgpu-arch");
+  bool NVIDIAOnly = NVPTX || sys::path::stem(argv[0]).starts_with("nvptx-arch");
 
   int NVIDIAResult = 0;
   if (!AMDGPUOnly)

>From b29902af6addec776123ef01157105db62b2ed12 Mon Sep 17 00:00:00 2001
From: Joseph Huber <huberjn at outlook.com>
Date: Mon, 7 Apr 2025 19:59:02 -0500
Subject: [PATCH 4/6] Enums

---
 clang/tools/offload-arch/OffloadArch.cpp | 30 +++++++++++++++---------
 1 file changed, 19 insertions(+), 11 deletions(-)

diff --git a/clang/tools/offload-arch/OffloadArch.cpp b/clang/tools/offload-arch/OffloadArch.cpp
index 20a69bee88d05..6e241a8ebacea 100644
--- a/clang/tools/offload-arch/OffloadArch.cpp
+++ b/clang/tools/offload-arch/OffloadArch.cpp
@@ -17,14 +17,21 @@ 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");
 
-static cl::opt<bool> Verbose("verbose", cl::desc("Enable verbose output"),
-                             cl::init(false), cl::cat(OffloadArchCategory));
-
-static cl::opt<bool> AMDGPU("amdgpu-only", cl::desc("Print only AMD GPUs"),
-                            cl::init(false), cl::cat(OffloadArchCategory));
-
-static cl::opt<bool> NVPTX("nvptx-only", cl::desc("Print only NVIDIA GPUs"),
-                           cl::init(false), cl::cat(OffloadArchCategory));
+enum VendorName {
+  all,
+  amdgpu,
+  nvptx,
+};
+
+static cl::opt<VendorName>
+    Only("only", cl::desc("Restrict to vendor:"), cl::cat(OffloadArchCategory),
+         cl::init(all),
+         cl::values(clEnumVal(all, "Print all GPUs (default)"),
+                    clEnumVal(amdgpu, "Only print AMD GPUs"),
+                    clEnumVal(nvptx, "Only print NVIDIA GPUs")));
+
+cl::opt<bool> Verbose("verbose", cl::desc("Enable verbose output"),
+                      cl::init(false), cl::cat(OffloadArchCategory));
 
 static void PrintVersion(raw_ostream &OS) {
   OS << clang::getClangToolFullVersion("offload-arch") << '\n';
@@ -62,9 +69,10 @@ int main(int argc, char *argv[]) {
   }
 
   // If this was invoked from the legacy symlinks provide the same behavior.
-  bool AMDGPUOnly =
-      AMDGPU || sys::path::stem(argv[0]).starts_with("amdgpu-arch");
-  bool NVIDIAOnly = NVPTX || sys::path::stem(argv[0]).starts_with("nvptx-arch");
+  bool AMDGPUOnly = Only == VendorName::amdgpu ||
+                    sys::path::stem(argv[0]).starts_with("amdgpu-arch");
+  bool NVIDIAOnly = Only == VendorName::nvptx ||
+                    sys::path::stem(argv[0]).starts_with("nvptx-arch");
 
   int NVIDIAResult = 0;
   if (!AMDGPUOnly)

>From 8f8ab5d9a53b2abd6f3bcebc8b0867f9d0a03c5b Mon Sep 17 00:00:00 2001
From: Joseph Huber <huberjn at outlook.com>
Date: Mon, 7 Apr 2025 20:01:42 -0500
Subject: [PATCH 5/6] Static 2

---
 clang/tools/offload-arch/OffloadArch.cpp | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/clang/tools/offload-arch/OffloadArch.cpp b/clang/tools/offload-arch/OffloadArch.cpp
index 6e241a8ebacea..409feaf120e69 100644
--- a/clang/tools/offload-arch/OffloadArch.cpp
+++ b/clang/tools/offload-arch/OffloadArch.cpp
@@ -41,7 +41,7 @@ int printGPUsByKFD();
 int printGPUsByHIP();
 int printGPUsByCUDA();
 
-int printAMD() {
+static int printAMD() {
 #ifndef _WIN32
   if (!printGPUsByKFD())
     return 0;
@@ -50,7 +50,7 @@ int printAMD() {
   return printGPUsByHIP();
 }
 
-int printNVIDIA() { return printGPUsByCUDA(); }
+static int printNVIDIA() { return printGPUsByCUDA(); }
 
 int main(int argc, char *argv[]) {
   cl::HideUnrelatedOptions(OffloadArchCategory);

>From a58f0ff8bd4866b99f75c3a74f99a025ed4aef66 Mon Sep 17 00:00:00 2001
From: Joseph Huber <huberjn at outlook.com>
Date: Mon, 7 Apr 2025 20:51:14 -0500
Subject: [PATCH 6/6] fix

---
 clang/tools/offload-arch/OffloadArch.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/clang/tools/offload-arch/OffloadArch.cpp b/clang/tools/offload-arch/OffloadArch.cpp
index 409feaf120e69..c8f18cdf3d223 100644
--- a/clang/tools/offload-arch/OffloadArch.cpp
+++ b/clang/tools/offload-arch/OffloadArch.cpp
@@ -15,7 +15,7 @@ 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");
+static cl::OptionCategory OffloadArchCategory("offload-arch options");
 
 enum VendorName {
   all,



More information about the cfe-commits mailing list