Skip to content

TBDGen: Specify the correct macCatalyst platform ID in $ld$previous directives #72109

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
merged 2 commits into from
Mar 6, 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
2 changes: 1 addition & 1 deletion include/swift/AST/Attr.h
Original file line number Diff line number Diff line change
Expand Up @@ -1862,8 +1862,8 @@ class OriginallyDefinedInAttr: public DeclAttribute {
struct ActiveVersion {
StringRef ModuleName;
PlatformKind Platform;
bool IsSimulator;
llvm::VersionTuple Version;
bool ForTargetVariant = false;
};

/// Returns non-optional if this attribute is active given the current platform.
Expand Down
3 changes: 1 addition & 2 deletions lib/AST/Attr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2215,7 +2215,6 @@ OriginallyDefinedInAttr::isActivePlatform(const ASTContext &ctx) const {
Result.Version = MovedVersion;
Result.ModuleName = OriginalModuleName;
if (isPlatformActive(Platform, ctx.LangOpts, /*TargetVariant*/false)) {
Result.IsSimulator = ctx.LangOpts.Target.isSimulatorEnvironment();
return Result;
}

Expand All @@ -2224,7 +2223,7 @@ OriginallyDefinedInAttr::isActivePlatform(const ASTContext &ctx) const {
// libraries.
if (ctx.LangOpts.TargetVariant.has_value() &&
isPlatformActive(Platform, ctx.LangOpts, /*TargetVariant*/true)) {
Result.IsSimulator = ctx.LangOpts.TargetVariant->isSimulatorEnvironment();
Result.ForTargetVariant = true;
return Result;
}
return std::nullopt;
Expand Down
54 changes: 30 additions & 24 deletions lib/IRGen/TBDGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,24 +100,23 @@ getAllMovedPlatformVersions(Decl *D) {
return Results;
}

static StringRef getLinkerPlatformName(uint8_t Id) {
static StringRef getLinkerPlatformName(LinkerPlatformId Id) {
switch (Id) {
#define LD_PLATFORM(Name, Id) case Id: return #Name;
#define LD_PLATFORM(Name, Id) case LinkerPlatformId::Name: return #Name;
#include "ldPlatformKinds.def"
default:
llvm_unreachable("unrecognized platform id");
}
llvm_unreachable("unrecognized platform id");
}

static std::optional<uint8_t> getLinkerPlatformId(StringRef Platform) {
return llvm::StringSwitch<std::optional<uint8_t>>(Platform)
#define LD_PLATFORM(Name, Id) .Case(#Name, Id)
static std::optional<LinkerPlatformId> getLinkerPlatformId(StringRef Platform) {
return llvm::StringSwitch<std::optional<LinkerPlatformId>>(Platform)
#define LD_PLATFORM(Name, Id) .Case(#Name, LinkerPlatformId::Name)
#include "ldPlatformKinds.def"
.Default(std::nullopt);
}

StringRef InstallNameStore::getInstallName(LinkerPlatformId Id) const {
auto It = PlatformInstallName.find((uint8_t)Id);
auto It = PlatformInstallName.find(Id);
if (It == PlatformInstallName.end())
return InstallName;
else
Expand All @@ -129,8 +128,9 @@ static std::string getScalaNodeText(Node *N) {
return cast<ScalarNode>(N)->getValue(Buffer).str();
}

static std::set<int8_t> getSequenceNodePlatformList(ASTContext &Ctx, Node *N) {
std::set<int8_t> Results;
static std::set<LinkerPlatformId> getSequenceNodePlatformList(ASTContext &Ctx,
Node *N) {
std::set<LinkerPlatformId> Results;
for (auto &E: *cast<SequenceNode>(N)) {
auto Platform = getScalaNodeText(&E);
auto Id = getLinkerPlatformId(Platform);
Expand Down Expand Up @@ -158,7 +158,7 @@ parseEntry(ASTContext &Ctx,
auto *MN = cast<MappingNode>(&*It);
std::string ModuleName;
std::string InstallName;
std::optional<std::set<int8_t>> Platforms;
std::optional<std::set<LinkerPlatformId>> Platforms;
for (auto &Pair: *MN) {
auto Key = getScalaNodeText(Pair.getKey());
auto* Value = Pair.getValue();
Expand Down Expand Up @@ -233,7 +233,12 @@ TBDGenVisitor::parsePreviousModuleInstallNameMap() {
}

static LinkerPlatformId
getLinkerPlatformId(OriginallyDefinedInAttr::ActiveVersion Ver) {
getLinkerPlatformId(OriginallyDefinedInAttr::ActiveVersion Ver,
ASTContext &Ctx) {
auto target =
Ver.ForTargetVariant ? Ctx.LangOpts.TargetVariant : Ctx.LangOpts.Target;
bool isSimulator = target ? target->isSimulatorEnvironment() : false;

switch(Ver.Platform) {
case swift::PlatformKind::none:
llvm_unreachable("cannot find platform kind");
Expand All @@ -243,16 +248,16 @@ getLinkerPlatformId(OriginallyDefinedInAttr::ActiveVersion Ver) {
llvm_unreachable("not used for this platform");
case swift::PlatformKind::iOS:
case swift::PlatformKind::iOSApplicationExtension:
return Ver.IsSimulator ? LinkerPlatformId::iOS_sim:
LinkerPlatformId::iOS;
if (target && target->isMacCatalystEnvironment())
return LinkerPlatformId::macCatalyst;
return isSimulator ? LinkerPlatformId::iOS_sim : LinkerPlatformId::iOS;
case swift::PlatformKind::tvOS:
case swift::PlatformKind::tvOSApplicationExtension:
return Ver.IsSimulator ? LinkerPlatformId::tvOS_sim:
LinkerPlatformId::tvOS;
return isSimulator ? LinkerPlatformId::tvOS_sim : LinkerPlatformId::tvOS;
case swift::PlatformKind::watchOS:
case swift::PlatformKind::watchOSApplicationExtension:
return Ver.IsSimulator ? LinkerPlatformId::watchOS_sim:
LinkerPlatformId::watchOS;
return isSimulator ? LinkerPlatformId::watchOS_sim
: LinkerPlatformId::watchOS;
case swift::PlatformKind::macOS:
case swift::PlatformKind::macOSApplicationExtension:
return LinkerPlatformId::macOS;
Expand All @@ -264,8 +269,9 @@ getLinkerPlatformId(OriginallyDefinedInAttr::ActiveVersion Ver) {
}

static StringRef
getLinkerPlatformName(OriginallyDefinedInAttr::ActiveVersion Ver) {
return getLinkerPlatformName((uint8_t)getLinkerPlatformId(Ver));
getLinkerPlatformName(OriginallyDefinedInAttr::ActiveVersion Ver,
ASTContext &Ctx) {
return getLinkerPlatformName(getLinkerPlatformId(Ver, Ctx));
}

/// Find the most relevant introducing version of the decl stack we have visited
Expand Down Expand Up @@ -313,17 +319,17 @@ void TBDGenVisitor::addLinkerDirectiveSymbolsLdPrevious(
// so we don't need the linker directives.
if (*IntroVer >= Ver.Version)
continue;
auto PlatformNumber = getLinkerPlatformId(Ver);
auto PlatformNumber = getLinkerPlatformId(Ver, Ctx);
auto It = previousInstallNameMap->find(Ver.ModuleName.str());
if (It == previousInstallNameMap->end()) {
Ctx.Diags.diagnose(SourceLoc(), diag::cannot_find_install_name,
Ver.ModuleName, getLinkerPlatformName(Ver));
Ver.ModuleName, getLinkerPlatformName(Ver, Ctx));
continue;
}
auto InstallName = It->second.getInstallName(PlatformNumber);
if (InstallName.empty()) {
Ctx.Diags.diagnose(SourceLoc(), diag::cannot_find_install_name,
Ver.ModuleName, getLinkerPlatformName(Ver));
Ver.ModuleName, getLinkerPlatformName(Ver, Ctx));
continue;
}
llvm::SmallString<64> Buffer;
Expand All @@ -333,7 +339,7 @@ void TBDGenVisitor::addLinkerDirectiveSymbolsLdPrevious(
OS << "$ld$previous$";
OS << InstallName << "$";
OS << ComptibleVersion << "$";
OS << std::to_string((uint8_t)PlatformNumber) << "$";
OS << std::to_string(static_cast<uint8_t>(PlatformNumber)) << "$";
static auto getMinor = [](std::optional<unsigned> Minor) {
return Minor.has_value() ? *Minor : 0;
};
Expand Down
2 changes: 1 addition & 1 deletion lib/IRGen/TBDGenVisitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ struct InstallNameStore {
std::string InstallName;
// The install name specific to the platform id. This takes precedence over
// the default install name.
std::map<uint8_t, std::string> PlatformInstallName;
std::map<LinkerPlatformId, std::string> PlatformInstallName;
StringRef getInstallName(LinkerPlatformId Id) const;
};

Expand Down
5 changes: 5 additions & 0 deletions test/TBD/Inputs/install-name-map-toasterkit.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,10 @@
"module": "ToasterKit",
"install_name": "/System/Previous/iOS/ToasterKit.dylib",
"platforms": ["iOS", "iOS_sim"]
},
{
"module": "ToasterKit",
"install_name": "/System/Previous/macCatalyst/ToasterKit.dylib",
"platforms": ["macCatalyst"]
}
]
37 changes: 20 additions & 17 deletions test/TBD/linker-directives-ld-previous-macos.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,27 @@

// RUN: %empty-directory(%t)

// RUN: %target-swift-frontend -typecheck %S/Inputs/linker-directive.swift -tbd-is-installapi -emit-tbd -emit-tbd-path %t/linker_directives.tbd -previous-module-installname-map-file %S/Inputs/install-name-map-toasterkit.json -tbd-install_name toasterkit
// RUN: %llvm-nm %t/linker_directives.tbd | %FileCheck %s
// RUN: %llvm-nm %t/linker_directives.tbd | %FileCheck -check-prefix=CHECK-NO-NEW-SYMBOL %s
// RUN: %target-swift-frontend -typecheck %S/Inputs/linker-directive.swift -emit-tbd -emit-tbd-path %t/linker_directives.tbd -previous-module-installname-map-file %S/Inputs/install-name-map-toasterkit.json -tbd-install_name toasterkit
// RUN: %llvm-nm %t/linker_directives.tbd | %FileCheck %s
// RUN: %llvm-nm %t/linker_directives.tbd | %FileCheck -check-prefix=CHECK-NO-NEW-SYMBOL %s
// RUN: %target-swift-frontend -typecheck %S/Inputs/linker-directive.swift -tbd-is-installapi -emit-tbd -emit-tbd-path %t/linker_directives_installapi.tbd -previous-module-installname-map-file %S/Inputs/install-name-map-toasterkit.json -tbd-install_name toasterkit
// RUN: %llvm-nm %t/linker_directives_installapi.tbd | %FileCheck %s --check-prefixes=CHECK,CHECK-MAC --implicit-check-not "System/Previous/macCatalyst"
// RUN: %target-swift-frontend -typecheck %S/Inputs/linker-directive.swift -emit-tbd -emit-tbd-path %t/linker_directives_macos.tbd -previous-module-installname-map-file %S/Inputs/install-name-map-toasterkit.json -tbd-install_name toasterkit
// RUN: %llvm-nm %t/linker_directives_macos.tbd | %FileCheck %s --check-prefixes=CHECK,CHECK-MAC --implicit-check-not "System/Previous/macCatalyst"

// RUN: %target-swift-frontend -target-variant x86_64-apple-ios13.1-macabi -typecheck %S/Inputs/linker-directive.swift -emit-tbd -emit-tbd-path %t/linker_directives.tbd -previous-module-installname-map-file %S/Inputs/install-name-map-toasterkit.json -tbd-install_name toasterkit
// RUN: %llvm-nm %t/linker_directives.tbd | %FileCheck -check-prefix=CHECK-ZIPPERED %s
// RUN: %llvm-nm %t/linker_directives.tbd | %FileCheck -check-prefix=CHECK-NO-NEW-SYMBOL %s
// RUN: %target-swift-frontend -target-variant x86_64-apple-ios13.1-macabi -typecheck %S/Inputs/linker-directive.swift -emit-tbd -emit-tbd-path %t/linker_directives_macos_macabi.tbd -previous-module-installname-map-file %S/Inputs/install-name-map-toasterkit.json -tbd-install_name toasterkit
// RUN: %llvm-nm %t/linker_directives_macos_macabi.tbd | %FileCheck -check-prefixes=CHECK,CHECK-MAC,CHECK-MACCATALYST %s

// CHECK: D $ld$previous$/System/Previous/macOS/ToasterKit.dylib$$1$10.8$10.15$_$s10ToasterKit5toastyyF$
// CHECK: D $ld$previous$/System/Previous/macOS/ToasterKit.dylib$$1$10.8$10.15$_$s10ToasterKit7VehicleV4moveyyF$
// CHECK: D $ld$previous$/System/Previous/macOS/ToasterKit.dylib$$1$10.8$10.15$_$s10ToasterKit7VehicleVMa$
// CHECK: D $ld$previous$/System/Previous/macOS/ToasterKit.dylib$$1$10.8$10.15$_$s10ToasterKit7VehicleVMn$
// CHECK: D $ld$previous$/System/Previous/macOS/ToasterKit.dylib$$1$10.8$10.15$_$s10ToasterKit7VehicleVN$
// RUN: %target-swift-frontend -target x86_64-apple-ios13.1-macabi -typecheck %S/Inputs/linker-directive.swift -emit-tbd -emit-tbd-path %t/linker_directives_macabi.tbd -previous-module-installname-map-file %S/Inputs/install-name-map-toasterkit.json -tbd-install_name toasterkit
// R/UN: %llvm-nm %t/linker_directives_macabi.tbd | %FileCheck -check-prefixes=CHECK,CHECK-MACCATALYST %s --implicit-check-not "System/Previous/macOS"

// CHECK-ZIPPERED: $ld$previous$/System/Previous/iOS/ToasterKit.dylib$$2$10.2$13.0$_$s10ToasterKit5toastyyF$
// CHECK-ZIPPERED: $ld$previous$/System/Previous/macOS/ToasterKit.dylib$$1$10.8$10.15$_$s10ToasterKit5toastyyF$
// CHECK-MACCATALYST: D $ld$previous$/System/Previous/macCatalyst/ToasterKit.dylib$$6$10.2$13.0$_$s10ToasterKit5toastyyF$
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm pretty sure the versions are wrong here and they need to be the iOS versions mapped from the SDKSettings. We should also test that 10.15/13.1 is the earliest version. i.e. something available in 10.14 and moved in 12.0 should make an $ld$previous$ of 13.1$15.0. And something available in 10.14 and moved in 10.15 shouldn't make an $ld$previous$ for Mac Catalyst at all.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

10.2 and 13 come directly from the iOS availability of the declaration (see https://github.com/apple/swift/blob/main/test/TBD/Inputs/linker-directive.swift). So I don't think a remapping is needed. I can see about flooring the versions at 13.1 though.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, I'd like to separate clamping the values to minimum OS versions and potentially skipping emission of directives out into a separate radar/PR. I agree it's the right thing to do but it's also applicable to all platforms and it's unclear to me whether the current behavior is concretely causing any issues. I think it should be treated as a separate bug fix from a scheduling and risk perspective.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh you're right, I saw "10.something" and got mixed up.

// CHECK-MACCATALYST: D $ld$previous$/System/Previous/macCatalyst/ToasterKit.dylib$$6$10.2$13.0$_$s10ToasterKit7VehicleV4moveyyF$
// CHECK-MACCATALYST: D $ld$previous$/System/Previous/macCatalyst/ToasterKit.dylib$$6$10.2$13.0$_$s10ToasterKit7VehicleVMa$
// CHECK-MACCATALYST: D $ld$previous$/System/Previous/macCatalyst/ToasterKit.dylib$$6$10.2$13.0$_$s10ToasterKit7VehicleVMn$
// CHECK-MACCATALYST: D $ld$previous$/System/Previous/macCatalyst/ToasterKit.dylib$$6$10.2$13.0$_$s10ToasterKit7VehicleVN$

// CHECK-NO-NEW-SYMBOL-NOT: $_$s10ToasterKit7VehicleV32originallyDefinedInCurrentModuleyyF
// CHECK-MAC: D $ld$previous$/System/Previous/macOS/ToasterKit.dylib$$1$10.8$10.15$_$s10ToasterKit5toastyyF$
// CHECK-MAC: D $ld$previous$/System/Previous/macOS/ToasterKit.dylib$$1$10.8$10.15$_$s10ToasterKit7VehicleV4moveyyF$
// CHECK-MAC: D $ld$previous$/System/Previous/macOS/ToasterKit.dylib$$1$10.8$10.15$_$s10ToasterKit7VehicleVMa$
// CHECK-MAC: D $ld$previous$/System/Previous/macOS/ToasterKit.dylib$$1$10.8$10.15$_$s10ToasterKit7VehicleVMn$
// CHECK-MAC: D $ld$previous$/System/Previous/macOS/ToasterKit.dylib$$1$10.8$10.15$_$s10ToasterKit7VehicleVN$

// CHECK-NOT: $_$s10ToasterKit7VehicleV32originallyDefinedInCurrentModuleyyF