Skip to content

Commit e2c5404

Browse files
committed
[ownership] If we have an undef address, do not treat it as being owned. It is any.
1 parent 934f34d commit e2c5404

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed

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;

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+
}

0 commit comments

Comments
 (0)