Skip to content

Commit 4893989

Browse files
authored
Merge pull request swiftlang#30064 from gottesmm/pr-f768cd12b2b9ed81d02c7df2f86e47aa07ef8cbe
[stdlib] Mark Unmanaged._withUnsafeGuaranteedRef as _transparent and add a test/comments as requested in the original PR where this landed.
2 parents d823be9 + 5982381 commit 4893989

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

stdlib/public/core/Unmanaged.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,10 +203,16 @@ public struct Unmanaged<Instance: AnyObject> {
203203
/// }
204204
/// }
205205
@inlinable // unsafe-performance
206+
@_transparent
206207
public func _withUnsafeGuaranteedRef<Result>(
207208
_ body: (Instance) throws -> Result
208209
) rethrows -> Result {
209210
var tmp = self
211+
// Builtin.convertUnownedUnsafeToGuaranteed expects to have a base value
212+
// that the +0 value depends on. In this case, we are assuming that is done
213+
// for us opaquely already. So, the builtin will emit a mark_dependence on a
214+
// trivial object. The optimizer knows to eliminate that so we do not have
215+
// any overhead from this.
210216
let fakeBase: Int? = nil
211217
return try body(Builtin.convertUnownedUnsafeToGuaranteed(fakeBase,
212218
&tmp._value))

test/stdlib/unmanaged_rc.swift

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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

Comments
 (0)