Skip to content

[dotnet-linker] Use [DynamicDependency] attributes instead of manual marking when optimizing generated code.#24979

Draft
rolfbjarne wants to merge 7 commits intodev/rolf/use-dynamic-dependency-attributes-preserveblockcodefrom
dev/rolf/use-dynamic-dependency-attributes-optimizegeneratedcode
Draft

[dotnet-linker] Use [DynamicDependency] attributes instead of manual marking when optimizing generated code.#24979
rolfbjarne wants to merge 7 commits intodev/rolf/use-dynamic-dependency-attributes-preserveblockcodefrom
dev/rolf/use-dynamic-dependency-attributes-optimizegeneratedcode

Conversation

@rolfbjarne
Copy link
Member

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

Also simplify a few things:

  • There's no need to compute whether the code is optimizable, because the result
    is never used.
  • Unify code to determine whether the InlineIsARM64CallingConvention optimization
    is to be applied.
  • Any code that modifies the current assembly now returns a boolean saying so (so
    that we know if the current assembly has to be saved).

Contributes towards #17693.

…marking when optimizing generated code.

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

Also simplify a few things:

* There's no need to compute whether the code is optimizable, because the result
  is never used.
* Unify code to determine whether the InlineIsARM64CallingConvention optimization
  is to be applied.
* Any code that modifies the current assembly now returns a boolean saying so (so
  that we know if the current assembly has to be saved).

Contributes towards #17693.
Copy link
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

This PR migrates the “binding optimizer” from a MarkHandler-based approach toward an AssemblyModifierStep (pre-mark) flow, aligning with the longer-term goal of reducing reliance on custom linker steps and enabling future trimmer/NativeAOT scenarios.

Changes:

  • Introduces a new OptimizeGeneratedCodeStep (assembly modifier) and wires it into the SDK targets behind a DynamicDependency-based switch.
  • Refactors OptimizeGeneratedCodeHandler to use a shared OptimizeGeneratedCodeData context and to report whether IL was modified.
  • Extends AssemblyModifierStep with a ProcessMethod hook + helper to iterate methods, and updates smart-enum preservation to use the helper.

Reviewed changes

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

Show a summary per file
File Description
tools/linker/CoreOptimizeGeneratedCode.cs Refactors optimizer logic to be data-driven/static and adds modification tracking + shared data carrier.
tools/dotnet-linker/Steps/AssemblyModifierStep.cs Adds method-level processing hook and helper (ProcessMethods) for modifier steps.
tools/dotnet-linker/PreserveSmartEnumConversionsStep.cs Simplifies type processing by reusing ProcessMethods and overrides the new method hook.
tools/dotnet-linker/OptimizeGeneratedCodeStep.cs New pre-mark step that runs the generated code optimizer as an assembly modifier.
tools/common/Target.cs Centralizes the ABI-specific “inline ARM64 calling convention” determination and caches it.
dotnet/targets/Xamarin.Shared.Sdk.targets Adds build properties and conditions to choose between the old MarkHandler and new pre-mark step.

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

✅ [CI Build #065c042] Build passed (Build packages) ✅

Pipeline on Agent
Hash: 065c04250b2768b042616130cff2b19698a5ed82 [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
Collaborator

✅ [CI Build #065c042] Build passed (Build macOS tests) ✅

Pipeline on Agent
Hash: 065c04250b2768b042616130cff2b19698a5ed82 [PR build]

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2
Copy link
Collaborator

🔥 [CI Build #065c042] Test results 🔥

Test results

❌ Tests failed on VSTS: test results

0 tests crashed, 19 tests failed, 137 tests passed.

Failures

❌ dotnettests tests (iOS)

1 tests failed, 0 tests passed.

Failed tests

  • DotNet tests: Failed (Execution failed with exit code 1)
    • Xamarin.Tests.AppSizeTest.MonoVM_Interpreter(iOS,"ios-arm64"): No removed APIs (set the environment variable WRITE_KNOWN_FAILURES=1 and run the test again to update the expected set of APIs...
    • Xamarin.Tests.AppSizeTest.MonoVM(iOS,"ios-arm64"): No removed APIs (set the environment variable WRITE_KNOWN_FAILURES=1 and run the test again to update the expected set of APIs...
    • Xamarin.Tests.BundleStructureTest.Build(iOS,"ios-arm64",All,"Deb...: Warnings
      Expected is <System.Collections.Generic.List`1[System.String]> with 10 elements, actual is <System.String[11]>
      Va...
    • ... and 7 more

Html Report (VSDrops) Download

❌ dotnettests tests (MacCatalyst)

1 tests failed, 0 tests passed.

Failed tests

  • DotNet tests: Failed (Execution failed with exit code 1)
    • Xamarin.Tests.BundleStructureTest.Build(MacCatalyst,"maccatalyst...: Warnings
      Expected is <System.Collections.Generic.List`1[System.String]> with 20 elements, actual is <System.String[22]>
      Va...
    • Xamarin.Tests.DotNetProjectTest.PublishAot(MacCatalyst,"maccatal...: No warnings expected, but got:
      /Users/builder/azdo/_work/4/s/macios/src/Network/NWWebSocketResponse.cs(77): warning MT2106: Cou...
    • Xamarin.Tests.DotNetProjectTest.PublishAot(MacCatalyst,"maccatal...: No warnings expected, but got:
      /Users/builder/azdo/_work/4/s/macios/src/Network/NWWebSocketResponse.cs(77): warning MT2106: Cou...
    • ... and 4 more

Html Report (VSDrops) Download

❌ dotnettests tests (macOS)

1 tests failed, 0 tests passed.

Failed tests

  • DotNet tests: Failed (Execution failed with exit code 1)
    • Xamarin.Tests.DotNetProjectTest.PublishAot(MacOSX,"","Release"): No warnings expected, but got:
      /Users/builder/azdo/_work/4/s/macios/src/Network/NWWebSocketResponse.cs(77): warning MM2106: Cou...
    • Xamarin.Tests.DotNetProjectTest.PublishAot(MacOSX,"osx-arm64;osx...: No warnings expected, but got:
      /Users/builder/azdo/_work/4/s/macios/src/Network/NWWebSocketResponse.cs(77): warning MM2106: Cou...
    • Xamarin.Tests.DotNetProjectTest.PublishAot(MacOSX,"osx-arm64;osx...: No warnings expected, but got:
      /Users/builder/azdo/_work/4/s/macios/src/Network/NWWebSocketResponse.cs(77): warning MM2106: Cou...
    • ... and 4 more

Html Report (VSDrops) Download

❌ dotnettests tests (tvOS)

1 tests failed, 0 tests passed.

Failed tests

  • DotNet tests: Failed (Execution failed with exit code 1)
    • Xamarin.Tests.BundleStructureTest.Build(TVOS,"tvos-arm64",All,"D...: Warnings
      Expected is <System.Collections.Generic.List`1[System.String]> with 10 elements, actual is <System.String[11]>
      Va...
    • Xamarin.Tests.BundleStructureTest.Build(TVOS,"tvos-arm64",All,"R...: Warnings
      Expected is <System.Collections.Generic.List`1[System.String]> with 10 elements, actual is <System.String[11]>
      Va...
    • Xamarin.Tests.DotNetProjectTest.PublishAot(TVOS,"tvossimulator-x...: No warnings expected, but got:
      /Users/builder/azdo/_work/4/s/macios/src/Network/NWWebSocketResponse.cs(77): warning MT2106: Cou...
    • ... and 4 more

Html Report (VSDrops) Download

❌ linker tests

4 tests failed, 40 tests passed.

Failed tests

  • link all/macOS/Release: Failed (Test run failed.
    Tests run: 93 Passed: 82 Inconclusive: 0 Failed: 1 Ignored: 10)
  • link all/Mac Catalyst/Release: Failed (Test run failed.
    Tests run: 97 Passed: 86 Inconclusive: 0 Failed: 1 Ignored: 10)
  • trimmode link/macOS/Release: Failed (Test run failed.
    Tests run: 120 Passed: 117 Inconclusive: 0 Failed: 1 Ignored: 2)
  • trimmode link/Mac Catalyst/Release: Failed (Test run failed.
    Tests run: 133 Passed: 129 Inconclusive: 0 Failed: 1 Ignored: 3)

Html Report (VSDrops) Download

❌ monotouch tests (iOS)

2 tests failed, 9 tests passed.

Failed tests

  • monotouch-test/iOS - simulator/Release (NativeAOT, ARM64): Failed
  • monotouch-test/iOS - simulator/Release (NativeAOT, x64): Failed

Html Report (VSDrops) Download

❌ monotouch tests (MacCatalyst)

3 tests failed, 12 tests passed.

Failed tests

  • monotouch-test/Mac Catalyst/Release (NativeAOT): Failed (Test run failed.
    Tests run: 3739 Passed: 3570 Inconclusive: 10 Failed: 1 Ignored: 168)
  • monotouch-test/Mac Catalyst/Release (NativeAOT, ARM64): Failed (Test run failed.
    Tests run: 3739 Passed: 3570 Inconclusive: 10 Failed: 1 Ignored: 168)
  • monotouch-test/Mac Catalyst/Release (NativeAOT, x64): Failed (Test run failed.
    Tests run: 3739 Passed: 3567 Inconclusive: 10 Failed: 1 Ignored: 171)

Html Report (VSDrops) Download

❌ monotouch tests (macOS)

3 tests failed, 9 tests passed.

Failed tests

  • monotouch-test/macOS/Release (NativeAOT): Failed (Test run failed.
    Tests run: 3667 Passed: 3553 Inconclusive: 4 Failed: 1 Ignored: 113)
  • monotouch-test/macOS/Release (NativeAOT, ARM64): Failed (Test run failed.
    Tests run: 3667 Passed: 3554 Inconclusive: 4 Failed: 1 Ignored: 112)
  • monotouch-test/macOS/Release (NativeAOT, x64): Failed (Test run failed.
    Tests run: 3667 Passed: 3536 Inconclusive: 4 Failed: 1 Ignored: 130)

Html Report (VSDrops) Download

❌ monotouch tests (tvOS)

2 tests failed, 9 tests passed.

Failed tests

  • monotouch-test/tvOS - simulator/Release (NativeAOT, ARM64): Failed
  • monotouch-test/tvOS - simulator/Release (NativeAOT, x64): Failed

Html Report (VSDrops) Download

❌ windows tests

1 tests failed, 2 tests passed.

Failed tests

  • Remote .NET tests/Xamarin.Tests.WindowsTest.BundleStructureWithRemoteMac(iOS,"ios-arm64",All,"Debug"): Failed: Incorrect warnings: Warnings

Html Report (VSDrops) Download

Successes

✅ cecil: All 1 tests passed. Html Report (VSDrops) Download
✅ dotnettests (Multiple platforms): 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
✅ msbuild: All 2 tests passed. Html Report (VSDrops) Download
✅ sharpie: All 1 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: 065c04250b2768b042616130cff2b19698a5ed82 [PR build]

…kcode' into dev/rolf/use-dynamic-dependency-attributes-optimizegeneratedcode
@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
Collaborator

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

Pipeline on Agent
Hash: b6ae4b2bead117e033a0141e1cf689e8a0554a07 [PR build]

@vs-mobiletools-engineering-service2
Copy link
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: b6ae4b2bead117e033a0141e1cf689e8a0554a07 [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

This comment has been minimized.

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