SHA256
1
0
forked from pool/llvm14
llvm14/llvm-lifetime-for-rust.patch
Aaron Puchert 4e37c37b16 - 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
2022-08-26 23:23:42 +00:00

91 lines
4.1 KiB
Diff

From 59b1d748157ddce5f701dfcaa4fae9a553fc9775 Mon Sep 17 00:00:00 2001
From: Simonas Kazlauskas <git@kazlauskas.me>
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<LibFunc, AllocFnsTy> 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;