diff options
author | Mark Seaborn <mseaborn@chromium.org> | 2013-05-20 09:54:58 -0700 |
---|---|---|
committer | Mark Seaborn <mseaborn@chromium.org> | 2013-05-20 09:54:58 -0700 |
commit | 6aea8b4a69a0e6c9c8907a94298efb40c8cd9e87 (patch) | |
tree | 7842d7df3d78e6c6f58216f36c78237459e2a69d | |
parent | 23577055b156b53520c138fc269bc558f5bb115e (diff) |
PNaCl: Fix FlattenGlobals to correctly handle implicitly-aligned variables
If a global variable has no "align" attribute, it must be aligned
based on its type.
BUG=https://code.google.com/p/nativeclient/issues/detail?id=3437
TEST=flatten-globals.ll
Review URL: https://codereview.chromium.org/15359006
-rw-r--r-- | lib/Transforms/NaCl/FlattenGlobals.cpp | 6 | ||||
-rw-r--r-- | test/Transforms/NaCl/flatten-globals.ll | 20 |
2 files changed, 24 insertions, 2 deletions
diff --git a/lib/Transforms/NaCl/FlattenGlobals.cpp b/lib/Transforms/NaCl/FlattenGlobals.cpp index 86a46b5d30..77ce2b36b4 100644 --- a/lib/Transforms/NaCl/FlattenGlobals.cpp +++ b/lib/Transforms/NaCl/FlattenGlobals.cpp @@ -255,8 +255,8 @@ bool FlattenGlobals::runOnModule(Module &M) { continue; Modified = true; - uint64_t Size = DL.getTypeAllocSize( - Global->getType()->getPointerElementType()); + Type *GlobalType = Global->getType()->getPointerElementType(); + uint64_t Size = DL.getTypeAllocSize(GlobalType); Constant *NewInit; Type *NewType; if (Global->hasInitializer()) { @@ -281,6 +281,8 @@ bool FlattenGlobals::runOnModule(Module &M) { NewInit, "", Global, Global->getThreadLocalMode()); NewGlobal->copyAttributesFrom(Global); + if (NewGlobal->getAlignment() == 0) + NewGlobal->setAlignment(DL.getPrefTypeAlignment(GlobalType)); NewGlobal->setExternallyInitialized(Global->isExternallyInitialized()); NewGlobal->takeName(Global); Global->replaceAllUsesWith( diff --git a/test/Transforms/NaCl/flatten-globals.ll b/test/Transforms/NaCl/flatten-globals.ll index 4ac96d6a7c..938f11fec7 100644 --- a/test/Transforms/NaCl/flatten-globals.ll +++ b/test/Transforms/NaCl/flatten-globals.ll @@ -91,6 +91,26 @@ target datalayout = "p:32:32:32" ; CHECK: @aligned_var = global [4 x i8] c"\04\01\00\00", align 8 +; Check alignment handling + +@implicit_alignment_i32 = global i32 zeroinitializer +; CHECK: @implicit_alignment_i32 = global [4 x i8] zeroinitializer, align 4 + +@implicit_alignment_double = global double zeroinitializer +; CHECK: @implicit_alignment_double = global [8 x i8] zeroinitializer, align 8 + +; FlattenGlobals is not allowed to increase the alignment of the +; variable when an explicit section is specified (although PNaCl does +; not support this attribute). +@lower_alignment_section = global i32 0, section "mysection", align 1 +; CHECK: @lower_alignment_section = global [4 x i8] zeroinitializer, section "mysection", align 1 + +; FlattenGlobals could increase the alignment when no section is +; specified, but it does not. +@lower_alignment = global i32 0, align 1 +; CHECK: @lower_alignment = global [4 x i8] zeroinitializer, align 1 + + ; Check handling of global references @var1 = external global i32 |