Skip to content

Commit ef1fdaf

Browse files
Merge pull request #58642 from aschwaighofer/fix_eager_specialization_of_shared_definitions
Eager specializer: Fix pre-specialization of imported code
2 parents 6fe3ee1 + bcf24e4 commit ef1fdaf

File tree

5 files changed

+36
-8
lines changed

5 files changed

+36
-8
lines changed

lib/SILOptimizer/PassManager/PassPipeline.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -943,6 +943,11 @@ SILPassPipelinePlan::getOnonePassPipeline(const SILOptions &Options) {
943943
// be no need to run another analysis of copies at -Onone.
944944
P.addMandatoryARCOpts();
945945

946+
// Create pre-specializations.
947+
// This needs to run pre-serialization because it needs to identify native
948+
// inlinable functions from imported ones.
949+
P.addOnonePrespecializations();
950+
946951
// First serialize the SIL if we are asked to.
947952
P.startPipeline("Serialization");
948953
P.addSerializeSILPass();
@@ -962,9 +967,6 @@ SILPassPipelinePlan::getOnonePassPipeline(const SILOptions &Options) {
962967
P.addAssumeSingleThreaded();
963968
}
964969

965-
// Create pre-specializations.
966-
P.addOnonePrespecializations();
967-
968970
// Has only an effect if the -sil-based-debuginfo option is specified.
969971
P.addSILDebugInfoGenerator();
970972

lib/SILOptimizer/Transforms/EagerSpecializer.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -853,6 +853,14 @@ void EagerSpecializerTransform::run() {
853853
assert(success);
854854
}
855855
onlyCreatePrespecializations = true;
856+
} else if (targetFunc->getLinkage() == SILLinkage::Shared) {
857+
// We have `shared` linkage if we deserialize a public serialized
858+
// function.
859+
// That means we are loading it from another module. In this case, we
860+
// don't want to create a pre-specialization.
861+
SpecializedFuncs.push_back(nullptr);
862+
ReInfoVec.emplace_back(ReabstractionInfo());
863+
continue;
856864
}
857865
ReInfoVec.emplace_back(FuncBuilder.getModule().getSwiftModule(),
858866
FuncBuilder.getModule().isWholeModule(), targetFunc,

test/SILOptimizer/eager_specialize.sil

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -848,6 +848,24 @@ bb0(%0 : $*T):
848848
return %8 : $Builtin.Int64
849849
}
850850

851+
// Don't specialize `shared` definitions they are imported from another module.
852+
// CHECK-NOT: sil @$s24testDontSpecializeSharedSi_Ts5
853+
sil shared [_specialize exported: true, kind: full, where T == Int] @testDontSpecializeShared : $@convention(thin) <T>(@in T) -> () {
854+
bb(%0: $*T):
855+
destroy_addr %0 : $*T
856+
%t = tuple()
857+
return %t : $()
858+
}
859+
860+
// But do specialize `shared` definitions when they are target from another // function.
861+
// CHECK: sil @$s24testDontSpecializeSharedSd_Ts5
862+
sil shared [_specialize exported: true, kind: full, target: "testDontSpecializeShared" ,where T == Double] @butSpecializeWhenTargetIsPresent : $@convention(thin) <T>(@in T) -> () {
863+
bb(%0: $*T):
864+
destroy_addr %0 : $*T
865+
%t = tuple()
866+
return %t : $()
867+
}
868+
851869
sil_vtable ClassUsingThrowingP {
852870
#ClassUsingThrowingP.init!allocator: (ClassUsingThrowingP.Type) -> () -> ClassUsingThrowingP : @$s34eager_specialize_throwing_function19ClassUsingThrowingPCACycfC // ClassUsingThrowingP.__allocating_init()
853871
#ClassUsingThrowingP.init!initializer: (ClassUsingThrowingP.Type) -> () -> ClassUsingThrowingP : @$s34eager_specialize_throwing_function19ClassUsingThrowingPCACycfc // ClassUsingThrowingP.init()

test/Serialization/serialize_attr.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,6 @@ public class CC<T : PP> {
7979
}
8080
}
8181

82-
// CHECK-DAG: sil [serialized] [_specialize exported: false, kind: full, where T == Int, U == Float] [canonical] [ossa] @$s14serialize_attr14specializeThis_1uyx_q_tr0_lF : $@convention(thin) <T, U> (@in_guaranteed T, @in_guaranteed U) -> () {
82+
// CHECK-DAG: sil [serialized] [canonical] [ossa] @$s14serialize_attr14specializeThis_1uyx_q_tr0_lF : $@convention(thin) <T, U> (@in_guaranteed T, @in_guaranteed U) -> () {
8383

84-
// CHECK-DAG: sil [serialized] [noinline] [_specialize exported: false, kind: full, where T == RR, U == SS] [canonical] [ossa] @$s14serialize_attr2CCC3foo_1gqd___AA2GGVyxGtqd___AHtAA2QQRd__lF : $@convention(method) <T where T : PP><U where U : QQ> (@in_guaranteed U, GG<T>, @guaranteed CC<T>) -> (@out U, GG<T>) {
84+
// CHECK-DAG: sil [serialized] [noinline] [canonical] [ossa] @$s14serialize_attr2CCC3foo_1gqd___AA2GGVyxGtqd___AHtAA2QQRd__lF : $@convention(method) <T where T : PP><U where U : QQ> (@in_guaranteed U, GG<T>, @guaranteed CC<T>) -> (@out U, GG<T>) {

test/sil-passpipeline-dump/basic.test-sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22

33
// CHECK: ---
44
// CHECK: name: Non-Diagnostic Mandatory Optimizations
5-
// CHECK: passes: [ "for-each-loop-unroll", "mandatory-combine",
6-
// CHECK: "mandatory-arc-opts" ]
5+
// CHECK: passes: [ "for-each-loop-unroll", "mandatory-combine", "mandatory-arc-opts",
6+
// CHECK: "onone-prespecializer" ]
77
// CHECK: ---
88
// CHECK: name: Serialization
99
// CHECK: passes: [ "serialize-sil", "sil-onone-debuginfo-canonicalizer",
1010
// CHECK-NEXT: "ownership-model-eliminator" ]
1111
// CHECK: ---
1212
// CHECK: name: Rest of Onone
13-
// CHECK: passes: [ "use-prespecialized", "onone-prespecializer", "sil-debuginfo-gen" ]
13+
// CHECK: passes: [ "use-prespecialized", "sil-debuginfo-gen" ]
1414
// CHECK: ...

0 commit comments

Comments
 (0)