Skip to content

Commit 195ca7c

Browse files
committed
Improve LifetimeDependence.Scope for mutable trivial variables.
Recognize dependence on the address of a trivial 'var' as an "access" dependence instead of an "unknown" dependence. This allows the mark_dependence to be resolved as "[nonescaping]".
1 parent 49136f2 commit 195ca7c

File tree

5 files changed

+18
-10
lines changed

5 files changed

+18
-10
lines changed

SwiftCompilerSources/Sources/Optimizer/Utilities/LifetimeDependenceUtils.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,8 @@ extension LifetimeDependence.Scope {
394394
self = .borrowed(BeginBorrowValue(bbi)!)
395395
case is MoveValueInst:
396396
self = .owned(introducer)
397+
case let bai as BeginAccessInst:
398+
self = .access(bai)
397399
default:
398400
self = .unknown(introducer)
399401
}

test/SILGen/addressors.swift

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ struct A {
4646
// CHECK: [[BASE:%.*]] = load [[T0]] : $*UnsafeMutablePointer<Int32>
4747
// CHECK: return [[BASE]] : $UnsafeMutablePointer<Int32>
4848

49+
// SILGEN-LABEL: sil hidden [ossa] @$s10addressors5test0yyF : $@convention(thin) () -> () {
4950
// CHECK-LABEL: sil hidden @$s10addressors5test0yyF : $@convention(thin) () -> () {
5051
func test0() {
5152
// CHECK: [[A:%.*]] = alloc_stack [var_decl] $A
@@ -55,12 +56,16 @@ func test0() {
5556
// CHECK: store [[AVAL]] to [[A]]
5657
var a = A()
5758

58-
// CHECK: [[ACCESS:%.*]] = begin_access [read] [static] [[A]] : $*A
59+
// SILGEN: [[ACCESS:%.*]] = begin_access [read] [unknown] %{{.*}} : $*A
60+
// SILGEN: [[LD:%.*]] = load [trivial] [[ACCESS]] : $*A
61+
// SILGEN: [[T3:%.*]] = pointer_to_address %{{.*}} : $Builtin.RawPointer to [strict] $*Int32
62+
// SILGEN: [[MD:%.*]] = mark_dependence [unresolved] [[T3]] : $*Int32 on [[LD]] : $A
63+
5964
// CHECK: [[T0:%.*]] = function_ref @$s10addressors1AVys5Int32VAEcilu :
6065
// CHECK: [[T1:%.*]] = apply [[T0]]({{%.*}}, [[AVAL]])
6166
// CHECK: [[T2:%.*]] = struct_extract [[T1]] : $UnsafePointer<Int32>, #UnsafePointer._rawValue
6267
// CHECK: [[T3:%.*]] = pointer_to_address [[T2]] : $Builtin.RawPointer to [strict] $*Int32
63-
// CHECK: [[MD:%.*]] = mark_dependence [nonescaping] [[T3]] : $*Int32 on [[ACCESS]] : $*A
68+
// CHECK: [[MD:%.*]] = mark_dependence [nonescaping] [[T3]] : $*Int32 on [[AVAL]] : $A
6469
// CHECK: [[ACCESS:%.*]] = begin_access [read] [unsafe] [[MD]] : $*Int32
6570
// CHECK: [[Z:%.*]] = load [[ACCESS]] : $*Int32
6671
let z = a[10]

test/SILOptimizer/lifetime_dependence/lifetime_dependence_scope_fixup.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ public func test10() {
231231
// CHECK-LABEL: sil hidden @$s31lifetime_dependence_scope_fixup37testPointeeDependenceOnMutablePointer1pySPys5Int64VG_tF : $@convention(thin) (UnsafePointer<Int64>) -> () {
232232
// CHECK: bb0(%0 : $UnsafePointer<Int64>):
233233
// CHECK: [[ALLOC:%.*]] = alloc_stack [var_decl] $UnsafePointer<Int64>, var, name "ptr", type $UnsafePointer<Int64>
234-
// CHECK: mark_dependence %{{.*}} on %0
234+
// CHECK: mark_dependence [nonescaping] %{{.*}} on %0
235235
// CHECK-LABEL: } // end sil function '$s31lifetime_dependence_scope_fixup37testPointeeDependenceOnMutablePointer1pySPys5Int64VG_tF'
236236
func testPointeeDependenceOnMutablePointer(p: UnsafePointer<Int64>) {
237237
var ptr = p

test/SILOptimizer/lifetime_dependence/scopefixup.sil

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,11 @@ sil @pointeeAddressor : $@convention(method) <τ_0_0 where τ_0_0 : ~Copyable> (
9191
// CHECK: bb0(%0 : $UnsafePointer<Int64>):
9292
// CHECK: [[ACCESS:%.*]] = begin_access [read] [unknown]
9393
// CHECK: [[LD:%.*]] = load [trivial] [[ACCESS]]
94-
// CHECK: end_access [[ACCESS]]
9594
// CHECK: [[ADR:%.*]] = pointer_to_address %{{.*}} to [strict] $*Int64
96-
// CHECK: mark_dependence [unresolved] [[ADR]] on [[LD]]
95+
// CHECK: [[MD:%.*]] = mark_dependence [unresolved] [[ADR]] on [[LD]]
96+
// CHECK: [[ACCESS2:%.*]] = begin_access [read] [unsafe] [[MD]]
97+
// CHECK: end_access [[ACCESS2]]
98+
// CHECK: end_access [[ACCESS]]
9799
// CHECK-LABEL: } // end sil function 'testTrivialAccess'
98100
sil hidden [ossa] @testTrivialAccess : $@convention(thin) (UnsafePointer<Int64>) -> () {
99101
bb0(%0 : $UnsafePointer<Int64>):

test/SILOptimizer/unsafeAddress.swift

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -97,13 +97,11 @@ func testSMod(s: inout S) {
9797

9898
// Accessing s.data causes an escaping dependence because we don't extend the local access scope of 's'.
9999
//
100-
// TODO: could we notice the local access is already nested inside the same kind of access (modify) and consider this to
101-
// be 'mark_dependence [nonescaping]'?
102-
//
103100
// CHECK-LABEL: sil hidden @$s4main16testSInoutBorrow5mut_syAA1SVz_tF : $@convention(thin) (@inout S) -> () {
104101
// CHECK: [[ACCESS:%.*]] = begin_access [read] [static] %0
102+
// CHECK: [[LD:%.*]] = load [[ACCESS]]
105103
// CHECK: [[ADR:%.*]] = pointer_to_address %{{.*}} to [strict] $*NC
106-
// CHECK: [[MD:%.*]] = mark_dependence [[ADR]] on [[ACCESS]]
104+
// CHECK: [[MD:%.*]] = mark_dependence [nonescaping] [[ADR]] on [[LD]]
107105
// CHECK: begin_access [read] [unsafe] [[MD]]
108106
// CHECK: apply
109107
// CHECK: end_access
@@ -114,8 +112,9 @@ func testSInoutBorrow(mut_s s: inout S) {
114112

115113
// CHECK-LABEL: sil hidden @$s4main19testSInoutMutBorrow5mut_syAA1SVz_tF : $@convention(thin) (@inout S) -> () {
116114
// CHECK: [[ACCESS:%.*]] = begin_access [read] [static] %0
115+
// CHECK: [[LD:%.*]] = load [[ACCESS]]
117116
// CHECK: [[ADR:%.*]] = pointer_to_address %{{.*}} to [strict] $*NC
118-
// CHECK: [[MD:%.*]] = mark_dependence [nonescaping] [[ADR]] on [[ACCESS]]
117+
// CHECK: [[MD:%.*]] = mark_dependence [nonescaping] [[ADR]] on [[LD]]
119118
// CHECK: begin_access [read] [unsafe] [[MD]]
120119
// CHECK: apply
121120
// CHECK: end_access

0 commit comments

Comments
 (0)