Repository navigation
[dotnet-linker] Don't preserve base type members for [Preserve (AllMembers = true)]. - #26362
Conversation
* 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
There was a problem hiding this comment.
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
DynamicDependencyattributes. - Keep the “constructors only” behavior for non-
AllMemberspreservation. - Introduce a shared helper (
AddPreserveAllMembersDynamicDependencyAttributes) inAppBundleRewriter.
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. |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
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>
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
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>
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
|
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>
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
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>
✅ API diff for current PR / commitNET (empty diffs)✅ API diff vs stableNET (empty diffs)ℹ️ Generator diffGenerator Diff: vsdrops (html) vsdrops (raw diff) gist (raw diff) - Please review changes) Pipeline on Agent |
🚀 [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 macOS tests✅ Tests on macOS Monterey (12): All 5 tests passed. Html Report (VSDrops) Download Linux Build VerificationPipeline on Agent |
When the assembly preparer applies
[Preserve (AllMembers = true)](and whenit 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 resolvesDynamicallyAccessedMemberTypesover the entire type hierarchy, so every memberof every base type is preserved as well. For an NSObject subclass that means all
of
NSObjectis preserved - andNSObjectin turn drags inNSNumber,NSValue,NSArray,ObjCRuntime.Messaging,System.Drawing.PointF/SizeF/ RectangleFand, transitively, theSystem.Drawing,System.Drawing.Primitives,System.ObjectModelandSystem.ComponentModel.TypeConverterassemblies.In practice this was triggered by the
[Preserve (AllMembers = true)]attributeon the internal
NSObject.NSObject_Disposerclass, which has 12 members of itsown but ended up preserving 121 members of
NSObjectand 8 members ofSystem.Object.Emit one
DynamicDependencyattribute 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):
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