From 020513f6164f6574ec725d363dd0017788679d21 Mon Sep 17 00:00:00 2001 From: Cordell Bloor Date: Wed, 8 Mar 2023 12:50:40 -0700 Subject: [PATCH 1/4] Backport support for HIP in /usr --- debian/changelog | 3 + debian/patches/amdgpu-usr-search-paths.patch | 123 +++++++++++++++++++ debian/patches/series | 1 + 3 files changed, 127 insertions(+) create mode 100644 debian/patches/amdgpu-usr-search-paths.patch diff --git a/debian/changelog b/debian/changelog index 0e13d098..1a4c0843 100644 --- a/debian/changelog +++ b/debian/changelog @@ -13,6 +13,9 @@ llvm-toolchain-15 (1:15.0.7-2) UNRELEASED; urgency=medium [ John Paul Adrian Glaubitz ] * Don't run chrpath for libclang on powerpc and powerpcspe + [ Cordell Bloor ] + * Backport support for HIP in /usr + -- Sylvestre Ledru Sun, 15 Jan 2023 10:54:41 +0100 llvm-toolchain-15 (1:15.0.7-1) unstable; urgency=medium diff --git a/debian/patches/amdgpu-usr-search-paths.patch b/debian/patches/amdgpu-usr-search-paths.patch new file mode 100644 index 00000000..9006a853 --- /dev/null +++ b/debian/patches/amdgpu-usr-search-paths.patch @@ -0,0 +1,123 @@ +From 082593ff7aff68060bd66dccfa43493d07d9c255 Mon Sep 17 00:00:00 2001 +From: "Yaxun (Sam) Liu" +Date: Tue, 11 Oct 2022 18:53:59 -0400 +Subject: [PATCH] [HIP] Detect HIP for Debian/Fedora + +HIP is installed at /usr or /usr/local on Debin/Fedora, +and the version file is at {root}/share/hip/version. + +Reviewed by: Artem Belevich + +Differential Revision: https://reviews.llvm.org/D135796 +--- + clang/lib/Driver/ToolChains/AMDGPU.cpp | 42 ++++++++++++++++++++------ + clang/lib/Driver/ToolChains/ROCm.h | 1 + + clang/test/Driver/hip-version.hip | 8 +++++ + 3 files changed, 41 insertions(+), 10 deletions(-) + +diff --git a/clang/lib/Driver/ToolChains/AMDGPU.cpp b/clang/lib/Driver/ToolChains/AMDGPU.cpp +index 6246568710916..261594171564e 100644 +--- a/clang/lib/Driver/ToolChains/AMDGPU.cpp ++++ b/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 @@ RocmInstallationDetector::getInstallationPathCandidates() { + 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 @@ void RocmInstallationDetector::detectHIPRuntime() { + 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; + } +diff --git a/clang/lib/Driver/ToolChains/ROCm.h b/clang/lib/Driver/ToolChains/ROCm.h +index 33baaa887043e..b16deecdebec5 100644 +--- a/clang/lib/Driver/ToolChains/ROCm.h ++++ b/clang/lib/Driver/ToolChains/ROCm.h +@@ -107,6 +107,7 @@ class RocmInstallationDetector { + SmallString<0> LibPath; + SmallString<0> LibDevicePath; + SmallString<0> IncludePath; ++ SmallString<0> SharePath; + llvm::StringMap LibDeviceMap; + + // Libraries that are always linked. +diff --git a/clang/test/Driver/hip-version.hip b/clang/test/Driver/hip-version.hip +index a9bac813a9195..4ef9e4ade0881 100644 +--- a/clang/test/Driver/hip-version.hip ++++ b/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 diff --git a/debian/patches/series b/debian/patches/series index b918a51a..7961162d 100644 --- a/debian/patches/series +++ b/debian/patches/series @@ -160,3 +160,4 @@ fix-mesa-build.patch link-grpc.diff amdgpu-nonlinux.patch +amdgpu-usr-search-paths.patch From 8bbef9430567a15dea3c4cd09d79f1d7497135b7 Mon Sep 17 00:00:00 2001 From: Cordell Bloor Date: Thu, 9 Mar 2023 00:56:25 -0700 Subject: [PATCH 2/4] Add Debian paths for rocm-device-libs to search list --- debian/changelog | 3 +- debian/patches/amdgpu-bitcode-multiarch.patch | 60 +++++++++++++++++++ debian/patches/series | 1 + 3 files changed, 63 insertions(+), 1 deletion(-) create mode 100644 debian/patches/amdgpu-bitcode-multiarch.patch diff --git a/debian/changelog b/debian/changelog index 1a4c0843..daadbcc8 100644 --- a/debian/changelog +++ b/debian/changelog @@ -14,7 +14,8 @@ llvm-toolchain-15 (1:15.0.7-2) UNRELEASED; urgency=medium * Don't run chrpath for libclang on powerpc and powerpcspe [ Cordell Bloor ] - * Backport support for HIP in /usr + * Backport support for HIP in /usr and add Debian paths + for rocm-device-libs to search list (Closes: #1031808) -- Sylvestre Ledru Sun, 15 Jan 2023 10:54:41 +0100 diff --git a/debian/patches/amdgpu-bitcode-multiarch.patch b/debian/patches/amdgpu-bitcode-multiarch.patch new file mode 100644 index 00000000..b2a1cd06 --- /dev/null +++ b/debian/patches/amdgpu-bitcode-multiarch.patch @@ -0,0 +1,60 @@ +From efa9394d72968e0c84c2c9283476c70807f60b80 Mon Sep 17 00:00:00 2001 +From: Cordell Bloor +Date: Wed, 8 Mar 2023 02:57:51 -0700 +Subject: [PATCH] Search for the bitcode libraries in a multiarch directory + +The Debian path for the rocm-device-libs is +/usr/lib/$(DEB_HOST_MULTIARCH)/amdgcn/bitcode + +Forwarded: not-needed +--- + clang/lib/Driver/ToolChains/AMDGPU.cpp | 19 +++++++++++++------ + 1 file changed, 13 insertions(+), 6 deletions(-) + +diff --git a/clang/lib/Driver/ToolChains/AMDGPU.cpp b/clang/lib/Driver/ToolChains/AMDGPU.cpp +index f20b28a66c67..2e8e56b39ff6 100644 +--- a/clang/lib/Driver/ToolChains/AMDGPU.cpp ++++ b/clang/lib/Driver/ToolChains/AMDGPU.cpp +@@ -14,9 +14,11 @@ + #include "clang/Driver/DriverDiagnostic.h" + #include "clang/Driver/InputInfo.h" + #include "clang/Driver/Options.h" ++#include "llvm/ADT/Triple.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" +@@ -422,16 +424,21 @@ void RocmInstallationDetector::detectDeviceLibrary() { + // The possible structures are: + // - ${ROCM_ROOT}/amdgcn/bitcode/* + // - ${ROCM_ROOT}/lib/* ++ // - ${ROCM_ROOT}/lib//amdgcn/bitcode/* + // - ${ROCM_ROOT}/lib/bitcode/* +- // so try to detect these layouts. +- static constexpr std::array SubDirsList[] = { +- {"amdgcn", "bitcode"}, +- {"lib", ""}, +- {"lib", "bitcode"}, ++ // so try to detect these layouts. Note that bitcode is associated with the ++ // compiler that built it (not the target architecture). ++ llvm::Triple HostTriple(llvm::sys::getProcessTriple()); ++ std::string Multiarch = Twine(HostTriple.getArchName() + "-linux-gnu").str(); ++ static const std::array SubDirsList[] = { ++ {"amdgcn", "bitcode", "", ""}, ++ {"lib", "", "", ""}, ++ {"lib", Multiarch, "amdgcn", "bitcode"}, ++ {"lib", "bitcode", "", ""}, + }; + + // Make a path by appending sub-directories to InstallPath. +- auto MakePath = [&](const llvm::ArrayRef &SubDirs) { ++ auto MakePath = [&](const llvm::ArrayRef &SubDirs) { + auto Path = CandidatePath; + for (auto SubDir : SubDirs) + llvm::sys::path::append(Path, SubDir); +-- +2.39.2 + diff --git a/debian/patches/series b/debian/patches/series index 7961162d..52067f5f 100644 --- a/debian/patches/series +++ b/debian/patches/series @@ -161,3 +161,4 @@ link-grpc.diff amdgpu-nonlinux.patch amdgpu-usr-search-paths.patch +amdgpu-bitcode-multiarch.patch From 787cd5c4400f8991862141b301376a95cbf7e3a3 Mon Sep 17 00:00:00 2001 From: Cordell Bloor Date: Thu, 9 Mar 2023 01:53:43 -0700 Subject: [PATCH 3/4] d/qualify-clang.sh: add test for hip-lang --- debian/qualify-clang.sh | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/debian/qualify-clang.sh b/debian/qualify-clang.sh index 1804077f..d0e300fa 100755 --- a/debian/qualify-clang.sh +++ b/debian/qualify-clang.sh @@ -1665,6 +1665,18 @@ if test ! -f /usr/lib/llvm-$VERSION/lib/libclangBasic.a; then exit 1 fi +# check that the hip language is functioning +echo "Testing HIP language ..." +echo "if this fails, please run" +echo "apt-get install hipcc" +if test $VERSION -eq 15; then + cat > foo.hip < +int main() { return 0; } +EOF + clang++-15 -x hip -lamdhip64 foo.hip +fi + #clean up rm -f a.out bar crash-* foo foo.* lldb-cmd.txt main.* test_fuzzer.cc foo.* o rm -rf output matmul.* *profraw opt.ll a.json default.profdata test test.cpp From c7394d42a6efb9b0ccd35002364dc59b77f73bd3 Mon Sep 17 00:00:00 2001 From: Cordell Bloor Date: Thu, 9 Mar 2023 02:32:15 -0700 Subject: [PATCH 4/4] Move amdgpu patches to their own subdirectory --- .../bitcode-multiarch.patch} | 0 .../{amdgpu-nonlinux.patch => amdgpu/nonlinux.patch} | 0 .../usr-search-paths.patch} | 0 debian/patches/series | 6 +++--- 4 files changed, 3 insertions(+), 3 deletions(-) rename debian/patches/{amdgpu-bitcode-multiarch.patch => amdgpu/bitcode-multiarch.patch} (100%) rename debian/patches/{amdgpu-nonlinux.patch => amdgpu/nonlinux.patch} (100%) rename debian/patches/{amdgpu-usr-search-paths.patch => amdgpu/usr-search-paths.patch} (100%) diff --git a/debian/patches/amdgpu-bitcode-multiarch.patch b/debian/patches/amdgpu/bitcode-multiarch.patch similarity index 100% rename from debian/patches/amdgpu-bitcode-multiarch.patch rename to debian/patches/amdgpu/bitcode-multiarch.patch diff --git a/debian/patches/amdgpu-nonlinux.patch b/debian/patches/amdgpu/nonlinux.patch similarity index 100% rename from debian/patches/amdgpu-nonlinux.patch rename to debian/patches/amdgpu/nonlinux.patch diff --git a/debian/patches/amdgpu-usr-search-paths.patch b/debian/patches/amdgpu/usr-search-paths.patch similarity index 100% rename from debian/patches/amdgpu-usr-search-paths.patch rename to debian/patches/amdgpu/usr-search-paths.patch diff --git a/debian/patches/series b/debian/patches/series index 52067f5f..af7b175a 100644 --- a/debian/patches/series +++ b/debian/patches/series @@ -159,6 +159,6 @@ add-arm-vcmpz-test.patch fix-mesa-build.patch link-grpc.diff -amdgpu-nonlinux.patch -amdgpu-usr-search-paths.patch -amdgpu-bitcode-multiarch.patch +amdgpu/nonlinux.patch +amdgpu/usr-search-paths.patch +amdgpu/bitcode-multiarch.patch