Skip to content

Commit 9479fc6

Browse files
lewingCopilot
andcommitted
[wasm] Retype integer constant struct returns to the native return type
When LowerRetStruct retypes a struct return to a primitive nativeReturnType and the return value is a GT_CNS_INT, the integer branch left the constant as TYP_INT even when the native return type is wider (e.g. TYP_LONG). On register targets this is harmless because integer registers are untyped, but wasm has a typed value stack, so codegen emitted i32.const 0 for an i64-typed return. This surfaced validating the R2R-compiled CoreLib for wasm: Vector64<byte>.get_Zero returns a zero-initialized 8-byte struct, which the wasm ABI lowers to an i64 return. The emitted body ended with i32.const 0 and wasmtime rejected the module with "type mismatch: expected i64, found i32". Retype the integer constant to the native return type under TARGET_WASM so codegen emits an i64.const. Applies to both browser and wasi, which share the JIT. Also submitted standalone as dotnet#129102; will reconcile if that lands first. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent f3448f6 commit 9479fc6

1 file changed

Lines changed: 10 additions & 0 deletions

File tree

src/coreclr/jit/lower.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5859,6 +5859,16 @@ void Lowering::LowerRetStruct(GenTreeUnOp* ret)
58595859
else
58605860
{
58615861
assert(varTypeUsesIntReg(nativeReturnType));
5862+
#ifdef TARGET_WASM
5863+
// Wasm has a typed value stack, so the constant's type must match the
5864+
// type the function returns. Retype an INT constant (e.g. a zero from a
5865+
// promoted/zero-initialized struct field) to the wider native return
5866+
// type so codegen emits an i64.const rather than an i32.const.
5867+
if (genActualType(retVal) != genActualType(nativeReturnType))
5868+
{
5869+
retVal->ChangeType(genActualType(nativeReturnType));
5870+
}
5871+
#endif // TARGET_WASM
58625872
}
58635873
break;
58645874
}

0 commit comments

Comments
 (0)