diff options
author | Arthur Eubanks <aeubanks@google.com> | 2024-06-11 10:50:13 -0600 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-06-11 09:50:13 -0700 |
commit | 71497cc7a4695d22fc5dfd64958744816c15a19e (patch) | |
tree | d11ee580a32384decfe7fb2845b829128ff06deb /llvm/lib/Analysis/LazyCallGraph.cpp | |
parent | a03e93e1b2172791085f3f8c293b8e5d6ed8d841 (diff) | |
download | llvm-71497cc7a4695d22fc5dfd64958744816c15a19e.zip llvm-71497cc7a4695d22fc5dfd64958744816c15a19e.tar.gz llvm-71497cc7a4695d22fc5dfd64958744816c15a19e.tar.bz2 |
[CGSCC] Fix compile time blowup with large RefSCCs (#94815)
In some modules, e.g. Kotlin-generated IR, we end up with a huge RefSCC
and the call graph updates done as a result of the inliner take a long
time. This is due to RefSCC::removeInternalRefEdges() getting called
many times, each time removing one function from the RefSCC, but each
call to removeInternalRefEdges() is proportional to the size of the
RefSCC.
There are two places that call removeInternalRefEdges(), in
updateCGAndAnalysisManagerForPass() and
LazyCallGraph::removeDeadFunction().
1) Since LazyCallGraph can deal with spurious (edges that exist in the
graph but not in the IR) ref edges, we can simply not call
removeInternalRefEdges() in updateCGAndAnalysisManagerForPass().
2) LazyCallGraph::removeDeadFunction() still ends up taking the brunt of
compile time with the above change for the original reason. So instead
we batch all the dead function removals so we can call
removeInternalRefEdges() just once. This requires some changes to
callers of removeDeadFunction() to not actually erase the function from
the module, but defer it to when we batch delete dead functions at the
end of the CGSCC run, leaving the function body as "unreachable" in the
meantime. We still need to ensure that call edges are accurate. I had
also tried deleting dead functions after visiting a RefSCC, but deleting
them all at once at the end was simpler.
Many test changes are due to not performing unnecessary revisits of an
SCC (the CGSCC infrastructure deems ref edge refinements as unimportant
when it comes to revisiting SCCs, although that seems to not be
consistently true given these changes) because we don't remove some ref
edges. Specifically for devirt-invalidated.ll this seems to expose an
inlining order issue with the inliner. Probably unimportant for this
type of intentionally weird call graph.
Compile time:
https://llvm-compile-time-tracker.com/compare.php?from=6f2c61071c274a1b5e212e6ad4114641ec7c7fc3&to=b08c90d05e290dd065755ea776ceaf1420680224&stat=instructions:u
Diffstat (limited to 'llvm/lib/Analysis/LazyCallGraph.cpp')
-rw-r--r-- | llvm/lib/Analysis/LazyCallGraph.cpp | 123 |
1 files changed, 64 insertions, 59 deletions
diff --git a/llvm/lib/Analysis/LazyCallGraph.cpp b/llvm/lib/Analysis/LazyCallGraph.cpp index 48a7ca0..e6bf8c9 100644 --- a/llvm/lib/Analysis/LazyCallGraph.cpp +++ b/llvm/lib/Analysis/LazyCallGraph.cpp @@ -1160,8 +1160,8 @@ void LazyCallGraph::RefSCC::removeOutgoingEdge(Node &SourceN, Node &TargetN) { } SmallVector<LazyCallGraph::RefSCC *, 1> -LazyCallGraph::RefSCC::removeInternalRefEdge(Node &SourceN, - ArrayRef<Node *> TargetNs) { +LazyCallGraph::RefSCC::removeInternalRefEdges( + ArrayRef<std::pair<Node *, Node *>> Edges) { // We return a list of the resulting *new* RefSCCs in post-order. SmallVector<RefSCC *, 1> Result; @@ -1179,25 +1179,21 @@ LazyCallGraph::RefSCC::removeInternalRefEdge(Node &SourceN, #endif // First remove the actual edges. - for (Node *TargetN : TargetNs) { - assert(!(*SourceN)[*TargetN].isCall() && + for (auto [SourceN, TargetN] : Edges) { + assert(!(**SourceN)[*TargetN].isCall() && "Cannot remove a call edge, it must first be made a ref edge"); - bool Removed = SourceN->removeEdgeInternal(*TargetN); + bool Removed = (*SourceN)->removeEdgeInternal(*TargetN); (void)Removed; assert(Removed && "Target not in the edge set for this caller?"); } // Direct self references don't impact the ref graph at all. - if (llvm::all_of(TargetNs, - [&](Node *TargetN) { return &SourceN == TargetN; })) - return Result; - // If all targets are in the same SCC as the source, because no call edges // were removed there is no RefSCC structure change. - SCC &SourceC = *G->lookupSCC(SourceN); - if (llvm::all_of(TargetNs, [&](Node *TargetN) { - return G->lookupSCC(*TargetN) == &SourceC; + if (llvm::all_of(Edges, [&](std::pair<Node *, Node *> E) { + return E.first == E.second || + G->lookupSCC(*E.first) == G->lookupSCC(*E.second); })) return Result; @@ -1499,7 +1495,7 @@ void LazyCallGraph::removeEdge(Node &SourceN, Node &TargetN) { assert(Removed && "Target not in the edge set for this caller?"); } -void LazyCallGraph::removeDeadFunction(Function &F) { +void LazyCallGraph::markDeadFunction(Function &F) { // FIXME: This is unnecessarily restrictive. We should be able to remove // functions which recursively call themselves. assert(F.hasZeroLiveUses() && @@ -1515,57 +1511,66 @@ void LazyCallGraph::removeDeadFunction(Function &F) { Node &N = *NI->second; - // Cannot remove a function which has yet to be visited in the DFS walk, so - // if we have a node at all then we must have an SCC and RefSCC. - auto CI = SCCMap.find(&N); - assert(CI != SCCMap.end() && - "Tried to remove a node without an SCC after DFS walk started!"); - SCC &C = *CI->second; - RefSCC *RC = &C.getOuterRefSCC(); - - // In extremely rare cases, we can delete a dead function which is still in a - // non-trivial RefSCC. This can happen due to spurious ref edges sticking - // around after an IR function reference is removed. - if (RC->size() != 1) { - SmallVector<Node *, 0> NodesInRC; - for (SCC &OtherC : *RC) { - for (Node &OtherN : OtherC) - NodesInRC.push_back(&OtherN); + // Remove all call edges out of dead function. + for (Edge E : *N) { + if (E.isCall()) + N->setEdgeKind(E.getNode(), Edge::Ref); + } +} + +void LazyCallGraph::removeDeadFunctions(ArrayRef<Function *> DeadFs) { + if (DeadFs.empty()) + return; + + // Group dead functions by the RefSCC they're in. + DenseMap<RefSCC *, SmallVector<Node *, 1>> RCs; + for (Function *DeadF : DeadFs) { + Node *N = lookup(*DeadF); +#ifndef NDEBUG + for (Edge &E : **N) { + assert(!E.isCall() && + "dead function shouldn't have any outgoing call edges"); } - for (Node *OtherN : NodesInRC) { - if ((*OtherN)->lookup(N)) { - auto NewRefSCCs = - RC->removeInternalRefEdge(*OtherN, ArrayRef<Node *>(&N)); - // If we've split into multiple RefSCCs, RC is now invalid and the - // RefSCC containing C will be different. - if (!NewRefSCCs.empty()) - RC = &C.getOuterRefSCC(); +#endif + RefSCC *RC = lookupRefSCC(*N); + RCs[RC].push_back(N); + } + // Remove outgoing edges from all dead functions. Dead functions should + // already have had their call edges removed in markDeadFunction(), so we only + // need to worry about spurious ref edges. + for (auto [RC, DeadNs] : RCs) { + SmallVector<std::pair<Node *, Node *>> InternalEdgesToRemove; + for (Node *DeadN : DeadNs) { + for (Edge &E : **DeadN) { + if (lookupRefSCC(E.getNode()) == RC) + InternalEdgesToRemove.push_back({DeadN, &E.getNode()}); + else + RC->removeOutgoingEdge(*DeadN, E.getNode()); } } + // We ignore the returned RefSCCs since at this point we're done with CGSCC + // iteration and don't need to add it to any worklists. + (void)RC->removeInternalRefEdges(InternalEdgesToRemove); + for (Node *DeadN : DeadNs) { + RefSCC *DeadRC = lookupRefSCC(*DeadN); + assert(DeadRC->size() == 1); + assert(DeadRC->begin()->size() == 1); + DeadRC->clear(); + DeadRC->G = nullptr; + } } + // Clean up data structures. + for (Function *DeadF : DeadFs) { + Node &N = *lookup(*DeadF); + + EntryEdges.removeEdgeInternal(N); + SCCMap.erase(SCCMap.find(&N)); + NodeMap.erase(NodeMap.find(DeadF)); - NodeMap.erase(NI); - EntryEdges.removeEdgeInternal(N); - SCCMap.erase(CI); - - // This node must be the only member of its SCC as it has no callers, and - // that SCC must be the only member of a RefSCC as it has no references. - // Validate these properties first. - assert(C.size() == 1 && "Dead functions must be in a singular SCC"); - assert(RC->size() == 1 && "Dead functions must be in a singular RefSCC"); - - // Finally clear out all the data structures from the node down through the - // components. postorder_ref_scc_iterator will skip empty RefSCCs, so no need - // to adjust LazyCallGraph data structures. - N.clear(); - N.G = nullptr; - N.F = nullptr; - C.clear(); - RC->clear(); - RC->G = nullptr; - - // Nothing to delete as all the objects are allocated in stable bump pointer - // allocators. + N.clear(); + N.G = nullptr; + N.F = nullptr; + } } // Gets the Edge::Kind from one function to another by looking at the function's |