Skip to content

Commit 85a580a

Browse files
committed
Fix ossa borrow completion
end_borrow was not considered as a lifetime ending use, leading to insertion of redundant end_borrows at boundaries.
1 parent e355fd8 commit 85a580a

File tree

2 files changed

+59
-1
lines changed

2 files changed

+59
-1
lines changed

include/swift/SIL/OwnershipUseVisitor.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -388,8 +388,9 @@ bool OwnershipUseVisitor<Impl>::visitGuaranteedUse(Operand *use) {
388388
case OperandOwnership::ForwardingUnowned:
389389
case OperandOwnership::UnownedInstantaneousUse:
390390
case OperandOwnership::BitwiseEscape:
391-
case OperandOwnership::EndBorrow:
392391
return handleUsePoint(use, UseLifetimeConstraint::NonLifetimeEnding);
392+
case OperandOwnership::EndBorrow:
393+
return handleUsePoint(use, UseLifetimeConstraint::LifetimeEnding);
393394

394395
case OperandOwnership::Reborrow:
395396
if (!asImpl().handleOuterReborrow(use))

test/SILOptimizer/ossa_lifetime_completion.sil

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ import Builtin
66

77
class C {}
88

9+
public enum FakeOptional<T> {
10+
case none
11+
case some(T)
12+
}
13+
914
// CHECK-LABEL: begin running test 1 of 1 on eagerConsumneOwnedArg: ossa-lifetime-completion with: @argument
1015
// CHECK-LABEL: OSSA lifetime completion: %0 = argument of bb0 : $C
1116
// CHECK: sil [ossa] @eagerConsumneOwnedArg : $@convention(thin) (@owned C) -> () {
@@ -45,3 +50,55 @@ bb3:
4550
%r = tuple ()
4651
return %r : $()
4752
}
53+
54+
// CHECK-LABEL: sil [ossa] @borrowTest : $@convention(method) (@owned C) -> () {
55+
// CHECK: bb1:
56+
// CHECK-NEXT: end_borrow
57+
// CHECK-NEXT: br bb3
58+
// CHECK-LABEL: } // end sil function 'borrowTest'
59+
sil [ossa] @borrowTest : $@convention(method) (@owned C) -> () {
60+
bb0(%0 : @owned $C):
61+
test_specification "ossa-lifetime-completion @instruction[0]"
62+
%borrow = begin_borrow %0 : $C
63+
cond_br undef, bb1, bb2
64+
65+
bb1:
66+
end_borrow %borrow : $C
67+
br bb3
68+
69+
bb2:
70+
end_borrow %borrow : $C
71+
br bb3
72+
73+
bb3:
74+
destroy_value %0 : $C
75+
%r = tuple ()
76+
return %r : $()
77+
}
78+
79+
// CHECK-LABEL: sil [ossa] @enumTest : $@convention(method) (@guaranteed FakeOptional<C>) -> () {
80+
// CHECK: bb2
81+
// CHECK: destroy_value
82+
// CHECK: br bb3
83+
// CHECK-LABEL: } // end sil function 'enumTest'
84+
sil [ossa] @enumTest : $@convention(method) (@guaranteed FakeOptional<C>) -> () {
85+
bb0(%0 : @guaranteed $FakeOptional<C>):
86+
test_specification "ossa-lifetime-completion @instruction[0]"
87+
%copy = copy_value %0 : $FakeOptional<C>
88+
%borrow = begin_borrow %copy : $FakeOptional<C>
89+
switch_enum %borrow : $FakeOptional<C>, case #FakeOptional.some!enumelt: bb1, case #FakeOptional.none!enumelt: bb2
90+
91+
bb1(%some : @guaranteed $C):
92+
end_borrow %borrow : $FakeOptional<C>
93+
destroy_value %copy : $FakeOptional<C>
94+
br bb3
95+
96+
bb2:
97+
end_borrow %borrow : $FakeOptional<C>
98+
br bb3
99+
100+
bb3:
101+
%r = tuple ()
102+
return %r : $()
103+
}
104+

0 commit comments

Comments
 (0)