Skip to content

Commit 5a59df7

Browse files
authored
Merge pull request #64866 from atrick/5.9-fix-mandatory-arc
[5.9] Fix MandatoryARCOpts tryJoiningCopyValueLiveRangeWithOperand.
2 parents fbe6805 + 7d10ee3 commit 5a59df7

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed

lib/SILOptimizer/SemanticARC/CopyValueOpts.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,12 @@ static bool tryJoinIfDestroyConsumingUseInSameBlock(
493493
}))
494494
return false;
495495
}
496+
// Check whether the uses considered immediately above are all effectively
497+
// instantaneous uses. Pointer escapes propagate values ways that may not be
498+
// discoverable.
499+
if (hasPointerEscape(operand)) {
500+
return false;
501+
}
496502

497503
// Ok, we now know that we can eliminate this value.
498504
LLVM_DEBUG(llvm::dbgs()

test/SILOptimizer/semantic-arc-opts-lifetime-joining.sil

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
// RUN: %target-sil-opt -module-name Swift -enable-sil-verify-all -semantic-arc-opts -sil-semantic-arc-peepholes-lifetime-joining %s | %FileCheck %s
2-
// REQUIRES: swift_stdlib_asserts
2+
//
3+
// Enabling specific ARC opts requires an asserts build.
4+
// REQUIRES: asserts
35

46
// NOTE: Some of our tests here depend on borrow elimination /not/ running!
57
// Please do not add it to clean up the IR like we did in
@@ -15,6 +17,13 @@ import Builtin
1517

1618
typealias AnyObject = Builtin.AnyObject
1719

20+
struct Bool {
21+
var value : Builtin.Int1
22+
}
23+
24+
sil @closureCapturesBool : $@convention(thin) (@guaranteed { var Bool }) -> ()
25+
sil @closureArgumentEscapes : $@convention(thin) (@owned @callee_guaranteed () -> ()) -> ()
26+
1827
enum MyNever {}
1928
enum FakeOptional<T> {
2029
case none
@@ -883,3 +892,34 @@ bb3(%result : @owned $FakeOptional<Builtin.NativeObject>):
883892
dealloc_stack %allocStack : $*Builtin.NativeObject
884893
return %result : $FakeOptional<Builtin.NativeObject>
885894
}
895+
896+
// Don't do this optimization:
897+
// Eliminate borrowed copy with useless lifetime:
898+
// %5 = copy_value %0 : ${ var Bool }
899+
//
900+
// CHECK: sil hidden [ossa] @testCapturedSingleDestroyCopy : $@convention(thin) () -> Bool {
901+
// CHECK: load [trivial] %{{.*}} : $*Bool
902+
// CHECK: destroy_value %0 : ${ var Bool }
903+
sil hidden [ossa] @testCapturedSingleDestroyCopy : $@convention(thin) () -> Bool {
904+
bb0:
905+
// var test = false
906+
%0 = alloc_box ${ var Bool }, var, name "test"
907+
%1 = project_box %0 : ${ var Bool }, 0
908+
%2 = integer_literal $Builtin.Int1, 0
909+
%3 = struct $Bool (%2 : $Builtin.Int1)
910+
store %3 to [trivial] %1 : $*Bool
911+
912+
// capture test in an escaping closure
913+
%5 = copy_value %0 : ${ var Bool }
914+
%6 = function_ref @closureCapturesBool : $@convention(thin) (@guaranteed { var Bool }) -> ()
915+
%7 = partial_apply [callee_guaranteed] %6(%5) : $@convention(thin) (@guaranteed { var Bool }) -> ()
916+
%8 = function_ref @closureArgumentEscapes : $@convention(thin) (@owned @callee_guaranteed () -> ()) -> ()
917+
%9 = apply %8(%7) : $@convention(thin) (@owned @callee_guaranteed () -> ()) -> ()
918+
919+
// return test
920+
%10 = begin_access [read] [dynamic] %1 : $*Bool
921+
%11 = load [trivial] %10 : $*Bool
922+
end_access %10 : $*Bool
923+
destroy_value %0 : ${ var Bool }
924+
return %11 : $Bool
925+
}

0 commit comments

Comments
 (0)