Skip to content

Commit fac3930

Browse files
mtrofingithub-actions[bot]
authored andcommitted
Automerge: [mlgo][inliner] Fix incorrect state tracking when deferring to the default policy (#163477)
Identified in https://issues.chromium.org/issues/369637577 The problem was that we were missing edge updates when deferring to non-cold edges.
2 parents 9c40b97 + e2db9a9 commit fac3930

File tree

2 files changed

+70
-3
lines changed

2 files changed

+70
-3
lines changed

llvm/lib/Analysis/MLInlineAdvisor.cpp

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ static cl::opt<SkipMLPolicyCriteria> SkipPolicy(
6161
static cl::opt<std::string> ModelSelector("ml-inliner-model-selector",
6262
cl::Hidden, cl::init(""));
6363

64+
static cl::opt<bool> StopImmediatelyForTest("ml-inliner-stop-immediately",
65+
cl::Hidden);
66+
6467
#if defined(LLVM_HAVE_TF_AOT_INLINERSIZEMODEL)
6568
// codegen-ed file
6669
#include "InlinerSizeModel.h" // NOLINT
@@ -214,6 +217,7 @@ MLInlineAdvisor::MLInlineAdvisor(
214217
return;
215218
}
216219
ModelRunner->switchContext("");
220+
ForceStop = StopImmediatelyForTest;
217221
}
218222

219223
unsigned MLInlineAdvisor::getInitialFunctionLevel(const Function &F) const {
@@ -379,9 +383,17 @@ std::unique_ptr<InlineAdvice> MLInlineAdvisor::getAdviceImpl(CallBase &CB) {
379383
auto &ORE = FAM.getResult<OptimizationRemarkEmitterAnalysis>(Caller);
380384

381385
if (SkipPolicy == SkipMLPolicyCriteria::IfCallerIsNotCold) {
382-
if (!PSI.isFunctionEntryCold(&Caller))
383-
return std::make_unique<InlineAdvice>(this, CB, ORE,
384-
GetDefaultAdvice(CB));
386+
if (!PSI.isFunctionEntryCold(&Caller)) {
387+
// Return a MLInlineAdvice, despite delegating to the default advice,
388+
// because we need to keep track of the internal state. This is different
389+
// from the other instances where we return a "default" InlineAdvice,
390+
// which happen at points we won't come back to the MLAdvisor for
391+
// decisions requiring that state.
392+
return ForceStop ? std::make_unique<InlineAdvice>(this, CB, ORE,
393+
GetDefaultAdvice(CB))
394+
: std::make_unique<MLInlineAdvice>(this, CB, ORE,
395+
GetDefaultAdvice(CB));
396+
}
385397
}
386398
auto MandatoryKind = InlineAdvisor::getMandatoryKind(CB, FAM, ORE);
387399
// If this is a "never inline" case, there won't be any changes to internal
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 6
2+
; REQUIRES: llvm_inliner_model_autogenerated && asserts
3+
; RUN: opt -passes='default<O3>' -enable-ml-inliner=release -ml-inliner-skip-policy=if-caller-not-cold -S %s -o - | FileCheck %s
4+
; RUN: opt -passes='default<O3>' -ml-inliner-stop-immediately -enable-ml-inliner=release -ml-inliner-skip-policy=if-caller-not-cold -S %s -o - | FileCheck %s
5+
6+
declare ptr @f()
7+
8+
define void @e() #0 {
9+
; CHECK-LABEL: define void @e(
10+
; CHECK-SAME: ) local_unnamed_addr #[[ATTR0:[0-9]+]] {
11+
; CHECK-NEXT: tail call void @d()
12+
; CHECK-NEXT: tail call void @g()
13+
; CHECK-NEXT: tail call void @d()
14+
; CHECK-NEXT: tail call void @g()
15+
; CHECK-NEXT: tail call void @d()
16+
; CHECK-NEXT: tail call void @g()
17+
; CHECK-NEXT: ret void
18+
;
19+
call void @h()
20+
call void @h()
21+
call void @h()
22+
ret void
23+
}
24+
25+
define void @d() {
26+
; CHECK-LABEL: define void @d() local_unnamed_addr {
27+
; CHECK-NEXT: tail call void @f()
28+
; CHECK-NEXT: ret void
29+
;
30+
call void @f()
31+
ret void
32+
}
33+
34+
define void @g() {
35+
; CHECK-LABEL: define void @g() local_unnamed_addr {
36+
; CHECK-NEXT: tail call void @f()
37+
; CHECK-NEXT: ret void
38+
;
39+
call void @f()
40+
ret void
41+
}
42+
43+
define void @h() #0 {
44+
; CHECK-LABEL: define void @h(
45+
; CHECK-SAME: ) local_unnamed_addr #[[ATTR0]] {
46+
; CHECK-NEXT: tail call void @d()
47+
; CHECK-NEXT: tail call void @g()
48+
; CHECK-NEXT: ret void
49+
;
50+
call void @d()
51+
call void @g()
52+
ret void
53+
}
54+
55+
attributes #0 = { "sign-return-address"="non-leaf" "sign-return-address-key"="a_key" }

0 commit comments

Comments
 (0)