Skip to content

Commit 4ecf2ca

Browse files
authored
[BOLT] Use aggregated FuncBranchData in writeBATYAML
Switch from FuncBranchData intermediate maps (Intra/InterIndex) to aggregated Data, same as one used by DataReader: https://github.com/llvm/llvm-project/blob/e62ce1f8842cca36eb14126d79dcca0a85bf6d36/bolt/lib/Profile/DataReader.cpp#L385-L389 This aligns the order of the output between YAMLProfileWriter and writeBATYAML. Test Plan: updated bolt-address-translation-yaml.test Reviewers: rafaelauler, dcci, ayermolo, maksfb Reviewed By: ayermolo, maksfb Pull Request: #91289
1 parent 70e227a commit 4ecf2ca

File tree

3 files changed

+33
-43
lines changed

3 files changed

+33
-43
lines changed

bolt/lib/Profile/DataAggregator.cpp

Lines changed: 20 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -2355,30 +2355,6 @@ std::error_code DataAggregator::writeBATYAML(BinaryContext &BC,
23552355
for (auto BI = BlockMap.begin(), BE = BlockMap.end(); BI != BE; ++BI)
23562356
YamlBF.Blocks[BI->second.getBBIndex()].Hash = BI->second.getBBHash();
23572357

2358-
auto getSuccessorInfo = [&](uint32_t SuccOffset, unsigned SuccDataIdx) {
2359-
const llvm::bolt::BranchInfo &BI = Branches.Data.at(SuccDataIdx);
2360-
yaml::bolt::SuccessorInfo SI;
2361-
SI.Index = BlockMap.getBBIndex(SuccOffset);
2362-
SI.Count = BI.Branches;
2363-
SI.Mispreds = BI.Mispreds;
2364-
return SI;
2365-
};
2366-
2367-
auto getCallSiteInfo = [&](Location CallToLoc, unsigned CallToIdx,
2368-
uint32_t Offset) {
2369-
const llvm::bolt::BranchInfo &BI = Branches.Data.at(CallToIdx);
2370-
yaml::bolt::CallSiteInfo CSI;
2371-
CSI.DestId = 0; // designated for unknown functions
2372-
CSI.EntryDiscriminator = 0;
2373-
CSI.Count = BI.Branches;
2374-
CSI.Mispreds = BI.Mispreds;
2375-
CSI.Offset = Offset;
2376-
if (BinaryData *BD = BC.getBinaryDataByName(CallToLoc.Name))
2377-
YAMLProfileWriter::setCSIDestination(BC, CSI, BD->getSymbol(), BAT,
2378-
CallToLoc.Offset);
2379-
return CSI;
2380-
};
2381-
23822358
// Lookup containing basic block offset and index
23832359
auto getBlock = [&BlockMap](uint32_t Offset) {
23842360
auto BlockIt = BlockMap.upper_bound(Offset);
@@ -2390,25 +2366,26 @@ std::error_code DataAggregator::writeBATYAML(BinaryContext &BC,
23902366
return std::pair(BlockIt->first, BlockIt->second.getBBIndex());
23912367
};
23922368

2393-
for (const auto &[FromOffset, SuccKV] : Branches.IntraIndex) {
2394-
const auto &[_, Index] = getBlock(FromOffset);
2395-
yaml::bolt::BinaryBasicBlockProfile &YamlBB = YamlBF.Blocks[Index];
2396-
for (const auto &[SuccOffset, SuccDataIdx] : SuccKV)
2397-
if (BlockMap.isInputBlock(SuccOffset))
2398-
YamlBB.Successors.emplace_back(
2399-
getSuccessorInfo(SuccOffset, SuccDataIdx));
2400-
}
2401-
for (const auto &[FromOffset, CallTo] : Branches.InterIndex) {
2402-
const auto &[BlockOffset, BlockIndex] = getBlock(FromOffset);
2403-
yaml::bolt::BinaryBasicBlockProfile &YamlBB = YamlBF.Blocks[BlockIndex];
2404-
const uint32_t Offset = FromOffset - BlockOffset;
2405-
for (const auto &[CallToLoc, CallToIdx] : CallTo)
2406-
YamlBB.CallSites.emplace_back(
2407-
getCallSiteInfo(CallToLoc, CallToIdx, Offset));
2408-
llvm::sort(YamlBB.CallSites, [](yaml::bolt::CallSiteInfo &A,
2409-
yaml::bolt::CallSiteInfo &B) {
2410-
return A.Offset < B.Offset;
2411-
});
2369+
for (const llvm::bolt::BranchInfo &BI : Branches.Data) {
2370+
using namespace yaml::bolt;
2371+
const auto &[BlockOffset, BlockIndex] = getBlock(BI.From.Offset);
2372+
BinaryBasicBlockProfile &YamlBB = YamlBF.Blocks[BlockIndex];
2373+
if (BI.To.IsSymbol && BI.To.Name == BI.From.Name && BI.To.Offset != 0) {
2374+
// Internal branch
2375+
const unsigned SuccIndex = getBlock(BI.To.Offset).second;
2376+
auto &SI = YamlBB.Successors.emplace_back(SuccessorInfo{SuccIndex});
2377+
SI.Count = BI.Branches;
2378+
SI.Mispreds = BI.Mispreds;
2379+
} else {
2380+
// Call
2381+
const uint32_t Offset = BI.From.Offset - BlockOffset;
2382+
auto &CSI = YamlBB.CallSites.emplace_back(CallSiteInfo{Offset});
2383+
CSI.Count = BI.Branches;
2384+
CSI.Mispreds = BI.Mispreds;
2385+
if (const BinaryData *BD = BC.getBinaryDataByName(BI.To.Name))
2386+
YAMLProfileWriter::setCSIDestination(BC, CSI, BD->getSymbol(), BAT,
2387+
BI.To.Offset);
2388+
}
24122389
}
24132390
// Set entry counts, similar to DataReader::readProfile.
24142391
for (const llvm::bolt::BranchInfo &BI : Branches.EntryData) {
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
B 800154 401050 20 0
2+
F 800159 800193 7

bolt/test/X86/bolt-address-translation-yaml.test

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,17 @@ BRANCHENTRY-YAML-CHECK: - name: SolveCubic
1515
BRANCHENTRY-YAML-CHECK: bid: 0
1616
BRANCHENTRY-YAML-CHECK: hash: 0x700F19D24600000
1717
BRANCHENTRY-YAML-CHECK-NEXT: succ: [ { bid: 7, cnt: 1 }
18+
# Check that the order is correct between BAT YAML and FDATA->YAML.
19+
RUN: perf2bolt %t.out --pa -p %p/Inputs/blarge_new_bat_order.preagg.txt \
20+
RUN: -w %t.yaml -o %t.fdata
21+
RUN: llvm-bolt %t.exe -data %t.fdata -w %t.yaml-fdata -o %t.null
22+
RUN: FileCheck --input-file %t.yaml --check-prefix ORDER-YAML-CHECK %s
23+
RUN: FileCheck --input-file %t.yaml-fdata --check-prefix ORDER-YAML-CHECK %s
24+
ORDER-YAML-CHECK: - name: SolveCubic
25+
ORDER-YAML-CHECK: bid: 3
26+
ORDER-YAML-CHECK: hash: 0xDDA1DC5F69F900AC
27+
ORDER-YAML-CHECK-NEXT: calls: [ { off: 0x26, fid: [[#]], cnt: 20 } ]
28+
ORDER-YAML-CHECK-NEXT: succ: [ { bid: 5, cnt: 7 }
1829
# Large profile test
1930
RUN: perf2bolt %t.out --pa -p %p/Inputs/blarge_new_bat.preagg.txt -w %t.yaml -o %t.fdata \
2031
RUN: 2>&1 | FileCheck --check-prefix READ-BAT-CHECK %s

0 commit comments

Comments
 (0)