Skip to content

Commit dc4185f

Browse files
[TLI] Add support for reallocarray (#114818)
reallocarray is available in glibc since 2.29 under _DEFAULT_SOURCE and under _GNU_SOURCE before, let's model it appropriately.
1 parent 04d450f commit dc4185f

File tree

6 files changed

+27
-2
lines changed

6 files changed

+27
-2
lines changed

llvm/include/llvm/Analysis/TargetLibraryInfo.def

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2077,6 +2077,11 @@ TLI_DEFINE_ENUM_INTERNAL(reallocf)
20772077
TLI_DEFINE_STRING_INTERNAL("reallocf")
20782078
TLI_DEFINE_SIG_INTERNAL(Ptr, Ptr, SizeT)
20792079

2080+
/// void *reallocarray(void *ptr, size_t nmemb, size_t size);
2081+
TLI_DEFINE_ENUM_INTERNAL(reallocarray)
2082+
TLI_DEFINE_STRING_INTERNAL("reallocarray")
2083+
TLI_DEFINE_SIG_INTERNAL(Ptr, Ptr, SizeT, SizeT)
2084+
20802085
/// char *realpath(const char *file_name, char *resolved_name);
20812086
TLI_DEFINE_ENUM_INTERNAL(realpath)
20822087
TLI_DEFINE_STRING_INTERNAL("realpath")

llvm/lib/Analysis/TargetLibraryInfo.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -852,6 +852,7 @@ static void initializeLibCalls(TargetLibraryInfoImpl &TLI, const Triple &T,
852852
TLI.setUnavailable(LibFunc_memrchr);
853853
TLI.setUnavailable(LibFunc_ntohl);
854854
TLI.setUnavailable(LibFunc_ntohs);
855+
TLI.setUnavailable(LibFunc_reallocarray);
855856
TLI.setUnavailable(LibFunc_reallocf);
856857
TLI.setUnavailable(LibFunc_roundeven);
857858
TLI.setUnavailable(LibFunc_roundevenf);

llvm/lib/Transforms/Utils/BuildLibCalls.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -577,6 +577,20 @@ bool llvm::inferNonMandatoryLibFuncAttrs(Function &F,
577577
Changed |= setDoesNotCapture(F, 0);
578578
Changed |= setArgNoUndef(F, 1);
579579
break;
580+
case LibFunc_reallocarray:
581+
Changed |= setAllocFamily(F, "malloc");
582+
Changed |= setAllocKind(F, AllocFnKind::Realloc);
583+
Changed |= setAllocatedPointerParam(F, 0);
584+
Changed |= setAllocSize(F, 1, 2);
585+
Changed |= setOnlyAccessesInaccessibleMemOrArgMem(F);
586+
Changed |= setRetNoUndef(F);
587+
Changed |= setDoesNotThrow(F);
588+
Changed |= setRetDoesNotAlias(F);
589+
Changed |= setWillReturn(F);
590+
Changed |= setDoesNotCapture(F, 0);
591+
Changed |= setArgNoUndef(F, 1);
592+
Changed |= setArgNoUndef(F, 2);
593+
break;
580594
case LibFunc_read:
581595
// May throw; "read" is a valid pthread cancellation point.
582596
Changed |= setRetAndArgsNoUndef(F);

llvm/test/Transforms/InferFunctionAttrs/annotate.ll

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -827,6 +827,9 @@ declare i64 @readlink(ptr, ptr, i64)
827827
; CHECK: declare noalias noundef ptr @realloc(ptr allocptr nocapture, i64 noundef) [[INACCESSIBLEMEMORARGMEMONLY_NOUNWIND_WILLRETURN_ALLOCKIND_REALLOC_ALLOCSIZE1_FAMILY_MALLOC:#[0-9]+]]
828828
declare ptr @realloc(ptr, i64)
829829

830+
; CHECK: declare noalias noundef ptr @reallocarray(ptr allocptr nocapture, i64 noundef, i64 noundef) [[INACCESSIBLEMEMORARGMEMONLY_NOUNWIND_WILLRETURN_ALLOCKIND_REALLOC_ALLOCSIZE12_FAMILY_MALLOC:#[0-9]+]]
831+
declare ptr @reallocarray(ptr, i64, i64)
832+
830833
; CHECK: declare noalias noundef ptr @reallocf(ptr allocptr nocapture, i64 noundef) [[INACCESSIBLEMEMORARGMEMONLY_NOUNWIND_WILLRETURN_ALLOCKIND_REALLOC_ALLOCSIZE1_FAMILY_MALLOC]]
831834
declare ptr @reallocf(ptr, i64)
832835

@@ -1194,6 +1197,7 @@ declare void @memset_pattern16(ptr, ptr, i64)
11941197
; CHECK-DAG: attributes [[NOFREE]] = { nofree }
11951198
; CHECK-DAG: attributes [[ARGMEMONLY_NOFREE_NOUNWIND]] = { nofree nounwind memory(argmem: readwrite) }
11961199
; CHECK-DAG: attributes [[INACCESSIBLEMEMORARGMEMONLY_NOUNWIND_WILLRETURN_ALLOCKIND_REALLOC_ALLOCSIZE1_FAMILY_MALLOC]] = { mustprogress nounwind willreturn allockind("realloc") allocsize(1) memory(argmem: readwrite, inaccessiblemem: readwrite) "alloc-family"="malloc" }
1200+
; CHECK-DAG: attributes [[INACCESSIBLEMEMORARGMEMONLY_NOUNWIND_WILLRETURN_ALLOCKIND_REALLOC_ALLOCSIZE12_FAMILY_MALLOC]] = { mustprogress nounwind willreturn allockind("realloc") allocsize(1,2) memory(argmem: readwrite, inaccessiblemem: readwrite) "alloc-family"="malloc" }
11971201
; CHECK-DAG: attributes [[INACCESSIBLEMEMORARGONLY_NOFREE_NOUNWIND_WILLRETURN_FAMILY_MALLOC]] = { mustprogress nofree nounwind willreturn memory(argmem: readwrite, inaccessiblemem: readwrite) "alloc-family"="malloc" }
11981202
; CHECK-DAG: attributes [[NOFREE_COLD]] = { cold nofree }
11991203
; CHECK-DAG: attributes [[NOFREE_COLD_NORETURN]] = { cold nofree noreturn }

llvm/test/tools/llvm-tli-checker/ps4-tli-check.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,10 @@
5454
## the exact count first; the two directives should add up to that.
5555
## Yes, this means additions to TLI will fail this test, but the argument
5656
## to -COUNT can't be an expression.
57-
# AVAIL: TLI knows 522 symbols, 289 available
57+
# AVAIL: TLI knows 523 symbols, 289 available
5858
# AVAIL-COUNT-289: {{^}} available
5959
# AVAIL-NOT: {{^}} available
60-
# UNAVAIL-COUNT-233: not available
60+
# UNAVAIL-COUNT-234: not available
6161
# UNAVAIL-NOT: not available
6262

6363
## This is a large file so it's worth telling lit to stop here.

llvm/unittests/Analysis/TargetLibraryInfoTest.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,7 @@ TEST_F(TargetLibraryInfoTest, ValidProto) {
318318
"declare void @qsort(i8*, i64, i64, i32 (i8*, i8*)*)\n"
319319
"declare i64 @readlink(i8*, i8*, i64)\n"
320320
"declare i8* @realloc(i8*, i64)\n"
321+
"declare i8* @reallocarray(i8*, i64, i64)\n"
321322
"declare i8* @reallocf(i8*, i64)\n"
322323
"declare double @remainder(double, double)\n"
323324
"declare float @remainderf(float, float)\n"

0 commit comments

Comments
 (0)