Skip to content

Eager specializer: Fix pre-specialization of imported code #58642

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions lib/SILOptimizer/PassManager/PassPipeline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -943,6 +943,11 @@ SILPassPipelinePlan::getOnonePassPipeline(const SILOptions &Options) {
// be no need to run another analysis of copies at -Onone.
P.addMandatoryARCOpts();

// Create pre-specializations.
// This needs to run pre-serialization because it needs to identify native
// inlinable functions from imported ones.
P.addOnonePrespecializations();

// First serialize the SIL if we are asked to.
P.startPipeline("Serialization");
P.addSerializeSILPass();
Expand All @@ -962,9 +967,6 @@ SILPassPipelinePlan::getOnonePassPipeline(const SILOptions &Options) {
P.addAssumeSingleThreaded();
}

// Create pre-specializations.
P.addOnonePrespecializations();

// Has only an effect if the -sil-based-debuginfo option is specified.
P.addSILDebugInfoGenerator();

Expand Down
8 changes: 8 additions & 0 deletions lib/SILOptimizer/Transforms/EagerSpecializer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -853,6 +853,14 @@ void EagerSpecializerTransform::run() {
assert(success);
}
onlyCreatePrespecializations = true;
} else if (targetFunc->getLinkage() == SILLinkage::Shared) {
// We have `shared` linkage if we deserialize a public serialized
// function.
// That means we are loading it from another module. In this case, we
// don't want to create a pre-specialization.
SpecializedFuncs.push_back(nullptr);
ReInfoVec.emplace_back(ReabstractionInfo());
continue;
}
ReInfoVec.emplace_back(FuncBuilder.getModule().getSwiftModule(),
FuncBuilder.getModule().isWholeModule(), targetFunc,
Expand Down
18 changes: 18 additions & 0 deletions test/SILOptimizer/eager_specialize.sil
Original file line number Diff line number Diff line change
Expand Up @@ -848,6 +848,24 @@ bb0(%0 : $*T):
return %8 : $Builtin.Int64
}

// Don't specialize `shared` definitions they are imported from another module.
// CHECK-NOT: sil @$s24testDontSpecializeSharedSi_Ts5
sil shared [_specialize exported: true, kind: full, where T == Int] @testDontSpecializeShared : $@convention(thin) <T>(@in T) -> () {
bb(%0: $*T):
destroy_addr %0 : $*T
%t = tuple()
return %t : $()
}

// But do specialize `shared` definitions when they are target from another // function.
// CHECK: sil @$s24testDontSpecializeSharedSd_Ts5
sil shared [_specialize exported: true, kind: full, target: "testDontSpecializeShared" ,where T == Double] @butSpecializeWhenTargetIsPresent : $@convention(thin) <T>(@in T) -> () {
bb(%0: $*T):
destroy_addr %0 : $*T
%t = tuple()
return %t : $()
}

sil_vtable ClassUsingThrowingP {
#ClassUsingThrowingP.init!allocator: (ClassUsingThrowingP.Type) -> () -> ClassUsingThrowingP : @$s34eager_specialize_throwing_function19ClassUsingThrowingPCACycfC // ClassUsingThrowingP.__allocating_init()
#ClassUsingThrowingP.init!initializer: (ClassUsingThrowingP.Type) -> () -> ClassUsingThrowingP : @$s34eager_specialize_throwing_function19ClassUsingThrowingPCACycfc // ClassUsingThrowingP.init()
Expand Down
4 changes: 2 additions & 2 deletions test/Serialization/serialize_attr.swift
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,6 @@ public class CC<T : PP> {
}
}

// 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) -> () {
// CHECK-DAG: sil [serialized] [canonical] [ossa] @$s14serialize_attr14specializeThis_1uyx_q_tr0_lF : $@convention(thin) <T, U> (@in_guaranteed T, @in_guaranteed U) -> () {

// 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>) {
// 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>) {
6 changes: 3 additions & 3 deletions test/sil-passpipeline-dump/basic.test-sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

// CHECK: ---
// CHECK: name: Non-Diagnostic Mandatory Optimizations
// CHECK: passes: [ "for-each-loop-unroll", "mandatory-combine",
// CHECK: "mandatory-arc-opts" ]
// CHECK: passes: [ "for-each-loop-unroll", "mandatory-combine", "mandatory-arc-opts",
// CHECK: "onone-prespecializer" ]
// CHECK: ---
// CHECK: name: Serialization
// CHECK: passes: [ "serialize-sil", "sil-onone-debuginfo-canonicalizer",
// CHECK-NEXT: "ownership-model-eliminator" ]
// CHECK: ---
// CHECK: name: Rest of Onone
// CHECK: passes: [ "use-prespecialized", "onone-prespecializer", "sil-debuginfo-gen" ]
// CHECK: passes: [ "use-prespecialized", "sil-debuginfo-gen" ]
// CHECK: ...