Skip to content

Commit 79cb6f0

Browse files
authored
[Clang] Unify 'nvptx-arch' and 'amdgpu-arch' into 'offload-arch' (llvm#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`.
1 parent db7fb70 commit 79cb6f0

File tree

9 files changed

+97
-108
lines changed

9 files changed

+97
-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: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
//===- OffloadArch.cpp - list available GPUs ------------------------------===//
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("offload-arch options");
19+
20+
enum VendorName {
21+
all,
22+
amdgpu,
23+
nvptx,
24+
};
25+
26+
static cl::opt<VendorName>
27+
Only("only", cl::desc("Restrict to vendor:"), cl::cat(OffloadArchCategory),
28+
cl::init(all),
29+
cl::values(clEnumVal(all, "Print all GPUs (default)"),
30+
clEnumVal(amdgpu, "Only print AMD GPUs"),
31+
clEnumVal(nvptx, "Only print NVIDIA GPUs")));
32+
33+
cl::opt<bool> Verbose("verbose", cl::desc("Enable verbose output"),
34+
cl::init(false), cl::cat(OffloadArchCategory));
35+
36+
static void PrintVersion(raw_ostream &OS) {
37+
OS << clang::getClangToolFullVersion("offload-arch") << '\n';
38+
}
39+
40+
int printGPUsByKFD();
41+
int printGPUsByHIP();
42+
int printGPUsByCUDA();
43+
44+
static int printAMD() {
45+
#ifndef _WIN32
46+
if (!printGPUsByKFD())
47+
return 0;
48+
#endif
49+
50+
return printGPUsByHIP();
51+
}
52+
53+
static int printNVIDIA() { return printGPUsByCUDA(); }
54+
55+
int main(int argc, char *argv[]) {
56+
cl::HideUnrelatedOptions(OffloadArchCategory);
57+
58+
cl::SetVersionPrinter(PrintVersion);
59+
cl::ParseCommandLineOptions(
60+
argc, argv,
61+
"A tool to detect the presence of offloading devices on the system. \n\n"
62+
"The tool will output each detected GPU architecture separated by a\n"
63+
"newline character. If multiple GPUs of the same architecture are found\n"
64+
"a string will be printed for each\n");
65+
66+
if (Help) {
67+
cl::PrintHelpMessage();
68+
return 0;
69+
}
70+
71+
// If this was invoked from the legacy symlinks provide the same behavior.
72+
bool AMDGPUOnly = Only == VendorName::amdgpu ||
73+
sys::path::stem(argv[0]).starts_with("amdgpu-arch");
74+
bool NVIDIAOnly = Only == VendorName::nvptx ||
75+
sys::path::stem(argv[0]).starts_with("nvptx-arch");
76+
77+
int NVIDIAResult = 0;
78+
if (!AMDGPUOnly)
79+
NVIDIAResult = printNVIDIA();
80+
81+
int AMDResult = 0;
82+
if (!NVIDIAOnly)
83+
AMDResult = printAMD();
84+
85+
// We only failed if all cases returned an error.
86+
return AMDResult && NVIDIAResult;
87+
}

0 commit comments

Comments
 (0)