|
| 1 | +// RUN: %empty-directory(%t) |
| 2 | +// RUN: %target-swift-frontend %s -emit-sil | %FileCheck %s |
| 3 | +// RUN: %target-swift-frontend %s -emit-sil -O | %FileCheck -check-prefix=OPT %s |
| 4 | + |
| 5 | +// Make sure that when we invoke Unmanaged._withUnsafeGuaranteedRef, we do not |
| 6 | +// have any ref count overhead. |
| 7 | +public class Klass {} |
| 8 | +public class KlassContainer { |
| 9 | + let k = Klass() |
| 10 | +} |
| 11 | + |
| 12 | +@inline(never) |
| 13 | +public func myPrint(_ k: Klass) { print(k) } |
| 14 | + |
| 15 | +// Check the codegen of _withUnsafeGuaranteedRef |
| 16 | +// |
| 17 | +// CHECK-LABEL: sil public_external [transparent] [serialized] @$ss9UnmanagedV24_withUnsafeGuaranteedRefyqd__qd__xKXEKlF : $@convention(method) <Instance where Instance : AnyObject><Result> (@noescape @callee_guaranteed <τ_0_0, τ_0_1 where τ_0_0 : _RefCountedObject> in (@guaranteed τ_0_0) -> (@out τ_0_1, @error Error) for <Instance, Result>, Unmanaged<Instance>) -> (@out Result, @error Error) { |
| 18 | +// CHECK: bb0([[RESULT:%.*]] : $*Result, [[FUNC:%.*]] : $@noescape @callee_guaranteed <τ_0_0, τ_0_1 where τ_0_0 : _RefCountedObject> in (@guaranteed τ_0_0) -> (@out τ_0_1, @error Error) for <Instance, Result>, [[UNMANAGED:%.*]] : $Unmanaged<Instance>): |
| 19 | +// CHECK: [[UNMANAGED_REF:%.*]] = struct_extract [[UNMANAGED]] |
| 20 | +// CHECK: [[REF:%.*]] = unmanaged_to_ref [[UNMANAGED_REF]] |
| 21 | +// CHECK: [[REF_MARK_DEP:%.*]] = mark_dependence [[REF]] |
| 22 | +// CHECK: try_apply {{%.*}}([[RESULT]], [[REF_MARK_DEP]]) : $@noescape @callee_guaranteed <τ_0_0, τ_0_1 where τ_0_0 : _RefCountedObject> in (@guaranteed τ_0_0) -> (@out τ_0_1, @error Error) for <Instance, Result>, normal bb2, error bb1 |
| 23 | +// CHECK-NOT: destroy_value |
| 24 | +// CHECK: } // end sil function '$ss9UnmanagedV24_withUnsafeGuaranteedRefyqd__qd__xKXEKlF' |
| 25 | + |
| 26 | +// OPT-LABEL: sil @$s12unmanaged_rc12useUnmanagedyys0D0VyAA14KlassContainerCGF : $@convention(thin) (Unmanaged<KlassContainer>) -> () { |
| 27 | +// OPT: bb0([[UNMANAGED:%.*]] : |
| 28 | +// OPT: [[UNMANAGED_REF:%.*]] = struct_extract [[UNMANAGED]] |
| 29 | +// OPT: [[REF:%.*]] = unmanaged_to_ref [[UNMANAGED_REF]] |
| 30 | +// OPT: [[REF_ELT_ADDR:%.*]] = ref_element_addr [[REF]] : $KlassContainer, #KlassContainer.k |
| 31 | +// OPT: [[VALUE:%.*]] = load [[REF_ELT_ADDR]] |
| 32 | +// OPT: apply {{%.*}}([[VALUE]]) |
| 33 | +// OPT: } // end sil function '$s12unmanaged_rc12useUnmanagedyys0D0VyAA14KlassContainerCGF' |
| 34 | +public func useUnmanaged(_ u: Unmanaged<KlassContainer>) { |
| 35 | + u._withUnsafeGuaranteedRef { |
| 36 | + myPrint($0.k) |
| 37 | + } |
| 38 | +} |
0 commit comments