Skip to content

IRGen: Use @_weakLinked to test backward deployment of resilient protocols #20173

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 1 commit into from
Nov 1, 2018
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: 2 additions & 3 deletions include/swift/AST/Attr.def
Original file line number Diff line number Diff line change
Expand Up @@ -359,9 +359,8 @@ DECL_ATTR(_clangImporterSynthesizedType, ClangImporterSynthesizedType,
LongAttribute | RejectByParser | UserInaccessible |
NotSerialized, 74)
SIMPLE_DECL_ATTR(_weakLinked, WeakLinked,
OnNominalType | OnFunc | OnAccessor | OnVar | OnSubscript | OnConstructor |
OnEnumElement |
UserInaccessible,
OnNominalType | OnAssociatedType | OnFunc | OnAccessor | OnVar |
OnSubscript | OnConstructor | OnEnumElement | UserInaccessible,
75)
SIMPLE_DECL_ATTR(_frozen, Frozen,
OnEnum |
Expand Down
18 changes: 1 addition & 17 deletions include/swift/IRGen/Linking.h
Original file line number Diff line number Diff line change
Expand Up @@ -919,23 +919,7 @@ class LinkEntity {
}

/// Determine whether this entity will be weak-imported.
bool isWeakImported(ModuleDecl *module) const {
if (getKind() == Kind::SILGlobalVariable &&
getSILGlobalVariable()->getDecl())
return getSILGlobalVariable()->getDecl()->isWeakImported(module);

if (getKind() == Kind::SILFunction) {
if (auto clangOwner = getSILFunction()->getClangNodeOwner())
return clangOwner->isWeakImported(module);
if (getSILFunction()->isWeakLinked())
return getSILFunction()->isAvailableExternally();
}

if (!isDeclKind(getKind()))
return false;

return getDecl()->isWeakImported(module);
}
bool isWeakImported(ModuleDecl *module) const;

/// Return the source file whose codegen should trigger emission of this
/// link entity, if one can be identified.
Expand Down
6 changes: 6 additions & 0 deletions lib/AST/Decl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,12 @@ bool Decl::isWeakImported(ModuleDecl *fromModule) const {
if (getAttrs().hasAttribute<WeakLinkedAttr>())
return true;

if (auto *accessor = dyn_cast<AccessorDecl>(this))
return accessor->getStorage()->isWeakImported(fromModule);

if (auto *dtor = dyn_cast<DestructorDecl>(this))
return cast<ClassDecl>(dtor->getDeclContext())->isWeakImported(fromModule);
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think we need this one. It should be covered by availability when we implement that, but otherwise you'd have to apply this rule to every member of every decl.


// FIXME: Also check availability when containingModule is resilient.
return false;
}
Expand Down
88 changes: 88 additions & 0 deletions lib/IRGen/Linking.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -763,6 +763,94 @@ Alignment LinkEntity::getAlignment(IRGenModule &IGM) const {
}
}

bool LinkEntity::isWeakImported(ModuleDecl *module) const {
switch (getKind()) {
case Kind::SILGlobalVariable:
if (getSILGlobalVariable()->getDecl())
return getSILGlobalVariable()->getDecl()->isWeakImported(module);
return false;

case Kind::SILFunction: {
// For imported functions check the Clang declaration.
if (auto clangOwner = getSILFunction()->getClangNodeOwner())
return clangOwner->isWeakImported(module);

// For native functions check a flag on the SILFunction
// itself.
if (getSILFunction()->isWeakLinked())
return getSILFunction()->isAvailableExternally();
return false;
}

case Kind::AssociatedConformanceDescriptor:
case Kind::DefaultAssociatedConformanceAccessor: {
// Associated conformance descriptors use the protocol as
// their declaration, but are weak linked if the associated
// type stored in extra storage area is weak linked.
auto assocConformance = getAssociatedConformance();
auto *depMemTy = assocConformance.first->castTo<DependentMemberType>();
return depMemTy->getAssocType()->isWeakImported(module);
}

case Kind::TypeMetadata:
case Kind::TypeMetadataAccessFunction: {
if (auto *nominalDecl = getType()->getAnyNominal())
return nominalDecl->isWeakImported(module);
return false;
}

case Kind::DispatchThunk:
case Kind::DispatchThunkInitializer:
case Kind::DispatchThunkAllocator:
case Kind::MethodDescriptor:
case Kind::MethodDescriptorInitializer:
case Kind::MethodDescriptorAllocator:
case Kind::MethodLookupFunction:
case Kind::EnumCase:
case Kind::FieldOffset:
case Kind::ObjCClass:
case Kind::ObjCClassRef:
case Kind::ObjCMetaclass:
case Kind::SwiftMetaclassStub:
case Kind::ObjCMetadataUpdateFunction:
case Kind::ClassMetadataBaseOffset:
case Kind::PropertyDescriptor:
case Kind::NominalTypeDescriptor:
case Kind::ModuleDescriptor:
case Kind::ProtocolDescriptor:
case Kind::ProtocolRequirementsBaseDescriptor:
case Kind::AssociatedTypeDescriptor:
return getDecl()->isWeakImported(module);

// TODO: Revisit some of the below, for weak conformances.
case Kind::TypeMetadataPattern:
case Kind::TypeMetadataInstantiationCache:
case Kind::TypeMetadataInstantiationFunction:
case Kind::TypeMetadataSingletonInitializationCache:
case Kind::TypeMetadataCompletionFunction:
case Kind::ExtensionDescriptor:
case Kind::AnonymousDescriptor:
case Kind::DirectProtocolWitnessTable:
case Kind::ProtocolWitnessTablePattern:
case Kind::GenericProtocolWitnessTableInstantiationFunction:
case Kind::AssociatedTypeWitnessTableAccessFunction:
case Kind::ReflectionAssociatedTypeDescriptor:
case Kind::ProtocolConformanceDescriptor:
case Kind::ProtocolWitnessTableLazyAccessFunction:
case Kind::ProtocolWitnessTableLazyCacheVariable:
case Kind::ValueWitness:
case Kind::ValueWitnessTable:
case Kind::TypeMetadataLazyCacheVariable:
case Kind::ForeignTypeMetadataCandidate:
case Kind::ReflectionBuiltinDescriptor:
case Kind::ReflectionFieldDescriptor:
case Kind::CoroutineContinuationPrototype:
return false;
}

llvm_unreachable("Bad link entity kind");
}

const SourceFile *LinkEntity::getSourceFileForEmission() const {
const SourceFile *sf;

Expand Down
6 changes: 3 additions & 3 deletions lib/SIL/SILFunctionBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,6 @@ static void addFunctionAttributes(SILFunction *F, DeclAttributes &Attrs,
// @_silgen_name and @_cdecl functions may be called from C code somewhere.
if (Attrs.hasAttribute<SILGenNameAttr>() || Attrs.hasAttribute<CDeclAttr>())
F->setHasCReferences(true);

if (Attrs.hasAttribute<WeakLinkedAttr>())
F->setWeakLinked();
}

SILFunction *
Expand Down Expand Up @@ -115,6 +112,9 @@ SILFunctionBuilder::getOrCreateFunction(SILLocation loc, SILDeclRef constant,
if (constant.isForeign && decl->hasClangNode())
F->setClangNodeOwner(decl);

if (decl->isWeakImported(/*fromModule=*/nullptr))
F->setWeakLinked();

if (auto *accessor = dyn_cast<AccessorDecl>(decl)) {
auto *storage = accessor->getStorage();
// Add attributes for e.g. computed properties.
Expand Down
12 changes: 12 additions & 0 deletions test/IRGen/Inputs/weak_import_native_helper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,15 @@ public enum GenericE<T> {}

@_weakLinked
open class GenericC<T> {}

public protocol OtherProtocol {}
public struct ConcreteType : OtherProtocol {}

public protocol ProtocolWithWeakMembers {
@_weakLinked associatedtype T : OtherProtocol = ConcreteType
@_weakLinked func f()
}

extension ProtocolWithWeakMembers {
@_weakLinked public func f() {}
}
67 changes: 36 additions & 31 deletions test/IRGen/weak_import_native.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@

import weak_import_native_helper

// CHECK-DAG: @"$s25weak_import_native_helper23ProtocolWithWeakMembersP1TAC_AA05OtherE0Tn" = extern_weak global %swift.protocol_requirement
// CHECK-DAG: @"$s1T25weak_import_native_helper23ProtocolWithWeakMembersPTl" = extern_weak global %swift.protocol_requirement
// CHECK-DAG: @"$s25weak_import_native_helper23ProtocolWithWeakMembersP1fyyFTq" = extern_weak global %swift.method_descriptor
// CHECK-DAG: declare extern_weak swiftcc void @"$s25weak_import_native_helper23ProtocolWithWeakMembersPAAE1fyyF"(%swift.type*, i8**, %swift.opaque* noalias nocapture swiftself)
struct ConformsToProtocolWithWeakMembers : ProtocolWithWeakMembers {}

func testTopLevel() {
// CHECK-DAG: declare extern_weak {{.+}} @"$s25weak_import_native_helper2fnyyF"()
fn()
Expand Down Expand Up @@ -52,10 +58,16 @@ func testStruct() {
}

func testEnum() {
// FIXME: We're still referencing tags directly.
// CHECK-DAG: @"$s25weak_import_native_helper1EO6strongyA2CmFWC" = external constant i32
_ = E.strong

// CHECK-DAG: @"$s25weak_import_native_helper1EO0A0yA2CmFWC" = extern_weak constant i32
_ = E.weak

// CHECK-DAG: @"$s25weak_import_native_helper1EO11strongAssocyACSicACmFWC" = external constant i32
_ = E.strongAssoc(0)

// CHECK-DAG: @"$s25weak_import_native_helper1EO0A5AssocyACSicACmFWC" = extern_weak constant i32
_ = E.weakAssoc(0)
}

Expand All @@ -66,25 +78,23 @@ func testClass() {
// CHECK-DAG: declare extern_weak {{.+}} @"$s25weak_import_native_helper1CC2fnyyFTj"
c.fn()

// FIXME: vtable dispatch functions for accessors should also be weak

// CHECK-DAG: declare swiftcc {{.+}} @"$s25weak_import_native_helper1CC10storedPropSivgTj"
// CHECK-DAG: declare swiftcc {{.+}} @"$s25weak_import_native_helper1CC10storedPropSivsTj"
// CHECK-DAG: declare swiftcc {{.+}} @"$s25weak_import_native_helper1CC10storedPropSivMTj"
// CHECK-DAG: declare extern_weak swiftcc {{.+}} @"$s25weak_import_native_helper1CC10storedPropSivgTj"
// CHECK-DAG: declare extern_weak swiftcc {{.+}} @"$s25weak_import_native_helper1CC10storedPropSivsTj"
// CHECK-DAG: declare extern_weak swiftcc {{.+}} @"$s25weak_import_native_helper1CC10storedPropSivMTj"
let x = c.storedProp
c.storedProp = x
c.storedProp += 1

// CHECK-DAG: declare swiftcc {{.+}} @"$s25weak_import_native_helper1CC12computedPropSivgTj"
// CHECK-DAG: declare swiftcc {{.+}} @"$s25weak_import_native_helper1CC12computedPropSivsTj"
// CHECK-DAG: declare swiftcc {{.+}} @"$s25weak_import_native_helper1CC12computedPropSivMTj"
// CHECK-DAG: declare extern_weak swiftcc {{.+}} @"$s25weak_import_native_helper1CC12computedPropSivgTj"
// CHECK-DAG: declare extern_weak swiftcc {{.+}} @"$s25weak_import_native_helper1CC12computedPropSivsTj"
// CHECK-DAG: declare extern_weak swiftcc {{.+}} @"$s25weak_import_native_helper1CC12computedPropSivMTj"
let y = c.computedProp
c.computedProp = y
c.computedProp += 1

// CHECK-DAG: declare swiftcc {{.+}} @"$s25weak_import_native_helper1CCyS2icigTj"
// CHECK-DAG: declare swiftcc {{.+}} @"$s25weak_import_native_helper1CCyS2icisTj"
// CHECK-DAG: declare swiftcc {{.+}} @"$s25weak_import_native_helper1CCyS2iciMTj"
// CHECK-DAG: declare extern_weak swiftcc {{.+}} @"$s25weak_import_native_helper1CCyS2icigTj"
// CHECK-DAG: declare extern_weak swiftcc {{.+}} @"$s25weak_import_native_helper1CCyS2icisTj"
// CHECK-DAG: declare extern_weak swiftcc {{.+}} @"$s25weak_import_native_helper1CCyS2iciMTj"
let z = c[0]
c[0] = z
c[0] += 1
Expand All @@ -104,18 +114,16 @@ func testProtocolExistential(_ p: P) {
// CHECK-DAG: declare extern_weak {{.+}} @"$s25weak_import_native_helper1PP2fnyyFTj"
p.fn()

// FIXME: witness table dispatch functions for accessors should also be weak

// CHECK-DAG: declare swiftcc {{.+}} @"$s25weak_import_native_helper1PP4propSivgTj"
// CHECK-DAG: declare swiftcc {{.+}} @"$s25weak_import_native_helper1PP4propSivsTj"
// CHECK-DAG: declare swiftcc {{.+}} @"$s25weak_import_native_helper1PP4propSivMTj"
// CHECK-DAG: declare extern_weak swiftcc {{.+}} @"$s25weak_import_native_helper1PP4propSivgTj"
// CHECK-DAG: declare extern_weak swiftcc {{.+}} @"$s25weak_import_native_helper1PP4propSivsTj"
// CHECK-DAG: declare extern_weak swiftcc {{.+}} @"$s25weak_import_native_helper1PP4propSivMTj"
let x = p.prop
mutP.prop = x
mutP.prop += 1

// CHECK-DAG: declare swiftcc {{.+}} @"$s25weak_import_native_helper1PPyS2icigTj"
// CHECK-DAG: declare swiftcc {{.+}} @"$s25weak_import_native_helper1PPyS2icisTj"
// CHECK-DAG: declare swiftcc {{.+}} @"$s25weak_import_native_helper1PPyS2iciMTj"
// CHECK-DAG: declare extern_weak swiftcc {{.+}} @"$s25weak_import_native_helper1PPyS2icigTj"
// CHECK-DAG: declare extern_weak swiftcc {{.+}} @"$s25weak_import_native_helper1PPyS2icisTj"
// CHECK-DAG: declare extern_weak swiftcc {{.+}} @"$s25weak_import_native_helper1PPyS2iciMTj"
let z = p[0]
mutP[0] = z
mutP[0] += 1
Expand All @@ -137,27 +145,24 @@ func testProtocolGeneric<Impl: P>(_ type: Impl.Type) {
}

func testWeakTypes() -> [Any.Type] {
// FIXME: These should be weak.
// CHECK-DAG: declare swiftcc %swift.metadata_response @"$s25weak_import_native_helper5WeakSVMa"
// CHECK-DAG: declare swiftcc %swift.metadata_response @"$s25weak_import_native_helper5WeakEOMa"
// CHECK-DAG: declare swiftcc %swift.metadata_response @"$s25weak_import_native_helper5WeakCCMa"
// CHECK-DAG: declare extern_weak swiftcc %swift.metadata_response @"$s25weak_import_native_helper5WeakSVMa"
// CHECK-DAG: declare extern_weak swiftcc %swift.metadata_response @"$s25weak_import_native_helper5WeakEOMa"
// CHECK-DAG: declare extern_weak swiftcc %swift.metadata_response @"$s25weak_import_native_helper5WeakCCMa"
// CHECK-DAG: @"$s25weak_import_native_helper5WeakPMp" = extern_weak global %swift.protocol
// CHECK-DAG: declare swiftcc %swift.metadata_response @"$s25weak_import_native_helper8GenericSVMa"
// CHECK-DAG: declare swiftcc %swift.metadata_response @"$s25weak_import_native_helper8GenericEOMa"
// CHECK-DAG: declare swiftcc %swift.metadata_response @"$s25weak_import_native_helper8GenericCCMa"
// CHECK-DAG: declare extern_weak swiftcc %swift.metadata_response @"$s25weak_import_native_helper8GenericSVMa"
// CHECK-DAG: declare extern_weak swiftcc %swift.metadata_response @"$s25weak_import_native_helper8GenericEOMa"
// CHECK-DAG: declare extern_weak swiftcc %swift.metadata_response @"$s25weak_import_native_helper8GenericCCMa"
return [WeakS.self, WeakE.self, WeakC.self, WeakP.self, GenericS<Int>.self, GenericE<Int>.self, GenericC<Int>.self]
}

class WeakSub: WeakC {
deinit {
// FIXME: This should be weak.
// CHECK-DAG: declare swiftcc {{.+}} @"$s25weak_import_native_helper5WeakCCfd"
// CHECK-DAG: declare extern_weak swiftcc {{.+}} @"$s25weak_import_native_helper5WeakCCfd"
}
}

class WeakGenericSub: GenericC<Int> {
deinit {
// FIXME: This should be weak.
// CHECK-DAG: declare swiftcc {{.+}} @"$s25weak_import_native_helper8GenericCCfd"
// CHECK-DAG: declare extern_weak swiftcc {{.+}} @"$s25weak_import_native_helper8GenericCCfd"
}
}
Loading