From 4e37c37b165c1e7f68ea55997bfdc6b2e61d636ef41e177301463f5397178c89 Mon Sep 17 00:00:00 2001 From: Aaron Puchert Date: Fri, 26 Aug 2022 23:23:42 +0000 Subject: [PATCH] - Add llvm-lifetime-for-rust.patch to have Rust memory management functions considered as lifetime markers. This should aid dead store elimination to dynamically allocated memory in Rust code. OBS-URL: https://build.opensuse.org/package/show/devel:tools:compiler/llvm14?expand=0&rev=33 --- llvm-lifetime-for-rust.patch | 90 ++++++++++++++++++++++++++++++++++++ llvm14.changes | 7 +++ llvm14.spec | 3 ++ 3 files changed, 100 insertions(+) create mode 100644 llvm-lifetime-for-rust.patch diff --git a/llvm-lifetime-for-rust.patch b/llvm-lifetime-for-rust.patch new file mode 100644 index 0000000..23c8c07 --- /dev/null +++ b/llvm-lifetime-for-rust.patch @@ -0,0 +1,90 @@ +From 59b1d748157ddce5f701dfcaa4fae9a553fc9775 Mon Sep 17 00:00:00 2001 +From: Simonas Kazlauskas +Date: Sat, 3 Jun 2017 18:55:08 +0300 +Subject: [PATCH] [rust] Add knowledge of __rust_{alloc,realloc,dealloc} + +--- + .../llvm/Analysis/TargetLibraryInfo.def | 13 ++++++++++++ + llvm/lib/Analysis/MemoryBuiltins.cpp | 6 +++++- + llvm/lib/Analysis/TargetLibraryInfo.cpp | 20 +++++++++++++++++++ + 3 files changed, 38 insertions(+), 1 deletion(-) + +diff --git a/llvm/include/llvm/Analysis/TargetLibraryInfo.def b/llvm/include/llvm/Analysis/TargetLibraryInfo.def +index 9c1abef33b288..70a79112ded85 100644 +--- a/llvm/include/llvm/Analysis/TargetLibraryInfo.def ++++ b/llvm/include/llvm/Analysis/TargetLibraryInfo.def +@@ -391,6 +391,19 @@ TLI_DEFINE_STRING_INTERNAL("__powf_finite") + /// long double __powl_finite(long double x, long double y); + TLI_DEFINE_ENUM_INTERNAL(powl_finite) + TLI_DEFINE_STRING_INTERNAL("__powl_finite") ++ ++TLI_DEFINE_ENUM_INTERNAL(rust_alloc) ++TLI_DEFINE_STRING_INTERNAL("__rust_alloc") ++ ++TLI_DEFINE_ENUM_INTERNAL(rust_alloc_zeroed) ++TLI_DEFINE_STRING_INTERNAL("__rust_alloc_zeroed") ++ ++TLI_DEFINE_ENUM_INTERNAL(rust_dealloc) ++TLI_DEFINE_STRING_INTERNAL("__rust_dealloc") ++ ++TLI_DEFINE_ENUM_INTERNAL(rust_realloc) ++TLI_DEFINE_STRING_INTERNAL("__rust_realloc") ++ + /// double __sincospi_stret(double x); + TLI_DEFINE_ENUM_INTERNAL(sincospi_stret) + TLI_DEFINE_STRING_INTERNAL("__sincospi_stret") +diff --git a/llvm/lib/Analysis/MemoryBuiltins.cpp b/llvm/lib/Analysis/MemoryBuiltins.cpp +index 9e26f292b789a..4b08e6417ebf6 100644 +--- a/llvm/lib/Analysis/MemoryBuiltins.cpp ++++ b/llvm/lib/Analysis/MemoryBuiltins.cpp +@@ -111,6 +111,9 @@ static const std::pair AllocationFnData[] = { + {LibFunc_strdup, {StrDupLike, 1, -1, -1, -1}}, + {LibFunc_strndup, {StrDupLike, 2, 1, -1, -1}}, + {LibFunc___kmpc_alloc_shared, {MallocLike, 1, 0, -1, -1}}, ++ {LibFunc_rust_alloc, {MallocLike, 2, 0, -1, 1}}, ++ {LibFunc_rust_alloc_zeroed, {CallocLike, 2, 0, -1, 1}}, ++ {LibFunc_rust_realloc, {ReallocLike, 4, 3, -1, 2}}, + // TODO: Handle "int posix_memalign(void **, size_t, size_t)" + }; + +@@ -429,7 +432,8 @@ bool llvm::isLibFreeFunction(const Function *F, const LibFunc TLIFn) { + TLIFn == LibFunc_ZdlPvjSt11align_val_t || // delete(void*, unsigned long, align_val_t) + TLIFn == LibFunc_ZdlPvmSt11align_val_t || // delete(void*, unsigned long, align_val_t) + TLIFn == LibFunc_ZdaPvjSt11align_val_t || // delete[](void*, unsigned int, align_val_t) +- TLIFn == LibFunc_ZdaPvmSt11align_val_t) // delete[](void*, unsigned long, align_val_t) ++ TLIFn == LibFunc_ZdaPvmSt11align_val_t || // delete[](void*, unsigned long, align_val_t) ++ TLIFn == LibFunc_rust_dealloc) + ExpectedNumParams = 3; + else + return false; +diff --git a/llvm/lib/Analysis/TargetLibraryInfo.cpp b/llvm/lib/Analysis/TargetLibraryInfo.cpp +index 02923c2c7eb14..22d6a5f04152a 100644 +--- a/llvm/lib/Analysis/TargetLibraryInfo.cpp ++++ b/llvm/lib/Analysis/TargetLibraryInfo.cpp +@@ -1793,6 +1793,26 @@ bool TargetLibraryInfoImpl::isValidProtoForLibFunc(const FunctionType &FTy, + else + return false; + } ++ ++ case LibFunc_rust_alloc: ++ case LibFunc_rust_alloc_zeroed: ++ return (NumParams == 2 && FTy.getReturnType()->isPointerTy() && ++ FTy.getParamType(0)->isIntegerTy() && ++ FTy.getParamType(1)->isIntegerTy()); ++ ++ case LibFunc_rust_dealloc: ++ return (NumParams == 3 && FTy.getReturnType()->isVoidTy() && ++ FTy.getParamType(0)->isPointerTy() && ++ FTy.getParamType(1)->isIntegerTy() && ++ FTy.getParamType(2)->isIntegerTy()); ++ ++ case LibFunc_rust_realloc: ++ return (NumParams == 4 && FTy.getReturnType()->isPointerTy() && ++ FTy.getParamType(0)->isPointerTy() && ++ FTy.getParamType(1)->isIntegerTy() && ++ FTy.getParamType(2)->isIntegerTy() && ++ FTy.getParamType(3)->isIntegerTy()); ++ + case LibFunc::NumLibFuncs: + case LibFunc::NotLibFunc: + break; diff --git a/llvm14.changes b/llvm14.changes index 6a78ef3..b85f892 100644 --- a/llvm14.changes +++ b/llvm14.changes @@ -1,3 +1,10 @@ +------------------------------------------------------------------- +Fri Aug 26 21:18:56 UTC 2022 - Aaron Puchert + +- Add llvm-lifetime-for-rust.patch to have Rust memory management + functions considered as lifetime markers. This should aid dead + store elimination to dynamically allocated memory in Rust code. + ------------------------------------------------------------------- Tue Aug 23 21:20:47 UTC 2022 - Aaron Puchert diff --git a/llvm14.spec b/llvm14.spec index 32dd2d2..61968b1 100644 --- a/llvm14.spec +++ b/llvm14.spec @@ -344,6 +344,8 @@ Source100: %{name}-rpmlintrc Source101: baselibs.conf # PATCH-FIX-OPENSUSE lto-disable-cache.patch -- Disable ThinLTO cache Patch0: lto-disable-cache.patch +# PATCH-FIX-OPENSUSE -- Consider Rust memory management functions as lifetime markers. (From https://github.com/rust-lang/llvm-project.) +Patch1: llvm-lifetime-for-rust.patch # PATCH-FIX-OPENSUSE assume-opensuse.patch idoenmez@suse.de -- Always enable openSUSE/SUSE features Patch2: assume-opensuse.patch # PATCH-FIX-OPENSUSE default-to-i586.patch -- Use i586 as default target for 32bit @@ -790,6 +792,7 @@ This package contains the development files for Polly. %prep %setup -q -a 1 -a 2 -a 3 -a 4 -a 5 -a 6 -a 7 -a 8 -a 9 -b 50 -b 51 -n llvm-%{_version}.src %patch0 -p2 +%patch1 -p2 %patch5 -p1 %patch13 -p1 %patch14 -p1