Skip to content

Commit dc3757e

Browse files
lewingCopilot
andauthored
[wasm] Model class-init helper as value-returning in the JIT (#130813)
## Summary On wasm, the class-init JIT helpers (`CORINFO_HELP_INITCLASS` / `CORINFO_HELP_INITINSTCLASS`) are dispatched through portable entry points to the managed `InitClass` / `InitInstantiatedClass` helpers (`InitHelpers.cs`). Those helpers were changed to return `void*` (a discarded dummy value) so the compiled `call_indirect` signature matches the interpreter's value-returning portable-entry-point convention — but the **JIT caller side was never updated** and still typed these helpers as `TYP_VOID`. As a result, R2R-compiled wasm code emits a `(...)->()` `call_indirect` for the first class-init of a type, while the runtime dispatches the cold class-init through the i32-returning thunk. The wasm engine traps with `function signature mismatch`. This change models the class-init helpers as value-returning (`TYP_I_IMPL`) on wasm at the five sites that create them: - `fgGetStaticsCCtorHelper` (the shared-cctor path used by non-generic methods) - `impInitClass` (runtime-lookup path) - the three `fgInitThisClass` generic-lookup cases (`THISOBJ`, `CLASSPARAM`, `METHODPARAM`) The helper result is pushed and then discarded via the existing unused-value `drop` in wasm codegen. The change is guarded by `#ifdef TARGET_WASM`; other targets are unaffected. ## ⚠️ Dependency This is the **JIT caller-side half** of the class-init-helper return-arity change. It depends on the corelib half — `InitClass` / `InitInstantiatedClass` returning `void*` in `InitHelpers.cs` — which is **not yet on `main`** (it is part of the in-progress wasm R2R / SPC bring-up work, e.g. `AndyAyersMS:next-blocker-spc`). This PR is **not correct in isolation** without that corelib change: the JIT would model the helper as i32-returning while the helper still returned void. The two halves must land together (or this should be folded into the SPC bring-up set). Opening as **draft for human review** of the JIT modeling change. There is currently **no wasm-R2R CI leg**, so validation below is from a local sweep. ## Validation (local, browser wasm R2R) Built the fixed cross-JIT, re-crossgen'd, and ran under `corerun.js` with `DOTNET_ReadyToRun=1`: | Test | Before | After | |---|---|---| | `conv_ovf`, `127931`, `indexMinusOne` (interpreted corelib) | trap (exit 1) | **exit 100** | | `conv_ovf` (R2R corelib) | trap (exit 1) | **exit 100** | | `b491215`, `GitHub_13561` (type-load) | pass | **still pass** | This resolves the residual overflow bucket of #129622 and removes the class-init signature-mismatch trap in #130634 (that repro now progresses into managed execution and surfaces a separate, unrelated `NullReferenceException`). Contributes to #129622 / #130634. > [!NOTE] > This PR was created with assistance from GitHub Copilot (AI). --------- Co-authored-by: Copilot App <[email protected]>
1 parent c66d7e1 commit dc3757e

4 files changed

Lines changed: 14 additions & 5 deletions

File tree

‎src/coreclr/jit/compiler.h‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3358,6 +3358,14 @@ class Compiler
33583358
GenTreeCall* gtNewHelperCallNode(
33593359
unsigned helper, var_types type, GenTree* arg1 = nullptr, GenTree* arg2 = nullptr, GenTree* arg3 = nullptr, GenTree* arg4 = nullptr);
33603360

3361+
#ifdef TARGET_WASM
3362+
// On wasm these helpers return void* (InitHelpers.InitClass/InitInstantiatedClass). Model them as
3363+
// value-returning so the call_indirect signature matches the compiled managed helper; the value is unused.
3364+
static constexpr var_types HelperInitClassRetType = TYP_I_IMPL;
3365+
#else
3366+
static constexpr var_types HelperInitClassRetType = TYP_VOID;
3367+
#endif // TARGET_WASM
3368+
33613369
GenTreeCall* gtNewVirtualFunctionLookupHelperCallNode(
33623370
unsigned helper, var_types type, GenTree* thisPtr, GenTree* methHnd, GenTree* clsHnd = nullptr);
33633371

‎src/coreclr/jit/flowgraph.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -766,7 +766,7 @@ GenTreeCall* Compiler::fgGetStaticsCCtorHelper(CORINFO_CLASS_HANDLE cls, CorInfo
766766
break;
767767

768768
case CORINFO_HELP_INITCLASS:
769-
type = TYP_VOID;
769+
type = HelperInitClassRetType;
770770
break;
771771

772772
default:

‎src/coreclr/jit/importer.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3909,7 +3909,7 @@ GenTree* Compiler::impInitClass(CORINFO_RESOLVED_TOKEN* pResolvedToken)
39093909

39103910
if (runtimeLookup)
39113911
{
3912-
node = gtNewHelperCallNode(CORINFO_HELP_INITCLASS, TYP_VOID, node);
3912+
node = gtNewHelperCallNode(CORINFO_HELP_INITCLASS, HelperInitClassRetType, node);
39133913
}
39143914
else
39153915
{

‎src/coreclr/jit/morph.cpp‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14674,21 +14674,22 @@ GenTree* Compiler::fgInitThisClass()
1467414674
vtTree = gtNewMethodTableLookup(vtTree);
1467514675
GenTree* methodHnd = gtNewIconEmbMethHndNode(info.compMethodHnd);
1467614676

14677-
return gtNewHelperCallNode(CORINFO_HELP_INITINSTCLASS, TYP_VOID, vtTree, methodHnd);
14677+
return gtNewHelperCallNode(CORINFO_HELP_INITINSTCLASS, HelperInitClassRetType, vtTree, methodHnd);
1467814678
}
1467914679

1468014680
case CORINFO_LOOKUP_CLASSPARAM:
1468114681
{
1468214682
GenTree* vtTree = gtNewLclvNode(info.compTypeCtxtArg, TYP_I_IMPL);
1468314683
vtTree->gtFlags |= GTF_VAR_CONTEXT;
14684-
return gtNewHelperCallNode(CORINFO_HELP_INITCLASS, TYP_VOID, vtTree);
14684+
return gtNewHelperCallNode(CORINFO_HELP_INITCLASS, HelperInitClassRetType, vtTree);
1468514685
}
1468614686

1468714687
case CORINFO_LOOKUP_METHODPARAM:
1468814688
{
1468914689
GenTree* methHndTree = gtNewLclvNode(info.compTypeCtxtArg, TYP_I_IMPL);
1469014690
methHndTree->gtFlags |= GTF_VAR_CONTEXT;
14691-
return gtNewHelperCallNode(CORINFO_HELP_INITINSTCLASS, TYP_VOID, gtNewIconNode(0), methHndTree);
14691+
return gtNewHelperCallNode(CORINFO_HELP_INITINSTCLASS, HelperInitClassRetType, gtNewIconNode(0),
14692+
methHndTree);
1469214693
}
1469314694

1469414695
default:

0 commit comments

Comments
 (0)