Skip to content

Commit 6aea8b4

Browse files
author
Mark Seaborn
committed
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
1 parent 2357705 commit 6aea8b4

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

lib/Transforms/NaCl/FlattenGlobals.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -255,8 +255,8 @@ bool FlattenGlobals::runOnModule(Module &M) {
255255
continue;
256256
Modified = true;
257257

258-
uint64_t Size = DL.getTypeAllocSize(
259-
Global->getType()->getPointerElementType());
258+
Type *GlobalType = Global->getType()->getPointerElementType();
259+
uint64_t Size = DL.getTypeAllocSize(GlobalType);
260260
Constant *NewInit;
261261
Type *NewType;
262262
if (Global->hasInitializer()) {
@@ -281,6 +281,8 @@ bool FlattenGlobals::runOnModule(Module &M) {
281281
NewInit, "", Global,
282282
Global->getThreadLocalMode());
283283
NewGlobal->copyAttributesFrom(Global);
284+
if (NewGlobal->getAlignment() == 0)
285+
NewGlobal->setAlignment(DL.getPrefTypeAlignment(GlobalType));
284286
NewGlobal->setExternallyInitialized(Global->isExternallyInitialized());
285287
NewGlobal->takeName(Global);
286288
Global->replaceAllUsesWith(

test/Transforms/NaCl/flatten-globals.ll

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,26 @@ target datalayout = "p:32:32:32"
9191
; CHECK: @aligned_var = global [4 x i8] c"\04\01\00\00", align 8
9292

9393

94+
; Check alignment handling
95+
96+
@implicit_alignment_i32 = global i32 zeroinitializer
97+
; CHECK: @implicit_alignment_i32 = global [4 x i8] zeroinitializer, align 4
98+
99+
@implicit_alignment_double = global double zeroinitializer
100+
; CHECK: @implicit_alignment_double = global [8 x i8] zeroinitializer, align 8
101+
102+
; FlattenGlobals is not allowed to increase the alignment of the
103+
; variable when an explicit section is specified (although PNaCl does
104+
; not support this attribute).
105+
@lower_alignment_section = global i32 0, section "mysection", align 1
106+
; CHECK: @lower_alignment_section = global [4 x i8] zeroinitializer, section "mysection", align 1
107+
108+
; FlattenGlobals could increase the alignment when no section is
109+
; specified, but it does not.
110+
@lower_alignment = global i32 0, align 1
111+
; CHECK: @lower_alignment = global [4 x i8] zeroinitializer, align 1
112+
113+
94114
; Check handling of global references
95115

96116
@var1 = external global i32

0 commit comments

Comments
 (0)