[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 12:39:02 PDT 2025
https://github.com/jhuber6 created https://github.com/llvm/llvm-project/pull/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`.
>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] [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;
+}
More information about the cfe-commits
mailing list