|
| 1 | +//===--- OpaqueConsumingUsers.swift ---------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2018 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +import TestsUtils |
| 14 | + |
| 15 | +public let OpaqueConsumingUsers = BenchmarkInfo( |
| 16 | + name: "OpaqueConsumingUsers", |
| 17 | + runFunction: run_OpaqueConsumingUsers, |
| 18 | + tags: [.regression, .abstraction, .refcount], |
| 19 | + setUpFunction: setup_OpaqueConsumingUsers) |
| 20 | + |
| 21 | +// This test exercises the ability of the optimizer to propagate the +1 from a |
| 22 | +// consuming argument of a non-inlineable through multiple non-inlinable call |
| 23 | +// frames. |
| 24 | +// |
| 25 | +// We are trying to simulate a user application that calls into a resilient |
| 26 | +// setter or initialization. We want to be able to propagate the +1 from the |
| 27 | +// setter through the app as far as we can. |
| 28 | + |
| 29 | +class Klass {} |
| 30 | + |
| 31 | +class ConsumingUser { |
| 32 | + var _innerValue: Klass = Klass() |
| 33 | + |
| 34 | + var value: Klass { |
| 35 | + @inline(never) get { |
| 36 | + return _innerValue |
| 37 | + } |
| 38 | + @inline(never) set { |
| 39 | + _innerValue = newValue |
| 40 | + } |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +var data: Klass? = nil |
| 45 | +var user: ConsumingUser? = nil |
| 46 | + |
| 47 | +func setup_OpaqueConsumingUsers() { |
| 48 | + switch (data, user) { |
| 49 | + case (let x?, let y?): |
| 50 | + let _ = x |
| 51 | + let _ = y |
| 52 | + return |
| 53 | + case (nil, nil): |
| 54 | + data = Klass() |
| 55 | + user = ConsumingUser() |
| 56 | + default: |
| 57 | + fatalError("Data and user should both be .none or .some") |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +@inline(never) |
| 62 | +func callFrame1(_ data: Klass, _ user: ConsumingUser) { |
| 63 | + callFrame2(data, user) |
| 64 | +} |
| 65 | + |
| 66 | +@inline(never) |
| 67 | +func callFrame2(_ data: Klass, _ user: ConsumingUser) { |
| 68 | + callFrame3(data, user) |
| 69 | +} |
| 70 | + |
| 71 | +@inline(never) |
| 72 | +func callFrame3(_ data: Klass, _ user: ConsumingUser) { |
| 73 | + callFrame4(data, user) |
| 74 | +} |
| 75 | + |
| 76 | +@inline(never) |
| 77 | +func callFrame4(_ data: Klass, _ user: ConsumingUser) { |
| 78 | + user.value = data |
| 79 | +} |
| 80 | + |
| 81 | +@inline(never) |
| 82 | +public func run_OpaqueConsumingUsers(_ N: Int) { |
| 83 | + let d = data._unsafelyUnwrappedUnchecked |
| 84 | + let u = user._unsafelyUnwrappedUnchecked |
| 85 | + for _ in 0..<N*200000 { |
| 86 | + callFrame4(d, u) |
| 87 | + } |
| 88 | +} |
0 commit comments