Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[6.1] Treat swift_willThrow(Typed) as not locking or performing allocation #77811

Merged
merged 3 commits into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion lib/SIL/Utils/InstructionUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,27 @@ static RuntimeEffect metadataEffect(SILType ty) {
return RuntimeEffect::MetaData;
}

/// Whether this particular SIL function is known a prior not to use the
/// generic metadata it is given.
static bool knownToNotUseGenericMetadata(SILFunction &f) {
// swift_willThrowTypedImpl only uses the generic metadata when a global
// hook has been installed, so we treat it as if the generic metadata is
// unused.
if (f.getName() == "swift_willThrowTypedImpl")
return true;

return false;
}

/// Whether this apply site is a call to a functio that is known not to use
/// the generic metadata it is given.
static bool knownToNotUseGenericMetadata(ApplySite &as) {
if (auto *callee = as.getCalleeFunction()) {
return knownToNotUseGenericMetadata(*callee);
}
return false;
}

RuntimeEffect swift::getRuntimeEffect(SILInstruction *inst, SILType &impactType) {
auto ifNonTrivial = [&](SILType type, RuntimeEffect effect) -> RuntimeEffect {
// Nonescaping closures are modeled with ownership to track borrows, but
Expand Down Expand Up @@ -1017,7 +1038,7 @@ RuntimeEffect swift::getRuntimeEffect(SILInstruction *inst, SILType &impactType)
}
}

if (!as.getSubstitutionMap().empty())
if (!as.getSubstitutionMap().empty() && !knownToNotUseGenericMetadata(as))
rt |= RuntimeEffect::MetaData;
if (auto *pa = dyn_cast<PartialApplyInst>(inst)) {
if (!pa->isOnStack())
Expand Down
5 changes: 5 additions & 0 deletions stdlib/public/core/Availability.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import SwiftShims
#if os(iOS) && !os(visionOS)
@_effects(readnone)
@_transparent
@_noLocks
public func _stdlib_isOSVersionAtLeast(
_ major: Builtin.Word,
_ minor: Builtin.Word,
Expand All @@ -51,6 +52,7 @@ public func _stdlib_isOSVersionAtLeast(
@_semantics("availability.osversion")
@_effects(readnone)
@_unavailableInEmbedded
@_noLocks
public func _stdlib_isOSVersionAtLeast(
_ major: Builtin.Word,
_ minor: Builtin.Word,
Expand All @@ -63,6 +65,7 @@ public func _stdlib_isOSVersionAtLeast(
@_semantics("availability.osversion")
@_effects(readnone)
@_alwaysEmitIntoClient
@_noLocks
public func _stdlib_isOSVersionAtLeast_AEIC(
_ major: Builtin.Word,
_ minor: Builtin.Word,
Expand Down Expand Up @@ -107,6 +110,7 @@ public func _stdlib_isOSVersionAtLeast_AEIC(
@_semantics("availability.osversion")
@_effects(readnone)
@available(macOS 10.15, iOS 13.0, *)
@_noLocks
public func _stdlib_isVariantOSVersionAtLeast(
_ major: Builtin.Word,
_ minor: Builtin.Word,
Expand Down Expand Up @@ -149,6 +153,7 @@ public func _stdlib_isVariantOSVersionAtLeast(
@_semantics("availability.osversion")
@_effects(readnone)
@_unavailableInEmbedded
@_noLocks
public func _stdlib_isOSVersionAtLeastOrVariantVersionAtLeast(
_ major: Builtin.Word,
_ minor: Builtin.Word,
Expand Down
1 change: 1 addition & 0 deletions stdlib/public/core/ErrorType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ public func _bridgeErrorToNSError(_ error: __owned Error) -> AnyObject
@_silgen_name("swift_willThrowTypedImpl")
@available(SwiftStdlib 6.0, *)
@usableFromInline
@_noLocks
func _willThrowTypedImpl<E: Error>(_ error: E)

#if !$Embedded
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public func testInlinable() -> Int {
// CHECK-macosx10_14: apply [[F]]
// CHECK-macosx10_14: } // end sil function '$s33constant_propagation_availability27testAvailabilityPropagationSiyF'

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

// CHECK-inlinable-LABEL: sil {{.*}} @$s4Test13testInlinableSiyF : $@convention(thin) () -> Int {
// CHECK-inlinable: [[F:%.*]] = function_ref @$ss26_stdlib_isOSVersionAtLeastyBi1_Bw_BwBwtF
Expand Down
31 changes: 31 additions & 0 deletions test/SILOptimizer/performance-annotations.swift
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,37 @@ func testCatch(_ b: Bool) throws -> Int? {
}
}

enum ErrorEnum: Error {
case failed
case tryAgain
}

@_noLocks
func concreteError(_ b: Bool) throws(ErrorEnum) -> Int {
if b {
return 28
}

throw .tryAgain
}

func concreteErrorOther(_ b: Bool) throws(ErrorEnum) -> Int {
if b {
return 28
}

throw .tryAgain
}

@_noLocks
func testCatchConcrete(_ b: Bool) -> Int {
do {
return try concreteError(b) + concreteErrorOther(b)
} catch {
return 17
}
}

@_noLocks
func testRecursion(_ i: Int) -> Int {
if i > 0 {
Expand Down