Skip to content

[6.2] Fix LifetimeDependenceScopeFixup: extend scopes enclosing coroutines #81208

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -422,9 +422,6 @@ extension ScopeExtension {
mutating func gatherYieldExtension(_ beginApply: BeginApplyInst) {
// Create a separate ScopeExtension for each operand that the yielded value depends on.
for operand in beginApply.parameterOperands {
guard let dep = beginApply.resultDependence(on: operand), dep.isScoped else {
continue
}
gatherExtensions(valueOrAddress: operand.value)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -300,3 +300,30 @@ func testReassignment(b: UnsafeMutableRawBufferPointer) {
sub = span.update()
use(sub)
}


// Coroutine nested in a mutable access scope.
//
// rdar://150275147 (Invalid SIL after lifetime dependence fixup involving coroutines)
struct ArrayWrapper {
private var a: [Int]

var array: [Int] {
_read {
yield a
}
_modify {
yield &a
}
}
}

@_silgen_name("gms")
func getMutableSpan(_: inout [Int]) -> MutableSpan<Int>

func testWrite(_ w: inout ArrayWrapper) {
var span = getMutableSpan(&w.array)
for i in span.indices {
span[i] = 0
}
}
50 changes: 50 additions & 0 deletions test/SILOptimizer/lifetime_dependence/scope_fixup.sil
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ sil @useRawSpan : $@convention(thin) (@guaranteed RawSpan) -> ()
sil @initHolder : $@convention(method) (@thin Holder.Type) -> @owned Holder
sil @readAccess : $@yield_once @convention(method) (@guaranteed Holder) -> @lifetime(borrow 0) @yields @guaranteed NE

sil @yieldInoutHolder : $@yield_once @convention(method) (@inout Holder) -> @yields @inout Holder
sil @yieldInoutNE : $@yield_once @convention(method) (@inout Holder) -> @lifetime(borrow 0) @owned NE

// NCContainer.wrapper._read:
// var wrapper: Wrapper {
// _read {
Expand Down Expand Up @@ -442,6 +445,53 @@ bb0(%0 : @owned $B):
return %24
}

// =============================================================================
// Coroutine extension
// =============================================================================

// Sink the inner end_apply and the outer end_access.
//
// CHECK-LABEL: sil hidden [ossa] @testCoroutineInsideAccess : $@convention(thin) (@inout Holder) -> () {
// CHECK: bb0(%0 : $*Holder):
// CHECK: [[BORROW:%[0-9]+]] = begin_borrow [lexical] [var_decl]
// CHECK: [[ACCESS:%[0-9]+]] = begin_access [modify] [unknown] %0
// CHECK: ({{.*}}, [[TOKEN:%[0-9]+]]) = begin_apply %{{.*}}([[ACCESS]]) : $@yield_once @convention(method) (@inout Holder) -> @yields @inout Holder
// CHECK: apply
// CHECK: [[MD:%[0-9]+]] = mark_dependence [unresolved] %10 on %7
// CHECK: apply
// CHECK: end_borrow
// CHECK: end_access
// CHECK: end_apply [[TOKEN]] as $()
// CHECK: end_access [[ACCESS]]
// CHECK: end_borrow [[BORROW]]
// CHECK-LABEL: } // end sil function 'testCoroutineInsideAccess'
sil hidden [ossa] @testCoroutineInsideAccess : $@convention(thin) (@inout Holder) -> () {// %0 "w"
bb0(%0 : $*Holder):
debug_value %0, var, name "w", argno 1, expr op_deref
%2 = alloc_box ${ var NE }, var, name "span"
%3 = begin_borrow [lexical] [var_decl] %2
%4 = project_box %3, 0
%5 = begin_access [modify] [unknown] %0
%6 = function_ref @yieldInoutHolder : $@yield_once @convention(method) (@inout Holder) -> @yields @inout Holder
(%7, %8) = begin_apply %6(%5) : $@yield_once @convention(method) (@inout Holder) -> @yields @inout Holder
%9 = function_ref @getOwnedNEFromInout : $@convention(thin) (@inout Holder) -> @lifetime(borrow 0) @owned NE
%10 = apply %9(%7) : $@convention(thin) (@inout Holder) -> @lifetime(borrow 0) @owned NE
%11 = mark_dependence [unresolved] %10 on %7
%12 = end_apply %8 as $()
end_access %5
store %11 to [init] %4
%15 = begin_access [read] [unknown] %4
%16 = load_borrow [unchecked] %15
%17 = function_ref @useNE : $@convention(thin) (@guaranteed NE) -> ()
%18 = apply %17(%16) : $@convention(thin) (@guaranteed NE) -> ()
end_borrow %16
end_access %15
end_borrow %3
destroy_value %2
%99 = tuple ()
return %99
}

// =============================================================================
// Return value extension
// =============================================================================
Expand Down