Skip to content

Commit e44db82

Browse files
committed
[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`.
1 parent e288577 commit e44db82

File tree

9 files changed

+88
-108
lines changed

9 files changed

+88
-108
lines changed

clang/tools/CMakeLists.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,5 +50,4 @@ add_llvm_external_project(clang-tools-extra extra)
5050
# libclang may require clang-tidy in clang-tools-extra.
5151
add_clang_subdirectory(libclang)
5252

53-
add_clang_subdirectory(amdgpu-arch)
54-
add_clang_subdirectory(nvptx-arch)
53+
add_clang_subdirectory(offload-arch)

clang/tools/amdgpu-arch/AMDGPUArch.cpp

Lines changed: 0 additions & 56 deletions
This file was deleted.

clang/tools/amdgpu-arch/CMakeLists.txt

Lines changed: 0 additions & 13 deletions
This file was deleted.

clang/tools/nvptx-arch/CMakeLists.txt

Lines changed: 0 additions & 12 deletions
This file was deleted.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
set(LLVM_LINK_COMPONENTS Support)
2+
3+
add_clang_tool(offload-arch OffloadArch.cpp NVPTXArch.cpp AMDGPUArchByKFD.cpp AMDGPUArchByHIP.cpp)
4+
5+
add_clang_symlink(amdgpu-arch offload-arch)
6+
add_clang_symlink(nvptx-arch offload-arch)
7+
8+
target_link_libraries(offload-arch PRIVATE clangBasic)

clang/tools/nvptx-arch/NVPTXArch.cpp renamed to clang/tools/offload-arch/NVPTXArch.cpp

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,6 @@
2121

2222
using namespace llvm;
2323

24-
static cl::opt<bool> Help("h", cl::desc("Alias for -help"), cl::Hidden);
25-
26-
static void PrintVersion(raw_ostream &OS) {
27-
OS << clang::getClangToolFullVersion("nvptx-arch") << '\n';
28-
}
29-
// Mark all our options with this category, everything else (except for -version
30-
// and -help) will be hidden.
31-
static cl::OptionCategory NVPTXArchCategory("nvptx-arch options");
32-
3324
typedef enum cudaError_enum {
3425
CUDA_SUCCESS = 0,
3526
CUDA_ERROR_NO_DEVICE = 100,
@@ -84,22 +75,7 @@ static int handleError(CUresult Err) {
8475
return 1;
8576
}
8677

87-
int main(int argc, char *argv[]) {
88-
cl::HideUnrelatedOptions(NVPTXArchCategory);
89-
90-
cl::SetVersionPrinter(PrintVersion);
91-
cl::ParseCommandLineOptions(
92-
argc, argv,
93-
"A tool to detect the presence of NVIDIA devices on the system. \n\n"
94-
"The tool will output each detected GPU architecture separated by a\n"
95-
"newline character. If multiple GPUs of the same architecture are found\n"
96-
"a string will be printed for each\n");
97-
98-
if (Help) {
99-
cl::PrintHelpMessage();
100-
return 0;
101-
}
102-
78+
int printGPUsByCUDA() {
10379
// Attempt to load the NVPTX driver runtime.
10480
if (llvm::Error Err = loadCUDA()) {
10581
logAllUnhandledErrors(std::move(Err), llvm::errs());
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
//===- OffloadArch.cpp - list available GPUs ------------*- C++ -*---------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "clang/Basic/Version.h"
10+
#include "llvm/Support/CommandLine.h"
11+
#include "llvm/Support/Path.h"
12+
13+
using namespace llvm;
14+
15+
static cl::opt<bool> Help("h", cl::desc("Alias for -help"), cl::Hidden);
16+
17+
// Mark all our options with this category.
18+
static cl::OptionCategory OffloadArchCategory("amdgpu-arch options");
19+
20+
cl::opt<bool> Verbose("verbose", cl::desc("Enable verbose output"),
21+
cl::init(false), cl::cat(OffloadArchCategory));
22+
23+
cl::opt<bool> AMDGPU("amdgpu-only", cl::desc("Print only AMD GPUs"),
24+
cl::init(false), cl::cat(OffloadArchCategory));
25+
26+
cl::opt<bool> NVPTX("nvptx-only", cl::desc("Print only NVIDIA GPUs"),
27+
cl::init(false), cl::cat(OffloadArchCategory));
28+
29+
static void PrintVersion(raw_ostream &OS) {
30+
OS << clang::getClangToolFullVersion("offload-arch") << '\n';
31+
}
32+
33+
int printGPUsByKFD();
34+
int printGPUsByHIP();
35+
int printGPUsByCUDA();
36+
37+
int printAMD() {
38+
#ifndef _WIN32
39+
if (!printGPUsByKFD())
40+
return 0;
41+
#endif
42+
43+
return printGPUsByHIP();
44+
}
45+
46+
int printNVIDIA() { return printGPUsByCUDA(); }
47+
48+
int main(int argc, char *argv[]) {
49+
cl::HideUnrelatedOptions(OffloadArchCategory);
50+
51+
cl::SetVersionPrinter(PrintVersion);
52+
cl::ParseCommandLineOptions(
53+
argc, argv,
54+
"A tool to detect the presence of offloading devices on the system. \n\n"
55+
"The tool will output each detected GPU architecture separated by a\n"
56+
"newline character. If multiple GPUs of the same architecture are found\n"
57+
"a string will be printed for each\n");
58+
59+
if (Help) {
60+
cl::PrintHelpMessage();
61+
return 0;
62+
}
63+
64+
// If this was invoked from the legacy symlinks provide the same behavior.
65+
bool AMDGPUOnly = AMDGPU || sys::path::filename(argv[0]) == "amdgpu-arch";
66+
bool NVIDIAOnly = NVPTX || sys::path::filename(argv[0]) == "nvptx-arch";
67+
68+
int NVIDIAResult = 0;
69+
if (!AMDGPUOnly)
70+
NVIDIAResult = printNVIDIA();
71+
72+
int AMDResult = 0;
73+
if (!NVIDIAOnly)
74+
AMDResult = printAMD();
75+
76+
// We only failed if all cases returned an error.
77+
return AMDResult && NVIDIAResult;
78+
}

0 commit comments

Comments
 (0)