Skip to content

Commit 3579fc0

Browse files
authored
[COFF] Preserve UniqueID used to create MCSectionCOFF (#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

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
; CHECK: .section .CRT$XTT12345,"dr"
1010
; CHECK: .p2align 3
1111
; CHECK: .quad g
12-
; CHECK: .section .CRT$XTT23456,"dr",associative,h
12+
; CHECK: .section .CRT$XTT23456,"dr",associative,h,unique,0
1313
; CHECK: .p2align 3
1414
; CHECK: .quad init_h
1515
; CHECK: .section .CRT$XTX,"dr"

llvm/test/CodeGen/X86/global-sections.ll

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ define void @F1() {
1212
ret void
1313
}
1414

15-
; WIN32-SECTIONS: .section .text,"xr",one_only,_F1
15+
; WIN32-SECTIONS: .section .text,"xr",one_only,_F1,unique,0
1616
; WIN32-SECTIONS: .globl _F1
1717

1818
define void @F2(i32 %y) {
@@ -49,9 +49,9 @@ bb5:
4949
; LINUX-FUNC-SECTIONS-NEXT: .cfi_endproc
5050
; LINUX-FUNC-SECTIONS-NEXT: .section .rodata.F2,"a",@progbits
5151

52-
; WIN32-FUNC-SECTIONS: .section .text,"xr",one_only,_F2
52+
; WIN32-FUNC-SECTIONS: .section .text,"xr",one_only,_F2,unique,1
5353
; WIN32-FUNC-SECTIONS-NOT: .section
54-
; WIN32-FUNC-SECTIONS: .section .rdata,"dr",associative,_F2
54+
; WIN32-FUNC-SECTIONS: .section .rdata,"dr",associative,_F2,unique,2
5555

5656

5757
; LINUX-SECTIONS-PIC: .section .text.F2,"ax",@progbits
@@ -138,7 +138,7 @@ bb7:
138138
; LINUX-SECTIONS: .section .rodata.G3,"a",@progbits
139139
; LINUX-SECTIONS: .globl G3
140140

141-
; WIN32-SECTIONS: .section .rdata,"dr",one_only,_G3
141+
; WIN32-SECTIONS: .section .rdata,"dr",one_only,_G3,unique,6
142142
; WIN32-SECTIONS: .globl _G3
143143

144144

@@ -213,7 +213,7 @@ bb7:
213213
; LINUX-SECTIONS: .section .rodata.str1.1,"aMS",@progbits,1
214214
; LINUX-SECTIONS: .globl G7
215215

216-
; WIN32-SECTIONS: .section .rdata,"dr",one_only,_G7
216+
; WIN32-SECTIONS: .section .rdata,"dr",one_only,_G7,unique,11
217217
; WIN32-SECTIONS: .globl _G7
218218

219219

@@ -276,7 +276,7 @@ bb7:
276276
; LINUX-SECTIONS: .asciz "foo"
277277
; LINUX-SECTIONS: .size .LG14, 4
278278

279-
; WIN32-SECTIONS: .section .rdata,"dr",one_only,_G14
279+
; WIN32-SECTIONS: .section .rdata,"dr",one_only,_G14,unique,18
280280
; WIN32-SECTIONS: _G14:
281281
; WIN32-SECTIONS: .asciz "foo"
282282

@@ -298,7 +298,7 @@ bb7:
298298
; LINUX-SECTIONS: .section .rodata.cst8,"aM",@progbits,8
299299
; LINUX-SECTIONS: G15:
300300

301-
; WIN32-SECTIONS: .section .rdata,"dr",one_only,_G15
301+
; WIN32-SECTIONS: .section .rdata,"dr",one_only,_G15,unique,19
302302
; WIN32-SECTIONS: _G15:
303303

304304
@G16 = unnamed_addr constant i256 0
@@ -309,7 +309,7 @@ bb7:
309309
; LINUX-SECTIONS: .section .rodata.cst32,"aM",@progbits,32
310310
; LINUX-SECTIONS: G16:
311311

312-
; WIN32-SECTIONS: .section .rdata,"dr",one_only,_G16
312+
; WIN32-SECTIONS: .section .rdata,"dr",one_only,_G16,unique,20
313313
; WIN32-SECTIONS: _G16:
314314

315315
; PR26570
@@ -326,7 +326,7 @@ bb7:
326326
; LINUX-SECTIONS: .byte 0
327327
; LINUX-SECTIONS: .size G17, 1
328328

329-
; WIN32-SECTIONS: .section .bss,"bw",one_only,_G17
329+
; WIN32-SECTIONS: .section .bss,"bw",one_only,_G17,unique,21
330330
; WIN32-SECTIONS: _G17:
331331
; WIN32-SECTIONS:.byte 0
332332

llvm/test/CodeGen/X86/mingw-comdats.ll

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,23 +27,23 @@ entry:
2727
ret i32 %call
2828
}
2929

30-
; CHECK: .section .text,"xr",one_only,main
30+
; CHECK: .section .text,"xr",one_only,main,unique,0
3131
; CHECK: main:
32-
; GNU: .section .text$main,"xr",one_only,main
32+
; GNU: .section .text$main,"xr",one_only,main,unique,0
3333
; GNU: main:
34-
; GNU32: .section .text$main,"xr",one_only,_main
34+
; GNU32: .section .text$main,"xr",one_only,_main,unique,0
3535
; GNU32: _main:
3636

3737
define dso_local x86_fastcallcc i32 @fastcall(i32 %x, i32 %y) {
3838
%rv = add i32 %x, %y
3939
ret i32 %rv
4040
}
4141

42-
; CHECK: .section .text,"xr",one_only,fastcall
42+
; CHECK: .section .text,"xr",one_only,fastcall,unique,1
4343
; CHECK: fastcall:
44-
; GNU: .section .text$fastcall,"xr",one_only,fastcall
44+
; GNU: .section .text$fastcall,"xr",one_only,fastcall,unique,1
4545
; GNU: fastcall:
46-
; GNU32: .section .text$fastcall,"xr",one_only,@fastcall@8
46+
; GNU32: .section .text$fastcall,"xr",one_only,@fastcall@8,unique,1
4747
; GNU32: @fastcall@8:
4848

4949
; Function Attrs: inlinehint uwtable
@@ -55,19 +55,19 @@ entry:
5555
ret i32 %add
5656
}
5757

58-
; CHECK: .section .text,"xr",discard,_Z3fooi
58+
; CHECK: .section .text,"xr",discard,_Z3fooi,unique,2
5959
; CHECK: _Z3fooi:
6060
; CHECK: .section .data,"dw",discard,gv
6161
; CHECK: gv:
6262
; CHECK: .long 42
6363

64-
; GNU: .section .text$_Z3fooi,"xr",discard,_Z3fooi
64+
; GNU: .section .text$_Z3fooi,"xr",discard,_Z3fooi,unique,2
6565
; GNU: _Z3fooi:
6666
; GNU: .section .data$gv,"dw",discard,gv
6767
; GNU: gv:
6868
; GNU: .long 42
6969

70-
; GNU32: .section .text$_Z3fooi,"xr",discard,__Z3fooi
70+
; GNU32: .section .text$_Z3fooi,"xr",discard,__Z3fooi,unique,2
7171
; GNU32: __Z3fooi:
7272
; GNU32: .section .data$gv,"dw",discard,_gv
7373
; GNU32: _gv:

0 commit comments

Comments
 (0)