Skip to content

Commit 3579fc0

Browse files
authored
[COFF] Preserve UniqueID used to create MCSectionCOFF (llvm#123869)
This UniqueID can be used later to create associative section. e.g. A .pseudo_probe associated to the section of the corresponding function.
1 parent e17f07c commit 3579fc0

16 files changed

+156
-67
lines changed

llvm/include/llvm/MC/MCParser/MCAsmParserExtension.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,8 @@ class MCAsmParserExtension {
100100

101101
bool parseDirectiveCGProfile(StringRef, SMLoc);
102102

103+
bool maybeParseUniqueID(int64_t &UniqueID);
104+
103105
bool check(bool P, const Twine &Msg) {
104106
return getParser().check(P, Msg);
105107
}

llvm/include/llvm/MC/MCSectionCOFF.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,16 +47,19 @@ class MCSectionCOFF final : public MCSection {
4747
/// section (Characteristics & IMAGE_SCN_LNK_COMDAT) != 0
4848
mutable int Selection;
4949

50+
unsigned UniqueID;
51+
5052
private:
5153
friend class MCContext;
5254
// The storage of Name is owned by MCContext's COFFUniquingMap.
5355
MCSectionCOFF(StringRef Name, unsigned Characteristics,
54-
MCSymbol *COMDATSymbol, int Selection, MCSymbol *Begin)
56+
MCSymbol *COMDATSymbol, int Selection, unsigned UniqueID,
57+
MCSymbol *Begin)
5558
: MCSection(SV_COFF, Name, Characteristics & COFF::IMAGE_SCN_CNT_CODE,
5659
Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA,
5760
Begin),
5861
Characteristics(Characteristics), COMDATSymbol(COMDATSymbol),
59-
Selection(Selection) {
62+
Selection(Selection), UniqueID(UniqueID) {
6063
assert((Characteristics & 0x00F00000) == 0 &&
6164
"alignment must not be set upon section creation");
6265
}
@@ -72,6 +75,9 @@ class MCSectionCOFF final : public MCSection {
7275

7376
void setSelection(int Selection) const;
7477

78+
bool isUnique() const { return UniqueID != NonUniqueID; }
79+
unsigned getUniqueID() const { return UniqueID; }
80+
7581
void printSwitchToSection(const MCAsmInfo &MAI, const Triple &T,
7682
raw_ostream &OS,
7783
uint32_t Subsection) const override;

llvm/lib/MC/MCContext.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -718,7 +718,7 @@ MCSectionCOFF *MCContext::getCOFFSection(StringRef Section,
718718
StringRef CachedName = Iter->first.SectionName;
719719
MCSymbol *Begin = getOrCreateSectionSymbol<MCSymbolCOFF>(Section);
720720
MCSectionCOFF *Result = new (COFFAllocator.Allocate()) MCSectionCOFF(
721-
CachedName, Characteristics, COMDATSymbol, Selection, Begin);
721+
CachedName, Characteristics, COMDATSymbol, Selection, UniqueID, Begin);
722722
Iter->second = Result;
723723
auto *F = allocInitialFragment(*Result);
724724
Begin->setFragment(F);

llvm/lib/MC/MCParser/COFFAsmParser.cpp

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,12 @@ class COFFAsmParser : public MCAsmParserExtension {
3838
bool parseSectionSwitch(StringRef Section, unsigned Characteristics);
3939

4040
bool parseSectionSwitch(StringRef Section, unsigned Characteristics,
41-
StringRef COMDATSymName, COFF::COMDATType Type);
41+
StringRef COMDATSymName, COFF::COMDATType Type,
42+
unsigned UniqueID);
4243

4344
bool parseSectionName(StringRef &SectionName);
4445
bool parseSectionFlags(StringRef SectionName, StringRef FlagsString,
4546
unsigned *Flags);
46-
4747
void Initialize(MCAsmParser &Parser) override {
4848
// Call the base implementation.
4949
MCAsmParserExtension::Initialize(Parser);
@@ -315,19 +315,21 @@ bool COFFAsmParser::parseDirectiveCGProfile(StringRef S, SMLoc Loc) {
315315

316316
bool COFFAsmParser::parseSectionSwitch(StringRef Section,
317317
unsigned Characteristics) {
318-
return parseSectionSwitch(Section, Characteristics, "", (COFF::COMDATType)0);
318+
return parseSectionSwitch(Section, Characteristics, "", (COFF::COMDATType)0,
319+
MCSection::NonUniqueID);
319320
}
320321

321322
bool COFFAsmParser::parseSectionSwitch(StringRef Section,
322323
unsigned Characteristics,
323324
StringRef COMDATSymName,
324-
COFF::COMDATType Type) {
325+
COFF::COMDATType Type,
326+
unsigned UniqueID) {
325327
if (getLexer().isNot(AsmToken::EndOfStatement))
326328
return TokError("unexpected token in section switching directive");
327329
Lex();
328330

329331
getStreamer().switchSection(getContext().getCOFFSection(
330-
Section, Characteristics, COMDATSymName, Type));
332+
Section, Characteristics, COMDATSymName, Type, UniqueID));
331333

332334
return false;
333335
}
@@ -386,7 +388,8 @@ bool COFFAsmParser::parseSectionArguments(StringRef, SMLoc) {
386388

387389
COFF::COMDATType Type = (COFF::COMDATType)0;
388390
StringRef COMDATSymName;
389-
if (getLexer().is(AsmToken::Comma)) {
391+
if (getLexer().is(AsmToken::Comma) &&
392+
getLexer().peekTok().getString() != "unique") {
390393
Type = COFF::IMAGE_COMDAT_SELECT_ANY;
391394
Lex();
392395

@@ -407,6 +410,10 @@ bool COFFAsmParser::parseSectionArguments(StringRef, SMLoc) {
407410
return TokError("expected identifier in directive");
408411
}
409412

413+
int64_t UniqueID = MCSection::NonUniqueID;
414+
if (maybeParseUniqueID(UniqueID))
415+
return true;
416+
410417
if (getLexer().isNot(AsmToken::EndOfStatement))
411418
return TokError("unexpected token in directive");
412419

@@ -415,7 +422,7 @@ bool COFFAsmParser::parseSectionArguments(StringRef, SMLoc) {
415422
if (T.getArch() == Triple::arm || T.getArch() == Triple::thumb)
416423
Flags |= COFF::IMAGE_SCN_MEM_16BIT;
417424
}
418-
parseSectionSwitch(SectionName, Flags, COMDATSymName, Type);
425+
parseSectionSwitch(SectionName, Flags, COMDATSymName, Type, UniqueID);
419426
return false;
420427
}
421428

llvm/lib/MC/MCParser/ELFAsmParser.cpp

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,6 @@ class ELFAsmParser : public MCAsmParserExtension {
137137
bool parseMergeSize(int64_t &Size);
138138
bool parseGroup(StringRef &GroupName, bool &IsComdat);
139139
bool parseLinkedToSym(MCSymbolELF *&LinkedToSym);
140-
bool maybeParseUniqueID(int64_t &UniqueID);
141140
};
142141

143142
} // end anonymous namespace
@@ -474,28 +473,6 @@ bool ELFAsmParser::parseLinkedToSym(MCSymbolELF *&LinkedToSym) {
474473
return false;
475474
}
476475

477-
bool ELFAsmParser::maybeParseUniqueID(int64_t &UniqueID) {
478-
MCAsmLexer &L = getLexer();
479-
if (L.isNot(AsmToken::Comma))
480-
return false;
481-
Lex();
482-
StringRef UniqueStr;
483-
if (getParser().parseIdentifier(UniqueStr))
484-
return TokError("expected identifier");
485-
if (UniqueStr != "unique")
486-
return TokError("expected 'unique'");
487-
if (L.isNot(AsmToken::Comma))
488-
return TokError("expected commma");
489-
Lex();
490-
if (getParser().parseAbsoluteExpression(UniqueID))
491-
return true;
492-
if (UniqueID < 0)
493-
return TokError("unique id must be positive");
494-
if (!isUInt<32>(UniqueID) || UniqueID == ~0U)
495-
return TokError("unique id is too large");
496-
return false;
497-
}
498-
499476
static bool hasPrefix(StringRef SectionName, StringRef Prefix) {
500477
return SectionName.consume_front(Prefix) &&
501478
(SectionName.empty() || SectionName[0] == '.');

llvm/lib/MC/MCParser/MCAsmParserExtension.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,25 @@ bool MCAsmParserExtension::parseDirectiveCGProfile(StringRef, SMLoc) {
5858
MCSymbolRefExpr::create(ToSym, getContext(), ToLoc), Count);
5959
return false;
6060
}
61+
62+
bool MCAsmParserExtension::maybeParseUniqueID(int64_t &UniqueID) {
63+
MCAsmLexer &L = getLexer();
64+
if (L.isNot(AsmToken::Comma))
65+
return false;
66+
Lex();
67+
StringRef UniqueStr;
68+
if (getParser().parseIdentifier(UniqueStr))
69+
return TokError("expected identifier");
70+
if (UniqueStr != "unique")
71+
return TokError("expected 'unique'");
72+
if (L.isNot(AsmToken::Comma))
73+
return TokError("expected commma");
74+
Lex();
75+
if (getParser().parseAbsoluteExpression(UniqueID))
76+
return true;
77+
if (UniqueID < 0)
78+
return TokError("unique id must be positive");
79+
if (!isUInt<32>(UniqueID) || UniqueID == ~0U)
80+
return TokError("unique id is too large");
81+
return false;
82+
}

llvm/lib/MC/MCSectionCOFF.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ using namespace llvm;
1818
// should be printed before the section name
1919
bool MCSectionCOFF::shouldOmitSectionDirective(StringRef Name,
2020
const MCAsmInfo &MAI) const {
21-
if (COMDATSymbol)
21+
if (COMDATSymbol || isUnique())
2222
return false;
2323

2424
// FIXME: Does .section .bss/.data/.text work everywhere??
@@ -67,6 +67,10 @@ void MCSectionCOFF::printSwitchToSection(const MCAsmInfo &MAI, const Triple &T,
6767
OS << 'i';
6868
OS << '"';
6969

70+
// unique should be tail of .section directive.
71+
if (isUnique() && !COMDATSymbol)
72+
OS << ",unique," << UniqueID;
73+
7074
if (getCharacteristics() & COFF::IMAGE_SCN_LNK_COMDAT) {
7175
if (COMDATSymbol)
7276
OS << ",";
@@ -103,6 +107,10 @@ void MCSectionCOFF::printSwitchToSection(const MCAsmInfo &MAI, const Triple &T,
103107
COMDATSymbol->print(OS, &MAI);
104108
}
105109
}
110+
111+
if (isUnique() && COMDATSymbol)
112+
OS << ",unique," << UniqueID;
113+
106114
OS << '\n';
107115
}
108116

llvm/test/CodeGen/X86/constructor.ll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,10 @@ entry:
7777
; MCU-CTORS: .section .ctors,"aw",@progbits
7878
; MCU-INIT-ARRAY: .section .init_array,"aw",@init_array
7979

80-
; COFF-CTOR: .section .ctors.65520,"dw",associative,v
80+
; COFF-CTOR: .section .ctors.65520,"dw",associative,v,unique,0
8181
; COFF-CTOR-NEXT: .p2align 3
8282
; COFF-CTOR-NEXT: .quad g
83-
; COFF-CTOR-NEXT: .section .ctors.09980,"dw",associative,v
83+
; COFF-CTOR-NEXT: .section .ctors.09980,"dw",associative,v,unique,0
8484
; COFF-CTOR-NEXT: .p2align 3
8585
; COFF-CTOR-NEXT: .quad h
8686
; COFF-CTOR-NEXT: .section .ctors,"dw"

llvm/test/CodeGen/X86/ctor-priority-coff.ll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@
1212
; CHECK: .section .CRT$XCC00250,"dr"
1313
; CHECK: .p2align 3
1414
; CHECK: .quad k
15-
; CHECK: .section .CRT$XCL,"dr"
15+
; CHECK: .section .CRT$XCL,"dr",unique,0
1616
; CHECK: .p2align 3
1717
; CHECK: .quad j
1818
; CHECK: .section .CRT$XCT12345,"dr"
1919
; CHECK: .p2align 3
2020
; CHECK: .quad g
21-
; CHECK: .section .CRT$XCT23456,"dr",associative,h
21+
; CHECK: .section .CRT$XCT23456,"dr",associative,h,unique,0
2222
; CHECK: .p2align 3
2323
; CHECK: .quad init_h
2424

llvm/test/CodeGen/X86/data-section-prefix.ll

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@
1313
; ELF-NOUNIQ: .section .bss.unlikely.,"aw",@nobits,unique,3
1414
; ELF-NOUNIQ: .section .bss,"aw",@nobits,unique,4
1515

16-
; COFF-MSVC: .section .data,"dw",one_only,foo
17-
; COFF-MSVC: .section .data,"dw",one_only,bar
18-
; COFF-MSVC: .section .bss,"bw",one_only,baz
19-
; COFF-MSVC: .section .bss,"bw",one_only,quz
16+
; COFF-MSVC: .section .data,"dw",one_only,foo,unique,0
17+
; COFF-MSVC: .section .data,"dw",one_only,bar,unique,1
18+
; COFF-MSVC: .section .bss,"bw",one_only,baz,unique,2
19+
; COFF-MSVC: .section .bss,"bw",one_only,quz,unique,3
2020

2121
@foo = global i32 1, !section_prefix !0
2222
@bar = global i32 2

0 commit comments

Comments
 (0)