1 //===- AMDGPUArch.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 AMDGPU installed in system
10 // using HSA. This tool is used by AMDGPU OpenMP driver.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "llvm/Support/DynamicLibrary.h"
15 #include "llvm/Support/Error.h"
16 #include <memory>
17 #include <string>
18 #include <vector>
19 
20 #if DYNAMIC_HSA
21 typedef enum {
22   HSA_STATUS_SUCCESS = 0x0,
23 } hsa_status_t;
24 
25 typedef enum {
26   HSA_DEVICE_TYPE_CPU = 0,
27   HSA_DEVICE_TYPE_GPU = 1,
28 } hsa_device_type_t;
29 
30 typedef enum {
31   HSA_AGENT_INFO_NAME = 0,
32   HSA_AGENT_INFO_DEVICE = 17,
33 } hsa_agent_info_t;
34 
35 typedef struct hsa_agent_s {
36   uint64_t handle;
37 } hsa_agent_t;
38 
39 hsa_status_t (*hsa_init)();
40 hsa_status_t (*hsa_shut_down)();
41 hsa_status_t (*hsa_agent_get_info)(hsa_agent_t, hsa_agent_info_t, void *);
42 hsa_status_t (*hsa_iterate_agents)(hsa_status_t (*)(hsa_agent_t, void *),
43                                    void *);
44 
45 constexpr const char *DynamicHSAPath = "libhsa-runtime64.so";
46 
47 llvm::Error loadHSA() {
48   std::string ErrMsg;
49   auto DynlibHandle = std::make_unique<llvm::sys::DynamicLibrary>(
50       llvm::sys::DynamicLibrary::getPermanentLibrary(DynamicHSAPath, &ErrMsg));
51   if (!DynlibHandle->isValid()) {
52     return llvm::createStringError(llvm::inconvertibleErrorCode(),
53                                    "Failed to 'dlopen' %s", DynamicHSAPath);
54   }
55 #define DYNAMIC_INIT(SYMBOL)                                                   \
56   {                                                                            \
57     void *SymbolPtr = DynlibHandle->getAddressOfSymbol(#SYMBOL);               \
58     if (!SymbolPtr)                                                            \
59       return llvm::createStringError(llvm::inconvertibleErrorCode(),           \
60                                      "Failed to 'dlsym' " #SYMBOL);            \
61     SYMBOL = reinterpret_cast<decltype(SYMBOL)>(SymbolPtr);                    \
62   }
63   DYNAMIC_INIT(hsa_init);
64   DYNAMIC_INIT(hsa_shut_down);
65   DYNAMIC_INIT(hsa_agent_get_info);
66   DYNAMIC_INIT(hsa_iterate_agents);
67 #undef DYNAMIC_INIT
68   return llvm::Error::success();
69 }
70 #else
71 
72 #if defined(__has_include)
73 #if __has_include("hsa/hsa.h")
74 #include "hsa/hsa.h"
75 #elif __has_include("hsa.h")
76 #include "hsa.h"
77 #endif
78 #include "hsa/hsa.h"
79 #endif
80 
81 llvm::Error loadHSA() { return llvm::Error::success(); }
82 #endif
83 
84 static hsa_status_t iterateAgentsCallback(hsa_agent_t Agent, void *Data) {
85   hsa_device_type_t DeviceType;
86   hsa_status_t Status =
87       hsa_agent_get_info(Agent, HSA_AGENT_INFO_DEVICE, &DeviceType);
88 
89   // continue only if device type if GPU
90   if (Status != HSA_STATUS_SUCCESS || DeviceType != HSA_DEVICE_TYPE_GPU) {
91     return Status;
92   }
93 
94   std::vector<std::string> *GPUs =
95       static_cast<std::vector<std::string> *>(Data);
96   char GPUName[64];
97   Status = hsa_agent_get_info(Agent, HSA_AGENT_INFO_NAME, GPUName);
98   if (Status != HSA_STATUS_SUCCESS) {
99     return Status;
100   }
101   GPUs->push_back(GPUName);
102   return HSA_STATUS_SUCCESS;
103 }
104 
105 int main(int argc, char *argv[]) {
106   // Attempt to load the HSA runtime.
107   if (llvm::Error Err = loadHSA()) {
108     logAllUnhandledErrors(std::move(Err), llvm::errs());
109     return 1;
110   }
111 
112   hsa_status_t Status = hsa_init();
113   if (Status != HSA_STATUS_SUCCESS) {
114     return 1;
115   }
116 
117   std::vector<std::string> GPUs;
118   Status = hsa_iterate_agents(iterateAgentsCallback, &GPUs);
119   if (Status != HSA_STATUS_SUCCESS) {
120     return 1;
121   }
122 
123   for (const auto &GPU : GPUs)
124     printf("%s\n", GPU.c_str());
125 
126   if (GPUs.size() < 1)
127     return 1;
128 
129   hsa_shut_down();
130   return 0;
131 }
132