Skip to content

Add support for "extended" layouts in the runtimes and add CStruct layout #116082

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

Open
wants to merge 20 commits into
base: main
Choose a base branch
from

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

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

Copy link
Contributor

@Copilot 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)

public partial class ExternalException : System.SystemException
{
public ExternalException() { }
[System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)]
[System.ObsoleteAttribute("This API supports obsolete formatter-based serialization. It should not be called or extended by application code.", DiagnosticId="SYSLIB0051", UrlFormat="https://aka.ms/dotnet-warnings/{0}")]
[System.ObsoleteAttribute("This API supports obsolete formatter-based serialization. It should not be called or extended by application code.", DiagnosticId = "SYSLIB0051", UrlFormat = "https://aka.ms/dotnet-warnings/{0}")]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
[System.ObsoleteAttribute("This API supports obsolete formatter-based serialization. It should not be called or extended by application code.", DiagnosticId = "SYSLIB0051", UrlFormat = "https://aka.ms/dotnet-warnings/{0}")]
[System.ObsoleteAttribute("This API supports obsolete formatter-based serialization. It should not be called or extended by application code.", DiagnosticId="SYSLIB0051", UrlFormat="https://aka.ms/dotnet-warnings/{0}")]

Nit: Canonical formatting generated by genapi tool does not have spaces.

@@ -14202,11 +14203,21 @@ protected virtual void Dispose(bool disposing) { }
protected void SetHandle(System.IntPtr handle) { }
public void SetHandleAsInvalid() { }
}

[System.AttributeUsageAttribute(System.AttributeTargets.Struct, Inherited = false)]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
[System.AttributeUsageAttribute(System.AttributeTargets.Struct, Inherited = false)]
[System.AttributeUsageAttribute(System.AttributeTargets.Struct, Inherited=false)]

{
return ComputeAutoFieldLayout(type, numInstanceFields);
}
MetadataLayoutKind.Explicit => ComputeExplicitFieldLayout(type, numInstanceFields),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pass the result of type.GetClassLayout() to the worked methods to avoid recomputing it? (All except ComputeCStructFieldLayout needs it on some path.) It can be passed around as in since it is likely going grow into a large struct over time.

// CStruct cannot have zero size.
ThrowHelper.ThrowTypeLoadException(ExceptionStringID.ClassLoadBadFormat, type);
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need InlineArray handling here?

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