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

[mono][aot] Enable deduplication of runtime invoke wrappers #84304

Merged

Conversation

kotlarmilos
Copy link
Member

@kotlarmilos kotlarmilos commented Apr 4, 2023

This PR contributes to #83973. It improves code maintenance by using deduplication enum instead of multiple flags. It enables deduplication of runtime invoke wrappers on iOS and restricts emitting additional methods into the dedup AOT module that are not collected in the dedup_methods hash table.

Here are preliminary results comparing size on disk (SOD) and build time between the baseline (main branch) and the target (this branch).

Update: System.Runtime are failing on the ios-arm64 CI, even though they pass locally. The failure could be related to missing runtime invoke wrappers during application startup, and the error log may have been masked by a TCP failure. The tests have passed on tvos-arm64, which shouldn't differ from ios-arm64 on the CI. Before merging the deduplication of runtime invoke wrappers on iOS, the CI should be updated to report any error logs that occurred during startup. The table with preliminary size savings on iOS has been moved to the tracking issue.

@kotlarmilos kotlarmilos added area-Codegen-AOT-mono os-ios Apple iOS size-reduction Issues impacting final app size primary for size sensitive workloads labels Apr 4, 2023
@kotlarmilos kotlarmilos added this to the 8.0.0 milestone Apr 4, 2023
@kotlarmilos kotlarmilos self-assigned this Apr 4, 2023
@ghost
Copy link

ghost commented Apr 4, 2023

Tagging subscribers to 'os-ios': @steveisok, @akoeplinger
See info in area-owners.md if you want to be subscribed.

Issue Details

This PR contributes to #83973. It enables deduplication of runtime invoke wrappers on iOS and restricts emitting additional methods into the dedup AOT module that are not collected in the dedup_methods hash table.

Here are preliminary results comparing size on disk (SOD) and build time between the baseline (main branch) and the target (this branch).

HelloiOS app configuration Baseline SOD (mb) Target SOD (mb) Baseline build time (s) Target build time (s) SOD diff (%)
llvm release 26,10 24,90 20,52 16,98 -4,60
nollvm release 26,90 25,40 21,43 18,20 -5,58
Author: kotlarmilos
Assignees: kotlarmilos
Labels:

area-Codegen-AOT-mono, os-ios, size-reduction

Milestone: 8.0.0

@ghost
Copy link

ghost commented Apr 4, 2023

Tagging subscribers to 'size-reduction': @eerhardt, @SamMonoRT, @marek-safar
See info in area-owners.md if you want to be subscribed.

Issue Details

This PR contributes to #83973. It enables deduplication of runtime invoke wrappers on iOS and restricts emitting additional methods into the dedup AOT module that are not collected in the dedup_methods hash table.

Here are preliminary results comparing size on disk (SOD) and build time between the baseline (main branch) and the target (this branch).

HelloiOS app configuration Baseline SOD (mb) Target SOD (mb) Baseline build time (s) Target build time (s) SOD diff (%)
llvm release 26,10 24,90 20,52 16,98 -4,60
nollvm release 26,90 25,40 21,43 18,20 -5,58
Author: kotlarmilos
Assignees: kotlarmilos
Labels:

area-Codegen-AOT-mono, os-ios, size-reduction

Milestone: 8.0.0

@kotlarmilos
Copy link
Member Author

/azp run runtime-ioslike,runtime-ioslikesimulator

@azure-pipelines
Copy link

Azure Pipelines successfully started running 2 pipeline(s).

@SamMonoRT
Copy link
Member

/cc @ivanpovazan

@kotlarmilos
Copy link
Member Author

Relevant failing tests indicate that some wrappers are emitted into the dedup assembly during AOT compilation. These wrappers are later retrieved during runtime by the mono_aot_get_trampoline function, which relies on the mscorlib AOT module only.

To simplify the build process and improve the SOD, one potential solution is to use mscorlib as the dedup assembly. Instead of generating an additional empty dedup assembly, during the collect phase, AOT compiler could collect and emit all deduplicated methods within mscorlib. Then, at runtime, mscorlib could serve as the container assembly, as it is for wrappers already.

@vargaz @lambdageek @ivanpovazan any thoughts on the proposed approach?

@lambdageek
Copy link
Member

Relevant failing tests indicate that some wrappers are emitted into the dedup assembly during AOT compilation. These wrappers are later retrieved during runtime by the mono_aot_get_trampoline function, which relies on the mscorlib AOT module only.

To simplify the build process and improve the SOD, one potential solution is to use mscorlib as the dedup assembly. Instead of generating an additional empty dedup assembly, during the collect phase, AOT compiler could collect and emit all deduplicated methods within mscorlib. Then, at runtime, mscorlib could serve as the container assembly, as it is for wrappers already.

@vargaz @lambdageek @ivanpovazan any thoughts on the proposed approach?

We talked about this briefly during the standup. I think it's a good idea in general to have a uniform approach to where we put "extra methods" during AOT compilation. So it makes sense to me to have a general mechanism in the AOT compiler for emitting the extra methods that doesn't always just hardcode corelib.

Separately there's a question of whether corelib could be the destination for all this "extra" stuff when we're in dedup mode.
I think the answer is probably that is not a great idea.

On ios we have to support Mono.framework - which bundles the BCL and its AOT images into something that is shared across apps. In the future, we might want to support dedup of the final app in this sort of compilation, too. In that case we couldn't use corelib as the dedup assembly since it's not going to be per-app.

We may want to use the entrypoint assembly as the dedup assembly. Or continue with the current approach of emitting a separate AOT image with dedup content.

at AOT runtime we could decide based on whether dedup was used whether to look for wrappers in corelib (if there's no dedup, use the existing behavior), or look for them in the dedup asembly (if dedup was turned on - this would be new behavior).

@kotlarmilos
Copy link
Member Author

kotlarmilos commented Apr 6, 2023

Thank you for the detailed explanation.

After further investigation, I've found that during the AOT compilation of deduplicated methods, the add_referenced_patch function adds generic instances that are referenced by compiled method. These instances must be emitted as they are skipped in the dedup-skip phase.

Therefore, it is not possible to restrict the emitting of additional methods into the dedup AOT module that are not collected in the dedup_methods hash table.

With the deduplication of runtime wrappers only, an additional size saving of approximately 1% to 2% can be achieved on the HelloiOS sample app. Additionally, introducing a dedup state, as you outlined, can improve code maintenance.

@kotlarmilos
Copy link
Member Author

kotlarmilos commented Apr 13, 2023

/azp run list

@azure-pipelines
Copy link

CI/CD Pipelines for this repository:

@azure-pipelines
Copy link

No pipelines are associated with this pull request.

@kotlarmilos
Copy link
Member Author

/azp run runtime-ioslike,runtime-ioslikesimulator,runtime-wasm

@azure-pipelines
Copy link

Azure Pipelines successfully started running 3 pipeline(s).

@kotlarmilos
Copy link
Member Author

The failures appear to be unrelated. The timeout issue for tvos-arm64 is being tracked in dotnet/arcade#11683. The failures related to WASM are known #84434 and #84101.

@kg
Copy link
Contributor

kg commented May 8, 2023

We can de-revert this now that vlad's fixes landed, right?

@kotlarmilos
Copy link
Member Author

kotlarmilos commented May 8, 2023

This PR identified the underlying bug and accidentally introduced a regression, so it was reverted. Currently, there is an open PR #85908 that should deduplicate runtime invoke wrappers on iOS.

Thanks for the reminder! There is no need for any further actions.

@ghost ghost locked as resolved and limited conversation to collaborators Jun 8, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-Codegen-AOT-mono os-ios Apple iOS size-reduction Issues impacting final app size primary for size sensitive workloads
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants