Skip to content

Commit ae735f3

Browse files
committed
Fix SILCombine of MarkDependenceInst.
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. (only relevant on 6.2)
1 parent 25c2359 commit ae735f3

File tree

2 files changed

+47
-5
lines changed

2 files changed

+47
-5
lines changed

lib/SILOptimizer/SILCombiner/SILCombinerMiscVisitors.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1699,11 +1699,16 @@ static SILInstruction *combineMarkDependenceBaseInst(
16991699
{
17001700
SILType baseType = mdi->getBase()->getType();
17011701
if (baseType.getObjectType().isTrivial(*mdi->getFunction())) {
1702-
if (auto mdValue = dyn_cast<MarkDependenceInst>(mdi)) {
1703-
auto &valOper = mdi->getAllOperands()[MarkDependenceInst::Dependent];
1704-
mdValue->replaceAllUsesWith(valOper.get());
1702+
// begin_apply is a special case. A dependency on the token is limited
1703+
// to the coroutine scope (ideally, the token
1704+
// would have a non-trivial type like $Builtin.Token).
1705+
if (!isa_and_nonnull<BeginApplyInst>(mdi->getBase()->getDefiningInstruction())) {
1706+
if (auto mdValue = dyn_cast<MarkDependenceInst>(mdi)) {
1707+
auto &valOper = mdi->getAllOperands()[MarkDependenceInst::Dependent];
1708+
mdValue->replaceAllUsesWith(valOper.get());
1709+
}
1710+
return C->eraseInstFromFunction(*mdi);
17051711
}
1706-
return C->eraseInstFromFunction(*mdi);
17071712
}
17081713
}
17091714
return nullptr;

test/SILOptimizer/sil_combine_apply.sil

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
// RUN: %target-sil-opt -sil-print-types -enable-sil-verify-all %s -sil-combine -sil-combine-disable-alloc-stack-opts | %FileCheck %s
1+
// RUN: %target-sil-opt -sil-print-types -enable-sil-verify-all %s -sil-combine -sil-combine-disable-alloc-stack-opts -enable-experimental-feature Lifetimes | %FileCheck %s
22

33
// REQUIRES: swift_in_compiler
4+
// REQUIRES: swift_feature_Lifetimes
45

56
import Swift
67
import Builtin
@@ -27,6 +28,18 @@ protocol FakeProtocol {
2728
func requirement()
2829
}
2930

31+
struct NE: ~Escapable {}
32+
33+
struct NEHolder : ~Escapable {
34+
var ne: NE {
35+
@_lifetime(borrow self)
36+
get
37+
}
38+
}
39+
40+
sil @read_ne : $@yield_once @convention(method) (@guaranteed NEHolder) -> @lifetime(borrow 0) @yields @guaranteed NE
41+
sil @use_ne : $@convention(thin) (@guaranteed NE) -> ()
42+
3043
/////////////////////////////////
3144
// Tests for SILCombinerApply. //
3245
/////////////////////////////////
@@ -1019,3 +1032,27 @@ bb3:
10191032
%rv = tuple()
10201033
return %rv : $()
10211034
}
1035+
1036+
// CHECK-LABEL: sil hidden [ossa] @testChainedDependence : $@convention(thin) (@guaranteed NEHolder) -> () {
1037+
// CHECK: bb0(%0 : @noImplicitCopy @guaranteed $NEHolder):
1038+
// CHECK: ([[YIELD:%[0-9]+]], [[TOKEN:%[0-9]+]]) = begin_apply %{{.*}}(%{{.*}}) : $@yield_once @convention(method) (@guaranteed NEHolder) -> @lifetime(borrow 0) @yields @guaranteed NE
1039+
// CHECK: [[MD1:%[0-9]+]] = mark_dependence [nonescaping] [[YIELD]] : $NE on [[TOKEN]]
1040+
// CHECK: mark_dependence [nonescaping] [[MD1]]
1041+
// CHECK-LABEL: } // end sil function 'testChainedDependence'
1042+
sil hidden [ossa] @testChainedDependence : $@convention(thin) (@guaranteed NEHolder) -> () {
1043+
bb0(%0 : @noImplicitCopy @guaranteed $NEHolder):
1044+
%2 = begin_borrow %0
1045+
%3 = function_ref @read_ne : $@yield_once @convention(method) (@guaranteed NEHolder) -> @lifetime(borrow 0) @yields @guaranteed NE
1046+
(%4, %5) = begin_apply %3(%2) : $@yield_once @convention(method) (@guaranteed NEHolder) -> @lifetime(borrow 0) @yields @guaranteed NE
1047+
%6 = mark_dependence [nonescaping] %4 on %5
1048+
%7 = mark_dependence [nonescaping] %6 on %2
1049+
%8 = copy_value %7
1050+
%9 = move_value [var_decl] %8
1051+
%11 = function_ref @use_ne : $@convention(thin) (@guaranteed NE) -> ()
1052+
%12 = apply %11(%9) : $@convention(thin) (@guaranteed NE) -> ()
1053+
destroy_value %9
1054+
%14 = end_apply %5 as $()
1055+
end_borrow %2
1056+
%16 = tuple ()
1057+
return %16
1058+
}

0 commit comments

Comments
 (0)