Skip to content

Conversation

MichalStrehovsky
Copy link
Member

@MichalStrehovsky MichalStrehovsky commented Jul 4, 2025

Before this change, whenever we needed to refer to a type from a generic dictionary, we used the fully loaded "allocated" form of it. But we can do the same optimization we do for non-generic code where we distinguish between MethodTables that we need for casting-like operations and MethodTables that are actually for allocation.

Cc @dotnet/ilc-contrib

@github-actions github-actions bot added the needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners label Jul 4, 2025
Copy link
Contributor

Tagging subscribers to this area: @agocke, @MichalStrehovsky, @jkotas
See info in area-owners.md if you want to be subscribed.

@MichalStrehovsky
Copy link
Member Author

/azp run runtime-nativeaot-outerloop

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

AOT-incompatible code that worked due to luck.

```
System.NotSupportedException : 'System.Nullable`1[System.DayOfWeek][]' is missing native code or metadata. This can happen for code that is not compatible with trimming or AOT. Inspect and fix trimming and AOT related warnings that were generated when the app was published. For more information see https://aka.ms/nativeaot-compatibility
   at Internal.Runtime.Augments.RuntimeAugments.EnsureMethodTableSafeToAllocate(MethodTable*) + 0x58
   at Internal.Runtime.Augments.RuntimeAugments.NewArray(RuntimeTypeHandle, Int32) + 0x1c
   at System.Array.InternalCreate(RuntimeType elementType, Int32 rank, Int32* pLengths, Int32* pLowerBounds) + 0x1f8
   at System.Array.CreateInstance(Type elementType, Int32 length) + 0x5c
   at System.Linq.Expressions.Interpreter.NewArrayInitInstruction.Run(InterpretedFrame) + 0x24
   at System.Linq.Expressions.Interpreter.Interpreter.Run(InterpretedFrame) + 0x48
   at System.Linq.Expressions.Interpreter.LightLambda.Run(Object[] arguments) + 0x90
```
@MichalStrehovsky
Copy link
Member Author

/azp run runtime-nativeaot-outerloop

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@MichalStrehovsky MichalStrehovsky marked this pull request as ready for review July 9, 2025 05:36
@Copilot Copilot AI review requested due to automatic review settings July 9, 2025 05:36
Copy link
Contributor

@Copilot 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 adds support for using NecessaryTypeSymbol in generic dictionaries to optimize non-allocating type loads and extends this across the JIT interface, IL importer, dictionary layout, codegen backends, and the generic lookup infrastructure. It also introduces a smoke test to validate trimming behavior for necessary type handles in generic dictionaries.

  • Introduced TestTypeHandlesInGenericDictionaries smoke test in native AOT trimming behaviors.
  • Updated CorInfoImpl.RyuJit and ILImporter.Scanner to request NecessaryTypeHandle where appropriate.
  • Extended generic lookup nodes, dictionary layout, and target-specific helper emitters to handle NecessaryTypeHandle.

Reviewed Changes

Copilot reviewed 16 out of 16 changed files in this pull request and generated no comments.

Show a summary per file
File Description
src/tests/nativeaot/SmokeTests/TrimmingBehaviors/DeadCodeElimination.cs Added TestTypeHandlesInGenericDictionaries test
src/libraries/System.Linq.Expressions/tests/default.rd.xml Exposed trimming for DayOfWeek[] and its nullable
src/coreclr/tools/aot/ILCompiler.RyuJit/JitInterface/CorInfoImpl.RyuJit.cs Handle Method token and runtime‐determined types
src/coreclr/tools/aot/ILCompiler.Compiler/IL/ILImporter.Scanner.cs Swap to NecessaryTypeHandle for unbox, ldtoken, ldelema
src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/ILScanner.cs Deduplicate necessary vs. constructed type lookups
Multiple target‐specific helper nodes Added case for NecessaryTypeHandle in emitters
src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/DependencyAnalysis/ReadyToRunGenericHelperNode.cs Support NecessaryTypeHandle in lookup signatures
src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/DependencyAnalysis/NodeFactory.GenericLookups.cs New cache and API for necessary type lookups
src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/DependencyAnalysis/GenericLookupResult.cs Introduced NecessaryTypeHandleGenericLookupResult
src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/DependencyAnalysis/DictionaryLayoutNode.cs Allow fallback from necessary to constructed slot
src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/Compilation.cs Removed normalization of necessary to type handles
Comments suppressed due to low confidence (1)

src/tests/nativeaot/SmokeTests/TrimmingBehaviors/DeadCodeElimination.cs:1136

  • C# requires nested class declarations to include a body. Replace class InvisibleType1; with class InvisibleType1 {} (and similarly for InvisibleType2 and VisibleType1) to ensure valid syntax and successful compilation.
        class InvisibleType1;

@MichalStrehovsky
Copy link
Member Author

@jkoritzinsky would you be comfortable reviewing this? (We can also do this over a Teams call.)

@jkoritzinsky
Copy link
Member

I'll take a look today. If I have questions I'll let you know.

@MichalStrehovsky
Copy link
Member Author

Mmm, segfault in the macos leg.

2025-07-09T05:52:37.6560490Z   /var/folders/vk/nx37ffx50hv5djclhltc26vw0000gn/T/MSBuildTemprunner/tmpf745a23dd5cb487fb7a12111876bc729.exec.cmd: line 2: 17738 Segmentation fault: 11  "/Users/runner/work/1/s/artifacts/bin/coreclr/osx.x64.Release/x64/ilc/ilc" @"/Users/runner/work/1/s/artifacts/obj/System.Diagnostics.Contracts.Tests/Release/net10.0/native/System.Diagnostics.Contracts.Tests.ilc.rsp"
2025-07-09T05:52:37.7184620Z /Users/runner/work/1/s/artifacts/bin/coreclr/osx.x64.Release/build/Microsoft.NETCore.Native.targets(334,5): error MSB3073: The command ""/Users/runner/work/1/s/artifacts/bin/coreclr/osx.x64.Release/x64/ilc/ilc" @"/Users/runner/work/1/s/artifacts/obj/System.Diagnostics.Contracts.Tests/Release/net10.0/native/System.Diagnostics.Contracts.Tests.ilc.rsp"" exited with code 139. 

I'll re-trigger that leg just in case.

@MichalStrehovsky MichalStrehovsky merged commit 61fc02b into dotnet:main Jul 11, 2025
151 of 157 checks passed
@MichalStrehovsky MichalStrehovsky deleted the necessaryindict branch July 11, 2025 11:50
@github-actions github-actions bot locked and limited conversation to collaborators Aug 11, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants