Skip to content

ILC: only keep type loader candidate type map entries when their generic arguments are present - #137541

Merged
AaronRobinsonMSFT merged 9 commits into
dotnet:mainfrom
Sergio0694:user/sergiopedri/ilc-gate-type-loader-type-map-keys-main
Nov 20, 2026
Merged

AaronRobinsonMSFT merged 9 commits into
dotnet:mainfrom
Sergio0694:user/sergiopedri/ilc-gate-type-loader-type-map-keys-main

Conversation

@Sergio0694

Copy link
Copy Markdown
Contributor

Two NativeAOT rules keep a generic type map key alive whenever the type loader could create it at runtime (the key has a reference type generic argument and a type loader template exists for its canonical form):

  • ProxyTypeMapNode: "Proxy map entry that could be loaded at runtime"
  • RuntimeConstructableTypeDependencies.AddTypeLoaderDependencies (external type map trim targets and UnmanagedCallersOnly associated source types)

In apps with many generic instantiations over reference types, this keeps a large number of types alive even when their generic arguments are never used anywhere else.

This routes both rules through a new TypeLoaderArgumentGateNode, which keeps the key only once every generic argument is present in the compilation or could itself be created by the type loader (arrays of a present element type, and generic types that have a template and whose own arguments pass the same check). A key with an argument that can't exist at runtime can't be created by the type loader either, so there is nothing to keep.

Size impact

Microsoft Store (CsWinRT 3.0, about 3,000 WinRT generic instantiations), WinStore.App.dll, retail, OptimizationPreference=Size:

Toolchain Before After Saved
main (net12.0) 108,382,720 B 84,961,792 B 23.4 MB (-21.6%)
release/11.0 108,068,352 B 84,881,408 B 23.2 MB (-21.5%)

Testing

  • Added a smoke test to TrimmingBehaviors covering proxy and external type map keys over present arguments, never-used arguments, and runtime-created generic and array arguments.
  • NativeAOT smoke tests (nativeaot tree, 28/28), Interop/TypeMap apps as NativeAOT, and the ILCompiler TypeSystem, DependencyAnalysisFramework and Trimming unit tests pass (4 Trimming tests fail locally because ilasm isn't built in my tree, unrelated).
  • The Store built with this compiler was smoke tested in Windows Sandbox (tabs, search, product pages, settings) with no crashes or missing content.

Note

This PR description was drafted with GitHub Copilot.

Sergio0694 and others added 2 commits November 14, 2026 01:46
…ric arguments are present

The type map rules that keep a generic key alive when the type loader could create it at runtime
(proxy type map entries and the external type map trim targets) currently create the key whenever its
canonical form has a template. In apps with many generic instantiations over reference types, this keeps a
large number of types alive even when their generic arguments are never used.

Route both rules through a new TypeLoaderArgumentGateNode, which keeps the key only once every generic
argument is present in the compilation or could itself be created by the type loader (arrays of a present
element type, and generic types with a template whose own arguments pass the gate).

Co-authored-by: Copilot App <[email protected]>
@dotnet-policy-service dotnet-policy-service Bot added the community-contribution Indicates that the PR has been added by a community member label Nov 14, 2026
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 3 pipeline(s).
12 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @agocke, @dotnet/ilc-contrib
See info in area-owners.md if you want to be subscribed.

@Sergio0694
Sergio0694 marked this pull request as ready for review November 14, 2026 09:50
@Sergio0694

Copy link
Copy Markdown
Contributor Author

/azp run runtime-nativeaot-outerloop

@azure-pipelines

Copy link
Copy Markdown
Commenter does not have sufficient privileges for PR 137541 in repo dotnet/runtime

@jkotas

jkotas commented Nov 14, 2026

Copy link
Copy Markdown
Member

/azp run runtime-nativeaot-outerloop

@azure-pipelines

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

Split the gate into TypeLoaderArgumentGateNode, whose index is always a real
generic argument position, and TypeLoaderLoadableTypeNode, which means that the
type loader can create a type. The chain of gates for a type is shared by all
callers, and the callers condition on the loadable node. Route the proxy type
map and the nested generic arguments through the same AddTypeLoaderDependencies
helper, and document why a chain with one node per argument is needed.

Co-authored-by: Copilot App <[email protected]>
Sergio0694 and others added 2 commits November 14, 2026 17:30
… type

The type loader can create multi-dimensional arrays, pointers and byrefs out of
their parameter type, but single-dimensional arrays also need a template. Use
GetEffectiveTrimTargetType to pick the type that a generic argument of a type
loader candidate is conditioned on, and let AddTypeLoaderDependencies handle
arguments that are arrays with a template. Add a test for an array of a struct
that is never created.

Co-authored-by: Copilot App <[email protected]>

@MichalStrehovsky MichalStrehovsky left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The ladder is pretty elegant. I hadn't thought about that when I scoped trimming these generic cases out as "too complicated for questionable benefit": #128180 (comment)

The last step of the chain of TypeLoaderArgumentGateNode nodes for a type already
means that the type can be created by the type loader, so use it directly through
GetLast instead of a separate node type. This also removes the special case for
types without generic arguments.

Co-authored-by: Copilot App <[email protected]>
@MichalStrehovsky
MichalStrehovsky requested a balanced review from Copilot November 17, 2026 06:10

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

Array gates skip element-type checks, retaining generic-struct array entries with otherwise unused type arguments and defeating trimming.

1 open issue
What changed in this PR

This PR reduces NativeAOT type-map retention by requiring generic arguments to be present or runtime-creatable before eagerly retaining type-loader candidate keys.

Changes:

  • Adds cached dependency gates that check generic arguments recursively.
  • Routes proxy and external type-map candidate retention through the shared gating logic.
  • Adds smoke tests for present, missing, generic, and array arguments.
File Description
src/​tests/​nativeaot/​SmokeTests/​TrimmingBehaviors/​DeadCodeElimination.cs Adds type-map retention and trimming scenarios.
src/​coreclr/​tools/​aot/​ILCompiler.Compiler/​ILCompiler.Compiler.csproj Includes the new gate node in compilation.
src/​coreclr/​tools/​aot/​ILCompiler.Compiler/​Compiler/​RuntimeConstructableTypeDependencies.cs Conditions candidate retention on template and argument gates.
src/​coreclr/​tools/​aot/​ILCompiler.Compiler/​Compiler/​DependencyAnalysis/​TypeLoaderArgumentGateNode.cs Implements recursive argument availability checks.
src/​coreclr/​tools/​aot/​ILCompiler.Compiler/​Compiler/​DependencyAnalysis/​ProxyTypeMapNode.cs Uses shared gating for proxy map keys.
src/​coreclr/​tools/​aot/​ILCompiler.Compiler/​Compiler/​DependencyAnalysis/​NodeFactory.cs Caches gate nodes by type and argument index.

Review effort: Balanced

Arrays have no generic arguments, so the gate for an array was already the last
one and never checked the element type. A key over an array of a generic type
with a missing argument was kept as long as the array template was present. Use
the element type as the single argument of an array, and add tests for arrays of
a generic struct with a missing and with a present argument.

Co-authored-by: Copilot App <[email protected]>
auto-merge was automatically disabled November 17, 2026 17:45

Head branch was pushed to by a user without write access

@Sergio0694

Copy link
Copy Markdown
Contributor Author

@MichalStrehovsky I pushed a fix to handle that array scenario better, can you re-stamp it if it looks fine? 😄

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🔵 Needs a closer look

The smoke tests cover only single-argument keys, leaving multi-argument AND-chain behavior unverified and allowing regressions that retain entries after checking only the first argument to pass.

No open issues 🎉

1 previously missed issue

In code that hasn't changed since last review

Medium severity Add multi-argument tests for AND-chain gating

src/​coreclr/​tools/​aot/​ILCompiler.Compiler/​Compiler/​DependencyAnalysis/​TypeLoaderArgumentGateNode.cs:68

The new smoke-test keys all have one generic parameter, so none exercises the AND-chain across multiple arguments. A regression that advances straight to the terminal gate after checking only the first argument would still pass these tests. Please add two-argument proxy and external-map cases with both arguments present, only the first present, and only the second present, asserting that only the fully satisfiable entries survive.

1 resolved since last review

Review effort: Balanced

@MichalStrehovsky

Copy link
Copy Markdown
Member

Could you add a test that copilot identified? ("The new smoke-test keys all have one generic parameter, so none exercises the AND-chain across multiple arguments")

Maybe also do a pass over the test: the tests that assert X was kept should not reuse the X for another "was X kept?" assert because then we just need one of the codepaths to keep X for the test to succeed.

Add keys with two generic arguments to the type loader key smoke test, covering
both arguments present, only the first or the second present, both created by
the type loader, and a loader created first argument with a missing second one,
for the proxy and external type maps.

A kept key references its generic arguments, which makes them present, so the
proxy and external cases that rely on an argument being created by the type
loader now use different instantiations. Otherwise one of the rules keeping its
key could make the other one pass.

Co-authored-by: Copilot App <[email protected]>
@Sergio0694

Copy link
Copy Markdown
Contributor Author

@MichalStrehovsky done in 9d7849a!

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🔵 Needs a closer look

Recursive reachability pruning affects NativeAOT type maps and UnmanagedCallersOnly exports, requiring human validation of constructibility and end-to-end loader behavior because compiler and smoke tests cannot run here.

No open issues 🎉

Review effort: Balanced

@AaronRobinsonMSFT

Copy link
Copy Markdown
Member

/ba-g osx arm64 deadletter

@AaronRobinsonMSFT
AaronRobinsonMSFT merged commit 4080a13 into dotnet:main Nov 20, 2026
97 of 101 checks passed
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 12.0-preview1 milestone Nov 23, 2026
Sergio0694 added a commit to Sergio0694/runtime that referenced this pull request Nov 24, 2026
Backport of the final test update of dotnet#137541: add the multi-argument cases for the proxy and external type map rules, and use distinct generic arguments for the proxy and external cases so retaining one map's key cannot mask a failure in the other.

Co-authored-by: Copilot App <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-NativeAOT-coreclr community-contribution Indicates that the PR has been added by a community member

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants