Skip to content

Commit

Permalink
Merge pull request swiftlang#78188 from tshortli/silgen-name-linkage-6.1
Browse files Browse the repository at this point in the history
[6.1] SIL: Always give `@_silgen_name` forward declarations public linkage
  • Loading branch information
tshortli authored Dec 15, 2024
2 parents 0a22dd0 + 0619a83 commit 09efe03
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
8 changes: 8 additions & 0 deletions lib/SIL/IR/SILDeclRef.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -651,6 +651,14 @@ SILLinkage SILDeclRef::getDefinitionLinkage() const {
effectiveAccess = std::max(effectiveAccess, AccessLevel::Internal);
}

// Declarations with a @_silgen_name attribute and no body may be forward
// declarations of functions defined in another module. Therefore they must
// always have external (public) linkage, regardless of declared access level.
if (auto afd = getAbstractFunctionDecl()) {
if (!afd->hasBody() && afd->getAttrs().hasAttribute<SILGenNameAttr>())
effectiveAccess = AccessLevel::Public;
}

switch (effectiveAccess) {
case AccessLevel::Private:
case AccessLevel::FilePrivate:
Expand Down
49 changes: 49 additions & 0 deletions test/IRGen/silgen_name_linkage.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// RUN: %target-swift-frontend -parse-as-library -emit-ir %s | %FileCheck %s

// Since this test depends on weak linking based on availability, it only
// applies to Apple platforms.
// REQUIRES: OS=macosx || OS=ios || OS=tvos || OS=watchos || OS=visionos

@available(SwiftStdlib 9999, *)
@_silgen_name("privateForwardDecl")
private func privateForwardDecl()

@available(SwiftStdlib 9999, *)
@_silgen_name("internalForwardDecl")
internal func internalForwardDecl()

@available(SwiftStdlib 9999, *)
@_silgen_name("publicForwardDecl")
public func publicForwardDecl()

@available(SwiftStdlib 9999, *)
@_silgen_name("privateDefined")
private func privateDefined() {}

// CHECK: define internal swiftcc void @privateDefined()

@available(SwiftStdlib 9999, *)
@_silgen_name("internalDefined")
internal func internalDefined() {}

// CHECK: define hidden swiftcc void @internalDefined()

@available(SwiftStdlib 9999, *)
@_silgen_name("publicDefined")
public func publicDefined() {}

// CHECK: define swiftcc void @publicDefined()

public func test() {
guard #available(SwiftStdlib 9999, *) else { return }
privateForwardDecl()
internalForwardDecl()
publicForwardDecl()
privateDefined()
internalDefined()
publicDefined()
}

// CHECK: declare extern_weak swiftcc void @privateForwardDecl()
// CHECK: declare extern_weak swiftcc void @internalForwardDecl()
// CHECK: declare extern_weak swiftcc void @publicForwardDecl()

0 comments on commit 09efe03

Please sign in to comment.