Skip to content

Commit 6c2bde9

Browse files
authored
[nfc][instr] Encapsulate CFGMST (#72207)
Very little of it needs to be public.
1 parent f6c4bb0 commit 6c2bde9

File tree

3 files changed

+58
-50
lines changed

3 files changed

+58
-50
lines changed

llvm/include/llvm/Transforms/Instrumentation/CFGMST.h

Lines changed: 36 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ namespace llvm {
3535
/// Implements a Union-find algorithm to compute Minimum Spanning Tree
3636
/// for a given CFG.
3737
template <class Edge, class BBInfo> class CFGMST {
38-
public:
3938
Function &F;
4039

4140
// Store all the edges in CFG. It may contain some stale edges
@@ -49,6 +48,12 @@ template <class Edge, class BBInfo> class CFGMST {
4948
// (For function with an infinite loop, this block may be absent)
5049
bool ExitBlockFound = false;
5150

51+
BranchProbabilityInfo *const BPI;
52+
BlockFrequencyInfo *const BFI;
53+
54+
// If function entry will be always instrumented.
55+
const bool InstrumentFuncEntry;
56+
5257
// Find the root group of the G and compress the path from G to the root.
5358
BBInfo *findAndCompressGroup(BBInfo *G) {
5459
if (G->Group != G)
@@ -77,21 +82,6 @@ template <class Edge, class BBInfo> class CFGMST {
7782
return true;
7883
}
7984

80-
// Give BB, return the auxiliary information.
81-
BBInfo &getBBInfo(const BasicBlock *BB) const {
82-
auto It = BBInfos.find(BB);
83-
assert(It->second.get() != nullptr);
84-
return *It->second.get();
85-
}
86-
87-
// Give BB, return the auxiliary information if it's available.
88-
BBInfo *findBBInfo(const BasicBlock *BB) const {
89-
auto It = BBInfos.find(BB);
90-
if (It == BBInfos.end())
91-
return nullptr;
92-
return It->second.get();
93-
}
94-
9585
// Traverse the CFG using a stack. Find all the edges and assign the weight.
9686
// Edges with large weight will be put into MST first so they are less likely
9787
// to be instrumented.
@@ -236,6 +226,7 @@ template <class Edge, class BBInfo> class CFGMST {
236226
}
237227
}
238228

229+
public:
239230
// Dump the Debug information about the instrumentation.
240231
void dumpEdges(raw_ostream &OS, const Twine &Message) const {
241232
if (!Message.str().empty())
@@ -274,25 +265,42 @@ template <class Edge, class BBInfo> class CFGMST {
274265
return *AllEdges.back();
275266
}
276267

277-
BranchProbabilityInfo *BPI;
278-
BlockFrequencyInfo *BFI;
279-
280-
// If function entry will be always instrumented.
281-
bool InstrumentFuncEntry;
282-
283-
public:
284-
CFGMST(Function &Func, bool InstrumentFuncEntry_,
285-
BranchProbabilityInfo *BPI_ = nullptr,
286-
BlockFrequencyInfo *BFI_ = nullptr)
287-
: F(Func), BPI(BPI_), BFI(BFI_),
288-
InstrumentFuncEntry(InstrumentFuncEntry_) {
268+
CFGMST(Function &Func, bool InstrumentFuncEntry,
269+
BranchProbabilityInfo *BPI = nullptr,
270+
BlockFrequencyInfo *BFI = nullptr)
271+
: F(Func), BPI(BPI), BFI(BFI), InstrumentFuncEntry(InstrumentFuncEntry) {
289272
buildEdges();
290273
sortEdgesByWeight();
291274
computeMinimumSpanningTree();
292275
if (AllEdges.size() > 1 && InstrumentFuncEntry)
293276
std::iter_swap(std::move(AllEdges.begin()),
294277
std::move(AllEdges.begin() + AllEdges.size() - 1));
295278
}
279+
280+
const std::vector<std::unique_ptr<Edge>> &allEdges() const {
281+
return AllEdges;
282+
}
283+
284+
std::vector<std::unique_ptr<Edge>> &allEdges() { return AllEdges; }
285+
286+
size_t numEdges() const { return AllEdges.size(); }
287+
288+
size_t bbInfoSize() const { return BBInfos.size(); }
289+
290+
// Give BB, return the auxiliary information.
291+
BBInfo &getBBInfo(const BasicBlock *BB) const {
292+
auto It = BBInfos.find(BB);
293+
assert(It->second.get() != nullptr);
294+
return *It->second.get();
295+
}
296+
297+
// Give BB, return the auxiliary information if it's available.
298+
BBInfo *findBBInfo(const BasicBlock *BB) const {
299+
auto It = BBInfos.find(BB);
300+
if (It == BBInfos.end())
301+
return nullptr;
302+
return It->second.get();
303+
}
296304
};
297305

298306
} // end namespace llvm

llvm/lib/Transforms/Instrumentation/GCOVProfiling.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -750,7 +750,7 @@ static BasicBlock *getInstrBB(CFGMST<Edge, BBInfo> &MST, Edge &E,
750750
#ifndef NDEBUG
751751
static void dumpEdges(CFGMST<Edge, BBInfo> &MST, GCOVFunction &GF) {
752752
size_t ID = 0;
753-
for (auto &E : make_pointee_range(MST.AllEdges)) {
753+
for (const auto &E : make_pointee_range(MST.allEdges())) {
754754
GCOVBlock &Src = E.SrcBB ? GF.getBlock(E.SrcBB) : GF.getEntryBlock();
755755
GCOVBlock &Dst = E.DestBB ? GF.getBlock(E.DestBB) : GF.getReturnBlock();
756756
dbgs() << " Edge " << ID++ << ": " << Src.Number << "->" << Dst.Number
@@ -820,8 +820,8 @@ bool GCOVProfiler::emitProfileNotes(
820820
CFGMST<Edge, BBInfo> MST(F, /*InstrumentFuncEntry_=*/false, BPI, BFI);
821821

822822
// getInstrBB can split basic blocks and push elements to AllEdges.
823-
for (size_t I : llvm::seq<size_t>(0, MST.AllEdges.size())) {
824-
auto &E = *MST.AllEdges[I];
823+
for (size_t I : llvm::seq<size_t>(0, MST.numEdges())) {
824+
auto &E = *MST.allEdges()[I];
825825
// For now, disable spanning tree optimization when fork or exec* is
826826
// used.
827827
if (HasExecOrFork)
@@ -836,16 +836,16 @@ bool GCOVProfiler::emitProfileNotes(
836836

837837
// Some non-tree edges are IndirectBr which cannot be split. Ignore them
838838
// as well.
839-
llvm::erase_if(MST.AllEdges, [](std::unique_ptr<Edge> &E) {
839+
llvm::erase_if(MST.allEdges(), [](std::unique_ptr<Edge> &E) {
840840
return E->Removed || (!E->InMST && !E->Place);
841841
});
842842
const size_t Measured =
843843
std::stable_partition(
844-
MST.AllEdges.begin(), MST.AllEdges.end(),
844+
MST.allEdges().begin(), MST.allEdges().end(),
845845
[](std::unique_ptr<Edge> &E) { return E->Place; }) -
846-
MST.AllEdges.begin();
846+
MST.allEdges().begin();
847847
for (size_t I : llvm::seq<size_t>(0, Measured)) {
848-
Edge &E = *MST.AllEdges[I];
848+
Edge &E = *MST.allEdges()[I];
849849
GCOVBlock &Src =
850850
E.SrcBB ? Func.getBlock(E.SrcBB) : Func.getEntryBlock();
851851
GCOVBlock &Dst =
@@ -854,13 +854,13 @@ bool GCOVProfiler::emitProfileNotes(
854854
E.DstNumber = Dst.Number;
855855
}
856856
std::stable_sort(
857-
MST.AllEdges.begin(), MST.AllEdges.begin() + Measured,
857+
MST.allEdges().begin(), MST.allEdges().begin() + Measured,
858858
[](const std::unique_ptr<Edge> &L, const std::unique_ptr<Edge> &R) {
859859
return L->SrcNumber != R->SrcNumber ? L->SrcNumber < R->SrcNumber
860860
: L->DstNumber < R->DstNumber;
861861
});
862862

863-
for (const Edge &E : make_pointee_range(MST.AllEdges)) {
863+
for (const Edge &E : make_pointee_range(MST.allEdges())) {
864864
GCOVBlock &Src =
865865
E.SrcBB ? Func.getBlock(E.SrcBB) : Func.getEntryBlock();
866866
GCOVBlock &Dst =
@@ -917,7 +917,7 @@ bool GCOVProfiler::emitProfileNotes(
917917
CountersBySP.emplace_back(Counters, SP);
918918

919919
for (size_t I : llvm::seq<size_t>(0, Measured)) {
920-
const Edge &E = *MST.AllEdges[I];
920+
const Edge &E = *MST.allEdges()[I];
921921
IRBuilder<> Builder(E.Place, E.Place->getFirstInsertionPt());
922922
Value *V = Builder.CreateConstInBoundsGEP2_64(
923923
Counters->getValueType(), Counters, 0, I);

llvm/lib/Transforms/Instrumentation/PGOInstrumentation.cpp

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -582,12 +582,12 @@ template <class Edge, class BBInfo> class FuncPGOInstrumentation {
582582
if (!IsCS) {
583583
NumOfPGOSelectInsts += SIVisitor.getNumOfSelectInsts();
584584
NumOfPGOMemIntrinsics += ValueSites[IPVK_MemOPSize].size();
585-
NumOfPGOBB += MST.BBInfos.size();
585+
NumOfPGOBB += MST.bbInfoSize();
586586
ValueSites[IPVK_IndirectCallTarget] = VPC.get(IPVK_IndirectCallTarget);
587587
} else {
588588
NumOfCSPGOSelectInsts += SIVisitor.getNumOfSelectInsts();
589589
NumOfCSPGOMemIntrinsics += ValueSites[IPVK_MemOPSize].size();
590-
NumOfCSPGOBB += MST.BBInfos.size();
590+
NumOfCSPGOBB += MST.bbInfoSize();
591591
}
592592

593593
FuncName = getIRPGOFuncName(F);
@@ -597,7 +597,7 @@ template <class Edge, class BBInfo> class FuncPGOInstrumentation {
597597
renameComdatFunction();
598598
LLVM_DEBUG(dumpInfo("after CFGMST"));
599599

600-
for (auto &E : MST.AllEdges) {
600+
for (const auto &E : MST.allEdges()) {
601601
if (E->Removed)
602602
continue;
603603
IsCS ? NumOfCSPGOEdge++ : NumOfPGOEdge++;
@@ -640,7 +640,7 @@ void FuncPGOInstrumentation<Edge, BBInfo>::computeCFGHash() {
640640
FunctionHash = (uint64_t)SIVisitor.getNumOfSelectInsts() << 56 |
641641
(uint64_t)ValueSites[IPVK_IndirectCallTarget].size() << 48 |
642642
//(uint64_t)ValueSites[IPVK_MemOPSize].size() << 40 |
643-
(uint64_t)MST.AllEdges.size() << 32 | JC.getCRC();
643+
(uint64_t)MST.numEdges() << 32 | JC.getCRC();
644644
} else {
645645
// The higher 32 bits.
646646
auto updateJCH = [&JCH](uint64_t Num) {
@@ -654,7 +654,7 @@ void FuncPGOInstrumentation<Edge, BBInfo>::computeCFGHash() {
654654
if (BCI) {
655655
updateJCH(BCI->getInstrumentedBlocksHash());
656656
} else {
657-
updateJCH((uint64_t)MST.AllEdges.size());
657+
updateJCH((uint64_t)MST.numEdges());
658658
}
659659

660660
// Hash format for context sensitive profile. Reserve 4 bits for other
@@ -669,7 +669,7 @@ void FuncPGOInstrumentation<Edge, BBInfo>::computeCFGHash() {
669669
LLVM_DEBUG(dbgs() << "Function Hash Computation for " << F.getName() << ":\n"
670670
<< " CRC = " << JC.getCRC()
671671
<< ", Selects = " << SIVisitor.getNumOfSelectInsts()
672-
<< ", Edges = " << MST.AllEdges.size() << ", ICSites = "
672+
<< ", Edges = " << MST.numEdges() << ", ICSites = "
673673
<< ValueSites[IPVK_IndirectCallTarget].size());
674674
if (!PGOOldCFGHashing) {
675675
LLVM_DEBUG(dbgs() << ", Memops = " << ValueSites[IPVK_MemOPSize].size()
@@ -757,8 +757,8 @@ void FuncPGOInstrumentation<Edge, BBInfo>::getInstrumentBBs(
757757

758758
// Use a worklist as we will update the vector during the iteration.
759759
std::vector<Edge *> EdgeList;
760-
EdgeList.reserve(MST.AllEdges.size());
761-
for (auto &E : MST.AllEdges)
760+
EdgeList.reserve(MST.numEdges());
761+
for (const auto &E : MST.allEdges())
762762
EdgeList.push_back(E.get());
763763

764764
for (auto &E : EdgeList) {
@@ -1163,12 +1163,12 @@ class PGOUseFunc {
11631163
} // end anonymous namespace
11641164

11651165
/// Set up InEdges/OutEdges for all BBs in the MST.
1166-
static void
1167-
setupBBInfoEdges(FuncPGOInstrumentation<PGOUseEdge, PGOUseBBInfo> &FuncInfo) {
1166+
static void setupBBInfoEdges(
1167+
const FuncPGOInstrumentation<PGOUseEdge, PGOUseBBInfo> &FuncInfo) {
11681168
// This is not required when there is block coverage inference.
11691169
if (FuncInfo.BCI)
11701170
return;
1171-
for (auto &E : FuncInfo.MST.AllEdges) {
1171+
for (const auto &E : FuncInfo.MST.allEdges()) {
11721172
if (E->Removed)
11731173
continue;
11741174
const BasicBlock *SrcBB = E->SrcBB;
@@ -1224,7 +1224,7 @@ bool PGOUseFunc::setInstrumentedCounts(
12241224
// Set the profile count the Instrumented edges. There are BBs that not in
12251225
// MST but not instrumented. Need to set the edge count value so that we can
12261226
// populate the profile counts later.
1227-
for (auto &E : FuncInfo.MST.AllEdges) {
1227+
for (const auto &E : FuncInfo.MST.allEdges()) {
12281228
if (E->Removed || E->InMST)
12291229
continue;
12301230
const BasicBlock *SrcBB = E->SrcBB;

0 commit comments

Comments
 (0)