Skip to content

[IRGen] De-duplicate implementations of minimum OS versions supporting mangling. #38986

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

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
5 changes: 5 additions & 0 deletions include/swift/AST/ASTContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -784,6 +784,11 @@ class ASTContext final {
/// compiler for the target platform.
AvailabilityContext getSwift56Availability();

// Note: Update this function if you add a new getSwiftXYAvailability above.
/// Get the runtime availability for a particular version of Swift (5.0+).
AvailabilityContext
getSwift5PlusAvailability(llvm::VersionTuple swiftVersion);

/// Get the runtime availability of features that have been introduced in the
/// Swift compiler for future versions of the target platform.
AvailabilityContext getSwiftFutureAvailability();
Expand Down
18 changes: 18 additions & 0 deletions lib/AST/Availability.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -457,3 +457,21 @@ AvailabilityContext ASTContext::getSwiftFutureAvailability() {
return AvailabilityContext::alwaysAvailable();
}
}

AvailabilityContext
ASTContext::getSwift5PlusAvailability(llvm::VersionTuple swiftVersion) {
if (swiftVersion.getMajor() == 5) {
switch (*swiftVersion.getMinor()) {
case 0: return getSwift50Availability();
case 1: return getSwift51Availability();
case 2: return getSwift52Availability();
case 3: return getSwift53Availability();
case 4: return getSwift54Availability();
case 5: return getSwift55Availability();
case 6: return getSwift56Availability();
default: break;
}
}
llvm::report_fatal_error("Missing call to getSwiftXYAvailability for Swift " +
swiftVersion.getAsString());
}
18 changes: 8 additions & 10 deletions lib/IRGen/GenReflection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -172,11 +172,8 @@ class PrintMetadataSource
}
};

// Return the minimum Swift runtime version that supports demangling a given
// type.
static llvm::VersionTuple
getRuntimeVersionThatSupportsDemanglingType(IRGenModule &IGM,
CanType type) {
Optional<llvm::VersionTuple>
swift::irgen::getRuntimeVersionThatSupportsDemanglingType(CanType type) {
// Associated types of opaque types weren't mangled in a usable form by the
// Swift 5.1 runtime, so we needed to add a new mangling in 5.2.
if (type->hasOpaqueArchetype()) {
Expand All @@ -194,8 +191,7 @@ getRuntimeVersionThatSupportsDemanglingType(IRGenModule &IGM,
// guards, so we don't need to limit availability of mangled names
// involving them.
}

return llvm::VersionTuple(5, 0);
return None;
}

// Produce a fallback mangled type name that uses an open-coded callback
Expand Down Expand Up @@ -289,9 +285,11 @@ getTypeRefImpl(IRGenModule &IGM,
// symbolic reference with a callback function.
if (auto runtimeCompatVersion = getSwiftRuntimeCompatibilityVersionForTarget
(IGM.Context.LangOpts.Target)) {
if (*runtimeCompatVersion <
getRuntimeVersionThatSupportsDemanglingType(IGM, type)) {
return getTypeRefByFunction(IGM, sig, type);
if (auto minimumSupportedRuntimeVersion =
getRuntimeVersionThatSupportsDemanglingType(type)) {
if (*runtimeCompatVersion < *minimumSupportedRuntimeVersion) {
return getTypeRefByFunction(IGM, sig, type);
}
}
}

Expand Down
5 changes: 5 additions & 0 deletions lib/IRGen/IRGenMangler.h
Original file line number Diff line number Diff line change
Expand Up @@ -650,6 +650,11 @@ class IRGenMangler : public Mangle::ASTMangler {
}
};

/// Does this type require a special minimum Swift runtime version which
/// supports demangling it?
Optional<llvm::VersionTuple>
getRuntimeVersionThatSupportsDemanglingType(CanType type);

} // end namespace irgen
} // end namespace swift

Expand Down
16 changes: 4 additions & 12 deletions lib/IRGen/MetadataRequest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2487,18 +2487,10 @@ static bool shouldAccessByMangledName(IRGenModule &IGM, CanType type) {
}

// The Swift 5.1 runtime fails to demangle associated types of opaque types.
if (!IGM.getAvailabilityContext().isContainedIn(IGM.Context.getSwift52Availability())) {
auto hasNestedOpaqueArchetype = type.findIf([](CanType sub) -> bool {
if (auto archetype = dyn_cast<NestedArchetypeType>(sub)) {
if (isa<OpaqueTypeArchetypeType>(archetype->getRoot())) {
return true;
}
}
return false;
});

if (hasNestedOpaqueArchetype)
return false;
if (auto minimumSwiftVersion =
getRuntimeVersionThatSupportsDemanglingType(type)) {
return IGM.getAvailabilityContext().isContainedIn(
IGM.Context.getSwift5PlusAvailability(*minimumSwiftVersion));
}

return true;
Expand Down