Skip to content

IRGen: Weakly link symbols for unavailable declarations #64353

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 3 commits into from
Mar 15, 2023

Conversation

tshortli
Copy link
Contributor

@tshortli tshortli commented Mar 14, 2023

When computing linkage, the compiler would treat unavailable declarations as if they were "always available" if they also lack an introduced: version:

// Library
@available(macOS, unavailable)
public func foo() {
  // …
}

// Client
import Library

@available(macOS, unavailable)
func bar() {
  // Even though foo() and bar() are unavalable on macOS the compiler still
  // strongly links foo().
  foo()
}

This created an unnecessary dependency between libraries and their clients and also can interfere with back deployment, since unavailable declarations may not be present in a library on all OS versions. Developers could work around these
issues by conditionally compiling the code that references an unavailable declaration, but they shouldn't have to given that unavailable code is meant to be provably unreachable at runtime. Additionally, in the future it could improve library code size if we allowed the compiler to strip unavailable declarations from a binary completely.

Resolves rdar://106673713

@tshortli
Copy link
Contributor Author

@swift-ci please test

Copy link
Contributor

@nkcsgexi nkcsgexi left a comment

Choose a reason for hiding this comment

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

👍

@xedin xedin removed their request for review March 14, 2023 18:26
@tshortli tshortli force-pushed the weak-link-unavailable-symbols branch from 29ffab3 to cc05fbf Compare March 14, 2023 22:37
@tshortli
Copy link
Contributor Author

I tweaked this a bit to allow unavailable declarations with an introduced: version to be treated as ABI and linked accordingly (e.g. Float16 #35997).

When computing linkage, the compiler would treat unavailable declarations as if
they were "always available" when they lack an `introduced:` version:

```
// Library
@available(macOS, unavailable)
public func foo() {
  // …
}

// Client
import Library

@available(macOS, unavailable)
func bar() {
  // Even though foo() and bar() are unavalable on macOS the compiler still
  // strongly links foo().
  foo()
}
```

This created an unnecessary dependency between libraries and their clients and
also can interfere with back deployment, since unavailable declarations may not
be present in a library on all OS versions. Developers could work around these
issues by conditionally compiling the code that references an unavailable
declaration, but they shouldn't have to given that unavailable code is meant to
be provably unreachable at runtime. Additionally, it could improve library code
size if we allowed the compiler to strip unavailable declarations from a binary
completely.

Resolves rdar://106673713
…ions.

If a declaration is unavailable but also has an `introduced:` availability
version, treat that as an indication that it is considered ABI and should be
linked as if it were available.

Part of rdar://106673713
@tshortli tshortli force-pushed the weak-link-unavailable-symbols branch from cc05fbf to 084ef39 Compare March 15, 2023 01:06
@tshortli
Copy link
Contributor Author

@swift-ci please test

@tshortli tshortli merged commit d9e028f into swiftlang:main Mar 15, 2023
@tshortli tshortli deleted the weak-link-unavailable-symbols branch March 15, 2023 05:54
@compnerd
Copy link
Member

I believe that this is causing a regression for Windows:

DateComponentsFormatter.swift.obj : fatal error LNK1227: conflicting weak extern definition for '$s10Foundation9FormatterC11objectValueyypSgSSKF'.  New default '.weak.$s10Foundation9FormatterC11objectValueyypSgSSKF.default.$s10Foundation23DateComponentsFormatterC10UnitsStyleO8rawValueAESgSi_tcfC' conflicts with old default '.weak.$s10Foundation9FormatterC11objectValueyypSgSSKF.default.$s10Foundation18ByteCountFormatterC5UnitsV8rawValueSuvg' in ByteCountFormatter.swift.obj

https://ci-external.swift.org/job/oss-swift-windows-toolchain-x86_64-vs2019/2829/console

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants