Skip to content

Commit 69b7e25

Browse files
committed
[BOLT] Switch to using layout order in YAML
Use layout order in YAML profile reading/writing. Preserve old behavior (DFS order) under `-profile-use-dfs` option. Reviewed By: spupyrev Differential Revision: https://reviews.llvm.org/D155514
1 parent 599e0da commit 69b7e25

File tree

4 files changed

+33
-14
lines changed

4 files changed

+33
-14
lines changed

bolt/lib/Profile/YAMLProfileReader.cpp

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ static llvm::cl::opt<bool>
2626
IgnoreHash("profile-ignore-hash",
2727
cl::desc("ignore hash while reading function profile"),
2828
cl::Hidden, cl::cat(BoltOptCategory));
29+
30+
llvm::cl::opt<bool> ProfileUseDFS("profile-use-dfs",
31+
cl::desc("use DFS order for YAML profile"),
32+
cl::Hidden, cl::cat(BoltOptCategory));
2933
}
3034

3135
namespace llvm {
@@ -90,7 +94,7 @@ bool YAMLProfileReader::parseFunctionProfile(
9094
FuncRawBranchCount += YamlSI.Count;
9195
BF.setRawBranchCount(FuncRawBranchCount);
9296

93-
if (!opts::IgnoreHash && YamlBF.Hash != BF.computeHash(/*UseDFS=*/true)) {
97+
if (!opts::IgnoreHash && YamlBF.Hash != BF.computeHash(opts::ProfileUseDFS)) {
9498
if (opts::Verbosity >= 1)
9599
errs() << "BOLT-WARNING: function hash mismatch\n";
96100
ProfileMatched = false;
@@ -102,18 +106,22 @@ bool YAMLProfileReader::parseFunctionProfile(
102106
ProfileMatched = false;
103107
}
104108

105-
BinaryFunction::BasicBlockOrderType DFSOrder = BF.dfs();
109+
BinaryFunction::BasicBlockOrderType Order;
110+
if (opts::ProfileUseDFS)
111+
llvm::copy(BF.dfs(), std::back_inserter(Order));
112+
else
113+
llvm::copy(BF.getLayout().blocks(), std::back_inserter(Order));
106114

107115
for (const yaml::bolt::BinaryBasicBlockProfile &YamlBB : YamlBF.Blocks) {
108-
if (YamlBB.Index >= DFSOrder.size()) {
116+
if (YamlBB.Index >= Order.size()) {
109117
if (opts::Verbosity >= 2)
110118
errs() << "BOLT-WARNING: index " << YamlBB.Index
111119
<< " is out of bounds\n";
112120
++MismatchedBlocks;
113121
continue;
114122
}
115123

116-
BinaryBasicBlock &BB = *DFSOrder[YamlBB.Index];
124+
BinaryBasicBlock &BB = *Order[YamlBB.Index];
117125

118126
// Basic samples profile (without LBR) does not have branches information
119127
// and needs a special processing.
@@ -197,14 +205,14 @@ bool YAMLProfileReader::parseFunctionProfile(
197205
}
198206

199207
for (const yaml::bolt::SuccessorInfo &YamlSI : YamlBB.Successors) {
200-
if (YamlSI.Index >= DFSOrder.size()) {
208+
if (YamlSI.Index >= Order.size()) {
201209
if (opts::Verbosity >= 1)
202210
errs() << "BOLT-WARNING: index out of bounds for profiled block\n";
203211
++MismatchedEdges;
204212
continue;
205213
}
206214

207-
BinaryBasicBlock &SuccessorBB = *DFSOrder[YamlSI.Index];
215+
BinaryBasicBlock &SuccessorBB = *Order[YamlSI.Index];
208216
if (!BB.getSuccessor(SuccessorBB.getLabel())) {
209217
if (opts::Verbosity >= 1)
210218
errs() << "BOLT-WARNING: no successor for block " << BB.getName()
@@ -338,7 +346,7 @@ Error YAMLProfileReader::readProfile(BinaryContext &BC) {
338346

339347
// Recompute hash once per function.
340348
if (!opts::IgnoreHash)
341-
Function.computeHash(/*UseDFS=*/true);
349+
Function.computeHash(opts::ProfileUseDFS);
342350

343351
for (StringRef FunctionName : Function.getNames()) {
344352
auto PI = ProfileNameToProfile.find(FunctionName);

bolt/lib/Profile/YAMLProfileWriter.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,17 @@
1212
#include "bolt/Profile/ProfileReaderBase.h"
1313
#include "bolt/Profile/ProfileYAMLMapping.h"
1414
#include "bolt/Rewrite/RewriteInstance.h"
15+
#include "llvm/Support/CommandLine.h"
1516
#include "llvm/Support/FileSystem.h"
1617
#include "llvm/Support/raw_ostream.h"
1718

1819
#undef DEBUG_TYPE
1920
#define DEBUG_TYPE "bolt-prof"
2021

22+
namespace opts {
23+
extern llvm::cl::opt<bool> ProfileUseDFS;
24+
} // namespace opts
25+
2126
namespace llvm {
2227
namespace bolt {
2328

@@ -29,7 +34,7 @@ void convert(const BinaryFunction &BF,
2934
const uint16_t LBRProfile = BF.getProfileFlags() & BinaryFunction::PF_LBR;
3035

3136
// Prepare function and block hashes
32-
BF.computeHash(/*UseDFS=*/true);
37+
BF.computeHash(opts::ProfileUseDFS);
3338
BF.computeBlockHashes();
3439

3540
YamlBF.Name = BF.getPrintName();
@@ -38,7 +43,11 @@ void convert(const BinaryFunction &BF,
3843
YamlBF.NumBasicBlocks = BF.size();
3944
YamlBF.ExecCount = BF.getKnownExecutionCount();
4045

41-
for (const BinaryBasicBlock *BB : BF.dfs()) {
46+
BinaryFunction::BasicBlockOrderType Order;
47+
llvm::copy(opts::ProfileUseDFS ? BF.dfs() : BF.getLayout().blocks(),
48+
std::back_inserter(Order));
49+
50+
for (const BinaryBasicBlock *BB : Order) {
4251
yaml::bolt::BinaryBasicBlockProfile YamlBB;
4352
YamlBB.Index = BB->getLayoutIndex();
4453
YamlBB.NumInstructions = BB->getNumNonPseudos();

bolt/test/X86/pre-aggregated-perf.test

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
REQUIRES: system-linux
1111

1212
RUN: yaml2obj %p/Inputs/blarge.yaml &> %t.exe
13-
RUN: perf2bolt %t.exe -o %t --pa -p %p/Inputs/pre-aggregated.txt -w %t.new
13+
RUN: perf2bolt %t.exe -o %t --pa -p %p/Inputs/pre-aggregated.txt -w %t.new \
14+
RUN: --profile-use-dfs
1415
RUN: cat %t | sort | FileCheck %s -check-prefix=PERF2BOLT
1516
RUN: cat %t.new | FileCheck %s -check-prefix=NEWFORMAT
1617

@@ -20,7 +21,7 @@ RUN: --profile-format=fdata
2021
RUN: cat %t.fdata | sort | FileCheck %s -check-prefix=PERF2BOLT
2122

2223
RUN: perf2bolt %t.exe -o %t.yaml --pa -p %p/Inputs/pre-aggregated.txt \
23-
RUN: --profile-format=yaml
24+
RUN: --profile-format=yaml --profile-use-dfs
2425
RUN: cat %t.yaml | FileCheck %s -check-prefix=NEWFORMAT
2526

2627
# Test --profile-format option with llvm-bolt --aggregate-only
@@ -29,7 +30,7 @@ RUN: --aggregate-only --profile-format=fdata
2930
RUN: cat %t.bolt.fdata | sort | FileCheck %s -check-prefix=PERF2BOLT
3031

3132
RUN: llvm-bolt %t.exe -o %t.bolt.yaml --pa -p %p/Inputs/pre-aggregated.txt \
32-
RUN: --aggregate-only --profile-format=yaml
33+
RUN: --aggregate-only --profile-format=yaml --profile-use-dfs
3334
RUN: cat %t.bolt.yaml | FileCheck %s -check-prefix=NEWFORMAT
3435

3536
PERF2BOLT: 0 [unknown] 7f36d18d60c0 1 main 53c 0 2

bolt/test/X86/reader-stale-yaml.test

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22
# correctly and stale data is corrected.
33

44
RUN: yaml2obj %p/Inputs/blarge.yaml &> %t.exe
5-
RUN: llvm-bolt %t.exe -o /dev/null --b %p/Inputs/blarge_profile_stale.yaml --print-cfg --print-only=usqrt --infer-stale-profile=1 --profile-ignore-hash=1 \
6-
RUN: 2>&1 | FileCheck %s -check-prefix=CHECK
5+
RUN: llvm-bolt %t.exe -o /dev/null --b %p/Inputs/blarge_profile_stale.yaml \
6+
RUN: --print-cfg --print-only=usqrt --infer-stale-profile=1 \
7+
RUN: --profile-ignore-hash=1 --profile-use-dfs 2>&1 | FileCheck %s
78

89
# Verify that yaml reader works as expected.
910
CHECK: pre-processing profile using YAML profile reader

0 commit comments

Comments
 (0)