Skip to content

Commit f3dc732

Browse files
authored
[BOLT][NFC] Make estimateEdgeCounts a BinaryFunctionPass (llvm#93074)
1 parent 990bed6 commit f3dc732

File tree

5 files changed

+48
-8
lines changed

5 files changed

+48
-8
lines changed

bolt/include/bolt/Passes/MCF.h

+15-2
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@
99
#ifndef BOLT_PASSES_MCF_H
1010
#define BOLT_PASSES_MCF_H
1111

12+
#include "bolt/Passes/BinaryPasses.h"
13+
#include "llvm/Support/CommandLine.h"
14+
1215
namespace llvm {
1316
namespace bolt {
1417

15-
class BinaryFunction;
1618
class DataflowInfoManager;
1719

1820
/// Implement the idea in "SamplePGO - The Power of Profile Guided Optimizations
@@ -23,7 +25,18 @@ void equalizeBBCounts(DataflowInfoManager &Info, BinaryFunction &BF);
2325

2426
/// Fill edge counts based on the basic block count. Used in nonLBR mode when
2527
/// we only have bb count.
26-
void estimateEdgeCounts(BinaryFunction &BF);
28+
class EstimateEdgeCounts : public BinaryFunctionPass {
29+
void runOnFunction(BinaryFunction &BF);
30+
31+
public:
32+
explicit EstimateEdgeCounts(const cl::opt<bool> &PrintPass)
33+
: BinaryFunctionPass(PrintPass) {}
34+
35+
const char *getName() const override { return "estimate-edge-counts"; }
36+
37+
/// Pass entry point
38+
Error runOnFunctions(BinaryContext &BC) override;
39+
};
2740

2841
} // end namespace bolt
2942
} // end namespace llvm

bolt/lib/Passes/MCF.cpp

+23-1
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,11 @@
1212

1313
#include "bolt/Passes/MCF.h"
1414
#include "bolt/Core/BinaryFunction.h"
15+
#include "bolt/Core/ParallelUtilities.h"
1516
#include "bolt/Passes/DataflowInfoManager.h"
1617
#include "bolt/Utils/CommandLineOpts.h"
1718
#include "llvm/ADT/DenseMap.h"
19+
#include "llvm/ADT/STLExtras.h"
1820
#include "llvm/Support/CommandLine.h"
1921
#include <algorithm>
2022
#include <vector>
@@ -432,7 +434,7 @@ void equalizeBBCounts(DataflowInfoManager &Info, BinaryFunction &BF) {
432434
}
433435
}
434436

435-
void estimateEdgeCounts(BinaryFunction &BF) {
437+
void EstimateEdgeCounts::runOnFunction(BinaryFunction &BF) {
436438
EdgeWeightMap PredEdgeWeights;
437439
EdgeWeightMap SuccEdgeWeights;
438440
if (!opts::IterativeGuess) {
@@ -453,5 +455,25 @@ void estimateEdgeCounts(BinaryFunction &BF) {
453455
recalculateBBCounts(BF, /*AllEdges=*/false);
454456
}
455457

458+
Error EstimateEdgeCounts::runOnFunctions(BinaryContext &BC) {
459+
if (llvm::none_of(llvm::make_second_range(BC.getBinaryFunctions()),
460+
[](const BinaryFunction &BF) {
461+
return BF.getProfileFlags() == BinaryFunction::PF_SAMPLE;
462+
}))
463+
return Error::success();
464+
465+
ParallelUtilities::WorkFuncTy WorkFun = [&](BinaryFunction &BF) {
466+
runOnFunction(BF);
467+
};
468+
ParallelUtilities::PredicateTy SkipFunc = [&](const BinaryFunction &BF) {
469+
return BF.getProfileFlags() != BinaryFunction::PF_SAMPLE;
470+
};
471+
472+
ParallelUtilities::runOnEachFunction(
473+
BC, ParallelUtilities::SchedulingPolicy::SP_BB_QUADRATIC, WorkFun,
474+
SkipFunc, "EstimateEdgeCounts");
475+
return Error::success();
476+
}
477+
456478
} // namespace bolt
457479
} // namespace llvm

bolt/lib/Profile/DataReader.cpp

-2
Original file line numberDiff line numberDiff line change
@@ -598,8 +598,6 @@ void DataReader::readSampleData(BinaryFunction &BF) {
598598
}
599599

600600
BF.ExecutionCount = TotalEntryCount;
601-
602-
estimateEdgeCounts(BF);
603601
}
604602

605603
void DataReader::convertBranchData(BinaryFunction &BF) const {

bolt/lib/Profile/YAMLProfileReader.cpp

+1-3
Original file line numberDiff line numberDiff line change
@@ -253,10 +253,8 @@ bool YAMLProfileReader::parseFunctionProfile(
253253
if (BB.getExecutionCount() == BinaryBasicBlock::COUNT_NO_PROFILE)
254254
BB.setExecutionCount(0);
255255

256-
if (YamlBP.Header.Flags & BinaryFunction::PF_SAMPLE) {
256+
if (YamlBP.Header.Flags & BinaryFunction::PF_SAMPLE)
257257
BF.setExecutionCount(FunctionExecutionCount);
258-
estimateEdgeCounts(BF);
259-
}
260258

261259
ProfileMatched &= !MismatchedBlocks && !MismatchedCalls && !MismatchedEdges;
262260

bolt/lib/Rewrite/BinaryPassManager.cpp

+9
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "bolt/Passes/JTFootprintReduction.h"
2424
#include "bolt/Passes/LongJmp.h"
2525
#include "bolt/Passes/LoopInversionPass.h"
26+
#include "bolt/Passes/MCF.h"
2627
#include "bolt/Passes/PLTCall.h"
2728
#include "bolt/Passes/PatchEntries.h"
2829
#include "bolt/Passes/RegReAssign.h"
@@ -90,6 +91,11 @@ PrintAfterLowering("print-after-lowering",
9091
cl::desc("print function after instruction lowering"),
9192
cl::Hidden, cl::cat(BoltOptCategory));
9293

94+
static cl::opt<bool> PrintEstimateEdgeCounts(
95+
"print-estimate-edge-counts",
96+
cl::desc("print function after edge counts are set for no-LBR profile"),
97+
cl::Hidden, cl::cat(BoltOptCategory));
98+
9399
cl::opt<bool>
94100
PrintFinalized("print-finalized",
95101
cl::desc("print function after CFG is finalized"),
@@ -334,6 +340,9 @@ Error BinaryFunctionPassManager::runPasses() {
334340
Error BinaryFunctionPassManager::runAllPasses(BinaryContext &BC) {
335341
BinaryFunctionPassManager Manager(BC);
336342

343+
Manager.registerPass(
344+
std::make_unique<EstimateEdgeCounts>(PrintEstimateEdgeCounts));
345+
337346
const DynoStats InitialDynoStats =
338347
getDynoStats(BC.getBinaryFunctions(), BC.isAArch64());
339348

0 commit comments

Comments
 (0)