Skip to content

Commit 3467f9c

Browse files
committed
Revert D138653 [Alignment][NFC] Use the Align type in MCSection"
This breaks the bolt project. This reverts commit 409f0dc.
1 parent 409f0dc commit 3467f9c

File tree

17 files changed

+68
-63
lines changed

17 files changed

+68
-63
lines changed

llvm/include/llvm/MC/MCSection.h

Lines changed: 1 addition & 1 deletion
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-
Align getAlign() const { return Alignment; }
140+
unsigned getAlignment() const { return Alignment.value(); }
141141
void setAlignment(Align Value) { Alignment = Value; }
142142

143143
unsigned getOrdinal() const { return Ordinal; }

llvm/lib/MC/ELFObjectWriter.cpp

Lines changed: 18 additions & 19 deletions
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(Align Alignment);
145+
uint64_t align(unsigned Alignment);
146146

147147
bool maybeWriteCompression(uint32_t ChType, uint64_t Size,
148148
SmallVectorImpl<uint8_t> &CompressedContents,
149-
Align Alignment);
149+
unsigned 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, MaybeAlign Alignment,
204+
uint32_t Link, uint32_t Info, uint64_t Alignment,
205205
uint64_t EntrySize);
206206

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

318318
} // end anonymous namespace
319319

320-
uint64_t ELFWriter::align(Align Alignment) {
321-
uint64_t Offset = W.OS.tell();
322-
uint64_t NewOffset = alignTo(Offset, Alignment);
320+
uint64_t ELFWriter::align(unsigned Alignment) {
321+
uint64_t Offset = W.OS.tell(), NewOffset = alignTo(Offset, Alignment);
323322
W.OS.write_zeros(NewOffset - Offset);
324323
return NewOffset;
325324
}
@@ -623,7 +622,7 @@ void ELFWriter::computeSymbolTable(
623622
SymtabSection->setAlignment(is64Bit() ? Align(8) : Align(4));
624623
SymbolTableIndex = addToSectionTable(SymtabSection);
625624

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

628627
// The first entry is the undefined symbol entry.
629628
Writer.writeSymbol(0, 0, 0, 0, 0, 0, false);
@@ -821,7 +820,7 @@ MCSectionELF *ELFWriter::createRelocationSection(MCContext &Ctx,
821820
// Include the debug info compression header.
822821
bool ELFWriter::maybeWriteCompression(
823822
uint32_t ChType, uint64_t Size,
824-
SmallVectorImpl<uint8_t> &CompressedContents, Align Alignment) {
823+
SmallVectorImpl<uint8_t> &CompressedContents, unsigned Alignment) {
825824
uint64_t HdrSize =
826825
is64Bit() ? sizeof(ELF::Elf32_Chdr) : sizeof(ELF::Elf64_Chdr);
827826
if (Size <= HdrSize + CompressedContents.size())
@@ -832,12 +831,12 @@ bool ELFWriter::maybeWriteCompression(
832831
write(static_cast<ELF::Elf64_Word>(ChType));
833832
write(static_cast<ELF::Elf64_Word>(0)); // ch_reserved field.
834833
write(static_cast<ELF::Elf64_Xword>(Size));
835-
write(static_cast<ELF::Elf64_Xword>(Alignment.value()));
834+
write(static_cast<ELF::Elf64_Xword>(Alignment));
836835
} else {
837836
// Write Elf32_Chdr header otherwise.
838837
write(static_cast<ELF::Elf32_Word>(ChType));
839838
write(static_cast<ELF::Elf32_Word>(Size));
840-
write(static_cast<ELF::Elf32_Word>(Alignment.value()));
839+
write(static_cast<ELF::Elf32_Word>(Alignment));
841840
}
842841
return true;
843842
}
@@ -879,7 +878,7 @@ void ELFWriter::writeSectionData(const MCAssembler &Asm, MCSection &Sec,
879878
compression::compress(compression::Params(CompressionType), Uncompressed,
880879
Compressed);
881880
if (!maybeWriteCompression(ChType, UncompressedData.size(), Compressed,
882-
Sec.getAlign())) {
881+
Sec.getAlignment())) {
883882
W.OS << UncompressedData;
884883
return;
885884
}
@@ -894,7 +893,7 @@ void ELFWriter::writeSectionData(const MCAssembler &Asm, MCSection &Sec,
894893
void ELFWriter::WriteSecHdrEntry(uint32_t Name, uint32_t Type, uint64_t Flags,
895894
uint64_t Address, uint64_t Offset,
896895
uint64_t Size, uint32_t Link, uint32_t Info,
897-
MaybeAlign Alignment, uint64_t EntrySize) {
896+
uint64_t Alignment, uint64_t EntrySize) {
898897
W.write<uint32_t>(Name); // sh_name: index into string table
899898
W.write<uint32_t>(Type); // sh_type
900899
WriteWord(Flags); // sh_flags
@@ -903,7 +902,7 @@ void ELFWriter::WriteSecHdrEntry(uint32_t Name, uint32_t Type, uint64_t Flags,
903902
WriteWord(Size); // sh_size
904903
W.write<uint32_t>(Link); // sh_link
905904
W.write<uint32_t>(Info); // sh_info
906-
WriteWord(Alignment ? Alignment->value() : 0); // sh_addralign
905+
WriteWord(Alignment); // sh_addralign
907906
WriteWord(EntrySize); // sh_entsize
908907
}
909908

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

10261025
WriteSecHdrEntry(StrTabBuilder.getOffset(Section.getName()),
10271026
Section.getType(), Section.getFlags(), 0, Offset, Size,
1028-
sh_link, sh_info, Section.getAlign(),
1027+
sh_link, sh_info, Section.getAlignment(),
10291028
Section.getEntrySize());
10301029
}
10311030

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

10421041
for (const MCSectionELF *Section : SectionTable) {
10431042
uint32_t GroupSymbolIndex;
@@ -1088,7 +1087,7 @@ uint64_t ELFWriter::writeObject(MCAssembler &Asm, const MCAsmLayout &Layout) {
10881087
continue;
10891088

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

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

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

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

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

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

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

11851184
// ... then the section header table ...
11861185
writeSectionHeader(Layout, SectionIndexMap, SectionOffsets);

llvm/lib/MC/MCELFStreamer.cpp

Lines changed: 1 addition & 1 deletion
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->getAlign() < Assembler.getBundleAlignSize())
143+
Section->getAlignment() < Assembler.getBundleAlignSize())
144144
Section->setAlignment(Align(Assembler.getBundleAlignSize()));
145145
}
146146

llvm/lib/MC/MCObjectStreamer.cpp

Lines changed: 1 addition & 1 deletion
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 (CurSec->getAlign() < ByteAlignment)
655+
if (ByteAlignment > CurSec->getAlignment())
656656
CurSec->setAlignment(Align(ByteAlignment));
657657
}
658658

llvm/lib/MC/MCSectionXCOFF.cpp

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

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

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

llvm/lib/MC/MCWinCOFFStreamer.cpp

Lines changed: 2 additions & 2 deletions
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->getAlign() < 4)
193+
if (SXData->getAlignment() < 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->getAlign() < 4)
210+
if (Sec->getAlignment() < 4)
211211
Sec->setAlignment(Align(4));
212212

213213
new MCSymbolIdFragment(Symbol, getCurrentSectionOnly());

llvm/lib/MC/MachObjectWriter.cpp

Lines changed: 4 additions & 3 deletions
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, NextSec.getAlign());
132+
return offsetToAlignment(EndAddr, Align(NextSec.getAlignment()));
133133
}
134134

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

247-
W.write<uint32_t>(Log2(Section.getAlign()));
247+
assert(isPowerOf2_32(Section.getAlignment()) && "Invalid alignment!");
248+
W.write<uint32_t>(Log2_32(Section.getAlignment()));
248249
W.write<uint32_t>(NumRelocations ? RelocationsStart : 0);
249250
W.write<uint32_t>(NumRelocations);
250251
W.write<uint32_t>(Flags);
@@ -644,7 +645,7 @@ void MachObjectWriter::computeSectionAddresses(const MCAssembler &Asm,
644645
const MCAsmLayout &Layout) {
645646
uint64_t StartAddress = 0;
646647
for (const MCSection *Sec : Layout.getSectionOrder()) {
647-
StartAddress = alignTo(StartAddress, Sec->getAlign());
648+
StartAddress = alignTo(StartAddress, Sec->getAlignment());
648649
SectionAddress[Sec] = StartAddress;
649650
StartAddress += Layout.getSectionAddressSize(Sec);
650651

llvm/lib/MC/WasmObjectWriter.cpp

Lines changed: 3 additions & 3 deletions
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.getAlign()));
719+
DataBytes.resize(alignTo(DataBytes.size(), DataSection.getAlignment()));
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.getAlign());
1501+
DataSize = alignTo(DataSize, Section.getAlignment());
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(Section.getAlign());
1511+
Segment.Alignment = Log2_32(Section.getAlignment());
15121512
Segment.LinkingFlags = Section.getSegmentFlags();
15131513
DataSize += Segment.Data.size();
15141514
Section.setSegmentIndex(SegmentIndex);

llvm/lib/MC/WinCOFFObjectWriter.cpp

Lines changed: 1 addition & 1 deletion
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.getAlign().value()) {
270+
switch (Sec.getAlignment()) {
271271
case 1:
272272
return COFF::IMAGE_SCN_ALIGN_1BYTES;
273273
case 2:

llvm/lib/MC/XCOFFObjectWriter.cpp

Lines changed: 6 additions & 4 deletions
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->getAlign());
1195+
Csect.Address = alignTo(Address, MCSec->getAlignment());
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->getAlign()) -
1250+
(*DwarfSections.begin()).DwarfSect->MCSec->getAlignment()) -
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->getAlign());
1276+
alignTo(Address, MCSec->getAlignment());
12771277

12781278
// Section size.
12791279
// For DWARF section, we must use the real size which may be not aligned.
@@ -1448,7 +1448,9 @@ 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 Log2Align = Log2(Sec->getAlign());
1451+
unsigned Align = Sec->getAlignment();
1452+
assert(isPowerOf2_32(Align) && "Alignment must be a power of 2.");
1453+
unsigned Log2Align = Log2_32(Align);
14521454
// Result is a number in the range [0, 31] which fits in the 5 least
14531455
// significant bits. Shift this value into the 5 most significant bits, and
14541456
// bitwise-or in the csect type.

llvm/lib/Target/AMDGPU/AMDGPUAsmPrinter.cpp

Lines changed: 1 addition & 1 deletion
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.getAlign() < 64)
256+
if (ReadOnlySection.getAlignment() < 64)
257257
ReadOnlySection.setAlignment(Align(64));
258258

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

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

Lines changed: 1 addition & 1 deletion
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 (Section.getAlign() < ByteAlignment)
120+
if (Align(ByteAlignment) > Section.getAlignment())
121121
Section.setAlignment(Align(ByteAlignment));
122122

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

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

Lines changed: 11 additions & 9 deletions
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(std::max(Align(16), TextSection.getAlign()));
903-
DataSection.setAlignment(std::max(Align(16), DataSection.getAlign()));
904-
BSSSection.setAlignment(std::max(Align(16), BSSSection.getAlign()));
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())));
905905

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

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());
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+
}
921923
}
922924
}
923925

llvm/lib/Target/PowerPC/PPCAsmPrinter.cpp

Lines changed: 1 addition & 1 deletion
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->getAlign())
2592+
if (GOAlign > Csect->getAlignment())
25932593
Csect->setAlignment(GOAlign);
25942594
};
25952595

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

Lines changed: 1 addition & 1 deletion
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 (Sec->getAlign() < AlignBoundary)
587+
if (AlignBoundary.value() > Sec->getAlignment())
588588
Sec->setAlignment(AlignBoundary);
589589
}
590590

0 commit comments

Comments
 (0)