Skip to content

Commit 9a67f2d

Browse files
committed
[ownership] Ignore non-consuming uses in dead end blocks when we are analyzing objects that are consumed once in the same block in which they are defined.
1 parent 934f34d commit 9a67f2d

File tree

2 files changed

+32
-4
lines changed

2 files changed

+32
-4
lines changed

lib/SIL/LinearLifetimeChecker.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -486,11 +486,12 @@ LinearLifetimeError swift::valueHasLinearLifetime(
486486
// have been detected by initializing our consuming uses. So we are done.
487487
if (consumingUses.size() == 1 &&
488488
consumingUses[0].getParent() == value->getParentBlock()) {
489-
// Check if any of our non consuming uses are not in the parent block. We
490-
// flag those as additional use after frees. Any in the same block, we would
491-
// have flagged.
489+
// Check if any of our non consuming uses are not in the parent block and
490+
// are reachable. We flag those as additional use after frees. Any in the
491+
// same block, we would have flagged.
492492
if (llvm::any_of(nonConsumingUses, [&](BranchPropagatedUser user) {
493-
return user.getParent() != value->getParentBlock();
493+
return user.getParent() != value->getParentBlock() &&
494+
!deBlocks.isDeadEnd(user.getParent());
494495
})) {
495496
state.error.handleUseAfterFree([&] {
496497
llvm::errs() << "Function: '" << value->getFunction()->getName()

test/SIL/ownership-verifier/use_verifier.sil

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,10 @@ sil @unowned_tuple_user : $@convention(thin) ((Builtin.Int32, Builtin.NativeObje
105105
sil @produce_owned_optional : $@convention(thin) () -> @owned Optional<Builtin.NativeObject>
106106
sil @guaranteed_nativeobject_user : $@convention(thin) (@guaranteed Builtin.NativeObject) -> ()
107107

108+
class UnsafeUnownedFieldKlass {
109+
unowned(safe) var x: @sil_unowned Builtin.NativeObject
110+
}
111+
108112
////////////////
109113
// Test Cases //
110114
////////////////
@@ -1156,3 +1160,26 @@ bb0:
11561160
%9999 = tuple()
11571161
return %9999 : $()
11581162
}
1163+
1164+
// Make sure that we do not false positive due to borrow users of %12 in an
1165+
// unreachable block.
1166+
sil [ossa] @test_unreachable_users : $@convention(method) (@guaranteed UnsafeUnownedFieldKlass) -> () {
1167+
bb0(%0 : @guaranteed $UnsafeUnownedFieldKlass):
1168+
%3 = ref_element_addr %0 : $UnsafeUnownedFieldKlass, #UnsafeUnownedFieldKlass.x
1169+
%4 = load_borrow %3 : $*@sil_unowned Builtin.NativeObject
1170+
%5 = copy_unowned_value %4 : $@sil_unowned Builtin.NativeObject
1171+
end_borrow %4 : $@sil_unowned Builtin.NativeObject
1172+
%12 = begin_borrow %5 : $Builtin.NativeObject
1173+
%func = function_ref @guaranteed_nativeobject_user : $@convention(thin) (@guaranteed Builtin.NativeObject) -> ()
1174+
apply %func(%12) : $@convention(thin) (@guaranteed Builtin.NativeObject) -> ()
1175+
end_borrow %12 : $Builtin.NativeObject
1176+
destroy_value %5 : $Builtin.NativeObject
1177+
%36 = tuple ()
1178+
return %36 : $()
1179+
1180+
bb1:
1181+
%func2 = function_ref @guaranteed_nativeobject_user : $@convention(thin) (@guaranteed Builtin.NativeObject) -> ()
1182+
apply %func2(%12) : $@convention(thin) (@guaranteed Builtin.NativeObject) -> ()
1183+
unreachable
1184+
}
1185+

0 commit comments

Comments
 (0)