Skip to content

[dotnet-linker] Don't preserve base type members for [Preserve (AllMembers = true)]. - #26362

Merged
rolfbjarne merged 14 commits into
mainfrom
dev/rolf/preserve-all-members
Aug 4, 2026
Merged

rolfbjarne merged 14 commits into
mainfrom
dev/rolf/preserve-all-members

Conversation

@rolfbjarne

Copy link
Copy Markdown
Member

When the assembly preparer applies [Preserve (AllMembers = true)] (and when
it preserves NSObject subclasses in user assemblies), it used to emit a single
[DynamicDependency (DynamicallyAccessedMemberTypes.…, typeof (TheType))]
attribute on the module constructor.

That's not equivalent to [Preserve (AllMembers = true)]: the trimmer resolves
DynamicallyAccessedMemberTypes over the entire type hierarchy, so every member
of every base type is preserved as well. For an NSObject subclass that means all
of NSObject is preserved - and NSObject in turn drags in NSNumber,
NSValue, NSArray, ObjCRuntime.Messaging, System.Drawing.PointF/SizeF/ RectangleF and, transitively, the System.Drawing, System.Drawing.Primitives,
System.ObjectModel and System.ComponentModel.TypeConverter assemblies.

In practice this was triggered by the [Preserve (AllMembers = true)] attribute
on the internal NSObject.NSObject_Disposer class, which has 12 members of its
own but ended up preserving 121 members of NSObject and 8 members of
System.Object.

Emit one DynamicDependency attribute per member declared on the type instead,
which matches [Preserve (AllMembers = true)] semantics exactly.

This only affects the assembly preparer (PrepareAssemblies=true); when our steps
run inside ILLink we use an xml descriptor file, which already had the correct
semantics.

Size impact for tests/dotnet/SizeTestApp/iOS (net11.0, ios-arm64, Release,
CoreCLR + ReadyToRun, trimmable-static + PrepareAssemblies + PostProcessAssemblies):

Configuration App size
Before 11,523,224 B
After 11,380,424 B
Difference -142,800 B (-1.24%)

which brings trimmable-static to within 0.38% of managed-static (11,337,504 B),
down from 1.6%.

Copilot-Session: 6aeeddfd-3bd8-4129-ad90-7292860c4b66

rolfbjarne and others added 2 commits July 30, 2026 11:52
* Remove unnecessary accesibility modifier ('private').
* Remove the unused 'drainHandle' field.
* Remove the [Preserve] attribute, it's no longer needed.
…mbers = true)].

When the assembly preparer applies `[Preserve (AllMembers = true)]` (and when
it preserves NSObject subclasses in user assemblies), it used to emit a single
`[DynamicDependency (DynamicallyAccessedMemberTypes.…, typeof (TheType))]`
attribute on the module constructor.

That's not equivalent to `[Preserve (AllMembers = true)]`: the trimmer resolves
`DynamicallyAccessedMemberTypes` over the entire type hierarchy, so every member
of every base type is preserved as well. For an NSObject subclass that means all
of `NSObject` is preserved - and `NSObject` in turn drags in `NSNumber`,
`NSValue`, `NSArray`, `ObjCRuntime.Messaging`, `System.Drawing.PointF/SizeF/
RectangleF` and, transitively, the `System.Drawing`, `System.Drawing.Primitives`,
`System.ObjectModel` and `System.ComponentModel.TypeConverter` assemblies.

In practice this was triggered by the `[Preserve (AllMembers = true)]` attribute
on the internal `NSObject.NSObject_Disposer` class, which has 12 members of its
own but ended up preserving 121 members of `NSObject` and 8 members of
`System.Object`.

Emit one `DynamicDependency` attribute per member declared on the type instead,
which matches `[Preserve (AllMembers = true)]` semantics exactly.

This only affects the assembly preparer (PrepareAssemblies=true); when our steps
run inside ILLink we use an xml descriptor file, which already had the correct
semantics.

Size impact for tests/dotnet/SizeTestApp/iOS (net11.0, ios-arm64, Release,
CoreCLR + ReadyToRun, trimmable-static + PrepareAssemblies + PostProcessAssemblies):

| Configuration                       | App size     |
|-------------------------------------|--------------|
| Before                              | 11,523,224 B |
| After                               | 11,380,424 B |
| **Difference**                      | **-142,800 B (-1.24%)** |

which brings trimmable-static to within 0.38% of managed-static (11,337,504 B),
down from 1.6%.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 6aeeddfd-3bd8-4129-ad90-7292860c4b66
Copilot AI review requested due to automatic review settings July 30, 2026 10:07

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 adjusts the dotnet-linker assembly preparer behavior so [Preserve (AllMembers = true)] no longer over-preserves members from base types via a single DynamicDependency(DynamicallyAccessedMemberTypes…, typeof(T)). Instead, it emits one DynamicDependency per member declared on the preserved type, matching Preserve semantics more closely and reducing app size.

Changes:

  • Switch Preserve-all-members logic in the assembly preparer to emit per-member DynamicDependency attributes.
  • Keep the “constructors only” behavior for non-AllMembers preservation.
  • Introduce a shared helper (AddPreserveAllMembersDynamicDependencyAttributes) in AppBundleRewriter.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
tools/dotnet-linker/MarkNSObjectsStep.cs Uses the new per-member preservation helper when allMembers == true.
tools/dotnet-linker/ApplyPreserveAttributeStep.cs Uses the new helper for [Preserve(AllMembers=true)] and keeps enum handling as fields-only.
tools/dotnet-linker/AppBundleRewriter.cs Adds the helper that expands preserve-all-members into per-member DynamicDependency attributes.

@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.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@rolfbjarne
rolfbjarne changed the base branch from main to dev/rolf/disposer July 31, 2026 14:18
@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.

Compiler-generated methods and fields are implementation details retained through their containing members. Avoid emitting DynamicDependency signatures for them, since generated local-function signatures cannot be resolved by ILLink.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@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.

…PreserveAllMembersDynamicDependencyAttributes.

For types with no declared members, using a DynamicDependency with
DynamicallyAccessedMemberTypes.PublicParameterlessConstructor is a no-op
(e.g. for interfaces), so the type itself wouldn't be rooted. Instead add a
placeholder member to the type and point a DynamicDependency at that member,
matching the existing AddDynamicDependencyAttributeToStaticConstructor pattern.

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.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@rolfbjarne
rolfbjarne requested a review from dalexsoto as a code owner August 3, 2026 16:59
@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.

Base automatically changed from dev/rolf/disposer to main August 3, 2026 22:08
@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

Copy link
Copy Markdown
Collaborator

⚠️ AppSizeTest expected files changed ⚠️

The AppSizeTest detected changes in the expected app size files.

To update the expected files, add a comment with the following command:

/apply-gist https://gist.github.com/vs-mobiletools-engineering-service2/69951a724ba912b3a5cf6d1b464232b9
Updated files
  • iOS-NativeAOT-size.txt
  • iOS-NativeAOT-TrimmableStatic-size.txt
  • MacCatalyst-NativeAOT-size.txt
  • MacCatalyst-NativeAOT-TrimmableStatic-size.txt
  • MacOSX-NativeAOT-size.txt
  • MacOSX-NativeAOT-TrimmableStatic-size.txt
  • TVOS-NativeAOT-size.txt
  • TVOS-NativeAOT-TrimmableStatic-size.txt

Pipeline on Agent
Hash: 2060a773b28616d639bb27b78e142269c9d88b3c [PR build]

rolfbjarne and others added 4 commits August 4, 2026 07:41
The previous `DynamicallyAccessedMemberTypes` based implementation included
`Interfaces`, and dropping it made the trimmer sweep protocol interfaces from
user types.

This broke the static registrar: the block proxy types (`ObjCRuntime.Trampolines.NID*`)
are only referenced from the custom attributes on the protocol interfaces, so
they'd be trimmed away, while the static registrar still emitted metadata token
references to their `Create` methods. At runtime, resolving such a token threw:

    [FAIL] GenericClassWithUnrelatedGenericDelegate : System.ArgumentOutOfRangeException : Argument_InvalidToken, 100805969, System.ModuleHandle Arg_ParamName_Name, methodToken

Add back a single `DynamicDependency` attribute with
`DynamicallyAccessedMemberTypes.Interfaces` next to the per-member attributes,
and update the expected app sizes accordingly.

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

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

The `DynamicDependency (DynamicallyAccessedMemberTypes.Interfaces, type)`
attribute added for `[Preserve (AllMembers = true)]` types made the trimmer
fail with IL2037 for types without any interfaces:

    ILLink : error IL2037: <Module>..cctor(): No members were resolved for 'Interfaces' on type 'LinkAll.CommonLinkAllTest'.

which broke the build of the 'link all' test suite (for instance with the
'prepare-assemblies|trimmable-static-registrar' test variation).

Only add the attribute when the type, or any of its base types, actually
implements an interface.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@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: 070cd4bb70065e5a785576a0208c5b90bd96a8ef [PR build]

@vs-mobiletools-engineering-service2

Copy link
Copy Markdown
Collaborator

🚀 [CI Build #070cd4b] 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. 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. 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. Html Report (VSDrops) Download
✅ windows: All 3 tests passed. Html Report (VSDrops) Download
✅ xcframework: All 4 tests passed. Html Report (VSDrops) Download
✅ xtro: All 1 tests passed. Html Report (VSDrops) Download

macOS tests

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

Linux Build Verification

✅ Linux build succeeded

Pipeline on Agent
Hash: 070cd4bb70065e5a785576a0208c5b90bd96a8ef [PR build]

@rolfbjarne
rolfbjarne enabled auto-merge (squash) August 4, 2026 16:04
@rolfbjarne rolfbjarne added the ready-to-review This PR is ready to review/merge. label Aug 4, 2026
@rolfbjarne
rolfbjarne merged commit d17ed03 into main Aug 4, 2026
56 checks passed
@rolfbjarne
rolfbjarne deleted the dev/rolf/preserve-all-members branch August 4, 2026 17:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

copilot ready-to-review This PR is ready to review/merge.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants