From 174d9c26f14b5503082a4c90abdfe1ac00a3369b Mon Sep 17 00:00:00 2001 From: Eli Friedman Date: Thu, 29 May 2008 11:10:27 +0000 Subject: [PATCH] Add CodeGen support for alignment on globals, both for unusual natural alignment and alignment attributes. llvm-svn: 51676 --- clang/lib/CodeGen/CodeGenModule.cpp | 6 ++++++ clang/test/CodeGen/var-align.c | 4 ++++ 2 files changed, 10 insertions(+) create mode 100644 clang/test/CodeGen/var-align.c diff --git a/clang/lib/CodeGen/CodeGenModule.cpp b/clang/lib/CodeGen/CodeGenModule.cpp index 0b7d30ac5001..e5bd15e8566d 100644 --- a/clang/lib/CodeGen/CodeGenModule.cpp +++ b/clang/lib/CodeGen/CodeGenModule.cpp @@ -477,6 +477,12 @@ void CodeGenModule::EmitGlobalVarInit(const VarDecl *D) { "Initializer codegen type mismatch!"); GV->setInitializer(Init); + unsigned Align = Context.getTypeAlign(D->getType()); + if (const AlignedAttr* AA = D->getAttr()) { + Align = std::max(Align, AA->getAlignment()); + } + GV->setAlignment(Align / 8); + if (const VisibilityAttr *attr = D->getAttr()) setVisibility(GV, attr->getVisibility()); // FIXME: else handle -fvisibility diff --git a/clang/test/CodeGen/var-align.c b/clang/test/CodeGen/var-align.c new file mode 100644 index 000000000000..be585c052e8a --- /dev/null +++ b/clang/test/CodeGen/var-align.c @@ -0,0 +1,4 @@ +// RUN: clang -emit-llvm %s -o - | grep "align 16" | count 2 + +__attribute((aligned(16))) float a[128]; +union {int a[4]; __attribute((aligned(16))) float b[4];} u;