|
| 1 | +// RUN: %target-swift-emit-silgen -parse-stdlib %s -disable-access-control -disable-objc-attr-requires-foundation-module -enable-objc-interop | %FileCheck %s |
| 2 | +// RUN: %target-swift-emit-sil -Onone -parse-stdlib %s -disable-access-control -disable-objc-attr-requires-foundation-module -enable-objc-interop | %FileCheck -check-prefix=CANONICAL %s |
| 3 | +// RUN: %target-swift-emit-sil -O -parse-stdlib %s -disable-access-control -disable-objc-attr-requires-foundation-module -enable-objc-interop | %FileCheck -check-prefix=OPT %s |
| 4 | + |
| 5 | +import Swift |
| 6 | + |
| 7 | +// Eventually element will be unconstrained, but for testing this builtin, we |
| 8 | +// should use it this way. |
| 9 | +@frozen |
| 10 | +public struct UnsafeValue<Element: AnyObject> { |
| 11 | + @usableFromInline |
| 12 | + internal unowned(unsafe) var _value: Element? |
| 13 | + |
| 14 | + @_transparent |
| 15 | + @inlinable |
| 16 | + public init() { |
| 17 | + _value = nil |
| 18 | + } |
| 19 | + |
| 20 | + // Create a new unmanaged value that owns the underlying value. This unmanaged |
| 21 | + // value must after use be deinitialized by calling the function deinitialize() |
| 22 | + // |
| 23 | + // This will insert a retain that the optimizer can not remove! |
| 24 | + @_transparent |
| 25 | + @inlinable |
| 26 | + public init(copying newValue: __shared Element) { |
| 27 | + self.init() |
| 28 | + _value = newValue |
| 29 | + } |
| 30 | + |
| 31 | + // Create a new unmanaged value that unsafely produces a new |
| 32 | + // unmanaged value without introducing any rr traffic. |
| 33 | + // |
| 34 | + // CHECK-LABEL: sil [transparent] [serialized] [ossa] @$s11unsafevalue11UnsafeValueV14unsafelyAssignACyxGxh_tcfC : $@convention(method) <Element where Element : AnyObject> (@guaranteed Element, @thin UnsafeValue<Element>.Type) -> UnsafeValue<Element> { |
| 35 | + // CHECK: bb0([[INPUT_ELEMENT:%.*]] : @guaranteed $Element, |
| 36 | + // CHECK: [[BOX:%.*]] = alloc_box |
| 37 | + // CHECK: [[UNINIT_BOX:%.*]] = mark_uninitialized [delegatingself] [[BOX]] |
| 38 | + // CHECK: [[PROJECT_UNINIT_BOX:%.*]] = project_box [[UNINIT_BOX]] |
| 39 | + // CHECK: [[DELEGATING_INIT_RESULT:%.*]] = apply {{%.*}}<Element>( |
| 40 | + // CHECK: assign [[DELEGATING_INIT_RESULT]] to [[PROJECT_UNINIT_BOX]] |
| 41 | + // CHECK: [[ACCESS:%.*]] = begin_access [read] [unknown] [[PROJECT_UNINIT_BOX]] |
| 42 | + // CHECK: [[STRUCT_ACCESS:%.*]] = struct_element_addr [[ACCESS]] |
| 43 | + // CHECK: [[OPT_INPUT_ELEMENT:%.*]] = enum $Optional<Element>, #Optional.some!enumelt.1, [[INPUT_ELEMENT]] |
| 44 | + // CHECK: [[UNMANAGED_OPT_INPUT_ELEMENT:%.*]] = ref_to_unmanaged [[OPT_INPUT_ELEMENT]] |
| 45 | + // CHECK: store [[UNMANAGED_OPT_INPUT_ELEMENT]] to [trivial] [[STRUCT_ACCESS]] |
| 46 | + // CHECK: end_access [[ACCESS]] |
| 47 | + // CHECK: [[RESULT:%.*]] = load [trivial] [[PROJECT_UNINIT_BOX]] |
| 48 | + // CHECK: destroy_value [[UNINIT_BOX]] |
| 49 | + // CHECK: return [[RESULT]] |
| 50 | + // CHECK: } // end sil function '$s11unsafevalue11UnsafeValueV14unsafelyAssignACyxGxh_tcfC' |
| 51 | + // |
| 52 | + // CANONICAL-LABEL: sil [transparent] [serialized] @$s11unsafevalue11UnsafeValueV14unsafelyAssignACyxGxh_tcfC : $@convention(method) <Element where Element : AnyObject> (@guaranteed Element, @thin UnsafeValue<Element>.Type) -> UnsafeValue<Element> { |
| 53 | + // CANONICAL: bb0([[INPUT_ELEMENT:%.*]] : $Element, |
| 54 | + // CANONICAL-NEXT: debug_value |
| 55 | + // TODO(gottesmm): release_value on a .none shouldn't exist. |
| 56 | + // CANONICAL-NEXT: [[NONE:%.*]] = enum $Optional<Element>, #Optional.none!enumelt |
| 57 | + // CANONICAL-NEXT: release_value [[NONE]] |
| 58 | + // CANONICAL-NEXT: [[ENUM:%.*]] = enum $Optional<Element>, #Optional.some!enumelt.1, [[INPUT_ELEMENT]] |
| 59 | + // CANONICAL-NEXT: [[UNMANAGED_ENUM:%.*]] = ref_to_unmanaged [[ENUM]] |
| 60 | + // CANONICAL-NEXT: [[RESULT:%.*]] = struct $UnsafeValue<Element> ([[UNMANAGED_ENUM]] : $@sil_unmanaged Optional<Element>) |
| 61 | + // CANONICAL-NEXT: tuple |
| 62 | + // CANONICAL-NEXT: return [[RESULT]] |
| 63 | + // CANONICAL: } // end sil function '$s11unsafevalue11UnsafeValueV14unsafelyAssignACyxGxh_tcfC' |
| 64 | + // |
| 65 | + // OPT-LABEL: sil [transparent] @$s11unsafevalue11UnsafeValueV14unsafelyAssignACyxGxh_tcfC : $@convention(method) <Element where Element : AnyObject> (@guaranteed Element, @thin UnsafeValue<Element>.Type) -> UnsafeValue<Element> { |
| 66 | + // OPT: bb0([[INPUT_ELEMENT:%.*]] : $Element, |
| 67 | + // OPT-NEXT: debug_value |
| 68 | + // OPT-NEXT: [[ENUM:%.*]] = enum $Optional<Element>, #Optional.some!enumelt.1, [[INPUT_ELEMENT]] |
| 69 | + // OPT-NEXT: [[UNMANAGED_ENUM:%.*]] = ref_to_unmanaged [[ENUM]] |
| 70 | + // OPT-NEXT: [[RESULT:%.*]] = struct $UnsafeValue<Element> ([[UNMANAGED_ENUM]] : $@sil_unmanaged Optional<Element>) |
| 71 | + // OPT-NEXT: return [[RESULT]] |
| 72 | + // OPT: } // end sil function '$s11unsafevalue11UnsafeValueV14unsafelyAssignACyxGxh_tcfC' |
| 73 | + @_transparent |
| 74 | + @inlinable |
| 75 | + public init(unsafelyAssign newValue: __shared Element) { |
| 76 | + self.init() |
| 77 | + Builtin.convertStrongToUnownedUnsafe(newValue, &_value) |
| 78 | + } |
| 79 | + |
| 80 | + // Access the underlying value at +0, guaranteeing its lifetime by base. |
| 81 | + @_transparent |
| 82 | + @inlinable |
| 83 | + func withGuaranteeingBase<Base>(base: Base, f: (Element) -> ()) { |
| 84 | + // TODO: Once we have a builtin for mark_dependence, fill this in. |
| 85 | + } |
| 86 | + |
| 87 | + // If the unmanaged value was initialized with a copy, release the underlying value. |
| 88 | + // |
| 89 | + // This will insert a release that can not be removed by the optimizer. |
| 90 | + @_transparent |
| 91 | + @inlinable |
| 92 | + mutating func deinitialize() { |
| 93 | + _value = nil |
| 94 | + } |
| 95 | + |
| 96 | + // Return a new strong reference to the unmanaged value. |
| 97 | + // |
| 98 | + // This will insert a retain that can not be removed by the optimizer! |
| 99 | + @_transparent |
| 100 | + @inlinable |
| 101 | + public var strongRef: Element { _value.unsafelyUnwrapped } |
| 102 | +} |
0 commit comments