Skip to content

Commit a2768b4

Browse files
committed
[CSSPGO] Honor preinliner decision for ThinLTO importing
When pre-inliner decision is used for CSSPGO, we should take that into account for ThinLTO importing as well, so post-link sample loader inliner can favor that decision. This is handled by a small tweak in this patch. It also includes a change to transfer preinliner decision when merging context. Differential Revision: https://reviews.llvm.org/D109088
1 parent 3891b45 commit a2768b4

File tree

3 files changed

+21
-6
lines changed

3 files changed

+21
-6
lines changed

llvm/lib/Transforms/IPO/SampleContextTracker.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -545,6 +545,8 @@ void SampleContextTracker::mergeContextNode(ContextTrieNode &FromNode,
545545
ToSamples->merge(*FromSamples);
546546
ToSamples->getContext().setState(SyntheticContext);
547547
FromSamples->getContext().setState(MergedContext);
548+
if (FromSamples->getContext().hasAttribute(ContextShouldBeInlined))
549+
ToSamples->getContext().setAttribute(ContextShouldBeInlined);
548550
} else if (FromSamples) {
549551
// Transfer FromSamples from FromNode to ToNode
550552
ToNode.setFunctionSamples(FromSamples);

llvm/lib/Transforms/IPO/SampleProfile.cpp

+15-5
Original file line numberDiff line numberDiff line change
@@ -977,7 +977,14 @@ void SampleProfileLoader::findExternalInlineCandidate(
977977
// For CSSPGO profile, retrieve candidate profile by walking over the
978978
// trie built for context profile. Note that also take call targets
979979
// even if callee doesn't have a corresponding context profile.
980-
if (!CalleeSample || CalleeSample->getEntrySamples() < Threshold)
980+
if (!CalleeSample)
981+
continue;
982+
983+
// If pre-inliner decision is used, honor that for importing as well.
984+
bool PreInline =
985+
UsePreInlinerDecision &&
986+
CalleeSample->getContext().hasAttribute(ContextShouldBeInlined);
987+
if (!PreInline && CalleeSample->getEntrySamples() < Threshold)
981988
continue;
982989

983990
StringRef Name = CalleeSample->getFuncName();
@@ -1299,10 +1306,13 @@ SampleProfileLoader::shouldInlineCandidate(InlineCandidate &Candidate) {
12991306
// we replay that inline decision under `sample-profile-use-preinliner`.
13001307
// Note that we don't need to handle negative decision from preinliner as
13011308
// context profile for not inlined calls are merged by preinliner already.
1302-
if (UsePreInlinerDecision &&
1303-
Candidate.CalleeSamples->getContext().hasAttribute(
1304-
ContextShouldBeInlined))
1305-
return InlineCost::getAlways("preinliner");
1309+
SampleContext &Context = Candidate.CalleeSamples->getContext();
1310+
if (UsePreInlinerDecision && Context.hasAttribute(ContextShouldBeInlined))
1311+
// Once two node are merged due to promotion, we're losing some context
1312+
// so the original context-sensitive preinliner decision should be ignored
1313+
// for SyntheticContext.
1314+
if (!Context.hasState(SyntheticContext))
1315+
return InlineCost::getAlways("preinliner");
13061316

13071317
// For old FDO inliner, we inline the call site as long as cost is not
13081318
// "Never". The cost-benefit check is done earlier.

llvm/test/Transforms/SampleProfile/csspgo-use-preinliner.ll

+4-1
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,12 @@
88
; DEFAULT-NOT: inlined into
99

1010
; PREINLINE-NOT: inlined into
11+
; `[main:3 @ _Z5funcAi]` does not have preinline decision, so it's up for loader inliner to decided.
1112
; PREINLINE: '_Z5funcAi' inlined into 'main'
13+
; `[main:3 @ _Z5funcAi:1 @ _Z8funcLeafi]` is inlined according to preinline decision.
1214
; PREINLINE: '_Z8funcLeafi' inlined into 'main'
13-
; PREINLINE: '_Z8funcLeafi' inlined into '_Z5funcBi'
15+
; Even though `[main:3.1 @ _Z5funcBi]` context is marked should inline, `_Z5funcBi` is a noinline function, so we honor that and don't inline.
16+
; When _Z5funcBi is promoted to be top level context-less profile, `[_Z5funcBi:1 @ _Z8funcLeafi]` becomes synthetic context, so preinline decision is ignored and we don't inline `_Z8funcLeafi`.
1417
; PREINLINE-NOT: inlined into
1518

1619
@factor = dso_local global i32 3, align 4, !dbg !0

0 commit comments

Comments
 (0)