Skip to content

Allow NecessaryTypeSymbol in generic dictionaries - #117315

Merged
MichalStrehovsky merged 4 commits into
dotnet:mainfrom
MichalStrehovsky:necessaryindict
Jul 11, 2025
Merged

Allow NecessaryTypeSymbol in generic dictionaries#117315
MichalStrehovsky merged 4 commits into
dotnet:mainfrom
MichalStrehovsky:necessaryindict

Conversation

@MichalStrehovsky

@MichalStrehovskyMichalStrehovsky commented Jul 4, 2025

Copy link
Copy Markdown
Member

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-actionsgithub-actionsBot added the needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners label Jul 4, 2025
@github-actionsgithub-actionsBot mentioned this pull request Jul 4, 2025
@dotnet-policy-service

Copy link
Copy Markdown
Contributor

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

@MichalStrehovsky

Copy link
Copy Markdown
MemberAuthor

/azp run runtime-nativeaot-outerloop

@azure-pipelines

Copy link
Copy Markdown
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
Copy Markdown
MemberAuthor

/azp run runtime-nativeaot-outerloop

@azure-pipelines

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

@MichalStrehovsky
MichalStrehovsky marked this pull request as ready for review July 9, 2025 05:36
CopilotAI review requested due to automatic review settings July 9, 2025 05:36

CopilotAI 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.

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
FileDescription
src/tests/nativeaot/SmokeTests/TrimmingBehaviors/DeadCodeElimination.csAdded TestTypeHandlesInGenericDictionaries test
src/libraries/System.Linq.Expressions/tests/default.rd.xmlExposed trimming for DayOfWeek[] and its nullable
src/coreclr/tools/aot/ILCompiler.RyuJit/JitInterface/CorInfoImpl.RyuJit.csHandle Method token and runtime‐determined types
src/coreclr/tools/aot/ILCompiler.Compiler/IL/ILImporter.Scanner.csSwap to NecessaryTypeHandle for unbox, ldtoken, ldelema
src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/ILScanner.csDeduplicate necessary vs. constructed type lookups
Multiple target‐specific helper nodesAdded case for NecessaryTypeHandle in emitters
src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/DependencyAnalysis/ReadyToRunGenericHelperNode.csSupport NecessaryTypeHandle in lookup signatures
src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/DependencyAnalysis/NodeFactory.GenericLookups.csNew cache and API for necessary type lookups
src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/DependencyAnalysis/GenericLookupResult.csIntroduced NecessaryTypeHandleGenericLookupResult
src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/DependencyAnalysis/DictionaryLayoutNode.csAllow fallback from necessary to constructed slot
src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/Compilation.csRemoved 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
Copy Markdown
MemberAuthor

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

@jkoritzinsky

Copy link
Copy Markdown
Member

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

@MichalStrehovsky

Copy link
Copy Markdown
MemberAuthor

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:mainJul 11, 2025
@MichalStrehovsky
MichalStrehovsky deleted the necessaryindict branch July 11, 2025 11:50
@github-actionsgithub-actionsBot locked and limited conversation to collaborators Aug 11, 2025
Sign up for freeto 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.

4 participants

@MichalStrehovsky@jkoritzinsky@teo-tsirpanis