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";
27constexpr static long GFX1250_VERSION = 120500;
28
29// See the ROCm implementation for how this is handled.
30// https://github.com/ROCm/ROCT-Thunk-Interface/blob/master/src/libhsakmt.h#L126
31constexpr static long getMajor(long Ver) { return (Ver / 10000) % 100; }
32constexpr static long getMinor(long Ver) { return (Ver / 100) % 100; }
33constexpr static long getStep(long Ver) { return Ver % 100; }
34
35// For A0, print gfx1250-strict to match rocminfo
36static StringRef getRevisionSuffix(long GFXVersion, long ASICRevision) {
37 return (GFXVersion == GFX1250_VERSION && ASICRevision == 0) ? "-strict" : "";
38}
39
40// Exposed for testing
41int printGPUsByKFD(StringRef NodePath) {
42 struct KFDNode {
43 long Node;
44 long GFXVersion;
45 long ASICRevision;
46 };
47
48 SmallVector<KFDNode> Devices;
49 std::error_code EC;
50 sys::fs::directory_iterator Begin(NodePath, EC), End;
51
52 // Fail if the sysfs topology does not exist (e.g., WSL)
53 if (EC)
54 return 1;
55
56 for (; Begin != End; Begin.increment(ec&: EC)) {
57 if (EC)
58 return 1;
59
60 long Node = 0;
61 if (sys::path::stem(path: Begin->path()).consumeInteger(Radix: 10, Result&: Node))
62 return 1;
63
64 SmallString<0> Path(Begin->path());
65 sys::path::append(path&: Path, a: "properties");
66
67 ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOrErr =
68 MemoryBuffer::getFileOrSTDIN(Filename: Path);
69 if (std::error_code EC = BufferOrErr.getError())
70 return 1;
71
72 long GFXVersion = 0;
73 uint64_t Capability = 0;
74 for (line_iterator Lines(**BufferOrErr, false); !Lines.is_at_end();
75 ++Lines) {
76 StringRef Line(*Lines);
77 if (Line.consume_front(Prefix: "gfx_target_version")) {
78 if (Line.drop_while(F: [](char C) { return std::isspace(C); })
79 .consumeInteger(Radix: 10, Result&: GFXVersion))
80 return 1;
81 // Differentiate between capability and capability2
82 } else if (Line.consume_front(Prefix: "capability") && !Line.starts_with(Prefix: '2')) {
83 if (Line.drop_while(F: [](char C) { return std::isspace(C); })
84 .consumeInteger(Radix: 10, Result&: Capability))
85 return 1;
86 }
87 }
88
89 // If this is zero the node is a CPU.
90 if (GFXVersion == 0)
91 continue;
92 // ASIC revision is bits 25:22 in capability
93 long ASICRevision = (Capability >> 22) & 0xf;
94 Devices.push_back(Elt: {.Node: Node, .GFXVersion: GFXVersion, .ASICRevision: ASICRevision});
95 }
96
97 // Sort the devices by their node to make sure it prints in order.
98 llvm::sort(C&: Devices, Comp: [](auto &L, auto &R) { return L.Node < R.Node; });
99 for (const auto &[Node, GFXVersion, ASICRevision] : Devices) {
100 outs() << "gfx" << getMajor(Ver: GFXVersion) << getMinor(Ver: GFXVersion)
101 << format_hex_no_prefix(N: getStep(Ver: GFXVersion), Width: 1)
102 << getRevisionSuffix(GFXVersion, ASICRevision) << '\n';
103 }
104
105 return 0;
106}
107
108int printGPUsByKFD() { return printGPUsByKFD(NodePath: KFD_SYSFS_NODE_PATH); }
109