Skip to content

[linker] Convert the Runtime.Arch optimization to a trimmer feature switch. Fixes #26106. - #26111

Merged
rolfbjarne merged 2 commits into
mainfrom
dev/rolf/issue-26106-runtime-arch-feature-switch
Jul 17, 2026
Merged

rolfbjarne merged 2 commits into
mainfrom
dev/rolf/issue-26106-runtime-arch-feature-switch

Conversation

@rolfbjarne

Copy link
Copy Markdown
Member

Convert the Runtime.Arch optimization from the hand-written IL rewrite in OptimizeGeneratedCode (ProcessRuntimeArch, which inlined ObjCRuntime.Runtime.Arch — device vs. simulator) to an ILLink field substitution, so the trimmer folds the constant branch instead.

The ObjCRuntime.Runtime.Arch.IsSimulator feature switch is already wired up in Xamarin.Shared.Sdk.targets (via RuntimeHostConfigurationOption) and already stubs GetRuntimeArch(). The only missing piece was the Arch static field itself: I added a <field> substitution keyed on the same feature so ILLink folds ldsfld ObjCRuntime.Runtime::Arch reads to a constant and removes the dead branches — the same net effect as the old step:

<field name="Arch" feature="ObjCRuntime.Runtime.Arch.IsSimulator" featurevalue="false" value="DEVICE" />
<field name="Arch" feature="ObjCRuntime.Runtime.Arch.IsSimulator" featurevalue="true"  value="SIMULATOR" />

(Added to ILLink.Substitutions.iOS.xml and .tvOS.xml; the Arch field only exists on iOS/tvOS, so macOS/Mac Catalyst don't need it.)

Additional cleanup:

  • Removed ProcessRuntimeArch and the "Arch" case in ProcessLoadStaticField, plus the now-unused Device field on OptimizeGeneratedCodeData and its assignments.
  • Marked inline-runtime-arch as removed (mirroring inline-intptr-size): kept the option name for back-compat, emptied its list of valid platforms, and dropped the default-enable logic.
  • Removed the inline-runtime-arch entries from the Versions-*.plist.in files and the "Inline Runtime.Arch" section from docs/website/optimizations.md.

Fixes #26106.

🤖 Pull request created by Copilot

…witch. Fixes #26106.

Remove the hand-written IL rewrite `ProcessRuntimeArch` (which inlined
`ObjCRuntime.Runtime.Arch` - device vs. simulator) and rely on the trimmer
instead.

The `ObjCRuntime.Runtime.Arch.IsSimulator` feature switch is already wired up
in `Xamarin.Shared.Sdk.targets` and already stubs `GetRuntimeArch()`. Add an
ILLink `<field>` substitution for the `Arch` static field (keyed on the same
feature) so ILLink folds `ldsfld Runtime::Arch` reads to a constant and removes
the dead branches - the same net effect as the old step.

Also remove the now-unused `Device` field from `OptimizeGeneratedCodeData`, and
mark the `inline-runtime-arch` optimization as removed (mirroring
`inline-intptr-size`): keep the option name for back-compat but empty its list
of valid platforms and drop the default-enable logic. Remove the corresponding
entries from the Versions plists and the optimizations documentation.

Fixes #26106.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR migrates the ObjCRuntime.Runtime.Arch constant-folding optimization from a custom IL rewrite (OptimizeGeneratedCode) to an ILLink feature-switch-driven field substitution, so the trimmer can fold Runtime.Arch branches during linking/trimming. It also removes the now-obsolete inline-runtime-arch optimization documentation/metadata and updates size/preserved-API baselines to match the new trimming results.

Changes:

  • Replace the Runtime.Arch optimizer step with ILLink <field> substitutions keyed off ObjCRuntime.Runtime.Arch.IsSimulator (iOS/tvOS).
  • Remove the old ProcessRuntimeArch implementation and associated plumbing (Device flag).
  • Clean up optimization docs/metadata and refresh expected app-size + preserved-API outputs affected by the trimming change.

Reviewed changes

Copilot reviewed 29 out of 29 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
tools/linker/OptimizeGeneratedCode.cs Removes the custom IL rewrite for Runtime.Arch loads.
tools/linker/CoreOptimizeGeneratedCode.cs Drops passing the now-removed Device flag into optimizer data.
tools/dotnet-linker/OptimizeGeneratedCodeStep.cs Drops passing the now-removed Device flag into optimizer data.
tools/common/Optimizations.cs Marks inline-runtime-arch as removed (platform applicability + defaults adjusted).
src/ILLink.Substitutions.tvOS.xml Adds Runtime.Arch field substitution keyed on Arch.IsSimulator.
src/ILLink.Substitutions.iOS.xml Adds Runtime.Arch field substitution keyed on Arch.IsSimulator.
docs/website/optimizations.md Removes the “Inline Runtime.Arch” optimization documentation section.
builds/Versions-tvOS.plist.in Removes inline-runtime-arch from the published optimizations list.
builds/Versions-macOS.plist.in Removes inline-runtime-arch from the published optimizations list.
builds/Versions-MacCatalyst.plist.in Removes inline-runtime-arch from the published optimizations list.
builds/Versions-iOS.plist.in Removes inline-runtime-arch from the published optimizations list.
tests/dotnet/UnitTests/expected/TVOS-NativeAOT-TrimmableStatic-size.txt Updates expected app size output for tvOS NativeAOT trimmable-static.
tests/dotnet/UnitTests/expected/TVOS-NativeAOT-size.txt Updates expected app size output for tvOS NativeAOT.
tests/dotnet/UnitTests/expected/TVOS-MonoVM-size.txt Updates expected app size output for tvOS MonoVM.
tests/dotnet/UnitTests/expected/TVOS-MonoVM-interpreter-size.txt Updates expected app size output for tvOS interpreter.
tests/dotnet/UnitTests/expected/TVOS-MonoVM-interpreter-preservedapis.txt Updates expected preserved API list for tvOS interpreter.
tests/dotnet/UnitTests/expected/MacOSX-NativeAOT-TrimmableStatic-size.txt Updates expected app size output for macOS NativeAOT trimmable-static.
tests/dotnet/UnitTests/expected/MacOSX-NativeAOT-size.txt Updates expected app size output for macOS NativeAOT.
tests/dotnet/UnitTests/expected/MacOSX-CoreCLR-Interpreter-TrimmableStatic-size.txt Updates expected app size output for macOS CoreCLR interpreter trimmable-static.
tests/dotnet/UnitTests/expected/MacOSX-CoreCLR-Interpreter-size.txt Updates expected app size output for macOS CoreCLR interpreter.
tests/dotnet/UnitTests/expected/MacCatalyst-NativeAOT-TrimmableStatic-size.txt Updates expected app size output for Mac Catalyst NativeAOT trimmable-static.
tests/dotnet/UnitTests/expected/MacCatalyst-NativeAOT-size.txt Updates expected app size output for Mac Catalyst NativeAOT.
tests/dotnet/UnitTests/expected/MacCatalyst-MonoVM-size.txt Updates expected app size output for Mac Catalyst MonoVM.
tests/dotnet/UnitTests/expected/MacCatalyst-MonoVM-interpreter-size.txt Updates expected app size output for Mac Catalyst interpreter.
tests/dotnet/UnitTests/expected/iOS-NativeAOT-TrimmableStatic-size.txt Updates expected app size output for iOS NativeAOT trimmable-static.
tests/dotnet/UnitTests/expected/iOS-NativeAOT-size.txt Updates expected app size output for iOS NativeAOT.
tests/dotnet/UnitTests/expected/iOS-MonoVM-size.txt Updates expected app size output for iOS MonoVM.
tests/dotnet/UnitTests/expected/iOS-MonoVM-interpreter-size.txt Updates expected app size output for iOS interpreter.
tests/dotnet/UnitTests/expected/iOS-MonoVM-interpreter-preservedapis.txt Updates expected preserved API list for iOS interpreter.

Comment thread src/ILLink.Substitutions.iOS.xml Resolved
Comment thread src/ILLink.Substitutions.tvOS.xml Resolved
Comment thread tools/common/Optimizations.cs Resolved
Comment thread docs/website/optimizations.md Resolved
@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@rolfbjarne
rolfbjarne marked this pull request as ready for review July 16, 2026 13:50
@vs-mobiletools-engineering-service2

Copy link
Copy Markdown
Collaborator

✅ API diff for current PR / commit

NET (empty diffs)

✅ API diff vs stable

NET (empty diffs)

ℹ️ Generator diff

Generator Diff: vsdrops (html) vsdrops (raw diff) gist (raw diff) - Please review changes)

Pipeline on Agent
Hash: 86b3dd42d2eda2806539a6f5c60556e34122e1ea [PR build]

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@rolfbjarne
rolfbjarne enabled auto-merge (squash) July 17, 2026 11:16
@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

Copy link
Copy Markdown
Collaborator

🚀 [CI Build #86b3dd4] Test results 🚀

Test results

✅ All tests passed on VSTS: test results.

🎉 All 203 tests passed 🎉

Tests counts

✅ assembly-processing: All 1 tests passed. Html Report (VSDrops) Download
✅ cecil: All 1 tests passed. Html Report (VSDrops) Download
✅ dotnettests (iOS): All 1 tests passed. Html Report (VSDrops) Download
✅ dotnettests (MacCatalyst): All 1 tests passed. Html Report (VSDrops) Download
✅ dotnettests (macOS): All 1 tests passed. Html Report (VSDrops) Download
✅ dotnettests (Multiple platforms): All 1 tests passed. Html Report (VSDrops) Download
✅ dotnettests (tvOS): All 1 tests passed. [attempt 2] Html Report (VSDrops) Download
✅ framework: All 2 tests passed. Html Report (VSDrops) Download
✅ fsharp: All 4 tests passed. Html Report (VSDrops) Download
✅ generator: All 5 tests passed. Html Report (VSDrops) Download
✅ interdependent-binding-projects: All 4 tests passed. Html Report (VSDrops) Download
✅ introspection: All 4 tests passed. Html Report (VSDrops) Download
✅ linker (iOS): All 15 tests passed. Html Report (VSDrops) Download
✅ linker (MacCatalyst): All 15 tests passed. [attempt 2] Html Report (VSDrops) Download
✅ linker (macOS): All 21 tests passed. Html Report (VSDrops) Download
✅ linker (tvOS): All 15 tests passed. Html Report (VSDrops) Download
✅ monotouch (iOS): All 19 tests passed. Html Report (VSDrops) Download
✅ monotouch (MacCatalyst): All 18 tests passed. Html Report (VSDrops) Download
✅ monotouch (macOS): All 19 tests passed. Html Report (VSDrops) Download
✅ monotouch (tvOS): All 19 tests passed. Html Report (VSDrops) Download
✅ msbuild: All 2 tests passed. Html Report (VSDrops) Download
✅ sharpie: All 1 tests passed. [attempt 2] Html Report (VSDrops) Download
✅ windows: All 3 tests passed. Html Report (VSDrops) Download
✅ xcframework: All 4 tests passed. [attempt 2] Html Report (VSDrops) Download
✅ xtro: All 1 tests passed. Html Report (VSDrops) Download

macOS tests

✅ Tests on macOS Monterey (12): All 5 tests passed. [attempt 2] Html Report (VSDrops) Download
✅ Tests on macOS Ventura (13): All 5 tests passed. [attempt 2] Html Report (VSDrops) Download
✅ Tests on macOS Sonoma (14): All 5 tests passed. [attempt 2] Html Report (VSDrops) Download
✅ Tests on macOS Sequoia (15): All 5 tests passed. [attempt 2] Html Report (VSDrops) Download
✅ Tests on macOS Tahoe (26): All 5 tests passed. [attempt 4] Html Report (VSDrops) Download

Linux Build Verification

✅ Linux build succeeded

Pipeline on Agent
Hash: 86b3dd42d2eda2806539a6f5c60556e34122e1ea [PR build]

@rolfbjarne
rolfbjarne merged commit 6993e2c into main Jul 17, 2026
57 checks passed
@rolfbjarne
rolfbjarne deleted the dev/rolf/issue-26106-runtime-arch-feature-switch branch July 17, 2026 12:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Convert Runtime.Arch optimization to a trimmer feature switch

4 participants