Skip to content

Commit 129a1a2

Browse files
authored
[amdgpu-arch] Replace use of HSA with reading sysfs directly (#116651)
Summary: For Linux systems, we currently use the HSA library to determine the installed GPUs. However, this isn't really necessary and adds a dependency on the HSA runtime as well as a lot of overhead. Instead, this patch uses the `sysfs` interface exposed by `amdkfd` to do this directly.
1 parent 7dcefb3 commit 129a1a2

File tree

4 files changed

+81
-125
lines changed

4 files changed

+81
-125
lines changed

clang/tools/amdgpu-arch/AMDGPUArch.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ static void PrintVersion(raw_ostream &OS) {
2525
OS << clang::getClangToolFullVersion("amdgpu-arch") << '\n';
2626
}
2727

28-
int printGPUsByHSA();
28+
int printGPUsByKFD();
2929
int printGPUsByHIP();
3030

3131
int main(int argc, char *argv[]) {
@@ -45,7 +45,7 @@ int main(int argc, char *argv[]) {
4545
}
4646

4747
#ifndef _WIN32
48-
if (!printGPUsByHSA())
48+
if (!printGPUsByKFD())
4949
return 0;
5050
#endif
5151

clang/tools/amdgpu-arch/AMDGPUArchByHSA.cpp

Lines changed: 0 additions & 122 deletions
This file was deleted.
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
//===- AMDGPUArchByKFD.cpp - list AMDGPU installed ------*- 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+
// This file implements a tool for detecting name of AMD GPUs installed in
10+
// system using the Linux sysfs interface for the AMD KFD driver. This file does
11+
// not respect ROCR_VISIBLE_DEVICES like the ROCm environment would.
12+
//
13+
//===----------------------------------------------------------------------===//
14+
15+
#include "llvm/Support/FileSystem.h"
16+
#include "llvm/Support/LineIterator.h"
17+
#include "llvm/Support/MemoryBuffer.h"
18+
#include "llvm/Support/Path.h"
19+
#include <memory>
20+
#include <string>
21+
22+
using namespace llvm;
23+
24+
constexpr static const char *KFD_SYSFS_NODE_PATH =
25+
"/sys/devices/virtual/kfd/kfd/topology/nodes";
26+
27+
// See the ROCm implementation for how this is handled.
28+
// https://github.com/ROCm/ROCT-Thunk-Interface/blob/master/src/libhsakmt.h#L126
29+
constexpr static long getMajor(long Ver) { return (Ver / 10000) % 100; }
30+
constexpr static long getMinor(long Ver) { return (Ver / 100) % 100; }
31+
constexpr static long getStep(long Ver) { return Ver % 100; }
32+
33+
int printGPUsByKFD() {
34+
SmallVector<std::pair<long, long>> Devices;
35+
std::error_code EC;
36+
for (sys::fs::directory_iterator Begin(KFD_SYSFS_NODE_PATH, EC), End;
37+
Begin != End; Begin.increment(EC)) {
38+
if (EC)
39+
return 1;
40+
41+
long Node = 0;
42+
if (sys::path::stem(Begin->path()).consumeInteger(10, Node))
43+
return 1;
44+
45+
SmallString<0> Path(Begin->path());
46+
sys::path::append(Path, "properties");
47+
48+
ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOrErr =
49+
MemoryBuffer::getFileOrSTDIN(Path);
50+
if (std::error_code EC = BufferOrErr.getError())
51+
return 1;
52+
53+
long GFXVersion = 0;
54+
for (line_iterator Lines(**BufferOrErr, false); !Lines.is_at_end();
55+
++Lines) {
56+
StringRef Line(*Lines);
57+
if (Line.consume_front("gfx_target_version")) {
58+
Line.drop_while([](char C) { return std::isspace(C); });
59+
if (Line.consumeInteger(10, GFXVersion))
60+
return 1;
61+
break;
62+
}
63+
}
64+
65+
// If this is zero the node is a CPU.
66+
if (GFXVersion == 0)
67+
continue;
68+
Devices.emplace_back(Node, GFXVersion);
69+
}
70+
71+
// Sort the devices by their node to make sure it prints in order.
72+
llvm::sort(Devices, [](auto &L, auto &R) { return L.first < R.first; });
73+
for (const auto &[Node, GFXVersion] : Devices)
74+
std::fprintf(stdout, "gfx%ld%ld%lx\n", getMajor(GFXVersion),
75+
getMinor(GFXVersion), getStep(GFXVersion));
76+
77+
return 0;
78+
}

clang/tools/amdgpu-arch/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@
88

99
set(LLVM_LINK_COMPONENTS Support)
1010

11-
add_clang_tool(amdgpu-arch AMDGPUArch.cpp AMDGPUArchByHSA.cpp AMDGPUArchByHIP.cpp)
11+
add_clang_tool(amdgpu-arch AMDGPUArch.cpp AMDGPUArchByKFD.cpp AMDGPUArchByHIP.cpp)
1212

1313
target_link_libraries(amdgpu-arch PRIVATE clangBasic)

0 commit comments

Comments
 (0)