Skip to content

Commit 385e3e2

Browse files
authored
[BOLT] Set EntryDiscriminator in YAML profile for indirect calls
Indirect call handling missed setting an `EntryDiscriminator` while it's set for direct calls and tail calls. Improve YAML profile accuracy by unifying the destination setting between direct and indirect calls into `setCSIDestination` method. Depends on: #86848 Test Plan: Updated bolt/test/X86/yaml-secondary-entry-discriminator.s Reviewers: ayermolo, maksfb, rafaelauler Reviewed By: maksfb Pull Request: #82128
1 parent b76fd1e commit 385e3e2

File tree

2 files changed

+30
-19
lines changed

2 files changed

+30
-19
lines changed

bolt/lib/Profile/YAMLProfileWriter.cpp

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,25 @@ extern llvm::cl::opt<bool> ProfileUseDFS;
2525
namespace llvm {
2626
namespace bolt {
2727

28+
/// Set CallSiteInfo destination fields from \p Symbol and return a target
29+
/// BinaryFunction for that symbol.
30+
static const BinaryFunction *setCSIDestination(const BinaryContext &BC,
31+
yaml::bolt::CallSiteInfo &CSI,
32+
const MCSymbol *Symbol) {
33+
CSI.DestId = 0; // designated for unknown functions
34+
CSI.EntryDiscriminator = 0;
35+
if (Symbol) {
36+
uint64_t EntryID = 0;
37+
if (const BinaryFunction *const Callee =
38+
BC.getFunctionForSymbol(Symbol, &EntryID)) {
39+
CSI.DestId = Callee->getFunctionNumber();
40+
CSI.EntryDiscriminator = EntryID;
41+
return Callee;
42+
}
43+
}
44+
return nullptr;
45+
}
46+
2847
yaml::bolt::BinaryFunctionProfile
2948
YAMLProfileWriter::convert(const BinaryFunction &BF, bool UseDFS) {
3049
yaml::bolt::BinaryFunctionProfile YamlBF;
@@ -79,31 +98,20 @@ YAMLProfileWriter::convert(const BinaryFunction &BF, bool UseDFS) {
7998
continue;
8099
for (const IndirectCallProfile &CSP : ICSP.get()) {
81100
StringRef TargetName = "";
82-
CSI.DestId = 0; // designated for unknown functions
83-
CSI.EntryDiscriminator = 0;
84-
if (CSP.Symbol) {
85-
const BinaryFunction *Callee = BC.getFunctionForSymbol(CSP.Symbol);
86-
if (Callee) {
87-
CSI.DestId = Callee->getFunctionNumber();
88-
TargetName = Callee->getOneName();
89-
}
90-
}
101+
const BinaryFunction *Callee = setCSIDestination(BC, CSI, CSP.Symbol);
102+
if (Callee)
103+
TargetName = Callee->getOneName();
91104
CSI.Count = CSP.Count;
92105
CSI.Mispreds = CSP.Mispreds;
93106
CSTargets.emplace_back(TargetName, CSI);
94107
}
95108
} else { // direct call or a tail call
96-
uint64_t EntryID = 0;
97-
CSI.DestId = 0;
98109
StringRef TargetName = "";
99110
const MCSymbol *CalleeSymbol = BC.MIB->getTargetSymbol(Instr);
100111
const BinaryFunction *const Callee =
101-
BC.getFunctionForSymbol(CalleeSymbol, &EntryID);
102-
if (Callee) {
103-
CSI.DestId = Callee->getFunctionNumber();
104-
CSI.EntryDiscriminator = EntryID;
112+
setCSIDestination(BC, CSI, CalleeSymbol);
113+
if (Callee)
105114
TargetName = Callee->getOneName();
106-
}
107115

108116
auto getAnnotationWithDefault = [&](const MCInst &Inst, StringRef Ann) {
109117
return BC.MIB->getAnnotationWithDefault(Instr, Ann, 0ull);

bolt/test/X86/yaml-secondary-entry-discriminator.s

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@
1919
# CHECK-NEXT: insns: 1
2020
# CHECK-NEXT: hash: 0x36A303CBA4360014
2121
# CHECK-NEXT: calls: [ { off: 0x0, fid: 1, disc: 1, cnt: 1 } ]
22+
# CHECK: - bid: 2
23+
# CHECK-NEXT: insns: 5
24+
# CHECK-NEXT: hash: 0x8B2F5747CD0019
25+
# CHECK-NEXT: calls: [ { off: 0x0, fid: 1, disc: 1, cnt: 1, mis: 1 } ]
2226

2327
# Make sure that the profile is attached correctly
2428
# RUN: llvm-bolt %t.exe -o %t.out --data %t.yaml --print-profile \
@@ -27,13 +31,12 @@
2731
# CHECK-CFG: Binary Function "main" after attaching profile {
2832
# CHECK-CFG: callq secondary_entry # Offset: [[#]] # Count: 1
2933
# CHECK-CFG: callq *%rax # Offset: [[#]] # CallProfile: 1 (1 misses) :
30-
# XXX: uncomment after discriminator is set correctly for indirect calls
31-
# COM: CHECK-CFG-NEXT: { secondary_entry: 1 (1 misses) }
32-
# CHECK-CFG: }
34+
# CHECK-CFG-NEXT: { secondary_entry: 1 (1 misses) }
3335

3436
.globl func
3537
.type func, @function
3638
func:
39+
# FDATA: 0 [unknown] 0 1 func 0 1 0
3740
.cfi_startproc
3841
pushq %rbp
3942
movq %rsp, %rbp

0 commit comments

Comments
 (0)