Repository navigation
[dotnet-linker] Set AssociatedSourceType on UnmanagedCallersOnly trampolines - #25915
rolfbjarne wants to merge 5 commits into
Conversation
This comment was marked as outdated.
This comment was marked as outdated.
There was a problem hiding this comment.
Pull request overview
This PR updates the managed registrar’s generated UnmanagedCallersOnly trampolines to set AssociatedSourceType, so NativeAOT/trimming can elide entry points when the corresponding NSObject subclass is trimmed, reducing app size.
Changes:
- Pass the exported method’s declaring type into
CreateUnmanagedCallersAttribute. - Emit an
AssociatedSourceTypenamed argument on generatedUnmanagedCallersOnlyAttributeinstances.
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.
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.
ead51c2 to
c905f4a
Compare
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.
…tocol on net11.0. The trimmable static registrar's .NET 11 TypeMap path (--typemap-entry-assembly) preserves the full exported method surface of registered framework types that the app subclasses (e.g. SceneDelegate : UIResponder, AppDelegate : UIApplicationDelegate). That keeps methods like UIResponder.PressesBegan (which takes an NSSet<UIPress>), UIResponder.AccessibilityAssistiveTechnologyFocusedIdentifiers (NSSet<NSString>) and UIApplicationDelegate.DidDiscardSceneSessions (NSSet<UISceneSession>) alive, which in turn keeps NSSet<T> alive - so it isn't trimmed away and this test fails. The [ProtocolMember] attribute trimming this test was written for still works correctly (no ProtocolMemberAttribute instances remain in the linked assemblies); NSSet<T> is simply kept alive by these real methods instead. This is fixed by #25915, so ignore the test until that PR is merged. The Assert.Ignore is placed so that it produces a merge conflict with the corresponding marker in #25915, forcing us to re-enable the test when resolving the conflict. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: a19e8dea-25aa-4e58-a63c-2f2e161164ed
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.
…ar after ILC Set the AssociatedSourceType field on the UnmanagedCallersOnly attributes the managed registrar emits, so the NativeAOT compiler (ILC) can drop a trampoline's native export when the associated type (the NSObject subclass declaring the exported method) can't be constructed. This is an app-size optimization for NativeAOT (issue #25631). The field is only emitted for NativeAOT + the trimmable-static registrar + PrepareAssemblies: * Only ILC understands the field; CoreCLR's attribute parser rejects any UnmanagedCallersOnly named argument it doesn't recognize ("Known custom attribute named argument not recognized.") at startup, so it must not be emitted for CoreCLR/Mono. * The trimmable-static registrar emits native glue (registrar.o) that references the trampoline symbols directly for the whole set of registered SDK types. ILLink keeps those types (so the glue is emitted), but ILC independently drops some trampoline exports, which would leave undefined symbols at native link time. The fix requires a post-ILC step that can reconcile the generated glue with the exports that survived ILC, which only PrepareAssemblies has. To make this work with the trimmable-static registrar, the native registrar-generating postprocessing pipeline is reordered to run *after* ILC instead of before (gated on trimmable-static + NativeAOT + PrepareAssemblies). This is possible because ILLink is skipped in this scenario (ILC consumes the prepared assemblies directly), so the postprocessing output no longer feeds ILC. After ILC, the registrar inspects ILC's output object file to determine which trampolines survived: * Trampolines ILC trimmed away are routed through the dlsym fallback (a runtime string reference) instead of a direct native symbol reference. Since ILC only trims a trampoline when the associated type can't be constructed, the trampoline is never actually invoked, so the fallback is never reached. * Classes whose trampolines were all trimmed away are skipped entirely (no @interface/@implementation is emitted), except base classes still referenced as a superclass of an emitted class (the whole base-class chain is kept). Additionally, a warning is emitted if any post-ILC postprocessing step modifies a managed assembly, since ILC has already consumed them and the change would be silently lost. Two spurious "modifications" are fixed to avoid false warnings: ManagedRegistrarStep.ProcessType now reports the actual modification state (during post-processing it only collects information, a read-only operation), and the NSObject.RegisterToggleRef stub (already applied before ILC) is no longer re-applied post-ILC. Fixes #25631 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 2b6450f9-2ad9-4e89-823a-3cc6e2f33f53
29e1862 to
b1131da
Compare
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
✅ [CI Build #a04ce3e] Prepare .NET Release succeeded ✅📦 Published NuGet packages (32 packages)iOS
MacCatalyst
macOS
tvOS
Other
Pipeline on Agent |
This comment has been minimized.
This comment has been minimized.
…SSet<T> alive
The test verifies that a generic type referenced only from an optional protocol
member (Foundation.NSSet<T>) is trimmed away when using the trimmable static
registrar. It did so with:
typeof (NSObject).Assembly.GetType (NamespacePrefix + "Foundation.NSSet`1")
Since NamespacePrefix is a `const string = ""`, the argument is a compile-time
constant, so ILLink's intrinsic handling of Assembly.GetType(string) resolves it
and marks Foundation.NSSet`1 as kept - the test's own assertion was the sole
reason the type survived (confirmed via --dump-dependencies). The result was the
assertion failing with "NSSet<T> must be linked away, otherwise this test is
useless".
Fix it the same way the sibling RemovedAttributes test already does: copy the
name pieces into non-const locals and go through Helper.GetType, so ILLink can't
statically resolve the type name and keep it. With this change NSSet<T> is
trimmed and the test passes.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 2b6450f9-2ad9-4e89-823a-3cc6e2f33f53
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.
…callersonly-associatedsourcetype
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.
✅ 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 #6f9ce95] Test results 🔥Test results❌ Tests failed on VSTS: test results 0 tests crashed, 12 tests failed, 244 tests passed. Failures❌ linker tests (iOS)4 tests failed, 27 tests passed.Failed tests
Html Report (VSDrops) Download ❌ linker tests (MacCatalyst)4 tests failed, 27 tests passed.Failed tests
Html Report (VSDrops) Download ❌ linker tests (tvOS)4 tests failed, 27 tests passed.Failed tests
Html Report (VSDrops) Download Successes✅ assembly-processing: All 1 tests passed. Html Report (VSDrops) Download macOS tests✅ Tests on macOS Sonoma (14): All 5 tests passed. Html Report (VSDrops) Download Linux Build VerificationPipeline on Agent |
|
Incorporated into #26408. |
Set the AssociatedSourceType field on the UnmanagedCallersOnly attributes the managed registrar emits, so the NativeAOT compiler (ILC) can drop a trampoline's native export when the associated type (the NSObject subclass declaring the exported method) can't be constructed. This is an app-size optimization for NativeAOT (issue #25631).
Fixes #25631
Co-authored-by: Copilot 223556219+Copilot@users.noreply.github.com
Copilot-Session: 2b6450f9-2ad9-4e89-823a-3cc6e2f33f53