Skip to content

Commit 282bf9e

Browse files
committed
[HIP] Fix ROCm detection
ROCm has changed installation path to /opt/rocm-{release}. Add detection for that. Also support ROCM_PATH environment variable. Reviewed by: Artem Belevich Differential Revision: https://reviews.llvm.org/D98867
1 parent c210167 commit 282bf9e

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

clang/lib/Driver/ToolChains/AMDGPU.cpp

+43
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,12 @@ RocmInstallationDetector::getInstallationPathCandidates() {
186186
ROCmSearchDirs.emplace_back(RocmPathArg.str());
187187
DoPrintROCmSearchDirs();
188188
return ROCmSearchDirs;
189+
} else if (const char *RocmPathEnv = ::getenv("ROCM_PATH")) {
190+
if (!StringRef(RocmPathEnv).empty()) {
191+
ROCmSearchDirs.emplace_back(RocmPathEnv);
192+
DoPrintROCmSearchDirs();
193+
return ROCmSearchDirs;
194+
}
189195
}
190196

191197
// Try to find relative to the compiler binary.
@@ -247,6 +253,43 @@ RocmInstallationDetector::getInstallationPathCandidates() {
247253

248254
ROCmSearchDirs.emplace_back(D.SysRoot + "/opt/rocm",
249255
/*StrictChecking=*/true);
256+
257+
// Find the latest /opt/rocm-{release} directory.
258+
std::error_code EC;
259+
std::string LatestROCm;
260+
llvm::VersionTuple LatestVer;
261+
// Get ROCm version from ROCm directory name.
262+
auto GetROCmVersion = [](StringRef DirName) {
263+
llvm::VersionTuple V;
264+
std::string VerStr = DirName.drop_front(strlen("rocm-")).str();
265+
// The ROCm directory name follows the format of
266+
// rocm-{major}.{minor}.{subMinor}[-{build}]
267+
std::replace(VerStr.begin(), VerStr.end(), '-', '.');
268+
V.tryParse(VerStr);
269+
return V;
270+
};
271+
for (llvm::vfs::directory_iterator
272+
File = D.getVFS().dir_begin(D.SysRoot + "/opt", EC),
273+
FileEnd;
274+
File != FileEnd && !EC; File.increment(EC)) {
275+
llvm::StringRef FileName = llvm::sys::path::filename(File->path());
276+
if (!FileName.startswith("rocm-"))
277+
continue;
278+
if (LatestROCm.empty()) {
279+
LatestROCm = FileName.str();
280+
LatestVer = GetROCmVersion(LatestROCm);
281+
continue;
282+
}
283+
auto Ver = GetROCmVersion(FileName);
284+
if (LatestVer < Ver) {
285+
LatestROCm = FileName.str();
286+
LatestVer = Ver;
287+
}
288+
}
289+
if (!LatestROCm.empty())
290+
ROCmSearchDirs.emplace_back(D.SysRoot + "/opt/" + LatestROCm,
291+
/*StrictChecking=*/true);
292+
250293
DoPrintROCmSearchDirs();
251294
return ROCmSearchDirs;
252295
}

clang/test/Driver/rocm-detect.hip

+19
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,20 @@
2121
// RUN: --rocm-path=%S/Inputs/rocm %s 2>&1 \
2222
// RUN: | FileCheck -check-prefixes=COMMON,NODEFAULTLIBS %s
2323

24+
// Test environment variable ROCM_PATH.
25+
// RUN: env ROCM_PATH=%S/Inputs/rocm %clang -### -target x86_64-linux-gnu \
26+
// RUN: --print-rocm-search-dirs %s 2>&1 \
27+
// RUN: | FileCheck -check-prefixes=ROCM-ENV %s
28+
29+
// Test detecting latest /opt/rocm-{release} directory.
30+
// RUN: rm -rf %T/opt
31+
// RUN: mkdir -p %T/opt
32+
// RUN: cp -r %S/Inputs/rocm %T/opt/rocm-3.9.0-1234
33+
// RUN: cp -r %S/Inputs/rocm %T/opt/rocm-3.10.0
34+
// RUN: %clang -### -target x86_64-linux-gnu --sysroot=%T \
35+
// RUN: --print-rocm-search-dirs %s 2>&1 \
36+
// RUN: | FileCheck -check-prefixes=ROCM-REL %s
37+
2438
// Test ROCm installation built by SPACK by invoke clang at %T/rocm-spack/llvm-amdgpu-*
2539
// directory through a soft link.
2640

@@ -60,6 +74,11 @@
6074

6175
// COMMON: "-triple" "amdgcn-amd-amdhsa"
6276

77+
// ROCM-ENV: ROCm installation search path: {{.*}}/Inputs/rocm
78+
79+
// ROCM-REL: ROCm installation search path: {{.*}}/opt/rocm
80+
// ROCM-REL: ROCm installation search path: {{.*}}/opt/rocm-3.10.0
81+
6382
// SPACK: ROCm installation search path (Spack 4.0.0): [[DIR:.*]]
6483
// SPACK: ROCm installation search path: [[CLANG:.*]]
6584
// SPACK: ROCm installation search path: [[CLANG]]/lib/clang/{{[0-9.]+}}

0 commit comments

Comments
 (0)