-
Notifications
You must be signed in to change notification settings - Fork 565
[dotnet-linker] Add a trimmer step to inline calls to Class.GetHandle[Intrinsic]. #25318
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
Draft
rolfbjarne
wants to merge
4
commits into
main
Choose a base branch
from
dev/rolf/inline-class-gethandle
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
8b6452b
[src] Add [Un]SupportedSimulator attributes.
rolfbjarne d8423ba
dlfcn inlining: use simulator supportedness attributes
rolfbjarne 34c7a93
[dotnet-linker] Add a trimmer step to inline calls to Class.GetHandle…
rolfbjarne a51653c
Auto-format source code
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| # Objective-C classes | ||
|
|
||
| Objective-C classes can be referenced from managed code in several ways: | ||
|
|
||
| * Calls to Class.GetHandle / GetHandleIntrinsic | ||
|
|
||
| It's highly desirable to use a direct native reference to Objective-C classes when building a mobile app, for a few reasons: | ||
|
|
||
| * It's faster at runtime, and the app is smaller. | ||
| * If the referenced Objective-C class comes from a third-party static library, the | ||
| native linker can remove it if it's configured to remove unused code | ||
| (because the native linker can't see that the class is in fact used | ||
| at runtime) unless there's a direct native reference to the class. | ||
|
|
||
| On the other hand there's one scenario when a direct native reference is not desirable: when the native Objective-C class does not exist. | ||
|
|
||
| In order to create a direct native reference to Objective-C classes, we need to know the names of those Objective-C classes. | ||
|
|
||
| ## The `InlineClassGetHandle` property | ||
|
|
||
| This behavior is controlled by the `InlineClassGetHandle` MSBuild property, which | ||
| can either be enabled or disabled. | ||
|
|
||
| See the [build properties documentation](../building-apps/build-properties.md) for default values. | ||
|
|
||
| ## How it works | ||
|
|
||
| During the build we try to collect the following: | ||
|
|
||
| * Any calls to `Class.GetHandle[Intrinsic]` APIs: we try to collect the class name (this might not always succeed, if the class name is not a constant). | ||
|
|
||
| This is further complicated by the fact that we only want to create native | ||
| references for managed references that survive trimming. | ||
|
|
||
| So we do the following: | ||
|
|
||
| 1. During trimming, two custom linker steps execute: | ||
|
|
||
| * `InlineClassGetHandleStep`: for every call `Class.GetHandle` we've | ||
| collected, this step creates a P/Invoke to a native method that will | ||
| return the Objective-C class for that symbol (using a direct native | ||
| reference), and modifies the code that fetches that symbol to call said | ||
| P/Invoke. | ||
|
|
||
| 2. After trimming, we figure out which of those symbols survived: | ||
|
|
||
| * For ILTrim: the `_CollectPostILTrimInformation` MSBuild target inspects | ||
| the trimmed assemblies and collects all the inlined P/Invokes that | ||
| survived. Per-assembly results are cached to speed up incremental builds. | ||
| * For NativeAOT: the `_CollectPostNativeAOTTrimInformation` MSBuild target | ||
| inspects the native object file (or static library) produced by NativeAOT, | ||
| collects all unresolved native references, and filters them against the | ||
| Objective-C classes to determine which survived. | ||
|
|
||
| 3. The `_PostTrimmingProcessing` MSBuild target takes the surviving symbols | ||
| from either path, generates the corresponding native Objective-C code, and | ||
| adds it to the list of files to compile and link into the final executable. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.