aboutsummaryrefslogtreecommitdiff
path: root/llvm/lib/Analysis/LazyCallGraph.cpp
diff options
context:
space:
mode:
authorArthur Eubanks <aeubanks@google.com>2022-09-14 16:01:28 -0700
committerArthur Eubanks <aeubanks@google.com>2022-09-22 15:01:15 -0700
commita8f1da128d86d86a3efc1e96e46ace725bf4f3cb (patch)
treef5ce1f2a5850c542bffa8c1869ff4b1eb480e199 /llvm/lib/Analysis/LazyCallGraph.cpp
parent5b2f838db42ea190fdda147a33b5f0fcc8145689 (diff)
downloadllvm-a8f1da128d86d86a3efc1e96e46ace725bf4f3cb.zip
llvm-a8f1da128d86d86a3efc1e96e46ace725bf4f3cb.tar.gz
llvm-a8f1da128d86d86a3efc1e96e46ace725bf4f3cb.tar.bz2
[LazyCallGraph] Handle spurious ref edges when deleting a dead function
Spurious ref edges are ref edges that still exist in the call graph even though the corresponding IR reference no longer exists. This can cause issues when deleting a dead function which has a spurious ref edge pointed at it because currently we expect the dead function's RefSCC to be trivial. In the case that the dead function's RefSCC is not trivial, remove all ref edges from other nodes in the RefSCC to it. Removing a ref edge can result in splitting RefSCCs. There's actually no reason to revisit those RefSCCs because currently we only run passes on SCCs, and we've already added all SCCs in the RefSCC to the worklist. (as opposed to removing the ref edge in updateCGAndAnalysisManagerForPass() which can modify the call graph of SCCs we have not visited yet). We also don't expect that RefSCC refinement will allow us to glean any more information for optimization use. Also, doing so would drastically increase the complexity of LazyCallGraph::removeDeadFunction(), requiring us to return a list of invalidated RefSCCs and new RefSCCs to add to the worklist. Fixes #56503 Reviewed By: asbirlea Differential Revision: https://reviews.llvm.org/D133907
Diffstat (limited to 'llvm/lib/Analysis/LazyCallGraph.cpp')
-rw-r--r--llvm/lib/Analysis/LazyCallGraph.cpp36
1 files changed, 28 insertions, 8 deletions
diff --git a/llvm/lib/Analysis/LazyCallGraph.cpp b/llvm/lib/Analysis/LazyCallGraph.cpp
index 51459ee..a28e8f8 100644
--- a/llvm/lib/Analysis/LazyCallGraph.cpp
+++ b/llvm/lib/Analysis/LazyCallGraph.cpp
@@ -1508,10 +1508,6 @@ void LazyCallGraph::removeDeadFunction(Function &F) {
return;
Node &N = *NI->second;
- NodeMap.erase(NI);
-
- // Remove this from the entry edges if present.
- EntryEdges.removeEdgeInternal(N);
// 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.
@@ -1519,14 +1515,38 @@ void LazyCallGraph::removeDeadFunction(Function &F) {
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);
+ }
+ 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();
+ }
+ }
+ }
+
+ NodeMap.erase(NI);
+ EntryEdges.removeEdgeInternal(N);
SCCMap.erase(CI);
- RefSCC &RC = C.getOuterRefSCC();
// 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");
+ 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
@@ -1535,8 +1555,8 @@ void LazyCallGraph::removeDeadFunction(Function &F) {
N.G = nullptr;
N.F = nullptr;
C.clear();
- RC.clear();
- RC.G = nullptr;
+ RC->clear();
+ RC->G = nullptr;
// Nothing to delete as all the objects are allocated in stable bump pointer
// allocators.