Skip to content

Commit 46beeaa

Browse files
authored
[MC] Remove SectionKind from MCSection (#96067)
There are only three actual uses of the section kind in MCSection: isText(), XCOFF, and WebAssembly. Store isText() in the MCSection, and store other info in the actual section variants where required. ELF and COFF flags also encode all relevant information, so for these two section variants, remove the SectionKind parameter entirely. This allows to remove the string switch (which is unnecessary and inaccurate) from createELFSectionImpl. This was introduced in [D133456](https://reviews.llvm.org/D133456), but apparently, it was never hit for non-writable sections anyway and the resulting kind was never used.
1 parent acf675b commit 46beeaa

27 files changed

+224
-328
lines changed

llvm/include/llvm/MC/MCContext.h

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -357,8 +357,7 @@ class MCContext {
357357
unsigned Instance);
358358

359359
MCSectionELF *createELFSectionImpl(StringRef Section, unsigned Type,
360-
unsigned Flags, SectionKind K,
361-
unsigned EntrySize,
360+
unsigned Flags, unsigned EntrySize,
362361
const MCSymbolELF *Group, bool IsComdat,
363362
unsigned UniqueID,
364363
const MCSymbolELF *LinkedToSym);
@@ -602,13 +601,11 @@ class MCContext {
602601
MCSection *Parent, const MCExpr *SubsectionId);
603602

604603
MCSectionCOFF *getCOFFSection(StringRef Section, unsigned Characteristics,
605-
SectionKind Kind, StringRef COMDATSymName,
606-
int Selection,
604+
StringRef COMDATSymName, int Selection,
607605
unsigned UniqueID = GenericSectionID,
608606
const char *BeginSymName = nullptr);
609607

610608
MCSectionCOFF *getCOFFSection(StringRef Section, unsigned Characteristics,
611-
SectionKind Kind,
612609
const char *BeginSymName = nullptr);
613610

614611
/// Gets or creates a section equivalent to Sec that is associated with the

llvm/include/llvm/MC/MCSection.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,8 @@ class MCSection {
104104

105105
bool IsRegistered : 1;
106106

107+
bool IsText : 1;
108+
107109
MCDummyFragment DummyFragment;
108110

109111
// Mapping from subsection number to fragment list. At layout time, the
@@ -124,17 +126,16 @@ class MCSection {
124126
// TODO Make Name private when possible.
125127
StringRef Name;
126128
SectionVariant Variant;
127-
SectionKind Kind;
128129

129-
MCSection(SectionVariant V, StringRef Name, SectionKind K, MCSymbol *Begin);
130+
MCSection(SectionVariant V, StringRef Name, bool IsText, MCSymbol *Begin);
130131
~MCSection();
131132

132133
public:
133134
MCSection(const MCSection &) = delete;
134135
MCSection &operator=(const MCSection &) = delete;
135136

136137
StringRef getName() const { return Name; }
137-
SectionKind getKind() const { return Kind; }
138+
bool isText() const { return IsText; }
138139

139140
SectionVariant getVariant() const { return Variant; }
140141

llvm/include/llvm/MC/MCSectionCOFF.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#define LLVM_MC_MCSECTIONCOFF_H
1515

1616
#include "llvm/ADT/StringRef.h"
17+
#include "llvm/BinaryFormat/COFF.h"
1718
#include "llvm/MC/MCSection.h"
1819
#include "llvm/MC/SectionKind.h"
1920
#include <cassert>
@@ -50,10 +51,11 @@ class MCSectionCOFF final : public MCSection {
5051
friend class MCContext;
5152
// The storage of Name is owned by MCContext's COFFUniquingMap.
5253
MCSectionCOFF(StringRef Name, unsigned Characteristics,
53-
MCSymbol *COMDATSymbol, int Selection, SectionKind K,
54-
MCSymbol *Begin)
55-
: MCSection(SV_COFF, Name, K, Begin), Characteristics(Characteristics),
56-
COMDATSymbol(COMDATSymbol), Selection(Selection) {
54+
MCSymbol *COMDATSymbol, int Selection, MCSymbol *Begin)
55+
: MCSection(SV_COFF, Name, Characteristics & COFF::IMAGE_SCN_CNT_CODE,
56+
Begin),
57+
Characteristics(Characteristics), COMDATSymbol(COMDATSymbol),
58+
Selection(Selection) {
5759
assert((Characteristics & 0x00F00000) == 0 &&
5860
"alignment must not be set upon section creation");
5961
}

llvm/include/llvm/MC/MCSectionDXContainer.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class MCSectionDXContainer final : public MCSection {
2424
friend class MCContext;
2525

2626
MCSectionDXContainer(StringRef Name, SectionKind K, MCSymbol *Begin)
27-
: MCSection(SV_DXContainer, Name, K, Begin) {}
27+
: MCSection(SV_DXContainer, Name, K.isText(), Begin) {}
2828

2929
public:
3030
void printSwitchToSection(const MCAsmInfo &, const Triple &, raw_ostream &,

llvm/include/llvm/MC/MCSectionELF.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
#include "llvm/ADT/PointerIntPair.h"
1717
#include "llvm/ADT/StringRef.h"
18+
#include "llvm/BinaryFormat/ELF.h"
1819
#include "llvm/MC/MCSection.h"
1920
#include "llvm/MC/MCSymbolELF.h"
2021
#include "llvm/MC/SectionKind.h"
@@ -49,13 +50,13 @@ class MCSectionELF final : public MCSection {
4950
friend class MCContext;
5051

5152
// The storage of Name is owned by MCContext's ELFUniquingMap.
52-
MCSectionELF(StringRef Name, unsigned type, unsigned flags, SectionKind K,
53+
MCSectionELF(StringRef Name, unsigned type, unsigned flags,
5354
unsigned entrySize, const MCSymbolELF *group, bool IsComdat,
5455
unsigned UniqueID, MCSymbol *Begin,
5556
const MCSymbolELF *LinkedToSym)
56-
: MCSection(SV_ELF, Name, K, Begin), Type(type), Flags(flags),
57-
UniqueID(UniqueID), EntrySize(entrySize), Group(group, IsComdat),
58-
LinkedToSym(LinkedToSym) {
57+
: MCSection(SV_ELF, Name, flags & ELF::SHF_EXECINSTR, Begin), Type(type),
58+
Flags(flags), UniqueID(UniqueID), EntrySize(entrySize),
59+
Group(group, IsComdat), LinkedToSym(LinkedToSym) {
5960
if (Group.getPointer())
6061
Group.getPointer()->setIsSignature();
6162
}

llvm/include/llvm/MC/MCSectionGOFF.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ class MCSectionGOFF final : public MCSection {
3030

3131
friend class MCContext;
3232
MCSectionGOFF(StringRef Name, SectionKind K, MCSection *P, const MCExpr *Sub)
33-
: MCSection(SV_GOFF, Name, K, nullptr), Parent(P), SubsectionId(Sub) {}
33+
: MCSection(SV_GOFF, Name, K.isText(), nullptr), Parent(P),
34+
SubsectionId(Sub) {}
3435

3536
public:
3637
void printSwitchToSection(const MCAsmInfo &MAI, const Triple &T,

llvm/include/llvm/MC/MCSectionSPIRV.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ class MCSymbol;
2323
class MCSectionSPIRV final : public MCSection {
2424
friend class MCContext;
2525

26-
MCSectionSPIRV(SectionKind K, MCSymbol *Begin)
27-
: MCSection(SV_SPIRV, "", K, Begin) {}
26+
MCSectionSPIRV()
27+
: MCSection(SV_SPIRV, "", /*IsText=*/true, /*Begin=*/nullptr) {}
2828
// TODO: Add StringRef Name to MCSectionSPIRV.
2929

3030
public:

llvm/include/llvm/MC/MCSectionWasm.h

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,20 @@ class MCSectionWasm final : public MCSection {
4040
// For data sections, whether to use a passive segment
4141
bool IsPassive = false;
4242

43+
bool IsWasmData;
44+
45+
bool IsMetadata;
46+
4347
// For data sections, bitfield of WasmSegmentFlag
4448
unsigned SegmentFlags;
4549

4650
// The storage of Name is owned by MCContext's WasmUniquingMap.
4751
friend class MCContext;
4852
MCSectionWasm(StringRef Name, SectionKind K, unsigned SegmentFlags,
4953
const MCSymbolWasm *Group, unsigned UniqueID, MCSymbol *Begin)
50-
: MCSection(SV_Wasm, Name, K, Begin), UniqueID(UniqueID), Group(Group),
51-
SegmentFlags(SegmentFlags) {}
54+
: MCSection(SV_Wasm, Name, K.isText(), Begin), UniqueID(UniqueID),
55+
Group(Group), IsWasmData(K.isReadOnly() || K.isWriteable()),
56+
IsMetadata(K.isMetadata()), SegmentFlags(SegmentFlags) {}
5257

5358
public:
5459
/// Decides whether a '.section' directive should be printed before the
@@ -64,10 +69,8 @@ class MCSectionWasm final : public MCSection {
6469
bool useCodeAlign() const override;
6570
bool isVirtualSection() const override;
6671

67-
bool isWasmData() const {
68-
return Kind.isGlobalWriteableData() || Kind.isReadOnly() ||
69-
Kind.isThreadLocal();
70-
}
72+
bool isWasmData() const { return IsWasmData; }
73+
bool isMetadata() const { return IsMetadata; }
7174

7275
bool isUnique() const { return UniqueID != ~0U; }
7376
unsigned getUniqueID() const { return UniqueID; }

llvm/include/llvm/MC/MCSectionXCOFF.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,17 +37,18 @@ class MCSectionXCOFF final : public MCSection {
3737
StringRef SymbolTableName;
3838
std::optional<XCOFF::DwarfSectionSubtypeFlags> DwarfSubtypeFlags;
3939
bool MultiSymbolsAllowed;
40+
SectionKind Kind;
4041
static constexpr unsigned DefaultAlignVal = 4;
4142
static constexpr unsigned DefaultTextAlignVal = 32;
4243

4344
MCSectionXCOFF(StringRef Name, XCOFF::StorageMappingClass SMC,
4445
XCOFF::SymbolType ST, SectionKind K, MCSymbolXCOFF *QualName,
4546
MCSymbol *Begin, StringRef SymbolTableName,
4647
bool MultiSymbolsAllowed)
47-
: MCSection(SV_XCOFF, Name, K, Begin),
48+
: MCSection(SV_XCOFF, Name, K.isText(), Begin),
4849
CsectProp(XCOFF::CsectProperties(SMC, ST)), QualName(QualName),
4950
SymbolTableName(SymbolTableName), DwarfSubtypeFlags(std::nullopt),
50-
MultiSymbolsAllowed(MultiSymbolsAllowed) {
51+
MultiSymbolsAllowed(MultiSymbolsAllowed), Kind(K) {
5152
assert(
5253
(ST == XCOFF::XTY_SD || ST == XCOFF::XTY_CM || ST == XCOFF::XTY_ER) &&
5354
"Invalid or unhandled type for csect.");
@@ -72,9 +73,9 @@ class MCSectionXCOFF final : public MCSection {
7273
XCOFF::DwarfSectionSubtypeFlags DwarfSubtypeFlags,
7374
MCSymbol *Begin, StringRef SymbolTableName,
7475
bool MultiSymbolsAllowed)
75-
: MCSection(SV_XCOFF, Name, K, Begin), QualName(QualName),
76+
: MCSection(SV_XCOFF, Name, K.isText(), Begin), QualName(QualName),
7677
SymbolTableName(SymbolTableName), DwarfSubtypeFlags(DwarfSubtypeFlags),
77-
MultiSymbolsAllowed(MultiSymbolsAllowed) {
78+
MultiSymbolsAllowed(MultiSymbolsAllowed), Kind(K) {
7879
assert(QualName != nullptr && "QualName is needed.");
7980

8081
// FIXME: use a more meaningful name for non csect sections.
@@ -125,6 +126,7 @@ class MCSectionXCOFF final : public MCSection {
125126
std::optional<XCOFF::CsectProperties> getCsectProp() const {
126127
return CsectProp;
127128
}
129+
SectionKind getKind() const { return Kind; }
128130
};
129131

130132
} // end namespace llvm

llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2415,8 +2415,7 @@ bool AsmPrinter::doFinalization(Module &M) {
24152415
SectionName,
24162416
COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | COFF::IMAGE_SCN_MEM_READ |
24172417
COFF::IMAGE_SCN_LNK_COMDAT,
2418-
SectionKind::getReadOnly(), Stub.first->getName(),
2419-
COFF::IMAGE_COMDAT_SELECT_ANY));
2418+
Stub.first->getName(), COFF::IMAGE_COMDAT_SELECT_ANY));
24202419
emitAlignment(Align(DL.getPointerSize()));
24212420
OutStreamer->emitSymbolAttribute(Stub.first, MCSA_Global);
24222421
OutStreamer->emitLabel(Stub.first);
@@ -2898,8 +2897,8 @@ bool AsmPrinter::emitSpecialLLVMGlobal(const GlobalVariable *GV) {
28982897
// For ARM64EC, print the table that maps between symbols and the
28992898
// corresponding thunks to translate between x64 and AArch64 code.
29002899
// This table is generated by AArch64Arm64ECCallLowering.
2901-
OutStreamer->switchSection(OutContext.getCOFFSection(
2902-
".hybmp$x", COFF::IMAGE_SCN_LNK_INFO, SectionKind::getMetadata()));
2900+
OutStreamer->switchSection(
2901+
OutContext.getCOFFSection(".hybmp$x", COFF::IMAGE_SCN_LNK_INFO));
29032902
auto *Arr = cast<ConstantArray>(GV->getInitializer());
29042903
for (auto &U : Arr->operands()) {
29052904
auto *C = cast<Constant>(U);
@@ -3158,7 +3157,7 @@ void AsmPrinter::emitAlignment(Align Alignment, const GlobalObject *GV,
31583157
if (Alignment == Align(1))
31593158
return; // 1-byte aligned: no need to emit alignment.
31603159

3161-
if (getCurrentSection()->getKind().isText()) {
3160+
if (getCurrentSection()->isText()) {
31623161
const MCSubtargetInfo *STI = nullptr;
31633162
if (this->MF)
31643163
STI = &getSubtargetInfo();

llvm/lib/CodeGen/AsmPrinter/DwarfDebug.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2998,8 +2998,7 @@ void DwarfDebug::emitDebugARanges() {
29982998
if (SCU.Sym->isInSection()) {
29992999
// Make a note of this symbol and it's section.
30003000
MCSection *Section = &SCU.Sym->getSection();
3001-
if (!Section->getKind().isMetadata())
3002-
SectionMap[Section].push_back(SCU);
3001+
SectionMap[Section].push_back(SCU);
30033002
} else {
30043003
// Some symbols (e.g. common/bss on mach-o) can have no section but still
30053004
// appear in the output. This sucks as we rely on sections to build

llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1696,7 +1696,7 @@ MCSection *TargetLoweringObjectFileCOFF::getExplicitSectionGlobal(
16961696
}
16971697
}
16981698

1699-
return getContext().getCOFFSection(Name, Characteristics, Kind, COMDATSymName,
1699+
return getContext().getCOFFSection(Name, Characteristics, COMDATSymName,
17001700
Selection);
17011701
}
17021702

@@ -1755,12 +1755,12 @@ MCSection *TargetLoweringObjectFileCOFF::SelectSectionForGlobal(
17551755
if (getContext().getTargetTriple().isWindowsGNUEnvironment())
17561756
raw_svector_ostream(Name) << '$' << ComdatGV->getName();
17571757

1758-
return getContext().getCOFFSection(Name, Characteristics, Kind,
1759-
COMDATSymName, Selection, UniqueID);
1758+
return getContext().getCOFFSection(Name, Characteristics, COMDATSymName,
1759+
Selection, UniqueID);
17601760
} else {
17611761
SmallString<256> TmpData;
17621762
getMangler().getNameWithPrefix(TmpData, GO, /*CannotUsePrivateLabel=*/true);
1763-
return getContext().getCOFFSection(Name, Characteristics, Kind, TmpData,
1763+
return getContext().getCOFFSection(Name, Characteristics, TmpData,
17641764
Selection, UniqueID);
17651765
}
17661766
}
@@ -1817,9 +1817,9 @@ MCSection *TargetLoweringObjectFileCOFF::getSectionForJumpTable(
18171817
Characteristics |= COFF::IMAGE_SCN_LNK_COMDAT;
18181818
unsigned UniqueID = NextUniqueID++;
18191819

1820-
return getContext().getCOFFSection(
1821-
SecName, Characteristics, Kind, COMDATSymName,
1822-
COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE, UniqueID);
1820+
return getContext().getCOFFSection(SecName, Characteristics, COMDATSymName,
1821+
COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE,
1822+
UniqueID);
18231823
}
18241824

18251825
bool TargetLoweringObjectFileCOFF::shouldPutJumpTableInFunctionSection(
@@ -1846,10 +1846,8 @@ void TargetLoweringObjectFileCOFF::emitModuleMetadata(MCStreamer &Streamer,
18461846
GetObjCImageInfo(M, Version, Flags, Section);
18471847
if (!Section.empty()) {
18481848
auto &C = getContext();
1849-
auto *S = C.getCOFFSection(Section,
1850-
COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
1851-
COFF::IMAGE_SCN_MEM_READ,
1852-
SectionKind::getReadOnly());
1849+
auto *S = C.getCOFFSection(Section, COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
1850+
COFF::IMAGE_SCN_MEM_READ);
18531851
Streamer.switchSection(S);
18541852
Streamer.emitLabel(C.getOrCreateSymbol(StringRef("OBJC_IMAGE_INFO")));
18551853
Streamer.emitInt32(Version);
@@ -1929,21 +1927,17 @@ void TargetLoweringObjectFileCOFF::Initialize(MCContext &Ctx,
19291927
if (T.isWindowsMSVCEnvironment() || T.isWindowsItaniumEnvironment()) {
19301928
StaticCtorSection =
19311929
Ctx.getCOFFSection(".CRT$XCU", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
1932-
COFF::IMAGE_SCN_MEM_READ,
1933-
SectionKind::getReadOnly());
1930+
COFF::IMAGE_SCN_MEM_READ);
19341931
StaticDtorSection =
19351932
Ctx.getCOFFSection(".CRT$XTX", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
1936-
COFF::IMAGE_SCN_MEM_READ,
1937-
SectionKind::getReadOnly());
1933+
COFF::IMAGE_SCN_MEM_READ);
19381934
} else {
19391935
StaticCtorSection = Ctx.getCOFFSection(
19401936
".ctors", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
1941-
COFF::IMAGE_SCN_MEM_READ | COFF::IMAGE_SCN_MEM_WRITE,
1942-
SectionKind::getData());
1937+
COFF::IMAGE_SCN_MEM_READ | COFF::IMAGE_SCN_MEM_WRITE);
19431938
StaticDtorSection = Ctx.getCOFFSection(
19441939
".dtors", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
1945-
COFF::IMAGE_SCN_MEM_READ | COFF::IMAGE_SCN_MEM_WRITE,
1946-
SectionKind::getData());
1940+
COFF::IMAGE_SCN_MEM_READ | COFF::IMAGE_SCN_MEM_WRITE);
19471941
}
19481942
}
19491943

@@ -1981,8 +1975,7 @@ static MCSectionCOFF *getCOFFStaticStructorSection(MCContext &Ctx,
19811975
if (AddPrioritySuffix)
19821976
OS << format("%05u", Priority);
19831977
MCSectionCOFF *Sec = Ctx.getCOFFSection(
1984-
Name, COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | COFF::IMAGE_SCN_MEM_READ,
1985-
SectionKind::getReadOnly());
1978+
Name, COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | COFF::IMAGE_SCN_MEM_READ);
19861979
return Ctx.getAssociativeCOFFSection(Sec, KeySym, 0);
19871980
}
19881981

@@ -1993,8 +1986,7 @@ static MCSectionCOFF *getCOFFStaticStructorSection(MCContext &Ctx,
19931986
return Ctx.getAssociativeCOFFSection(
19941987
Ctx.getCOFFSection(Name, COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
19951988
COFF::IMAGE_SCN_MEM_READ |
1996-
COFF::IMAGE_SCN_MEM_WRITE,
1997-
SectionKind::getData()),
1989+
COFF::IMAGE_SCN_MEM_WRITE),
19981990
KeySym, 0);
19991991
}
20001992

@@ -2112,7 +2104,7 @@ MCSection *TargetLoweringObjectFileCOFF::getSectionForConstant(
21122104
}
21132105

21142106
if (!COMDATSymName.empty())
2115-
return getContext().getCOFFSection(".rdata", Characteristics, Kind,
2107+
return getContext().getCOFFSection(".rdata", Characteristics,
21162108
COMDATSymName,
21172109
COFF::IMAGE_COMDAT_SELECT_ANY);
21182110
}

0 commit comments

Comments
 (0)