Skip to content

Commit faedffa

Browse files
authored
Merge pull request #38148 from atrick/fix-borrow-branch
Fix BorrowingOperand for branches.
2 parents 93a0c52 + 87ba9cc commit faedffa

File tree

3 files changed

+39
-2
lines changed

3 files changed

+39
-2
lines changed

include/swift/SIL/OwnershipUtils.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,12 @@ struct BorrowingOperand {
263263
BorrowingOperandKind kind;
264264

265265
BorrowingOperand(Operand *op)
266-
: op(op), kind(BorrowingOperandKind::get(op->getUser()->getKind())) {}
266+
: op(op), kind(BorrowingOperandKind::get(op->getUser()->getKind())) {
267+
if (kind == BorrowingOperandKind::Branch
268+
&& op->get().getOwnershipKind() != OwnershipKind::Guaranteed) {
269+
kind = BorrowingOperandKind::Invalid;
270+
}
271+
}
267272
BorrowingOperand(const BorrowingOperand &other)
268273
: op(other.op), kind(other.kind) {}
269274
BorrowingOperand &operator=(const BorrowingOperand &other) {
@@ -288,6 +293,11 @@ struct BorrowingOperand {
288293
auto kind = BorrowingOperandKind::get(user->getKind());
289294
if (!kind)
290295
return {nullptr, kind};
296+
297+
if (kind == BorrowingOperandKind::Branch
298+
&& op->get().getOwnershipKind() != OwnershipKind::Guaranteed) {
299+
return {nullptr, BorrowingOperandKind::Invalid};
300+
}
291301
return {op, kind};
292302
}
293303

lib/SIL/Verifier/LinearLifetimeChecker.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,8 +314,9 @@ void State::checkForSameBlockUseAfterFree(Operand *consumingUse,
314314
}) == userBlock->end()) {
315315
continue;
316316
}
317-
} else if (auto borrowingOperand = BorrowingOperand::get(consumingUse)) {
317+
} else if (auto borrowingOperand = BorrowingOperand::get(nonConsumingUse)) {
318318
assert(borrowingOperand.isReborrow());
319+
// a reborrow is expected to be consumed by the same phi.
319320
continue;
320321
}
321322

test/SIL/ownership-verifier/borrow_scope_introducing_operands_positive.sil

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77

88
import Builtin
99

10+
class C {}
11+
12+
sil @getOwnedC : $@convention(thin) () -> (@owned C)
13+
1014
sil [ossa] @coroutine_callee : $@yield_once (@guaranteed Builtin.NativeObject) -> () {
1115
bb0(%0 : @guaranteed $Builtin.NativeObject):
1216
yield (), resume bb1, unwind bb2
@@ -172,3 +176,25 @@ bb3:
172176
%r = tuple ()
173177
return %r : $()
174178
}
179+
180+
// Test a reborrow on the same branch as the consume.
181+
sil [ossa] @testReborrow : $@convention(thin) (@owned C) -> () {
182+
bb0(%0 : @owned $C):
183+
cond_br undef, bb1, bb2
184+
bb1:
185+
destroy_value %0 : $C
186+
%f = function_ref @getOwnedC : $@convention(thin) () -> (@owned C)
187+
%owned1 = apply %f() : $@convention(thin) () -> (@owned C)
188+
%copy1 = copy_value %owned1 : $C
189+
%borrow1 = begin_borrow %copy1 : $C
190+
destroy_value %owned1 : $C
191+
br bb3(%borrow1 : $C, %copy1 : $C)
192+
bb2:
193+
%borrow2 = begin_borrow %0 : $C
194+
br bb3(%borrow2 : $C, %0 : $C)
195+
bb3(%borrow3 : @guaranteed $C, %copy3 : @owned $C):
196+
end_borrow %borrow3 : $C
197+
destroy_value %copy3 : $C
198+
%result = tuple ()
199+
return %result : $()
200+
}

0 commit comments

Comments
 (0)