Skip to content

Commit 37dfabd

Browse files
authored
Merge pull request #60104 from meg-gupta/cherrypickossafix
[5.6] Fix OwnershipLiveRange for @owned values getting transformed to none values via switch_enum
2 parents 8e4c2c0 + b575e5a commit 37dfabd

File tree

2 files changed

+45
-2
lines changed

2 files changed

+45
-2
lines changed

lib/SILOptimizer/SemanticARC/OwnershipLiveRange.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,9 +124,14 @@ OwnershipLiveRange::OwnershipLiveRange(SILValue value)
124124
continue;
125125

126126
for (auto *succArg : succBlock->getSILPhiArguments()) {
127-
// If we have an any value, just continue.
128-
if (succArg->getOwnershipKind() == OwnershipKind::None)
127+
// Owned values can get transformed to None values, currently we bail
128+
// out computing OwnershipLiveRange in this case, because it can lead to
129+
// incorrect results in the presence of dead edges on the non-trivial
130+
// paths of switch_enum.
131+
if (succArg->getOwnershipKind() == OwnershipKind::None) {
132+
tmpUnknownConsumingUses.push_back(op);
129133
continue;
134+
}
130135

131136
// Otherwise add all users of this BBArg to the worklist to visit
132137
// recursively.

test/SILOptimizer/semantic-arc-opts-loadcopy-to-loadborrow.sil

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ sil @get_owned_obj : $@convention(thin) () -> @owned Builtin.NativeObject
2727
sil @unreachable_guaranteed_user : $@convention(thin) (@guaranteed Builtin.NativeObject) -> MyNever
2828
sil @inout_user : $@convention(thin) (@inout FakeOptional<NativeObjectPair>) -> ()
2929
sil @get_native_object : $@convention(thin) () -> @owned Builtin.NativeObject
30+
sil [ossa] @get_enum : $@convention(thin) () -> @owned EnumA
3031

3132
struct NativeObjectPair {
3233
var obj1 : Builtin.NativeObject
@@ -104,6 +105,18 @@ struct StructWithEnumWithIndirectCaseField {
104105
var field : EnumWithIndirectCase
105106
}
106107

108+
struct TrivialStruct {
109+
}
110+
111+
enum EnumA {
112+
case none
113+
case sometrivial(TrivialStruct)
114+
case somenontrivial(NonTrivialStruct)
115+
}
116+
117+
struct StructWithEnum {
118+
var val: EnumA
119+
}
107120
sil @get_fakeoptional_nativeobject : $@convention(thin) () -> @owned FakeOptional<Builtin.NativeObject>
108121
sil @black_hole : $@convention(thin) (@guaranteed Klass) -> ()
109122

@@ -1514,3 +1527,28 @@ bb0(%0 : $*NonTrivialStruct, %1 : $*NonTrivialStruct):
15141527
return %9999 : $()
15151528
}
15161529

1530+
// CHECK-LABEL: sil [ossa] @switch_enum_test :
1531+
// CHECK: load [copy]
1532+
// CHECK: } // end sil function 'switch_enum_test'
1533+
sil [ossa] @switch_enum_test : $@convention(thin) (@inout StructWithEnum) -> () {
1534+
bb0(%0 : $*StructWithEnum):
1535+
%1 = struct_element_addr %0 : $*StructWithEnum, #StructWithEnum.val
1536+
%2 = struct_element_addr %0 : $*StructWithEnum, #StructWithEnum.val
1537+
%3 = load [copy] %2 : $*EnumA
1538+
destroy_addr %1 : $*EnumA
1539+
switch_enum %3 : $EnumA, case #EnumA.sometrivial!enumelt: bb1, case #EnumA.somenontrivial!enumelt:bb2, case #EnumA.none!enumelt: bb3
1540+
1541+
bb1(%6 : $TrivialStruct):
1542+
%f = function_ref @get_enum : $@convention(thin) () -> @owned EnumA
1543+
%r = apply %f() : $@convention(thin) () -> @owned EnumA
1544+
%ele = struct_element_addr %0 : $*StructWithEnum, #StructWithEnum.val
1545+
store %r to [init] %ele :$*EnumA
1546+
%9999 = tuple()
1547+
return %9999 : $()
1548+
1549+
bb2(%7 : @owned $NonTrivialStruct):
1550+
unreachable
1551+
1552+
bb3:
1553+
unreachable
1554+
}

0 commit comments

Comments
 (0)