Skip to content

Commit 0662045

Browse files
authored
[TLI] Add support for pvalloc() (#144949)
While pvalloc() is a legacy POSIX function, it remains widely available in common C libraries like glibc. Model pvalloc() in TargetLibraryInfo, allowing LLVM to correctly infer its attributes.
1 parent 714b2fd commit 0662045

File tree

6 files changed

+16
-3
lines changed

6 files changed

+16
-3
lines changed

llvm/include/llvm/Analysis/TargetLibraryInfo.def

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2046,6 +2046,11 @@ TLI_DEFINE_ENUM_INTERNAL(puts)
20462046
TLI_DEFINE_STRING_INTERNAL("puts")
20472047
TLI_DEFINE_SIG_INTERNAL(Int, Ptr)
20482048

2049+
/// void *pvalloc(size_t size);
2050+
TLI_DEFINE_ENUM_INTERNAL(pvalloc)
2051+
TLI_DEFINE_STRING_INTERNAL("pvalloc")
2052+
TLI_DEFINE_SIG_INTERNAL(Ptr, SizeT)
2053+
20492054
/// ssize_t pwrite(int fildes, const void *buf, size_t nbyte, off_t offset);
20502055
TLI_DEFINE_ENUM_INTERNAL(pwrite)
20512056
TLI_DEFINE_STRING_INTERNAL("pwrite")

llvm/lib/Analysis/TargetLibraryInfo.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -814,6 +814,7 @@ static void initializeLibCalls(TargetLibraryInfoImpl &TLI, const Triple &T,
814814
TLI.setUnavailable(LibFunc_pclose);
815815
TLI.setUnavailable(LibFunc_popen);
816816
TLI.setUnavailable(LibFunc_pread);
817+
TLI.setUnavailable(LibFunc_pvalloc);
817818
TLI.setUnavailable(LibFunc_pwrite);
818819
TLI.setUnavailable(LibFunc_read);
819820
TLI.setUnavailable(LibFunc_readlink);

llvm/lib/Transforms/Utils/BuildLibCalls.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -514,10 +514,12 @@ bool llvm::inferNonMandatoryLibFuncAttrs(Function &F,
514514
case LibFunc_valloc:
515515
case LibFunc_malloc:
516516
case LibFunc_vec_malloc:
517+
Changed |= setAllocSize(F, 0, std::nullopt);
518+
[[fallthrough]];
519+
case LibFunc_pvalloc:
517520
Changed |= setAllocFamily(F, TheLibFunc == LibFunc_vec_malloc ? "vec_malloc"
518521
: "malloc");
519522
Changed |= setAllocKind(F, AllocFnKind::Alloc | AllocFnKind::Uninitialized);
520-
Changed |= setAllocSize(F, 0, std::nullopt);
521523
Changed |= setOnlyAccessesInaccessibleMemory(F);
522524
Changed |= setRetAndArgsNoUndef(F);
523525
Changed |= setDoesNotThrow(F);

llvm/test/Transforms/InferFunctionAttrs/annotate.ll

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -812,6 +812,9 @@ declare i32 @putchar_unlocked(i32)
812812
; CHECK: declare noundef i32 @puts(ptr noundef readonly captures(none)) [[NOFREE_NOUNWIND]]
813813
declare i32 @puts(ptr)
814814

815+
; CHECK: declare noalias noundef ptr @pvalloc(i64 noundef) [[INACCESSIBLEMEMONLY_NOFREE_NOUNWIND_WILLRETURN_ALLOCKIND_ALLOCUNINIT_FAMILY_MALLOC:#[0-9]+]]
816+
declare ptr @pvalloc(i64)
817+
815818
; CHECK: declare noundef i64 @pwrite(i32 noundef, ptr noundef readonly captures(none), i64 noundef, i64 noundef) [[NOFREE]]
816819
declare i64 @pwrite(i32, ptr, i64, i64)
817820

@@ -1195,6 +1198,7 @@ declare void @memset_pattern16(ptr, ptr, i64)
11951198
; CHECK-DAG: attributes [[NOFREE_NOUNWIND_READONLY]] = { nofree nounwind memory(read) }
11961199
; CHECK-DAG: attributes [[INACCESSIBLEMEMORARGMEMONLY_NOUNWIND_WILLRETURN_ALLOCKIND_FREE_FAMILY_MALLOC]] = { mustprogress nounwind willreturn allockind("free") memory(argmem: readwrite, inaccessiblemem: readwrite) "alloc-family"="malloc" }
11971200
; CHECK-DAG: attributes [[INACCESSIBLEMEMONLY_NOFREE_NOUNWIND_WILLRETURN_ALLOCKIND_ALLOCUNINIT_ALLOCSIZE0_FAMILY_MALLOC]] = { mustprogress nofree nounwind willreturn allockind("alloc,uninitialized") allocsize(0) memory(inaccessiblemem: readwrite) "alloc-family"="malloc" }
1201+
; CHECK-DAG: attributes [[INACCESSIBLEMEMONLY_NOFREE_NOUNWIND_WILLRETURN_ALLOCKIND_ALLOCUNINIT_FAMILY_MALLOC]] = { mustprogress nofree nounwind willreturn allockind("alloc,uninitialized") memory(inaccessiblemem: readwrite) "alloc-family"="malloc" }
11981202
; CHECK-DAG: attributes [[ARGMEMONLY_NOFREE_NOUNWIND_READONLY_WILLRETURN]] = { mustprogress nocallback nofree nounwind willreturn memory(argmem: read) }
11991203
; CHECK-DAG: attributes [[NOFREE]] = { nofree }
12001204
; CHECK-DAG: attributes [[ARGMEMONLY_NOFREE_NOUNWIND]] = { nocallback nofree nounwind memory(argmem: readwrite) }

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 523 symbols, 289 available
57+
# AVAIL: TLI knows 524 symbols, 289 available
5858
# AVAIL-COUNT-289: {{^}} available
5959
# AVAIL-NOT: {{^}} available
60-
# UNAVAIL-COUNT-234: not available
60+
# UNAVAIL-COUNT-235: 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
@@ -315,6 +315,7 @@ TEST_F(TargetLibraryInfoTest, ValidProto) {
315315
"declare i32 @putchar(i32)\n"
316316
"declare i32 @putchar_unlocked(i32)\n"
317317
"declare i32 @puts(i8*)\n"
318+
"declare i8* @pvalloc(i64)\n"
318319
"declare void @qsort(i8*, i64, i64, i32 (i8*, i8*)*)\n"
319320
"declare i64 @readlink(i8*, i8*, i64)\n"
320321
"declare i8* @realloc(i8*, i64)\n"

0 commit comments

Comments
 (0)