Skip to content

Commit 6277345

Browse files
authored
Merge pull request #25711 from gottesmm/pr-9073d6d3c8a1f3106ffd1b8355860ae58f149d8f
Re-commit harmless patches that could not have caused the verification failure (since we wouldn't run it).
2 parents e7c8e22 + 2dd87b5 commit 6277345

File tree

5 files changed

+102
-5
lines changed

5 files changed

+102
-5
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()

lib/SIL/SILUndef.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,15 @@
1616
using namespace swift;
1717

1818
static ValueOwnershipKind getOwnershipKindForUndef(SILType type, const SILFunction &f) {
19+
if (type.isAddress()) {
20+
// If we have an address only type and we are supposed to use
21+
// lowered addresses, return Owned. Otherwise addresses are any.
22+
if (type.isAddressOnly(f) && f.getConventions().useLoweredAddresses()) {
23+
return ValueOwnershipKind::Owned;
24+
}
25+
return ValueOwnershipKind::Any;
26+
}
27+
1928
if (type.isTrivial(f))
2029
return ValueOwnershipKind::Any;
2130
return ValueOwnershipKind::Owned;

lib/SILOptimizer/Mandatory/MarkUninitializedFixup.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ struct MarkUninitializedFixup : SILFunctionTransform {
119119

120120
// Then create the new mark_uninitialized and force all uses of the
121121
// project_box to go through the new mark_uninitialized.
122-
SILBuilder B(std::next(PBI->getIterator()));
122+
SILBuilderWithScope B(&*std::next(PBI->getIterator()));
123123
SILValue Undef = SILUndef::get(PBI->getType(), *PBI->getFunction());
124124
auto *NewMUI =
125125
B.createMarkUninitialized(PBI->getLoc(), Undef, MUI->getKind());

test/SIL/ownership-verifier/undef.sil

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// RUN: %target-sil-opt -enable-objc-interop -enable-sil-verify-all=0 -module-name Swift -ownership-dumper -o /dev/null %s | %FileCheck %s
2+
3+
// REQUIRES: asserts
4+
5+
sil_stage raw
6+
7+
import Builtin
8+
9+
class Klass {}
10+
11+
struct MyInt {
12+
var i: Builtin.Int32
13+
}
14+
15+
// Make sure that we handle undef in an appropriate way. Do to special handling
16+
// in SIL around undef, we test this using the ownership dumper for simplicity.
17+
18+
// CHECK-LABEL: *** Dumping Function: 'undef_addresses_have_any_ownership'
19+
// CHECK: Visiting: {{.*}}%0 = mark_uninitialized [var] undef : $*Klass
20+
// CHECK-NEXT: Operand Ownership Map:
21+
// CHECK-NEXT: Op #: 0
22+
// CHECK-NEXT: Ownership Map: -- OperandOwnershipKindMap --
23+
// CHECK-NEXT: unowned: Yes. Liveness: MustBeLive
24+
// CHECK-NEXT: owned: Yes. Liveness: MustBeLive
25+
// CHECK-NEXT: guaranteed: Yes. Liveness: MustBeLive
26+
// CHECK-NEXT: any: Yes. Liveness: MustBeLive
27+
// CHECK: Visiting: {{.*}}%1 = mark_uninitialized [var] undef : $Klass
28+
// CHECK-NEXT: Operand Ownership Map:
29+
// CHECK-NEXT: Op #: 0
30+
// CHECK-NEXT: Ownership Map: -- OperandOwnershipKindMap --
31+
// CHECK-NEXT: unowned: No.
32+
// CHECK-NEXT: owned: Yes. Liveness: MustBeInvalidated
33+
// CHECK-NEXT: guaranteed: No.
34+
// CHECK-NEXT: any: Yes. Liveness: MustBeLive
35+
// CHECK: Visiting: {{.*}}%3 = mark_uninitialized [var] undef : $*Builtin.Int32
36+
// CHECK-NEXT: Operand Ownership Map:
37+
// CHECK-NEXT: Op #: 0
38+
// CHECK-NEXT: Ownership Map: -- OperandOwnershipKindMap --
39+
// CHECK-NEXT: unowned: Yes. Liveness: MustBeLive
40+
// CHECK-NEXT: owned: Yes. Liveness: MustBeLive
41+
// CHECK-NEXT: guaranteed: Yes. Liveness: MustBeLive
42+
// CHECK-NEXT: any: Yes. Liveness: MustBeLive
43+
// CHECK: Visiting: {{.*}}%4 = struct $MyInt (undef : $Builtin.Int32)
44+
// CHECK-NEXT: Operand Ownership Map:
45+
// CHECK-NEXT: Op #: 0
46+
// CHECK-NEXT: Ownership Map: -- OperandOwnershipKindMap --
47+
// CHECK-NEXT: unowned: Yes. Liveness: MustBeLive
48+
// CHECK-NEXT: owned: Yes. Liveness: MustBeLive
49+
// CHECK-NEXT: guaranteed: Yes. Liveness: MustBeLive
50+
// CHECK-NEXT: any: Yes. Liveness: MustBeLive
51+
sil [ossa] @undef_addresses_have_any_ownership : $@convention(thin) () -> () {
52+
bb0:
53+
%0 = mark_uninitialized [var] undef : $*Klass
54+
%1 = mark_uninitialized [var] undef : $Klass
55+
destroy_value %1 : $Klass
56+
%2 = mark_uninitialized [var] undef : $*Builtin.Int32
57+
%3 = struct $MyInt(undef : $Builtin.Int32)
58+
%9999 = tuple()
59+
return %9999 : $()
60+
}

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)