[clang] 2cae702 - Reland "[Clang] Propagate guaranteed alignment for malloc and others"

Dávid Bolvanský via cfe-commits cfe-commits at lists.llvm.org
Fri Apr 23 05:06:35 PDT 2021


Author: Dávid Bolvanský
Date: 2021-04-23T14:05:57+02:00
New Revision: 2cae7025c1d9e83d971f670c9a66497a8c1094ec

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

LOG: Reland "[Clang] Propagate guaranteed alignment for malloc and others"

This relands commit 6914a0ed2b30924b188968e59a83efa07ac5fe57. Crash in InstCombine was fixed.

Added: 
    clang/test/CodeGen/alloc-fns-alignment.c

Modified: 
    clang/include/clang/Basic/TargetInfo.h
    clang/lib/CodeGen/CGCall.cpp

Removed: 
    


################################################################################
diff  --git a/clang/include/clang/Basic/TargetInfo.h b/clang/include/clang/Basic/TargetInfo.h
index 3bcaaceb63d8e..449c026639b90 100644
--- a/clang/include/clang/Basic/TargetInfo.h
+++ b/clang/include/clang/Basic/TargetInfo.h
@@ -612,8 +612,8 @@ class TargetInfo : public virtual TransferrableTargetInfo,
   }
 
   /// Return the largest alignment for which a suitably-sized allocation with
-  /// '::operator new(size_t)' is guaranteed to produce a correctly-aligned
-  /// pointer.
+  /// '::operator new(size_t)' or 'malloc' is guaranteed to produce a
+  /// correctly-aligned pointer.
   unsigned getNewAlign() const {
     return NewAlign ? NewAlign : std::max(LongDoubleAlign, LongLongAlign);
   }

diff  --git a/clang/lib/CodeGen/CGCall.cpp b/clang/lib/CodeGen/CGCall.cpp
index 366c83eeb8e89..6c77f1889fb52 100644
--- a/clang/lib/CodeGen/CGCall.cpp
+++ b/clang/lib/CodeGen/CGCall.cpp
@@ -2048,6 +2048,24 @@ void CodeGenModule::ConstructAttributeList(
       // allows it to work on indirect virtual function calls.
       if (AttrOnCallSite && TargetDecl->hasAttr<NoMergeAttr>())
         FuncAttrs.addAttribute(llvm::Attribute::NoMerge);
+
+      // Add known guaranteed alignment for allocation functions.
+      if (unsigned BuiltinID = Fn->getBuiltinID()) {
+        switch (BuiltinID) {
+        case Builtin::BIaligned_alloc:
+        case Builtin::BIcalloc:
+        case Builtin::BImalloc:
+        case Builtin::BImemalign:
+        case Builtin::BIrealloc:
+        case Builtin::BIstrdup:
+        case Builtin::BIstrndup:
+          RetAttrs.addAlignmentAttr(Context.getTargetInfo().getNewAlign() /
+                                    Context.getTargetInfo().getCharWidth());
+          break;
+        default:
+          break;
+        }
+      }
     }
 
     // 'const', 'pure' and 'noalias' attributed functions are also nounwind.

diff  --git a/clang/test/CodeGen/alloc-fns-alignment.c b/clang/test/CodeGen/alloc-fns-alignment.c
new file mode 100644
index 0000000000000..d2f9b467196a7
--- /dev/null
+++ b/clang/test/CodeGen/alloc-fns-alignment.c
@@ -0,0 +1,35 @@
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -emit-llvm < %s | FileCheck %s --check-prefix=ALIGN16
+// RUN: %clang_cc1 -triple x86_64-windows-msvc      -emit-llvm < %s | FileCheck %s --check-prefix=ALIGN16
+// RUN: %clang_cc1 -triple i386-apple-darwin        -emit-llvm < %s | FileCheck %s --check-prefix=ALIGN16
+// RUN: %clang_cc1 -triple i386-unknown-linux-gnu   -emit-llvm < %s | FileCheck %s --check-prefix=ALIGN8
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fno-builtin-malloc  -emit-llvm < %s  | FileCheck %s --check-prefix=NOBUILTIN-MALLOC
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fno-builtin-calloc  -emit-llvm < %s  | FileCheck %s --check-prefix=NOBUILTIN-CALLOC
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fno-builtin-realloc -emit-llvm < %s  | FileCheck %s --check-prefix=NOBUILTIN-REALLOC
+
+typedef __SIZE_TYPE__ size_t;
+
+void *malloc(size_t);
+void *calloc(size_t, size_t);
+void *realloc(void *, size_t);
+
+void *malloc_test(size_t n) {
+  return malloc(n);
+}
+
+void *calloc_test(size_t n) {
+  return calloc(1, n);
+}
+
+void *raalloc_test(void *p, size_t n) {
+  return realloc(p, n);
+}
+
+// ALIGN16: align 16 i8* @malloc
+// ALIGN16: align 16 i8* @calloc
+// ALIGN16: align 16 i8* @realloc
+// ALIGN8: align 8 i8* @malloc
+// ALIGN8: align 8 i8* @calloc
+// ALIGN8: align 8 i8* @realloc
+// NOBUILTIN-MALLOC: declare i8* @malloc
+// NOBUILTIN-CALLOC: declare i8* @calloc
+// NOBUILTIN-REALLOC: declare i8* @realloc


        


More information about the cfe-commits mailing list