[PATCH] D123084: BuildLibCalls: infer allocptr attribute for free and realloc() family functions

Augie Fackler via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Apr 4 15:17:29 PDT 2022


durin42 created this revision.
Herald added a subscriber: hiraditya.
Herald added a project: All.
durin42 requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

Depends on D123083 <https://reviews.llvm.org/D123083>


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D123084

Files:
  llvm/lib/Transforms/Utils/BuildLibCalls.cpp
  llvm/test/Transforms/InferFunctionAttrs/annotate.ll


Index: llvm/test/Transforms/InferFunctionAttrs/annotate.ll
===================================================================
--- llvm/test/Transforms/InferFunctionAttrs/annotate.ll
+++ llvm/test/Transforms/InferFunctionAttrs/annotate.ll
@@ -489,7 +489,7 @@
 ; CHECK: declare noundef i64 @fread(i8* nocapture noundef, i64 noundef, i64 noundef, %opaque* nocapture noundef) [[NOFREE_NOUNWIND]]
 declare i64 @fread(i8*, i64, i64, %opaque*)
 
-; CHECK: declare void @free(i8* nocapture noundef) [[INACCESSIBLEMEMORARGMEMONLY_NOUNWIND_WILLRETURN:#[0-9]+]]
+; CHECK: declare void @free(i8* allocptr nocapture noundef) [[INACCESSIBLEMEMORARGMEMONLY_NOUNWIND_WILLRETURN:#[0-9]+]]
 declare void @free(i8*)
 
 ; CHECK: declare double @frexp(double, i32* nocapture) [[NOFREE_NOUNWIND_WILLRETURN:#[0-9]+]]
@@ -774,10 +774,10 @@
 ; CHECK: declare noundef i64 @readlink(i8* nocapture noundef readonly, i8* nocapture noundef, i64 noundef) [[NOFREE_NOUNWIND]]
 declare i64 @readlink(i8*, i8*, i64)
 
-; CHECK: declare noalias noundef i8* @realloc(i8* nocapture, i64 noundef) [[INACCESSIBLEMEMORARGMEMONLY_NOUNWIND_WILLRETURN_ALLOCSIZE:#[0-9]+]]
+; CHECK: declare noalias noundef i8* @realloc(i8* allocptr nocapture, i64 noundef) [[INACCESSIBLEMEMORARGMEMONLY_NOUNWIND_WILLRETURN_ALLOCSIZE:#[0-9]+]]
 declare i8* @realloc(i8*, i64)
 
-; CHECK: declare noalias noundef i8* @reallocf(i8* nocapture, i64 noundef) [[INACCESSIBLEMEMORARGMEMONLY_NOUNWIND_WILLRETURN_ALLOCSIZE]]
+; CHECK: declare noalias noundef i8* @reallocf(i8* allocptr nocapture, i64 noundef) [[INACCESSIBLEMEMORARGMEMONLY_NOUNWIND_WILLRETURN_ALLOCSIZE]]
 declare i8* @reallocf(i8*, i64)
 
 ; CHECK: declare noundef i8* @realpath(i8* nocapture noundef readonly, i8* noundef) [[NOFREE_NOUNWIND]]
Index: llvm/lib/Transforms/Utils/BuildLibCalls.cpp
===================================================================
--- llvm/lib/Transforms/Utils/BuildLibCalls.cpp
+++ llvm/lib/Transforms/Utils/BuildLibCalls.cpp
@@ -229,6 +229,13 @@
   return true;
 }
 
+static bool setAllocatedPointerParam(Function &F, unsigned ArgNo) {
+  if (F.hasParamAttribute(ArgNo, Attribute::AllocatedPointer))
+    return false;
+  F.addParamAttr(ArgNo, Attribute::AllocatedPointer);
+  return true;
+}
+
 static bool setAllocSize(Function &F, unsigned ElemSizeArg,
                          Optional<unsigned> NumElemsArg) {
   if (F.hasFnAttribute(Attribute::AllocSize)) {
@@ -524,6 +531,7 @@
   case LibFunc_realloc:
   case LibFunc_vec_realloc:
   case LibFunc_reallocf:
+    Changed |= setAllocatedPointerParam(F, 0);
     Changed |= setAllocSize(F, 1, None);
     Changed |= setOnlyAccessesInaccessibleMemOrArgMem(F);
     Changed |= setRetNoUndef(F);
@@ -657,6 +665,7 @@
     return Changed;
   case LibFunc_free:
   case LibFunc_vec_free:
+    Changed |= setAllocatedPointerParam(F, 0);
     Changed |= setOnlyAccessesInaccessibleMemOrArgMem(F);
     Changed |= setArgsNoUndef(F);
     Changed |= setDoesNotThrow(F);


STAMPS
actor(@durin42) application(Differential) author(@durin42) herald(H225) herald(H343) herald(H428) herald(H576) herald(H615) herald(H864) monogram(D123084) object-type(DREV) phid(PHID-DREV-cv2sp7z3u7wqkm46ooe6) revision-repository(rG) revision-status(needs-review) subscriber(@hiraditya) subscriber(@llvm-commits) tag(#all) tag(#llvm) via(conduit)

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D123084.420323.patch
Type: text/x-patch
Size: 2948 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220404/41d071df/attachment-0001.bin>


More information about the llvm-commits mailing list