Skip to content

Commit

Permalink
[HIP] Fix version detection for old HIP-PATH
Browse files Browse the repository at this point in the history
ROCm used to install components under individual directories,
e.g. HIP installed to /opt/rocm/hip and rocblas installed to
/opt/rocm/rocblas. ROCm has transitioned to a flat directory
structure where all components are installed to /opt/rocm.
HIP-PATH and --hip-path are supposed to be /opt/rocm as
clang detect HIP version by /opt/rocm/share/hip/version.
However, some existing HIP app still uses HIP-PATH=/opt/rocm/hip.
To avoid regression, clang will also try detect share/hip/version
under the parent directory of HIP-PATH or --hip-path.
This way, the detection will work for both new HIP-PATH and
old HIP-PATH.

Reviewed by: Artem Belevich

Differential Revision: https://reviews.llvm.org/D154077

Fixes: SWDEV-407757
  • Loading branch information
yxsamliu committed Jun 29, 2023
1 parent bb4e547 commit 41a1625
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
15 changes: 13 additions & 2 deletions clang/lib/Driver/ToolChains/AMDGPU.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -461,10 +461,21 @@ void RocmInstallationDetector::detectHIPRuntime() {
SharePath = InstallPath;
llvm::sys::path::append(SharePath, "share");

// Get parent of InstallPath and append "share"
SmallString<0> ParentSharePath = llvm::sys::path::parent_path(InstallPath);
llvm::sys::path::append(ParentSharePath, "share");

auto Append = [](SmallString<0> &path, const Twine &a, const Twine &b = "",
const Twine &c = "", const Twine &d = "") {
SmallString<0> newpath = path;
llvm::sys::path::append(newpath, a, b, c, d);
return newpath;
};
// If HIP version file can be found and parsed, use HIP version from there.
for (const auto &VersionFilePath :
{std::string(SharePath) + "/hip/version",
std::string(BinPath) + "/.hipVersion"}) {
{Append(SharePath, "hip", "version"),
Append(ParentSharePath, "hip", "version"),
Append(BinPath, ".hipVersion")}) {
llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> VersionFile =
FS.getBufferForFile(VersionFilePath);
if (!VersionFile)
Expand Down
6 changes: 6 additions & 0 deletions clang/test/Driver/hip-version.hip
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,17 @@
// RUN: mkdir -p %t/Inputs
// RUN: cp -r %S/Inputs/rocm %t/Inputs
// RUN: mkdir -p %t/Inputs/rocm/share/hip
// RUN: mkdir -p %t/Inputs/rocm/hip
// RUN: mv %t/Inputs/rocm/bin/.hipVersion %t/Inputs/rocm/share/hip/version
// RUN: %clang -v --rocm-path=%t/Inputs/rocm 2>&1 \
// RUN: | FileCheck -check-prefixes=FOUND %s
// RUN: %clang -v --hip-path=%t/Inputs/rocm 2>&1 \
// RUN: | FileCheck -check-prefixes=FOUND %s
// RUN: %clang -v --hip-path=%t/Inputs/rocm/hip 2>&1 \
// RUN: | FileCheck -check-prefixes=HIP-PATH %s

// FOUND: Found HIP installation: {{.*Inputs.*rocm}}, version 3.6.20214-a2917cd
// HIP-PATH: Found HIP installation: {{.*Inputs.*rocm.*hip}}, version 3.6.20214-a2917cd

// When --rocm-path is set and .hipVersion is not found, use default version

Expand Down

0 comments on commit 41a1625

Please sign in to comment.