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 } 79