Skip to content

Move handling of WASM Function section entries to dependency graph - #132543

Draft
jtschuster wants to merge 2 commits into
dev/jtschuster/wasm-jit-naot-changesfrom
dev/jtschuster/WasmFunctionSectionEntriesInDepGraph
Draft

Move handling of WASM Function section entries to dependency graph#132543
jtschuster wants to merge 2 commits into
dev/jtschuster/wasm-jit-naot-changesfrom
dev/jtschuster/WasmFunctionSectionEntriesInDepGraph

Conversation

@jtschuster

@jtschusterjtschuster commented Aug 19, 2026

Copy link
Copy Markdown
Member

This PR adds ObjectNodes representing the entries in the Function section of WASM modules. They are dependencies of all INodeWithTypeSignature, and entries are also created for each function of INodeWithFunclets. The ordering of entries in the Function section kept aligned with the code nodes by sorting the FunctionEntryNodes by their _methodCodeNode, then by their funclet index. The ObjectData for the nodes is a single reloc to the index of the WasmTypeNode.

Since the webcil functions getWebcilSize, fillWebcilTable, and getWebcilPayload are inserted as stubs in the ObjectWriter, they had to be moved to the dependency graph as well. I made them a new subclass of AssemblyStubNode and added them as roots when compiling a webcil module. They sort themselves ahead of all other method nodes.

fillWebcilTable required knowing the size of the table as a constant, so a new reloc type was created, WASM_FUNCTON_COUNT_SLEB that represents the number of entries in the Code and Function sections of the module, and a new Node was created to be the target of this reloc, WasmFunctionCountNode.

The relocs for the section are shrunk thanks to the work done in #132029.

In the end, this makes the Function section fit into the dependency graph ObjectNode model, but likely is less performant. The existing system for emitting the Function section isn't particularly convoluted or confusing, so I'm not sure this is really a net positive, but the work is done, so I thought I'd create the PR for discussion.

@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.

CopilotAI lite review requested due to automatic review settings August 20, 2026 17:05
@jtschuster
jtschusterforce-pushed the dev/jtschuster/WasmFunctionSectionEntriesInDepGraph branch from 71cecf8 to 1142e0cCompareAugust 20, 2026 17:05
@jtschuster
jtschusterforce-pushed the dev/jtschuster/WasmFunctionSectionEntriesInDepGraph branch from 1142e0c to 0d32d2aCompareAugust 20, 2026 17:06

CopilotAI 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 refactors WebAssembly WebCIL emission so that WASM Function section entries are represented as dependency-graph ObjectNodes, aligning the Function section with the existing object-node model used elsewhere. It also moves the three WebCIL “spec” helper functions into the dependency graph and introduces a new relocation to encode the final emitted function count directly into fillWebcilTable, with accompanying ReadyToRun tests validating section alignment and minimal encodings.

Changes:

  • Model WASM Function-section entries as dependency-graph nodes (WasmFunctionEntryNode) and make all INodeWithTypeSignature depend on the corresponding Function-section entry node(s), including funclets.
  • Move WebCIL default stub methods (getWebcilSize, fillWebcilTable, getWebcilPayload) into the dependency graph and root them for WASM R2R builds; add a WASM_FUNCTION_COUNT_SLEB reloc for fillWebcilTable.
  • Update WebCIL relocation resolution to support shrinking/resolving Function-section relocs and add tests that validate Function/Code section consistency and the fillWebcilTable body.

Reviewed changes

Copilot reviewed 18 out of 18 changed files in this pull request and generated 2 comments.

Show a summary per file
FileDescription
src/coreclr/tools/Common/Compiler/ObjectWriter/WebCilObjectWriter.csAdds Function-section relocation shrinking/resolution path and new reloc handling for function-count.
src/coreclr/tools/Common/Compiler/ObjectWriter/WasmObjectWriter.csStops directly emitting Function-section entries/signature indices; Function section becomes externally counted.
src/coreclr/tools/Common/Compiler/ObjectWriter/WasmInstructions.csAdds WASM_FUNCTION_COUNT_SLEB relocation support and helper for emitting it.
src/coreclr/tools/Common/Compiler/ObjectWriter/Wasm/WasmSection.csRemoves WasmFunctionSection (Function section now uses externally-counted vector section).
src/coreclr/tools/Common/Compiler/DependencyAnalysis/WasmFunctionEntryNode.csNew node type representing a WASM Function-section entry tied to a code node + signature.
src/coreclr/tools/Common/Compiler/DependencyAnalysis/Target_Wasm/WebcilDefaultMethodNode.csAdds dependency-graph nodes for WebCIL spec methods and a symbol node for function-count relocs.
src/coreclr/tools/Common/Compiler/DependencyAnalysis/SortableDependencyNode.csAdds sort order entry for WebCIL default methods to ensure they come first.
src/coreclr/tools/Common/Compiler/DependencyAnalysis/Relocation.csIntroduces WASM_FUNCTION_COUNT_SLEB and wires it into size/read/write helpers.
src/coreclr/tools/Common/Compiler/DependencyAnalysis/ObjectNode.csAdds automatic WASM Function-section entry dependencies for INodeWithTypeSignature nodes (and funclets).
src/coreclr/tools/Common/Compiler/DependencyAnalysis/NodeFactory.Wasm.csAdds node cache for WasmFunctionEntryNode.
src/coreclr/tools/Common/Compiler/DependencyAnalysis/INodeWithTypeSignature.csCentralizes signature→WasmFuncType derivation via an extension method.
src/coreclr/tools/aot/ILCompiler.ReadyToRun/ILCompiler.ReadyToRun.csprojIncludes new dependency-analysis source files in the R2R project.
src/coreclr/tools/aot/ILCompiler.ReadyToRun/Compiler/DependencyAnalysis/ReadyToRunCodegenNodeFactory.csAdds WasmFunctionCount symbol and roots WebCIL default method nodes for WASM builds; initializes new caches.
src/coreclr/tools/aot/ILCompiler.ReadyToRun.Tests/TestCasesRunner/WasmR2RAssert.csAdds assertions verifying Function section matches Code section and fillWebcilTable uses the defined function count.
src/coreclr/tools/aot/ILCompiler.ReadyToRun.Tests/TestCases/Webcil/WasmWebcilModule.csAdds a throwing method to exercise EH/funclet-related emission scenarios.
src/coreclr/tools/aot/ILCompiler.ReadyToRun.Tests/TestCases/R2RTestSuites.csHooks new WASM WebCIL structural assertions into the test suite.
src/coreclr/tools/aot/ILCompiler.Compiler/ILCompiler.Compiler.csprojIncludes WasmFunctionEntryNode.cs in the compiler project.
src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/DependencyAnalysis/NodeFactory.csInitializes the new wasm function-entry node cache and adds WasmTypeNode(WasmFuncType) helper.

}

static WasmFunctionBody GetWebcilSize = new WasmFunctionBody(
new WasmFuncType(new([WasmValueType.I32]), new([])), // (func (destPtr i32) (result))
Comment on lines +874 to +878
foreach (SymbolicRelocation reloc in relocs)
{
Debug.Assert(sourceStream.Position != reloc.Offset,
$"Unexpected data in the WASM Function section between offsets {sourceStream.Position} and {reloc.Offset}.");

CopilotAI review requested due to automatic review settings August 20, 2026 19:18
@jtschuster
jtschusterforce-pushed the dev/jtschuster/WasmFunctionSectionEntriesInDepGraph branch from 0d32d2a to 59d5420CompareAugust 20, 2026 19:18

CopilotAI 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

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

Suppressed comments (1)

src/coreclr/tools/Common/Compiler/ObjectWriter/WebCilObjectWriter.cs:878

  • The assertion that validates the Function section contains only relocation placeholders is inverted: it currently asserts sourceStream.Position != reloc.Offset, which will fail in debug builds when the stream position correctly matches the relocation offset (i.e., when there is no data between relocs).
 foreach (SymbolicRelocation reloc in relocs)
{
Debug.Assert(sourceStream.Position != reloc.Offset,
$"Unexpected data in the WASM Function section between offsets {sourceStream.Position} and {reloc.Offset}.");

- Create WasmFunctionEntryNode to represent an entry in the Function section.
- These emit relocations to the index of the wasm type / signature index for
the method.
- They use the represented method as the key for sorting to ensure the order
of the function section matches the order of the method code nodes in the
code section.
- These are rooted as dependencies of all ObjectNodes that implement
INodeWithTypeSignature. Funclets also get their own and fit into the
sorting to ensure they are placed in order.
- Create WebcilDefaultMethodNode to represent a webcil spec method that must be
exported.
- These are implemented as assembly stubs.
- They are emitted in the Ordered phase to ensure they are emitted first, and
sort properly between themselves.
- fillWebcilTable requires a const for the method count of the module, so a
new reloc WASM_FUNCTION_COUNT_SLEB is added.
- The three methods are rooted in crossgen wasm compilations.
- Tests validate that the WASM_FUNCTION_COUNT_SLEB relocation is
accurate.
- Validated crossgen test modules with wasm-validate.
- ILCompiler also has these changes, but full wasm modules are not yet
emitted, so this is untested.
- Override equals in NodeCache key to avoid boxing.
- Make WasmFunctionEntryNode._methodCodeNode an ObjectNode and avoid emitting when _methodCodeNode is not emitted or is folded.
- Shrink Function section relocations and add test validating they are shrunk.
@jtschuster
jtschusterforce-pushed the dev/jtschuster/WasmFunctionSectionEntriesInDepGraph branch from 988cb84 to 3823269CompareAugust 21, 2026 16:55
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

2 participants

@jtschuster