Skip to content

Commit

Permalink
[TLI] Add support for reallocarray
Browse files Browse the repository at this point in the history
reallocarray is available in glibc since 2.29 under _DEFAULT_SOURCE and
under _GNU_SOURCE before, let's model it appropriately.
  • Loading branch information
serge-sans-paille committed Nov 4, 2024
1 parent b4ef43f commit c46cdf6
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 2 deletions.
6 changes: 6 additions & 0 deletions clang/include/clang/Basic/Builtins.td
Original file line number Diff line number Diff line change
Expand Up @@ -3224,6 +3224,12 @@ def AllocA : GNULibBuiltin<"stdlib.h"> {
let AddBuiltinPrefixedAlias = 1;
}

def ReallocArray : GNULibBuiltin<"stdlib.h"> {
let Spellings = ["reallocarray"];
let Prototype = "void*(void*, size_t, size_t)";
}


// POSIX malloc.h

def MemAlign : GNULibBuiltin<"malloc.h"> {
Expand Down
1 change: 1 addition & 0 deletions clang/test/Sema/builtins-gnu-mode.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ int stpncpy;
int strdup;
int strndup;
int index;
int reallocarray;
int rindex;
int bzero;
int strcasecmp;
Expand Down
5 changes: 5 additions & 0 deletions llvm/include/llvm/Analysis/TargetLibraryInfo.def
Original file line number Diff line number Diff line change
Expand Up @@ -2077,6 +2077,11 @@ TLI_DEFINE_ENUM_INTERNAL(reallocf)
TLI_DEFINE_STRING_INTERNAL("reallocf")
TLI_DEFINE_SIG_INTERNAL(Ptr, Ptr, SizeT)

/// void *reallocarray(void *ptr, size_t nmemb, size_t size);
TLI_DEFINE_ENUM_INTERNAL(reallocarray)
TLI_DEFINE_STRING_INTERNAL("reallocarray")
TLI_DEFINE_SIG_INTERNAL(Ptr, Ptr, SizeT, SizeT)

/// char *realpath(const char *file_name, char *resolved_name);
TLI_DEFINE_ENUM_INTERNAL(realpath)
TLI_DEFINE_STRING_INTERNAL("realpath")
Expand Down
1 change: 1 addition & 0 deletions llvm/lib/Analysis/TargetLibraryInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -852,6 +852,7 @@ static void initializeLibCalls(TargetLibraryInfoImpl &TLI, const Triple &T,
TLI.setUnavailable(LibFunc_memrchr);
TLI.setUnavailable(LibFunc_ntohl);
TLI.setUnavailable(LibFunc_ntohs);
TLI.setUnavailable(LibFunc_reallocarray);
TLI.setUnavailable(LibFunc_reallocf);
TLI.setUnavailable(LibFunc_roundeven);
TLI.setUnavailable(LibFunc_roundevenf);
Expand Down
15 changes: 15 additions & 0 deletions llvm/lib/Transforms/Utils/BuildLibCalls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -577,6 +577,21 @@ bool llvm::inferNonMandatoryLibFuncAttrs(Function &F,
Changed |= setDoesNotCapture(F, 0);
Changed |= setArgNoUndef(F, 1);
break;
case LibFunc_reallocarray:
Changed |= setAllocFamily(F, "malloc");
Changed |= setAllocKind(F, AllocFnKind::Realloc);
Changed |= setAllocatedPointerParam(F, 0);
Changed |= setAllocSize(F, 1, std::nullopt);
Changed |= setAllocSize(F, 2, std::nullopt);
Changed |= setOnlyAccessesInaccessibleMemOrArgMem(F);
Changed |= setRetNoUndef(F);
Changed |= setDoesNotThrow(F);
Changed |= setRetDoesNotAlias(F);
Changed |= setWillReturn(F);
Changed |= setDoesNotCapture(F, 0);
Changed |= setArgNoUndef(F, 1);
Changed |= setArgNoUndef(F, 2);
break;
case LibFunc_read:
// May throw; "read" is a valid pthread cancellation point.
Changed |= setRetAndArgsNoUndef(F);
Expand Down
4 changes: 2 additions & 2 deletions llvm/test/tools/llvm-tli-checker/ps4-tli-check.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@
## the exact count first; the two directives should add up to that.
## Yes, this means additions to TLI will fail this test, but the argument
## to -COUNT can't be an expression.
# AVAIL: TLI knows 522 symbols, 289 available
# AVAIL: TLI knows 523 symbols, 289 available
# AVAIL-COUNT-289: {{^}} available
# AVAIL-NOT: {{^}} available
# UNAVAIL-COUNT-233: not available
# UNAVAIL-COUNT-234: not available
# UNAVAIL-NOT: not available

## This is a large file so it's worth telling lit to stop here.
Expand Down
1 change: 1 addition & 0 deletions llvm/unittests/Analysis/TargetLibraryInfoTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,7 @@ TEST_F(TargetLibraryInfoTest, ValidProto) {
"declare void @qsort(i8*, i64, i64, i32 (i8*, i8*)*)\n"
"declare i64 @readlink(i8*, i8*, i64)\n"
"declare i8* @realloc(i8*, i64)\n"
"declare i8* @reallocarray(i8*, i64, i64)\n"
"declare i8* @reallocf(i8*, i64)\n"
"declare double @remainder(double, double)\n"
"declare float @remainderf(float, float)\n"
Expand Down

0 comments on commit c46cdf6

Please sign in to comment.