Skip to content

Commit 9f1d727

Browse files
authored
Merge pull request #78629 from atrick/fix-borrow-depend
LifetimeDependenceInsertion: fix dependence on store_borrow.
2 parents fba8095 + a8caedf commit 9f1d727

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed

SwiftCompilerSources/Sources/Optimizer/Utilities/AddressUtils.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,11 @@ extension AccessBase {
215215
return nil
216216
}
217217
baseAddr = arg
218+
case let .storeBorrow(sbi):
219+
guard case let .stack(allocStack) = sbi.destinationOperand.value.accessBase else {
220+
return nil
221+
}
222+
return (initialAddress: allocStack, initializingStore: sbi)
218223
default:
219224
return nil
220225
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// RUN: %target-sil-opt %s \
2+
// RUN: --lifetime-dependence-insertion \
3+
// RUN: -sil-verify-all \
4+
// RUN: -enable-experimental-feature LifetimeDependence \
5+
// RUN: 2>&1 | %FileCheck %s
6+
7+
// REQUIRES: swift_in_compiler
8+
// REQUIRES: swift_feature_LifetimeDependence
9+
10+
sil_stage raw
11+
12+
import Swift
13+
14+
struct NE: ~Escapable {
15+
var p: UnsafeRawPointer
16+
17+
@lifetime(immortal)
18+
init()
19+
}
20+
21+
sil @getPtr : $@convention(thin) () -> @out UnsafeRawPointer
22+
sil @getSpan : $@convention(thin) (@in_guaranteed AnyObject) -> @lifetime(borrow 0) @out NE
23+
24+
// Check that the inserted dependence is on the 'self' argument, not the temporary borrow.
25+
//
26+
// CHECK-LABEL: sil [available 9999] [ossa] @testSpanProp : $@convention(method) (@guaranteed AnyObject) -> @lifetime(borrow 0) @owned NE {
27+
// CHECK: bb0(%0 : @guaranteed $AnyObject):
28+
// CHECK: [[OUT:%.*]] = alloc_stack $NE
29+
// CHECK: [[TEMP:%.*]] = begin_borrow
30+
// CHECK: [[MV:%.*]] = moveonlywrapper_to_copyable [guaranteed] [[TEMP]]
31+
// CHECK: [[IN:%.*]] = alloc_stack $AnyObject
32+
// CHECK: [[SB:%.*]] = store_borrow [[MV]] to [[IN]]
33+
// CHECK: apply %{{.*}}([[OUT]], [[SB]]) : $@convention(thin) (@in_guaranteed AnyObject) -> @lifetime(borrow 0) @out NE
34+
// CHECK: [[MD:%.*]] = mark_dependence [unresolved] [[OUT]] on %0
35+
// CHECK: end_borrow [[SB]]
36+
// CHECK: end_borrow [[TEMP]]
37+
// CHECK: [[LD:%.*]] = load [take] [[OUT]]
38+
// CHECK: return [[LD]]
39+
// CHECK-LABEL: } // end sil function 'testSpanProp'
40+
sil [available 9999] [ossa] @testSpanProp : $@convention(method) (@guaranteed AnyObject) -> @lifetime(borrow 0) @owned NE {
41+
bb0(%0 : @guaranteed $AnyObject):
42+
// setup the 'self' variable
43+
%1 = copyable_to_moveonlywrapper [guaranteed] %0
44+
%2 = copy_value %1
45+
%3 = mark_unresolved_non_copyable_value [no_consume_or_assign] %2
46+
debug_value %3, let, name "self", argno 1
47+
%5 = alloc_stack $NE
48+
49+
// temporarily borrow 'self' but don't depend on this borrow.
50+
%6 = begin_borrow %3
51+
%7 = moveonlywrapper_to_copyable [guaranteed] %6
52+
%8 = alloc_stack $AnyObject
53+
%9 = store_borrow %7 to %8
54+
55+
%10 = function_ref @getSpan : $@convention(thin) (@in_guaranteed AnyObject) -> @lifetime(borrow 0) @out NE
56+
%11 = apply %10(%5, %9) : $@convention(thin) (@in_guaranteed AnyObject) -> @lifetime(borrow 0) @out NE
57+
end_borrow %9
58+
dealloc_stack %8
59+
end_borrow %6
60+
%15 = load [take] %5
61+
dealloc_stack %5
62+
destroy_value %3
63+
return %15
64+
}

0 commit comments

Comments
 (0)