Skip to content

Commit 12ac804

Browse files
committed
Handle YieldInst in LifetimeDependenceUtils
1 parent 8209294 commit 12ac804

File tree

4 files changed

+78
-17
lines changed

4 files changed

+78
-17
lines changed

SwiftCompilerSources/Sources/Optimizer/FunctionPasses/LifetimeDependenceDiagnostics.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,10 @@ extension DiagnoseDependenceWalker : LifetimeDependenceDefUseWalker {
375375
return diagnostics.checkFunctionResult(operand: operand)
376376
}
377377

378+
mutating func yieldedDependence(result: Operand) -> WalkResult {
379+
return diagnostics.checkFunctionResult(operand: result)
380+
}
381+
378382
// Override AddressUseVisitor here because LifetimeDependenceDefUseWalker
379383
// returns .abortWalk, and we want a more useful crash report.
380384
mutating func unknownAddressUse(of operand: Operand) -> WalkResult {

SwiftCompilerSources/Sources/Optimizer/FunctionPasses/LifetimeDependenceScopeFixup.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,5 +168,9 @@ private struct LifetimeDependenceScopeFixupWalker : LifetimeDependenceDefUseWalk
168168
using operand: Operand) -> WalkResult {
169169
return .continueWalk
170170
}
171+
172+
mutating func yieldedDependence(result: Operand) -> WalkResult {
173+
return .continueWalk
174+
}
171175
}
172176

SwiftCompilerSources/Sources/Optimizer/Utilities/LifetimeDependenceUtils.swift

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -780,7 +780,7 @@ extension LifetimeDependenceUseDefWalker {
780780
/// escapingDependence(on operand: Operand) -> WalkResult
781781
/// returnedDependence(result: Operand) -> WalkResult
782782
/// returnedDependence(address: FunctionArgument, using: Operand) -> WalkResult
783-
///
783+
/// yieldedDependence(result: Operand) -> WalkResult
784784
/// Start walking:
785785
/// walkDown(root: Value)
786786
protocol LifetimeDependenceDefUseWalker : ForwardingDefUseWalker,
@@ -796,6 +796,8 @@ protocol LifetimeDependenceDefUseWalker : ForwardingDefUseWalker,
796796

797797
mutating func returnedDependence(address: FunctionArgument, using: Operand)
798798
-> WalkResult
799+
800+
mutating func yieldedDependence(result: Operand) -> WalkResult
799801
}
800802

801803
extension LifetimeDependenceDefUseWalker {
@@ -841,6 +843,9 @@ extension LifetimeDependenceDefUseWalker {
841843
if operand.instruction is ReturnInst, !operand.value.type.isEscapable {
842844
return returnedDependence(result: operand)
843845
}
846+
if operand.instruction is YieldInst, !operand.value.type.isEscapable {
847+
return yieldedDependence(result: operand)
848+
}
844849
return escapingDependence(on: operand)
845850
}
846851
}
@@ -1009,6 +1014,9 @@ extension LifetimeDependenceDefUseWalker {
10091014
if operand.instruction is ReturnInst, !operand.value.type.isEscapable {
10101015
return returnedDependence(result: operand)
10111016
}
1017+
if operand.instruction is YieldInst, !operand.value.type.isEscapable {
1018+
return yieldedDependence(result: operand)
1019+
}
10121020
return escapingDependence(on: operand)
10131021
}
10141022

@@ -1208,6 +1216,11 @@ private struct LifetimeDependenceUsePrinter : LifetimeDependenceDefUseWalker {
12081216
print("Returned use: \(operand) in: \(address)")
12091217
return .continueWalk
12101218
}
1219+
1220+
mutating func yieldedDependence(result: Operand) -> WalkResult {
1221+
print("Yielded use: \(result)")
1222+
return .continueWalk
1223+
}
12111224
}
12121225

12131226
let lifetimeDependenceUseTest = FunctionTest("lifetime_dependence_use") {

test/SILOptimizer/lifetime_dependence_scope_fixup.swift

Lines changed: 56 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,50 +12,64 @@
1212

1313
struct NCContainer : ~Copyable {
1414
let ptr: UnsafeRawBufferPointer
15-
init(_ ptr: UnsafeRawBufferPointer) {
15+
let c: Int
16+
init(_ ptr: UnsafeRawBufferPointer, _ c: Int) {
1617
self.ptr = ptr
18+
self.c = c
1719
}
1820
}
1921

2022
struct NEContainer : ~Escapable {
2123
let ptr: UnsafeRawBufferPointer
24+
let c: Int
2225
@_unsafeNonescapableResult
23-
init(_ ptr: UnsafeRawBufferPointer) {
26+
init(_ ptr: UnsafeRawBufferPointer, _ c: Int) {
2427
self.ptr = ptr
28+
self.c = c
2529
}
2630
}
2731

2832
struct View : ~Escapable {
2933
let ptr: UnsafeRawBufferPointer
34+
let c: Int
3035
@_unsafeNonescapableResult
31-
init(_ ptr: UnsafeRawBufferPointer) {
36+
init(_ ptr: UnsafeRawBufferPointer, _ c: Int) {
3237
self.ptr = ptr
38+
self.c = c
3339
}
3440
init(_ otherBV: borrowing View) {
3541
self.ptr = otherBV.ptr
42+
self.c = otherBV.c
3643
}
3744
init(_ k: borrowing NCContainer) {
3845
self.ptr = k.ptr
46+
self.c = k.c
3947
}
4048
init(_ k: consuming NEContainer) {
4149
self.ptr = k.ptr
50+
self.c = k.c
4251
}
4352
}
4453

4554
struct MutableView : ~Copyable, ~Escapable {
4655
let ptr: UnsafeRawBufferPointer
56+
let c: Int
4757
@_unsafeNonescapableResult
48-
init(_ ptr: UnsafeRawBufferPointer) {
58+
init(_ ptr: UnsafeRawBufferPointer, _ c: Int) {
4959
self.ptr = ptr
60+
self.c = c
5061
}
5162
init(_ otherBV: borrowing View) {
5263
self.ptr = otherBV.ptr
64+
self.c = otherBV.c
5365
}
5466
init(_ k: borrowing NCContainer) {
5567
self.ptr = k.ptr
68+
self.c = k.c
5669
}
5770
init(_ k: consuming NEContainer) {
5871
self.ptr = k.ptr
72+
self.c = k.c
5973
}
6074
}
6175

@@ -72,24 +86,24 @@ func getConsumingView(_ x: consuming NEContainer) -> _consume(x) View {
7286
}
7387

7488
func getConsumingView(_ x: consuming View) -> _consume(x) View {
75-
return View(x.ptr)
89+
return View(x.ptr, x.c)
7690
}
7791

7892
func getBorrowingView(_ x: borrowing View) -> _borrow(x) View {
79-
return View(x.ptr)
93+
return View(x.ptr, x.c)
8094
}
8195

8296
func getBorrowingView(_ x: borrowing NCContainer) -> _borrow(x) View {
83-
return View(x.ptr)
97+
return View(x.ptr, x.c)
8498
}
8599

86100
func getBorrowingView(_ x: borrowing NEContainer) -> _borrow(x) View {
87-
return View(x.ptr)
101+
return View(x.ptr, x.c)
88102
}
89103

90104
func test1(_ a: Array<Int>) {
91105
a.withUnsafeBytes {
92-
var x = NEContainer($0)
106+
var x = NEContainer($0, a.count)
93107
mutate(&x)
94108
let view = getConsumingView(x)
95109
let newView = View(view)
@@ -100,7 +114,7 @@ func test1(_ a: Array<Int>) {
100114

101115
func test2(_ a: Array<Int>) {
102116
a.withUnsafeBytes {
103-
var x = NCContainer($0)
117+
var x = NCContainer($0, a.count)
104118
mutate(&x)
105119
let view = getBorrowingView(x)
106120
use(view)
@@ -110,7 +124,7 @@ func test2(_ a: Array<Int>) {
110124

111125
func test3(_ a: Array<Int>) {
112126
a.withUnsafeBytes {
113-
var x = View($0)
127+
var x = View($0, a.count)
114128
mutate(&x)
115129
let view = getConsumingView(x)
116130
use(view)
@@ -123,7 +137,7 @@ func test3(_ a: Array<Int>) {
123137
// def-use chain involving a stack temporary
124138
func test4(_ a: Array<Int>) {
125139
a.withUnsafeBytes {
126-
var x = NCContainer($0)
140+
var x = NCContainer($0, a.count)
127141
mutate(&x)
128142
let view = MutableView(x)
129143
use(view)
@@ -134,7 +148,7 @@ func test4(_ a: Array<Int>) {
134148

135149
func test5(_ a: Array<Int>) {
136150
a.withUnsafeBytes {
137-
let x = NEContainer($0)
151+
let x = NEContainer($0, a.count)
138152
let view = getBorrowingView(x)
139153
let anotherView = getConsumingView(view)
140154
use(anotherView)
@@ -144,7 +158,7 @@ func test5(_ a: Array<Int>) {
144158
func test6(_ a: Array<Int>) {
145159
var p : View?
146160
a.withUnsafeBytes {
147-
var x = NCContainer($0)
161+
var x = NCContainer($0, a.count)
148162
mutate(&x)
149163
let view = View(x)
150164
p = view
@@ -153,7 +167,7 @@ func test6(_ a: Array<Int>) {
153167
}
154168

155169
func test7(_ a: UnsafeRawBufferPointer) {
156-
var x = NEContainer(a)
170+
var x = NEContainer(a, a.count)
157171
do {
158172
let view = getBorrowingView(x)
159173
use(view)
@@ -163,10 +177,36 @@ func test7(_ a: UnsafeRawBufferPointer) {
163177

164178
func test8(_ a: Array<Int>) {
165179
a.withUnsafeBytes {
166-
var x = NEContainer($0)
180+
var x = NEContainer($0, a.count)
167181
mutate(&x)
168182
let view = MutableView(x)
169183
use(view)
170184
consume(view)
171185
}
172186
}
187+
188+
struct Wrapper : ~Escapable {
189+
var _view: View
190+
var view: View {
191+
_read {
192+
yield _view
193+
}
194+
_modify {
195+
yield &_view
196+
}
197+
}
198+
init(_ view: consuming View) {
199+
self._view = view
200+
}
201+
}
202+
203+
func test9() {
204+
let a = [Int](repeating: 0, count: 4)
205+
a.withUnsafeBytes {
206+
let view = View($0, a.count)
207+
var c = Wrapper(view)
208+
use(c.view)
209+
mutate(&c.view)
210+
}
211+
}
212+

0 commit comments

Comments
 (0)