@@ -249,7 +249,7 @@ private struct ScopeExtension {
249
249
250
250
private extension LifetimeDependence . Scope {
251
251
/// The instruction that introduces an extendable scope. This returns a non-nil scope introducer for
252
- /// Extendable .nestedScopes.
252
+ /// ScopeExtension .nestedScopes.
253
253
var extendableBegin : Instruction ? {
254
254
switch self {
255
255
case let . access( beginAccess) :
@@ -258,6 +258,17 @@ private extension LifetimeDependence.Scope {
258
258
return beginBorrow. value. definingInstruction!
259
259
case let . yield( yieldedValue) :
260
260
return yieldedValue. definingInstruction!
261
+ case let . initialized( initializer) :
262
+ switch initializer {
263
+ case let . store( initializingStore: store, initialAddress: _) :
264
+ if let sb = store as? StoreBorrowInst {
265
+ return sb
266
+ }
267
+ return nil
268
+ case . argument, . yield:
269
+ // TODO: extend indirectly yielded scopes.
270
+ return nil
271
+ }
261
272
default :
262
273
return nil
263
274
}
@@ -277,29 +288,31 @@ private extension LifetimeDependence.Scope {
277
288
let accessExtension = gatherAccessExtension ( beginAccess: beginAccess, innerScopes: & innerScopes)
278
289
return SingleInlineArray ( element: accessExtension)
279
290
case let . borrowed( beginBorrow) :
280
- let borrowedValue = beginBorrow. baseOperand!. value
281
- let enclosingScope = LifetimeDependence . Scope ( base: borrowedValue, context)
282
- innerScopes. push ( self )
283
- var innerBorrowScopes = innerScopes
284
- innerBorrowScopes. push ( enclosingScope)
285
- if let extensions = enclosingScope. gatherExtensions ( innerScopes: innerBorrowScopes, context) {
286
- return extensions
287
- }
288
- // This is the outermost scope to be extended because gatherExtensions did not find an enclosing scope.
289
- return SingleInlineArray ( element: getOuterExtension ( owner: enclosingScope. parentValue, nestedScopes: innerScopes,
290
- context) )
291
+ return gatherBorrowExtension ( borrowedValue: beginBorrow. baseOperand!. value, innerScopes: & innerScopes, context)
292
+
291
293
case let . yield( yieldedValue) :
292
294
innerScopes. push ( self )
293
295
var extensions = SingleInlineArray < ScopeExtension > ( )
294
296
let applySite = yieldedValue. definingInstruction as! BeginApplyInst
295
297
for operand in applySite. parameterOperands {
296
- guard let dep = applySite. resultDependence ( on: operand) , dep == . scope else {
298
+ guard let dep = applySite. resultDependence ( on: operand) , dep. isScoped else {
297
299
continue
298
300
}
299
301
// Pass a copy of innerScopes without modifying this one.
300
302
extensions. append ( contentsOf: gatherOperandExtension ( on: operand, innerScopes: innerScopes, context) )
301
303
}
302
304
return extensions
305
+ case let . initialized( initializer) :
306
+ switch initializer {
307
+ case let . store( initializingStore: store, initialAddress: _) :
308
+ if let sb = store as? StoreBorrowInst {
309
+ return gatherBorrowExtension ( borrowedValue: sb. source, innerScopes: & innerScopes, context)
310
+ }
311
+ return nil
312
+ case . argument, . yield:
313
+ // TODO: extend indirectly yielded scopes.
314
+ return nil
315
+ }
303
316
default :
304
317
return nil
305
318
}
@@ -360,6 +373,23 @@ private extension LifetimeDependence.Scope {
360
373
}
361
374
return ScopeExtension ( owner: outerBeginAccess, nestedScopes: innerScopes, dependsOnArg: nil )
362
375
}
376
+
377
+ func gatherBorrowExtension( borrowedValue: Value ,
378
+ innerScopes: inout SingleInlineArray < LifetimeDependence . Scope > ,
379
+ _ context: FunctionPassContext )
380
+ -> SingleInlineArray < ScopeExtension > {
381
+
382
+ let enclosingScope = LifetimeDependence . Scope ( base: borrowedValue, context)
383
+ innerScopes. push ( self )
384
+ var innerBorrowScopes = innerScopes
385
+ innerBorrowScopes. push ( enclosingScope)
386
+ if let extensions = enclosingScope. gatherExtensions ( innerScopes: innerBorrowScopes, context) {
387
+ return extensions
388
+ }
389
+ // This is the outermost scope to be extended because gatherExtensions did not find an enclosing scope.
390
+ return SingleInlineArray ( element: getOuterExtension ( owner: enclosingScope. parentValue, nestedScopes: innerScopes,
391
+ context) )
392
+ }
363
393
}
364
394
365
395
/// Compute the range of the a scope owner. Nested scopes must stay within this range.
@@ -584,6 +614,17 @@ private extension LifetimeDependence.Scope {
584
614
case let . yield( yieldedValue) :
585
615
let beginApply = yieldedValue. definingInstruction as! BeginApplyInst
586
616
return beginApply. createEnd ( builder, context)
617
+ case let . initialized( initializer) :
618
+ switch initializer {
619
+ case let . store( initializingStore: store, initialAddress: _) :
620
+ if let sb = store as? StoreBorrowInst {
621
+ return builder. createEndBorrow ( of: sb)
622
+ }
623
+ return nil
624
+ case . argument, . yield:
625
+ // TODO: extend indirectly yielded scopes.
626
+ return nil
627
+ }
587
628
default :
588
629
return nil
589
630
}
0 commit comments