Skip to content

Wasm: encode struct arg alignment in wasm signature strings - #132244

Open
AndyAyersMS wants to merge 1 commit into
dotnet:mainfrom
AndyAyersMS:struct-arg-align-rebase
Open

Wasm: encode struct arg alignment in wasm signature strings#132244
AndyAyersMS wants to merge 1 commit into
dotnet:mainfrom
AndyAyersMS:struct-arg-align-rebase

Conversation

@AndyAyersMS

Copy link
Copy Markdown
Member

Support structs with 16 byte alignment requirements via A<N> signature string.

Fixes #131639

Note

Portions of this change and this description were generated with GitHub Copilot.

Thunks are keyed globally by signature string, but S<N> encoded only the
struct size, so structs of the same size with different alignments shared
a thunk and disagreed on argument offsets.

Fixes dotnet#131639

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI lite review requested due to automatic review settings August 12, 2026 23:18
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 4 pipeline(s).
12 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

@AndyAyersMS

Copy link
Copy Markdown
Member Author

@davidwrighton PTAL
fyi @dotnet/wasm-contrib

This supplants the just-introduced return caching. Happy to put that back if desired. Likewise unsure if this warranted a GUID bump. It has one currently.

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging @dotnet/jit-contrib for JIT-EE GUID update

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

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 updates the WebAssembly ReadyToRun “signature string” encoding to incorporate a struct argument’s required transition-block alignment, distinguishing same-sized structs with different alignment requirements (e.g., 8 vs 16). This addresses the thunk-sharing/layout mismatch described in #131639.

Changes:

  • Introduces A<N> alongside S<N> to represent byref-passed structs whose transition-block slot requires 16-byte alignment.
  • Keys the struct-type cache by (size, alignment) and updates signature parsing/raising logic to roundtrip the new encoding.
  • Updates native signature key generation, tests, and the ReadyToRun format documentation; bumps the JIT/EE versioning GUID.

Reviewed changes

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

Show a summary per file
File Description
src/tasks/WasmAppBuilder/coreclr/SignatureMapper.cs Extends interop signature-string generation/parsing to accept A<N> and tracks known struct size+alignment.
src/coreclr/vm/wasm/helpers.cpp Encodes S<N> vs A<N> in runtime signature keys based on clamped struct alignment.
src/coreclr/tools/Common/JitInterface/WasmLowering.cs Adds struct-token alignment handling (A<N>), updates raising/lowering logic and struct-token caching.
src/coreclr/tools/Common/Compiler/CompilerTypeSystemContext.Wasm.cs Reworks struct cache to be keyed by (size, alignment) for signature raising.
src/coreclr/tools/aot/ILCompiler.ReadyToRun/Compiler/DependencyAnalysis/ReadyToRun/WasmR2RToInterpreterThunkNode.cs Updates return-struct-token detection to include A<N>.
src/coreclr/tools/aot/ILCompiler.ReadyToRun/Compiler/DependencyAnalysis/ReadyToRun/WasmInterpreterToR2RThunkNode.cs Updates return-struct-token detection to include A<N>.
src/coreclr/tools/aot/ILCompiler.ReadyToRun/Compiler/DependencyAnalysis/ReadyToRun/WasmImportThunk.cs Updates return-struct-token detection to include A<N>.
src/coreclr/tools/aot/ILCompiler.ReadyToRun.Tests/WasmArgumentLayoutTests.cs Adds/updates tests validating A<N> behavior and alignment-sensitive tokenization.
src/coreclr/inc/jiteeversionguid.h Updates the JIT/EE versioning GUID for the signature-encoding change.
docs/design/coreclr/botr/readytorun-format.md Documents the new A<N> token and rationale (alignment-sensitive thunk sharing).

Comment on lines 353 to 356
/// <summary>
/// Returns the number of INTERP_STACK_SLOT_SIZE slots consumed by a token.
/// Struct tokens (S&lt;N&gt;) consume max((size + 7) / 8, 1) slots; all others consume 1.
/// </summary>
Comment on lines +451 to +455
int alignment = GetStructArgAlignment(type);
sigBuilder.Append(alignment == WideStructArgAlignment ? 'A' : 'S');
sigBuilder.Append(type.GetElementSize().AsInt);
((CompilerTypeSystemContext)type.Context).CacheStructBySize(type, alignment);
}
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.

[wasm] R2R: 32-byte struct passed by value is corrupted (fields shifted by one word)

2 participants