Skip to content

[RyuJit Wasm] Fix Signature Generation Bug with Un-called methods#125090

Open
adamperlin wants to merge 16 commits intodotnet:mainfrom
adamperlin:adamperlin/wasm-crossgen-method-typenode-deps
Open

[RyuJit Wasm] Fix Signature Generation Bug with Un-called methods#125090
adamperlin wants to merge 16 commits intodotnet:mainfrom
adamperlin:adamperlin/wasm-crossgen-method-typenode-deps

Conversation

@adamperlin
Copy link
Contributor

@adamperlin adamperlin commented Mar 3, 2026

#124685 changed our signature recording logic in favor of using WasmTypeNodes in the dependency graph. However, we do not create these nodes currently unless they are the target of a relocation, but every compiled method will need one. This PR adds WasmTypeNode as a static dependency of MethodWithGCInfo to make sure we always create a type signature node for every method we compile.

…ithGCInfo on Wasm, clean up signature index assignment logic
Copy link
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

Ensures WebAssembly ReadyToRun compilation always materializes a WasmTypeNode (type signature) for every compiled managed method, fixing missing signature/type-section entries for methods that were compiled but never referenced by relocations.

Changes:

  • Add a NodeFactory.WasmTypeNode(MethodDesc) overload that derives the signature via WasmLowering.GetSignature.
  • Add a static dependency from MethodWithGCInfo to the method’s WasmTypeNode when targeting Wasm32.
  • Simplify signature index assignment in WasmObjectWriter.RecordMethodSignature by using _uniqueSignatures.Count.

Reviewed changes

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

File Description
src/coreclr/tools/aot/ILCompiler.ReadyToRun/Compiler/DependencyAnalysis/ReadyToRunCodegenNodeFactory.cs Adds a factory helper to create/cache WasmTypeNode from a MethodDesc signature.
src/coreclr/tools/aot/ILCompiler.ReadyToRun/Compiler/DependencyAnalysis/ReadyToRun/MethodWithGCInfo.cs Ensures each compiled method depends on (and thus emits/records) its Wasm signature node.
src/coreclr/tools/Common/Compiler/ObjectWriter/WasmObjectWriter.cs Uses dictionary count as the next signature index (removes redundant counter).

…yAnalysis/ReadyToRun/MethodWithGCInfo.cs

Co-authored-by: Michal Strehovský <MichalStrehovsky@users.noreply.github.com>
Copilot AI review requested due to automatic review settings March 3, 2026 01:43
Copy link
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 3 out of 3 changed files in this pull request and generated no new comments.

@SingleAccretion
Copy link
Contributor

WasmTypeNode as a static dependency of MethodWithGCInfo to make sure we always create a type signature node for every method we compile.

I don't think this is an unreasonable approach, but MethodWithGCInfo is not going to be the only node that needs a signature in the long run. All nodes that may inspire imports need a signature. Hand-generated WASM helpers will need a signature. It is going to be quite pervasive, especially for NAOT. Still better than handling it implicitly in the object writer?

@adamperlin
Copy link
Contributor Author

adamperlin commented Mar 3, 2026

WasmTypeNode as a static dependency of MethodWithGCInfo to make sure we always create a type signature node for every method we compile.

I don't think this is an unreasonable approach, but MethodWithGCInfo is not going to be the only node that needs a signature in the long run. All nodes that may inspire imports need a signature. Hand-generated WASM helpers will need a signature. It is going to be quite pervasive, especially for NAOT. Still better than handling it implicitly in the object writer?

This is a good point, and I wasn't considering all the possible NAOT nodes nodes that may need to have this new dependency. The reason I decided on this approach was for clarity -- it seemed clearer to have all type signatures originate in the dependency graph and to simply process them in the object writer, as opposed to having some originate in the graph (for reloc targets) and others in the object writer.

@kg
Copy link
Member

kg commented Mar 3, 2026

My work is blocked on this, can we land it as-is or is there something wrong with it?

…yAnalysis/ReadyToRun/MethodWithGCInfo.cs

Co-authored-by: SingleAccretion <62474226+SingleAccretion@users.noreply.github.com>
Copilot AI review requested due to automatic review settings March 3, 2026 23:58
@adamperlin
Copy link
Contributor Author

adamperlin commented Mar 4, 2026

My work is blocked on this, can we land it as-is or is there something wrong with it?

I think we can merge as-is so you're unblocked. I do have a follow up I'm working on that I'll want to merge shortly (basically, an interface that code nodes can implement so that we enforce that they do indeed produce a type signature in the graph).

Copy link
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 3 out of 3 changed files in this pull request and generated no new comments.

@kg
Copy link
Member

kg commented Mar 4, 2026

I was able to just apply your changes locally so we can take our time merging this if you want.

Copy link
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 15 out of 15 changed files in this pull request and generated 3 comments.

…nalysis/FatFunctionPointerNode.cs

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings March 5, 2026 01:21
…yAnalysis/ReadyToRun/MethodWithGCInfo.cs

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Copy link
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 14 out of 14 changed files in this pull request and generated no new comments.

Change IWasmCodeNode to look more like IMethodNode for ease of
integration. Implement IWasmMethodCodeNode for code carrying nodes, and
a dispatch check in ObjectNode.GetStaticDependencies to ensure we add a
new edge and node to represent the type signature of a method code node
in the dependency graph.
…b.com:adamperlin/runtime into adamperlin/wasm-crossgen-method-typenode-deps
Copilot AI review requested due to automatic review settings March 6, 2026 18:06
Copy link
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 14 out of 14 changed files in this pull request and generated 1 comment.

@adamperlin
Copy link
Contributor Author

@MichalStrehovsky I've implemented your suggestions for the interface / ObjectNode implementation. Please feel free to take another look when you have a chance. I'm leaving out AssemblyStubNode as an impl of the interface for now, as I think what we need to do there will become clearer shortly as we implement import thunks for Wasm!

}

private protected virtual void RecordMethodDeclaration(ISymbolDefinitionNode node, MethodDesc desc)
private protected virtual void RecordMethodDeclaration(IWasmCodeNode node, MethodDesc desc)
Copy link
Member

Choose a reason for hiding this comment

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

Hmm, I think we'd want to keep this as something general purpose. The general object writing API surface shouldn't have WASM things in it if we can help it.

We have INodeWithDebugInfo, INodeWithCodeInfo... maybe this interface is not Wasm specific and can be just INodeWithSignatureInfo (i.e. rename IWasmCodeNode to INodeWithSignatureInfo so that we don't add WASM specific virtuals)?

Copy link
Contributor Author

@adamperlin adamperlin Mar 9, 2026

Choose a reason for hiding this comment

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

I can try renaming this to INodeWithSignatureInfo. If we want to keep Wasm-specific things out of the object writing API surface, we may also want to rename WasmTypeNode to PredeclaredSignatureNode or something similar? or we could have: WasmTypeNode : IPredeclaredSignatureNode and check for IPredeclaredSignatureNode in the object writer?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@MichalStrehovsky this should be addressed now!

Copy link
Member

Choose a reason for hiding this comment

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

we may also want to rename WasmTypeNode to PredeclaredSignatureNode or something similar? or we could have: WasmTypeNode : IPredeclaredSignatureNode and check for IPredeclaredSignatureNode in the object writer?

I think that part can eventually be done somewhere else. I think we'll be able to stop special casing this node.

Copilot AI review requested due to automatic review settings March 9, 2026 18:41
Copy link
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 19 out of 19 changed files in this pull request and generated 3 comments.

namespace ILCompiler.DependencyAnalysis.ReadyToRun
{
public class MethodWithGCInfo : ObjectNode, IMethodBodyNode, ISymbolDefinitionNode
public class MethodWithGCInfo : ObjectNode, IMethodBodyNode, IMethodCodeNodeWithTypeSignature
Copy link

Copilot AI Mar 9, 2026

Choose a reason for hiding this comment

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

There’s trailing whitespace at the end of the class declaration line. This tends to cause noisy diffs and can violate repo formatting rules; please remove the extra space.

Copilot uses AI. Check for mistakes.
using ILCompiler.DependencyAnalysis;
using ILCompiler.DependencyAnalysis.ReadyToRun;
using ILCompiler.DependencyAnalysis.Wasm;

Copy link
Member

Choose a reason for hiding this comment

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

I don't know if this diff makes sense. The System ones should go all the way up and the last ILCompiler one can stay in this group. I would not touch this. Not sure if it's worth respinning CI just for that though.

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

Labels

arch-wasm WebAssembly architecture area-crossgen2-coreclr

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants