1 //===- AMDGPUArchByHIP.cpp - list AMDGPU installed ----------*- C++ -*-----===//
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
7 //===----------------------------------------------------------------------===//
9 // This file implements a tool for detecting name of AMDGPU installed in system
10 // using HIP runtime. This tool is used by AMDGPU OpenMP and HIP driver.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/Support/DynamicLibrary.h"
15 #include "llvm/Support/Error.h"
16 #include "llvm/Support/raw_ostream.h"
22 char gcnArchName
[256];
30 typedef hipError_t (*hipGetDeviceCount_t
)(int *);
31 typedef hipError_t (*hipDeviceGet_t
)(int *, int);
32 typedef hipError_t (*hipGetDeviceProperties_t
)(hipDeviceProp_t
*, int);
34 int printGPUsByHIP() {
36 constexpr const char *DynamicHIPPath
= "amdhip64.dll";
38 constexpr const char *DynamicHIPPath
= "libamdhip64.so";
42 auto DynlibHandle
= std::make_unique
<llvm::sys::DynamicLibrary
>(
43 llvm::sys::DynamicLibrary::getPermanentLibrary(DynamicHIPPath
, &ErrMsg
));
44 if (!DynlibHandle
->isValid()) {
45 llvm::errs() << "Failed to load " << DynamicHIPPath
<< ": " << ErrMsg
50 #define DYNAMIC_INIT_HIP(SYMBOL) \
52 void *SymbolPtr = DynlibHandle->getAddressOfSymbol(#SYMBOL); \
54 llvm::errs() << "Failed to find symbol " << #SYMBOL << '\n'; \
57 SYMBOL = reinterpret_cast<decltype(SYMBOL)>(SymbolPtr); \
60 hipGetDeviceCount_t hipGetDeviceCount
;
61 hipDeviceGet_t hipDeviceGet
;
62 hipGetDeviceProperties_t hipGetDeviceProperties
;
64 DYNAMIC_INIT_HIP(hipGetDeviceCount
);
65 DYNAMIC_INIT_HIP(hipDeviceGet
);
66 DYNAMIC_INIT_HIP(hipGetDeviceProperties
);
68 #undef DYNAMIC_INIT_HIP
71 hipError_t err
= hipGetDeviceCount(&deviceCount
);
72 if (err
!= hipSuccess
) {
73 llvm::errs() << "Failed to get device count\n";
77 for (int i
= 0; i
< deviceCount
; ++i
) {
79 err
= hipDeviceGet(&deviceId
, i
);
80 if (err
!= hipSuccess
) {
81 llvm::errs() << "Failed to get device id for ordinal " << i
<< '\n';
86 err
= hipGetDeviceProperties(&prop
, deviceId
);
87 if (err
!= hipSuccess
) {
88 llvm::errs() << "Failed to get device properties for device " << deviceId
92 llvm::outs() << prop
.gcnArchName
<< '\n';