Author: Joseph Huber
Date: 2025-04-08T07:27:12-05:00
New Revision: 79cb6f05da37520949c006e26c5cef1826090d9d

URL: 
https://github.com/llvm/llvm-project/commit/79cb6f05da37520949c006e26c5cef1826090d9d
DIFF: 
https://github.com/llvm/llvm-project/commit/79cb6f05da37520949c006e26c5cef1826090d9d.diff

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

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`.

Added: 
    clang/tools/offload-arch/AMDGPUArchByHIP.cpp
    clang/tools/offload-arch/AMDGPUArchByKFD.cpp
    clang/tools/offload-arch/CMakeLists.txt
    clang/tools/offload-arch/NVPTXArch.cpp
    clang/tools/offload-arch/OffloadArch.cpp

Modified: 
    clang/tools/CMakeLists.txt

Removed: 
    clang/tools/amdgpu-arch/AMDGPUArch.cpp
    clang/tools/amdgpu-arch/AMDGPUArchByHIP.cpp
    clang/tools/amdgpu-arch/AMDGPUArchByKFD.cpp
    clang/tools/amdgpu-arch/CMakeLists.txt
    clang/tools/nvptx-arch/CMakeLists.txt
    clang/tools/nvptx-arch/NVPTXArch.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..74be40214a0ec
--- /dev/null
+++ b/clang/tools/offload-arch/OffloadArch.cpp
@@ -0,0 +1,87 @@
+//===- OffloadArch.cpp - list available GPUs 
------------------------------===//
+//
+// 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("offload-arch options");
+
+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';
+}
+
+int printGPUsByKFD();
+int printGPUsByHIP();
+int printGPUsByCUDA();
+
+static int printAMD() {
+#ifndef _WIN32
+  if (!printGPUsByKFD())
+    return 0;
+#endif
+
+  return printGPUsByHIP();
+}
+
+static 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 = 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)
+    NVIDIAResult = printNVIDIA();
+
+  int AMDResult = 0;
+  if (!NVIDIAOnly)
+    AMDResult = printAMD();
+
+  // We only failed if all cases returned an error.
+  return AMDResult && NVIDIAResult;
+}


        
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to