-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Implement support for ExtendedLayoutKind.CUnion #123052
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
|
Tagging subscribers to this area: @dotnet/interop-contrib |
There was a problem hiding this 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 = 1to theExtendedLayoutKindenum 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 |
|
Are there any places under libraries that can take advantage of this? |
|
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, 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). |
Co-authored-by: Copilot <[email protected]>
Ah ok, I have not realized that we do not have Roslyn with the supporting changes yet. |
Implement support for the CUnion layout kind mentioned in #100896.
This layout kind places all fields at offset
0, like a Cuniontype. It only supportsunmanagedfields with non-auto layout, likeCStruct.Contributes to #100896