[clang] f3c2094 - [clang] `aligned_alloc` allocation function specifies alignment in first arg, manifest that knowledge

Roman Lebedev via cfe-commits cfe-commits at lists.llvm.org
Fri Sep 10 10:50:17 PDT 2021


Author: Roman Lebedev
Date: 2021-09-10T20:49:29+03:00
New Revision: f3c2094d8c112f40923ef45953f66c4b2ecb6d01

URL: https://github.com/llvm/llvm-project/commit/f3c2094d8c112f40923ef45953f66c4b2ecb6d01
DIFF: https://github.com/llvm/llvm-project/commit/f3c2094d8c112f40923ef45953f66c4b2ecb6d01.diff

LOG: [clang] `aligned_alloc` allocation function specifies alignment in first arg, manifest that knowledge

Mainly, if a constant value was passed as an alignment,
then we correctly annotate the alignment of the returned value
of @aligned_alloc. And if it wasn't constant,
then we also don't loose that, but emit an assumption.

Added: 
    

Modified: 
    clang/lib/Sema/SemaDecl.cpp
    clang/test/CodeGen/alloc-fns-alignment.c

Removed: 
    


################################################################################
diff  --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index 59726ad67fbb..cc3417d4ccba 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -15150,6 +15150,10 @@ void Sema::AddKnownFunctionAttributes(FunctionDecl *FD) {
     // Add known guaranteed alignment for allocation functions.
     switch (BuiltinID) {
     case Builtin::BIaligned_alloc:
+      if (!FD->hasAttr<AllocAlignAttr>())
+        FD->addAttr(AllocAlignAttr::CreateImplicit(Context, ParamIdx(1, FD),
+                                                   FD->getLocation()));
+      LLVM_FALLTHROUGH;
     case Builtin::BIcalloc:
     case Builtin::BImalloc:
     case Builtin::BImemalign:

diff  --git a/clang/test/CodeGen/alloc-fns-alignment.c b/clang/test/CodeGen/alloc-fns-alignment.c
index 1ce0cfbad97b..b19cf867f74c 100644
--- a/clang/test/CodeGen/alloc-fns-alignment.c
+++ b/clang/test/CodeGen/alloc-fns-alignment.c
@@ -48,13 +48,14 @@ void *aligned_alloc_large_constant_test(size_t n) {
 // ALIGN16: align 16 i8* @realloc
 
 // CHECK-LABEL: @aligned_alloc_variable_test
-// ALIGN16: align 16 i8* @aligned_alloc
+// ALIGN16:      %[[ALLOCATED:.*]] = call align 16 i8* @aligned_alloc({{i32|i64}} %[[ALIGN:.*]], {{i32|i64}} %[[NBYTES:.*]])
+// ALIGN16-NEXT: call void @llvm.assume(i1 true) [ "align"(i8* %[[ALLOCATED]], {{i32|i64}} %[[ALIGN]]) ]
 
 // CHECK-LABEL: @aligned_alloc_constant_test
 // ALIGN16: align 16 i8* @aligned_alloc
 
 // CHECK-LABEL: @aligned_alloc_large_constant_test
-// ALIGN16: align 16 i8* @aligned_alloc
+// ALIGN16: align 4096 i8* @aligned_alloc
 
 // CHECK-LABEL: @malloc_test
 // ALIGN8: align 8 i8* @malloc
@@ -72,7 +73,7 @@ void *aligned_alloc_large_constant_test(size_t n) {
 // ALIGN8: align 8 i8* @aligned_alloc
 
 // CHECK-LABEL: @aligned_alloc_large_constant_test
-// ALIGN8: align 8 i8* @aligned_alloc
+// ALIGN8: align 4096 i8* @aligned_alloc
 
 // NOBUILTIN-MALLOC: declare i8* @malloc
 // NOBUILTIN-CALLOC: declare i8* @calloc


        


More information about the cfe-commits mailing list