Skip to content

Introduce stub mode for -unavailable-decl-optimization #65557

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 13 commits into from
May 5, 2023

Conversation

tshortli
Copy link
Contributor

@tshortli tshortli commented May 1, 2023

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 added 13 commits May 3, 2023 15:19
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 branch from ab89db6 to 12a122a Compare May 3, 2023 22:52
@tshortli
Copy link
Contributor Author

tshortli commented May 3, 2023

@swift-ci please test

/// @_semantics("unavailable_code_reached")
bool isCalleeUnavailableCodeReached() const {
auto calleeFn = getCalleeFunction();
if (!calleeFn) return false;
Copy link
Contributor

Choose a reason for hiding this comment

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

nitpick: usually we split return to a separate line for better debuggability.

@tshortli
Copy link
Contributor Author

tshortli commented May 4, 2023

@swift-ci please test Linux

@tshortli tshortli merged commit 0366c42 into swiftlang:main May 5, 2023
@tshortli tshortli deleted the unavailable-decl-optimization-stub branch May 5, 2023 16:11
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.

2 participants