[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`.
This commit is contained in:
Joseph Huber
2025-04-08 07:27:12 -05:00
committed by GitHub
parent db7fb704f6
commit 79cb6f05da
9 changed files with 97 additions and 108 deletions

View File

@@ -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)

View File

@@ -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();
}

View File

@@ -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)

View File

@@ -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)

View File

@@ -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)

View File

@@ -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());

View File

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