Skip to content

Conversation

@adamperlin
Copy link
Contributor

This is the first prototype of a Wasm object writer for crossgen that only writes out method bodies. It builds the minimum module structure which is needed to declare and export the module bodies so that the resulting module can be loaded and method bodies can be called by an external loader. I have tested the output with a simple JavaScript loader that instantiates the module and calls one of the exports.

What IS handled

Emitting simple method bodies from an assembly that have no relocations or external dependencies of any kind.

What is NOT handled

Relocations and any metadata attached to compiled methods.

R2R format envelope (the plan is to embed this in the data section in a future PR)

R2R metadata is currently unused.

Signature generation to meet ABI requirements

Right now, there is a single i32 -> i32 signature which is used as a placeholder for any method. Later work will need to build support for generating and storing wasm-level type signatures for JIT compiled methods.

JIT Integration

Currently, the JIT is not called (I believe it will hit asserts on the Wasm target until #121973 is in). Instead, a simple (i32.const 0) (return) stub is emitted for any JIT calls.

…x wasm arch+os target validation, add obj-format option for Wasm
It currently only handles writing method body nodes to the code section,
in addition to building the type, function, and exports section.
It will export all written methods so that they can be called when the
module is loaded.
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

This PR introduces the first prototype of a WebAssembly object writer for crossgen2, enabling the compilation toolchain to output Wasm modules. The implementation focuses on emitting method bodies with minimal module structure required for loading and calling exported functions, while deferring relocations, R2R metadata, and proper ABI signature handling to future work.

Key Changes

  • Added support for Wasm as a valid target architecture and container format in crossgen2
  • Implemented core Wasm module writing infrastructure including section builders for Type, Function, Export, and Code sections
  • Created a temporary stub mechanism to bypass JIT compilation for Wasm32 architecture until proper codegen support is available

Reviewed changes

Copilot reviewed 10 out of 10 changed files in this pull request and generated 15 comments.

Show a summary per file
File Description
src/coreclr/tools/aot/crossgen2/Program.cs Added Wasm to allowed non-composite container formats
src/coreclr/tools/aot/crossgen2/Crossgen2RootCommand.cs Added "wasm" architecture and "wasi" OS to valid options, plus "wasm" output format
src/coreclr/tools/aot/ILCompiler.ReadyToRun/ILCompiler.ReadyToRun.csproj Included new Wasm object writer files (contains developer-specific path that should be removed)
src/coreclr/tools/aot/ILCompiler.ReadyToRun/CodeGen/ReadyToRunObjectWriter.cs Added factory method to create WasmObjectWriter with ABI context
src/coreclr/tools/aot/ILCompiler.ReadyToRun/CodeGen/ReadyToRunContainerFormat.cs Added Wasm enum value to container format options
src/coreclr/tools/aot/ILCompiler.ReadyToRun/ClassDiagram1.cd Added class diagram file (appears unrelated to Wasm implementation)
src/coreclr/tools/Common/JitInterface/CorInfoImpl.cs Implemented CompileWasmStub to emit placeholder bytecode for Wasm methods
src/coreclr/tools/Common/Compiler/ObjectWriter/WasmObjectWriter.cs Core Wasm module writer implementation handling section generation and export
src/coreclr/tools/Common/Compiler/ObjectWriter/WasmNative.cs Wasm type system definitions and encoding utilities
src/coreclr/tools/Common/Compiler/ObjectWriter/SectionWriter.cs Added WriteUtf8StringNoNull helper method for Wasm string encoding
src/coreclr/tools/Common/Compiler/ObjectWriter/ObjectWriter.cs Changed visibility modifiers to enable WasmObjectWriter inheritance

@am11 am11 added the arch-wasm WebAssembly architecture label Dec 2, 2025
@dotnet-policy-service
Copy link
Contributor

Tagging subscribers to 'arch-wasm': @lewing, @pavelsavara
See info in area-owners.md if you want to be subscribed.

@adamperlin
Copy link
Contributor Author

@SingleAccretion thank you for your comments! I think I've addressed them, please feel free to take another look!

Copy link
Member

@jkoritzinsky jkoritzinsky left a comment

Choose a reason for hiding this comment

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

LGTM for a v0

Copy link
Contributor

@SingleAccretion SingleAccretion left a comment

Choose a reason for hiding this comment

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

LGTM with some comments.

@adamperlin adamperlin merged commit 2316313 into dotnet:main Dec 8, 2025
94 of 96 checks passed
adamperlin added a commit that referenced this pull request Dec 9, 2025
This PR reverts the behavior from #122111 which stubs out a method
instead of calling the JIT for the crossgen Wasm target. Now, the JIT
will be invoked as normal so that the output of method codegen can be
placed in a simple object file.

Output code currently won't run (I tried this) because we aren't yet
emitting a correct local count at the beginning of compiled methods.
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.

7 participants