Skip to content

Conversation

@jkoritzinsky
Copy link
Member

Add support for the ExtendedLayout flag, the System.Runtime.InteropServices.ExtendedLayoutAttribute and the CStruct layout from #100896.

Contributes to #100896

Leaving other layouts from #100896 for later PRs to minimize diffs.

Copy link
Contributor

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 an “extended” layout flag in the runtimes, introduces the ExtendedLayoutAttribute and CStruct layout kind, and wires through layout info across types and metadata emitters.

  • Add IsExtendedLayout and GetExtendedLayoutInfo APIs to core metadata types
  • Implement ComputeCStructFieldLayout and dispatch in field layout algorithms
  • Update metadata headers, ILASM grammar, and reflection emit to recognize extended layout

Reviewed Changes

Copilot reviewed 45 out of 45 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/CompilerMetadataFieldLayoutAlgorithm.cs Dispatch ComputeExtendedFieldLayout before explicit layout
src/coreclr/tools/Common/TypeSystem/Interop/IL/PInvokeDelegateWrapper.cs Stub out IsExtendedLayout & GetExtendedLayoutInfo
src/coreclr/tools/Common/TypeSystem/Interop/IL/NativeStructType.cs Override extended layout members and simplify GetClassLayout
src/coreclr/tools/Common/TypeSystem/Interop/IL/MarshalUtils.cs Treat CStruct as blittable
src/coreclr/tools/Common/TypeSystem/Interop/IL/InlineArrayType.cs Stub out IsExtendedLayout & GetExtendedLayoutInfo
src/coreclr/tools/Common/TypeSystem/Ecma/EcmaType.cs Parse ExtendedLayoutAttribute and implement IsExtendedLayout
src/coreclr/tools/Common/TypeSystem/Common/TypeWithRepeatedFields.cs Forward extended layout members
src/coreclr/tools/Common/TypeSystem/Common/TypeSystemHelpers.cs Include IsExtendedLayout in HasLayout
src/coreclr/tools/Common/TypeSystem/Common/MetadataType.cs Add abstract IsExtendedLayout, GetExtendedLayoutInfo, and supporting types
src/coreclr/tools/Common/TypeSystem/Common/MetadataFieldLayoutAlgorithm.cs Implement ComputeCStructFieldLayout and ComputeExtendedFieldLayout
src/coreclr/tools/Common/TypeSystem/Common/InstantiatedType.Metadata.cs Override extended layout members
src/coreclr/tools/Common/TypeSystem/Canon/CanonTypes.Metadata.cs Stub out extended layout members
src/coreclr/nativeaot/Runtime.Base/src/System/Runtime/InteropServices/LayoutKind.cs Add LayoutKind.Extended enum
src/coreclr/md/compiler/custattr_emit.cpp Handle tdExtendedLayout in custom attr emitter
src/coreclr/inc/il_kywd.h Add IL keyword extended
src/coreclr/inc/corhdr.h Define tdExtendedLayout and IsTdExtendedLayout
src/coreclr/ilasm/prebuilt/asmparse.grammar Recognize extended in grammar
src/coreclr/ilasm/asmparse.y Add EXTENDED_ token and grammar rules
src/coreclr/System.Private.CoreLib/src/System/Reflection/RuntimeCustomAttributeData.cs Map ExtendedLayout to LayoutKind.Extended
src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/RuntimeTypeBuilder.cs Remove obsolete layout-mask validation
Comments suppressed due to low confidence (1)

src/coreclr/tools/Common/TypeSystem/Common/MetadataFieldLayoutAlgorithm.cs:502

  • There are no unit tests covering the new CStruct layout path (ComputeCStructFieldLayout and ComputeExtendedFieldLayout). Adding tests for valid field offsets, alignment boundaries, and error conditions will help catch regressions.
private static ComputedInstanceFieldLayout ComputeCStructFieldLayout(MetadataType type, int numInstanceFields)

@jkoritzinsky
Copy link
Member Author

I'll update the Ecma augments doc. It looks like we don't actually need to do any updates on Cecil to ensure correct behavior. Cecil reads layout info directly from the attributes bits, handles masked values right, and doesn't do any validation of valid or invalid values.

Cecil could use nice APIs to read if a type is ExtendedLayout, but it isn't required. Based on my tests, ILSpy will need to react to accurately represent the bits in their decompilation, but that's expected.

@jkotas
Copy link
Member

jkotas commented Jul 2, 2025

This looks good to me otherwise.

Is your Roslyn PR on track for .NET 10? We should make sure that either both this PR and Roslyn PR are in .NET 10, or neither.

@jkoritzinsky
Copy link
Member Author

I'm waiting on @jaredpar to let me know if Roslyn will have the resources to review the PR over there for this release.

@jkoritzinsky
Copy link
Member Author

/azp run runtime

@jkoritzinsky jkoritzinsky added this to the 11.0.0 milestone Aug 15, 2025
@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@jkoritzinsky
Copy link
Member Author

@jkotas now that we're in the .NET 11 time frame, can we get this merged in? Or do you want to wait until the Roslyn PR goes in before we get this in?

@jkotas
Copy link
Member

jkotas commented Sep 4, 2025

Fine with me.

Is there an ETA for the Roslyn PR merge?

@jkoritzinsky
Copy link
Member Author

Still need to work with @jaredpar on exact timing. Their main branch opens for .NET 11 in Sep/Oct timeframe. I'd like to get it in by Nov but it might slip until after the holidays due to vacations/leave/etc.

@jkoritzinsky jkoritzinsky enabled auto-merge (squash) September 4, 2025 22:39
@jkoritzinsky
Copy link
Member Author

/ba-g websockets failure and linker failures unrelated

@jkoritzinsky jkoritzinsky merged commit f0281d9 into dotnet:main Sep 5, 2025
170 of 176 checks passed
@jkoritzinsky jkoritzinsky deleted the extended-layout branch September 5, 2025 18:29
@github-actions github-actions bot locked and limited conversation to collaborators Oct 6, 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