Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Dec 3, 2025

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

  • Instruction definitions: Enabled i32_const, i64_const, f32_const, f64_const instructions in instrswasm.h with corresponding formats (IF_SLEB128, IF_F32, IF_F64) in emitfmtswasm.h

EDIT 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

This section details on the original issue you should resolve

<issue_title>[Wasm RyuJit] Add partial constant emitting support to codegenwasm.cpp</issue_title>
<issue_description>The WebAssembly RyuJIT backend in codegenwasm.cpp and emitwasm.cpp needs to be updated as follows:

  1. Add a definition for CodeGen::genSetRegToConst which switches on tree->gtOper() and has cases for GT_CNS_INT and GT_CNS_DBL. The case for GT_CNS_INT should use AsIntCon()->IconValue() to store the integer constant into a ssize_t constant. The case for GT_CNS_DBL should use AsDblCon()->DconValue() to store the double constant into a double constant.
  2. Declare a new member function in emitwasm.h called emitEncodeLEB64 with the signature size_t emitter:emitEncodeLEB64(uint8_t *destination, const void *source, bool valueIsSigned).
  3. Define emitEncodeLeb64 in emitwasm.cpp and make its body a NYI_WASM.
  4. Declare a new member function in emitwasm.h called emitIns_R_F with the signature void emitter::emitIns_R_F(instruction ins, emitAttr attr, regNumber reg, double immDbl).
  5. Define emitIns_R_F in emitwasm.cpp and make its body a NYI_WASM.
  6. Update genSetRegToConst's GT_CNS_INT case to use emitEncodeLEB64 to measure the size of the integer constant like so: emitAttr encodedSize = (emitAttr)GetEmitter()->emitEncodeLEB64(nullptr, &constant, true);
  7. Update genSetRegToConst's GT_CNS_DBL case to determine the size of the floating-point constant based on targetType, like so: emitAttr encodedSize = (targetType == TYP_FLOAT) ? EA_4BYTE : EA_8BYTE;
  8. Update genSetRegToConst's GT_CNS_INT case to call emitIns_R_I with appropriate arguments, like so: GetEmitter()->emitIns_R_I((targetType == TYP_INT) ? INS_i32_const : INS_i64_const, encodedSize, targetReg, constant);.
  9. Update genSetRegToConst's GT_CNS_DBL case to call emitIns_R_F with appropriate arguments, like so: GetEmitter()->emitIns_R_F((targetType == TYP_FLOAT) ? INS_f32_const : INS_f64_const, encodedSize, targetReg, constant);.
  10. Update CodeGen::genCodeForTreeNode to add cases for GT_CNS_INT and GT_CNS_DBL that call the new genSetRegToConst member function. The targetReg is treeNode->GetRegNum() and the targetType is treeNode->TypeGet(). After calling genSetRegToConst, call genProduceReg(treeNode).</issue_description>

Comments on the Issue (you are @copilot in this section)


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI assigned Copilot and kg Dec 3, 2025
@github-actions github-actions bot added the needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners label Dec 3, 2025
@kg kg added arch-wasm WebAssembly architecture area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI and removed needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners labels Dec 3, 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.

Copilot AI changed the title [WIP] Add partial constant emitting support to codegenwasm.cpp [Wasm RyuJit] Add partial constant emitting support to codegenwasm.cpp Dec 4, 2025
Copilot AI requested a review from kg December 4, 2025 00:08
Copilot finished work on behalf of kg December 4, 2025 00:08
@kg
Copy link
Member

kg commented Dec 4, 2025

cc @dotnet/jit-contrib

Copy link
Member

@AndyAyersMS AndyAyersMS left a 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

@kg
Copy link
Member

kg commented Dec 4, 2025

This should also be extended to handle the missing cases in gtSetEvalOrder

Looking now, I see GT_CNS_LNG. Did I miss another one?

@AndyAyersMS
Copy link
Member

AndyAyersMS commented Dec 4, 2025

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.

https://github.com/dotnet/runtime/blob/171a9433a1ef0d8e6316fba75a898367f6870ca4/src/coreclr/jit/gentree.cpp#L5176-L5182

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?

@kg
Copy link
Member

kg commented Dec 4, 2025

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.

https://github.com/dotnet/runtime/blob/171a9433a1ef0d8e6316fba75a898367f6870ca4/src/coreclr/jit/gentree.cpp#L5176-L5182

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.

@AndyAyersMS
Copy link
Member

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.

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.

@kg
Copy link
Member

kg commented Dec 4, 2025

Grabbing this PR to manually rebase it and redo the codegenwasm.cpp stuff from scratch to hopefully clean it up.

@kg kg force-pushed the copilot/add-partial-constant-emitting branch from 4cc70c5 to 3368ede Compare December 4, 2025 19:17
@kg kg self-requested a review December 5, 2025 19:07
@kg kg marked this pull request as ready for review December 5, 2025 19:38
Copilot AI review requested due to automatic review settings December 5, 2025 19:38
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 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 gtSetEvalOrder to 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

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 just a few final comments.

@kg kg changed the title [Wasm RyuJit] Add partial constant emitting support to codegenwasm.cpp [Wasm RyuJit] Add constant emitting support Dec 7, 2025
@kg kg merged commit 74cf618 into main Dec 7, 2025
124 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

arch-wasm WebAssembly architecture area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Wasm RyuJit] Add partial constant emitting support to codegenwasm.cpp

7 participants