From 25275086c28360e3591f99f8de3ea3350e51fcf2 Mon Sep 17 00:00:00 2001 From: Sylvestre Ledru Date: Sat, 7 Sep 2024 23:09:15 +0200 Subject: [PATCH] Backport two commits to fix riscv64 issue. (Closes: #1080435) --- debian/changelog | 7 +- ...lvm-toolchain-17-sve-types-aux-target.diff | 22 ++-- debian/patches/rv64-fix-PCREL_HI20-issue.diff | 104 ++++++++++++++++++ debian/patches/rv64-fix-mm-leak.diff | 33 ++++++ debian/patches/series | 3 + 5 files changed, 153 insertions(+), 16 deletions(-) create mode 100644 debian/patches/rv64-fix-PCREL_HI20-issue.diff create mode 100644 debian/patches/rv64-fix-mm-leak.diff diff --git a/debian/changelog b/debian/changelog index 77375cea..776883c5 100644 --- a/debian/changelog +++ b/debian/changelog @@ -1,4 +1,4 @@ -llvm-toolchain-18 (1:18.1.8-10) UNRELEASED; urgency=medium +llvm-toolchain-18 (1:18.1.8-10) unstable; urgency=medium [ Matthias Klose ] * ld.lld: When no package-metadata option is given, fall-back @@ -7,7 +7,10 @@ llvm-toolchain-18 (1:18.1.8-10) UNRELEASED; urgency=medium [ Emanuele Rocca ] * Enable bolt on arm64, now supported upstream. - -- Matthias Klose Tue, 06 Aug 2024 16:13:21 +0200 + [ Bo YU ] + * Backport two commits to fix riscv64 issue. (Closes: #1080435) + + -- Sylvestre Ledru Sat, 07 Sep 2024 23:08:26 +0200 llvm-toolchain-18 (1:18.1.8-9) unstable; urgency=medium diff --git a/debian/patches/llvm-toolchain-17-sve-types-aux-target.diff b/debian/patches/llvm-toolchain-17-sve-types-aux-target.diff index 23d14ffe..64445558 100644 --- a/debian/patches/llvm-toolchain-17-sve-types-aux-target.diff +++ b/debian/patches/llvm-toolchain-17-sve-types-aux-target.diff @@ -12,11 +12,9 @@ Co-authored-by: Sander De Smalen clang/test/PCH/aarch64-sve-types.c | 2 ++ 3 files changed, 7 insertions(+), 2 deletions(-) -Index: llvm-toolchain-17-17.0.6/clang/lib/AST/ASTContext.cpp -=================================================================== ---- llvm-toolchain-17-17.0.6.orig/clang/lib/AST/ASTContext.cpp -+++ llvm-toolchain-17-17.0.6/clang/lib/AST/ASTContext.cpp -@@ -1353,7 +1353,8 @@ void ASTContext::InitBuiltinTypes(const +--- a/clang/lib/AST/ASTContext.cpp ++++ b/clang/lib/AST/ASTContext.cpp +@@ -1353,7 +1353,8 @@ #include "clang/Basic/OpenCLExtensionTypes.def" } @@ -26,11 +24,9 @@ Index: llvm-toolchain-17-17.0.6/clang/lib/AST/ASTContext.cpp #define SVE_TYPE(Name, Id, SingletonId) \ InitBuiltinType(SingletonId, BuiltinType::Id); #include "clang/Basic/AArch64SVEACLETypes.def" -Index: llvm-toolchain-17-17.0.6/clang/lib/Sema/Sema.cpp -=================================================================== ---- llvm-toolchain-17-17.0.6.orig/clang/lib/Sema/Sema.cpp -+++ llvm-toolchain-17-17.0.6/clang/lib/Sema/Sema.cpp -@@ -424,7 +424,9 @@ void Sema::Initialize() { +--- a/clang/lib/Sema/Sema.cpp ++++ b/clang/lib/Sema/Sema.cpp +@@ -424,7 +424,9 @@ #include "clang/Basic/OpenCLExtensionTypes.def" } @@ -41,10 +37,8 @@ Index: llvm-toolchain-17-17.0.6/clang/lib/Sema/Sema.cpp #define SVE_TYPE(Name, Id, SingletonId) \ addImplicitTypedef(Name, Context.SingletonId); #include "clang/Basic/AArch64SVEACLETypes.def" -Index: llvm-toolchain-17-17.0.6/clang/test/PCH/aarch64-sve-types.c -=================================================================== ---- llvm-toolchain-17-17.0.6.orig/clang/test/PCH/aarch64-sve-types.c -+++ llvm-toolchain-17-17.0.6/clang/test/PCH/aarch64-sve-types.c +--- a/clang/test/PCH/aarch64-sve-types.c ++++ b/clang/test/PCH/aarch64-sve-types.c @@ -1,6 +1,8 @@ // RUN: %clang_cc1 -triple aarch64-linux-gnu -emit-pch -o %t %s // RUN: %clang_cc1 -triple aarch64-linux-gnu -include-pch %t \ diff --git a/debian/patches/rv64-fix-PCREL_HI20-issue.diff b/debian/patches/rv64-fix-PCREL_HI20-issue.diff new file mode 100644 index 00000000..bbb10de6 --- /dev/null +++ b/debian/patches/rv64-fix-PCREL_HI20-issue.diff @@ -0,0 +1,104 @@ +From 78f39dc70c1feaea5130b90ea3fb7b3ddd62446b Mon Sep 17 00:00:00 2001 +From: Jonas Hahnfeld +Date: Mon, 12 Feb 2024 19:45:52 +0100 +Subject: [PATCH] [JITLink][RISCV] Use hashmap to find PCREL_HI20 edge (#78849) + +As noted in issues #68594 and #73935, `JITLink/RISCV/ELF_ehframe.s` +fails with libstdc++'s expensive checks because `getRISCVPCRelHi20` +calls `std::equal_range` on the edges which may not be ordered by their +offset. Instead let `ELFJITLinker_riscv` build a hashmap of all edges +with type `R_RISCV_PCREL_HI20` that can be looked up in constant time. + +Bugs: https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=1080435 + +Closes #73935 +--- + .../lib/ExecutionEngine/JITLink/ELF_riscv.cpp | 68 ++++++++++--------- + 1 file changed, 35 insertions(+), 33 deletions(-) + +--- a/llvm/lib/ExecutionEngine/JITLink/ELF_riscv.cpp ++++ b/llvm/lib/ExecutionEngine/JITLink/ELF_riscv.cpp +@@ -133,38 +133,6 @@ + namespace llvm { + namespace jitlink { + +-static Expected getRISCVPCRelHi20(const Edge &E) { +- using namespace riscv; +- assert((E.getKind() == R_RISCV_PCREL_LO12_I || +- E.getKind() == R_RISCV_PCREL_LO12_S) && +- "Can only have high relocation for R_RISCV_PCREL_LO12_I or " +- "R_RISCV_PCREL_LO12_S"); +- +- const Symbol &Sym = E.getTarget(); +- const Block &B = Sym.getBlock(); +- orc::ExecutorAddrDiff Offset = Sym.getOffset(); +- +- struct Comp { +- bool operator()(const Edge &Lhs, orc::ExecutorAddrDiff Offset) { +- return Lhs.getOffset() < Offset; +- } +- bool operator()(orc::ExecutorAddrDiff Offset, const Edge &Rhs) { +- return Offset < Rhs.getOffset(); +- } +- }; +- +- auto Bound = +- std::equal_range(B.edges().begin(), B.edges().end(), Offset, Comp{}); +- +- for (auto It = Bound.first; It != Bound.second; ++It) { +- if (It->getKind() == R_RISCV_PCREL_HI20) +- return *It; +- } +- +- return make_error( +- "No HI20 PCREL relocation type be found for LO12 PCREL relocation type"); +-} +- + static uint32_t extractBits(uint32_t Num, unsigned Low, unsigned Size) { + return (Num & (((1ULL << Size) - 1) << Low)) >> Low; + } +@@ -184,9 +152,43 @@ + public: + ELFJITLinker_riscv(std::unique_ptr Ctx, + std::unique_ptr G, PassConfiguration PassConfig) +- : JITLinker(std::move(Ctx), std::move(G), std::move(PassConfig)) {} ++ : JITLinker(std::move(Ctx), std::move(G), std::move(PassConfig)) { ++ JITLinkerBase::getPassConfig().PostAllocationPasses.push_back( ++ [this](LinkGraph &G) { return gatherRISCVPCRelHi20(G); }); ++ } + + private: ++ DenseMap, const Edge *> ++ RelHi20; ++ ++ Error gatherRISCVPCRelHi20(LinkGraph &G) { ++ for (Block *B : G.blocks()) ++ for (Edge &E : B->edges()) ++ if (E.getKind() == R_RISCV_PCREL_HI20) ++ RelHi20[{B, E.getOffset()}] = &E; ++ ++ return Error::success(); ++ } ++ ++ Expected getRISCVPCRelHi20(const Edge &E) const { ++ using namespace riscv; ++ assert((E.getKind() == R_RISCV_PCREL_LO12_I || ++ E.getKind() == R_RISCV_PCREL_LO12_S) && ++ "Can only have high relocation for R_RISCV_PCREL_LO12_I or " ++ "R_RISCV_PCREL_LO12_S"); ++ ++ const Symbol &Sym = E.getTarget(); ++ const Block &B = Sym.getBlock(); ++ orc::ExecutorAddrDiff Offset = Sym.getOffset(); ++ ++ auto It = RelHi20.find({&B, Offset}); ++ if (It != RelHi20.end()) ++ return *It->second; ++ ++ return make_error("No HI20 PCREL relocation type be found " ++ "for LO12 PCREL relocation type"); ++ } ++ + Error applyFixup(LinkGraph &G, Block &B, const Edge &E) const { + using namespace riscv; + using namespace llvm::support; diff --git a/debian/patches/rv64-fix-mm-leak.diff b/debian/patches/rv64-fix-mm-leak.diff new file mode 100644 index 00000000..9442b74f --- /dev/null +++ b/debian/patches/rv64-fix-mm-leak.diff @@ -0,0 +1,33 @@ +From 3d67cf681a728e4cf0ab9947c0dd07539dda8b74 Mon Sep 17 00:00:00 2001 +From: Min-Yih Hsu +Date: Fri, 16 Feb 2024 16:19:56 -0800 +Subject: [PATCH] [JITLink] Always unmap standard segments in + InProcessMemoryManager::deallocate (#81943) + +Right now InProcessMemoryManager only releases a standard segment (via +sys::Memory::releaseMappedMemory) in `deallocate` when there is a +DeallocAction associated, leaving residual memory pages in the process +until termination. +Despite being a de facto memory leak, it won't cause a major issue if +users only create a single LLJIT instance per process, which is the most +common use cases. It will, however, drain virtual memory pages if we +create thousands of ephemeral LLJIT instances in the same process. + +This patch fixes this issue by releasing every standard segments +regardless of the attached DeallocAction. +--- + llvm/lib/ExecutionEngine/JITLink/JITLinkMemoryManager.cpp | 3 +-- + 1 file changed, 1 insertion(+), 2 deletions(-) + +--- a/llvm/lib/ExecutionEngine/JITLink/JITLinkMemoryManager.cpp ++++ b/llvm/lib/ExecutionEngine/JITLink/JITLinkMemoryManager.cpp +@@ -449,8 +449,7 @@ + for (auto &Alloc : Allocs) { + auto *FA = Alloc.release().toPtr(); + StandardSegmentsList.push_back(std::move(FA->StandardSegments)); +- if (!FA->DeallocActions.empty()) +- DeallocActionsList.push_back(std::move(FA->DeallocActions)); ++ DeallocActionsList.push_back(std::move(FA->DeallocActions)); + FA->~FinalizedAllocInfo(); + FinalizedAllocInfos.Deallocate(FA); + } diff --git a/debian/patches/series b/debian/patches/series index ae7d3fc6..9752a177 100644 --- a/debian/patches/series +++ b/debian/patches/series @@ -155,3 +155,6 @@ rename-libllvm.diff link-with-package-metadata.diff llvm-toolchain-17-sve-types-aux-target.diff env-lld-package-metadata.diff + +rv64-fix-PCREL_HI20-issue.diff +rv64-fix-mm-leak.diff