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

SILOptimizer: Allow inlining of transparent functions in @backDeployed thunks #76135

Merged
merged 4 commits into from
Aug 29, 2024

Conversation

tshortli
Copy link
Contributor

@tshortli tshortli commented Aug 28, 2024

In order for availability checks in iOS apps to be evaluated correctly when running on macOS, the application binary must call a copy of _stdlib_isOSVersionAtLeast_AEIC() that was emitted into the app, instead of calling the _stdlib_isOSVersionAtLeast() function provided by the standard library. This is because the call to the underlying compiler-rt function __isPlatformVersionAtLeast() must be given the correct platform identifier argument; if the call is not emitted into the client, then the macOS platform identifier is used and the iOS version number will be mistakenly interpreted as a macOS version number at runtime.

The _stdlib_isOSVersionAtLeast() function in the standard library is marked @_transparent on iOS so that its call to _stdlib_isOSVersionAtLeast_AEIC() is always inlined into the client. This works for the code generated by normal if #available checks, but for the @backDeployed function thunks, the calls to _stdlib_isOSVersionAtLeast() were not being inlined and that was causing calls to @backDeployed functions to crash in iOS apps running on macOS since their availability checks were being misevaluated.

The SIL optimizer has a heuristic which inhibits mandatory inlining in functions that are classified as thunks, in order to save code size. This heuristic needs to be relaxed in @backDeployed thunks, so that mandatory inlining of _stdlib_isOSVersionAtLeast() can behave as expected. The change should be safe since the only @_transparent function a @backDeployed thunk is ever expected to call is _stdlib_isOSVersionAtLeast().

Resolves rdar://134793410.

@tshortli tshortli force-pushed the backdeployed-ios-apps-on-macos branch from 4197cef to f7816cf Compare August 29, 2024 05:06
In the standard library shipped in Apple's SDKs and OSes, the implementation of
`_stdlib_isOSVersionAtLeast()` has diverged in order to solve some tricky
issues related to supporting iOS applications running on macOS. It's now time
to bring that change upstream in order to unblock further changes that depend
on it.

Originally introduced to resolve rdar://83378814.
@tshortli tshortli force-pushed the backdeployed-ios-apps-on-macos branch from f7816cf to 42a68fd Compare August 29, 2024 05:08
@tshortli tshortli changed the title AST: On iOS, use _stdlib_isOSVersionAtLeast_AEIC() for availability checks SILOptimizer: Allow inlining of transparent functions in @backDeployed thunks Aug 29, 2024
@tshortli
Copy link
Contributor Author

@swift-ci please test

@tshortli tshortli marked this pull request as ready for review August 29, 2024 05:10
@tshortli tshortli requested review from jckarter, rjmccall, a team, xymus and eeckstein as code owners August 29, 2024 05:10
@tshortli tshortli requested a review from mikeash August 29, 2024 05:10
@tshortli
Copy link
Contributor Author

@swift-ci please test Windows

@eeckstein
Copy link
Contributor

@swift-ci benchmark

@eeckstein
Copy link
Contributor

Run the benchmarks to be on the safe side (I don't expect any changes)

…d thunks.

In order for availability checks in iOS apps to be evaluated correctly when
running on macOS, the application binary must call a copy of
`_stdlib_isOSVersionAtLeast_AEIC()` that was emitted into the app, instead of
calling the `_stdlib_isOSVersionAtLeast()` function provided by the standard
library. This is because the call to the underlying compiler-rt function
`__isPlatformVersionAtLeast()` must be given the correct platform identifier
argument; if the call is not emitted into the client, then the macOS platform
identifier is used and the iOS version number will be mistakenly interpreted as
a macOS version number at runtime.

The `_stdlib_isOSVersionAtLeast()` function in the standard library is marked
`@_transparent` on iOS so that its call to `_stdlib_isOSVersionAtLeast_AEIC()`
is always inlined into the client. This works for the code generated by normal
`if #available` checks, but for the `@backDeployed` function thunks, the calls
to `_stdlib_isOSVersionAtLeast()` were not being inlined and that was causing
calls to `@backDeployed` functions to crash in iOS apps running on macOS since
their availability checks were being misevaluated.

The SIL optimizer has a heuristic which inhibits mandatory inlining in
functions that are classified as thunks, in order to save code size. This
heuristic needs to be relaxed in `@backDeployed` thunks, so that mandatory
inlining of `_stdlib_isOSVersionAtLeast()` can behave as expected. The change
should be safe since the only `@_transparent` function a `@backDeployed` thunk
is ever expected to call is `_stdlib_isOSVersionAtLeast()`.

Resolves rdar://134793410.
@tshortli tshortli force-pushed the backdeployed-ios-apps-on-macos branch from 42a68fd to 789b795 Compare August 29, 2024 15:44
@tshortli
Copy link
Contributor Author

@swift-ci please test

@tshortli
Copy link
Contributor Author

Run the benchmarks to be on the safe side (I don't expect any changes)

We lost the link to these benchmark results since I had to rerun the tests after fixing some broken ones. Here are the original results: https://ci.swift.org/job/swift-PR-macos-perf/1001/

Copy link
Contributor

@eeckstein eeckstein left a comment

Choose a reason for hiding this comment

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

lgtm!

@tshortli tshortli enabled auto-merge August 29, 2024 17:29
@tshortli tshortli merged commit a9120c3 into swiftlang:main Aug 29, 2024
4 of 5 checks passed
@tshortli tshortli deleted the backdeployed-ios-apps-on-macos branch August 29, 2024 22:43
tshortli added a commit to tshortli/swift that referenced this pull request Aug 30, 2024
As promised in the FIXME, update the SIL printer and parser to handle the new
thunk kind for `@backDeployed` thunks.

Follow up to swiftlang#76135.
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