r285316 - [CodeGen] Provide an appropriate alignment for dynamic allocas

David Majnemer via cfe-commits cfe-commits at lists.llvm.org
Thu Oct 27 10:18:24 PDT 2016


Author: majnemer
Date: Thu Oct 27 12:18:24 2016
New Revision: 285316

URL: http://llvm.org/viewvc/llvm-project?rev=285316&view=rev
Log:
[CodeGen] Provide an appropriate alignment for dynamic allocas

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

Differential Revision: https://reviews.llvm.org/D24378

Modified:
    cfe/trunk/lib/CodeGen/CGBuiltin.cpp
    cfe/trunk/test/CodeGen/builtins-ms.c

Modified: cfe/trunk/lib/CodeGen/CGBuiltin.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGBuiltin.cpp?rev=285316&r1=285315&r2=285316&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/CGBuiltin.cpp (original)
+++ cfe/trunk/lib/CodeGen/CGBuiltin.cpp Thu Oct 27 12:18:24 2016
@@ -1139,7 +1139,13 @@ RValue CodeGenFunction::EmitBuiltinExpr(
   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: {

Modified: cfe/trunk/test/CodeGen/builtins-ms.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGen/builtins-ms.c?rev=285316&r1=285315&r2=285316&view=diff
==============================================================================
--- cfe/trunk/test/CodeGen/builtins-ms.c (original)
+++ cfe/trunk/test/CodeGen/builtins-ms.c Thu Oct 27 12:18:24 2016
@@ -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]])
 }




More information about the cfe-commits mailing list