Skip to content

Commit e647b4f

Browse files
committed
[reland][Alignment][NFC] Use the Align type in MCSection
Differential Revision: https://reviews.llvm.org/D138653
1 parent c418dcc commit e647b4f

18 files changed

+64
-69
lines changed

bolt/lib/Core/BinaryEmitter.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ bool BinaryEmitter::emitFunction(BinaryFunction &Function,
300300
// Set section alignment to at least maximum possible object alignment.
301301
// We need this to support LongJmp and other passes that calculates
302302
// tentative layout.
303-
if (Section->getAlignment() < opts::AlignFunctions)
303+
if (Section->getAlign() < opts::AlignFunctions)
304304
Section->setAlignment(Align(opts::AlignFunctions));
305305

306306
Streamer.emitCodeAlignment(BinaryFunction::MinAlign, &*BC.STI);

llvm/include/llvm/MC/MCSection.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ class MCSection {
137137
MCSymbol *getEndSymbol(MCContext &Ctx);
138138
bool hasEnded() const;
139139

140-
unsigned getAlignment() const { return Alignment.value(); }
140+
Align getAlign() const { return Alignment; }
141141
void setAlignment(Align Value) { Alignment = Value; }
142142

143143
unsigned getOrdinal() const { return Ordinal; }

llvm/lib/MC/ELFObjectWriter.cpp

+19-18
Original file line numberDiff line numberDiff line change
@@ -142,11 +142,11 @@ struct ELFWriter {
142142
bool is64Bit() const;
143143
bool usesRela(const MCSectionELF &Sec) const;
144144

145-
uint64_t align(unsigned Alignment);
145+
uint64_t align(Align Alignment);
146146

147147
bool maybeWriteCompression(uint32_t ChType, uint64_t Size,
148148
SmallVectorImpl<uint8_t> &CompressedContents,
149-
unsigned Alignment);
149+
Align Alignment);
150150

151151
public:
152152
ELFWriter(ELFObjectWriter &OWriter, raw_pwrite_stream &OS,
@@ -201,7 +201,7 @@ struct ELFWriter {
201201

202202
void WriteSecHdrEntry(uint32_t Name, uint32_t Type, uint64_t Flags,
203203
uint64_t Address, uint64_t Offset, uint64_t Size,
204-
uint32_t Link, uint32_t Info, uint64_t Alignment,
204+
uint32_t Link, uint32_t Info, MaybeAlign Alignment,
205205
uint64_t EntrySize);
206206

207207
void writeRelocations(const MCAssembler &Asm, const MCSectionELF &Sec);
@@ -317,8 +317,9 @@ class ELFDwoObjectWriter : public ELFObjectWriter {
317317

318318
} // end anonymous namespace
319319

320-
uint64_t ELFWriter::align(unsigned Alignment) {
321-
uint64_t Offset = W.OS.tell(), NewOffset = alignTo(Offset, Alignment);
320+
uint64_t ELFWriter::align(Align Alignment) {
321+
uint64_t Offset = W.OS.tell();
322+
uint64_t NewOffset = alignTo(Offset, Alignment);
322323
W.OS.write_zeros(NewOffset - Offset);
323324
return NewOffset;
324325
}
@@ -622,7 +623,7 @@ void ELFWriter::computeSymbolTable(
622623
SymtabSection->setAlignment(is64Bit() ? Align(8) : Align(4));
623624
SymbolTableIndex = addToSectionTable(SymtabSection);
624625

625-
uint64_t SecStart = align(SymtabSection->getAlignment());
626+
uint64_t SecStart = align(SymtabSection->getAlign());
626627

627628
// The first entry is the undefined symbol entry.
628629
Writer.writeSymbol(0, 0, 0, 0, 0, 0, false);
@@ -820,7 +821,7 @@ MCSectionELF *ELFWriter::createRelocationSection(MCContext &Ctx,
820821
// Include the debug info compression header.
821822
bool ELFWriter::maybeWriteCompression(
822823
uint32_t ChType, uint64_t Size,
823-
SmallVectorImpl<uint8_t> &CompressedContents, unsigned Alignment) {
824+
SmallVectorImpl<uint8_t> &CompressedContents, Align Alignment) {
824825
uint64_t HdrSize =
825826
is64Bit() ? sizeof(ELF::Elf32_Chdr) : sizeof(ELF::Elf64_Chdr);
826827
if (Size <= HdrSize + CompressedContents.size())
@@ -831,12 +832,12 @@ bool ELFWriter::maybeWriteCompression(
831832
write(static_cast<ELF::Elf64_Word>(ChType));
832833
write(static_cast<ELF::Elf64_Word>(0)); // ch_reserved field.
833834
write(static_cast<ELF::Elf64_Xword>(Size));
834-
write(static_cast<ELF::Elf64_Xword>(Alignment));
835+
write(static_cast<ELF::Elf64_Xword>(Alignment.value()));
835836
} else {
836837
// Write Elf32_Chdr header otherwise.
837838
write(static_cast<ELF::Elf32_Word>(ChType));
838839
write(static_cast<ELF::Elf32_Word>(Size));
839-
write(static_cast<ELF::Elf32_Word>(Alignment));
840+
write(static_cast<ELF::Elf32_Word>(Alignment.value()));
840841
}
841842
return true;
842843
}
@@ -878,7 +879,7 @@ void ELFWriter::writeSectionData(const MCAssembler &Asm, MCSection &Sec,
878879
compression::compress(compression::Params(CompressionType), Uncompressed,
879880
Compressed);
880881
if (!maybeWriteCompression(ChType, UncompressedData.size(), Compressed,
881-
Sec.getAlignment())) {
882+
Sec.getAlign())) {
882883
W.OS << UncompressedData;
883884
return;
884885
}
@@ -893,7 +894,7 @@ void ELFWriter::writeSectionData(const MCAssembler &Asm, MCSection &Sec,
893894
void ELFWriter::WriteSecHdrEntry(uint32_t Name, uint32_t Type, uint64_t Flags,
894895
uint64_t Address, uint64_t Offset,
895896
uint64_t Size, uint32_t Link, uint32_t Info,
896-
uint64_t Alignment, uint64_t EntrySize) {
897+
MaybeAlign Alignment, uint64_t EntrySize) {
897898
W.write<uint32_t>(Name); // sh_name: index into string table
898899
W.write<uint32_t>(Type); // sh_type
899900
WriteWord(Flags); // sh_flags
@@ -902,7 +903,7 @@ void ELFWriter::WriteSecHdrEntry(uint32_t Name, uint32_t Type, uint64_t Flags,
902903
WriteWord(Size); // sh_size
903904
W.write<uint32_t>(Link); // sh_link
904905
W.write<uint32_t>(Info); // sh_info
905-
WriteWord(Alignment); // sh_addralign
906+
WriteWord(Alignment ? Alignment->value() : 0); // sh_addralign
906907
WriteWord(EntrySize); // sh_entsize
907908
}
908909

@@ -1024,7 +1025,7 @@ void ELFWriter::writeSection(const SectionIndexMapTy &SectionIndexMap,
10241025

10251026
WriteSecHdrEntry(StrTabBuilder.getOffset(Section.getName()),
10261027
Section.getType(), Section.getFlags(), 0, Offset, Size,
1027-
sh_link, sh_info, Section.getAlignment(),
1028+
sh_link, sh_info, Section.getAlign(),
10281029
Section.getEntrySize());
10291030
}
10301031

@@ -1036,7 +1037,7 @@ void ELFWriter::writeSectionHeader(
10361037
// Null section first.
10371038
uint64_t FirstSectionSize =
10381039
(NumSections + 1) >= ELF::SHN_LORESERVE ? NumSections + 1 : 0;
1039-
WriteSecHdrEntry(0, 0, 0, 0, 0, FirstSectionSize, 0, 0, 0, 0);
1040+
WriteSecHdrEntry(0, 0, 0, 0, 0, FirstSectionSize, 0, 0, None, 0);
10401041

10411042
for (const MCSectionELF *Section : SectionTable) {
10421043
uint32_t GroupSymbolIndex;
@@ -1087,7 +1088,7 @@ uint64_t ELFWriter::writeObject(MCAssembler &Asm, const MCAsmLayout &Layout) {
10871088
continue;
10881089

10891090
// Remember the offset into the file for this section.
1090-
const uint64_t SecStart = align(Section.getAlignment());
1091+
const uint64_t SecStart = align(Section.getAlign());
10911092

10921093
const MCSymbolELF *SignatureSymbol = Section.getGroup();
10931094
writeSectionData(Asm, Section, Layout);
@@ -1124,7 +1125,7 @@ uint64_t ELFWriter::writeObject(MCAssembler &Asm, const MCAsmLayout &Layout) {
11241125

11251126
for (MCSectionELF *Group : Groups) {
11261127
// Remember the offset into the file for this section.
1127-
const uint64_t SecStart = align(Group->getAlignment());
1128+
const uint64_t SecStart = align(Group->getAlign());
11281129

11291130
const MCSymbol *SignatureSymbol = Group->getGroup();
11301131
assert(SignatureSymbol);
@@ -1156,7 +1157,7 @@ uint64_t ELFWriter::writeObject(MCAssembler &Asm, const MCAsmLayout &Layout) {
11561157

11571158
for (MCSectionELF *RelSection : Relocations) {
11581159
// Remember the offset into the file for this section.
1159-
const uint64_t SecStart = align(RelSection->getAlignment());
1160+
const uint64_t SecStart = align(RelSection->getAlign());
11601161

11611162
writeRelocations(Asm,
11621163
cast<MCSectionELF>(*RelSection->getLinkedToSection()));
@@ -1179,7 +1180,7 @@ uint64_t ELFWriter::writeObject(MCAssembler &Asm, const MCAsmLayout &Layout) {
11791180
SectionOffsets[StrtabSection] = std::make_pair(SecStart, W.OS.tell());
11801181
}
11811182

1182-
const uint64_t SectionHeaderOffset = align(is64Bit() ? 8 : 4);
1183+
const uint64_t SectionHeaderOffset = align(is64Bit() ? Align(8) : Align(4));
11831184

11841185
// ... then the section header table ...
11851186
writeSectionHeader(Layout, SectionIndexMap, SectionOffsets);

llvm/lib/MC/MCELFStreamer.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ void MCELFStreamer::emitAssemblerFlag(MCAssemblerFlag Flag) {
140140
static void setSectionAlignmentForBundling(const MCAssembler &Assembler,
141141
MCSection *Section) {
142142
if (Section && Assembler.isBundlingEnabled() && Section->hasInstructions() &&
143-
Section->getAlignment() < Assembler.getBundleAlignSize())
143+
Section->getAlign() < Assembler.getBundleAlignSize())
144144
Section->setAlignment(Align(Assembler.getBundleAlignSize()));
145145
}
146146

llvm/lib/MC/MCObjectStreamer.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -652,7 +652,7 @@ void MCObjectStreamer::emitValueToAlignment(unsigned ByteAlignment,
652652

653653
// Update the maximum alignment on the current section if necessary.
654654
MCSection *CurSec = getCurrentSectionOnly();
655-
if (ByteAlignment > CurSec->getAlignment())
655+
if (CurSec->getAlign() < ByteAlignment)
656656
CurSec->setAlignment(Align(ByteAlignment));
657657
}
658658

llvm/lib/MC/MCSectionXCOFF.cpp

+1-2
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@ using namespace llvm;
2020
MCSectionXCOFF::~MCSectionXCOFF() = default;
2121

2222
void MCSectionXCOFF::printCsectDirective(raw_ostream &OS) const {
23-
OS << "\t.csect " << QualName->getName() << "," << Log2_32(getAlignment())
24-
<< '\n';
23+
OS << "\t.csect " << QualName->getName() << "," << Log2(getAlign()) << '\n';
2524
}
2625

2726
void MCSectionXCOFF::printSwitchToSection(const MCAsmInfo &MAI, const Triple &T,

llvm/lib/MC/MCWinCOFFStreamer.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ void MCWinCOFFStreamer::emitCOFFSafeSEH(MCSymbol const *Symbol) {
190190

191191
MCSection *SXData = getContext().getObjectFileInfo()->getSXDataSection();
192192
getAssembler().registerSection(*SXData);
193-
if (SXData->getAlignment() < 4)
193+
if (SXData->getAlign() < 4)
194194
SXData->setAlignment(Align(4));
195195

196196
new MCSymbolIdFragment(Symbol, SXData);
@@ -207,7 +207,7 @@ void MCWinCOFFStreamer::emitCOFFSafeSEH(MCSymbol const *Symbol) {
207207
void MCWinCOFFStreamer::emitCOFFSymbolIndex(MCSymbol const *Symbol) {
208208
MCSection *Sec = getCurrentSectionOnly();
209209
getAssembler().registerSection(*Sec);
210-
if (Sec->getAlignment() < 4)
210+
if (Sec->getAlign() < 4)
211211
Sec->setAlignment(Align(4));
212212

213213
new MCSymbolIdFragment(Symbol, getCurrentSectionOnly());

llvm/lib/MC/MachObjectWriter.cpp

+3-4
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ uint64_t MachObjectWriter::getPaddingSize(const MCSection *Sec,
129129
const MCSection &NextSec = *Layout.getSectionOrder()[Next];
130130
if (NextSec.isVirtualSection())
131131
return 0;
132-
return offsetToAlignment(EndAddr, Align(NextSec.getAlignment()));
132+
return offsetToAlignment(EndAddr, NextSec.getAlign());
133133
}
134134

135135
void MachObjectWriter::writeHeader(MachO::HeaderFileType Type,
@@ -244,8 +244,7 @@ void MachObjectWriter::writeSection(const MCAsmLayout &Layout,
244244
}
245245
W.write<uint32_t>(FileOffset);
246246

247-
assert(isPowerOf2_32(Section.getAlignment()) && "Invalid alignment!");
248-
W.write<uint32_t>(Log2_32(Section.getAlignment()));
247+
W.write<uint32_t>(Log2(Section.getAlign()));
249248
W.write<uint32_t>(NumRelocations ? RelocationsStart : 0);
250249
W.write<uint32_t>(NumRelocations);
251250
W.write<uint32_t>(Flags);
@@ -645,7 +644,7 @@ void MachObjectWriter::computeSectionAddresses(const MCAssembler &Asm,
645644
const MCAsmLayout &Layout) {
646645
uint64_t StartAddress = 0;
647646
for (const MCSection *Sec : Layout.getSectionOrder()) {
648-
StartAddress = alignTo(StartAddress, Sec->getAlignment());
647+
StartAddress = alignTo(StartAddress, Sec->getAlign());
649648
SectionAddress[Sec] = StartAddress;
650649
StartAddress += Layout.getSectionAddressSize(Sec);
651650

llvm/lib/MC/WasmObjectWriter.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -716,7 +716,7 @@ static void addData(SmallVectorImpl<char> &DataBytes,
716716
MCSectionWasm &DataSection) {
717717
LLVM_DEBUG(errs() << "addData: " << DataSection.getName() << "\n");
718718

719-
DataBytes.resize(alignTo(DataBytes.size(), DataSection.getAlignment()));
719+
DataBytes.resize(alignTo(DataBytes.size(), DataSection.getAlign()));
720720

721721
for (const MCFragment &Frag : DataSection) {
722722
if (Frag.hasInstructions())
@@ -1498,7 +1498,7 @@ uint64_t WasmObjectWriter::writeOneObject(MCAssembler &Asm,
14981498

14991499
if (Section.isWasmData()) {
15001500
uint32_t SegmentIndex = DataSegments.size();
1501-
DataSize = alignTo(DataSize, Section.getAlignment());
1501+
DataSize = alignTo(DataSize, Section.getAlign());
15021502
DataSegments.emplace_back();
15031503
WasmDataSegment &Segment = DataSegments.back();
15041504
Segment.Name = SectionName;
@@ -1508,7 +1508,7 @@ uint64_t WasmObjectWriter::writeOneObject(MCAssembler &Asm,
15081508
Segment.Offset = DataSize;
15091509
Segment.Section = &Section;
15101510
addData(Segment.Data, Section);
1511-
Segment.Alignment = Log2_32(Section.getAlignment());
1511+
Segment.Alignment = Log2(Section.getAlign());
15121512
Segment.LinkingFlags = Section.getSegmentFlags();
15131513
DataSize += Segment.Data.size();
15141514
Section.setSegmentIndex(SegmentIndex);

llvm/lib/MC/WinCOFFObjectWriter.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ COFFSection *WinCOFFObjectWriter::createSection(StringRef Name) {
267267
}
268268

269269
static uint32_t getAlignment(const MCSectionCOFF &Sec) {
270-
switch (Sec.getAlignment()) {
270+
switch (Sec.getAlign().value()) {
271271
case 1:
272272
return COFF::IMAGE_SCN_ALIGN_1BYTES;
273273
case 2:

llvm/lib/MC/XCOFFObjectWriter.cpp

+4-6
Original file line numberDiff line numberDiff line change
@@ -1192,7 +1192,7 @@ void XCOFFObjectWriter::assignAddressesAndIndices(const MCAsmLayout &Layout) {
11921192

11931193
for (auto &Csect : *Group) {
11941194
const MCSectionXCOFF *MCSec = Csect.MCSec;
1195-
Csect.Address = alignTo(Address, MCSec->getAlignment());
1195+
Csect.Address = alignTo(Address, MCSec->getAlign());
11961196
Csect.Size = Layout.getSectionAddressSize(MCSec);
11971197
Address = Csect.Address + Csect.Size;
11981198
Csect.SymbolTableIndex = SymbolTableIndex;
@@ -1247,7 +1247,7 @@ void XCOFFObjectWriter::assignAddressesAndIndices(const MCAsmLayout &Layout) {
12471247
if (!DwarfSections.empty())
12481248
PaddingsBeforeDwarf =
12491249
alignTo(Address,
1250-
(*DwarfSections.begin()).DwarfSect->MCSec->getAlignment()) -
1250+
(*DwarfSections.begin()).DwarfSect->MCSec->getAlign()) -
12511251
Address;
12521252

12531253
DwarfSectionEntry *LastDwarfSection = nullptr;
@@ -1273,7 +1273,7 @@ void XCOFFObjectWriter::assignAddressesAndIndices(const MCAsmLayout &Layout) {
12731273
// This address is used to tell where is the section in the final object.
12741274
// See writeSectionForDwarfSectionEntry().
12751275
DwarfSection.Address = DwarfSect.Address =
1276-
alignTo(Address, MCSec->getAlignment());
1276+
alignTo(Address, MCSec->getAlign());
12771277

12781278
// Section size.
12791279
// For DWARF section, we must use the real size which may be not aligned.
@@ -1448,9 +1448,7 @@ void XCOFFObjectWriter::writeSectionForExceptionSectionEntry(
14481448
// significant bits of a byte, then or's in the csect type into the least
14491449
// significant 3 bits.
14501450
uint8_t getEncodedType(const MCSectionXCOFF *Sec) {
1451-
unsigned Align = Sec->getAlignment();
1452-
assert(isPowerOf2_32(Align) && "Alignment must be a power of 2.");
1453-
unsigned Log2Align = Log2_32(Align);
1451+
unsigned Log2Align = Log2(Sec->getAlign());
14541452
// Result is a number in the range [0, 31] which fits in the 5 least
14551453
// significant bits. Shift this value into the 5 most significant bits, and
14561454
// bitwise-or in the csect type.

llvm/lib/Target/AMDGPU/AMDGPUAsmPrinter.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ void AMDGPUAsmPrinter::emitFunctionBodyEnd() {
253253
// CP microcode requires the kernel descriptor to be allocated on 64 byte
254254
// alignment.
255255
Streamer.emitValueToAlignment(64, 0, 1, 0);
256-
if (ReadOnlySection.getAlignment() < 64)
256+
if (ReadOnlySection.getAlign() < 64)
257257
ReadOnlySection.setAlignment(Align(64));
258258

259259
const GCNSubtarget &STM = MF->getSubtarget<GCNSubtarget>();

llvm/lib/Target/Hexagon/MCTargetDesc/HexagonMCELFStreamer.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ void HexagonMCELFStreamer::HexagonMCEmitCommonSymbol(MCSymbol *Symbol,
117117
}
118118

119119
// Update the maximum alignment of the section if necessary.
120-
if (Align(ByteAlignment) > Section.getAlignment())
120+
if (Section.getAlign() < ByteAlignment)
121121
Section.setAlignment(Align(ByteAlignment));
122122

123123
switchSection(P.first, P.second);

llvm/lib/Target/Mips/MCTargetDesc/MipsTargetStreamer.cpp

+9-11
Original file line numberDiff line numberDiff line change
@@ -899,9 +899,9 @@ void MipsTargetELFStreamer::finish() {
899899
MCSection &BSSSection = *OFI.getBSSSection();
900900
MCA.registerSection(BSSSection);
901901

902-
TextSection.setAlignment(Align(std::max(16u, TextSection.getAlignment())));
903-
DataSection.setAlignment(Align(std::max(16u, DataSection.getAlignment())));
904-
BSSSection.setAlignment(Align(std::max(16u, BSSSection.getAlignment())));
902+
TextSection.setAlignment(std::max(Align(16), TextSection.getAlign()));
903+
DataSection.setAlignment(std::max(Align(16), DataSection.getAlign()));
904+
BSSSection.setAlignment(std::max(Align(16), BSSSection.getAlign()));
905905

906906
if (RoundSectionSizes) {
907907
// Make sections sizes a multiple of the alignment. This is useful for
@@ -912,14 +912,12 @@ void MipsTargetELFStreamer::finish() {
912912
for (MCSection &S : MCA) {
913913
MCSectionELF &Section = static_cast<MCSectionELF &>(S);
914914

915-
unsigned Alignment = Section.getAlignment();
916-
if (Alignment) {
917-
OS.switchSection(&Section);
918-
if (Section.useCodeAlign())
919-
OS.emitCodeAlignment(Alignment, &STI, Alignment);
920-
else
921-
OS.emitValueToAlignment(Alignment, 0, 1, Alignment);
922-
}
915+
Align Alignment = Section.getAlign();
916+
OS.switchSection(&Section);
917+
if (Section.useCodeAlign())
918+
OS.emitCodeAlignment(Alignment.value(), &STI, Alignment.value());
919+
else
920+
OS.emitValueToAlignment(Alignment.value(), 0, 1, Alignment.value());
923921
}
924922
}
925923

llvm/lib/Target/PowerPC/PPCAsmPrinter.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -2589,7 +2589,7 @@ bool PPCAIXAsmPrinter::doInitialization(Module &M) {
25892589
getObjFileLowering().SectionForGlobal(GO, GOKind, TM));
25902590

25912591
Align GOAlign = getGVAlignment(GO, GO->getParent()->getDataLayout());
2592-
if (GOAlign > Csect->getAlignment())
2592+
if (GOAlign > Csect->getAlign())
25932593
Csect->setAlignment(GOAlign);
25942594
};
25952595

llvm/lib/Target/X86/MCTargetDesc/X86AsmBackend.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -584,7 +584,7 @@ void X86AsmBackend::emitInstructionEnd(MCObjectStreamer &OS, const MCInst &Inst)
584584

585585
// Update the maximum alignment on the current section if necessary.
586586
MCSection *Sec = OS.getCurrentSectionOnly();
587-
if (AlignBoundary.value() > Sec->getAlignment())
587+
if (Sec->getAlign() < AlignBoundary)
588588
Sec->setAlignment(AlignBoundary);
589589
}
590590

0 commit comments

Comments
 (0)