Skip to content

Conversation

@jkoritzinsky
Copy link
Member

Implement support for the CUnion layout kind mentioned in #100896.

This layout kind places all fields at offset 0, like a C union type. It only supports unmanaged fields with non-auto layout, like CStruct.

Contributes to #100896

@dotnet-policy-service
Copy link
Contributor

Tagging subscribers to this area: @dotnet/interop-contrib
See info in area-owners.md if you want to be subscribed.

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 pull request implements support for the CUnion extended layout kind, which allows defining value types with C-style union semantics where all fields are placed at offset 0 and the type's size is determined by the largest field.

Key changes:

  • Adds CUnion = 1 to the ExtendedLayoutKind enum across all implementations (CoreCLR, Mono, and TypeSystem)
  • Implements field layout logic that places all fields at offset 0 and calculates size as the maximum field size with proper alignment
  • Adds comprehensive validation including checks for GC pointers, auto-layout fields, empty unions, and inline arrays
  • Includes extensive test coverage with IL type definitions and C# tests

Reviewed changes

Copilot reviewed 18 out of 18 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/ExtendedLayoutKind.cs Adds CUnion enum value with documentation
src/libraries/System.Runtime/ref/System.Runtime.cs Adds CUnion enum value to reference assembly
src/coreclr/inc/corhdr.h Adds CUnion to CorExtendedLayoutKind enum
src/coreclr/vm/class.h Adds CUnion layout type and InitializeCUnionFieldLayout method
src/coreclr/vm/classlayoutinfo.cpp Implements CUnion field layout initialization logic
src/coreclr/vm/methodtablebuilder.h Declares HandleCUnionLayout method
src/coreclr/vm/methodtablebuilder.cpp Implements CUnion layout handling with validation
src/coreclr/tools/Common/TypeSystem/Common/MetadataType.cs Adds CUnion to MetadataLayoutKind enum
src/coreclr/tools/Common/TypeSystem/Common/MetadataFieldLayoutAlgorithm.cs Implements CUnion field layout computation
src/coreclr/tools/Common/TypeSystem/Ecma/EcmaType.cs Handles CUnion kind value parsing from metadata
src/coreclr/tools/Common/TypeSystem/Interop/IL/MarshalUtils.cs Treats CUnion as blittable like CStruct
src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/CompilerMetadataFieldLayoutAlgorithm.cs Adds CUnion case to layout computation
src/coreclr/tools/aot/ILCompiler.ReadyToRun/Compiler/ReadyToRunMetadataFieldLayoutAlgorithm.cs Adds CUnion case to layout computation
src/coreclr/tools/aot/ILCompiler.TypeSystem.Tests/TestMetadataFieldLayoutAlgorithm.cs Adds CUnion case to test layout algorithm
src/mono/mono/metadata/class-init.c Implements CUnion layout logic in Mono runtime
src/tests/Loader/classloader/ExtendedLayout/ExtendedLayoutTypes.il Defines CUnion test types in IL
src/tests/Loader/classloader/ExtendedLayout/CUnion.cs Adds comprehensive test cases for CUnion functionality
src/tests/Loader/classloader/ExtendedLayout/ExtendedLayout.csproj References new CUnion test file

@jkotas
Copy link
Member

jkotas commented Jan 9, 2026

Are there any places under libraries that can take advantage of this?

@jkoritzinsky
Copy link
Member Author

I was going to open a follow-up issue for us to start using this across the libraries code once we are using an SDK with a new enough Roslyn to support ExtendedLayout.

In particular, ComVariant immediately comes to mind. With a cursory search, it looks like there's a few interop types (like IO_STATUS, CMSG_KEY_AGREE_RECIPIENT_INFO_ORIGINATOR_UNION) that pop as well as the implementation types of [JSImport]/[JSExport].

I'm sure that there's more (most if not all usages of LayoutKind.Explicit in the libraries tree can probably be moved, as well as a number in CoreLib).

@jkotas
Copy link
Member

jkotas commented Jan 10, 2026

I was going to open a follow-up issue for us to start using this across the libraries code once we are using an SDK with a new enough Roslyn to support ExtendedLayout.

Ah ok, I have not realized that we do not have Roslyn with the supporting changes yet.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

2 participants