Skip to content

Commit

Permalink
[BOLT][NFC] Make estimateEdgeCounts a BinaryFunctionPass (llvm#93074)
Browse files Browse the repository at this point in the history
  • Loading branch information
aaupov committed May 22, 2024
1 parent 990bed6 commit f3dc732
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 8 deletions.
17 changes: 15 additions & 2 deletions bolt/include/bolt/Passes/MCF.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@
#ifndef BOLT_PASSES_MCF_H
#define BOLT_PASSES_MCF_H

#include "bolt/Passes/BinaryPasses.h"
#include "llvm/Support/CommandLine.h"

namespace llvm {
namespace bolt {

class BinaryFunction;
class DataflowInfoManager;

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

/// Fill edge counts based on the basic block count. Used in nonLBR mode when
/// we only have bb count.
void estimateEdgeCounts(BinaryFunction &BF);
class EstimateEdgeCounts : public BinaryFunctionPass {
void runOnFunction(BinaryFunction &BF);

public:
explicit EstimateEdgeCounts(const cl::opt<bool> &PrintPass)
: BinaryFunctionPass(PrintPass) {}

const char *getName() const override { return "estimate-edge-counts"; }

/// Pass entry point
Error runOnFunctions(BinaryContext &BC) override;
};

} // end namespace bolt
} // end namespace llvm
Expand Down
24 changes: 23 additions & 1 deletion bolt/lib/Passes/MCF.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@

#include "bolt/Passes/MCF.h"
#include "bolt/Core/BinaryFunction.h"
#include "bolt/Core/ParallelUtilities.h"
#include "bolt/Passes/DataflowInfoManager.h"
#include "bolt/Utils/CommandLineOpts.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/Support/CommandLine.h"
#include <algorithm>
#include <vector>
Expand Down Expand Up @@ -432,7 +434,7 @@ void equalizeBBCounts(DataflowInfoManager &Info, BinaryFunction &BF) {
}
}

void estimateEdgeCounts(BinaryFunction &BF) {
void EstimateEdgeCounts::runOnFunction(BinaryFunction &BF) {
EdgeWeightMap PredEdgeWeights;
EdgeWeightMap SuccEdgeWeights;
if (!opts::IterativeGuess) {
Expand All @@ -453,5 +455,25 @@ void estimateEdgeCounts(BinaryFunction &BF) {
recalculateBBCounts(BF, /*AllEdges=*/false);
}

Error EstimateEdgeCounts::runOnFunctions(BinaryContext &BC) {
if (llvm::none_of(llvm::make_second_range(BC.getBinaryFunctions()),
[](const BinaryFunction &BF) {
return BF.getProfileFlags() == BinaryFunction::PF_SAMPLE;
}))
return Error::success();

ParallelUtilities::WorkFuncTy WorkFun = [&](BinaryFunction &BF) {
runOnFunction(BF);
};
ParallelUtilities::PredicateTy SkipFunc = [&](const BinaryFunction &BF) {
return BF.getProfileFlags() != BinaryFunction::PF_SAMPLE;
};

ParallelUtilities::runOnEachFunction(
BC, ParallelUtilities::SchedulingPolicy::SP_BB_QUADRATIC, WorkFun,
SkipFunc, "EstimateEdgeCounts");
return Error::success();
}

} // namespace bolt
} // namespace llvm
2 changes: 0 additions & 2 deletions bolt/lib/Profile/DataReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -598,8 +598,6 @@ void DataReader::readSampleData(BinaryFunction &BF) {
}

BF.ExecutionCount = TotalEntryCount;

estimateEdgeCounts(BF);
}

void DataReader::convertBranchData(BinaryFunction &BF) const {
Expand Down
4 changes: 1 addition & 3 deletions bolt/lib/Profile/YAMLProfileReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -253,10 +253,8 @@ bool YAMLProfileReader::parseFunctionProfile(
if (BB.getExecutionCount() == BinaryBasicBlock::COUNT_NO_PROFILE)
BB.setExecutionCount(0);

if (YamlBP.Header.Flags & BinaryFunction::PF_SAMPLE) {
if (YamlBP.Header.Flags & BinaryFunction::PF_SAMPLE)
BF.setExecutionCount(FunctionExecutionCount);
estimateEdgeCounts(BF);
}

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

Expand Down
9 changes: 9 additions & 0 deletions bolt/lib/Rewrite/BinaryPassManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "bolt/Passes/JTFootprintReduction.h"
#include "bolt/Passes/LongJmp.h"
#include "bolt/Passes/LoopInversionPass.h"
#include "bolt/Passes/MCF.h"
#include "bolt/Passes/PLTCall.h"
#include "bolt/Passes/PatchEntries.h"
#include "bolt/Passes/RegReAssign.h"
Expand Down Expand Up @@ -90,6 +91,11 @@ PrintAfterLowering("print-after-lowering",
cl::desc("print function after instruction lowering"),
cl::Hidden, cl::cat(BoltOptCategory));

static cl::opt<bool> PrintEstimateEdgeCounts(
"print-estimate-edge-counts",
cl::desc("print function after edge counts are set for no-LBR profile"),
cl::Hidden, cl::cat(BoltOptCategory));

cl::opt<bool>
PrintFinalized("print-finalized",
cl::desc("print function after CFG is finalized"),
Expand Down Expand Up @@ -334,6 +340,9 @@ Error BinaryFunctionPassManager::runPasses() {
Error BinaryFunctionPassManager::runAllPasses(BinaryContext &BC) {
BinaryFunctionPassManager Manager(BC);

Manager.registerPass(
std::make_unique<EstimateEdgeCounts>(PrintEstimateEdgeCounts));

const DynoStats InitialDynoStats =
getDynoStats(BC.getBinaryFunctions(), BC.isAArch64());

Expand Down

0 comments on commit f3dc732

Please sign in to comment.