Skip to content

Commit 94471e6

Browse files
authored
[MLInliner] Handle CGSCC changes from #94815 (#96274)
With #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.
1 parent 9667e60 commit 94471e6

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
@@ -211,14 +211,12 @@ void MLInlineAdvisor::onPassEntry(LazyCallGraph::SCC *CurSCC) {
211211
// care about the nature of the Edge (call or ref). `FunctionLevels`-wise, we
212212
// record them at the same level as the original node (this is a choice, may
213213
// need revisiting).
214+
// - nodes are only deleted at the end of a call graph walk where they are
215+
// batch deleted, so we shouldn't see any dead nodes here.
214216
while (!NodesInLastSCC.empty()) {
215217
const auto *N = *NodesInLastSCC.begin();
218+
assert(!N->isDead());
216219
NodesInLastSCC.erase(N);
217-
// The Function wrapped by N could have been deleted since we last saw it.
218-
if (N->isDead()) {
219-
assert(!N->getFunction().isDeclaration());
220-
continue;
221-
}
222220
EdgeCount += getLocalCalls(N->getFunction());
223221
const auto NLevel = FunctionLevels.at(N);
224222
for (const auto &E : *(*N)) {
@@ -256,11 +254,9 @@ void MLInlineAdvisor::onPassExit(LazyCallGraph::SCC *CurSCC) {
256254
EdgesOfLastSeenNodes = 0;
257255

258256
// Check on nodes that were in SCC onPassEntry
259-
for (auto I = NodesInLastSCC.begin(); I != NodesInLastSCC.end();) {
260-
if ((*I)->isDead())
261-
NodesInLastSCC.erase(*I++);
262-
else
263-
EdgesOfLastSeenNodes += getLocalCalls((*I++)->getFunction());
257+
for (const LazyCallGraph::Node *N : NodesInLastSCC) {
258+
assert(!N->isDead());
259+
EdgesOfLastSeenNodes += getLocalCalls(N->getFunction());
264260
}
265261

266262
// Check on nodes that may have got added to SCC
@@ -311,8 +307,12 @@ void MLInlineAdvisor::onSuccessfulInlining(const MLInlineAdvice &Advice,
311307
int64_t NewCallerAndCalleeEdges =
312308
getCachedFPI(*Caller).DirectCallsToDefinedFunctions;
313309

310+
// A dead function's node is not actually removed from the call graph until
311+
// the end of the call graph walk, but the node no longer belongs to any valid
312+
// SCC.
314313
if (CalleeWasDeleted) {
315314
--NodeCount;
315+
NodesInLastSCC.erase(CG.lookup(*Callee));
316316
DeadFunctions.insert(Callee);
317317
} else {
318318
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)