Skip to content

Commit 2e26d09

Browse files
authored
BlockFrequencyInfo: Add PrintBlockFreq helper (#67512)
- Refactor the (Machine)BlockFrequencyInfo::printBlockFreq functions into a `PrintBlockFreq()` function returning a `Printable` object. This simplifies usage as it can be directly piped to a `raw_ostream` like `dbgs() << PrintBlockFreq(MBFI, Freq) << '\n';`. - Previously there was an interesting behavior where `BlockFrequencyInfoImpl` stores frequencies both as a `Scaled64` number and as an `uint64_t`. Most algorithms use the `BlockFrequency` abstraction with the integers, the print function for basic blocks printed the `Scaled64` number potentially showing higher accuracy than was used by the algorithm. This changes things to only print `BlockFrequency` values. - Replace some instances of `dbgs() << Freq.getFrequency()` with the new function.
1 parent 86d1f4c commit 2e26d09

11 files changed

+93
-110
lines changed

llvm/include/llvm/Analysis/BlockFrequencyInfo.h

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "llvm/IR/PassManager.h"
1717
#include "llvm/Pass.h"
1818
#include "llvm/Support/BlockFrequency.h"
19+
#include "llvm/Support/Printable.h"
1920
#include <cstdint>
2021
#include <memory>
2122
#include <optional>
@@ -92,14 +93,6 @@ class BlockFrequencyInfo {
9293
void calculate(const Function &F, const BranchProbabilityInfo &BPI,
9394
const LoopInfo &LI);
9495

95-
// Print the block frequency Freq to OS using the current functions entry
96-
// frequency to convert freq into a relative decimal form.
97-
raw_ostream &printBlockFreq(raw_ostream &OS, BlockFrequency Freq) const;
98-
99-
// Convenience method that attempts to look up the frequency associated with
100-
// BB and print it to OS.
101-
raw_ostream &printBlockFreq(raw_ostream &OS, const BasicBlock *BB) const;
102-
10396
BlockFrequency getEntryFreq() const;
10497
void releaseMemory();
10598
void print(raw_ostream &OS) const;
@@ -108,6 +101,15 @@ class BlockFrequencyInfo {
108101
void verifyMatch(BlockFrequencyInfo &Other) const;
109102
};
110103

104+
/// Print the block frequency @p Freq relative to the current functions entry
105+
/// frequency. Returns a Printable object that can be piped via `<<` to a
106+
/// `raw_ostream`.
107+
Printable printBlockFreq(const BlockFrequencyInfo &BFI, BlockFrequency Freq);
108+
109+
/// Convenience function equivalent to calling
110+
/// `printBlockFreq(BFI, BFI.getBlocakFreq(&BB))`.
111+
Printable printBlockFreq(const BlockFrequencyInfo &BFI, const BasicBlock &BB);
112+
111113
/// Analysis pass which computes \c BlockFrequencyInfo.
112114
class BlockFrequencyAnalysis
113115
: public AnalysisInfoMixin<BlockFrequencyAnalysis> {

llvm/include/llvm/Analysis/BlockFrequencyInfoImpl.h

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -533,15 +533,15 @@ class BlockFrequencyInfoImplBase {
533533

534534
void setBlockFreq(const BlockNode &Node, BlockFrequency Freq);
535535

536-
raw_ostream &printBlockFreq(raw_ostream &OS, const BlockNode &Node) const;
537-
raw_ostream &printBlockFreq(raw_ostream &OS, BlockFrequency Freq) const;
538-
539536
BlockFrequency getEntryFreq() const {
540537
assert(!Freqs.empty());
541538
return BlockFrequency(Freqs[0].Integer);
542539
}
543540
};
544541

542+
void printBlockFreqImpl(raw_ostream &OS, BlockFrequency EntryFreq,
543+
BlockFrequency Freq);
544+
545545
namespace bfi_detail {
546546

547547
template <class BlockT> struct TypeMap {};
@@ -1067,11 +1067,6 @@ template <class BT> class BlockFrequencyInfoImpl : BlockFrequencyInfoImplBase {
10671067
raw_ostream &print(raw_ostream &OS) const override;
10681068

10691069
using BlockFrequencyInfoImplBase::dump;
1070-
using BlockFrequencyInfoImplBase::printBlockFreq;
1071-
1072-
raw_ostream &printBlockFreq(raw_ostream &OS, const BlockT *BB) const {
1073-
return BlockFrequencyInfoImplBase::printBlockFreq(OS, getNode(BB));
1074-
}
10751070

10761071
void verifyMatch(BlockFrequencyInfoImpl<BT> &Other) const;
10771072
};
@@ -1862,7 +1857,7 @@ struct BFIDOTGraphTraitsBase : public DefaultDOTGraphTraits {
18621857
OS << Node->getName() << " : ";
18631858
switch (GType) {
18641859
case GVDT_Fraction:
1865-
Graph->printBlockFreq(OS, Node);
1860+
OS << printBlockFreq(*Graph, *Node);
18661861
break;
18671862
case GVDT_Integer:
18681863
OS << Graph->getBlockFreq(Node).getFrequency();

llvm/include/llvm/CodeGen/MBFIWrapper.h

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
#include "llvm/ADT/DenseMap.h"
1818
#include "llvm/Support/BlockFrequency.h"
19-
#include "llvm/Support/raw_ostream.h"
2019
#include <optional>
2120

2221
namespace llvm {
@@ -33,14 +32,11 @@ class MBFIWrapper {
3332
std::optional<uint64_t>
3433
getBlockProfileCount(const MachineBasicBlock *MBB) const;
3534

36-
raw_ostream &printBlockFreq(raw_ostream &OS,
37-
const MachineBasicBlock *MBB) const;
38-
raw_ostream &printBlockFreq(raw_ostream &OS, BlockFrequency Freq) const;
3935
void view(const Twine &Name, bool isSimple = true);
4036
BlockFrequency getEntryFreq() const;
41-
const MachineBlockFrequencyInfo &getMBFI() { return MBFI; }
37+
const MachineBlockFrequencyInfo &getMBFI() const { return MBFI; }
4238

43-
private:
39+
private:
4440
const MachineBlockFrequencyInfo &MBFI;
4541
DenseMap<const MachineBasicBlock *, BlockFrequency> MergedBBFreq;
4642
};

llvm/include/llvm/CodeGen/MachineBlockFrequencyInfo.h

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -91,20 +91,22 @@ class MachineBlockFrequencyInfo : public MachineFunctionPass {
9191
/// rendered using dot.
9292
void view(const Twine &Name, bool isSimple = true) const;
9393

94-
// Print the block frequency Freq to OS using the current functions entry
95-
// frequency to convert freq into a relative decimal form.
96-
raw_ostream &printBlockFreq(raw_ostream &OS, const BlockFrequency Freq) const;
97-
98-
// Convenience method that attempts to look up the frequency associated with
99-
// BB and print it to OS.
100-
raw_ostream &printBlockFreq(raw_ostream &OS,
101-
const MachineBasicBlock *MBB) const;
102-
10394
/// Divide a block's BlockFrequency::getFrequency() value by this value to
10495
/// obtain the entry block - relative frequency of said block.
10596
BlockFrequency getEntryFreq() const;
10697
};
10798

99+
/// Print the block frequency @p Freq relative to the current functions entry
100+
/// frequency. Returns a Printable object that can be piped via `<<` to a
101+
/// `raw_ostream`.
102+
Printable printBlockFreq(const MachineBlockFrequencyInfo &MBFI,
103+
BlockFrequency Freq);
104+
105+
/// Convenience function equivalent to calling
106+
/// `printBlockFreq(MBFI, MBFI.getBlockFreq(&MBB))`.
107+
Printable printBlockFreq(const MachineBlockFrequencyInfo &MBFI,
108+
const MachineBasicBlock &MBB);
109+
108110
} // end namespace llvm
109111

110112
#endif // LLVM_CODEGEN_MACHINEBLOCKFREQUENCYINFO_H

llvm/lib/Analysis/BlockFrequencyInfo.cpp

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -78,14 +78,13 @@ cl::opt<PGOViewCountsType> PGOViewCounts(
7878
clEnumValN(PGOVCT_Graph, "graph", "show a graph."),
7979
clEnumValN(PGOVCT_Text, "text", "show in text.")));
8080

81-
static cl::opt<bool> PrintBlockFreq(
82-
"print-bfi", cl::init(false), cl::Hidden,
83-
cl::desc("Print the block frequency info."));
84-
85-
cl::opt<std::string> PrintBlockFreqFuncName(
86-
"print-bfi-func-name", cl::Hidden,
87-
cl::desc("The option to specify the name of the function "
88-
"whose block frequency info is printed."));
81+
static cl::opt<bool> PrintBFI("print-bfi", cl::init(false), cl::Hidden,
82+
cl::desc("Print the block frequency info."));
83+
84+
cl::opt<std::string>
85+
PrintBFIFuncName("print-bfi-func-name", cl::Hidden,
86+
cl::desc("The option to specify the name of the function "
87+
"whose block frequency info is printed."));
8988
} // namespace llvm
9089

9190
namespace llvm {
@@ -193,9 +192,8 @@ void BlockFrequencyInfo::calculate(const Function &F,
193192
F.getName().equals(ViewBlockFreqFuncName))) {
194193
view();
195194
}
196-
if (PrintBlockFreq &&
197-
(PrintBlockFreqFuncName.empty() ||
198-
F.getName().equals(PrintBlockFreqFuncName))) {
195+
if (PrintBFI &&
196+
(PrintBFIFuncName.empty() || F.getName().equals(PrintBFIFuncName))) {
199197
print(dbgs());
200198
}
201199
}
@@ -267,17 +265,6 @@ const BranchProbabilityInfo *BlockFrequencyInfo::getBPI() const {
267265
return BFI ? &BFI->getBPI() : nullptr;
268266
}
269267

270-
raw_ostream &BlockFrequencyInfo::printBlockFreq(raw_ostream &OS,
271-
BlockFrequency Freq) const {
272-
return BFI ? BFI->printBlockFreq(OS, Freq) : OS;
273-
}
274-
275-
raw_ostream &
276-
BlockFrequencyInfo::printBlockFreq(raw_ostream &OS,
277-
const BasicBlock *BB) const {
278-
return BFI ? BFI->printBlockFreq(OS, BB) : OS;
279-
}
280-
281268
BlockFrequency BlockFrequencyInfo::getEntryFreq() const {
282269
return BFI ? BFI->getEntryFreq() : BlockFrequency(0);
283270
}
@@ -294,6 +281,18 @@ void BlockFrequencyInfo::verifyMatch(BlockFrequencyInfo &Other) const {
294281
BFI->verifyMatch(*Other.BFI);
295282
}
296283

284+
Printable llvm::printBlockFreq(const BlockFrequencyInfo &BFI,
285+
BlockFrequency Freq) {
286+
return Printable([&BFI, Freq](raw_ostream &OS) {
287+
printBlockFreqImpl(OS, BFI.getEntryFreq(), Freq);
288+
});
289+
}
290+
291+
Printable llvm::printBlockFreq(const BlockFrequencyInfo &BFI,
292+
const BasicBlock &BB) {
293+
return printBlockFreq(BFI, BFI.getBlockFreq(&BB));
294+
}
295+
297296
INITIALIZE_PASS_BEGIN(BlockFrequencyInfoWrapperPass, "block-freq",
298297
"Block Frequency Analysis", true, true)
299298
INITIALIZE_PASS_DEPENDENCY(BranchProbabilityInfoWrapperPass)

llvm/lib/Analysis/BlockFrequencyInfoImpl.cpp

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -640,19 +640,19 @@ BlockFrequencyInfoImplBase::getLoopName(const LoopData &Loop) const {
640640
return getBlockName(Loop.getHeader()) + (Loop.isIrreducible() ? "**" : "*");
641641
}
642642

643-
raw_ostream &
644-
BlockFrequencyInfoImplBase::printBlockFreq(raw_ostream &OS,
645-
const BlockNode &Node) const {
646-
return OS << getFloatingBlockFreq(Node);
647-
}
648-
649-
raw_ostream &
650-
BlockFrequencyInfoImplBase::printBlockFreq(raw_ostream &OS,
651-
BlockFrequency Freq) const {
643+
void llvm::printBlockFreqImpl(raw_ostream &OS, BlockFrequency EntryFreq,
644+
BlockFrequency Freq) {
645+
if (Freq == BlockFrequency(0)) {
646+
OS << "0";
647+
return;
648+
}
649+
if (EntryFreq == BlockFrequency(0)) {
650+
OS << "<invalid BFI>";
651+
return;
652+
}
652653
Scaled64 Block(Freq.getFrequency(), 0);
653-
Scaled64 Entry(getEntryFreq().getFrequency(), 0);
654-
655-
return OS << Block / Entry;
654+
Scaled64 Entry(EntryFreq.getFrequency(), 0);
655+
OS << Block / Entry;
656656
}
657657

658658
void IrreducibleGraph::addNodesInLoop(const BFIBase::LoopData &OuterLoop) {

llvm/lib/CodeGen/MBFIWrapper.cpp

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,16 +43,6 @@ MBFIWrapper::getBlockProfileCount(const MachineBasicBlock *MBB) const {
4343
return MBFI.getBlockProfileCount(MBB);
4444
}
4545

46-
raw_ostream & MBFIWrapper::printBlockFreq(raw_ostream &OS,
47-
const MachineBasicBlock *MBB) const {
48-
return MBFI.printBlockFreq(OS, getBlockFreq(MBB));
49-
}
50-
51-
raw_ostream & MBFIWrapper::printBlockFreq(raw_ostream &OS,
52-
BlockFrequency Freq) const {
53-
return MBFI.printBlockFreq(OS, Freq);
54-
}
55-
5646
void MBFIWrapper::view(const Twine &Name, bool isSimple) {
5747
MBFI.view(Name, isSimple);
5848
}

llvm/lib/CodeGen/MachineBlockFrequencyInfo.cpp

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ static cl::opt<bool> PrintMachineBlockFreq(
7575

7676
// Command line option to specify the name of the function for block frequency
7777
// dump. Defined in Analysis/BlockFrequencyInfo.cpp.
78-
extern cl::opt<std::string> PrintBlockFreqFuncName;
78+
extern cl::opt<std::string> PrintBFIFuncName;
7979
} // namespace llvm
8080

8181
static GVDAGType getGVDT() {
@@ -203,8 +203,7 @@ void MachineBlockFrequencyInfo::calculate(
203203
view("MachineBlockFrequencyDAGS." + F.getName());
204204
}
205205
if (PrintMachineBlockFreq &&
206-
(PrintBlockFreqFuncName.empty() ||
207-
F.getName().equals(PrintBlockFreqFuncName))) {
206+
(PrintBFIFuncName.empty() || F.getName().equals(PrintBFIFuncName))) {
208207
MBFI->print(dbgs());
209208
}
210209
}
@@ -274,18 +273,18 @@ const MachineBranchProbabilityInfo *MachineBlockFrequencyInfo::getMBPI() const {
274273
return MBFI ? &MBFI->getBPI() : nullptr;
275274
}
276275

277-
raw_ostream &
278-
MachineBlockFrequencyInfo::printBlockFreq(raw_ostream &OS,
279-
const BlockFrequency Freq) const {
280-
return MBFI ? MBFI->printBlockFreq(OS, Freq) : OS;
276+
BlockFrequency MachineBlockFrequencyInfo::getEntryFreq() const {
277+
return MBFI ? MBFI->getEntryFreq() : BlockFrequency(0);
281278
}
282279

283-
raw_ostream &
284-
MachineBlockFrequencyInfo::printBlockFreq(raw_ostream &OS,
285-
const MachineBasicBlock *MBB) const {
286-
return MBFI ? MBFI->printBlockFreq(OS, MBB) : OS;
280+
Printable llvm::printBlockFreq(const MachineBlockFrequencyInfo &MBFI,
281+
BlockFrequency Freq) {
282+
return Printable([&MBFI, Freq](raw_ostream &OS) {
283+
printBlockFreqImpl(OS, MBFI.getEntryFreq(), Freq);
284+
});
287285
}
288286

289-
BlockFrequency MachineBlockFrequencyInfo::getEntryFreq() const {
290-
return MBFI ? MBFI->getEntryFreq() : BlockFrequency(0);
287+
Printable llvm::printBlockFreq(const MachineBlockFrequencyInfo &MBFI,
288+
const MachineBasicBlock &MBB) {
289+
return printBlockFreq(MBFI, MBFI.getBlockFreq(&MBB));
291290
}

llvm/lib/CodeGen/MachineBlockPlacement.cpp

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1729,8 +1729,9 @@ MachineBasicBlock *MachineBlockPlacement::selectBestCandidateBlock(
17291729
"Found CFG-violating block");
17301730

17311731
BlockFrequency CandidateFreq = MBFI->getBlockFreq(MBB);
1732-
LLVM_DEBUG(dbgs() << " " << getBlockName(MBB) << " -> ";
1733-
MBFI->printBlockFreq(dbgs(), CandidateFreq) << " (freq)\n");
1732+
LLVM_DEBUG(dbgs() << " " << getBlockName(MBB) << " -> "
1733+
<< printBlockFreq(MBFI->getMBFI(), CandidateFreq)
1734+
<< " (freq)\n");
17341735

17351736
// For ehpad, we layout the least probable first as to avoid jumping back
17361737
// from least probable landingpads to more probable ones.
@@ -2095,8 +2096,8 @@ MachineBlockPlacement::findBestLoopTopHelper(
20952096
if (Pred == L.getHeader())
20962097
continue;
20972098
LLVM_DEBUG(dbgs() << " old top pred: " << getBlockName(Pred) << ", has "
2098-
<< Pred->succ_size() << " successors, ";
2099-
MBFI->printBlockFreq(dbgs(), Pred) << " freq\n");
2099+
<< Pred->succ_size() << " successors, "
2100+
<< printBlockFreq(MBFI->getMBFI(), *Pred) << " freq\n");
21002101
if (Pred->succ_size() > 2)
21012102
continue;
21022103

@@ -2240,10 +2241,10 @@ MachineBlockPlacement::findBestLoopExit(const MachineLoop &L,
22402241
}
22412242

22422243
BlockFrequency ExitEdgeFreq = MBFI->getBlockFreq(MBB) * SuccProb;
2243-
LLVM_DEBUG(dbgs() << " exiting: " << getBlockName(MBB) << " -> "
2244-
<< getBlockName(Succ) << " [L:" << SuccLoopDepth
2245-
<< "] (";
2246-
MBFI->printBlockFreq(dbgs(), ExitEdgeFreq) << ")\n");
2244+
LLVM_DEBUG(
2245+
dbgs() << " exiting: " << getBlockName(MBB) << " -> "
2246+
<< getBlockName(Succ) << " [L:" << SuccLoopDepth << "] ("
2247+
<< printBlockFreq(MBFI->getMBFI(), ExitEdgeFreq) << ")\n");
22472248
// Note that we bias this toward an existing layout successor to retain
22482249
// incoming order in the absence of better information. The exit must have
22492250
// a frequency higher than the current exit before we consider breaking
@@ -2537,8 +2538,8 @@ void MachineBlockPlacement::rotateLoopWithProfile(
25372538
}
25382539

25392540
LLVM_DEBUG(dbgs() << "The cost of loop rotation by making "
2540-
<< getBlockName(*Iter)
2541-
<< " to the top: " << Cost.getFrequency() << "\n");
2541+
<< getBlockName(*Iter) << " to the top: "
2542+
<< printBlockFreq(MBFI->getMBFI(), Cost) << "\n");
25422543

25432544
if (Cost < SmallestRotationCost) {
25442545
SmallestRotationCost = Cost;

llvm/lib/CodeGen/RegAllocGreedy.cpp

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1061,8 +1061,8 @@ MCRegister RAGreedy::tryRegionSplit(const LiveInterval &VirtReg,
10611061
// No benefit from the compact region, our fallback will be per-block
10621062
// splitting. Make sure we find a solution that is cheaper than spilling.
10631063
BestCost = SpillCost;
1064-
LLVM_DEBUG(dbgs() << "Cost of isolating all blocks = ";
1065-
MBFI->printBlockFreq(dbgs(), BestCost) << '\n');
1064+
LLVM_DEBUG(dbgs() << "Cost of isolating all blocks = "
1065+
<< printBlockFreq(*MBFI, BestCost) << '\n');
10661066
}
10671067

10681068
unsigned BestCand = calculateRegionSplitCost(VirtReg, Order, BestCost,
@@ -1112,8 +1112,8 @@ RAGreedy::calculateRegionSplitCostAroundReg(MCPhysReg PhysReg,
11121112
LLVM_DEBUG(dbgs() << printReg(PhysReg, TRI) << "\tno positive bundles\n");
11131113
return BestCand;
11141114
}
1115-
LLVM_DEBUG(dbgs() << printReg(PhysReg, TRI) << "\tstatic = ";
1116-
MBFI->printBlockFreq(dbgs(), Cost));
1115+
LLVM_DEBUG(dbgs() << printReg(PhysReg, TRI)
1116+
<< "\tstatic = " << printBlockFreq(*MBFI, Cost));
11171117
if (Cost >= BestCost) {
11181118
LLVM_DEBUG({
11191119
if (BestCand == NoCand)
@@ -1139,8 +1139,7 @@ RAGreedy::calculateRegionSplitCostAroundReg(MCPhysReg PhysReg,
11391139

11401140
Cost += calcGlobalSplitCost(Cand, Order);
11411141
LLVM_DEBUG({
1142-
dbgs() << ", total = ";
1143-
MBFI->printBlockFreq(dbgs(), Cost) << " with bundles";
1142+
dbgs() << ", total = " << printBlockFreq(*MBFI, Cost) << " with bundles";
11441143
for (int I : Cand.LiveBundles.set_bits())
11451144
dbgs() << " EB#" << I;
11461145
dbgs() << ".\n";
@@ -2313,9 +2312,9 @@ void RAGreedy::tryHintRecoloring(const LiveInterval &VirtReg) {
23132312
LLVM_DEBUG(dbgs() << "Checking profitability:\n");
23142313
BlockFrequency OldCopiesCost = getBrokenHintFreq(Info, CurrPhys);
23152314
BlockFrequency NewCopiesCost = getBrokenHintFreq(Info, PhysReg);
2316-
LLVM_DEBUG(dbgs() << "Old Cost: " << OldCopiesCost.getFrequency()
2317-
<< "\nNew Cost: " << NewCopiesCost.getFrequency()
2318-
<< '\n');
2315+
LLVM_DEBUG(dbgs() << "Old Cost: " << printBlockFreq(*MBFI, OldCopiesCost)
2316+
<< "\nNew Cost: "
2317+
<< printBlockFreq(*MBFI, NewCopiesCost) << '\n');
23192318
if (OldCopiesCost < NewCopiesCost) {
23202319
LLVM_DEBUG(dbgs() << "=> Not profitable.\n");
23212320
continue;

0 commit comments

Comments
 (0)