Skip to content

[dotnet-linker] Add a trimmer step to inline calls to Class.GetHandle[Intrinsic].#25318

Draft
rolfbjarne wants to merge 4 commits intomainfrom
dev/rolf/inline-class-gethandle
Draft

[dotnet-linker] Add a trimmer step to inline calls to Class.GetHandle[Intrinsic].#25318
rolfbjarne wants to merge 4 commits intomainfrom
dev/rolf/inline-class-gethandle

Conversation

@rolfbjarne
Copy link
Copy Markdown
Member

No description provided.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Adds a new trimming/linking pipeline to replace some ObjCRuntime.Dlfcn and Class.GetHandle[Intrinsic] calls with direct native references, so surviving symbols/classes can be materialized as native code after trimming and preserved for the final app link.

Changes:

  • Adds new linker steps and linker configuration for inlining Dlfcn and Class.GetHandle calls, plus symbol/type collection helpers.
  • Adds MSBuild tasks/targets to scan trimmed outputs / NativeAOT objects, generate native source files, and compile/link those generated references.
  • Adds docs and test variations/tests intended to exercise the new symbol/class inlining behavior.

Reviewed changes

Copilot reviewed 38 out of 39 changed files in this pull request and generated 6 comments.

Show a summary per file
File Description
tools/mtouch/Errors.resx Adds new linker warning/error resource strings.
tools/mtouch/Errors.designer.cs Regenerates strongly-typed resource accessors.
tools/linker/MonoTouch.Tuner/ProcessExportedFields.cs Collects [Field] symbol names for compat mode.
tools/linker/MonoTouch.Tuner/ListExportedSymbols.cs Refactors Objective-C type preservation and field handling.
tools/linker/CoreTypeMapStep.cs Emits Objective-C class/framework/version type-map data.
tools/dotnet-linker/Steps/InlineDlfcnMethodsStep.cs New step that rewrites supported Dlfcn call patterns.
tools/dotnet-linker/Steps/InlineClassGetHandleStep.cs New step that rewrites Class.GetHandle[Intrinsic] calls.
tools/dotnet-linker/Steps/GenerateReferencesStep.cs Minor symbol prefix usage update.
tools/dotnet-linker/Steps/GenerateInlinedClassGetHandleCodeStep.cs Adds a generated-symbol-list step for post-trim processing.
tools/dotnet-linker/Steps/GatherFrameworksStep.cs Broadens framework gathering to all assemblies.
tools/dotnet-linker/Steps/AssemblyModifierStep.cs Makes type iteration resilient to newly added types.
tools/dotnet-linker/LinkerConfiguration.cs Adds new inlining-related options and state.
tools/dotnet-linker/AppBundleRewriter.cs Adds helpers for creating synthetic types/PInvokes.
tools/common/Symbols.cs Exposes symbol prefix constants/shared APIs.
tools/common/StaticRegistrar.cs Exposes SDK-introduced-version lookup override.
tools/common/PathUtils.cs Adds directory-creation helper for file paths.
tools/common/MachO.cs Adds unresolved-symbol extraction from Mach-O/static libs.
tools/common/Frameworks.cs Adds framework lookup helpers and broader gathering logic.
tools/common/FileUtils.cs Adds a write-if-different helper for generated files.
tools/common/CompilerFlags.cs Updates symbol prefix usage.
tests/xharness/Jenkins/TestVariationsFactory.cs Adds new CI test variations for the inlining modes.
tests/monotouch-test/ObjCRuntime/DlfcnTest.cs Adds regression coverage for inlined Dlfcn scenarios.
tests/monotouch-test/Foundation/AttributedStringTest.cs Removes obsolete missing-symbol assertions.
tests/monotouch-test/dotnet/shared.csproj Adds ignored/reference native symbols for tests.
tests/monotouch-test/CoreGraphics/GeometryTest.cs Refactors CGRect symbol lookup test helper.
tests/monotouch-test/CoreFoundation/ProxyTest.cs Removes obsolete missing-symbol assertions.
tests/common/test-variations.csproj Declares new build/test variation names and properties.
src/ObjCRuntime/Registrar.cs Exposes exported Objective-C type-name lookup helpers.
msbuild/Xamarin.MacDev.Tasks/Xamarin.MacDev.Tasks.csproj Includes shared symbol code and new task files.
msbuild/Xamarin.MacDev.Tasks/Tasks/PostTrimmingProcessing.cs Generates post-trim native source files.
msbuild/Xamarin.MacDev.Tasks/Tasks/ComputeNativeAOTSurvivingNativeSymbols.cs Filters NativeAOT unresolved symbols into surviving lists.
msbuild/Xamarin.MacDev.Tasks/Tasks/CompileNativeCode.cs Ensures output directories exist before compilation.
msbuild/Xamarin.MacDev.Tasks/Tasks/CollectUnresolvedNativeSymbols.cs Collects unresolved symbols from NativeAOT outputs.
msbuild/Xamarin.MacDev.Tasks/Tasks/CollectPostILTrimInformation.cs Scans trimmed assemblies for surviving generated P/Invokes.
dotnet/targets/Xamarin.Shared.Sdk.targets Wires new linker parameters, steps, and post-trim targets.
dotnet/targets/Xamarin.Shared.Sdk.props Adds default values for the new inlining properties.
docs/code/native-symbols.md Documents the Dlfcn native-symbol pipeline.
docs/code/class-handles.md Documents the Class.GetHandle pipeline.
docs/building-apps/build-properties.md Documents InlineDlfcnMethods.
Files not reviewed (1)
  • tools/mtouch/Errors.designer.cs: Language not supported

Comment thread dotnet/targets/Xamarin.Shared.Sdk.props Outdated
Comment thread msbuild/Xamarin.MacDev.Tasks/Tasks/PostTrimmingProcessing.cs
Comment thread tools/common/FileUtils.cs Outdated
Comment thread tests/xharness/Jenkins/TestVariationsFactory.cs Outdated
Comment thread dotnet/targets/Xamarin.Shared.Sdk.targets
Comment thread tests/xharness/Jenkins/TestVariationsFactory.cs Outdated
@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.

@rolfbjarne rolfbjarne changed the base branch from main to dev/rolf/inline-dlfcn May 5, 2026 16:24
@rolfbjarne rolfbjarne force-pushed the dev/rolf/inline-class-gethandle branch from 83c1324 to 556a21c Compare May 5, 2026 16:25
@rolfbjarne
Copy link
Copy Markdown
Member Author

/review

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented May 5, 2026

.NET for Apple Platforms PR Reviewer completed successfully!

Copy link
Copy Markdown
Contributor

@github-actions github-actions Bot left a comment

Choose a reason for hiding this comment

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

Code Review Summary

This PR adds a new trimmer step to inline calls to Class.GetHandle[Intrinsic], converting them to direct P/Invoke calls that reference Objective-C classes natively. This optimization improves runtime performance and enables better native linking.

Critical Issues Found

🔴 Blocking Issues

  1. Typo in MSBuild property name (dotnet/targets/Xamarin.Shared.Sdk.props:111): TargetFrameworkVersion is split as "TargetFr ameworkVersion" with a space, breaking the condition that sets default values for InlineClassGetHandle. This will cause the feature to not activate correctly.

  2. Placeholder error codes (tools/dotnet-linker/Steps/InlineClassGetHandleStep.cs:41, 114, 119, 127, 132): Multiple error messages use placeholder code 9999 with "Copilot:" comments indicating they need proper error codes and resource strings. These must be replaced with actual error codes before merging.

⚠️ Non-Blocking Issues

  1. Typo in parameter name (PostTrimmingProcessing.cs:112): filterObjetiveCClasses should be filterObjectiveCClasses (missing 'c'). While this doesn't affect functionality, it should be corrected for code quality.

  2. Missing optimization (CollectPostILTrimInformation.cs:124): Comment indicates an unimplemented early-exit optimization for assemblies without __Internal module references.

  3. Missing validation in typeMap parsing (PostTrimmingProcessing.cs:156): The typeMap parsing doesn't validate that required fields exist, potentially creating entries with empty keys.

  4. Potential KeyNotFoundException (PostTrimmingProcessing.cs:182): ToDictionary call may throw if the typeMap file contains duplicate class names. Consider using a more defensive approach.

Architecture & Design

The overall design is sound:

  • Clear separation of concerns between trimming steps, MSBuild tasks, and code generation
  • Good use of caching for incremental builds
  • Appropriate handling of both ILTrim and NativeAOT paths
  • Documentation file (class-handles.md) provides good context

Recommendation

Request Changes - The typo in TargetFrameworkVersion and placeholder error codes must be fixed before this can be merged, as they will cause the feature to malfunction.

Generated by .NET for Apple Platforms PR Reviewer for issue #25318 · ● 1.6M

Comment thread dotnet/targets/Xamarin.Shared.Sdk.props
Comment thread msbuild/Xamarin.MacDev.Tasks/Tasks/PostTrimmingProcessing.cs
Comment thread msbuild/Xamarin.MacDev.Tasks/Tasks/PostTrimmingProcessing.cs
Comment thread tools/dotnet-linker/Steps/InlineClassGetHandleStep.cs Outdated
Comment thread msbuild/Xamarin.MacDev.Tasks/Tasks/PostTrimmingProcessing.cs Outdated
Comment thread msbuild/Xamarin.MacDev.Tasks/Tasks/CollectPostILTrimInformation.cs Outdated
Comment thread tools/dotnet-linker/Steps/InlineClassGetHandleStep.cs Outdated
@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.

@rolfbjarne rolfbjarne force-pushed the dev/rolf/inline-class-gethandle branch 2 times, most recently from 645a011 to 7003552 Compare May 7, 2026 08:16
rolfbjarne added 3 commits May 7, 2026 14:09
Add two attributes to specify whether an API is available in the simulator:

* [UnsupportedSimulator ("<platform>")]: API is not available.
* [SupportedSimulator ("<platform>[osversion]")]: API is available, optionally
  only in the specified OS version or not.

If no attribute is found, then the API is available in the simulator.

Unlike the normal availability attributes, specifying simulator availability for
one platform does not mean anything for any other platforms.
@rolfbjarne rolfbjarne changed the base branch from dev/rolf/inline-dlfcn to main May 7, 2026 12:09
@rolfbjarne rolfbjarne force-pushed the dev/rolf/inline-class-gethandle branch from 7003552 to 34c7a93 Compare May 7, 2026 12:10
@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
Copy link
Copy Markdown
Collaborator

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

Pipeline on Agent
Hash: a51653c636ae6a7bce274c0c08cfd654a119eb17 [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.

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

✅ [PR Build #a51653c] Build passed (Build packages) ✅

Pipeline on Agent
Hash: a51653c636ae6a7bce274c0c08cfd654a119eb17 [PR build]

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

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

✅ [PR Build #a51653c] Build passed (Build macOS tests) ✅

Pipeline on Agent
Hash: a51653c636ae6a7bce274c0c08cfd654a119eb17 [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 #a51653c] Test results 🔥

Test results

❌ Tests failed on VSTS: test results

1 tests crashed, 44 tests failed, 132 tests passed.

Failures

❌ linker tests

22 tests failed, 22 tests passed.

Failed tests

  • dont link/iOS - simulator/Debug: LaunchTimedOut
  • dont link/iOS - simulator/Release: LaunchTimedOut
  • dont link/tvOS - simulator/Debug: LaunchTimedOut
  • dont link/tvOS - simulator/Release: LaunchTimedOut
  • link sdk/iOS - simulator/Debug: LaunchTimedOut
  • link sdk/iOS - simulator/Release: LaunchTimedOut
  • link sdk/tvOS - simulator/Debug: LaunchTimedOut
  • link sdk/tvOS - simulator/Release: LaunchTimedOut
  • link all/iOS - simulator/Debug: LaunchTimedOut
  • link all/iOS - simulator/Release: LaunchTimedOut
  • link all/iOS - simulator/Debug (don't bundle original resources): LaunchTimedOut
  • link all/tvOS - simulator/Debug: LaunchTimedOut
  • link all/tvOS - simulator/Release: LaunchTimedOut
  • link all/tvOS - simulator/Debug (don't bundle original resources): LaunchTimedOut
  • trimmode copy/iOS - simulator/Debug: LaunchTimedOut
  • trimmode copy/iOS - simulator/Release: LaunchTimedOut
  • trimmode copy/tvOS - simulator/Debug: LaunchTimedOut
  • trimmode copy/tvOS - simulator/Release: LaunchTimedOut
  • trimmode link/iOS - simulator/Debug: LaunchTimedOut
  • trimmode link/iOS - simulator/Release: LaunchTimedOut
  • trimmode link/tvOS - simulator/Debug: LaunchTimedOut
  • trimmode link/tvOS - simulator/Release: LaunchTimedOut

Html Report (VSDrops) Download

❌ monotouch tests (iOS)

18 tests failed, 0 tests passed.

Failed tests

  • monotouch-test/iOS - simulator/Debug: LaunchTimedOut
  • monotouch-test/iOS - simulator/Debug (LinkSdk): LaunchTimedOut
  • monotouch-test/iOS - simulator/Debug (static registrar): LaunchTimedOut
  • monotouch-test/iOS - simulator/Release (all optimizations): LaunchTimedOut
  • monotouch-test/iOS - simulator/Debug (ARM64): LaunchTimedOut
  • monotouch-test/iOS - simulator/Release (NativeAOT, ARM64): LaunchTimedOut
  • monotouch-test/iOS - simulator/Release (trimmable static registrar, NativeAOT, ARM64): LaunchTimedOut
  • monotouch-test/iOS - simulator/Debug (managed static registrar): LaunchTimedOut
  • monotouch-test/iOS - simulator/Release (managed static registrar, all optimizations): LaunchTimedOut
  • monotouch-test/iOS - simulator/Release (NativeAOT, x64): LaunchTimedOut
  • monotouch-test/iOS - simulator/Release (trimmable static registrar, NativeAOT, x64): LaunchTimedOut
  • monotouch-test/iOS - simulator/Debug (interpreter): LaunchTimedOut
  • monotouch-test/iOS - simulator/Release (interpreter): LaunchTimedOut
  • monotouch-test/iOS - simulator/Release (compat inline Class.GetHandle): LaunchTimedOut
  • monotouch-test/iOS - simulator/Release (strict inline Class.GetHandle): LaunchTimedOut
  • monotouch-test/iOS - simulator/Release (compat inline dlfcn): LaunchTimedOut
  • monotouch-test/iOS - simulator/Release (strict inline dlfcn, link sdk): LaunchTimedOut
  • monotouch-test/iOS - simulator/Release (NativeAOT, .NET 11 defaults): BuildFailure

Html Report (VSDrops) Download

❌ monotouch tests (tvOS)

2 tests failed, 16 tests passed.

Failed tests

  • monotouch-test/tvOS - simulator/Release (strict inline dlfcn, link sdk): BuildFailure
  • monotouch-test/tvOS - simulator/Release (NativeAOT, .NET 11 defaults): BuildFailure

Html Report (VSDrops) Download

❌ windows tests

🔥 Failed catastrophically on VSTS: test results - windows (no summary found).

Html Report (VSDrops) Download

❌ xcframework tests

2 tests failed, 2 tests passed.

Failed tests

  • xcframework-test/iOS - simulator/Debug: LaunchTimedOut
  • xcframework-test/tvOS - simulator/Debug: LaunchTimedOut

Html Report (VSDrops) Download

Successes

✅ 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 6 tests passed. Html Report (VSDrops) Download
✅ monotouch (MacCatalyst): All 18 tests passed. Html Report (VSDrops) Download
✅ monotouch (macOS): All 18 tests passed. (⚠️ Html Report Publish failed ⚠️) Download
✅ msbuild: All 2 tests passed. Html Report (VSDrops) Download
✅ sharpie: All 1 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: a51653c636ae6a7bce274c0c08cfd654a119eb17 [PR build]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants