Skip to content
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

[6.1] SIL: Always give @_silgen_name forward declarations public linkage #78188

Merged
merged 1 commit into from
Dec 15, 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
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()