Skip to content

Commit 03efaf4

Browse files
committed
Fix MarkDependenceInst.simplify()
Do not eliminate a mark_dependence on a begin_apply scope even though the token has a trivial type. Ideally, token would have a non-trivial Builtin type to avoid special cases.
1 parent 7a29d9d commit 03efaf4

File tree

2 files changed

+40
-3
lines changed

2 files changed

+40
-3
lines changed

SwiftCompilerSources/Sources/Optimizer/InstructionSimplification/SimplifyMarkDependence.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,13 @@ extension MarkDependenceAddrInst : OnoneSimplifiable, SILCombineSimplifiable {
4242

4343
private extension MarkDependenceInstruction {
4444
var isRedundant: Bool {
45-
if base.type.isObject && base.type.isTrivial(in: base.parentFunction) {
45+
if base.type.isObject && base.type.isTrivial(in: base.parentFunction)
46+
&& !(base.definingInstruction is BeginApplyInst) {
4647
// Sometimes due to specialization/builtins, we can get a mark_dependence whose base is a trivial
4748
// typed object. Trivial values live forever. Therefore the mark_dependence does not have a meaning.
49+
// begin_apply is a special case. A dependency on the token is limited to the coroutine scope (ideally, the token
50+
// would have a non-trivial type like $Builtin.Token).
51+
//
4852
// Note: the mark_dependence is still needed for lifetime diagnostics. So it's important that this
4953
// simplification does not run before the lifetime diagnostic pass.
5054
return true

test/SILOptimizer/simplify_mark_dependence.sil

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
// RUN: %target-sil-opt %s -onone-simplification -simplify-instruction=mark_dependence -enable-experimental-feature LifetimeDependence | %FileCheck %s
1+
// RUN: %target-sil-opt %s -onone-simplification -simplify-instruction=mark_dependence -enable-experimental-feature Lifetimes | %FileCheck %s
22

3-
// REQUIRES: swift_feature_LifetimeDependence
3+
// REQUIRES: swift_feature_Lifetimes
44

55
import Swift
66
import Builtin
@@ -14,10 +14,20 @@ struct S {
1414

1515
struct NE: ~Escapable {}
1616

17+
struct NEHolder : ~Escapable {
18+
var ne: NE {
19+
@_lifetime(borrow self)
20+
get
21+
}
22+
}
23+
1724
struct S2: ~Escapable {
1825
var a: NE
1926
}
2027

28+
sil @read_ne : $@yield_once @convention(method) (@guaranteed NEHolder) -> @lifetime(borrow 0) @yields @guaranteed NE
29+
sil @use_ne : $@convention(thin) (@guaranteed NE) -> ()
30+
2131
// CHECK-LABEL: sil [ossa] @mark_dependence_trivial_base :
2232
// CHECK: %2 = copy_value %0
2333
// CHECK-NEXT: return %2
@@ -155,3 +165,26 @@ bb0(%0 : $*Builtin.Int64, %1 : $B):
155165
return %5
156166
}
157167

168+
// CHECK-LABEL: sil hidden [ossa] @testChainedDependence : $@convention(thin) (@guaranteed NEHolder) -> () {
169+
// CHECK: bb0(%0 : @noImplicitCopy @guaranteed $NEHolder):
170+
// CHECK: ([[YIELD:%[0-9]+]], [[TOKEN:%[0-9]+]]) = begin_apply %2(%1) : $@yield_once @convention(method) (@guaranteed NEHolder) -> @lifetime(borrow 0) @yields @guaranteed NE
171+
// CHECK: [[MD1:%[0-9]+]] = mark_dependence [nonescaping] [[YIELD]] on [[TOKEN]]
172+
// CHECK: mark_dependence [nonescaping] [[MD1]] on
173+
// CHECK-LABEL: } // end sil function 'testChainedDependence'
174+
sil hidden [ossa] @testChainedDependence : $@convention(thin) (@guaranteed NEHolder) -> () {
175+
bb0(%0 : @noImplicitCopy @guaranteed $NEHolder):
176+
%2 = begin_borrow %0
177+
%3 = function_ref @read_ne : $@yield_once @convention(method) (@guaranteed NEHolder) -> @lifetime(borrow 0) @yields @guaranteed NE
178+
(%4, %5) = begin_apply %3(%2) : $@yield_once @convention(method) (@guaranteed NEHolder) -> @lifetime(borrow 0) @yields @guaranteed NE
179+
%6 = mark_dependence [nonescaping] %4 on %5
180+
%7 = mark_dependence [nonescaping] %6 on %2
181+
%8 = copy_value %7
182+
%9 = move_value [var_decl] %8
183+
%11 = function_ref @use_ne : $@convention(thin) (@guaranteed NE) -> ()
184+
%12 = apply %11(%9) : $@convention(thin) (@guaranteed NE) -> ()
185+
destroy_value %9
186+
%14 = end_apply %5 as $()
187+
end_borrow %2
188+
%16 = tuple ()
189+
return %16
190+
}

0 commit comments

Comments
 (0)