Skip to content

Implement TentativeMethodNode for WASM - #132814

Draft
jtschuster with Copilot wants to merge 7 commits into
mainfrom
copilot/implement-tentativemethodnode-wasm
Draft

Implement TentativeMethodNode for WASM#132814
jtschuster with Copilot wants to merge 7 commits into
mainfrom
copilot/implement-tentativemethodnode-wasm

Conversation

Copilot AI commented Aug 27, 2026

Copy link
Copy Markdown
Contributor

NativeAOT’s WASM backend could not emit tentative method stubs.

  • Stub emission
    • Forward managed ABI parameters to the selected target.
    • Emit call plus unreachable for throw helpers.
    • Emit return_call when forwarding to the real body.
  • Compiler integration
    • Register tentative method signatures with the WASM object writer.
    • Add function-index relocations and NativeAOT emitter support.

@azure-pipelines

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

Co-authored-by: jtschuster <36744439+jtschuster@users.noreply.github.com>
@jkotas jkotas added trimming-for-aot `EnableAggressiveTrimming=true` used for running tests with AOT area-NativeAOT-coreclr and removed trimming-for-aot `EnableAggressiveTrimming=true` used for running tests with AOT area-Infrastructure labels Aug 27, 2026
@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @agocke, @dotnet/ilc-contrib
See info in area-owners.md if you want to be subscribed.

Copilot AI and others added 2 commits August 27, 2026 02:39
Co-authored-by: jtschuster <36744439+jtschuster@users.noreply.github.com>
Co-authored-by: jtschuster <36744439+jtschuster@users.noreply.github.com>
Copilot AI changed the title [WIP] Implement TentativeMethodNode for WASM Implement TentativeMethodNode for WASM Aug 27, 2026
Copilot AI requested a review from jtschuster August 27, 2026 02:48
@jtschuster
jtschuster marked this pull request as ready for review August 27, 2026 16:52
Copilot AI lite review requested due to automatic review settings August 27, 2026 16:52
@azure-pipelines

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

@jtschuster
jtschuster marked this pull request as draft August 27, 2026 16:54

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 implements WebAssembly (Wasm) codegen support for TentativeMethodNode in the NativeAOT compiler pipeline, enabling emission of tentative method stubs and ensuring the Wasm object writer can register the correct function signatures and relocations.

Changes:

  • Add Wasm instruction support for unreachable, call, and return_call, plus helpers to emit them with function-index relocations.
  • Enable Wasm emission in WasmEmitter outside #if READYTORUN, and implement Wasm stub emission for TentativeMethodNode.
  • Update TentativeMethodNode to provide a type signature to the Wasm object writer, and add a unit test that validates emitted bytes + relocation behavior.

Reviewed changes

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

Show a summary per file
File Description
src/coreclr/tools/Common/Compiler/ObjectWriter/WasmInstructions.cs Adds Wasm expr kinds and helpers to emit unreachable, call, and return_call with function-index relocations.
src/coreclr/tools/Common/Compiler/DependencyAnalysis/Target_Wasm/WasmEmitter.cs Removes #if READYTORUN gating so Wasm function bodies/relocs can be emitted in this compilation path.
src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/DependencyAnalysis/TentativeMethodNode.cs Makes TentativeMethodNode provide a type signature (via IMethodCodeNodeWithTypeSignature) for Wasm signature registration.
src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/DependencyAnalysis/Target_Wasm/WasmTentativeMethodNode.cs Implements Wasm stub emission: forward args to real body (return_call) or call throw-helper then unreachable.
src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/DependencyAnalysis/NodeFactory.cs Implements WasmTypeNode(MethodDesc) using WasmLowering.GetSignature(...).FuncType so Wasm signatures can be registered.
src/coreclr/tools/aot/ILCompiler.Compiler.Tests/DependencyGraphTests.cs Adds a focused unit test asserting Wasm encoding + relocation for tentative stubs.

Comment on lines 12 to 14
{
#if READYTORUN
public WasmFunctionBody FunctionBody = null;
#endif

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Updated FunctionBody to initialize with null! in ae1d521.

Copilot AI and others added 2 commits August 27, 2026 17:46
Co-authored-by: jtschuster <36744439+jtschuster@users.noreply.github.com>
Co-authored-by: jtschuster <36744439+jtschuster@users.noreply.github.com>
Comment on lines +21 to +31
if (ReferenceEquals(target, RealBody))
{
// No throw helper is available, so forward all parameters and tail-call the real body.
int parameterCount = signature.Params.Types.Length;
expressions = new WasmExpr[parameterCount + 1];
for (int i = 0; i < parameterCount; i++)
{
expressions[i] = Local.Get(i);
}
expressions[parameterCount] = ControlFlow.ReturnCall(target);
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Whatever is generated in this codepath is never actually written into the output object file (we're generating a reloc to RealBody; doing that marks it and makes object emission of this node skipped):

public override bool ShouldSkipEmittingObjectNode(NodeFactory factory)
{
// If the real body was marked, don't emit this assembly stub.
return _methodNode.Marked;
}

I think it would be completely fine to always do the else branch, the WASM doesn't need to be valid here.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Simplified WASM emission to always generate the minimal call relocation path in ae1d521.

[Theory]
[InlineData(false)]
[InlineData(true)]
public void TentativeMethodNodeEmitsWasmCall(bool useRealBody)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This test is quite some slop forcing invariants that are not useful, I'd delete it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Deleted this test and its supporting test-only node in ae1d521.

else
{
// The real body was removed, so call the throw helper with the shadow stack pointer.
Debug.Assert(!Method.IsUnmanagedCallersOnly);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

If we really want to assert this, it should be done in the constructor so we get a useful stack pointing to the blame place. But I don't find it likely we could accidentally use this with UnmanagedCallersOnly methods, I don't see a world where they could be tentative (i.e. reloc to them is reachable, but they could be effectively unexecutable).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Removed the unmanaged-caller assertion in ae1d521.

Co-authored-by: jtschuster <36744439+jtschuster@users.noreply.github.com>
End = 0x0B,
Call = 0x10,
CallIndirect = 0x11,
ReturnCall = 0x12,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This is dead code now. LGTM otherwise.

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

Projects

Status: No status
Status: No status

Development

Successfully merging this pull request may close these issues.

Implement TentativeMethodNode for WASM

5 participants