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/Format.h"
17#include "llvm/Support/LineIterator.h"
18#include "llvm/Support/MemoryBuffer.h"
19#include "llvm/Support/Path.h"
20#include "llvm/Support/raw_ostream.h"
21#include <memory>
22
23using namespace llvm;
24
25constexpr static const char *KFD_SYSFS_NODE_PATH =
26 "/sys/devices/virtual/kfd/kfd/topology/nodes";
27
28// See the ROCm implementation for how this is handled.
29// https://github.com/ROCm/ROCT-Thunk-Interface/blob/master/src/libhsakmt.h#L126
30constexpr static long getMajor(long Ver) { return (Ver / 10000) % 100; }
31constexpr static long getMinor(long Ver) { return (Ver / 100) % 100; }
32constexpr static long getStep(long Ver) { return Ver % 100; }
33
34// Exposed for testing
35int printGPUsByKFD(StringRef NodePath) {
36 SmallVector<std::pair<long, long>> Devices;
37 std::error_code EC;
38 sys::fs::directory_iterator Begin(NodePath, EC), End;
39
40 // Fail if the sysfs topology does not exist (e.g., WSL)
41 if (EC)
42 return 1;
43
44 for (; Begin != End; Begin.increment(ec&: EC)) {
45 if (EC)
46 return 1;
47
48 long Node = 0;
49 if (sys::path::stem(path: Begin->path()).consumeInteger(Radix: 10, Result&: Node))
50 return 1;
51
52 SmallString<0> Path(Begin->path());
53 sys::path::append(path&: Path, a: "properties");
54
55 ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOrErr =
56 MemoryBuffer::getFileOrSTDIN(Filename: Path);
57 if (std::error_code EC = BufferOrErr.getError())
58 return 1;
59
60 long GFXVersion = 0;
61 for (line_iterator Lines(**BufferOrErr, false); !Lines.is_at_end();
62 ++Lines) {
63 StringRef Line(*Lines);
64 if (Line.consume_front(Prefix: "gfx_target_version")) {
65 if (Line.drop_while(F: [](char C) { return std::isspace(C); })
66 .consumeInteger(Radix: 10, Result&: GFXVersion))
67 return 1;
68 break;
69 }
70 }
71
72 // If this is zero the node is a CPU.
73 if (GFXVersion == 0)
74 continue;
75 Devices.emplace_back(Args&: Node, Args&: GFXVersion);
76 }
77
78 // Sort the devices by their node to make sure it prints in order.
79 llvm::sort(C&: Devices, Comp: [](auto &L, auto &R) { return L.first < R.first; });
80 for (const auto &[Node, GFXVersion] : Devices)
81 outs() << "gfx" << getMajor(Ver: GFXVersion) << getMinor(Ver: GFXVersion)
82 << format_hex_no_prefix(N: getStep(Ver: GFXVersion), Width: 1) << '\n';
83
84 return 0;
85}
86
87int printGPUsByKFD() { return printGPUsByKFD(NodePath: KFD_SYSFS_NODE_PATH); }
88