Skip to content

Commit ba3742d

Browse files
aaupovyuxuanchen1997
authored andcommitted
[BOLT] Attach pseudo probes to blocks in YAML profile
Summary: Read pseudo probes in regular and BAT YAML profile generation, and attach them to YAML profile basic blocks. This exposes GUID, probe id, and probe type in profile for future use in stale profile matching. Test Plan: updated pseudoprobe-decoding-inline.test Reviewers: rafaelauler, ayermolo Reviewed By: rafaelauler Subscribers: Tasks: Tags: Differential Revision: https://phabricator.intern.facebook.com/D60251379
1 parent 3a3807f commit ba3742d

File tree

4 files changed

+71
-0
lines changed

4 files changed

+71
-0
lines changed

bolt/include/bolt/Profile/ProfileYAMLMapping.h

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,11 +93,36 @@ template <> struct MappingTraits<bolt::SuccessorInfo> {
9393
static const bool flow = true;
9494
};
9595

96+
namespace bolt {
97+
struct PseudoProbeInfo {
98+
llvm::yaml::Hex64 GUID;
99+
uint64_t Index;
100+
uint8_t Type;
101+
102+
bool operator==(const PseudoProbeInfo &Other) const {
103+
return GUID == Other.GUID && Index == Other.Index;
104+
}
105+
bool operator!=(const PseudoProbeInfo &Other) const {
106+
return !(*this == Other);
107+
}
108+
};
109+
} // end namespace bolt
110+
111+
template <> struct MappingTraits<bolt::PseudoProbeInfo> {
112+
static void mapping(IO &YamlIO, bolt::PseudoProbeInfo &PI) {
113+
YamlIO.mapRequired("guid", PI.GUID);
114+
YamlIO.mapRequired("id", PI.Index);
115+
YamlIO.mapRequired("type", PI.Type);
116+
}
117+
118+
static const bool flow = true;
119+
};
96120
} // end namespace yaml
97121
} // end namespace llvm
98122

99123
LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(llvm::yaml::bolt::CallSiteInfo)
100124
LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(llvm::yaml::bolt::SuccessorInfo)
125+
LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(llvm::yaml::bolt::PseudoProbeInfo)
101126

102127
namespace llvm {
103128
namespace yaml {
@@ -111,6 +136,7 @@ struct BinaryBasicBlockProfile {
111136
uint64_t EventCount{0};
112137
std::vector<CallSiteInfo> CallSites;
113138
std::vector<SuccessorInfo> Successors;
139+
std::vector<PseudoProbeInfo> PseudoProbes;
114140

115141
bool operator==(const BinaryBasicBlockProfile &Other) const {
116142
return Index == Other.Index;
@@ -132,6 +158,8 @@ template <> struct MappingTraits<bolt::BinaryBasicBlockProfile> {
132158
std::vector<bolt::CallSiteInfo>());
133159
YamlIO.mapOptional("succ", BBP.Successors,
134160
std::vector<bolt::SuccessorInfo>());
161+
YamlIO.mapOptional("pseudo_probes", BBP.PseudoProbes,
162+
std::vector<bolt::PseudoProbeInfo>());
135163
}
136164
};
137165

bolt/lib/Profile/DataAggregator.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2406,6 +2406,26 @@ std::error_code DataAggregator::writeBATYAML(BinaryContext &BC,
24062406
PseudoProbeDecoder->getFuncDescForGUID(YamlBF.GUID);
24072407
YamlBF.PseudoProbeDescHash = FuncDesc->FuncHash;
24082408
}
2409+
// Fetch probes belonging to all fragments
2410+
const AddressProbesMap &ProbeMap =
2411+
PseudoProbeDecoder->getAddress2ProbesMap();
2412+
BinaryFunction::FragmentsSetTy Fragments(BF->Fragments);
2413+
Fragments.insert(BF);
2414+
for (const BinaryFunction *F : Fragments) {
2415+
const uint64_t FuncAddr = F->getAddress();
2416+
const auto &FragmentProbes =
2417+
llvm::make_range(ProbeMap.lower_bound(FuncAddr),
2418+
ProbeMap.lower_bound(FuncAddr + F->getSize()));
2419+
for (const auto &[OutputAddress, Probes] : FragmentProbes) {
2420+
const uint32_t InputOffset = BAT->translate(
2421+
FuncAddr, OutputAddress - FuncAddr, /*IsBranchSrc=*/true);
2422+
const unsigned BlockIndex = getBlock(InputOffset).second;
2423+
for (const MCDecodedPseudoProbe &Probe : Probes)
2424+
YamlBF.Blocks[BlockIndex].PseudoProbes.emplace_back(
2425+
yaml::bolt::PseudoProbeInfo{Probe.getGuid(), Probe.getIndex(),
2426+
Probe.getType()});
2427+
}
2428+
}
24092429
}
24102430
// Drop blocks without a hash, won't be useful for stale matching.
24112431
llvm::erase_if(YamlBF.Blocks,

bolt/lib/Profile/YAMLProfileWriter.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,21 @@ YAMLProfileWriter::convert(const BinaryFunction &BF, bool UseDFS,
185185
++BranchInfo;
186186
}
187187

188+
if (PseudoProbeDecoder) {
189+
const AddressProbesMap &ProbeMap =
190+
PseudoProbeDecoder->getAddress2ProbesMap();
191+
const uint64_t FuncAddr = BF.getAddress();
192+
const std::pair<uint64_t, uint64_t> &BlockRange =
193+
BB->getInputAddressRange();
194+
const auto &BlockProbes =
195+
llvm::make_range(ProbeMap.lower_bound(FuncAddr + BlockRange.first),
196+
ProbeMap.lower_bound(FuncAddr + BlockRange.second));
197+
for (const auto &[_, Probes] : BlockProbes)
198+
for (const MCDecodedPseudoProbe &Probe : Probes)
199+
YamlBB.PseudoProbes.emplace_back(yaml::bolt::PseudoProbeInfo{
200+
Probe.getGuid(), Probe.getIndex(), Probe.getType()});
201+
}
202+
188203
YamlBF.Blocks.emplace_back(YamlBB);
189204
}
190205
return YamlBF;

bolt/test/X86/pseudoprobe-decoding-inline.test

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,20 @@
1313
# RUN: perf2bolt %t.bolt -p %t.preagg2 --pa -w %t.yaml2 -o %t.fdata2
1414
# RUN: FileCheck --input-file %t.yaml2 %s --check-prefix CHECK-YAML
1515
# CHECK-YAML: name: bar
16+
# CHECK-YAML: - bid: 0
17+
# CHECK-YAML: pseudo_probes: [ { guid: 0xE413754A191DB537, id: 1, type: 0 }, { guid: 0xE413754A191DB537, id: 4, type: 0 } ]
1618
# CHECK-YAML: guid: 0xE413754A191DB537
1719
# CHECK-YAML: pseudo_probe_desc_hash: 0x10E852DA94
20+
#
1821
# CHECK-YAML: name: foo
22+
# CHECK-YAML: - bid: 0
23+
# CHECK-YAML: pseudo_probes: [ { guid: 0x5CF8C24CDB18BDAC, id: 1, type: 0 }, { guid: 0x5CF8C24CDB18BDAC, id: 2, type: 0 } ]
1924
# CHECK-YAML: guid: 0x5CF8C24CDB18BDAC
2025
# CHECK-YAML: pseudo_probe_desc_hash: 0x200205A19C5B4
26+
#
2127
# CHECK-YAML: name: main
28+
# CHECK-YAML: - bid: 0
29+
# CHECK-YAML: pseudo_probes: [ { guid: 0xDB956436E78DD5FA, id: 1, type: 0 }, { guid: 0x5CF8C24CDB18BDAC, id: 1, type: 0 }, { guid: 0x5CF8C24CDB18BDAC, id: 2, type: 0 } ]
2230
# CHECK-YAML: guid: 0xDB956436E78DD5FA
2331
# CHECK-YAML: pseudo_probe_desc_hash: 0x10000FFFFFFFF
2432

0 commit comments

Comments
 (0)