You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
ILC: only keep type loader candidate type map entries when their generic arguments are present - #137541
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.
…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]>
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.
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]>
… 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]>
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]>
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.
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]>
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.
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.
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]>
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.
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]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 andUnmanagedCallersOnlyassociated 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:Testing
TrimmingBehaviorscovering proxy and external type map keys over present arguments, never-used arguments, and runtime-created generic and array arguments.nativeaottree, 28/28),Interop/TypeMapapps as NativeAOT, and the ILCompiler TypeSystem, DependencyAnalysisFramework and Trimming unit tests pass (4 Trimming tests fail locally becauseilasmisn't built in my tree, unrelated).Note
This PR description was drafted with GitHub Copilot.