Skip to content

Simplify nativeaot typeloader - #134843

Merged
MichalStrehovsky merged 6 commits into
dotnet:mainfrom
MichalStrehovsky:simplify-nativeaot-typeloader
Oct 2, 2026
Merged

MichalStrehovsky merged 6 commits into
dotnet:mainfrom
MichalStrehovsky:simplify-nativeaot-typeloader

Conversation

@MichalStrehovsky

@MichalStrehovsky MichalStrehovsky commented Sep 29, 2026 •

Copy link
Copy Markdown
Member

Fixes 3 bugs.
Deletes 10+% of the runtime type loader code.

MichalStrehovsky and others added 3 commits September 29, 2026 10:29
Remove obsolete loader state and lookup abstractions, reuse template layouts, and preserve compiler-emitted interface slots. Correct multidimensional array component-size replacement and add active regression coverage. Retain lazy-generics support and leave the shared Common TypeSystem unchanged.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot-Session: 20d0d104-fdd3-458f-985d-ea71d12d0065
Use resolved base dictionaries instead of cached presence checks, remove unused MethodTable setters and metadata-loading fallbacks, inline invoke-map parsing, and omit the unused native-format writer source. Add coverage for empty and reused base dictionaries. Preserve lazy generics and defer GCDesc emission changes.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 20d0d104-fdd3-458f-985d-ea71d12d0065
Translate boxed element GC series directly into array descriptors, removing the intermediate bitmap and its scanning helpers. Extend MD-array GC layout coverage.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: c1b4a0f0-8f10-4475-9efb-ef1edbac54d6
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 3 pipeline(s).
13 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.

@MichalStrehovsky

Copy link
Copy Markdown
Member Author

/azp run runtime-nativeaot-outerloop

@azure-pipelines

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

The array GCDesc "val_serie" encoding stores per-series run (nptrs) and
skip counts in HALF_SIZE_T fields, which are uint32 on 64-bit targets.
Both encoders wrote these fields by casting through a signed short, which
sign-extended values in [32768, 65535] into 0xFFFFxxxx. The GC then walked
such arrays with a bogus ~4GB stride, dropping or corrupting element
references (and crashing) for element types with two GC references
separated by a 32KB-64KB non-pointer gap.

Cast through unsigned types (ushort/uint) so the values zero-extend:
 * GCDescEncoder / ObjectDataBuilder (AOT compiler path): rename
   EmitHalfNaturalInt(short) to EmitHalfNaturalUInt(ushort).
 * EETypeCreator.CreateMdArrayGCDesc (runtime type loader path): use
   unsigned pointer and cast types.

The 32-bit path is byte-identical (the field is exactly uint16 there).

Adds a regression test to the NativeAOT UnitTests smoke test exercising
both encoder paths with a compacting GC.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: badcb654-1848-4160-b6f8-c6fad59803d7
@MichalStrehovsky
MichalStrehovsky marked this pull request as ready for review September 30, 2026 08:22
@MichalStrehovsky

Copy link
Copy Markdown
Member Author

/azp run runtime-nativeaot-outerloop

@azure-pipelines

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

The type loader was previously happy to construct MethodTable for e.g. `int&[]` (considering it a reference type element). Now it asserts.

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.

Copilot review overview

🟡 Changes recommended

The new GC and interface-map regressions do not yet deterministically cover all affected configurations.

Review effort: Balanced
Findings: 2 Low severity

Open (2)
What changed in this PR

Simplifies NativeAOT type loading by deriving runtime layout data directly from canonical templates and fixes GC descriptor, interface-map, and generic hierarchy handling.

Changes:

  • Removes redundant type-loader state and lookup abstractions.
  • Reuses template layouts for vtables, interfaces, statics, and arrays.
  • Adds regression coverage for dynamic generics and array GC layouts, with two test-quality gaps noted.
File Description
src/​tests/​nativeaot/​SmokeTests/​UnitTests/​MiscTests.cs Adds large-struct array GC regression.
src/​tests/​nativeaot/​SmokeTests/​UnitTests/​Generics.cs Tests hierarchy dictionaries and template array layouts.
src/​tests/​nativeaot/​SmokeTests/​DynamicGenerics/​interfaces.cs Extends collapsed-interface tests.
src/​tests/​nativeaot/​SmokeTests/​DynamicGenerics/​arrays.cs Expands dynamic multidimensional-array coverage.
src/​coreclr/​tools/​Common/​Internal/​Runtime/​MappingTableFlags.cs Removes unused mapping flags.
src/​coreclr/​tools/​Common/​Internal/​Runtime/​ITargetBinaryWriter.cs Makes half-width emission unsigned.
src/​coreclr/​tools/​Common/​Internal/​Runtime/​GCDescEncoder.cs Prevents signed extension in GC descriptors.
src/​coreclr/​tools/​Common/​Internal/​NativeFormat/​NativeFormat.cs Retires unused static-size entries.
src/​coreclr/​tools/​Common/​Compiler/​DependencyAnalysis/​ObjectDataBuilder.cs Implements unsigned half-width emission.
src/​coreclr/​tools/​aot/​ILCompiler.Compiler/​Compiler/​VirtualMethodCallHelper.cs Simplifies base-slot calculation.
src/​coreclr/​tools/​aot/​ILCompiler.Compiler/​Compiler/​DependencyAnalysis/​ReflectionFieldMapNode.cs Removes obsolete canonical-entry flag.
src/​coreclr/​tools/​aot/​ILCompiler.Compiler/​Compiler/​DependencyAnalysis/​NativeLayoutVertexNode.cs Stops emitting redundant static sizes.
src/​coreclr/​tools/​aot/​ILCompiler.Compiler/​Compiler/​DependencyAnalysis/​EETypeNode.cs Simplifies template vtable traversal.
src/​coreclr/​nativeaot/​System.Private.TypeLoader/​src/​System.Private.TypeLoader.csproj Removes deleted source files.
src/​coreclr/​nativeaot/​System.Private.TypeLoader/​src/​Internal/​TypeSystem/​TypeSystemContext.Runtime.cs Removes obsolete template accessor.
src/​coreclr/​nativeaot/​System.Private.TypeLoader/​src/​Internal/​TypeSystem/​TypeDesc.Runtime.cs Requires templates consistently.
src/​coreclr/​nativeaot/​System.Private.TypeLoader/​src/​Internal/​TypeSystem/​RuntimeMethodDesc.cs Inherits directly from MethodDesc.
src/​coreclr/​nativeaot/​System.Private.TypeLoader/​src/​Internal/​TypeSystem/​NoMetadataMethodDesc.cs Deletes redundant abstraction.
src/​coreclr/​nativeaot/​System.Private.TypeLoader/​src/​Internal/​TypeSystem/​MethodDesc.Runtime.cs Removes unused shareability cache.
src/​coreclr/​nativeaot/​System.Private.TypeLoader/​src/​Internal/​TypeSystem/​InstantiatedMethod.Runtime.cs Removes redundant override.
src/​coreclr/​nativeaot/​System.Private.TypeLoader/​src/​Internal/​TypeSystem/​DefType.Runtime.cs Deletes obsolete native-layout helpers.
src/​coreclr/​nativeaot/​System.Private.TypeLoader/​src/​Internal/​Runtime/​TypeLoader/​TypeSystemExtensions.cs Removes unused generic-type helper.
src/​coreclr/​nativeaot/​System.Private.TypeLoader/​src/​Internal/​Runtime/​TypeLoader/​TypeLoaderTypeSystemContext.cs Uses native-layout interfaces for unbuilt types.
src/​coreclr/​nativeaot/​System.Private.TypeLoader/​src/​Internal/​Runtime/​TypeLoader/​TypeLoaderEnvironment.Metadata.cs Simplifies invoke-map lookup.
src/​coreclr/​nativeaot/​System.Private.TypeLoader/​src/​Internal/​Runtime/​TypeLoader/​TypeLoaderEnvironment.GVMResolution.cs Removes redundant GVM conversions and lookup.
src/​coreclr/​nativeaot/​System.Private.TypeLoader/​src/​Internal/​Runtime/​TypeLoader/​TypeLoaderEnvironment.FieldAccess.cs Removes unused module metadata.
src/​coreclr/​nativeaot/​System.Private.TypeLoader/​src/​Internal/​Runtime/​TypeLoader/​TypeLoaderEnvironment.cs Specializes canonicalization and lookups.
src/​coreclr/​nativeaot/​System.Private.TypeLoader/​src/​Internal/​Runtime/​TypeLoader/​TypeLoaderEnvironment.ConstructedGenericMethodsLookup.cs Replaces lookup class hierarchy with a struct.
src/​coreclr/​nativeaot/​System.Private.TypeLoader/​src/​Internal/​Runtime/​TypeLoader/​TypeBuilderState.cs Removes computed layout caches.
src/​coreclr/​nativeaot/​System.Private.TypeLoader/​src/​Internal/​Runtime/​TypeLoader/​TypeBuilder.cs Simplifies preparation and finishing.
src/​coreclr/​nativeaot/​System.Private.TypeLoader/​src/​Internal/​Runtime/​TypeLoader/​TemplateLocator.cs Specializes template lookup to specific canon.
src/​coreclr/​nativeaot/​System.Private.TypeLoader/​src/​Internal/​Runtime/​TypeLoader/​NativeLayoutInterfacesAlgorithm.cs Reads compiler-emitted slot ordering directly.
src/​coreclr/​nativeaot/​System.Private.TypeLoader/​src/​Internal/​Runtime/​TypeLoader/​ModuleList.cs Simplifies immutable module lookup.
src/​coreclr/​nativeaot/​System.Private.TypeLoader/​src/​Internal/​Runtime/​TypeLoader/​MetadataNameExtensions.cs Removes unused metadata-name support.
src/​coreclr/​nativeaot/​System.Private.TypeLoader/​src/​Internal/​Runtime/​TypeLoader/​GenericDictionaryCell.cs Removes redundant dictionary-write hook.
src/​coreclr/​nativeaot/​System.Private.TypeLoader/​src/​Internal/​Runtime/​TypeLoader/​GenericDictionary.cs Writes created cells directly.
src/​coreclr/​nativeaot/​System.Private.TypeLoader/​src/​Internal/​Runtime/​TypeLoader/​EETypeCreator.cs Copies template layouts and rebuilds MD-array GC descriptors.
src/​coreclr/​nativeaot/​System.Private.TypeLoader/​src/​Internal/​Runtime/​TypeLoader/​CanonicallyEquivalentEntryLocator.cs Uses specific-canonical helpers directly.
src/​coreclr/​nativeaot/​System.Private.Reflection.Execution/​src/​Internal/​Reflection/​Execution/​ExecutionEnvironmentImplementation.MappingTables.cs Removes obsolete invoke checks and wrapper.
src/​coreclr/​nativeaot/​System.Private.CoreLib/​src/​System/​Reflection/​Runtime/​General/​TypeUnifier.cs Centralizes array element validation.
src/​coreclr/​nativeaot/​Common/​src/​Internal/​Runtime/​MethodTable.cs Removes unused setters and safely replaces component size.

Comment thread src/tests/nativeaot/SmokeTests/DynamicGenerics/interfaces.cs
Comment thread src/tests/nativeaot/SmokeTests/UnitTests/MiscTests.cs Outdated
Comment thread src/coreclr/tools/Common/Internal/Runtime/MappingTableFlags.cs Outdated
Pack mapping flags, remove redundant template validation, strengthen GC descriptor assertions, and retain original references in GC regression tests. Account for the post-decremented pointer in the descriptor size assertion.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 63b522df-2411-47e5-a588-7dfa448e93e0
@MichalStrehovsky

Copy link
Copy Markdown
Member Author

/ba-g failure is #67070. we have tons of issues about this, all in the Future milestone https://github.com/dotnet/runtime/issues?q=is%3Aissue+state%3Aopen+Multiple_From_Watched_To_Unwatched_Mac

@MichalStrehovsky
MichalStrehovsky merged commit 32314b9 into dotnet:main Oct 2, 2026
115 of 117 checks passed
@MichalStrehovsky
MichalStrehovsky deleted the simplify-nativeaot-typeloader branch October 2, 2026 04:29
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 12.0-preview1 milestone Oct 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants