File: unmanaged_rc.swift

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (40 lines) | stat: -rw-r--r-- 1,893 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
// RUN: %empty-directory(%t)
// RUN: %target-swift-frontend %s -emit-sil | %FileCheck %s
// RUN: %target-swift-frontend %s -emit-sil -O | %FileCheck -check-prefix=OPT %s

// REQUIRES: optimized_stdlib
// REQUIRES: swift_in_compiler

// Make sure that when we invoke Unmanaged._withUnsafeGuaranteedRef, we do not
// have any ref count overhead.
public class Klass {}
public class KlassContainer {
    let k = Klass()
}

@inline(never)
public func myPrint(_ k: Klass) { print(k) }

// Check the codegen of _withUnsafeGuaranteedRef
//
// CHECK-LABEL: sil public_external [transparent] @$ss9UnmanagedV24_withUnsafeGuaranteedRefyqd__qd__xKXEKlF :
// CHECK: bb0([[RESULT:%.*]] : $*Result, [[FUNC:%.*]] : $@noescape @callee_guaranteed @substituted <τ_0_0, τ_0_1 where τ_0_0 : AnyObject> (@guaranteed τ_0_0) -> (@out τ_0_1, @error any Error) for <Instance, Result>, [[UNMANAGED:%.*]] : $Unmanaged<Instance>):
// CHECK: [[UNMANAGED_REF:%.*]] = struct_extract [[UNMANAGED]]
// CHECK: [[REF:%.*]] = unmanaged_to_ref [[UNMANAGED_REF]]
// CHECK: try_apply {{%.*}}([[RESULT]], [[REF]]) :
// CHECK-NOT: destroy_value
// CHECK: } // end sil function '$ss9UnmanagedV24_withUnsafeGuaranteedRefyqd__qd__xKXEKlF'

// OPT-LABEL: sil @$s12unmanaged_rc12useUnmanagedyys0D0VyAA14KlassContainerCGF : $@convention(thin) (Unmanaged<KlassContainer>) -> () {
// OPT: bb0([[UNMANAGED:%.*]] :
// OPT:   [[UNMANAGED_REF:%.*]] = struct_extract [[UNMANAGED]]
// OPT:   [[REF:%.*]] = unmanaged_to_ref [[UNMANAGED_REF]]
// OPT:   [[REF_ELT_ADDR:%.*]] = ref_element_addr [immutable] [[REF]] : $KlassContainer, #KlassContainer.k
// OPT:   [[VALUE:%.*]] = load [[REF_ELT_ADDR]]
// OPT:   apply {{%.*}}([[VALUE]])
// OPT: } // end sil function '$s12unmanaged_rc12useUnmanagedyys0D0VyAA14KlassContainerCGF'
public func useUnmanaged(_ u: Unmanaged<KlassContainer>) {
    u._withUnsafeGuaranteedRef {
        myPrint($0.k)
    }
}