Skip to content

Commit eff9bac

Browse files
committed
Treat swift_willThrow(Typed) as not locking or performing allocation
Prior to throwing, Swift emits a call to `swift_willThrow(Typed)`, which allows various diagnostic tools (such as debuggers and testing libraries) to intercept errors at the point where they are initially thrown. Since `swift_willThrow(Typed)` can be hooked by arbitrary code at runtime, there is no way for it to meet performance constraints like @_noLocks or @_noAllocation. Therefore, in a function that has those performance constraints specified, disable emission of the call to `swift_willThrow(Typed)`. Fixes rdar://140230684.
1 parent 3fdec21 commit eff9bac

File tree

3 files changed

+37
-0
lines changed

3 files changed

+37
-0
lines changed

stdlib/public/core/Availability.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ import SwiftShims
4040
#if os(iOS) && !os(visionOS)
4141
@_effects(readnone)
4242
@_transparent
43+
@_noLocks
4344
public func _stdlib_isOSVersionAtLeast(
4445
_ major: Builtin.Word,
4546
_ minor: Builtin.Word,
@@ -51,6 +52,7 @@ public func _stdlib_isOSVersionAtLeast(
5152
@_semantics("availability.osversion")
5253
@_effects(readnone)
5354
@_unavailableInEmbedded
55+
@_noLocks
5456
public func _stdlib_isOSVersionAtLeast(
5557
_ major: Builtin.Word,
5658
_ minor: Builtin.Word,
@@ -63,6 +65,7 @@ public func _stdlib_isOSVersionAtLeast(
6365
@_semantics("availability.osversion")
6466
@_effects(readnone)
6567
@_alwaysEmitIntoClient
68+
@_noLocks
6669
public func _stdlib_isOSVersionAtLeast_AEIC(
6770
_ major: Builtin.Word,
6871
_ minor: Builtin.Word,
@@ -107,6 +110,7 @@ public func _stdlib_isOSVersionAtLeast_AEIC(
107110
@_semantics("availability.osversion")
108111
@_effects(readnone)
109112
@available(macOS 10.15, iOS 13.0, *)
113+
@_noLocks
110114
public func _stdlib_isVariantOSVersionAtLeast(
111115
_ major: Builtin.Word,
112116
_ minor: Builtin.Word,
@@ -149,6 +153,7 @@ public func _stdlib_isVariantOSVersionAtLeast(
149153
@_semantics("availability.osversion")
150154
@_effects(readnone)
151155
@_unavailableInEmbedded
156+
@_noLocks
152157
public func _stdlib_isOSVersionAtLeastOrVariantVersionAtLeast(
153158
_ major: Builtin.Word,
154159
_ minor: Builtin.Word,

stdlib/public/core/ErrorType.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ public func _bridgeErrorToNSError(_ error: __owned Error) -> AnyObject
181181
@_silgen_name("swift_willThrowTypedImpl")
182182
@available(SwiftStdlib 6.0, *)
183183
@usableFromInline
184+
@_noLocks
184185
func _willThrowTypedImpl<E: Error>(_ error: E)
185186

186187
#if !$Embedded

test/SILOptimizer/performance-annotations.swift

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,37 @@ func testCatch(_ b: Bool) throws -> Int? {
125125
}
126126
}
127127

128+
enum ErrorEnum: Error {
129+
case failed
130+
case tryAgain
131+
}
132+
133+
@_noLocks
134+
func concreteError(_ b: Bool) throws(ErrorEnum) -> Int {
135+
if b {
136+
return 28
137+
}
138+
139+
throw .tryAgain
140+
}
141+
142+
func concreteErrorOther(_ b: Bool) throws(ErrorEnum) -> Int {
143+
if b {
144+
return 28
145+
}
146+
147+
throw .tryAgain
148+
}
149+
150+
@_noLocks
151+
func testCatchConcrete(_ b: Bool) -> Int {
152+
do {
153+
return try concreteError(b) + concreteErrorOther(b)
154+
} catch {
155+
return 17
156+
}
157+
}
158+
128159
@_noLocks
129160
func testRecursion(_ i: Int) -> Int {
130161
if i > 0 {

0 commit comments

Comments
 (0)