Skip to content

Commit a38f015

Browse files
authored
[BOLT] Set InitialDynoStats after EstimateEdgeCounts (#93218)
InitialDynoStats used to be assigned inside `runAllPasses`, but the assignment executed before any of the passes. As we've moved `EstimateEdgeCounts` into a pass out of ProfileReader, it needs to execute before initial dyno stats are set. Thus move `InitialDynoStats` into BinaryContext and assignment into `DynoStatsSetPass`.
1 parent 73eb9b3 commit a38f015

File tree

4 files changed

+29
-10
lines changed

4 files changed

+29
-10
lines changed

bolt/include/bolt/Core/BinaryContext.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "bolt/Core/BinaryData.h"
1818
#include "bolt/Core/BinarySection.h"
1919
#include "bolt/Core/DebugData.h"
20+
#include "bolt/Core/DynoStats.h"
2021
#include "bolt/Core/JumpTable.h"
2122
#include "bolt/Core/MCPlusBuilder.h"
2223
#include "bolt/RuntimeLibs/RuntimeLibrary.h"
@@ -717,6 +718,9 @@ class BinaryContext {
717718
uint64_t NumStaleBlocksWithEqualIcount{0};
718719
} Stats;
719720

721+
// Original binary execution count stats.
722+
DynoStats InitialDynoStats;
723+
720724
// Address of the first allocated segment.
721725
uint64_t FirstAllocAddress{std::numeric_limits<uint64_t>::max()};
722726

bolt/include/bolt/Passes/BinaryPasses.h

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,31 @@ class BinaryFunctionPass {
5353
virtual Error runOnFunctions(BinaryContext &BC) = 0;
5454
};
5555

56+
/// A pass to set initial program-wide dynostats.
57+
class DynoStatsSetPass : public BinaryFunctionPass {
58+
public:
59+
DynoStatsSetPass() : BinaryFunctionPass(false) {}
60+
61+
const char *getName() const override {
62+
return "set dyno-stats before optimizations";
63+
}
64+
65+
bool shouldPrint(const BinaryFunction &BF) const override { return false; }
66+
67+
Error runOnFunctions(BinaryContext &BC) override {
68+
BC.InitialDynoStats = getDynoStats(BC.getBinaryFunctions(), BC.isAArch64());
69+
return Error::success();
70+
}
71+
};
72+
5673
/// A pass to print program-wide dynostats.
5774
class DynoStatsPrintPass : public BinaryFunctionPass {
5875
protected:
59-
DynoStats PrevDynoStats;
6076
std::string Title;
6177

6278
public:
63-
DynoStatsPrintPass(const DynoStats &PrevDynoStats, const char *Title)
64-
: BinaryFunctionPass(false), PrevDynoStats(PrevDynoStats), Title(Title) {}
79+
DynoStatsPrintPass(const char *Title)
80+
: BinaryFunctionPass(false), Title(Title) {}
6581

6682
const char *getName() const override {
6783
return "print dyno-stats after optimizations";
@@ -70,6 +86,7 @@ class DynoStatsPrintPass : public BinaryFunctionPass {
7086
bool shouldPrint(const BinaryFunction &BF) const override { return false; }
7187

7288
Error runOnFunctions(BinaryContext &BC) override {
89+
const DynoStats PrevDynoStats = BC.InitialDynoStats;
7390
const DynoStats NewDynoStats =
7491
getDynoStats(BC.getBinaryFunctions(), BC.isAArch64());
7592
const bool Changed = (NewDynoStats != PrevDynoStats);

bolt/lib/Core/BinaryContext.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ BinaryContext::BinaryContext(std::unique_ptr<MCContext> Ctx,
142142
AsmInfo(std::move(AsmInfo)), MII(std::move(MII)), STI(std::move(STI)),
143143
InstPrinter(std::move(InstPrinter)), MIA(std::move(MIA)),
144144
MIB(std::move(MIB)), MRI(std::move(MRI)), DisAsm(std::move(DisAsm)),
145-
Logger(Logger) {
145+
Logger(Logger), InitialDynoStats(isAArch64()) {
146146
Relocation::Arch = this->TheTriple->getArch();
147147
RegularPageSize = isAArch64() ? RegularPageSizeAArch64 : RegularPageSizeX86;
148148
PageAlign = opts::NoHugePages ? RegularPageSize : HugePageSize;

bolt/lib/Rewrite/BinaryPassManager.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -343,8 +343,7 @@ Error BinaryFunctionPassManager::runAllPasses(BinaryContext &BC) {
343343
Manager.registerPass(
344344
std::make_unique<EstimateEdgeCounts>(PrintEstimateEdgeCounts));
345345

346-
const DynoStats InitialDynoStats =
347-
getDynoStats(BC.getBinaryFunctions(), BC.isAArch64());
346+
Manager.registerPass(std::make_unique<DynoStatsSetPass>());
348347

349348
Manager.registerPass(std::make_unique<AsmDumpPass>(),
350349
opts::AsmDump.getNumOccurrences());
@@ -456,10 +455,9 @@ Error BinaryFunctionPassManager::runAllPasses(BinaryContext &BC) {
456455
Manager.registerPass(std::make_unique<SplitFunctions>(PrintSplit));
457456

458457
// Print final dyno stats right while CFG and instruction analysis are intact.
459-
Manager.registerPass(
460-
std::make_unique<DynoStatsPrintPass>(
461-
InitialDynoStats, "after all optimizations before SCTC and FOP"),
462-
opts::PrintDynoStats || opts::DynoStatsAll);
458+
Manager.registerPass(std::make_unique<DynoStatsPrintPass>(
459+
"after all optimizations before SCTC and FOP"),
460+
opts::PrintDynoStats || opts::DynoStatsAll);
463461

464462
// Add the StokeInfo pass, which extract functions for stoke optimization and
465463
// get the liveness information for them

0 commit comments

Comments
 (0)