Skip to content
This repository was archived by the owner on Feb 5, 2019. It is now read-only.

Commit 4c7edb1

Browse files
committed
[LCG] Add support for building persistent and connected SCCs to the
LazyCallGraph. This is the start of the whole point of this different abstraction, but it is just the initial bits. Here is a run-down of what's going on here. I'm planning to incorporate some (or all) of this into comments going forward, hopefully with better editing and wording. =] The crux of the problem with the traditional way of building SCCs is that they are ephemeral. The new pass manager however really needs the ability to associate analysis passes and results of analysis passes with SCCs in order to expose these analysis passes to the SCC passes. Making this work is kind-of the whole point of the new pass manager. =] So, when we're building SCCs for the call graph, we actually want to build persistent nodes that stick around and can be reasoned about later. We'd also like the ability to walk the SCC graph in more complex ways than just the traditional postorder traversal of the current CGSCC walk. That means that in addition to being persistent, the SCCs need to be connected into a useful graph structure. However, we still want the SCCs to be formed lazily where possible. These constraints are quite hard to satisfy with the SCC iterator. Also, using that would bypass our ability to actually add data to the nodes of the call graph to facilite implementing the Tarjan walk. So I've re-implemented things in a more direct and embedded way. This immediately makes it easy to get the persistence and connectivity correct, and it also allows leveraging the existing nodes to simplify the algorithm. I've worked somewhat to make this implementation more closely follow the traditional paper's nomenclature and strategy, although it is still a bit obtuse because it isn't recursive, using an explicit stack and a tail call instead, and it is interruptable, resuming each time we need another SCC. The other tricky bit here, and what actually took almost all the time and trials and errors I spent building this, is exactly *what* graph structure to build for the SCCs. The naive thing to build is the call graph in its newly acyclic form. I wrote about 4 versions of this which did precisely this. Inevitably, when I experimented with them across various use cases, they became incredibly awkward. It was all implementable, but it felt like a complete wrong fit. Square peg, round hole. There were two overriding aspects that pushed me in a different direction: 1) We want to discover the SCC graph in a postorder fashion. That means the root node will be the *last* node we find. Using the call-SCC DAG as the graph structure of the SCCs results in an orphaned graph until we discover a root. 2) We will eventually want to walk the SCC graph in parallel, exploring distinct sub-graphs independently, and synchronizing at merge points. This again is not helped by the call-SCC DAG structure. The structure which, quite surprisingly, ended up being completely natural to use is the *inverse* of the call-SCC DAG. We add the leaf SCCs to the graph as "roots", and have edges to the caller SCCs. Once I switched to building this structure, everything just fell into place elegantly. Aside from general cleanups (there are FIXMEs and too few comments overall) that are still needed, the other missing piece of this is support for iterating across levels of the SCC graph. These will become useful for implementing #2, but they aren't an immediate priority. Once SCCs are in good shape, I'll be working on adding mutation support for incremental updates and adding the pass manager that this analysis enables. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@206581 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent c32e261 commit 4c7edb1

File tree

3 files changed

+286
-4
lines changed

3 files changed

+286
-4
lines changed

include/llvm/Analysis/LazyCallGraph.h

+118
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,10 @@
3838
#include "llvm/ADT/DenseMap.h"
3939
#include "llvm/ADT/PointerUnion.h"
4040
#include "llvm/ADT/STLExtras.h"
41+
#include "llvm/ADT/SetVector.h"
4142
#include "llvm/ADT/SmallPtrSet.h"
4243
#include "llvm/ADT/SmallVector.h"
44+
#include "llvm/ADT/iterator_range.h"
4345
#include "llvm/IR/BasicBlock.h"
4446
#include "llvm/IR/Function.h"
4547
#include "llvm/IR/Module.h"
@@ -100,6 +102,7 @@ class raw_ostream;
100102
class LazyCallGraph {
101103
public:
102104
class Node;
105+
class SCC;
103106
typedef SmallVector<PointerUnion<Function *, Node *>, 4> NodeVectorT;
104107
typedef SmallVectorImpl<PointerUnion<Function *, Node *>> NodeVectorImplT;
105108

@@ -173,9 +176,16 @@ class LazyCallGraph {
173176
/// a callee, and facilitate iteration of child nodes in the graph.
174177
class Node {
175178
friend class LazyCallGraph;
179+
friend class LazyCallGraph::SCC;
176180

177181
LazyCallGraph *G;
178182
Function &F;
183+
184+
// We provide for the DFS numbering and Tarjan walk lowlink numbers to be
185+
// stored directly within the node.
186+
int DFSNumber;
187+
int LowLink;
188+
179189
mutable NodeVectorT Callees;
180190
SmallPtrSet<Function *, 4> CalleeSet;
181191

@@ -201,6 +211,79 @@ class LazyCallGraph {
201211
bool operator!=(const Node &N) const { return !operator==(N); }
202212
};
203213

214+
/// \brief An SCC of the call graph.
215+
///
216+
/// This represents a Strongly Connected Component of the call graph as
217+
/// a collection of call graph nodes. While the order of nodes in the SCC is
218+
/// stable, it is not any particular order.
219+
class SCC {
220+
friend class LazyCallGraph;
221+
friend class LazyCallGraph::Node;
222+
223+
SmallSetVector<SCC *, 1> ParentSCCs;
224+
SmallVector<Node *, 1> Nodes;
225+
SmallPtrSet<Function *, 1> NodeSet;
226+
227+
SCC() {}
228+
229+
public:
230+
typedef SmallVectorImpl<Node *>::const_iterator iterator;
231+
232+
iterator begin() const { return Nodes.begin(); }
233+
iterator end() const { return Nodes.end(); }
234+
};
235+
236+
/// \brief A post-order depth-first SCC iterator over the call graph.
237+
///
238+
/// This iterator triggers the Tarjan DFS-based formation of the SCC DAG for
239+
/// the call graph, walking it lazily in depth-first post-order. That is, it
240+
/// always visits SCCs for a callee prior to visiting the SCC for a caller
241+
/// (when they are in different SCCs).
242+
class postorder_scc_iterator
243+
: public std::iterator<std::forward_iterator_tag, SCC *, ptrdiff_t, SCC *,
244+
SCC *> {
245+
friend class LazyCallGraph;
246+
friend class LazyCallGraph::Node;
247+
typedef std::iterator<std::forward_iterator_tag, SCC *, ptrdiff_t,
248+
SCC *, SCC *> BaseT;
249+
250+
/// \brief Nonce type to select the constructor for the end iterator.
251+
struct IsAtEndT {};
252+
253+
LazyCallGraph *G;
254+
SCC *C;
255+
256+
// Build the begin iterator for a node.
257+
postorder_scc_iterator(LazyCallGraph &G) : G(&G) {
258+
C = G.getNextSCCInPostOrder();
259+
}
260+
261+
// Build the end iterator for a node. This is selected purely by overload.
262+
postorder_scc_iterator(LazyCallGraph &G, IsAtEndT /*Nonce*/)
263+
: G(&G), C(nullptr) {}
264+
265+
public:
266+
bool operator==(const postorder_scc_iterator &Arg) {
267+
return G == Arg.G && C == Arg.C;
268+
}
269+
bool operator!=(const postorder_scc_iterator &Arg) {
270+
return !operator==(Arg);
271+
}
272+
273+
reference operator*() const { return C; }
274+
pointer operator->() const { return operator*(); }
275+
276+
postorder_scc_iterator &operator++() {
277+
C = G->getNextSCCInPostOrder();
278+
return *this;
279+
}
280+
postorder_scc_iterator operator++(int) {
281+
postorder_scc_iterator prev = *this;
282+
++*this;
283+
return prev;
284+
}
285+
};
286+
204287
/// \brief Construct a graph for the given module.
205288
///
206289
/// This sets up the graph and computes all of the entry points of the graph.
@@ -229,6 +312,18 @@ class LazyCallGraph {
229312
iterator begin() { return iterator(*this, EntryNodes); }
230313
iterator end() { return iterator(*this, EntryNodes, iterator::IsAtEndT()); }
231314

315+
postorder_scc_iterator postorder_scc_begin() {
316+
return postorder_scc_iterator(*this);
317+
}
318+
postorder_scc_iterator postorder_scc_end() {
319+
return postorder_scc_iterator(*this, postorder_scc_iterator::IsAtEndT());
320+
}
321+
322+
iterator_range<postorder_scc_iterator> postorder_sccs() {
323+
return iterator_range<postorder_scc_iterator>(postorder_scc_begin(),
324+
postorder_scc_end());
325+
}
326+
232327
/// \brief Lookup a function in the graph which has already been scanned and
233328
/// added.
234329
Node *lookup(const Function &F) const { return NodeMap.lookup(&F); }
@@ -259,12 +354,35 @@ class LazyCallGraph {
259354
/// \brief Set of the entry nodes to the graph.
260355
SmallPtrSet<Function *, 4> EntryNodeSet;
261356

357+
/// \brief Allocator that holds all the call graph SCCs.
358+
SpecificBumpPtrAllocator<SCC> SCCBPA;
359+
360+
/// \brief Maps Function -> SCC for fast lookup.
361+
DenseMap<const Function *, SCC *> SCCMap;
362+
363+
/// \brief The leaf SCCs of the graph.
364+
///
365+
/// These are all of the SCCs which have no children.
366+
SmallVector<SCC *, 4> LeafSCCs;
367+
368+
/// \brief Stack of nodes not-yet-processed into SCCs.
369+
SmallVector<std::pair<Node *, iterator>, 4> DFSStack;
370+
371+
/// \brief Set of entry nodes not-yet-processed into SCCs.
372+
SmallSetVector<Function *, 4> SCCEntryNodes;
373+
374+
/// \brief Counter for the next DFS number to assign.
375+
int NextDFSNumber;
376+
262377
/// \brief Helper to insert a new function, with an already looked-up entry in
263378
/// the NodeMap.
264379
Node *insertInto(Function &F, Node *&MappedN);
265380

266381
/// \brief Helper to copy a node from another graph into this one.
267382
Node *copyInto(const Node &OtherN);
383+
384+
/// \brief Retrieve the next node in the post-order SCC walk of the call graph.
385+
SCC *getNextSCCInPostOrder();
268386
};
269387

270388
// Provide GraphTraits specializations for call graphs.

lib/Analysis/LazyCallGraph.cpp

+118-4
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
//===----------------------------------------------------------------------===//
99

1010
#include "llvm/Analysis/LazyCallGraph.h"
11-
#include "llvm/ADT/SCCIterator.h"
11+
#include "llvm/ADT/STLExtras.h"
1212
#include "llvm/IR/CallSite.h"
1313
#include "llvm/IR/InstVisitor.h"
1414
#include "llvm/IR/Instructions.h"
@@ -46,7 +46,8 @@ static void findCallees(
4646
}
4747
}
4848

49-
LazyCallGraph::Node::Node(LazyCallGraph &G, Function &F) : G(&G), F(F) {
49+
LazyCallGraph::Node::Node(LazyCallGraph &G, Function &F)
50+
: G(&G), F(F), DFSNumber(0), LowLink(0) {
5051
SmallVector<Constant *, 16> Worklist;
5152
SmallPtrSet<Constant *, 16> Visited;
5253
// Find all the potential callees in this function. First walk the
@@ -65,7 +66,7 @@ LazyCallGraph::Node::Node(LazyCallGraph &G, Function &F) : G(&G), F(F) {
6566
}
6667

6768
LazyCallGraph::Node::Node(LazyCallGraph &G, const Node &OtherN)
68-
: G(&G), F(OtherN.F), CalleeSet(OtherN.CalleeSet) {
69+
: G(&G), F(OtherN.F), DFSNumber(0), LowLink(0), CalleeSet(OtherN.CalleeSet) {
6970
// Loop over the other node's callees, adding the Function*s to our list
7071
// directly, and recursing to add the Node*s.
7172
Callees.reserve(OtherN.Callees.size());
@@ -91,6 +92,12 @@ LazyCallGraph::LazyCallGraph(Module &M) {
9192
Worklist.push_back(GV.getInitializer());
9293

9394
findCallees(Worklist, Visited, EntryNodes, EntryNodeSet);
95+
96+
for (auto &Entry : EntryNodes)
97+
if (Function *F = Entry.dyn_cast<Function *>())
98+
SCCEntryNodes.insert(F);
99+
else
100+
SCCEntryNodes.insert(&Entry.get<Node *>()->getFunction());
94101
}
95102

96103
LazyCallGraph::LazyCallGraph(const LazyCallGraph &G)
@@ -101,11 +108,22 @@ LazyCallGraph::LazyCallGraph(const LazyCallGraph &G)
101108
EntryNodes.push_back(Callee);
102109
else
103110
EntryNodes.push_back(copyInto(*EntryNode.get<Node *>()));
111+
112+
// Just re-populate the SCCEntryNodes structure so we recompute the SCCs if
113+
// needed.
114+
for (auto &Entry : EntryNodes)
115+
if (Function *F = Entry.dyn_cast<Function *>())
116+
SCCEntryNodes.insert(F);
117+
else
118+
SCCEntryNodes.insert(&Entry.get<Node *>()->getFunction());
104119
}
105120

106121
LazyCallGraph::LazyCallGraph(LazyCallGraph &&G)
107122
: BPA(std::move(G.BPA)), EntryNodes(std::move(G.EntryNodes)),
108-
EntryNodeSet(std::move(G.EntryNodeSet)) {
123+
EntryNodeSet(std::move(G.EntryNodeSet)), SCCBPA(std::move(G.SCCBPA)),
124+
SCCMap(std::move(G.SCCMap)), LeafSCCs(std::move(G.LeafSCCs)),
125+
DFSStack(std::move(G.DFSStack)),
126+
SCCEntryNodes(std::move(G.SCCEntryNodes)) {
109127
// Process all nodes updating the graph pointers.
110128
SmallVector<Node *, 16> Worklist;
111129
for (auto &Entry : EntryNodes)
@@ -133,6 +151,88 @@ LazyCallGraph::Node *LazyCallGraph::copyInto(const Node &OtherN) {
133151
return new (N = BPA.Allocate()) Node(*this, OtherN);
134152
}
135153

154+
LazyCallGraph::SCC *LazyCallGraph::getNextSCCInPostOrder() {
155+
// When the stack is empty, there are no more SCCs to walk in this graph.
156+
if (DFSStack.empty()) {
157+
// If we've handled all candidate entry nodes to the SCC forest, we're done.
158+
if (SCCEntryNodes.empty())
159+
return nullptr;
160+
161+
Node *N = get(*SCCEntryNodes.pop_back_val());
162+
DFSStack.push_back(std::make_pair(N, N->begin()));
163+
}
164+
165+
Node *N = DFSStack.back().first;
166+
if (N->DFSNumber == 0) {
167+
// This node hasn't been visited before, assign it a DFS number and remove
168+
// it from the entry set.
169+
N->LowLink = N->DFSNumber = NextDFSNumber++;
170+
SCCEntryNodes.remove(&N->getFunction());
171+
}
172+
173+
for (auto I = DFSStack.back().second, E = N->end(); I != E; ++I) {
174+
Node *ChildN = *I;
175+
if (ChildN->DFSNumber == 0) {
176+
// Mark that we should start at this child when next this node is the
177+
// top of the stack. We don't start at the next child to ensure this
178+
// child's lowlink is reflected.
179+
// FIXME: I don't actually think this is required, and we could start
180+
// at the next child.
181+
DFSStack.back().second = I;
182+
183+
// Recurse onto this node via a tail call.
184+
DFSStack.push_back(std::make_pair(ChildN, ChildN->begin()));
185+
return LazyCallGraph::getNextSCCInPostOrder();
186+
}
187+
188+
// Track the lowest link of the childen, if any are still in the stack.
189+
if (ChildN->LowLink < N->LowLink && !SCCMap.count(&ChildN->getFunction()))
190+
N->LowLink = ChildN->LowLink;
191+
}
192+
193+
// The tail of the stack is the new SCC. Allocate the SCC and pop the stack
194+
// into it.
195+
SCC *NewSCC = new (SCCBPA.Allocate()) SCC();
196+
197+
// Because we don't follow the strict Tarjan recursive formulation, walk
198+
// from the top of the stack down, propagating the lowest link and stopping
199+
// when the DFS number is the lowest link.
200+
int LowestLink = N->LowLink;
201+
do {
202+
Node *SCCN = DFSStack.pop_back_val().first;
203+
SCCMap.insert(std::make_pair(&SCCN->getFunction(), NewSCC));
204+
NewSCC->Nodes.push_back(SCCN);
205+
LowestLink = std::min(LowestLink, SCCN->LowLink);
206+
bool Inserted =
207+
NewSCC->NodeSet.insert(&SCCN->getFunction());
208+
(void)Inserted;
209+
assert(Inserted && "Cannot have duplicates in the DFSStack!");
210+
} while (!DFSStack.empty() && LowestLink <= DFSStack.back().first->DFSNumber);
211+
assert(LowestLink == NewSCC->Nodes.back()->DFSNumber &&
212+
"Cannot stop with a DFS number greater than the lowest link!");
213+
214+
// A final pass over all edges in the SCC (this remains linear as we only
215+
// do this once when we build the SCC) to connect it to the parent sets of
216+
// its children.
217+
bool IsLeafSCC = true;
218+
for (Node *SCCN : NewSCC->Nodes)
219+
for (Node *SCCChildN : *SCCN) {
220+
if (NewSCC->NodeSet.count(&SCCChildN->getFunction()))
221+
continue;
222+
SCC *ChildSCC = SCCMap.lookup(&SCCChildN->getFunction());
223+
assert(ChildSCC &&
224+
"Must have all child SCCs processed when building a new SCC!");
225+
ChildSCC->ParentSCCs.insert(NewSCC);
226+
IsLeafSCC = false;
227+
}
228+
229+
// For the SCCs where we fine no child SCCs, add them to the leaf list.
230+
if (IsLeafSCC)
231+
LeafSCCs.push_back(NewSCC);
232+
233+
return NewSCC;
234+
}
235+
136236
char LazyCallGraphAnalysis::PassID;
137237

138238
LazyCallGraphPrinterPass::LazyCallGraphPrinterPass(raw_ostream &OS) : OS(OS) {}
@@ -151,6 +251,16 @@ static void printNodes(raw_ostream &OS, LazyCallGraph::Node &N,
151251
OS << "\n";
152252
}
153253

254+
static void printSCC(raw_ostream &OS, LazyCallGraph::SCC &SCC) {
255+
ptrdiff_t SCCSize = std::distance(SCC.begin(), SCC.end());
256+
OS << " SCC with " << SCCSize << " functions:\n";
257+
258+
for (LazyCallGraph::Node *N : SCC)
259+
OS << " " << N->getFunction().getName() << "\n";
260+
261+
OS << "\n";
262+
}
263+
154264
PreservedAnalyses LazyCallGraphPrinterPass::run(Module *M,
155265
ModuleAnalysisManager *AM) {
156266
LazyCallGraph &G = AM->getResult<LazyCallGraphAnalysis>(M);
@@ -163,5 +273,9 @@ PreservedAnalyses LazyCallGraphPrinterPass::run(Module *M,
163273
if (Printed.insert(N))
164274
printNodes(OS, *N, Printed);
165275

276+
for (LazyCallGraph::SCC *SCC : G.postorder_sccs())
277+
printSCC(OS, *SCC);
278+
166279
return PreservedAnalyses::all();
280+
167281
}

0 commit comments

Comments
 (0)