Skip to content
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
36 changes: 31 additions & 5 deletions lib/IRGen/GenReflection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,25 @@ class PrintMetadataSource
}
};

/// Determine whether the given generic nominal that involves inverse
/// requirements (e.g., Optional, Span) is always available for demangling
/// purposes.
static bool nominalIsAlwaysAvailableForDemangling(const NominalTypeDecl *nom) {
// Only consider standard library types for this.
if (!nom->getModuleContext()->isStdlibModule())
return false;

// If there's an @_originallyDefined(in:) attribute, then the nominal is
// not always available for demangling.
for (auto attr: nom->getAttrs().getAttributes<OriginallyDefinedInAttr>()) {
if (!attr->isInvalid() && attr->isActivePlatform(nom->getASTContext()))
return false;
}

// Everything else is available.
return true;
}

std::optional<llvm::VersionTuple>
getRuntimeVersionThatSupportsDemanglingType(CanType type) {
enum VersionRequirement {
Expand All @@ -185,9 +204,10 @@ getRuntimeVersionThatSupportsDemanglingType(CanType type) {
Swift_5_5,
Swift_6_0,
Swift_6_1,
Swift_6_2,

// Short-circuit if we find this requirement.
Latest = Swift_6_1
Latest = Swift_6_2
};

VersionRequirement latestRequirement = None;
Expand All @@ -204,6 +224,11 @@ getRuntimeVersionThatSupportsDemanglingType(CanType type) {
auto isolation = fn->getIsolation();
auto sendingResult = fn->hasSendingResult();

// The mangling for nonisolated(nonsending) function types was introduced
// in Swift 6.2.
if (isolation.isNonIsolatedCaller())
return addRequirement(Swift_6_2);

// The Swift 6.1 runtime fixes a bug preventing successful demangling
// when @isolated(any) or global actor isolation is combined with a
// sending result.
Expand Down Expand Up @@ -246,16 +271,16 @@ getRuntimeVersionThatSupportsDemanglingType(CanType type) {
/// signature uses NoncopyableGenerics. Since inverses are mangled into
/// symbols, a Swift 6.0+ runtime is generally needed to demangle them.
///
/// We make an exception for types in the stdlib, like Optional, since the
/// runtime should still be able to demangle them, based on the availability
/// of the type.
/// We make an exception for some types in the stdlib, like Optional, since
/// the runtime should still be able to demangle them, based on the
/// availability of the type.
if (auto nominalTy = dyn_cast<NominalOrBoundGenericNominalType>(t)) {
auto *nom = nominalTy->getDecl();
if (auto sig = nom->getGenericSignature()) {
SmallVector<InverseRequirement, 2> inverses;
SmallVector<Requirement, 2> reqs;
sig->getRequirementsWithInverses(reqs, inverses);
if (!inverses.empty() && !nom->getModuleContext()->isStdlibModule()) {
if (!inverses.empty() && !nominalIsAlwaysAvailableForDemangling(nom)) {
return addRequirement(Swift_6_0);
}
}
Expand All @@ -271,6 +296,7 @@ getRuntimeVersionThatSupportsDemanglingType(CanType type) {
});

switch (latestRequirement) {
case Swift_6_2: return llvm::VersionTuple(6, 2);
case Swift_6_1: return llvm::VersionTuple(6, 1);
case Swift_6_0: return llvm::VersionTuple(6, 0);
case Swift_5_5: return llvm::VersionTuple(5, 5);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// RUN: %target-swift-frontend -target %target-cpu-apple-macos12 -emit-ir -o - -primary-file %s | %FileCheck %s
// REQUIRES: concurrency
// REQUIRES: OS=macosx

func useGenericMetatype(_ type: Any.Type) { }

// CHECK-LABEL: define hidden swiftcc void @"$s52backward_deploy_nonisolated_nonsending_function_type29testNonisolatedNonsendingTypeyyF"()
func testNonisolatedNonsendingType() {
typealias Fn = nonisolated(nonsending) () async throws -> Int

// CHECK: call swiftcc %swift.metadata_response @"$sSiyYaKYCcMa"
// CHECK: call swiftcc void @"$s52backward_deploy_nonisolated_nonsending_function_type18useGenericMetatypeyyypXpF"
useGenericMetatype(Fn.self)
}

// CHECK-LABEL: define linkonce_odr hidden swiftcc %swift.metadata_response @"$sSiyYaKYCcMa"
// CHECK: call ptr @swift_getExtendedFunctionTypeMetadata(i{{32|64}} 2768240640, {{i32|i64}} 0, ptr null, ptr null, ptr @"$sSiN"
14 changes: 14 additions & 0 deletions test/IRGen/backward_deploy_span.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// RUN: %target-swift-frontend -target %target-cpu-apple-macos12 -emit-ir -o - -primary-file %s | %FileCheck %s
// REQUIRES: OS=macosx

func useGenericMetatype(_ type: any ~Escapable.Type) { }

// CHECK-LABEL: define hidden swiftcc void @"$s20backward_deploy_span11testSpanIntyyF"()
func testSpanInt() {
// CHECK: call swiftcc %swift.metadata_response @"$ss4SpanVySiGMa"
// CHECK: call swiftcc void @"$s20backward_deploy_span18useGenericMetatypeyyypRi0_s_XPXpF"
useGenericMetatype(Span<Int>.self)
}

// CHECK-LABEL: define linkonce_odr hidden swiftcc %swift.metadata_response @"$ss4SpanVySiGMa"
// CHECK: call swiftcc %swift.metadata_response @"$ss4SpanVMa"({{i32|i64}} %0, ptr @"$sSiN")