LLVM 23.0.0git
CFGMST.h
Go to the documentation of this file.
1//===-- CFGMST.h - Minimum Spanning Tree for CFG ----------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file implements a Union-find algorithm to compute Minimum Spanning Tree
10// for a given CFG.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_TRANSFORMS_INSTRUMENTATION_CFGMST_H
15#define LLVM_TRANSFORMS_INSTRUMENTATION_CFGMST_H
16
17#include "llvm/ADT/DenseMap.h"
18#include "llvm/ADT/DenseSet.h"
19#include "llvm/ADT/STLExtras.h"
22#include "llvm/Analysis/CFG.h"
27#include "llvm/Support/Debug.h"
30#include <utility>
31#include <vector>
32
33#define DEBUG_TYPE "cfgmst"
34
35namespace llvm {
36
37/// An union-find based Minimum Spanning Tree for CFG
38///
39/// Implements a Union-find algorithm to compute Minimum Spanning Tree
40/// for a given CFG.
41template <class Edge, class BBInfo> class CFGMST {
42 Function &F;
43
44 // Store all the edges in CFG. It may contain some stale edges
45 // when Removed is set.
46 std::vector<std::unique_ptr<Edge>> AllEdges;
47
48 // This map records the auxiliary information for each BB.
50
51 // Whehter the function has an exit block with no successors.
52 // (For function with an infinite loop, this block may be absent)
53 bool ExitBlockFound = false;
54
55 BranchProbabilityInfo *const BPI;
56 BlockFrequencyInfo *const BFI;
57 LoopInfo *const LI;
58
59 // If function entry will be always instrumented.
60 const bool InstrumentFuncEntry;
61
62 // If true loop entries will be always instrumented.
63 const bool InstrumentLoopEntries;
64
65 // Find the root group of the G and compress the path from G to the root.
66 BBInfo *findAndCompressGroup(BBInfo *G) {
67 if (G->Group != G)
68 G->Group = findAndCompressGroup(static_cast<BBInfo *>(G->Group));
69 return static_cast<BBInfo *>(G->Group);
70 }
71
72 // Union BB1 and BB2 into the same group and return true.
73 // Returns false if BB1 and BB2 are already in the same group.
74 bool unionGroups(const BasicBlock *BB1, const BasicBlock *BB2) {
75 BBInfo *BB1G = findAndCompressGroup(&getBBInfo(BB1));
76 BBInfo *BB2G = findAndCompressGroup(&getBBInfo(BB2));
77
78 if (BB1G == BB2G)
79 return false;
80
81 // Make the smaller rank tree a direct child or the root of high rank tree.
82 if (BB1G->Rank < BB2G->Rank)
83 BB1G->Group = BB2G;
84 else {
85 BB2G->Group = BB1G;
86 // If the ranks are the same, increment root of one tree by one.
87 if (BB1G->Rank == BB2G->Rank)
88 BB1G->Rank++;
89 }
90 return true;
91 }
92
93 void handleCoroSuspendEdge(Edge *E) {
94 // We must not add instrumentation to the BB representing the
95 // "suspend" path, else CoroSplit won't be able to lower
96 // llvm.coro.suspend to a tail call. We do want profiling info for
97 // the other branches (resume/destroy). So we do 2 things:
98 // 1. we prefer instrumenting those other edges by setting the weight
99 // of the "suspend" edge to max, and
100 // 2. we mark the edge as "Removed" to guarantee it is not considered
101 // for instrumentation. That could technically happen:
102 // (from test/Transforms/Coroutines/coro-split-musttail.ll)
103 //
104 // %suspend = call i8 @llvm.coro.suspend(token %save, i1 false)
105 // switch i8 %suspend, label %exit [
106 // i8 0, label %await.ready
107 // i8 1, label %exit
108 // ]
109 if (!E->DestBB)
110 return;
111 assert(E->SrcBB);
112 if (llvm::isPresplitCoroSuspendExitEdge(*E->SrcBB, *E->DestBB))
113 E->Removed = true;
114 }
115
116 // Traverse the CFG using a stack. Find all the edges and assign the weight.
117 // Edges with large weight will be put into MST first so they are less likely
118 // to be instrumented.
119 void buildEdges() {
120 LLVM_DEBUG(dbgs() << "Build Edge on " << F.getName() << "\n");
121
122 BasicBlock *Entry = &(F.getEntryBlock());
123 uint64_t EntryWeight =
124 (BFI != nullptr ? BFI->getEntryFreq().getFrequency() : 2);
125 // If we want to instrument the entry count, lower the weight to 0.
126 if (InstrumentFuncEntry)
127 EntryWeight = 0;
128 Edge *EntryIncoming = nullptr, *EntryOutgoing = nullptr,
129 *ExitOutgoing = nullptr, *ExitIncoming = nullptr;
130 uint64_t MaxEntryOutWeight = 0, MaxExitOutWeight = 0, MaxExitInWeight = 0;
131
132 // Add a fake edge to the entry.
133 EntryIncoming = &addEdge(nullptr, Entry, EntryWeight);
134 LLVM_DEBUG(dbgs() << " Edge: from fake node to " << Entry->getName()
135 << " w = " << EntryWeight << "\n");
136
137 // Special handling for single BB functions.
138 if (succ_empty(Entry)) {
139 addEdge(Entry, nullptr, EntryWeight);
140 return;
141 }
142
143 static const uint32_t CriticalEdgeMultiplier = 1000;
144
145 for (BasicBlock &BB : F) {
146 Instruction *TI = BB.getTerminator();
147 uint64_t BBWeight =
148 (BFI != nullptr ? BFI->getBlockFreq(&BB).getFrequency() : 2);
149 uint64_t Weight = 2;
150 if (int successors = TI->getNumSuccessors()) {
151 for (int i = 0; i != successors; ++i) {
152 BasicBlock *TargetBB = TI->getSuccessor(i);
153 bool Critical = isCriticalEdge(TI, i);
154 uint64_t scaleFactor = BBWeight;
155 if (Critical) {
156 if (scaleFactor < UINT64_MAX / CriticalEdgeMultiplier)
157 scaleFactor *= CriticalEdgeMultiplier;
158 else
159 scaleFactor = UINT64_MAX;
160 }
161 if (BPI != nullptr)
162 Weight = BPI->getEdgeProbability(&BB, TargetBB).scale(scaleFactor);
163 // If InstrumentLoopEntries is on and the current edge leads to a loop
164 // (i.e., TargetBB is a loop head and BB is outside its loop), set
165 // Weight to be minimal, so that the edge won't be chosen for the MST
166 // and will be instrumented.
167 if (InstrumentLoopEntries && LI->isLoopHeader(TargetBB)) {
168 Loop *TargetLoop = LI->getLoopFor(TargetBB);
169 assert(TargetLoop);
170 if (!TargetLoop->contains(&BB))
171 Weight = 0;
172 }
173 if (Weight == 0)
174 Weight++;
175 auto *E = &addEdge(&BB, TargetBB, Weight);
176 E->IsCritical = Critical;
177 handleCoroSuspendEdge(E);
178 LLVM_DEBUG(dbgs() << " Edge: from " << BB.getName() << " to "
179 << TargetBB->getName() << " w=" << Weight << "\n");
180
181 // Keep track of entry/exit edges:
182 if (&BB == Entry) {
183 if (Weight > MaxEntryOutWeight) {
184 MaxEntryOutWeight = Weight;
185 EntryOutgoing = E;
186 }
187 }
188
189 auto *TargetTI = TargetBB->getTerminator();
190 if (TargetTI && !TargetTI->getNumSuccessors()) {
191 if (Weight > MaxExitInWeight) {
192 MaxExitInWeight = Weight;
193 ExitIncoming = E;
194 }
195 }
196 }
197 } else {
198 ExitBlockFound = true;
199 Edge *ExitO = &addEdge(&BB, nullptr, BBWeight);
200 if (BBWeight > MaxExitOutWeight) {
201 MaxExitOutWeight = BBWeight;
202 ExitOutgoing = ExitO;
203 }
204 LLVM_DEBUG(dbgs() << " Edge: from " << BB.getName() << " to fake exit"
205 << " w = " << BBWeight << "\n");
206 }
207 }
208
209 // Entry/exit edge adjustment heurisitic:
210 // prefer instrumenting entry edge over exit edge
211 // if possible. Those exit edges may never have a chance to be
212 // executed (for instance the program is an event handling loop)
213 // before the profile is asynchronously dumped.
214 //
215 // If EntryIncoming and ExitOutgoing has similar weight, make sure
216 // ExitOutging is selected as the min-edge. Similarly, if EntryOutgoing
217 // and ExitIncoming has similar weight, make sure ExitIncoming becomes
218 // the min-edge.
219 uint64_t EntryInWeight = EntryWeight;
220
221 if (EntryInWeight >= MaxExitOutWeight &&
222 EntryInWeight * 2 < MaxExitOutWeight * 3) {
223 EntryIncoming->Weight = MaxExitOutWeight;
224 ExitOutgoing->Weight = EntryInWeight + 1;
225 }
226
227 if (MaxEntryOutWeight >= MaxExitInWeight &&
228 MaxEntryOutWeight * 2 < MaxExitInWeight * 3) {
229 EntryOutgoing->Weight = MaxExitInWeight;
230 ExitIncoming->Weight = MaxEntryOutWeight + 1;
231 }
232 }
233
234 // Sort CFG edges based on its weight.
235 void sortEdgesByWeight() {
236 llvm::stable_sort(AllEdges, [](const std::unique_ptr<Edge> &Edge1,
237 const std::unique_ptr<Edge> &Edge2) {
238 return Edge1->Weight > Edge2->Weight;
239 });
240 }
241
242 // Traverse all the edges and compute the Minimum Weight Spanning Tree
243 // using union-find algorithm.
244 void computeMinimumSpanningTree() {
245 // First, put all the critical edge with landing-pad as the Dest to MST.
246 // This works around the insufficient support of critical edges split
247 // when destination BB is a landing pad.
248 for (auto &Ei : AllEdges) {
249 if (Ei->Removed)
250 continue;
251 if (Ei->IsCritical) {
252 if (Ei->DestBB && Ei->DestBB->isLandingPad()) {
253 if (unionGroups(Ei->SrcBB, Ei->DestBB))
254 Ei->InMST = true;
255 }
256 }
257 }
258
259 for (auto &Ei : AllEdges) {
260 if (Ei->Removed)
261 continue;
262 // If we detect infinite loops, force
263 // instrumenting the entry edge:
264 if (!ExitBlockFound && Ei->SrcBB == nullptr)
265 continue;
266 if (unionGroups(Ei->SrcBB, Ei->DestBB))
267 Ei->InMST = true;
268 }
269 }
270
271 [[maybe_unused]] bool validateLoopEntryInstrumentation() {
272 if (!InstrumentLoopEntries)
273 return true;
274 for (auto &Ei : AllEdges) {
275 if (Ei->Removed)
276 continue;
277 if (Ei->DestBB && LI->isLoopHeader(Ei->DestBB) &&
278 !LI->getLoopFor(Ei->DestBB)->contains(Ei->SrcBB) && Ei->InMST)
279 return false;
280 }
281 return true;
282 }
283
284public:
285 // Dump the Debug information about the instrumentation.
286 void dumpEdges(raw_ostream &OS, const Twine &Message) const {
287 if (!Message.str().empty())
288 OS << Message << "\n";
289 OS << " Number of Basic Blocks: " << BBInfos.size() << "\n";
290 // Collect and sort BBInfos deterministically by their assigned Index.
291 std::vector<std::pair<const BasicBlock *, const BBInfo *>> SortedBBInfos;
292 SortedBBInfos.reserve(BBInfos.size());
293 for (const auto &BI : BBInfos)
294 SortedBBInfos.emplace_back(BI.first, BI.second.get());
295
296#ifndef NDEBUG
297 SmallDenseSet<uint32_t, 16> SeenIndices;
298 for (const auto &P : SortedBBInfos)
299 assert(SeenIndices.insert(P.second->Index).second &&
300 "BBInfo indices should be unique");
301#endif
302
303 llvm::sort(SortedBBInfos, [](const auto &A, const auto &B) {
304 // Primary key: BBInfo Index
305 if (A.second->Index != B.second->Index)
306 return A.second->Index < B.second->Index;
307 // Secondary key: name string to keep a stable order even if
308 // indices tie (ties shouldn't happen, but this makes ordering
309 // explicit).
310 StringRef NameA = A.first ? A.first->getName() : StringRef("FakeNode");
311 StringRef NameB = B.first ? B.first->getName() : StringRef("FakeNode");
312 return NameA < NameB;
313 });
314
315 for (const auto &P : SortedBBInfos) {
316 const BasicBlock *BB = P.first;
317 const BBInfo *Info = P.second;
318 OS << " BB: " << (BB == nullptr ? "FakeNode" : BB->getName()) << " "
319 << Info->infoString() << "\n";
320 }
321 OS << " Number of Edges: " << AllEdges.size()
322 << " (*: Instrument, C: CriticalEdge, -: Removed)\n";
323 uint32_t Count = 0;
324 for (auto &EI : AllEdges)
325 OS << " Edge " << Count++ << ": " << getBBInfo(EI->SrcBB).Index << "-->"
326 << getBBInfo(EI->DestBB).Index << EI->infoString() << "\n";
327 }
328
329 // Add an edge to AllEdges with weight W.
330 Edge &addEdge(BasicBlock *Src, BasicBlock *Dest, uint64_t W) {
331 uint32_t Index = BBInfos.size();
332 auto Iter = BBInfos.end();
333 bool Inserted;
334 std::tie(Iter, Inserted) = BBInfos.try_emplace(Src);
335 if (Inserted) {
336 // Newly inserted, update the real info.
337 Iter->second = std::make_unique<BBInfo>(Index);
338 Index++;
339 }
340 std::tie(Iter, Inserted) = BBInfos.try_emplace(Dest);
341 if (Inserted)
342 // Newly inserted, update the real info.
343 Iter->second = std::make_unique<BBInfo>(Index);
344 AllEdges.emplace_back(new Edge(Src, Dest, W));
345 return *AllEdges.back();
346 }
347
348 CFGMST(Function &Func, bool InstrumentFuncEntry, bool InstrumentLoopEntries,
349 BranchProbabilityInfo *BPI = nullptr,
350 BlockFrequencyInfo *BFI = nullptr, LoopInfo *LI = nullptr)
351 : F(Func), BPI(BPI), BFI(BFI), LI(LI),
352 InstrumentFuncEntry(InstrumentFuncEntry),
353 InstrumentLoopEntries(InstrumentLoopEntries) {
354 assert(!(InstrumentLoopEntries && !LI) &&
355 "expected a LoopInfo to instrumenting loop entries");
356 buildEdges();
357 sortEdgesByWeight();
358 computeMinimumSpanningTree();
359 assert(validateLoopEntryInstrumentation() &&
360 "Loop entries should not be in MST when "
361 "InstrumentLoopEntries is on");
362 if (AllEdges.size() > 1 && InstrumentFuncEntry)
363 std::iter_swap(std::move(AllEdges.begin()),
364 std::move(AllEdges.begin() + AllEdges.size() - 1));
365 }
366
367 const std::vector<std::unique_ptr<Edge>> &allEdges() const {
368 return AllEdges;
369 }
370
371 std::vector<std::unique_ptr<Edge>> &allEdges() { return AllEdges; }
372
373 size_t numEdges() const { return AllEdges.size(); }
374
375 size_t bbInfoSize() const { return BBInfos.size(); }
376
377 // Give BB, return the auxiliary information.
378 BBInfo &getBBInfo(const BasicBlock *BB) const {
379 auto It = BBInfos.find(BB);
380 assert(It->second.get() != nullptr);
381 return *It->second.get();
382 }
383
384 // Give BB, return the auxiliary information if it's available.
385 BBInfo *findBBInfo(const BasicBlock *BB) const {
386 auto It = BBInfos.find(BB);
387 if (It == BBInfos.end())
388 return nullptr;
389 return It->second.get();
390 }
391};
392
393} // end namespace llvm
394
395#undef DEBUG_TYPE // "cfgmst"
396
397#endif // LLVM_TRANSFORMS_INSTRUMENTATION_CFGMST_H
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
This file defines the DenseMap class.
This file defines the DenseSet and SmallDenseSet classes.
#define G(x, y, z)
Definition MD5.cpp:55
#define P(N)
std::pair< BasicBlock *, BasicBlock * > Edge
This file contains some templates that are useful if you are working with the STL at all.
#define LLVM_DEBUG(...)
Definition Debug.h:119
LLVM Basic Block Representation.
Definition BasicBlock.h:62
const Instruction * getTerminator() const LLVM_READONLY
Returns the terminator instruction; assumes that the block is well-formed.
Definition BasicBlock.h:237
BlockFrequencyInfo pass uses BlockFrequencyInfoImpl implementation to estimate IR basic block frequen...
Analysis providing branch probability information.
std::vector< std::unique_ptr< Edge > > & allEdges()
Definition CFGMST.h:371
Edge & addEdge(BasicBlock *Src, BasicBlock *Dest, uint64_t W)
Definition CFGMST.h:330
const std::vector< std::unique_ptr< Edge > > & allEdges() const
Definition CFGMST.h:367
size_t bbInfoSize() const
Definition CFGMST.h:375
size_t numEdges() const
Definition CFGMST.h:373
CFGMST(Function &Func, bool InstrumentFuncEntry, bool InstrumentLoopEntries, BranchProbabilityInfo *BPI=nullptr, BlockFrequencyInfo *BFI=nullptr, LoopInfo *LI=nullptr)
Definition CFGMST.h:348
BBInfo * findBBInfo(const BasicBlock *BB) const
Definition CFGMST.h:385
BBInfo & getBBInfo(const BasicBlock *BB) const
Definition CFGMST.h:378
void dumpEdges(raw_ostream &OS, const Twine &Message) const
Definition CFGMST.h:286
LLVM_ABI unsigned getNumSuccessors() const LLVM_READONLY
Return the number of successors that this instruction has.
LLVM_ABI BasicBlock * getSuccessor(unsigned Idx) const LLVM_READONLY
Return the specified successor. This instruction must be a terminator.
bool contains(const LoopT *L) const
Return true if the specified loop is contained within in this loop.
Represents a single loop in the control flow graph.
Definition LoopInfo.h:40
Implements a dense probed hash-table based set with some number of buckets stored inline.
Definition DenseSet.h:301
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition Twine.h:82
LLVM_ABI std::string str() const
Return the twine contents as a std::string.
Definition Twine.cpp:17
LLVM_ABI StringRef getName() const
Return a constant reference to the value's name.
Definition Value.cpp:319
std::pair< iterator, bool > insert(const ValueT &V)
Definition DenseSet.h:212
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
#define UINT64_MAX
Definition DataTypes.h:77
This is an optimization pass for GlobalISel generic memory operations.
void stable_sort(R &&Range)
Definition STLExtras.h:2116
bool succ_empty(const Instruction *I)
Definition CFG.h:141
auto successors(const MachineBasicBlock *BB)
void sort(IteratorTy Start, IteratorTy End)
Definition STLExtras.h:1636
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:209
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Count
Definition InstrProf.h:145
LLVM_ABI bool isCriticalEdge(const Instruction *TI, unsigned SuccNum, bool AllowIdenticalEdges=false)
Return true if the specified edge is a critical edge.
Definition CFG.cpp:106
LLVM_ABI bool isPresplitCoroSuspendExitEdge(const BasicBlock &Src, const BasicBlock &Dest)
Definition CFG.cpp:424