Skip to content

Commit 657adcb

Browse files
authored
Merge pull request #77780 from DougGregor/willthrow-no-locks
Treat swift_willThrow(Typed) as not locking or performing allocation
2 parents 53557aa + 13f6169 commit 657adcb

File tree

5 files changed

+60
-2
lines changed

5 files changed

+60
-2
lines changed

lib/SIL/Utils/InstructionUtils.cpp

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -465,6 +465,27 @@ static RuntimeEffect metadataEffect(SILType ty) {
465465
return RuntimeEffect::MetaData;
466466
}
467467

468+
/// Whether this particular SIL function is known a prior not to use the
469+
/// generic metadata it is given.
470+
static bool knownToNotUseGenericMetadata(SILFunction &f) {
471+
// swift_willThrowTypedImpl only uses the generic metadata when a global
472+
// hook has been installed, so we treat it as if the generic metadata is
473+
// unused.
474+
if (f.getName() == "swift_willThrowTypedImpl")
475+
return true;
476+
477+
return false;
478+
}
479+
480+
/// Whether this apply site is a call to a functio that is known not to use
481+
/// the generic metadata it is given.
482+
static bool knownToNotUseGenericMetadata(ApplySite &as) {
483+
if (auto *callee = as.getCalleeFunction()) {
484+
return knownToNotUseGenericMetadata(*callee);
485+
}
486+
return false;
487+
}
488+
468489
RuntimeEffect swift::getRuntimeEffect(SILInstruction *inst, SILType &impactType) {
469490
auto ifNonTrivial = [&](SILType type, RuntimeEffect effect) -> RuntimeEffect {
470491
// Nonescaping closures are modeled with ownership to track borrows, but
@@ -1017,7 +1038,7 @@ RuntimeEffect swift::getRuntimeEffect(SILInstruction *inst, SILType &impactType)
10171038
}
10181039
}
10191040

1020-
if (!as.getSubstitutionMap().empty())
1041+
if (!as.getSubstitutionMap().empty() && !knownToNotUseGenericMetadata(as))
10211042
rt |= RuntimeEffect::MetaData;
10221043
if (auto *pa = dyn_cast<PartialApplyInst>(inst)) {
10231044
if (!pa->isOnStack())

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/constant_propagation_availability.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public func testInlinable() -> Int {
6262
// CHECK-macosx10_14: apply [[F]]
6363
// CHECK-macosx10_14: } // end sil function '$s33constant_propagation_availability27testAvailabilityPropagationSiyF'
6464

65-
// CHECK-macosx10_14: sil [readnone] [_semantics "availability.osversion"] @$ss26_stdlib_isOSVersionAtLeastyBi1_Bw_BwBwtF
65+
// CHECK-macosx10_14: sil [no_locks] [readnone] [_semantics "availability.osversion"] @$ss26_stdlib_isOSVersionAtLeastyBi1_Bw_BwBwtF
6666

6767
// CHECK-inlinable-LABEL: sil {{.*}} @$s4Test13testInlinableSiyF : $@convention(thin) () -> Int {
6868
// CHECK-inlinable: [[F:%.*]] = function_ref @$ss26_stdlib_isOSVersionAtLeastyBi1_Bw_BwBwtF

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)