Skip to content

Commit a8bea1e

Browse files
authored
[SILOpt] Reject existentials in AnyObject specialization application (swiftlang#63477)
Also allow this feature to be enabled in non-assert compilers
1 parent 01e4c8d commit a8bea1e

File tree

3 files changed

+15
-5
lines changed

3 files changed

+15
-5
lines changed

include/swift/Basic/Features.def

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ EXPERIMENTAL_FEATURE(MoveOnlyClasses, true)
111111

112112
EXPERIMENTAL_FEATURE(OneWayClosureParameters, false)
113113
EXPERIMENTAL_FEATURE(TypeWitnessSystemInference, false)
114-
EXPERIMENTAL_FEATURE(LayoutPrespecialization, false)
114+
EXPERIMENTAL_FEATURE(LayoutPrespecialization, true)
115115
EXPERIMENTAL_FEATURE(ModuleInterfaceExportAs, true)
116116

117117
/// Whether to enable experimental differentiable programming features:

lib/SILOptimizer/Utils/Generics.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2884,8 +2884,9 @@ bool usePrespecialized(
28842884

28852885
if (!erased || !layout || !layout->isClass()) {
28862886
newSubs.push_back(entry.value());
2887-
} else if (!entry.value()->isAnyClassReferenceType()) {
2888-
// non-reference type can't be applied
2887+
} else if (!entry.value()->isAnyClassReferenceType() ||
2888+
entry.value()->isAnyExistentialType()) {
2889+
// non-reference or existential type can't be applied
28892890
break;
28902891
} else if (!specializedSig->getRequiredProtocols(genericParam)
28912892
.empty()) {

test/SILOptimizer/pre_specialize_layouts.swift

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616
// RUN: %target-swift-frontend -enable-experimental-feature LayoutPrespecialization -O -swift-version 5 -enable-library-evolution -emit-module -o /dev/null -emit-module-interface-path %t/pre_specialized_module_layouts.swiftinterface %S/Inputs/pre_specialized_module_layouts.swift -module-name pre_specialized_module_layouts
1717
// RUN: %target-swift-frontend -enable-experimental-feature LayoutPrespecialization -I %t -O -Xllvm -sil-disable-pass=function-signature-opts -emit-sil %s | %FileCheck %s --check-prefix=OPT
1818

19-
// REQUIRES: asserts
20-
2119
import pre_specialized_module_layouts
2220

2321
// Helper to prevent return values from getting optimized away
@@ -187,6 +185,17 @@ public func usePartialApply(y: SomeClass) -> (SomeClass) -> SomeClass {
187185
return usePartialApplyInner
188186
}
189187

188+
// NOTE: AnyObject specializations MUST not be applied to existential references.
189+
190+
// OPT: sil @$s22pre_specialize_layouts48useLayoutPrespecializedEntryPointWithExistentialyyAA21SomeReferenceProtocol_pF : $@convention(thin) (@guaranteed any SomeReferenceProtocol) -> () {
191+
// OPT: {{bb.*}}([[A1:%.*]] : $any SomeReferenceProtocol):
192+
// OPT-NOT: {{%.*}} = function_ref @$s30pre_specialized_module_layouts20publicPrespecializedyyxlFyXl_Ts5 : $@convention(thin) (@guaranteed AnyObject) -> ()
193+
// OPT: } // end sil function '$s22pre_specialize_layouts48useLayoutPrespecializedEntryPointWithExistentialyyAA21SomeReferenceProtocol_pF'
194+
public protocol SomeReferenceProtocol: AnyObject {}
195+
public func useLayoutPrespecializedEntryPointWithExistential(_ p: any SomeReferenceProtocol) {
196+
publicPrespecialized(p)
197+
}
198+
190199
// OPT-macosx: sil [available 10.50] @$s22pre_specialize_layouts40usePrespecializedEntryPointsAvailabilityyyF : $@convention(thin) () -> () {
191200
// OPT-macosx: [[F1:%.*]] = function_ref @$s30pre_specialized_module_layouts20publicPrespecializedyyxlFAA8SomeDataV_Ts5 : $@convention(thin) (SomeData) -> ()
192201
// OPT-macosx: apply [[F1]](

0 commit comments

Comments
 (0)