Skip to content
This repository was archived by the owner on Feb 5, 2019. It is now read-only.

Commit 47dc019

Browse files
author
Eli Friedman
committed
[NFC] Make getPreferredAlignment honor section markings.
This should more accurately reflect what the AsmPrinter will actually do. This is NFC, as far as I can tell; all the places that might be affected already have an extra check to avoid using the result of getPreferredAlignment in this situation. Differential Revision: https://reviews.llvm.org/D51377 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@340999 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent 649d287 commit 47dc019

File tree

2 files changed

+16
-11
lines changed

2 files changed

+16
-11
lines changed

lib/CodeGen/GlobalMerge.cpp

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -462,17 +462,8 @@ bool GlobalMerge::doMerge(const SmallVectorImpl<GlobalVariable *> &Globals,
462462
for (j = i; j != -1; j = GlobalSet.find_next(j)) {
463463
Type *Ty = Globals[j]->getValueType();
464464

465-
// Make sure we use the same alignment AsmPrinter would use. There
466-
// currently isn't any helper to compute that, so we compute it
467-
// explicitly here.
468-
//
469-
// getPreferredAlignment will sometimes return an alignment higher
470-
// than the explicitly specified alignment; we must ignore that
471-
// if the section is explicitly specified, to avoid inserting extra
472-
// padding into that section.
465+
// Make sure we use the same alignment AsmPrinter would use.
473466
unsigned Align = DL.getPreferredAlignment(Globals[j]);
474-
if (Globals[j]->hasSection() && Globals[j]->getAlignment())
475-
Align = Globals[j]->getAlignment();
476467
unsigned Padding = alignTo(MergedSize, Align) - MergedSize;
477468
MergedSize += Padding;
478469
MergedSize += DL.getTypeAllocSize(Ty);

lib/IR/DataLayout.cpp

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -808,15 +808,29 @@ int64_t DataLayout::getIndexedOffsetInType(Type *ElemTy,
808808
/// global. This includes an explicitly requested alignment (if the global
809809
/// has one).
810810
unsigned DataLayout::getPreferredAlignment(const GlobalVariable *GV) const {
811+
unsigned GVAlignment = GV->getAlignment();
812+
// If a section is specified, always precisely honor explicit alignment,
813+
// so we don't insert padding into a section we don't control.
814+
if (GVAlignment && GV->hasSection())
815+
return GVAlignment;
816+
817+
// If no explicit alignment is specified, compute the alignment based on
818+
// the IR type. If an alignment is specified, increase it to match the ABI
819+
// alignment of the IR type.
820+
//
821+
// FIXME: Not sure it makes sense to use the alignment of the type if
822+
// there's already an explicit alignment specification.
811823
Type *ElemType = GV->getValueType();
812824
unsigned Alignment = getPrefTypeAlignment(ElemType);
813-
unsigned GVAlignment = GV->getAlignment();
814825
if (GVAlignment >= Alignment) {
815826
Alignment = GVAlignment;
816827
} else if (GVAlignment != 0) {
817828
Alignment = std::max(GVAlignment, getABITypeAlignment(ElemType));
818829
}
819830

831+
// If no explicit alignment is specified, and the global is large, increase
832+
// the alignment to 16.
833+
// FIXME: Why 16, specifically?
820834
if (GV->hasInitializer() && GVAlignment == 0) {
821835
if (Alignment < 16) {
822836
// If the global is not external, see if it is large. If so, give it a

0 commit comments

Comments
 (0)