Index: clang/lib/Driver/ToolChains/AMDGPU.cpp =================================================================== --- clang/lib/Driver/ToolChains/AMDGPU.cpp +++ clang/lib/Driver/ToolChains/AMDGPU.cpp @@ -10,12 +10,14 @@ #include "CommonArgs.h" #include "clang/Basic/TargetID.h" #include "clang/Driver/Compilation.h" +#include "clang/Driver/Distro.h" #include "clang/Driver/DriverDiagnostic.h" #include "clang/Driver/InputInfo.h" #include "clang/Driver/Options.h" #include "llvm/Option/ArgList.h" #include "llvm/Support/Error.h" #include "llvm/Support/FileUtilities.h" +#include "llvm/Support/Host.h" #include "llvm/Support/LineIterator.h" #include "llvm/Support/Path.h" #include "llvm/Support/VirtualFileSystem.h" @@ -306,6 +308,14 @@ ROCmSearchDirs.emplace_back(D.SysRoot + "/opt/" + LatestROCm, /*StrictChecking=*/true); + Distro Dist(D.getVFS(), llvm::Triple(llvm::sys::getProcessTriple())); + if (Dist.IsDebian() || Dist.IsRedhat()) { + ROCmSearchDirs.emplace_back(D.SysRoot + "/usr/local", + /*StrictChecking=*/true); + ROCmSearchDirs.emplace_back(D.SysRoot + "/usr", + /*StrictChecking=*/true); + } + DoPrintROCmSearchDirs(); return ROCmSearchDirs; } @@ -461,18 +471,30 @@ llvm::sys::path::append(IncludePath, "include"); LibPath = InstallPath; llvm::sys::path::append(LibPath, "lib"); - - llvm::ErrorOr> VersionFile = - FS.getBufferForFile(BinPath + "/.hipVersion"); - if (!VersionFile && Candidate.StrictChecking) - continue; - - if (HIPVersionArg.empty() && VersionFile) - if (parseHIPVersionFile((*VersionFile)->getBuffer())) + SharePath = InstallPath; + llvm::sys::path::append(SharePath, "share"); + + // 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"}) { + llvm::ErrorOr> VersionFile = + FS.getBufferForFile(VersionFilePath); + if (!VersionFile) continue; + if (HIPVersionArg.empty() && VersionFile) + if (parseHIPVersionFile((*VersionFile)->getBuffer())) + continue; - HasHIPRuntime = true; - return; + HasHIPRuntime = true; + return; + } + // Otherwise, if -rocm-path is specified (no strict checking), use the + // default HIP version or specified by --hip-version. + if (!Candidate.StrictChecking) { + HasHIPRuntime = true; + return; + } } HasHIPRuntime = false; } Index: clang/lib/Driver/ToolChains/ROCm.h =================================================================== --- clang/lib/Driver/ToolChains/ROCm.h +++ clang/lib/Driver/ToolChains/ROCm.h @@ -107,6 +107,7 @@ SmallString<0> LibPath; SmallString<0> LibDevicePath; SmallString<0> IncludePath; + SmallString<0> SharePath; llvm::StringMap LibDeviceMap; // Libraries that are always linked. Index: clang/test/Driver/hip-version.hip =================================================================== --- clang/test/Driver/hip-version.hip +++ clang/test/Driver/hip-version.hip @@ -9,6 +9,14 @@ // RUN: --target=amdgcn-amd-amdhsa \ // RUN: | FileCheck -check-prefixes=FOUND %s +// RUN: rm -rf %t/Inputs +// RUN: mkdir -p %t/Inputs +// RUN: cp -r %S/Inputs/rocm %t/Inputs +// RUN: mkdir -p %t/Inputs/rocm/share/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 + // FOUND: Found HIP installation: {{.*Inputs.*rocm}}, version 3.6.20214-a2917cd // When --rocm-path is set and .hipVersion is not found, use default version