From: David Majnemer Date: Thu, 27 Oct 2016 17:18:24 +0000 (+0000) Subject: [CodeGen] Provide an appropriate alignment for dynamic allocas X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=977531dd061b37a20ac260ee3db2a3a6187dac70;p=clang [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 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@285316 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/CodeGen/CGBuiltin.cpp b/lib/CodeGen/CGBuiltin.cpp index 5bbc9ea534..c73a6e1b06 100644 --- a/lib/CodeGen/CGBuiltin.cpp +++ b/lib/CodeGen/CGBuiltin.cpp @@ -1139,7 +1139,13 @@ RValue CodeGenFunction::EmitBuiltinExpr(const FunctionDecl *FD, 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: { diff --git a/test/CodeGen/builtins-ms.c b/test/CodeGen/builtins-ms.c index 0676e9df7a..31c0331c56 100644 --- a/test/CodeGen/builtins-ms.c +++ b/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]]) }