@@ -108,6 +108,8 @@ class SILPerformanceInliner {
108
108
DefaultApplyLength = 10
109
109
};
110
110
111
+ SILOptions::SILOptMode OptMode;
112
+
111
113
#ifndef NDEBUG
112
114
SILFunction *LastPrintedCaller = nullptr ;
113
115
void dumpCaller (SILFunction *Caller) {
@@ -146,8 +148,10 @@ class SILPerformanceInliner {
146
148
147
149
public:
148
150
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) {}
151
155
152
156
bool inlineCallsIntoFunction (SILFunction *F);
153
157
};
@@ -170,6 +174,22 @@ bool SILPerformanceInliner::isProfitableToInline(FullApplySite AI,
170
174
171
175
assert (EnableSILInliningOfGenerics || !IsGeneric);
172
176
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
+
173
193
// Bail out if this generic call can be optimized by means of
174
194
// the generic specialization, because we prefer generic specialization
175
195
// to inlining of generics.
@@ -190,21 +210,16 @@ bool SILPerformanceInliner::isProfitableToInline(FullApplySite AI,
190
210
int CalleeCost = 0 ;
191
211
int Benefit = 0 ;
192
212
193
- // Start with a base benefit.
194
- int BaseBenefit = RemovedCallBenefit;
195
-
196
213
SubstitutionMap CalleeSubstMap;
197
214
if (IsGeneric) {
198
215
CalleeSubstMap = Callee->getLoweredFunctionType ()
199
216
->getGenericSignature ()
200
217
->getSubstitutionMap (AI.getSubstitutions ());
201
218
}
202
219
203
- const SILOptions &Opts = Callee->getModule ().getOptions ();
204
-
205
220
// For some reason -Ounchecked can accept a higher base benefit without
206
221
// increasing the code size too much.
207
- if (Opts. Optimization == SILOptions::SILOptMode::OptimizeUnchecked)
222
+ if (OptMode == SILOptions::SILOptMode::OptimizeUnchecked)
208
223
BaseBenefit *= 2 ;
209
224
210
225
CallerWeight.updateBenefit (Benefit, BaseBenefit);
@@ -696,7 +711,9 @@ class SILPerformanceInlinerPass : public SILFunctionTransform {
696
711
return ;
697
712
}
698
713
699
- SILPerformanceInliner Inliner (WhatToInline, DA, LA, SEA);
714
+ auto OptMode = getFunction ()->getModule ().getOptions ().Optimization ;
715
+
716
+ SILPerformanceInliner Inliner (WhatToInline, DA, LA, SEA, OptMode);
700
717
701
718
assert (getFunction ()->isDefinition () &&
702
719
" Expected only functions with bodies!" );
0 commit comments