Skip to content

Conversation

@radekdoulik
Copy link
Member

Fixes #122294

The default layout is now equal to the struct layout in C. The auto layout is updated in the same way.

Fixes dotnet#122294

The default layout is now equal to the C struct layout. The auto
layout is updated in the same way.
@radekdoulik radekdoulik added this to the 11.0.0 milestone Dec 22, 2025
@github-actions github-actions bot added the needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners label Dec 22, 2025
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 fixes struct layout alignment for WebAssembly (WASM) in CoreCLR to match C language struct layout rules. The changes ensure that when a type requires 8-byte alignment on WASM, the actual field alignment requirement is consulted rather than hardcoding the alignment to 8 bytes, which aligns the behavior with C struct layout semantics.

  • Updates auto layout handling to use actual field alignment requirements on WASM when types require 8-byte alignment
  • Updates default layout handling with the same WASM-specific logic for consistency

Reviewed changes

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

File Description
src/coreclr/vm/methodtablebuilder.cpp Adds TARGET_WASM-specific logic in auto layout to use max(8, GetFieldAlignmentRequirement()) instead of hardcoded 8-byte alignment
src/coreclr/vm/classlayoutinfo.cpp Adds TARGET_WASM-specific logic in default layout to use max(8, GetFieldAlignmentRequirement()) instead of hardcoded 8-byte alignment

@radekdoulik radekdoulik added area-VM-coreclr and removed needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners labels Dec 22, 2025
#elif defined(FEATURE_64BIT_ALIGNMENT)
if (pNestedType.RequiresAlign8())
{
#ifdef TARGET_WASM
Copy link
Member

Choose a reason for hiding this comment

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

It looks like we have Arm32 bug here. It does not make sense for this ifdef to exist. Also, we may want to match !RequiresAlign8 logic that ignores GetFieldAlignmentRequirement for types with GC references. So maybe something like this?

            if (pNestedType.GetMethodTable()->ContainsGCPointers())
            {
                // this field type has GC pointers in it, which need to be pointer-size aligned
                placementInfo.m_alignment = TARGET_POINTER_SIZE;
            }
            else
            {
                placementInfo.m_alignment = pNestedType.GetMethodTable()->GetFieldAlignmentRequirement();
            }
#if defined(FEATURE_64BIT_ALIGNMENT)
            if (pNestedType.RequiresAlign8())
            {
                placementInfo.m_alignment = max(placementInfo.m_alignment, 8);
            }
#endif

Copy link
Member

Choose a reason for hiding this comment

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

Also, this logic is duplicates in

// This does not account for types that are marked IsAlign8Candidate due to 8-byte fields
for the AOT compiler. We should do the same change there as well.

Copy link
Member

Choose a reason for hiding this comment

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

I would agree with @jkotas. This is a bug for types with higher alignment requirements than 8 bytes. This shouldn't be wasm specific.

Also, when updating the layout algorithm, we generally have to bump the major R2R version and min-compatible major R2R version.

@jkotas
Copy link
Member

jkotas commented Dec 22, 2025

cc @jkoritzinsky @davidwrighton - field layout algorithm change

@dotnet-policy-service
Copy link
Contributor

Tagging subscribers to 'arch-wasm': @lewing, @pavelsavara
See info in area-owners.md if you want to be subscribed.

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

Labels

arch-wasm WebAssembly architecture area-TypeSystem-coreclr

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[wasm][coreclr] global::StructPacking.TestEntryPoint fails

3 participants