Skip to content

Commit ade2ed6

Browse files
committed
LifetimeDependenceInsertion: do not emit inherited dependencies.
These were always redundant. And there is no way to emit them correctly for valid OSA when the original dependence scope is in the caller. NFC without: -enable-experimental-feature NonescapableTypes -enable-lifetime-dependence-diagnostics
1 parent b4a5c5c commit ade2ed6

File tree

2 files changed

+52
-11
lines changed

2 files changed

+52
-11
lines changed

SwiftCompilerSources/Sources/Optimizer/FunctionPasses/LifetimeDependenceInsertion.swift

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,14 +102,16 @@ extension LifetimeDependentApply {
102102
}
103103
}
104104

105-
/// Replace the each dependent apply result with a chain of
106-
/// mark_dependence [nonescaping] instructions; one for each base.
105+
/// If the result of this apply depends on the scope of one or more
106+
/// arguments, then insert a mark_dependence [unresolved] from the
107+
/// result on each argument so that the result is recognized as a
108+
/// dependent value within each scope.
107109
private func insertDependencies(for apply: LifetimeDependentApply,
108110
_ context: FunctionPassContext ) {
109111
precondition(apply.applySite.results.count > 0,
110112
"a lifetime-dependent instruction must have at least one result")
111113

112-
let bases = recursivelyFindDependenceBases(of: apply, context)
114+
let bases = findDependenceBases(of: apply, context)
113115
let builder = Builder(after: apply.applySite, context)
114116
for dependentValue in apply.applySite.resultOrYields {
115117
insertMarkDependencies(value: dependentValue, initializer: nil,
@@ -138,6 +140,31 @@ private func insertDependencies(for apply: LifetimeDependentApply,
138140
}
139141
}
140142

143+
private func findDependenceBases(of apply: LifetimeDependentApply,
144+
_ context: FunctionPassContext)
145+
-> [Value] {
146+
log("Creating dependencies for \(apply.applySite)")
147+
var bases: [Value] = []
148+
for lifetimeArg in apply.getLifetimeArguments() {
149+
switch lifetimeArg.convention {
150+
case .inherit:
151+
continue
152+
case .scope:
153+
// Create a new dependence on the apply's access to the argument.
154+
for varIntoducer in gatherVariableIntroducers(for: lifetimeArg.value,
155+
context) {
156+
if let scope =
157+
LifetimeDependence.Scope(base: varIntoducer, context) {
158+
log("Scoped lifetime from \(lifetimeArg.value)")
159+
log(" scope: \(scope)")
160+
bases.append(scope.parentValue)
161+
}
162+
}
163+
}
164+
}
165+
return bases
166+
}
167+
141168
private func insertMarkDependencies(value: Value, initializer: Instruction?,
142169
bases: [Value], builder: Builder,
143170
_ context: FunctionPassContext) {
@@ -155,6 +182,7 @@ private func insertMarkDependencies(value: Value, initializer: Instruction?,
155182
}
156183
}
157184

185+
/*
158186
/// Return base values that this return value depends on.
159187
///
160188
/// For lifetime copies, walk up the dependence chain to find the
@@ -212,3 +240,4 @@ private func recursivelyUpdate(scope: LifetimeDependence.Scope,
212240
}
213241
return scope
214242
}
243+
*/

test/SILOptimizer/lifetime_dependence_diagnostics.swift

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,26 @@ func bv_copy(_ bv: borrowing BV) -> _copy(bv) BV {
1818
copy bv
1919
}
2020

21-
// Diagnostics resolves mark_dependence [nonescaping].
21+
// No mark_dependence is needed for a inherited scope.
2222
//
23-
// CHECK-LABEL: sil hidden @$s4test14bv_borrow_copy0B0AA2BVVAE_tF : $@convention(thin) (@guaranteed BV) -> _scope(1) @owned BV {
24-
// CHECK: bb0(%0 : @noImplicitCopy $BV):
25-
// CHECK: [[R:%.*]] = apply %{{.*}}(%0) : $@convention(thin) (@guaranteed BV) -> _inherit(1) @owned BV
26-
// CHECK: [[MD:%.*]] = mark_dependence [nonescaping] [[R]] : $BV on %0 : $BV
27-
// CHECK: return [[MD]] : $BV
28-
// CHECK-LABEL: } // end sil function '$s4test14bv_borrow_copy0B0AA2BVVAE_tF'
29-
func bv_borrow_copy(bv: borrowing BV) -> _borrow(bv) BV {
23+
// CHECK-LABEL: sil hidden @$s4test14bv_borrow_copyyAA2BVVADF : $@convention(thin) (@guaranteed BV) -> _scope(1) @owned BV {
24+
// CHECK: bb0(%0 : @noImplicitCopy $BV):
25+
// CHECK: apply %{{.*}}(%0) : $@convention(thin) (@guaranteed BV) -> _inherit(1) @owned BV // user: %4
26+
// CHECK-NEXT: return %3 : $BV
27+
// CHECK-LABEL: } // end sil function '$s4test14bv_borrow_copyyAA2BVVADF'
28+
func bv_borrow_copy(_ bv: borrowing BV) -> _borrow(bv) BV {
3029
bv_copy(bv)
3130
}
31+
32+
// The mark_dependence for the borrow scope should be marked
33+
// [nonescaping] after diagnostics.
34+
//
35+
// CHECK-LABEL: sil hidden @$s4test010bv_borrow_C00B0AA2BVVAE_tF : $@convention(thin) (@guaranteed BV) -> _scope(1) @owned BV {
36+
// CHECK: bb0(%0 : @noImplicitCopy $BV):
37+
// CHECK: [[R:%.*]] = apply %{{.*}}(%0) : $@convention(thin) (@guaranteed BV) -> _scope(1) @owned BV
38+
// CHECK: %{{.*}} = mark_dependence [nonescaping] [[R]] : $BV on %0 : $BV
39+
// CHECK-NEXT: return %{{.*}} : $BV
40+
// CHECK-LABEL: } // end sil function '$s4test010bv_borrow_C00B0AA2BVVAE_tF'
41+
func bv_borrow_borrow(bv: borrowing BV) -> _borrow(bv) BV {
42+
bv_borrow_copy(bv)
43+
}

0 commit comments

Comments
 (0)