Skip to content

Commit 2a63907

Browse files
committed
Make FSO thunks always_inline.
This forces the callsites to be rewritten by the inliner. we have the issue that the thunk changes from the time the its created to the time its reread to figure out what we have done to the original function This results in missed opportunities. This solution solves the problem gracefully, because the thunk carries the information on how to set up the call to the optimized functions. Inlining the thunk makes the callsite calling the optimized function for free. i.e. without any rewriting. I did not measure any regression with this change.
1 parent 9a020c8 commit 2a63907

File tree

6 files changed

+260
-112
lines changed

6 files changed

+260
-112
lines changed

lib/SILOptimizer/Analysis/FunctionSignatureAnalysis.cpp

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -252,8 +252,6 @@ std::string FunctionSignatureInfo::getOptimizedName() const {
252252
auto P = SpecializationPass::FunctionSignatureOpts;
253253
FunctionSignatureSpecializationMangler FSSM(P, M, F);
254254

255-
std::string ArgEnc;
256-
257255
// Handle arguments' changes.
258256
for (unsigned i : indices(ArgDescList)) {
259257
const ArgumentDescriptor &Arg = ArgDescList[i];
@@ -271,9 +269,6 @@ std::string FunctionSignatureInfo::getOptimizedName() const {
271269
// mangling.
272270
if (Arg.Explode && !Arg.IsEntirelyDead) {
273271
FSSM.setArgumentSROA(i);
274-
// Generate a string of encoding for the argument projection tree.
275-
// TODO: we can put this into the mangler itself.
276-
ArgEnc += "_arg" + std::to_string(i) + "_" + Arg.ProjTree.getNameEncoding();
277272
}
278273
}
279274

@@ -285,7 +280,7 @@ std::string FunctionSignatureInfo::getOptimizedName() const {
285280

286281
FSSM.mangle();
287282

288-
return M.finalize() + ArgEnc;
283+
return M.finalize();
289284
}
290285

291286

lib/SILOptimizer/PassManager/Passes.cpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -340,11 +340,6 @@ void swift::runSILOptimizationPasses(SILModule &Module) {
340340

341341
PM.setStageName("LowLevel");
342342

343-
// Rewrite to get the benefit of release devirtualizer.
344-
// Also, Make sure the run the rewriter to create the optimized functions before
345-
// the cloner on the current function is run !.
346-
PM.addFunctionSignatureOptRewriter();
347-
348343
// Should be after FunctionSignatureOpts and before the last inliner.
349344
PM.addReleaseDevirtualizer();
350345

lib/SILOptimizer/Transforms/FunctionSignatureOptCloner.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -566,7 +566,13 @@ class FunctionSignatureOptCloner : public SILFunctionTransform {
566566
createOptimizedFunction(RCIA->get(F), FSA->get(F), AA, F);
567567

568568
if (NewF) {
569-
// Make sure the PM knows about this function.
569+
// The thunk now carries the information on how the signature is
570+
// optimized. If we inline the thunk, we will get the benefit of calling
571+
// the signature optimized function without additional setup on the
572+
// caller side.
573+
F->setInlineStrategy(AlwaysInline);
574+
// Make sure the PM knows about this function. This will also help us
575+
// with self-recursion.
570576
notifyPassManagerOfFunction(NewF);
571577
invalidateAnalysis(SILAnalysis::InvalidationKind::Everything);
572578
}

test/SILOptimizer/devirt_default_case.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ func foo(a: A3) -> Int {
109109
// Check that call to A3.f() can be devirtualized.
110110
//
111111
// CHECK-NORMAL: sil{{( hidden)?}} [noinline] @_TF19devirt_default_case3fooFCS_2A3Si
112-
// CHECK-TESTABLE: sil{{( hidden)?}} [thunk] [noinline] @_TF19devirt_default_case3fooFCS_2A3Si
112+
// CHECK-TESTABLE: sil{{( hidden)?}} [thunk] [always_inline] @_TF19devirt_default_case3fooFCS_2A3Si
113113
// CHECK-NORMAL: function_ref @{{.*}}TFC19devirt_default_case2B31f
114114
// CHECK-NORMAL: function_ref @{{.*}}TFC19devirt_default_case2A31f
115115
// CHECK-NORMAL-NOT: class_method

0 commit comments

Comments
 (0)