Skip to content

[SILOpt] Apply _Class pre-specializations to wrapped single references #70170

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
merged 2 commits into from
Dec 5, 2023
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
14 changes: 11 additions & 3 deletions lib/SILOptimizer/Utils/Generics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2988,9 +2988,17 @@ bool usePrespecialized(

if (!erased || !layout || !layout->isClass()) {
newSubs.push_back(entry.value());
} else if (!entry.value()->isAnyClassReferenceType() ||
entry.value()->isAnyExistentialType()) {
// non-reference or existential type can't be applied
continue;
}

auto lowered = refF->getLoweredType(entry.value());
while (auto singleton = lowered.getSingletonAggregateFieldType(
refF->getModule(), refF->getResilienceExpansion())) {
lowered = singleton;
}

if (!lowered.hasRetainablePointerRepresentation()) {
// non-reference type can't be applied
break;
} else if (!specializedSig->getRequiredProtocols(genericParam)
.empty()) {
Expand Down
23 changes: 20 additions & 3 deletions test/SILOptimizer/pre_specialize_layouts.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@ import pre_specialized_module_layouts
@inline(never)
public func consume<T>(_ x: T) {}

public struct ReferenceWrapperStruct {
let x: AnyObject
}

@_alignment(16)
public struct OveralignedReferenceWrapperStruct {
let x: AnyObject
}

// Make sure we generate the public pre-specialized entry points.

// OPT-DAG: sil @$s22pre_specialize_layouts10testPublic1tyx_tlFSf_Ts5 : $@convention(thin) (Float) -> () {
Expand Down Expand Up @@ -63,7 +72,8 @@ internal func testEmitIntoClient<T>(t: T) {
print(t)
}

// OPT: sil @$s22pre_specialize_layouts28usePrespecializedEntryPointsyyF : $@convention(thin) () -> () {
// OPT: sil @$s22pre_specialize_layouts28usePrespecializedEntryPoints13wrapperStruct11overalignedyAA016ReferenceWrapperI0V_AA011OveralignedklI0VtF : $@convention(thin) (@guaranteed ReferenceWrapperStruct, @guaranteed OveralignedReferenceWrapperStruct) -> () {
// OPT: bb0([[P1:%.*]] : $ReferenceWrapperStruct, [[P2:%.*]] : $OveralignedReferenceWrapperStruct):
// OPT: [[F1:%.*]] = function_ref @$s30pre_specialized_module_layouts20publicPrespecializedyyxlFSi_Ts5 : $@convention(thin) (Int) -> ()
// OPT: apply [[F1]]
// OPT: [[F2:%.*]] = function_ref @$s30pre_specialized_module_layouts20publicPrespecializedyyxlFSd_Ts5 : $@convention(thin) (Double) -> ()
Expand All @@ -73,13 +83,17 @@ internal func testEmitIntoClient<T>(t: T) {
// OPT: [[F7:%.*]] = function_ref @$s30pre_specialized_module_layouts20publicPrespecializedyyxlFyXl_Ts5 : $@convention(thin) (@guaranteed AnyObject) -> ()
// OPT: [[A1:%.*]] = unchecked_ref_cast {{%.*}} : $SomeClass to $AnyObject
// OPT: apply [[F7]]([[A1]]) : $@convention(thin) (@guaranteed AnyObject) -> ()
// OPT: [[A2:%.*]] = unchecked_bitwise_cast [[P1]] : $ReferenceWrapperStruct to $AnyObject
// OPT: apply [[F7]]([[A2]]) : $@convention(thin) (@guaranteed AnyObject) -> ()
// OPT: [[A3:%.*]] = alloc_stack $OveralignedReferenceWrapperStruct
// OPT: apply {{%.*}}<OveralignedReferenceWrapperStruct>([[A3]])
// OPT: [[F3:%.*]] = function_ref @$s30pre_specialized_module_layouts36internalEmitIntoClientPrespecializedyyxlFSi_Ts5 : $@convention(thin) (Int) -> ()
// OPT: apply [[F3]]
// OPT: [[F4:%.*]] = function_ref @$s30pre_specialized_module_layouts36internalEmitIntoClientPrespecializedyyxlFSd_Ts5 : $@convention(thin) (Double) -> ()
// OPT: apply [[F4]]
// OPT: [[F5:%.*]] = function_ref @$s30pre_specialized_module_layouts16useInternalThingyyxlFSi_Tg5
// OPT: apply [[F5]]({{.*}}) : $@convention(thin) (Int) -> ()
// OPT: } // end sil function '$s22pre_specialize_layouts28usePrespecializedEntryPointsyyF'
// OPT: } // end sil function '$s22pre_specialize_layouts28usePrespecializedEntryPoints13wrapperStruct11overalignedyAA016ReferenceWrapperI0V_AA011OveralignedklI0VtF'

// OPT: sil {{.*}} @$s30pre_specialized_module_layouts16useInternalThingyyxlFSi_Tg5 : $@convention(thin) (Int) -> () {
// OPT: [[F1:%.*]] = function_ref @$s30pre_specialized_module_layouts14InternalThing2V7computexyFSi_Ts5 : $@convention(method) (InternalThing2<Int>) -> Int
Expand Down Expand Up @@ -124,11 +138,14 @@ internal func testEmitIntoClient<T>(t: T) {
// OPT: [[R10:%.*]] = unchecked_addr_cast [[R9]] : $*AnyObject to $*SomeClass
// OPT: } // end sil function '$s30pre_specialized_module_layouts16useInternalThingyyxlFAA9SomeClassC_Tg5'

public func usePrespecializedEntryPoints() {
public func usePrespecializedEntryPoints(wrapperStruct: ReferenceWrapperStruct, overaligned: OveralignedReferenceWrapperStruct) {
publicPrespecialized(1)
publicPrespecialized(1.0)
publicPrespecialized(SomeData())
publicPrespecialized(SomeClass())
publicPrespecialized(wrapperStruct)
// should not apply _Class specialization for overaligned struct
publicPrespecialized(overaligned)
useInternalEmitIntoClientPrespecialized(2)
useInternalEmitIntoClientPrespecialized(2.0)
useInternalThing(2)
Expand Down