Skip to content

Commit 0a734c2

Browse files
committed
[PackageCMO] Global var and accessor linkage should be kept private/hidden in SILGen.
This PR contains changes that ensure the following: * Global accessor linkage is kept hidden if its decl is resilient. - LinkageLimit::Never is returned for Global Accessor if its decl is resilient. - Since it's kept hidden, the use site should not expect a call to a global accessor if the static var decl being accessed is resilient. The bypassing resilience logic in AbstractStorageDecl::isResilient(accessingModule, decl) has been removed; to be addressed in Package CMO optimization pass. * sil_global linkage is kept private if its decl is resilient. Resolves rdar://129829925
1 parent 6ca531c commit 0a734c2

10 files changed

+95
-102
lines changed

lib/AST/Decl.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3060,9 +3060,6 @@ bool AbstractStorageDecl::isResilient(ModuleDecl *M,
30603060
case ResilienceExpansion::Maximal:
30613061
if (M == getModuleContext())
30623062
return false;
3063-
// Access non-resiliently if package optimization is enabled
3064-
if (bypassResilienceInPackage(M))
3065-
return false;
30663063
return isResilient();
30673064
}
30683065
llvm_unreachable("bad resilience expansion");

lib/SIL/IR/SILDeclRef.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,9 @@ static LinkageLimit getLinkageLimit(SILDeclRef constant) {
464464
return Limit::OnDemand;
465465

466466
case Kind::GlobalAccessor:
467-
return cast<VarDecl>(d)->isStrictlyResilient() ? Limit::NeverPublic : Limit::None;
467+
// global unsafeMutableAddressor should be kept hidden if its decl
468+
// is resilient.
469+
return cast<VarDecl>(d)->isResilient() ? Limit::NeverPublic : Limit::None;
468470

469471
case Kind::DefaultArgGenerator:
470472
// If the default argument is to be serialized, only use non-ABI public

lib/SILGen/SILGenGlobalVariable.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ SILGlobalVariable *SILGenModule::getSILGlobalVariable(VarDecl *gDecl,
4242

4343
// Get the linkage for SILGlobalVariable.
4444
FormalLinkage formalLinkage;
45-
if (gDecl->isResilient() &&
46-
!gDecl->getModuleContext()->serializePackageEnabled())
45+
// sil_global linkage should be kept private if its decl is resilient.
46+
if (gDecl->isResilient())
4747
formalLinkage = FormalLinkage::Private;
4848
else
4949
formalLinkage = getDeclLinkage(gDecl);

test/IRGen/package_bypass_resilience_global_accessor.swift

Lines changed: 0 additions & 21 deletions
This file was deleted.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// RUN: %empty-directory(%t)
2+
3+
// RUN: %target-build-swift -module-name=File -package-name Pkg -I%t -emit-ir %s | %FileCheck %s --check-prefix=CHECK-IR-NONRES
4+
// RUN: %target-build-swift -module-name=File -package-name Pkg -I%t -emit-ir %s -enable-library-evolution | %FileCheck %s --check-prefix=CHECK-IR-RES
5+
// RUN: %target-build-swift -module-name=File -package-name Pkg -I%t -emit-ir %s -enable-library-evolution -Xfrontend -experimental-allow-non-resilient-access -Xfrontend -experimental-package-cmo | %FileCheck %s --check-prefix=CHECK-IR-RES
6+
7+
public struct S {
8+
public static var x = "hello world"
9+
}
10+
11+
// CHECK-IR-NONRES: define{{( dllexport)?}}{{( protected)?}} swiftcc ptr @"$s4File1SV1xSSvau"()
12+
// CHECK-IR-RES: define hidden swiftcc ptr @"$s4File1SV1xSSvau"()
13+

test/SILGen/package_bypass_resilience.swift renamed to test/SILGen/package_allow_non_resilient_access.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717

1818
/// To bypass resilience at use site, Client needs to be in the same package as its
1919
/// loaded module and also opt in with -experimental-package-bypass-resilience.
20-
// RUN: %target-swift-frontend -emit-silgen %t/Client.swift -I %t -module-name Client -package-name mypkg -experimental-package-bypass-resilience | %FileCheck %s --check-prefixes=CHECK,CHECK-BYPASS
21-
// RUN: %target-swift-frontend -emit-silgen %t/Client.swift -I %t -module-name Client -package-name mypkg -experimental-package-bypass-resilience -enable-library-evolution | %FileCheck %s --check-prefixes=CHECK,CHECK-BYPASS
20+
// RUN: %target-swift-frontend -emit-silgen %t/Client.swift -I %t -module-name Client -package-name mypkg -experimental-package-bypass-resilience | %FileCheck %s --check-prefixes=CHECK,CHECK-ACCESS
21+
// RUN: %target-swift-frontend -emit-silgen %t/Client.swift -I %t -module-name Client -package-name mypkg -experimental-package-bypass-resilience -enable-library-evolution | %FileCheck %s --check-prefixes=CHECK,CHECK-ACCESS
2222

2323
/// Utils can be built with both -enable-testing and -experimental-allow-non-resilient-access.
2424
// RUN: rm -rf %t/Utils.swiftmodule
@@ -158,7 +158,7 @@ func foo() {
158158
// CHECK: sil hidden [ossa] @$s6Client3fooyyF : $@convention(thin) () -> () {
159159
// CHECK-DEFAULT: function_ref @$s5Utils9PkgStructV6pkgVarSivg : $@convention(method) (@in_guaranteed PkgStruct) -> Int
160160
// CHECK-DEFAULT: sil package_external @$s5Utils9PkgStructV6pkgVarSivg : $@convention(method) (@in_guaranteed PkgStruct) -> Int
161-
// CHECK-BYPASS: struct_element_addr {{.*}} : $*PkgStruct, #PkgStruct.pkgVar
161+
// CHECK-ACCESS: function_ref @$s5Utils9PkgStructV6pkgVarSivg
162162
// CHECK-NONRES: struct_extract {{.*}} : $PkgStruct, #PkgStruct.pkgVar
163163

164164
func bar() {
@@ -168,5 +168,5 @@ func bar() {
168168
// CHECK: sil hidden [ossa] @$s6Client3baryyF : $@convention(thin) () -> () {
169169
// CHECK-DEFAULT: function_ref @$s5Utils9PubStructV6pubVarSivg : $@convention(method) (@in_guaranteed PubStruct) -> Int
170170
// CHECK-DEFAULT: sil @$s5Utils9PubStructV6pubVarSivg : $@convention(method) (@in_guaranteed PubStruct) -> Int
171-
// CHECK-BYPASS: struct_element_addr {{.*}} : $*PubStruct, #PubStruct.pubVar
171+
// CHECK-ACCESS: function_ref @$s5Utils9PubStructV6pubVarSivg
172172
// CHECK-NONRES: struct_extract {{.*}} : $PubStruct, #PubStruct.pubVar
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: split-file %s %t
3+
4+
// RUN: %target-build-swift -module-name=Lib -package-name Pkg -I%t -emit-silgen %t/Lib.swift -o %t/Lib-nonres.sil
5+
// RUN: %FileCheck %s --check-prefix=CHECK-NONRES < %t/Lib-nonres.sil
6+
// RUN: %target-build-swift -module-name=Lib -package-name Pkg -I%t -emit-silgen -O -wmo %t/Lib.swift | %FileCheck %s --check-prefix=CHECK-NONRES
7+
// RUN: %target-build-swift -module-name=Lib -package-name Pkg -I%t -emit-silgen %t/Lib.swift -enable-library-evolution -o %t/Lib-res.sil
8+
// RUN: %FileCheck %s < %t/Lib-res.sil
9+
// RUN: %target-build-swift -module-name=Lib -package-name Pkg -I%t -emit-silgen %t/Lib.swift -enable-library-evolution -O -wmo | %FileCheck %s
10+
// RUN: %target-build-swift -module-name=Lib -package-name Pkg -I%t -emit-silgen %t/Lib.swift -enable-library-evolution -Xfrontend -experimental-allow-non-resilient-access -Xfrontend -experimental-package-cmo -O -wmo | %FileCheck %s
11+
12+
// RUN: %target-build-swift -module-name=Lib -package-name Pkg -I%t -emit-module %t/Lib.swift -enable-library-evolution -Xfrontend -experimental-allow-non-resilient-access -Xfrontend -experimental-package-cmo -O -wmo -o %t/Lib.swiftmodule
13+
// RUN: %target-build-swift -module-name=Client -package-name Pkg -I%t -emit-silgen %t/Client.swift -I %t | %FileCheck %s --check-prefix=CHECK-CLIENT
14+
15+
//--- Client.swift
16+
import Lib
17+
public func client() {
18+
/// Should not be calling S.x.unsafeMutableAddressor when accessing resilient global var;
19+
/// instead, should be calling the opaque getter.
20+
// CHECK-CLIENT-NOT: s3Lib1SV1xSSvau
21+
// CHECK-CLIENT: function_ref @$s3Lib1SV1xSSvgZ
22+
print(S.x)
23+
}
24+
25+
//--- Lib.swift
26+
public struct S {
27+
public static var x = "hello world"
28+
}
29+
30+
// one-time initialization token for x
31+
// CHECK-NONRES: sil_global private @$s3Lib1SV1x_Wz : $Builtin.Word
32+
// CHECK: sil_global private @$s3Lib1SV1x_Wz : $Builtin.Word
33+
34+
// static S.x
35+
// CHECK-NONRES: sil_global @$s3Lib1SV1xSSvpZ : $String
36+
// CHECK: sil_global private @$s3Lib1SV1xSSvpZ : $String
37+
38+
// one-time initialization function for x
39+
// CHECK-NONRES: sil private [global_init_once_fn] [ossa] @$s3Lib1SV1x_WZ : $@convention(c) (Builtin.RawPointer) -> () {
40+
// CHECK: sil private [global_init_once_fn] [ossa] @$s3Lib1SV1x_WZ : $@convention(c) (Builtin.RawPointer) -> () {
41+
42+
// S.x.unsafeMutableAddressor
43+
// CHECK-NONRES: sil [global_init] [ossa] @$s3Lib1SV1xSSvau : $@convention(thin) () -> Builtin.RawPointer {
44+
// CHECK: sil hidden [global_init] [ossa] @$s3Lib1SV1xSSvau : $@convention(thin) () -> Builtin.RawPointer {
45+
// CHECK: global_addr @$s3Lib1SV1x_Wz
46+
// CHECK: address_to_pointer
47+
// function_ref one-time initialization function for x
48+
// CHECK: function_ref @$s3Lib1SV1x_WZ
49+
// CHECK: global_addr @$s3Lib1SV1xSSvpZ
50+
// CHECK: address_to_pointer
51+
52+
// static S.x.getter
53+
// CHECK-NONRES: sil [transparent] [serialized] [ossa] @$s3Lib1SV1xSSvgZ : $@convention(method) (@thin S.Type) -> @owned String {
54+
// CHECK: sil [ossa] @$s3Lib1SV1xSSvgZ : $@convention(method) (@thin S.Type) -> @owned String {

test/SILOptimizer/package-cmo-inlinable.swift

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ public func inUsePubStruct(_ arg: Int) -> PubStruct {
4141
// CHECK-MAIN: sil [ossa] @$s4Main12usePubStructy3Lib0cD0VSiF : $@convention(thin) (Int) -> @out PubStruct {
4242
public func usePubStruct(_ arg: Int) -> PubStruct {
4343
var p = PubStruct(1)
44-
// CHECK-MAIN: struct_element_addr {{.*}} : $*PubStruct, #PubStruct.pub
44+
// CHECK-MAIN: function_ref @$s3Lib9PubStructVyACSicfC
45+
// CHECK-MAIN: function_ref @$s3Lib9PubStructV3pubSivM
4546
p.pub += arg
4647
return p
4748
}
@@ -58,15 +59,17 @@ package func inUseUfiPkgStruct(_ arg: Int) -> UfiPkgStruct {
5859
// CHECK-MAIN: sil package [ossa] @$s4Main15useUfiPkgStructy3Lib0cdE0VSiF : $@convention(thin) (Int) -> @out UfiPkgStruct {
5960
package func useUfiPkgStruct(_ arg: Int) -> UfiPkgStruct {
6061
var p = UfiPkgStruct(1)
61-
// CHECK-MAIN: struct_element_addr {{.*}} : $*UfiPkgStruct, #UfiPkgStruct.ufiPkg
62+
// CHECK-MAIN: function_ref @$s3Lib12UfiPkgStructVyACSicfC
63+
// CHECK-MAIN: function_ref @$s3Lib12UfiPkgStructV03ufiC0SivM
6264
p.ufiPkg += arg
6365
return p
6466
}
6567

6668
// CHECK-MAIN: sil package [ossa] @$s4Main12usePkgStructy3Lib0cD0VSiF : $@convention(thin) (Int) -> @out PkgStruct {
6769
package func usePkgStruct(_ arg: Int) -> PkgStruct {
6870
var p = PkgStruct(1)
69-
// CHECK-MAIN: struct_element_addr {{.*}} : $*PkgStruct, #PkgStruct.pkg
71+
// CHECK-MAIN: function_ref @$s3Lib9PkgStructVyACSicfC
72+
// CHECK-MAIN: function_ref @$s3Lib9PkgStructV3pkgSivM
7073
p.pkg += arg
7174
return p
7275
}

0 commit comments

Comments
 (0)