Skip to content

[RyuJIT] Make the mis-sized struct return logic only apply to return values that are structs#126772

Merged
AndyAyersMS merged 5 commits intodotnet:mainfrom
kg:wasm-missized-struct-return
Apr 16, 2026
Merged

[RyuJIT] Make the mis-sized struct return logic only apply to return values that are structs#126772
AndyAyersMS merged 5 commits intodotnet:mainfrom
kg:wasm-missized-struct-return

Conversation

@kg
Copy link
Copy Markdown
Member

@kg kg commented Apr 10, 2026

This addresses an assert in wasm codegen with a release SPC, i.e.:

--targetos:browser
--targetarch:wasm
--obj-format=wasm
--parallelism:1
--print-repro-instructions
--codegenopt:JitWasmNyiToR2RUnsupported=1
--codegenopt:JitDump=System.Nullable`1[System.Reflection.Metadata.TypeNameParseOptions]:op_Explicit
-r:"Z:\runtime\artifacts\tests\coreclr\browser.wasm.Release\Tests\Core_Root\*.dll"
"Z:\runtime\artifacts\tests\coreclr\browser.wasm.Release\Tests\Core_Root\System.Private.CoreLib.dll"
--out:"Z:\runtime\artifacts\spc.wasm"
--singlemethodtypename:"System.Nullable`1[[System.Reflection.Metadata.TypeNameParseOptions]]"
--singlemethodname:"op_Explicit"
--singlemethodindex:1

Would produce:

Single method repro args:--singlemethodtypename "System.Nullable`1[[System.Reflection.Metadata.TypeNameParseOptions]]" --singlemethodname "op_Explicit" --singlemethodindex 1
N004 (  7,  7) [000020] n---G+-----                         *  IND       ubyte  <l:$141, c:$181>
N003 (  3,  4) [000019] -----+-----                         \--*  ADD       byref  $200
N001 (  1,  1) [000012] -----+-----                            +--*  LCL_VAR   byref  V01 arg0         u:1 (last use) $c0
N002 (  1,  2) [000018] -----+-----                            \--*  CNS_INT   int    1 $41
Z:\runtime\src\coreclr\jit\gentree.cpp:16961
Assertion failed '!"Incompatible types for gtNewTempStore"' in 'System.Nullable`1[System.Reflection.Metadata.TypeNameParseOptions]:op_Explicit(System.Nullable`1[System.Reflection.Metadata.TypeNameParseOptions]):System.Reflection.Metadata.TypeNameParseOptions' during 'Lowering nodeinfo' (IL size 8; hash 0xbc4e1b69; FullOpts)

The problem is that we are creating a temporary local of struct type using the retTypeClass, but the type of the return value was erased, so we try to turn a GT_IND of type TYP_UBYTE into a temp of TYP_STRUCT which can't possibly work the way ReplaceWithLclVar is constructed.

EDIT:
In this scenario what we need to do is zero-extend the mis-sized return value (a byte or short, most likely) to match the native return type's size. So I wrap the return value in a CAST.

@kg kg added the area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI label Apr 10, 2026
Copilot AI review requested due to automatic review settings April 10, 2026 22:26
@dotnet-policy-service
Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch
See info in area-owners.md if you want to be subscribed.

Copy link
Copy Markdown
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

Fixes a RyuJIT lowering assertion encountered in WASM codegen when a struct return’s value node has an erased (non-struct) type, which caused an invalid spill-to-struct-temp transformation.

Changes:

  • Guard the mis-sized struct return “spill to temp local” workaround so it only runs when the return value node is actually TYP_STRUCT.
  • Avoid invoking ReplaceWithLclVar to store a non-struct-typed GT_IND into a struct-typed temp local.

Comment thread src/coreclr/jit/lower.cpp Outdated
@kg
Copy link
Copy Markdown
Member Author

kg commented Apr 10, 2026

I misread something and this is incorrect. Will keep working on it.

Copilot AI review requested due to automatic review settings April 10, 2026 23:09
Copy link
Copy Markdown
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

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

Comment thread src/coreclr/jit/lower.cpp Outdated
@kg kg force-pushed the wasm-missized-struct-return branch from 5ffc15f to 42e0205 Compare April 14, 2026 00:58
@kg kg marked this pull request as ready for review April 15, 2026 00:41
Copilot AI review requested due to automatic review settings April 15, 2026 00:41
@kg
Copy link
Copy Markdown
Member Author

kg commented Apr 15, 2026

@dotnet/jit-contrib PTAL

Copy link
Copy Markdown
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

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

@AndyAyersMS
Copy link
Copy Markdown
Member

@kg I'm going to merge this one for you

@AndyAyersMS AndyAyersMS merged commit f764578 into dotnet:main Apr 16, 2026
141 of 143 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants