Skip to content

Commit b5f209d

Browse files
authored
Merge pull request #61169 from tshortli/back-deploy-attr-weak-linkage
2 parents d3a710a + 6b61e37 commit b5f209d

16 files changed

+86
-34
lines changed

include/swift/AST/Attr.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2221,6 +2221,9 @@ class BackDeployAttr: public DeclAttribute {
22212221
/// The earliest platform version that may use the back deployed implementation.
22222222
const llvm::VersionTuple Version;
22232223

2224+
/// Returns true if this attribute is active given the current platform.
2225+
bool isActivePlatform(const ASTContext &ctx) const;
2226+
22242227
static bool classof(const DeclAttribute *DA) {
22252228
return DA->getKind() == DAK_BackDeploy;
22262229
}

include/swift/AST/Decl.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -837,7 +837,7 @@ class alignas(1 << DeclAlignInBits) Decl : public ASTAllocated<Decl> {
837837
/// Returns the OS version in which the decl became ABI as specified by the
838838
/// @_backDeploy attribute.
839839
Optional<llvm::VersionTuple>
840-
getBackDeployBeforeOSVersion(PlatformKind Kind) const;
840+
getBackDeployBeforeOSVersion(ASTContext &Ctx) const;
841841

842842
/// Returns the starting location of the entire declaration.
843843
SourceLoc getStartLoc() const { return getSourceRange().Start; }

include/swift/SIL/SILDeclRef.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,12 @@
1919
#ifndef SWIFT_SIL_SILDeclRef_H
2020
#define SWIFT_SIL_SILDeclRef_H
2121

22+
#include "swift/AST/Availability.h"
2223
#include "swift/AST/ClangNode.h"
2324
#include "swift/AST/GenericSignature.h"
2425
#include "swift/AST/TypeAlignments.h"
25-
#include "llvm/ADT/Hashing.h"
2626
#include "llvm/ADT/DenseMap.h"
27+
#include "llvm/ADT/Hashing.h"
2728
#include "llvm/ADT/PointerUnion.h"
2829
#include "llvm/Support/PrettyStackTrace.h"
2930

@@ -530,6 +531,9 @@ struct SILDeclRef {
530531
static AbstractFunctionDecl *getOverriddenWitnessTableEntry(
531532
AbstractFunctionDecl *func);
532533

534+
/// Returns the availability of the decl for computing linkage.
535+
Optional<AvailabilityContext> getAvailabilityForLinkage() const;
536+
533537
/// True if the referenced entity is some kind of thunk.
534538
bool isThunk() const;
535539

lib/AST/Attr.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1695,6 +1695,10 @@ bool AvailableAttr::isActivePlatform(const ASTContext &ctx) const {
16951695
return isPlatformActive(Platform, ctx.LangOpts);
16961696
}
16971697

1698+
bool BackDeployAttr::isActivePlatform(const ASTContext &ctx) const {
1699+
return isPlatformActive(Platform, ctx.LangOpts);
1700+
}
1701+
16981702
AvailableAttr *AvailableAttr::clone(ASTContext &C, bool implicit) const {
16991703
return new (C) AvailableAttr(implicit ? SourceLoc() : AtLoc,
17001704
implicit ? SourceRange() : getRange(),

lib/AST/Decl.cpp

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -382,18 +382,19 @@ Decl::getIntroducedOSVersion(PlatformKind Kind) const {
382382
}
383383

384384
Optional<llvm::VersionTuple>
385-
Decl::getBackDeployBeforeOSVersion(PlatformKind Kind) const {
385+
Decl::getBackDeployBeforeOSVersion(ASTContext &Ctx) const {
386386
for (auto *attr : getAttrs()) {
387387
if (auto *backDeployAttr = dyn_cast<BackDeployAttr>(attr)) {
388-
if (backDeployAttr->Platform == Kind && backDeployAttr->Version) {
388+
if (backDeployAttr->isActivePlatform(Ctx) && backDeployAttr->Version) {
389389
return backDeployAttr->Version;
390390
}
391391
}
392392
}
393393

394394
// Accessors may inherit `@_backDeploy`.
395395
if (getKind() == DeclKind::Accessor) {
396-
return cast<AccessorDecl>(this)->getStorage()->getBackDeployBeforeOSVersion(Kind);
396+
return cast<AccessorDecl>(this)->getStorage()->getBackDeployBeforeOSVersion(
397+
Ctx);
397398
}
398399

399400
return None;
@@ -937,14 +938,20 @@ bool Decl::isStdlibDecl() const {
937938
}
938939

939940
AvailabilityContext Decl::getAvailabilityForLinkage() const {
941+
ASTContext &ctx = getASTContext();
942+
943+
// When computing availability for linkage, use the "before" version from
944+
// the @_backDeploy attribute, if present.
945+
if (auto backDeployVersion = getBackDeployBeforeOSVersion(ctx))
946+
return AvailabilityContext{VersionRange::allGTE(*backDeployVersion)};
947+
940948
auto containingContext =
941949
AvailabilityInference::annotatedAvailableRange(this, getASTContext());
942950
if (containingContext.hasValue()) {
943-
// If this entity comes from the concurrency module, adjust it's
951+
// If this entity comes from the concurrency module, adjust its
944952
// availability for linkage purposes up to Swift 5.5, so that we use
945953
// weak references any time we reference those symbols when back-deploying
946954
// concurrency.
947-
ASTContext &ctx = getASTContext();
948955
if (getModuleContext()->getName() == ctx.Id_Concurrency) {
949956
containingContext->intersectWith(ctx.getConcurrencyAvailability());
950957
}

lib/SIL/IR/SILDeclRef.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,16 @@ ASTContext &SILDeclRef::getASTContext() const {
207207
llvm_unreachable("Unhandled case in switch");
208208
}
209209

210+
Optional<AvailabilityContext> SILDeclRef::getAvailabilityForLinkage() const {
211+
// Back deployment thunks and fallbacks don't have availability since they
212+
// are non-ABI.
213+
// FIXME: Generalize this check to all kinds of non-ABI functions.
214+
if (backDeploymentKind != SILDeclRef::BackDeploymentKind::None)
215+
return None;
216+
217+
return getDecl()->getAvailabilityForLinkage();
218+
}
219+
210220
bool SILDeclRef::isThunk() const {
211221
return isForeignToNativeThunk() || isNativeToForeignThunk() ||
212222
isDistributedThunk() || isBackDeploymentThunk();

lib/SIL/IR/SILFunctionBuilder.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,9 @@ SILFunction *SILFunctionBuilder::getOrCreateFunction(
311311
if (constant.isForeign && decl->hasClangNode())
312312
F->setClangNodeOwner(decl);
313313

314-
F->setAvailabilityForLinkage(decl->getAvailabilityForLinkage());
314+
if (auto availability = constant.getAvailabilityForLinkage())
315+
F->setAvailabilityForLinkage(*availability);
316+
315317
F->setIsAlwaysWeakImported(decl->isAlwaysWeakImported());
316318

317319
if (auto *accessor = dyn_cast<AccessorDecl>(decl)) {

lib/SILGen/SILGenBackDeploy.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,7 @@ static void emitBackDeployIfAvailableCondition(SILGenFunction &SGF,
5252
SILLocation loc,
5353
SILBasicBlock *availableBB,
5454
SILBasicBlock *unavailableBB) {
55-
PlatformKind platform = targetPlatform(SGF.SGM.getASTContext().LangOpts);
56-
auto version = AFD->getBackDeployBeforeOSVersion(platform);
55+
auto version = AFD->getBackDeployBeforeOSVersion(SGF.SGM.getASTContext());
5756
VersionRange OSVersion = VersionRange::empty();
5857
if (version.hasValue()) {
5958
OSVersion = VersionRange::allGTE(*version);
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: split-file %s %t
3+
// RUN: %target-swift-frontend -emit-module -emit-module-path %t/Library.swiftmodule -parse-as-library %t/Library.swift -enable-library-evolution
4+
5+
// RUN: %target-swift-frontend -primary-file %t/Client.swift -I %t -emit-ir -target %target-cpu-apple-macosx10.50 | %FileCheck %t/Client.swift --check-prefix=CHECK-OLD
6+
// RUN: %target-swift-frontend -primary-file %t/Client.swift -I %t -emit-ir -target %target-cpu-apple-macosx10.60 | %FileCheck %t/Client.swift --check-prefix=CHECK-NEW
7+
8+
// REQUIRES: OS=macosx
9+
10+
//--- Library.swift
11+
12+
@available(macOS 10.50, *)
13+
@_backDeploy(before: macOS 10.60)
14+
public func backDeployedFunc() {}
15+
16+
//--- Client.swift
17+
18+
import Library
19+
20+
// CHECK-OLD: declare extern_weak {{.*}} void @"$s7Library16backDeployedFuncyyF"()
21+
// CHECK-NEW: declare {{.*}} void @"$s7Library16backDeployedFuncyyF"()
22+
backDeployedFunc()

test/SILGen/back_deploy_attribute_accessor.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@
88
@available(macOS 10.50, *)
99
public struct TopLevelStruct {
1010
// -- Fallback definition for TopLevelStruct.property.getter
11-
// CHECK-LABEL: sil non_abi [serialized] [available 10.51] [ossa] @$s11back_deploy14TopLevelStructV8propertyACvgTwB : $@convention(method) (TopLevelStruct) -> TopLevelStruct
11+
// CHECK-LABEL: sil non_abi [serialized] [ossa] @$s11back_deploy14TopLevelStructV8propertyACvgTwB : $@convention(method) (TopLevelStruct) -> TopLevelStruct
1212
// CHECK: bb0([[SELF:%.*]] : $TopLevelStruct):
1313
// CHECK: return [[SELF]] : $TopLevelStruct
1414

1515
// -- Back deployment thunk for TopLevelStruct.property.getter
16-
// CHECK-LABEL: sil non_abi [serialized] [thunk] [available 10.51] [ossa] @$s11back_deploy14TopLevelStructV8propertyACvgTwb : $@convention(method) (TopLevelStruct) -> TopLevelStruct
16+
// CHECK-LABEL: sil non_abi [serialized] [thunk] [ossa] @$s11back_deploy14TopLevelStructV8propertyACvgTwb : $@convention(method) (TopLevelStruct) -> TopLevelStruct
1717
// CHECK: bb0([[BB0_ARG:%.*]] : $TopLevelStruct):
1818
// CHECK: [[MAJOR:%.*]] = integer_literal $Builtin.Word, 10
1919
// CHECK: [[MINOR:%.*]] = integer_literal $Builtin.Word, 52
@@ -36,7 +36,7 @@ public struct TopLevelStruct {
3636
// CHECK: return [[RETURN_BB_ARG]] : $TopLevelStruct
3737

3838
// -- Original definition of TopLevelStruct.property.getter
39-
// CHECK-LABEL: sil [available 10.51] [ossa] @$s11back_deploy14TopLevelStructV8propertyACvg : $@convention(method) (TopLevelStruct) -> TopLevelStruct
39+
// CHECK-LABEL: sil [available 10.52] [ossa] @$s11back_deploy14TopLevelStructV8propertyACvg : $@convention(method) (TopLevelStruct) -> TopLevelStruct
4040
@available(macOS 10.51, *)
4141
@_backDeploy(before: macOS 10.52)
4242
public var property: TopLevelStruct { self }

0 commit comments

Comments
 (0)