From 1878da43ea929093e9b843f375fe106b9323d4f5 Mon Sep 17 00:00:00 2001 From: David Majnemer Date: Thu, 27 Oct 2016 17:18:24 +0000 Subject: [PATCH] [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 llvm-svn: 285316 --- clang/lib/CodeGen/CGBuiltin.cpp | 8 +++++++- clang/test/CodeGen/builtins-ms.c | 2 +- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/clang/lib/CodeGen/CGBuiltin.cpp b/clang/lib/CodeGen/CGBuiltin.cpp index 5bbc9ea53413..c73a6e1b061e 100644 --- a/clang/lib/CodeGen/CGBuiltin.cpp +++ b/clang/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/clang/test/CodeGen/builtins-ms.c b/clang/test/CodeGen/builtins-ms.c index 0676e9df7a7d..31c0331c56d2 100644 --- a/clang/test/CodeGen/builtins-ms.c +++ b/clang/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]]) } -- 2.34.1