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