Skip to content

Commit d85b35b

Browse files
committed
Tune down inlining at Osize
This is obviously just a start. Don't inline into thunks and don't inline methods. Start off with half the benefit for removing a call.
1 parent 0abf3fa commit d85b35b

File tree

1 file changed

+26
-9
lines changed

1 file changed

+26
-9
lines changed

lib/SILOptimizer/Transforms/PerformanceInliner.cpp

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,8 @@ class SILPerformanceInliner {
108108
DefaultApplyLength = 10
109109
};
110110

111+
SILOptions::SILOptMode OptMode;
112+
111113
#ifndef NDEBUG
112114
SILFunction *LastPrintedCaller = nullptr;
113115
void dumpCaller(SILFunction *Caller) {
@@ -146,8 +148,10 @@ class SILPerformanceInliner {
146148

147149
public:
148150
SILPerformanceInliner(InlineSelection WhatToInline, DominanceAnalysis *DA,
149-
SILLoopAnalysis *LA, SideEffectAnalysis *SEA)
150-
: WhatToInline(WhatToInline), DA(DA), LA(LA), SEA(SEA), CBI(DA) {}
151+
SILLoopAnalysis *LA, SideEffectAnalysis *SEA,
152+
SILOptions::SILOptMode OptMode)
153+
: WhatToInline(WhatToInline), DA(DA), LA(LA), SEA(SEA), CBI(DA),
154+
OptMode(OptMode) {}
151155

152156
bool inlineCallsIntoFunction(SILFunction *F);
153157
};
@@ -170,6 +174,22 @@ bool SILPerformanceInliner::isProfitableToInline(FullApplySite AI,
170174

171175
assert(EnableSILInliningOfGenerics || !IsGeneric);
172176

177+
// Start with a base benefit.
178+
int BaseBenefit = RemovedCallBenefit;
179+
180+
// Osize heuristic.
181+
if (OptMode == SILOptions::SILOptMode::OptimizeForSize) {
182+
// Don't inline into thunks.
183+
if (AI.getFunction()->isThunk())
184+
return false;
185+
186+
// Don't inline methods.
187+
if (Callee->getRepresentation() == SILFunctionTypeRepresentation::Method)
188+
return false;
189+
190+
BaseBenefit = BaseBenefit / 2;
191+
}
192+
173193
// Bail out if this generic call can be optimized by means of
174194
// the generic specialization, because we prefer generic specialization
175195
// to inlining of generics.
@@ -190,21 +210,16 @@ bool SILPerformanceInliner::isProfitableToInline(FullApplySite AI,
190210
int CalleeCost = 0;
191211
int Benefit = 0;
192212

193-
// Start with a base benefit.
194-
int BaseBenefit = RemovedCallBenefit;
195-
196213
SubstitutionMap CalleeSubstMap;
197214
if (IsGeneric) {
198215
CalleeSubstMap = Callee->getLoweredFunctionType()
199216
->getGenericSignature()
200217
->getSubstitutionMap(AI.getSubstitutions());
201218
}
202219

203-
const SILOptions &Opts = Callee->getModule().getOptions();
204-
205220
// For some reason -Ounchecked can accept a higher base benefit without
206221
// increasing the code size too much.
207-
if (Opts.Optimization == SILOptions::SILOptMode::OptimizeUnchecked)
222+
if (OptMode == SILOptions::SILOptMode::OptimizeUnchecked)
208223
BaseBenefit *= 2;
209224

210225
CallerWeight.updateBenefit(Benefit, BaseBenefit);
@@ -696,7 +711,9 @@ class SILPerformanceInlinerPass : public SILFunctionTransform {
696711
return;
697712
}
698713

699-
SILPerformanceInliner Inliner(WhatToInline, DA, LA, SEA);
714+
auto OptMode = getFunction()->getModule().getOptions().Optimization;
715+
716+
SILPerformanceInliner Inliner(WhatToInline, DA, LA, SEA, OptMode);
700717

701718
assert(getFunction()->isDefinition() &&
702719
"Expected only functions with bodies!");

0 commit comments

Comments
 (0)