Skip to content

[5.9] Introduce -unavailable-decl-optimization=stub #65269

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

Conversation

tshortli
Copy link
Contributor

@tshortli tshortli commented Apr 18, 2023

Cherry pick of #65557

Implement a new -unavailable-decl-optimization mode that replaces the bodies of unavailable functions with calls to a standard library function that invokes fatalError. Unlike -unavailable-decl-optimization=complete, this compilation mode is designed to improve the code size of unavailable code while also preserving ABI compatibility. This is important since there are existing compiled binaries that link the symbols for unavailable functions even though those symbols should be provably unreachable.

This optimization is implemented in SILGen by inserting a call to a new standard library function _diagnoseUnavailableCodeReached() in the prologue of each unavailable function. The existing dead code elimination optimization pass then determines that the remainder of the function body is unreachable and removes all other code, leaving the body of the function as a single call to a no-return function.

There are several follow up changes that can be made to maximize the effectiveness of this optimization:

  • Some functions that could benefit from this optimization are generated during IRGen, like value witnesses functions. The same technique will need to be reproduced for IRGen to reduce code size further.
  • The generated machine code for the stubbed functions is not optimal. It currently includes stack pointer management before calling _diagnoseUnavailableCodeReached() and sometimes also includes a brk instruction after the call. Some work to reduce the machine code for these stubs down to the minimal necessary instructions (probably just a single branch?) may be warranted.

Resolves rdar://107388493

@tshortli tshortli force-pushed the unavailable-decl-optimization-stub-5.9 branch 3 times, most recently from 31835e4 to d2c7a51 Compare April 19, 2023 20:41
@tshortli
Copy link
Contributor Author

@swift-ci please test

1 similar comment
@tshortli
Copy link
Contributor Author

@swift-ci please test

@tshortli tshortli force-pushed the unavailable-decl-optimization-stub-5.9 branch from 20d657a to c3e0a89 Compare April 20, 2023 16:49
@tshortli
Copy link
Contributor Author

@swift-ci please test

@AnthonyLatsis AnthonyLatsis added 🍒 release cherry pick Flag: Release branch cherry picks swift 5.9 labels May 3, 2023
tshortli added 13 commits May 5, 2023 09:18
When `-unavailable-decl-optimization=stub` is specified, insert a call to
`_diagnoseUnavailableCodeReached()` at the beginning of the function to cause
it to trap if executed at run time.

Part of rdar://107388493
…tubs.

User code should not be diagnosed as "unreachable" by the SIL optimizer when
the no-return function that made the code unreachable is a compiler inserted
call to `_diagnoseUnavailableCodeReached()`.

Part of rdar://107388493
The logic was defaulting to `arm64` when a binary only contained `amr64_32` and
`arm64e` slices.
@tshortli tshortli force-pushed the unavailable-decl-optimization-stub-5.9 branch from 05f017b to 7c29f8c Compare May 5, 2023 16:18
@tshortli tshortli marked this pull request as ready for review May 5, 2023 16:25
@tshortli tshortli requested a review from a team as a code owner May 5, 2023 16:25
@nkcsgexi
Copy link
Contributor

nkcsgexi commented May 5, 2023

This is a mode behind an opt-in flag so it should be safe to be landed on 5.9

@tshortli
Copy link
Contributor Author

tshortli commented May 5, 2023

@swift-ci please test

@tshortli tshortli force-pushed the unavailable-decl-optimization-stub-5.9 branch from 5637532 to 8c45bb2 Compare May 6, 2023 01:28
@tshortli
Copy link
Contributor Author

tshortli commented May 6, 2023

@swift-ci please test

@tshortli tshortli merged commit 7208cf5 into swiftlang:release/5.9 May 7, 2023
@tshortli tshortli deleted the unavailable-decl-optimization-stub-5.9 branch May 7, 2023 00:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🍒 release cherry pick Flag: Release branch cherry picks swift 5.9
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants