[PATCH] D24378: [CodeGen] Provide an appropriate alignment for dynamic allocas

David Majnemer via cfe-commits cfe-commits at lists.llvm.org
Thu Sep 8 23:57:09 PDT 2016


majnemer created this revision.
majnemer added reviewers: rnk, rsmith, efriedma, chandlerc.
majnemer added a subscriber: cfe-commits.

GCC documents __builtin_alloca as aligning the storage to at least
__BIGGEST_ALIGNMENT__.

MSVC documents essentially the same for the x64 ABI:
https://msdn.microsoft.com/en-us/library/x9sx5da1.aspx

The 32-bit ABI follows the same rule: it emits a call to _alloca_probe_16

https://reviews.llvm.org/D24378

Files:
  lib/CodeGen/CGBuiltin.cpp
  test/CodeGen/builtins-ms.c

Index: test/CodeGen/builtins-ms.c
===================================================================
--- test/CodeGen/builtins-ms.c
+++ test/CodeGen/builtins-ms.c
@@ -4,6 +4,6 @@
 void capture(void *);
 void test_alloca(int n) {
   capture(_alloca(n));
-  // CHECK: %[[arg:.*]] = alloca i8, i32 %
+  // CHECK: %[[arg:.*]] = alloca i8, i32 %{{.*}}, align 16
   // CHECK: call void @capture(i8* %[[arg]])
 }
Index: lib/CodeGen/CGBuiltin.cpp
===================================================================
--- lib/CodeGen/CGBuiltin.cpp
+++ lib/CodeGen/CGBuiltin.cpp
@@ -1015,7 +1015,13 @@
   case Builtin::BI_alloca:
   case Builtin::BI__builtin_alloca: {
     Value *Size = EmitScalarExpr(E->getArg(0));
-    return RValue::get(Builder.CreateAlloca(Builder.getInt8Ty(), Size));
+    const TargetInfo &TI = getContext().getTargetInfo();
+    // The alignment of the alloca should correspond to __BIGGEST_ALIGNMENT__.
+    unsigned SuitableAlignmentInBytes =
+        TI.getSuitableAlign() / TI.getCharWidth();
+    AllocaInst *AI = Builder.CreateAlloca(Builder.getInt8Ty(), Size);
+    AI->setAlignment(SuitableAlignmentInBytes);
+    return RValue::get(AI);
   }
   case Builtin::BIbzero:
   case Builtin::BI__builtin_bzero: {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D24378.70789.patch
Type: text/x-patch
Size: 1230 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20160909/3b452761/attachment.bin>


More information about the cfe-commits mailing list