|
| 1 | +// RUN: %target-swift-frontend -primary-file %s -emit-ir | %FileCheck %s |
| 2 | +// RUN: %target-swift-frontend -primary-file %s -O -emit-ir | %FileCheck %s --check-prefix=OPT |
| 3 | + |
| 4 | +// On iOS _stdlib_isOSVersionAtLeast() is @_transparent, which affects optimization. |
| 5 | +// See IRGen/availability.swift for other Apple platforms. |
| 6 | +// REQUIRES: OS=ios |
| 7 | + |
| 8 | +import Foundation |
| 9 | + |
| 10 | +// We mustn't hoist the alloc_stack for measurement out of the availability |
| 11 | +// guard. |
| 12 | + |
| 13 | +// CHECK-LABEL: define{{.*}} @{{.*}}dontHoist |
| 14 | +// CHECK-NOT: s10Foundation11MeasurementVySo17NSUnitTemperature |
| 15 | +// CHECK: call swiftcc i1 @"$ss31_stdlib_isOSVersionAtLeast_AEICyBi1_Bw_BwBwtF"( |
| 16 | +// CHECK: s10Foundation11MeasurementVySo17NSUnitTemperature |
| 17 | + |
| 18 | +// OPT-LABEL: define{{.*}} @{{.*}}dontHoist |
| 19 | +// OPT-NOT: S10Foundation11MeasurementVySo17NSUnitTemperature |
| 20 | +// OPT: call {{.*}} @__isPlatformVersionAtLeast( |
| 21 | +// OPT: s10Foundation11MeasurementVySo17NSUnitTemperature |
| 22 | + |
| 23 | +public func dontHoist() { |
| 24 | + if #available(macOS 51.0, iOS 54.0, watchOS 57.0, tvOS 54.0, visionOS 51.1, *) { |
| 25 | + let measurement = Measurement<UnitTemperature>(value: Double(42), unit: .celsius) |
| 26 | + print("\(measurement)") |
| 27 | + } else { |
| 28 | + print("Not measurement") |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | + |
| 33 | +// Now that _isOSVersionAtLeast is no longer inlinable, we do still |
| 34 | +// mark it as _effects(readnone). |
| 35 | +// This means that unlike in the past the optimizer can now only coalesce |
| 36 | +// availability checks with the same availability. It does not determine, |
| 37 | +// for example, that a check for iOS 10 is sufficient to guarantee that a check |
| 38 | +// for iOS 9 will also succeed. |
| 39 | + |
| 40 | +// With optimizations on, multiple #availability checks should generate only |
| 41 | +// a single call into _isOSVersionAtLeast, which after inlining will be a |
| 42 | +// call to __isPlatformVersionAtLeast. |
| 43 | + |
| 44 | +// CHECK-LABEL: define{{.*}} @{{.*}}multipleAvailabilityChecks |
| 45 | +// CHECK: call swiftcc i1 @"$ss31_stdlib_isOSVersionAtLeast_AEICyBi1_Bw_BwBwtF"( |
| 46 | +// CHECK: call swiftcc i1 @"$ss31_stdlib_isOSVersionAtLeast_AEICyBi1_Bw_BwBwtF"( |
| 47 | +// CHECK: call swiftcc i1 @"$ss31_stdlib_isOSVersionAtLeast_AEICyBi1_Bw_BwBwtF"( |
| 48 | +// CHECK: ret void |
| 49 | + |
| 50 | +// OPT-LABEL: define{{.*}} @{{.*}}multipleAvailabilityChecks |
| 51 | +// OPT: call {{.*}} @__isPlatformVersionAtLeast |
| 52 | +// OPT-NOT: call {{.*}} @$__isPlatformVersionAtLeast |
| 53 | +// OPT: ret void |
| 54 | +public func multipleAvailabilityChecks() { |
| 55 | + if #available(macOS 51.0, iOS 54.0, watchOS 57.0, tvOS 54.0, visionOS 51.1, *) { |
| 56 | + print("test one") |
| 57 | + } |
| 58 | + if #available(macOS 51.0, iOS 54.0, watchOS 57.0, tvOS 54.0, visionOS 51.1, *) { |
| 59 | + print("test two") |
| 60 | + } |
| 61 | + if #available(macOS 51.0, iOS 54.0, watchOS 57.0, tvOS 54.0, visionOS 51.1, *) { |
| 62 | + print("test three") |
| 63 | + } |
| 64 | +} |
0 commit comments