-
Notifications
You must be signed in to change notification settings - Fork 5.3k
[Wasm RyuJit] Add constant emitting support #122162
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
|
Tagging subscribers to 'arch-wasm': @lewing, @pavelsavara |
|
cc @dotnet/jit-contrib |
AndyAyersMS
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should also be extended to handle the missing cases in gtSetEvalOrder
Looking now, I see GT_CNS_LNG. Did I miss another one? |
This NYI_WASM will fail compilation the before the JIT can get into codegen with constants. I think it makes sense to call from here into something to get size estimates for the encoded constants. For the "ex" costs it should be cheap so perhaps just cost at 1? |
I wasn't expecting this PR to actually get us to the point where we can emit constants, I was planning to come in and fix everything up by hand. I'm not sure I can convince Copilot to fix something as complex as that site in gentree. Ex cost of 0 or 1 seems about right, and for sz we can probably call the leb encoder to measure it. |
Fair enough, just expect we might need to rework things some (like the set reg stuff) once we can get the JIT to go that far. |
Co-authored-by: kg <198130+kg@users.noreply.github.com>
|
Grabbing this PR to manually rebase it and redo the codegenwasm.cpp stuff from scratch to hopefully clean it up. |
4cc70c5 to
3368ede
Compare
There was a problem hiding this 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 adds infrastructure for emitting integer and floating-point constants in the WebAssembly RyuJIT backend. It enables constant loading instructions (i32.const, i64.const, f32.const, f64.const) when constant nodes appear in the intermediate representation, laying groundwork for the experimental WASM backend.
Key Changes
- Instruction definitions and formats: Enabled constant instructions (i32_const, i64_const, f32_const, f64_const) with corresponding encoding formats (IF_SLEB128 for signed integers, IF_F32, IF_F64 for floats) in instruction tables and format definitions
- Type system updates: Introduced 64-bit constant storage types (
cnsval_ssize_t/cnsval_size_t) for WASM to support i64 constants in 32-bit mode - Code generation: Added
genCodeForConstant()to handle GT_CNS_INT, GT_CNS_LNG, and GT_CNS_DBL nodes, with proper instruction selection and constant bit packing - Emitter implementation: Implemented LEB128 encoding functions (
emitOutputULEB128,emitOutputSLEB128) and instruction output logic for constant formats with size calculation - Cost modeling: Updated instruction costing in
gtSetEvalOrderto properly account for LEB128-encoded integer constants and floating-point constants
Reviewed changes
Copilot reviewed 10 out of 10 changed files in this pull request and generated 4 comments.
Show a summary per file
| File | Description |
|---|---|
| src/coreclr/jit/target.h | Defines cnsval_ssize_t and cnsval_size_t as 64-bit types for WASM to support full-range constants |
| src/coreclr/jit/instrswasm.h | Uncomments and enables i32_const, i64_const, f32_const, f64_const instruction definitions |
| src/coreclr/jit/emitfmtswasm.h | Adds IF_SLEB128, IF_F32, and IF_F64 instruction format definitions |
| src/coreclr/jit/emitwasm.h | Updates function signatures to use cnsval_ssize_t and declares LEB128 output functions |
| src/coreclr/jit/emitwasm.cpp | Implements LEB128 encoding, instruction size calculation, output generation, and debug display for constant formats |
| src/coreclr/jit/emit.h | Declares global SizeOfULEB128 and SizeOfSLEB128 helper functions |
| src/coreclr/jit/gentree.cpp | Adds WASM-specific costing logic for integer and floating-point constants using LEB128 size calculations |
| src/coreclr/jit/codegenwasm.cpp | Implements genCodeForConstant() and adds GT_CNS_INT/LNG/DBL cases to code generation |
| src/coreclr/jit/codegen.h | Declares genCodeForConstant() function for WASM target |
| src/coreclr/jit/codegencommon.cpp | Adds early return for WASM in genEmitUnwindDebugGCandEH to avoid assertion failures |
SingleAccretion
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM with just a few final comments.
Description
Adds infrastructure for emitting integer and floating-point constants in the WebAssembly RyuJIT backend. This enables the backend to generate constant loading instructions (i32.const, i64.const, f32.const, f64.const) when constant nodes appear in the IR.
Changes Made
instrswasm.hwith corresponding formats (IF_SLEB128, IF_F32, IF_F64) inemitfmtswasm.hEDIT outdated copilot description removed
Customer Impact
None. This is foundational work for the experimental WebAssembly RyuJIT backend. The actual instruction emission logic is NYI and will be implemented in follow-up work.
Regression
No. The WebAssembly backend is not yet functional.
Testing
Built CoreCLR successfully with the changes. The WASM backend files (codegenwasm.cpp, emitwasm.cpp) compile without errors or warnings.
Risk
Minimal. Changes are isolated to the WebAssembly-specific codegen files which are not used in production. The NYI stubs ensure no silent failures occur if these code paths are reached unexpectedly.
Original prompt
💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.