Skip to content

[PackageCMO] Optimize witness thunks. #74468

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 1 commit into from
Jun 18, 2024
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
2 changes: 1 addition & 1 deletion lib/SIL/IR/Linker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ void SILLinkerVisitor::maybeAddFunctionToWorklist(
if (callerSerializedKind == IsSerialized &&
hasSharedVisibility(linkage) &&
!Mod.isSerialized() &&
!F->isSerialized()) {
!F->isAnySerialized()) {
F->setSerializedKind(IsSerialized);

// Push the function to the worklist so that all referenced shared functions
Expand Down
14 changes: 4 additions & 10 deletions lib/SIL/IR/SILWitnessTable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ SILWitnessTable::SILWitnessTable(
ArrayRef<ConditionalConformance> conditionalConformances)
: Mod(M), Name(N), Linkage(Linkage), Conformance(Conformance), Entries(),
ConditionalConformances(), IsDeclaration(true),
SerializedKind(IsNotSerialized) {
SerializedKind(SerializedKind) {
convertToDefinition(entries, conditionalConformances, SerializedKind);
}

Expand Down Expand Up @@ -169,21 +169,15 @@ void SILWitnessTable::convertToDefinition(

SerializedKind_t SILWitnessTable::conformanceSerializedKind(
const RootProtocolConformance *conformance) {
// Allow serializing conformance with package or public access level
// if package serialization is enabled.
auto optInPackage = conformance->getDeclContext()->getParentModule()->serializePackageEnabled();
auto accessLevelToCheck =
optInPackage ? AccessLevel::Package : AccessLevel::Public;

auto normalConformance = dyn_cast<NormalProtocolConformance>(conformance);
if (normalConformance && normalConformance->isResilient() && !optInPackage)
if (normalConformance && normalConformance->isResilient())
return IsNotSerialized;

if (conformance->getProtocol()->getEffectiveAccess() < accessLevelToCheck)
if (conformance->getProtocol()->getEffectiveAccess() < AccessLevel::Public)
return IsNotSerialized;

auto *nominal = conformance->getDeclContext()->getSelfNominalTypeDecl();
if (nominal->getEffectiveAccess() >= accessLevelToCheck)
if (nominal->getEffectiveAccess() >= AccessLevel::Public)
return IsSerialized;

return IsNotSerialized;
Expand Down
193 changes: 105 additions & 88 deletions lib/SILOptimizer/IPO/CrossModuleOptimization.cpp

Large diffs are not rendered by default.

76 changes: 53 additions & 23 deletions test/SILOptimizer/package-cmo-non-resilient-mode.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,34 @@
import Module
import ModuleTBD

// static ModuleStruct.privateFunctionPointer
// CHECK-LABEL: sil_global public_external @$s6Module0A6StructV22privateFunctionPointeryS2icvpZ : $@callee_guaranteed (Int) -> Int{{$}}

// static ModuleStruct.publicFunctionPointer
// CHECK-LABEL: sil_global public_external [serialized] @$s6Module0A6StructV21publicFunctionPointeryS2icvpZ : $@callee_guaranteed (Int) -> Int


// CHECK-LABEL: sil @$s4Main26callPrivateFunctionPointeryS2iF : $@convention(thin) (Int) -> Int {
// CHECK: global_addr @$s6Module0A6StructV22privateFunctionPointeryS2icvpZ
// CHECK: load
// CHECK: apply
public func callPrivateFunctionPointer(_ x: Int) -> Int {
return Module.ModuleStruct.privateFunctionPointer(x)
}

// CHECK-LABEL: sil_global package_external @$s6Module03PkgA6StructV14closurePointeryS2icvpZ : $@callee_guaranteed (Int) -> Int{{$}}
// CHECK-LABEL: sil package @$s4Main27callStaticPkgClosurePointeryS2iF : $@convention(thin) (Int) -> Int {
// CHECK: function_ref @$s6Module03PkgA6StructV14closurePointeryS2icvau
// CHECK: apply
// CHECK: pointer_to_address
// CHECK: load
// CHECK: apply

// PkgModuleStruct.closurePointer.unsafeMutableAddressor
// CHECK: sil package_external [global_init] @$s6Module03PkgA6StructV14closurePointeryS2icvau : $@convention(thin) () -> Builtin.RawPointer
package func callStaticPkgClosurePointer(_ x: Int) -> Int {
return Module.PkgModuleStruct.closurePointer(x)
}

// static ModuleStruct.publicFunctionPointer
// CHECK-LABEL: sil_global public_external [serialized] @$s6Module0A6StructV21publicFunctionPointeryS2icvpZ : $@callee_guaranteed (Int) -> Int
// static PkgModuleStruct.funcPointer
// CHECK-LABEL: sil_global package_external [serialized] @$s6Module03PkgA6StructV11funcPointeryS2icvpZ : $@callee_guaranteed (Int) -> Int


// CHECK-LABEL: sil @$s4Main25callPublicFunctionPointeryS2iF : $@convention(thin) (Int) -> Int {
// CHECK: global_addr @$s6Module0A6StructV21publicFunctionPointeryS2icvpZ : $*@callee_guaranteed (Int) -> Int
// CHECK: load
Expand All @@ -37,10 +49,14 @@ public func callPublicFunctionPointer(_ x: Int) -> Int {
}

// CHECK-LABEL: sil package @$s4Main28callStaticPkgFunctionPointeryS2iF : $@convention(thin) (Int) -> Int {
// CHECK: global_addr @$s6Module03PkgA6StructV11funcPointeryS2icvpZ : $*@callee_guaranteed (Int) -> Int
// CHECK: function_ref @$s6Module03PkgA6StructV11funcPointeryS2icvau
// CHECK: pointer_to_address
// CHECK: load
// CHECK: apply
// CHECK: } // end sil function '$s4Main28callStaticPkgFunctionPointeryS2iF'

// PkgModuleStruct.funcPointer.unsafeMutableAddressor
// CHECK-LABEL: sil package_external [global_init] @$s6Module03PkgA6StructV11funcPointeryS2icvau : $@convention(thin) () -> Builtin.RawPointer
package func callStaticPkgFunctionPointer(_ x: Int) -> Int {
return Module.PkgModuleStruct.funcPointer(x)
}
Expand Down Expand Up @@ -68,9 +84,12 @@ public func doIncrement(_ x: Int) -> Int {
}

// CHECK-LABEL: sil package @$s4Main11callPkgFuncyS2iF : $@convention(thin) (Int) -> Int {
// CHECK-NOT: function_ref
// CHECK-NOT: apply
// CHECK: function_ref @$s6Module7pkgFuncyS2iF
// CHECK: apply
// CHECK: } // end sil function '$s4Main11callPkgFuncyS2iF'

// pkgFunc(_:)
// CHECK-LABEL: sil package_external @$s6Module7pkgFuncyS2iF : $@convention(thin) (Int) -> Int
package func callPkgFunc(_ x: Int) -> Int {
return Module.pkgFunc(x)
}
Expand All @@ -83,8 +102,10 @@ public func doIncrementWithCall(_ x: Int) -> Int {
}

// CHECK-LABEL: sil package @$s4Main16callPkgFuncNoCMOyS2iF : $@convention(thin) (Int) -> Int {
// CHECK: function_ref @$s9Submodule15subPkgFuncNoCMOyS2iF
// CHECK: function_ref @$s6Module12pkgFuncNoCMOyS2iF
// CHECK: } // end sil function '$s4Main16callPkgFuncNoCMOyS2iF'
// pkgFuncNoCMO(_:)
// CHECK-LABEL: sil package_external @$s6Module12pkgFuncNoCMOyS2iF : $@convention(thin) (Int) -> Int
package func callPkgFuncNoCMO(_ x: Int) -> Int {
return Module.pkgFuncNoCMO(x)
}
Expand All @@ -98,9 +119,11 @@ public func doIncrementTBD(_ x: Int) -> Int {
}

// CHECK-LABEL: sil package @$s4Main14callPkgFuncTBDyS2iF : $@convention(thin) (Int) -> Int {
// CHECK-NOT: function_ref
// CHECK-NOT: apply
// CHECK: function_ref @$s9ModuleTBD7pkgFuncyS2iF
// CHECK: apply
// CHECK: } // end sil function '$s4Main14callPkgFuncTBDyS2iF'
// pkgFunc(_:)
// CHECK-LABEL: sil package_external @$s9ModuleTBD7pkgFuncyS2iF : $@convention(thin) (Int) -> Int
package func callPkgFuncTBD(_ x: Int) -> Int {
return ModuleTBD.pkgFunc(x)
}
Expand Down Expand Up @@ -130,9 +153,11 @@ public func getSubmoduleKlassMember() -> Int {
}

// CHECK-LABEL: sil package @$s4Main26getPkgSubmoduleKlassMemberSiyF : $@convention(thin) () -> Int {
// CHECK-NOT: function_ref
// CHECK-NOT: apply
// CHECK: function_ref @$s6Module23pkgSubmoduleKlassMemberSiyF
// CHECK: apply
// CHECK: } // end sil function '$s4Main26getPkgSubmoduleKlassMemberSiyF'
// pkgSubmoduleKlassMember()
// CHECK-LABEL: sil package_external @$s6Module23pkgSubmoduleKlassMemberSiyF : $@convention(thin) () -> Int
package func getPkgSubmoduleKlassMember() -> Int {
return Module.pkgSubmoduleKlassMember()
}
Expand All @@ -146,9 +171,12 @@ public func getSubmoduleKlassMemberTBD() -> Int {
}

// CHECK-LABEL: sil package @$s4Main29getPkgSubmoduleKlassMemberTBDSiyF : $@convention(thin) () -> Int {
// CHECK-NOT: function_ref
// CHECK-NOT: apply
// CHECK: function_ref @$s9ModuleTBD23pkgSubmoduleKlassMemberSiyF
// CHECK: apply
// CHECK: } // end sil function '$s4Main29getPkgSubmoduleKlassMemberTBDSiyF'

// pkgSubmoduleKlassMember()
// CHECK-LABEL:sil package_external @$s9ModuleTBD23pkgSubmoduleKlassMemberSiyF : $@convention(thin) () -> Int
package func getPkgSubmoduleKlassMemberTBD() -> Int {
return ModuleTBD.pkgSubmoduleKlassMember()
}
Expand All @@ -162,9 +190,11 @@ public func getModuleKlassMember() -> Int {
}

// CHECK-LABEL: sil package @$s4Main23getPkgModuleKlassMemberSiyF : $@convention(thin) () -> Int {
// CHECK-NOT: function_ref
// CHECK-NOT: apply
// CHECK: function_ref @$s6Module03pkgA11KlassMemberSiyF
// CHECK: apply
// CHECK: } // end sil function '$s4Main23getPkgModuleKlassMemberSiyF'
// pkgModuleKlassMember()
// CHECK-LABEL: sil package_external @$s6Module03pkgA11KlassMemberSiyF : $@convention(thin) () -> Int
package func getPkgModuleKlassMember() -> Int {
return Module.pkgModuleKlassMember()
}
Expand All @@ -178,14 +208,14 @@ public func getModuleKlassMemberTBD() -> Int {
}

// CHECK-LABEL: sil package @$s4Main26getPkgModuleKlassMemberTBDSiyF : $@convention(thin) () -> Int {
// CHECK-NOT: function_ref
// CHECK-NOT: apply
// CHECK: function_ref @$s9ModuleTBD03pkgA11KlassMemberSiyF
// CHECK: apply
// CHECK: } // end sil function '$s4Main26getPkgModuleKlassMemberTBDSiyF'
// pkgModuleKlassMember()
// CHECK-LABEL: sil package_external @$s9ModuleTBD03pkgA11KlassMemberSiyF : $@convention(thin) () -> Int
package func getPkgModuleKlassMemberTBD() -> Int {
return ModuleTBD.pkgModuleKlassMember()
}


// CHECK-LABEL: sil [_semantics "optimize.no.crossmodule"] @$s9Submodule19incrementByOneNoCMOyS2iF : $@convention(thin) (Int) -> Int

// CHECK-LABEL: sil package_external [_semantics "optimize.no.crossmodule"] @$s9Submodule15subPkgFuncNoCMOyS2iF : $@convention(thin) (Int) -> Int
Loading