Skip to content

Commit 195e380

Browse files
committed
[Runtime] Mark _swift_stdlib_operatingSystemVersion as __attribute__((const)).
This tells the compiler that repeated calls to this function will always generate the same result, which allows the optimizer to coalesce multiple calls, hoist calls out of loops, and other such nice things. rdar://problem/20690429
1 parent 34f6a1f commit 195e380

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

stdlib/public/SwiftShims/FoundationShims.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ typedef struct {
5555
} _SwiftNSOperatingSystemVersion;
5656

5757
SWIFT_RUNTIME_STDLIB_API
58-
_SwiftNSOperatingSystemVersion _swift_stdlib_operatingSystemVersion();
58+
_SwiftNSOperatingSystemVersion _swift_stdlib_operatingSystemVersion() __attribute__((const));
5959

6060
#ifdef __cplusplus
6161
}} // extern "C", namespace swift

test/IRGen/availability.swift

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,29 @@ public func dontHoist() {
2626
print("Not measurement")
2727
}
2828
}
29+
30+
31+
// With optimizations on, multiple #availability checks should generate only
32+
// a single call into _swift_stdlib_operatingSystemVersion.
33+
34+
// CHECK-LABEL: define{{.*}} @{{.*}}multipleAvailabilityChecks
35+
// CHECK: call swiftcc i1 @"$Ss26_stdlib_isOSVersionAtLeastyBi1_Bw_BwBwtF"(
36+
// CHECK: call swiftcc i1 @"$Ss26_stdlib_isOSVersionAtLeastyBi1_Bw_BwBwtF"(
37+
// CHECK: call swiftcc i1 @"$Ss26_stdlib_isOSVersionAtLeastyBi1_Bw_BwBwtF"(
38+
// CHECK: ret void
39+
40+
// OPT-LABEL: define{{.*}} @{{.*}}multipleAvailabilityChecks
41+
// OPT: call void @_swift_stdlib_operatingSystemVersion
42+
// OPT-NOT: call void @_swift_stdlib_operatingSystemVersion
43+
// OPT: ret void
44+
public func multipleAvailabilityChecks() {
45+
if #available(OSX 10.12, iOS 10.0, watchOS 3.0, tvOS 10.0, *) {
46+
print("test one")
47+
}
48+
if #available(OSX 10.11, iOS 9.0, watchOS 2.0, tvOS 9.0, *) {
49+
print("test two")
50+
}
51+
if #available(OSX 10.10, iOS 8.0, watchOS 1.0, tvOS 8.0, *) {
52+
print("test three")
53+
}
54+
}

0 commit comments

Comments
 (0)