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