Skip to content

Commit 506b617

Browse files
committed
Reland [CodeGen] emit CG profile for COFF object file
This reverts commit 90242ca. Error fixed at f543539 Differential Revision: https://reviews.llvm.org/D87811
1 parent 9bcf7b1 commit 506b617

File tree

5 files changed

+118
-53
lines changed

5 files changed

+118
-53
lines changed

llvm/include/llvm/CodeGen/TargetLoweringObjectFileImpl.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ class TargetLoweringObjectFileELF : public TargetLoweringObjectFile {
3535
protected:
3636
MCSymbolRefExpr::VariantKind PLTRelativeVariantKind =
3737
MCSymbolRefExpr::VK_None;
38-
const TargetMachine *TM = nullptr;
3938

4039
public:
4140
TargetLoweringObjectFileELF() = default;

llvm/include/llvm/Target/TargetLoweringObjectFile.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ class TargetLoweringObjectFile : public MCObjectFileInfo {
6161
/// This section contains the static destructor pointer list.
6262
MCSection *StaticDtorSection = nullptr;
6363

64+
const TargetMachine *TM = nullptr;
65+
6466
public:
6567
TargetLoweringObjectFile() = default;
6668
TargetLoweringObjectFile(const TargetLoweringObjectFile &) = delete;
@@ -81,6 +83,9 @@ class TargetLoweringObjectFile : public MCObjectFileInfo {
8183
/// Emit the module-level metadata that the platform cares about.
8284
virtual void emitModuleMetadata(MCStreamer &Streamer, Module &M) const {}
8385

86+
/// Emit Call Graph Profile metadata.
87+
virtual void emitCGProfile(MCStreamer &Streamer, Module &M) const;
88+
8489
/// Get the module-level metadata that the platform cares about.
8590
virtual void getModuleMetadata(Module &M) {}
8691

llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp

Lines changed: 14 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,6 @@ static void GetObjCImageInfo(Module &M, unsigned &Version, unsigned &Flags,
107107
void TargetLoweringObjectFileELF::Initialize(MCContext &Ctx,
108108
const TargetMachine &TgtM) {
109109
TargetLoweringObjectFile::Initialize(Ctx, TgtM);
110-
TM = &TgtM;
111110

112111
CodeModel::Model CM = TgtM.getCodeModel();
113112
InitializeELF(TgtM.Options.UseInitArray);
@@ -324,46 +323,7 @@ void TargetLoweringObjectFileELF::emitModuleMetadata(MCStreamer &Streamer,
324323
Streamer.AddBlankLine();
325324
}
326325

327-
SmallVector<Module::ModuleFlagEntry, 8> ModuleFlags;
328-
M.getModuleFlagsMetadata(ModuleFlags);
329-
330-
MDNode *CFGProfile = nullptr;
331-
332-
for (const auto &MFE : ModuleFlags) {
333-
StringRef Key = MFE.Key->getString();
334-
if (Key == "CG Profile") {
335-
CFGProfile = cast<MDNode>(MFE.Val);
336-
break;
337-
}
338-
}
339-
340-
if (!CFGProfile)
341-
return;
342-
343-
auto GetSym = [this](const MDOperand &MDO) -> MCSymbol * {
344-
if (!MDO)
345-
return nullptr;
346-
auto V = cast<ValueAsMetadata>(MDO);
347-
const Function *F = cast<Function>(V->getValue());
348-
return TM->getSymbol(F);
349-
};
350-
351-
for (const auto &Edge : CFGProfile->operands()) {
352-
MDNode *E = cast<MDNode>(Edge);
353-
const MCSymbol *From = GetSym(E->getOperand(0));
354-
const MCSymbol *To = GetSym(E->getOperand(1));
355-
// Skip null functions. This can happen if functions are dead stripped after
356-
// the CGProfile pass has been run.
357-
if (!From || !To)
358-
continue;
359-
uint64_t Count = cast<ConstantAsMetadata>(E->getOperand(2))
360-
->getValue()
361-
->getUniqueInteger()
362-
.getZExtValue();
363-
Streamer.emitCGProfileEntry(
364-
MCSymbolRefExpr::create(From, MCSymbolRefExpr::VK_None, C),
365-
MCSymbolRefExpr::create(To, MCSymbolRefExpr::VK_None, C), Count);
366-
}
326+
emitCGProfile(Streamer, M);
367327
}
368328

369329
MCSymbol *TargetLoweringObjectFileELF::getCFIPersonalitySymbol(
@@ -1598,18 +1558,20 @@ void TargetLoweringObjectFileCOFF::emitModuleMetadata(MCStreamer &Streamer,
15981558
StringRef Section;
15991559

16001560
GetObjCImageInfo(M, Version, Flags, Section);
1601-
if (Section.empty())
1602-
return;
1561+
if (!Section.empty()) {
1562+
auto &C = getContext();
1563+
auto *S = C.getCOFFSection(Section,
1564+
COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
1565+
COFF::IMAGE_SCN_MEM_READ,
1566+
SectionKind::getReadOnly());
1567+
Streamer.SwitchSection(S);
1568+
Streamer.emitLabel(C.getOrCreateSymbol(StringRef("OBJC_IMAGE_INFO")));
1569+
Streamer.emitInt32(Version);
1570+
Streamer.emitInt32(Flags);
1571+
Streamer.AddBlankLine();
1572+
}
16031573

1604-
auto &C = getContext();
1605-
auto *S = C.getCOFFSection(
1606-
Section, COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | COFF::IMAGE_SCN_MEM_READ,
1607-
SectionKind::getReadOnly());
1608-
Streamer.SwitchSection(S);
1609-
Streamer.emitLabel(C.getOrCreateSymbol(StringRef("OBJC_IMAGE_INFO")));
1610-
Streamer.emitInt32(Version);
1611-
Streamer.emitInt32(Flags);
1612-
Streamer.AddBlankLine();
1574+
emitCGProfile(Streamer, M);
16131575
}
16141576

16151577
void TargetLoweringObjectFileCOFF::emitLinkerDirectives(

llvm/lib/Target/TargetLoweringObjectFile.cpp

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ void TargetLoweringObjectFile::Initialize(MCContext &ctx,
4949
// Reset various EH DWARF encodings.
5050
PersonalityEncoding = LSDAEncoding = TTypeEncoding = dwarf::DW_EH_PE_absptr;
5151
CallSiteEncoding = dwarf::DW_EH_PE_uleb128;
52+
53+
this->TM = &TM;
5254
}
5355

5456
TargetLoweringObjectFile::~TargetLoweringObjectFile() {
@@ -136,6 +138,52 @@ void TargetLoweringObjectFile::emitPersonalityValue(MCStreamer &Streamer,
136138
const MCSymbol *Sym) const {
137139
}
138140

141+
void TargetLoweringObjectFile::emitCGProfile(MCStreamer &Streamer,
142+
Module &M) const {
143+
MCContext &C = getContext();
144+
SmallVector<Module::ModuleFlagEntry, 8> ModuleFlags;
145+
M.getModuleFlagsMetadata(ModuleFlags);
146+
147+
MDNode *CFGProfile = nullptr;
148+
149+
for (const auto &MFE : ModuleFlags) {
150+
StringRef Key = MFE.Key->getString();
151+
if (Key == "CG Profile") {
152+
CFGProfile = cast<MDNode>(MFE.Val);
153+
break;
154+
}
155+
}
156+
157+
if (!CFGProfile)
158+
return;
159+
160+
auto GetSym = [this](const MDOperand &MDO) -> MCSymbol * {
161+
if (!MDO)
162+
return nullptr;
163+
auto *V = cast<ValueAsMetadata>(MDO);
164+
const Function *F = cast<Function>(V->getValue());
165+
if (F->hasDLLImportStorageClass())
166+
return nullptr;
167+
return TM->getSymbol(F);
168+
};
169+
170+
for (const auto &Edge : CFGProfile->operands()) {
171+
MDNode *E = cast<MDNode>(Edge);
172+
const MCSymbol *From = GetSym(E->getOperand(0));
173+
const MCSymbol *To = GetSym(E->getOperand(1));
174+
// Skip null functions. This can happen if functions are dead stripped after
175+
// the CGProfile pass has been run.
176+
if (!From || !To)
177+
continue;
178+
uint64_t Count = cast<ConstantAsMetadata>(E->getOperand(2))
179+
->getValue()
180+
->getUniqueInteger()
181+
.getZExtValue();
182+
Streamer.emitCGProfileEntry(
183+
MCSymbolRefExpr::create(From, MCSymbolRefExpr::VK_None, C),
184+
MCSymbolRefExpr::create(To, MCSymbolRefExpr::VK_None, C), Count);
185+
}
186+
}
139187

140188
/// getKindForGlobal - This is a top-level target-independent classifier for
141189
/// a global object. Given a global variable and information from the TM, this

llvm/test/MC/COFF/cgprofile.ll

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
; RUN: llc -filetype=asm %s -o - -mtriple x86_64-pc-windows-msvc | FileCheck %s
2+
; RUN: llc -filetype=obj %s -o %t -mtriple x86_64-pc-windows-msvc
3+
; RUN: llvm-readobj --cg-profile %t | FileCheck %s --check-prefix=OBJ
4+
5+
declare void @b()
6+
7+
define void @a() {
8+
call void @b()
9+
ret void
10+
}
11+
12+
define void @freq(i1 %cond) {
13+
br i1 %cond, label %A, label %B
14+
A:
15+
call void @a();
16+
ret void
17+
B:
18+
call void @b();
19+
ret void
20+
}
21+
22+
!llvm.module.flags = !{!0}
23+
24+
!0 = !{i32 5, !"CG Profile", !1}
25+
!1 = !{!2, !3, !4, !5}
26+
!2 = !{void ()* @a, void ()* @b, i64 32}
27+
!3 = !{void (i1)* @freq, void ()* @a, i64 11}
28+
!4 = !{void (i1)* @freq, void ()* @b, i64 20}
29+
!5 = !{void (i1)* @freq, null, i64 20}
30+
31+
; CHECK: .cg_profile a, b, 32
32+
; CHECK: .cg_profile freq, a, 11
33+
; CHECK: .cg_profile freq, b, 20
34+
35+
; OBJ: CGProfile [
36+
; OBJ: CGProfileEntry {
37+
; OBJ: From: a
38+
; OBJ: To: b
39+
; OBJ: Weight: 32
40+
; OBJ: }
41+
; OBJ: CGProfileEntry {
42+
; OBJ: From: freq
43+
; OBJ: To: a
44+
; OBJ: Weight: 11
45+
; OBJ: }
46+
; OBJ: CGProfileEntry {
47+
; OBJ: From: freq
48+
; OBJ: To: b
49+
; OBJ: Weight: 20
50+
; OBJ: }
51+
; OBJ:]

0 commit comments

Comments
 (0)