From 3fcca83d0732f51bf9f08b52b11de6feb25f613d Mon Sep 17 00:00:00 2001 From: Evan Wilde Date: Thu, 7 Aug 2025 14:21:38 -0700 Subject: [PATCH] Refactor getMinPlatformVersion The definitions of how version numbers were extracted from target triples split between the minimum platform version and for determining the minimum inlining version. This resulted in inlinable and transparent functions not being imported correctly on non-Apple platforms where the version number is retained as part of the target triple. Specifically, `_checkExpectedExecutor` was found in the module, but didn't have the appropriate availability version assigned, resulting in it failing to import and the compiler silently omitting the check in SILGen when compiling for FreeBSD. This patch refactors the implementation of `getMinPlatformVersion` into a separate function that is used in both places so that they cannot get out of sync again. Note: This changes how Windows is handled. getMinPlatformVersion returned an empty version number for Windows, while the availability implementation returned the OS version number. This makes both consistently return the OS version number. --- include/swift/Basic/LangOptions.h | 14 ++------------ include/swift/Basic/Platform.h | 3 +++ lib/Basic/Platform.cpp | 17 +++++++++++++++++ lib/Frontend/CompilerInvocation.cpp | 13 +------------ 4 files changed, 23 insertions(+), 24 deletions(-) diff --git a/include/swift/Basic/LangOptions.h b/include/swift/Basic/LangOptions.h index 398e8e60fcc02..d1bc0292821f0 100644 --- a/include/swift/Basic/LangOptions.h +++ b/include/swift/Basic/LangOptions.h @@ -22,6 +22,7 @@ #include "swift/Basic/Feature.h" #include "swift/Basic/FunctionBodySkipping.h" #include "swift/Basic/LLVM.h" +#include "swift/Basic/Platform.h" #include "swift/Basic/PlaygroundOption.h" #include "swift/Basic/Version.h" #include "swift/Config.h" @@ -693,18 +694,7 @@ namespace swift { /// This is only implemented on certain OSs. If no target has been /// configured, returns v0.0.0. llvm::VersionTuple getMinPlatformVersion() const { - if (Target.isMacOSX()) { - llvm::VersionTuple OSVersion; - Target.getMacOSXVersion(OSVersion); - return OSVersion; - } else if (Target.isiOS()) { - return Target.getiOSVersion(); - } else if (Target.isWatchOS()) { - return Target.getOSVersion(); - } else if (Target.isXROS()) { - return Target.getOSVersion(); - } - return llvm::VersionTuple(/*Major=*/0, /*Minor=*/0, /*Subminor=*/0); + return getVersionForTriple(Target); } /// Sets an implicit platform condition. diff --git a/include/swift/Basic/Platform.h b/include/swift/Basic/Platform.h index 61337e45c4404..bf40edad76b33 100644 --- a/include/swift/Basic/Platform.h +++ b/include/swift/Basic/Platform.h @@ -85,6 +85,9 @@ namespace swift { /// returned. StringRef getPlatformNameForTriple(const llvm::Triple &triple); + /// Returns the version tuple for a given target triple + llvm::VersionTuple getVersionForTriple(const llvm::Triple &triple); + /// Returns the platform Kind for Darwin triples. DarwinPlatformKind getDarwinPlatformKind(const llvm::Triple &triple); diff --git a/lib/Basic/Platform.cpp b/lib/Basic/Platform.cpp index 40959a1b95b25..fa670ea95f6c3 100644 --- a/lib/Basic/Platform.cpp +++ b/lib/Basic/Platform.cpp @@ -278,6 +278,23 @@ StringRef swift::getPlatformNameForTriple(const llvm::Triple &triple) { llvm_unreachable("unsupported OS"); } +llvm::VersionTuple swift::getVersionForTriple(const llvm::Triple &triple) { + if (triple.isMacOSX()) { + llvm::VersionTuple OSVersion; + triple.getMacOSXVersion(OSVersion); + return OSVersion; + } else if (triple.isiOS()) { + return triple.getiOSVersion(); + } else if (triple.isWatchOS()) { + return triple.getOSVersion(); + } else if (triple.isXROS()) { + return triple.getOSVersion(); + } else if (triple.isOSWindows()) { + return triple.getOSVersion(); + } + return llvm::VersionTuple(/*Major=*/0, /*Minor=*/0, /*Subminor=*/0); +} + StringRef swift::getMajorArchitectureName(const llvm::Triple &Triple) { if (Triple.isOSLinux()) { switch (Triple.getSubArch()) { diff --git a/lib/Frontend/CompilerInvocation.cpp b/lib/Frontend/CompilerInvocation.cpp index 130b614943174..e4d91285b4458 100644 --- a/lib/Frontend/CompilerInvocation.cpp +++ b/lib/Frontend/CompilerInvocation.cpp @@ -56,17 +56,6 @@ swift::CompilerInvocation::CompilerInvocation() { setTargetTriple(llvm::sys::getDefaultTargetTriple()); } -/// Converts a llvm::Triple to a llvm::VersionTuple. -static llvm::VersionTuple -getVersionTuple(const llvm::Triple &triple) { - if (triple.isMacOSX()) { - llvm::VersionTuple OSVersion; - triple.getMacOSXVersion(OSVersion); - return OSVersion; - } - return triple.getOSVersion(); -} - void CompilerInvocation::computeRuntimeResourcePathFromExecutablePath( StringRef mainExecutablePath, bool shared, llvm::SmallVectorImpl &runtimeResourcePath) { @@ -1623,7 +1612,7 @@ static bool ParseLangArgs(LangOptions &Opts, ArgList &Args, // First, set up default minimum inlining target versions. auto getDefaultMinimumInliningTargetVersion = [&](const llvm::Triple &triple) -> llvm::VersionTuple { - const auto targetVersion = getVersionTuple(triple); + const auto targetVersion = getVersionForTriple(triple); // In API modules, default to the version when Swift first became available. if (Opts.LibraryLevel == LibraryLevel::API) {