Skip to content

Commit cb1390a

Browse files
aeubanksanton-bannykh
authored andcommitted
[MLInliner] Handle CGSCC changes from llvm#94815 (llvm#96274)
With llvm#94815, the nodes belonging to dead functions are no longer invalidated, but kept around to batch delete at the end of the call graph walk. The ML inliner needs to be updated to handle this. This fixes some asserts getting hit, e.g. https://crbug.com/348376263. (cherry picked from commit 94471e6)
1 parent 5251e28 commit cb1390a

File tree

2 files changed

+27
-10
lines changed

2 files changed

+27
-10
lines changed

llvm/lib/Analysis/MLInlineAdvisor.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -156,14 +156,12 @@ void MLInlineAdvisor::onPassEntry(LazyCallGraph::SCC *CurSCC) {
156156
// they'd be adjacent to Nodes in the last SCC. So we just need to check the
157157
// boundary of Nodes in NodesInLastSCC for Nodes we haven't seen. We don't
158158
// care about the nature of the Edge (call or ref).
159+
// - nodes are only deleted at the end of a call graph walk where they are
160+
// batch deleted, so we shouldn't see any dead nodes here.
159161
while (!NodesInLastSCC.empty()) {
160162
const auto *N = *NodesInLastSCC.begin();
163+
assert(!N->isDead());
161164
NodesInLastSCC.erase(N);
162-
// The Function wrapped by N could have been deleted since we last saw it.
163-
if (N->isDead()) {
164-
assert(!N->getFunction().isDeclaration());
165-
continue;
166-
}
167165
EdgeCount += getLocalCalls(N->getFunction());
168166
for (const auto &E : *(*N)) {
169167
const auto *AdjNode = &E.getNode();
@@ -199,11 +197,9 @@ void MLInlineAdvisor::onPassExit(LazyCallGraph::SCC *CurSCC) {
199197
EdgesOfLastSeenNodes = 0;
200198

201199
// Check on nodes that were in SCC onPassEntry
202-
for (auto I = NodesInLastSCC.begin(); I != NodesInLastSCC.end();) {
203-
if ((*I)->isDead())
204-
NodesInLastSCC.erase(*I++);
205-
else
206-
EdgesOfLastSeenNodes += getLocalCalls((*I++)->getFunction());
200+
for (const LazyCallGraph::Node *N : NodesInLastSCC) {
201+
assert(!N->isDead());
202+
EdgesOfLastSeenNodes += getLocalCalls(N->getFunction());
207203
}
208204

209205
// Check on nodes that may have got added to SCC
@@ -254,8 +250,12 @@ void MLInlineAdvisor::onSuccessfulInlining(const MLInlineAdvice &Advice,
254250
int64_t NewCallerAndCalleeEdges =
255251
getCachedFPI(*Caller).DirectCallsToDefinedFunctions;
256252

253+
// A dead function's node is not actually removed from the call graph until
254+
// the end of the call graph walk, but the node no longer belongs to any valid
255+
// SCC.
257256
if (CalleeWasDeleted) {
258257
--NodeCount;
258+
NodesInLastSCC.erase(CG.lookup(*Callee));
259259
DeadFunctions.insert(Callee);
260260
} else {
261261
NewCallerAndCalleeEdges +=
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
; REQUIRES: llvm_inliner_model_autogenerated
2+
; RUN: opt -passes=inliner-ml-advisor-release -S < %s | FileCheck %s
3+
4+
; Check that our accounting works when a function in a non-trivial SCC is dead.
5+
6+
; CHECK: define void @f
7+
; CHECK-NOT: @g
8+
9+
define void @f() {
10+
call void @g()
11+
ret void
12+
}
13+
14+
define internal void @g() {
15+
call void @f()
16+
ret void
17+
}

0 commit comments

Comments
 (0)