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
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions clang/tools/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
56 changes: 0 additions & 56 deletions clang/tools/amdgpu-arch/AMDGPUArch.cpp

This file was deleted.

13 changes: 0 additions & 13 deletions clang/tools/amdgpu-arch/CMakeLists.txt

This file was deleted.

12 changes: 0 additions & 12 deletions clang/tools/nvptx-arch/CMakeLists.txt

This file was deleted.

8 changes: 8 additions & 0 deletions clang/tools/offload-arch/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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)
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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());
Expand Down
78 changes: 78 additions & 0 deletions clang/tools/offload-arch/OffloadArch.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
//===- 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

//
// 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::stem(argv[0]) == "amdgpu-arch";
bool NVIDIAOnly = NVPTX || sys::path::stem(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;
}