|
| 1 | +// RUN: %target-sil-opt -module-name Swift -enable-sil-verify-all -semantic-arc-opts -sil-semantic-arc-peepholes-ownership-conversion-elim -sil-semantic-arc-peepholes-lifetime-joining %s | %FileCheck %s |
| 2 | + |
| 3 | +sil_stage canonical |
| 4 | + |
| 5 | +import Builtin |
| 6 | + |
| 7 | +////////////////// |
| 8 | +// Declarations // |
| 9 | +////////////////// |
| 10 | + |
| 11 | +typealias AnyObject = Builtin.AnyObject |
| 12 | + |
| 13 | +enum MyNever {} |
| 14 | +enum FakeOptional<T> { |
| 15 | +case none |
| 16 | +case some(T) |
| 17 | +} |
| 18 | + |
| 19 | +sil [ossa] @guaranteed_user : $@convention(thin) (@guaranteed Builtin.NativeObject) -> () |
| 20 | +sil [ossa] @owned_user : $@convention(thin) (@owned Builtin.NativeObject) -> () |
| 21 | +sil [ossa] @unowned_user : $@convention(thin) (Builtin.NativeObject) -> () |
| 22 | +sil [ossa] @get_owned_obj : $@convention(thin) () -> @owned Builtin.NativeObject |
| 23 | +sil [ossa] @unreachable_guaranteed_user : $@convention(thin) (@guaranteed Builtin.NativeObject) -> MyNever |
| 24 | +sil [ossa] @inout_user : $@convention(thin) (@inout FakeOptional<NativeObjectPair>) -> () |
| 25 | +sil [ossa] @get_native_object : $@convention(thin) () -> @owned Builtin.NativeObject |
| 26 | + |
| 27 | +struct NativeObjectPair { |
| 28 | + var obj1 : Builtin.NativeObject |
| 29 | + var obj2 : Builtin.NativeObject |
| 30 | +} |
| 31 | + |
| 32 | +sil [ossa] @get_object_pair : $@convention(thin) () -> @owned NativeObjectPair |
| 33 | + |
| 34 | +struct FakeOptionalNativeObjectPairPair { |
| 35 | + var pair1 : FakeOptional<NativeObjectPair> |
| 36 | + var pair2 : FakeOptional<NativeObjectPair> |
| 37 | +} |
| 38 | +sil [ossa] @inout_user2 : $@convention(thin) (@inout FakeOptionalNativeObjectPairPair) -> () |
| 39 | + |
| 40 | +sil [ossa] @get_nativeobject_pair : $@convention(thin) () -> @owned NativeObjectPair |
| 41 | +sil [ossa] @consume_nativeobject_pair : $@convention(thin) (@owned NativeObjectPair) -> () |
| 42 | + |
| 43 | +protocol MyFakeAnyObject : Klass { |
| 44 | + func myFakeMethod() |
| 45 | +} |
| 46 | + |
| 47 | +final class Klass { |
| 48 | + var base: Klass |
| 49 | + let baseLet: Klass |
| 50 | +} |
| 51 | + |
| 52 | +extension Klass : MyFakeAnyObject { |
| 53 | + func myFakeMethod() |
| 54 | +} |
| 55 | +sil [ossa] @guaranteed_klass_user : $@convention(thin) (@guaranteed Klass) -> () |
| 56 | +sil [ossa] @unowned_klass_user : $@convention(thin) (Klass) -> () |
| 57 | +sil [ossa] @guaranteed_fakeoptional_klass_user : $@convention(thin) (@guaranteed FakeOptional<Klass>) -> () |
| 58 | +sil [ossa] @guaranteed_fakeoptional_classlet_user : $@convention(thin) (@guaranteed FakeOptional<ClassLet>) -> () |
| 59 | + |
| 60 | +struct MyInt { |
| 61 | + var value: Builtin.Int32 |
| 62 | +} |
| 63 | + |
| 64 | +struct StructWithDataAndOwner { |
| 65 | + var data : Builtin.Int32 |
| 66 | + var owner : Klass |
| 67 | +} |
| 68 | + |
| 69 | +struct StructMemberTest { |
| 70 | + var c : Klass |
| 71 | + var s : StructWithDataAndOwner |
| 72 | + var t : (Builtin.Int32, StructWithDataAndOwner) |
| 73 | +} |
| 74 | + |
| 75 | +class ClassLet { |
| 76 | + @_hasStorage let aLet: Klass |
| 77 | + @_hasStorage var aVar: Klass |
| 78 | + @_hasStorage let aLetTuple: (Klass, Klass) |
| 79 | + @_hasStorage let anOptionalLet: FakeOptional<Klass> |
| 80 | + |
| 81 | + @_hasStorage let anotherLet: ClassLet |
| 82 | +} |
| 83 | + |
| 84 | +class SubclassLet: ClassLet {} |
| 85 | + |
| 86 | +sil_global [let] @a_let_global : $Klass |
| 87 | +sil_global @a_var_global : $Klass |
| 88 | + |
| 89 | +enum EnumWithIndirectCase { |
| 90 | +case first |
| 91 | +indirect case second(Builtin.NativeObject) |
| 92 | +} |
| 93 | + |
| 94 | +struct StructWithEnumWithIndirectCaseField { |
| 95 | + var i: Builtin.Int23 |
| 96 | + var field : EnumWithIndirectCase |
| 97 | +} |
| 98 | + |
| 99 | +sil [ossa] @get_fakeoptional_nativeobject : $@convention(thin) () -> @owned FakeOptional<Builtin.NativeObject> |
| 100 | + |
| 101 | +struct NativeObjectWrapper { |
| 102 | + var innerWrapper : Builtin.NativeObject |
| 103 | +} |
| 104 | + |
| 105 | +sil @owned_user_object_pair : $@convention(thin) (@owned NativeObjectPair) -> () |
| 106 | + |
| 107 | +/////////// |
| 108 | +// Tests // |
| 109 | +/////////// |
| 110 | + |
| 111 | +// CHECK-LABEL: sil [ossa] @guaranteed_to_unowned_positive_1 : $@convention(thin) (@owned Klass) -> () { |
| 112 | +// CHECK-NOT: unchecked_ownership_conversion |
| 113 | +// CHECK: } // end sil function 'guaranteed_to_unowned_positive_1' |
| 114 | +sil [ossa] @guaranteed_to_unowned_positive_1 : $@convention(thin) (@owned Klass) -> () { |
| 115 | +bb0(%0 : @owned $Klass): |
| 116 | + %1 = begin_borrow %0 : $Klass |
| 117 | + %2 = unchecked_ownership_conversion %1 : $Klass, @guaranteed to @unowned |
| 118 | + %func = function_ref @unowned_klass_user : $@convention(thin) (Klass) -> () |
| 119 | + apply %func(%2) : $@convention(thin) (Klass) -> () |
| 120 | + end_borrow %1 : $Klass |
| 121 | + destroy_value %0 : $Klass |
| 122 | + %9999 = tuple() |
| 123 | + return %9999 : $() |
| 124 | +} |
| 125 | + |
| 126 | +// CHECK-LABEL: sil [ossa] @guaranteed_to_unowned_unreachable : $@convention(thin) (@owned Klass) -> () { |
| 127 | +// CHECK: begin_borrow |
| 128 | +// CHECK-NOT: unchecked_ownership_conversion |
| 129 | +// CHECK: } // end sil function 'guaranteed_to_unowned_unreachable' |
| 130 | +sil [ossa] @guaranteed_to_unowned_unreachable : $@convention(thin) (@owned Klass) -> () { |
| 131 | +bb0(%0 : @owned $Klass): |
| 132 | + %1 = begin_borrow %0 : $Klass |
| 133 | + %2 = unchecked_ownership_conversion %1 : $Klass, @guaranteed to @unowned |
| 134 | + %func = function_ref @unowned_klass_user : $@convention(thin) (Klass) -> () |
| 135 | + apply %func(%2) : $@convention(thin) (Klass) -> () |
| 136 | + unreachable |
| 137 | +} |
| 138 | + |
| 139 | +// CHECK-LABEL: sil [ossa] @guaranteed_to_unowned_unreachable_2 : $@convention(thin) (@owned Klass) -> () { |
| 140 | +// CHECK-NOT: unchecked_ownership_conversion |
| 141 | +// CHECK: } // end sil function 'guaranteed_to_unowned_unreachable_2' |
| 142 | +sil [ossa] @guaranteed_to_unowned_unreachable_2 : $@convention(thin) (@owned Klass) -> () { |
| 143 | +bb0(%0 : @owned $Klass): |
| 144 | + %1 = begin_borrow %0 : $Klass |
| 145 | + %2 = unchecked_ownership_conversion %1 : $Klass, @guaranteed to @unowned |
| 146 | + cond_br undef, bb1, bb2 |
| 147 | + |
| 148 | +bb1: |
| 149 | + %func = function_ref @unowned_klass_user : $@convention(thin) (Klass) -> () |
| 150 | + apply %func(%2) : $@convention(thin) (Klass) -> () |
| 151 | + end_borrow %1 : $Klass |
| 152 | + br bb3 |
| 153 | + |
| 154 | +bb2: |
| 155 | + unreachable |
| 156 | + |
| 157 | +bb3: |
| 158 | + destroy_value %0 : $Klass |
| 159 | + %9999 = tuple() |
| 160 | + return %9999 : $() |
| 161 | +} |
| 162 | + |
| 163 | +// Borrow scope is too small, we fail. |
| 164 | +// |
| 165 | +// CHECK-LABEL: sil [ossa] @guaranteed_to_unowned_negative_1 : $@convention(thin) (@owned Klass) -> () { |
| 166 | +// CHECK: unchecked_ownership_conversion |
| 167 | +// CHECK: } // end sil function 'guaranteed_to_unowned_negative_1' |
| 168 | +sil [ossa] @guaranteed_to_unowned_negative_1 : $@convention(thin) (@owned Klass) -> () { |
| 169 | +bb0(%0 : @owned $Klass): |
| 170 | + %1 = begin_borrow %0 : $Klass |
| 171 | + %2 = unchecked_ownership_conversion %1 : $Klass, @guaranteed to @unowned |
| 172 | + end_borrow %1 : $Klass |
| 173 | + %func = function_ref @unowned_klass_user : $@convention(thin) (Klass) -> () |
| 174 | + apply %func(%2) : $@convention(thin) (Klass) -> () |
| 175 | + destroy_value %0 : $Klass |
| 176 | + %9999 = tuple() |
| 177 | + return %9999 : $() |
| 178 | +} |
| 179 | + |
| 180 | +///////////////// |
| 181 | +// Owned Tests // |
| 182 | +///////////////// |
| 183 | + |
| 184 | +// CHECK-LABEL: sil [ossa] @owned_to_unowned_positive_1 : $@convention(thin) (@guaranteed Klass) -> () { |
| 185 | +// CHECK-NOT: unchecked_ownership_conversion |
| 186 | +// CHECK: } // end sil function 'owned_to_unowned_positive_1' |
| 187 | +sil [ossa] @owned_to_unowned_positive_1 : $@convention(thin) (@guaranteed Klass) -> () { |
| 188 | +bb0(%0 : @guaranteed $Klass): |
| 189 | + %1 = copy_value %0 : $Klass |
| 190 | + %2 = unchecked_ownership_conversion %1 : $Klass, @owned to @unowned |
| 191 | + %func = function_ref @unowned_klass_user : $@convention(thin) (Klass) -> () |
| 192 | + apply %func(%2) : $@convention(thin) (Klass) -> () |
| 193 | + destroy_value %1 : $Klass |
| 194 | + %9999 = tuple() |
| 195 | + return %9999 : $() |
| 196 | +} |
| 197 | + |
| 198 | +// CHECK-LABEL: sil [ossa] @owned_to_unowned_unreachable : $@convention(thin) (@guaranteed Klass) -> () { |
| 199 | +// CHECK: copy_value |
| 200 | +// CHECK-NOT: unchecked_ownership_conversion |
| 201 | +// CHECK: } // end sil function 'owned_to_unowned_unreachable' |
| 202 | +sil [ossa] @owned_to_unowned_unreachable : $@convention(thin) (@guaranteed Klass) -> () { |
| 203 | +bb0(%0 : @guaranteed $Klass): |
| 204 | + %1 = copy_value %0 : $Klass |
| 205 | + %2 = unchecked_ownership_conversion %1 : $Klass, @owned to @unowned |
| 206 | + %func = function_ref @unowned_klass_user : $@convention(thin) (Klass) -> () |
| 207 | + apply %func(%2) : $@convention(thin) (Klass) -> () |
| 208 | + unreachable |
| 209 | +} |
| 210 | + |
| 211 | +// CHECK-LABEL: sil [ossa] @owned_to_unowned_unreachable_2 : $@convention(thin) (@guaranteed Klass) -> () { |
| 212 | +// CHECK-NOT: unchecked_ownership_conversion |
| 213 | +// CHECK: } // end sil function 'owned_to_unowned_unreachable_2' |
| 214 | +sil [ossa] @owned_to_unowned_unreachable_2 : $@convention(thin) (@guaranteed Klass) -> () { |
| 215 | +bb0(%0 : @guaranteed $Klass): |
| 216 | + %1 = copy_value %0 : $Klass |
| 217 | + %2 = unchecked_ownership_conversion %1 : $Klass, @owned to @unowned |
| 218 | + cond_br undef, bb1, bb2 |
| 219 | + |
| 220 | +bb1: |
| 221 | + %func = function_ref @unowned_klass_user : $@convention(thin) (Klass) -> () |
| 222 | + apply %func(%2) : $@convention(thin) (Klass) -> () |
| 223 | + destroy_value %1 : $Klass |
| 224 | + br bb3 |
| 225 | + |
| 226 | +bb2: |
| 227 | + unreachable |
| 228 | + |
| 229 | +bb3: |
| 230 | + %9999 = tuple() |
| 231 | + return %9999 : $() |
| 232 | +} |
| 233 | + |
| 234 | +// Borrow scope is too small, we fail. |
| 235 | +// |
| 236 | +// CHECK-LABEL: sil [ossa] @owned_to_unowned_negative_1 : $@convention(thin) (@guaranteed Klass) -> () { |
| 237 | +// CHECK: unchecked_ownership_conversion |
| 238 | +// CHECK: } // end sil function 'owned_to_unowned_negative_1' |
| 239 | +sil [ossa] @owned_to_unowned_negative_1 : $@convention(thin) (@guaranteed Klass) -> () { |
| 240 | +bb0(%0 : @guaranteed $Klass): |
| 241 | + %1 = copy_value %0 : $Klass |
| 242 | + %2 = unchecked_ownership_conversion %1 : $Klass, @owned to @unowned |
| 243 | + destroy_value %1 : $Klass |
| 244 | + %func = function_ref @unowned_klass_user : $@convention(thin) (Klass) -> () |
| 245 | + apply %func(%2) : $@convention(thin) (Klass) -> () |
| 246 | + %9999 = tuple() |
| 247 | + return %9999 : $() |
| 248 | +} |
0 commit comments