Skip to content

Commit

Permalink
[rust] Add knowledge of __rust_{alloc,realloc,dealloc}
Browse files Browse the repository at this point in the history
  • Loading branch information
nagisa authored and nikic committed Feb 3, 2021
1 parent f525115 commit c3fb401
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
10 changes: 10 additions & 0 deletions llvm/include/llvm/Analysis/TargetLibraryInfo.def
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,16 @@ 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_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")
Expand Down
8 changes: 6 additions & 2 deletions llvm/lib/Analysis/MemoryBuiltins.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,10 @@ static const std::pair<LibFunc, AllocFnsTy> AllocationFnData[] = {
{LibFunc_vec_realloc, {ReallocLike, 2, 1, -1}},
{LibFunc_reallocf, {ReallocLike, 2, 1, -1}},
{LibFunc_strdup, {StrDupLike, 1, -1, -1}},
{LibFunc_strndup, {StrDupLike, 2, 1, -1}}
{LibFunc_strndup, {StrDupLike, 2, 1, -1}},

{LibFunc_rust_alloc, {MallocLike, 2, 0, -1}},
{LibFunc_rust_realloc, {ReallocLike, 4, 3, -1}},
// TODO: Handle "int posix_memalign(void **, size_t, size_t)"
};

Expand Down Expand Up @@ -462,7 +465,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;
Expand Down
22 changes: 22 additions & 0 deletions llvm/lib/Analysis/TargetLibraryInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1513,6 +1513,28 @@ bool TargetLibraryInfoImpl::isValidProtoForLibFunc(const FunctionType &FTy,
else
return false;
}

case LibFunc_rust_alloc:
return (NumParams == 3 && FTy.getReturnType()->isPointerTy() &&
FTy.getParamType(0)->isIntegerTy() &&
FTy.getParamType(1)->isIntegerTy() &&
FTy.getParamType(2)->isPointerTy());

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 == 6 && FTy.getReturnType()->isPointerTy() &&
FTy.getParamType(0)->isPointerTy() &&
FTy.getParamType(1)->isIntegerTy() &&
FTy.getParamType(2)->isIntegerTy() &&
FTy.getParamType(3)->isIntegerTy() &&
FTy.getParamType(4)->isIntegerTy() &&
FTy.getParamType(5)->isPointerTy());

case LibFunc::NumLibFuncs:
case LibFunc::NotLibFunc:
break;
Expand Down

0 comments on commit c3fb401

Please sign in to comment.