|
| 1 | +// RUN: %target-swift-frontend -target x86_64-apple-ios13.1-macabi -primary-file %s -emit-ir | %FileCheck %s |
| 2 | +// RUN: %target-swift-frontend -target x86_64-apple-ios13.1-macabi -primary-file %s -O -emit-ir | %FileCheck %s --check-prefix=OPT |
| 3 | + |
| 4 | +// REQUIRES: objc_interop |
| 5 | +// REQUIRES: OS=macosx || OS=maccatalyst |
| 6 | + |
| 7 | +import Foundation |
| 8 | + |
| 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 @"$ss33_stdlib_isVariantOSVersionAtLeastyBi1_Bw_BwBwtF"( |
| 16 | +// CHECK: s10Foundation11MeasurementVySo17NSUnitTemperature |
| 17 | + |
| 18 | +// OPT-LABEL: define{{.*}} @{{.*}}dontHoist |
| 19 | +// OPT-NOT: S10Foundation11MeasurementVySo17NSUnitTemperature |
| 20 | +// OPT: call {{.*}} @"$ss33_stdlib_isVariantOSVersionAtLeastyBi1_Bw_BwBwtF"( |
| 21 | +// OPT: s10Foundation11MeasurementVySo17NSUnitTemperature |
| 22 | + |
| 23 | +public func dontHoist() { |
| 24 | + if #available(macOS 10.15.4, iOS 13.4, watchOS 6.2, tvOS 13.4, *) { |
| 25 | + let measurement = Measurement<UnitTemperature>(value: Double(42), unit: .celsius) |
| 26 | + print("\(measurement)") |
| 27 | + } else { |
| 28 | + print("Not measurement") |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +// The optimizer is weaker for _isVariantOSVersionAtLeast than for _isOSVersionAtLeast |
| 33 | +// because the first is implemented in a runtime call in compiler-rt. We don't want |
| 34 | +// to inline the implementation details of the compiler-rt support into apps, so |
| 35 | +// _isVariantOSVersionAtLeast not marked as inlinable, but we do mark it as |
| 36 | +// _effects(readnone). |
| 37 | +// This means that unlike _isOSVersionAtLeast the optimizer can only coalesce |
| 38 | +// availability checks with the same availability. It does not determine, for example, |
| 39 | +// that a check for iOS 10 is sufficient to guarantee that a check for iOS 9 will also |
| 40 | +// succeed. |
| 41 | + |
| 42 | +// With optimizations on, multiple #availability checks should generate only |
| 43 | +// a single call into _isVariantOSVersionAtLeast. |
| 44 | + |
| 45 | +// CHECK-LABEL: define{{.*}} @{{.*}}multipleAvailabilityChecks |
| 46 | +// CHECK: call swiftcc i1 @"$ss33_stdlib_isVariantOSVersionAtLeastyBi1_Bw_BwBwtF"( |
| 47 | +// CHECK: call swiftcc i1 @"$ss33_stdlib_isVariantOSVersionAtLeastyBi1_Bw_BwBwtF"( |
| 48 | +// CHECK: call swiftcc i1 @"$ss33_stdlib_isVariantOSVersionAtLeastyBi1_Bw_BwBwtF"( |
| 49 | +// CHECK: ret void |
| 50 | + |
| 51 | +// OPT-LABEL: define{{.*}} @{{.*}}multipleAvailabilityChecks |
| 52 | +// OPT: call {{.*}} @"$ss33_stdlib_isVariantOSVersionAtLeastyBi1_Bw_BwBwtF" |
| 53 | +// OPT-NOT: call {{.*}} @"$ss33_stdlib_isVariantOSVersionAtLeastyBi1_Bw_BwBwtF" |
| 54 | +// OPT: ret void |
| 55 | +public func multipleAvailabilityChecks() { |
| 56 | + if #available(macOS 10.15.4, iOS 13.4, watchOS 6.2, tvOS 13.4, *) { |
| 57 | + print("test one") |
| 58 | + } |
| 59 | + if #available(macOS 10.15.4, iOS 13.4, watchOS 6.2, tvOS 13.4, *) { |
| 60 | + print("test two") |
| 61 | + } |
| 62 | + if #available(macOS 10.15.4, iOS 13.4, watchOS 6.2, tvOS 13.4, *) { |
| 63 | + print("test three") |
| 64 | + } |
| 65 | +} |
0 commit comments