Skip to content

[dotnet-linker] Use [DynamicDependency] attributes instead of manual marking of NSObjects.#25146

Merged
rolfbjarne merged 12 commits intomainfrom
dev/rolf/use-dynamic-dependency-attributes-marknsobjects
Apr 22, 2026
Merged

[dotnet-linker] Use [DynamicDependency] attributes instead of manual marking of NSObjects.#25146
rolfbjarne merged 12 commits intomainfrom
dev/rolf/use-dynamic-dependency-attributes-marknsobjects

Conversation

@rolfbjarne
Copy link
Copy Markdown
Member

@rolfbjarne rolfbjarne commented Apr 14, 2026

Add a new MarkNSObjectsStep step that runs before the mark stage in the trimmer, which uses [DynamicDependency] attributes to ensure NSObject subclasses aren't trimmed away when they shouldn't be. The logic is identical to the existing MarkNSObjects sub step, except MarkNSObjects directly marks APIs. The advantage is that the new step can easily be moved out of the trimmer (which will be done in a later stage).

The new behavior is the default, but keep the old behavior behind an opt-out flag for now.

Also update the preservation code to not handle "Xamarin.iOS.dll" or "Xamarin.Forms.Platform.iOS",
since those assemblies aren't used anymore.

This makes it easier to move this code out of a custom linker step in the future.

There's one non-obvious part here: I had to move Class.ValidateObjectPointers to the Runtime class, because now the Class type's IsBeforeFieldInit flag is false (because otherwise the trimmer doesn't honor the [DynamicDependency] attributes we're now adding to the Class's static cctor), and then the trimmer deems the static cctor as having side effects:

https://github.com/dotnet/runtime/blob/16b9ae91f456c65ffcc5b28afc97034d828a19ad/src/tools/illink/src/linker/Linker.Steps/UnreachableBlocksOptimizer.cs#L268

and it refuses to inline anything:

https://github.com/dotnet/runtime/blob/16b9ae91f456c65ffcc5b28afc97034d828a19ad/src/tools/illink/src/linker/Linker.Steps/UnreachableBlocksOptimizer.cs#L526-L529

Contributes towards #17693.

@azure-pipelines
Copy link
Copy Markdown

Azure Pipelines:
2 pipeline(s) require an authorized user to comment /azp run to run.

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

@vs-mobiletools-engineering-service2

This comment has been minimized.

…marking of NSObjects.

Also update the preservation code to not handle "Xamarin.iOS.dll" or "Xamarin.Forms.Platform.iOS",
since those assemblies aren't used anymore.

This makes it easier to move this code out of a custom linker step in the future.

Contributes towards #17693.
@rolfbjarne rolfbjarne force-pushed the dev/rolf/use-dynamic-dependency-attributes-marknsobjects branch from 62163b1 to deda69c Compare April 15, 2026 07:44
@rolfbjarne rolfbjarne changed the base branch from dev/rolf/use-dynamic-dependency-attributes-markstaticregistrar to main April 15, 2026 08:02
@azure-pipelines
Copy link
Copy Markdown

Azure Pipelines:
Successfully started running 1 pipeline(s).

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

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

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

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

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

@vs-mobiletools-engineering-service2

This comment has been minimized.

The trimmer doesn't want to inline methods if their class has a static
constructor, so this makes sure the trimmer will still inline this method.
@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2
Copy link
Copy Markdown
Collaborator

✅ [CI Build #2bc9ea6] Build passed (Build packages) ✅

Pipeline on Agent
Hash: 2bc9ea650aaaa1ce9555367507d437b8710c1739 [PR build]

@vs-mobiletools-engineering-service2
Copy link
Copy Markdown
Collaborator

✅ [PR Build #2bc9ea6] Build passed (Detect API changes) ✅

Pipeline on Agent
Hash: 2bc9ea650aaaa1ce9555367507d437b8710c1739 [PR build]

@vs-mobiletools-engineering-service2

This comment has been minimized.

Copy link
Copy Markdown
Member

@simonrozsival simonrozsival left a comment

Choose a reason for hiding this comment

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

Adding [DynamicDependency] attributes forces the trimmer to keep extra metadata and so Native AOT app sizes will increase non-trivially (in the case of the macOS app it's 4.5%). If I understand it correctly and this is only supposed to be used in the case when ILLink does not support custom trimmer steps anymore and trimmable type maps are not ready yet or are not supported on the platform (Mono) or the developer chooses not to use the trimmable typemap, then I don't have any problems with this approach.

@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: 2bc9ea650aaaa1ce9555367507d437b8710c1739 [PR build]

@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

✅ [CI Build #2bc9ea6] Build passed (Build macOS tests) ✅

Pipeline on Agent
Hash: 2bc9ea650aaaa1ce9555367507d437b8710c1739 [PR build]

Copy link
Copy Markdown
Member Author

@rolfbjarne rolfbjarne left a comment

Choose a reason for hiding this comment

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

🤖 PR Review Summary

Overall: ✅ LGTM

Well-structured refactoring that extracts MarkNSObjects logic into an IMarkNSObjects interface + MarkNSObjectsImpl shared implementation, allowing both the existing MarkSubStepsDispatcher path (direct marking) and the new AssemblyModifierStep path ([DynamicDependency] attributes) to share the same logic. This is a clean stepping stone toward moving this code out of the trimmer entirely.

What the PR does:

  1. Introduces IMarkNSObjects interface and MarkNSObjectsImpl static helper to share NSObject preservation logic between two linker step implementations.
  2. Adds MarkNSObjectsStep (an AssemblyModifierStep) that uses [DynamicDependency] attributes instead of direct Annotations.Mark().
  3. Moves ValidateObjectPointers from Class to Runtime to avoid IsBeforeFieldInit=false causing the trimmer to refuse inlining (well explained in PR description).
  4. Removes dead Xamarin.iOS / Xamarin.Forms.Platform.iOS special cases from IsProductType.
  5. Fixes attribute constructor marking in AppBundleRewriter.CreateAttribute() — needed when running as a custom linker step.
  6. Adds [DynamicDependency] for proxy interfaces in ManagedRegistrarStep so the trimmer does not remove them before ManagedRegistrarLookupTables runs.
  7. Gates the whole MarkDispatcher behind _UseDynamicDependenciesForMarkDispatcher, which is only enabled when all 3 sub-steps have their DynamicDependency equivalents active.

Positive notes:

  • The allMemberTypes constant carefully avoids DynamicallyAccessedMemberTypes.All to prevent preserving nested types — good attention to detail.
  • The mutual exclusion via DidRunMarkNSObjectsStep flag is clean.
  • Expected test output updates look consistent across all platforms.

No blocking issues found. A few minor observations below.

Comment thread tools/linker/MarkNSObjects.cs
Comment thread tools/linker/MarkNSObjects.cs
@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@rolfbjarne rolfbjarne enabled auto-merge (squash) April 21, 2026 17:21
@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.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2
Copy link
Copy Markdown
Collaborator

🚀 [CI Build #2bc9ea6] Test results 🚀

Test results

✅ All tests passed on VSTS: test results.

🎉 All 156 tests passed 🎉

Tests counts

✅ cecil: All 1 tests passed. [attempt 2] 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 3] Html Report (VSDrops) Download
✅ framework: All 2 tests passed. [attempt 3] Html Report (VSDrops) Download
✅ fsharp: All 4 tests passed. Html Report (VSDrops) Download
✅ generator: All 5 tests passed. [attempt 2] Html Report (VSDrops) Download
✅ interdependent-binding-projects: All 4 tests passed. [attempt 3] Html Report (VSDrops) Download
✅ introspection: All 6 tests passed. Html Report (VSDrops) Download
✅ linker: All 44 tests passed. Html Report (VSDrops) Download
✅ monotouch (iOS): All 11 tests passed. [attempt 2] Html Report (VSDrops) Download
✅ monotouch (MacCatalyst): All 15 tests passed. Html Report (VSDrops) Download
✅ monotouch (macOS): All 12 tests passed. Html Report (VSDrops) Download
✅ monotouch (tvOS): All 11 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. [attempt 2] 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: 2bc9ea650aaaa1ce9555367507d437b8710c1739 [PR build]

@rolfbjarne rolfbjarne merged commit fbb929b into main Apr 22, 2026
48 checks passed
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.

4 participants