Skip to content

Comments

[Wasm RyuJit] internal registers, integer binop overflow checks#124731

Open
AndyAyersMS wants to merge 8 commits intodotnet:mainfrom
AndyAyersMS:WasmOverflowChecks
Open

[Wasm RyuJit] internal registers, integer binop overflow checks#124731
AndyAyersMS wants to merge 8 commits intodotnet:mainfrom
AndyAyersMS:WasmOverflowChecks

Conversation

@AndyAyersMS
Copy link
Member

Add support for internal registers.

Use these to implement overflow checks for ADD/SUB/MUL, where the result of the operation is multiply used (though for long MUL, defer to a runtime helper).

Add support for internal registers.

Use these to implement overflow checks for ADD/SUB/MUL, where the
result of the operation is multiply used (though for long MUL,
defer to a runtime helper).
Copilot AI review requested due to automatic review settings February 22, 2026 21:42
@github-actions github-actions bot added the area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI label Feb 22, 2026
@AndyAyersMS
Copy link
Member Author

@SingleAccretion is this what you envisioned for internal registers?

@AndyAyersMS
Copy link
Member Author

AndyAyersMS commented Feb 22, 2026

Also the MUL overflow codegen is not yet finished -- I want to reuse the range checks from the integer casts but either need to fake up a cast Desc or pull the needed cases out into their own methods.

It might be better to do this particular expansion in morph.

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 support for internal registers in the WASM RyuJit backend and implements overflow checking for integer binary operations (ADD, SUB, MUL). Internal registers are temporary registers allocated during code generation that are used to store intermediate values needed for complex operations like overflow detection.

Changes:

  • Added internal register allocation and management infrastructure to the WASM register allocator
  • Implemented overflow checking logic for ADD, SUB, and MUL operations with internal register support
  • Configured morph phase to transform 64-bit overflow multiplication into runtime helper calls
  • Marked operands as multiply-used in lowering when overflow checks are needed

Reviewed changes

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

Show a summary per file
File Description
src/coreclr/jit/regallocwasm.h Added declarations for internal register management and binop reference collection
src/coreclr/jit/regallocwasm.cpp Implemented internal register allocation, consumption, and binop overflow handling
src/coreclr/jit/morph.cpp Added logic to transform WASM 64-bit overflow multiplication to helper calls
src/coreclr/jit/lowerwasm.cpp Marked ADD/SUB overflow operands as multiply-used for correct register handling
src/coreclr/jit/codegenwasm.cpp Implemented overflow check code generation for ADD, SUB, and MUL operations
src/coreclr/jit/codegen.h Added declaration for genCodeForBinaryOverflow method

Copilot AI review requested due to automatic review settings February 22, 2026 22:14
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 6 out of 6 changed files in this pull request and generated 4 comments.

@SingleAccretion
Copy link
Contributor

is this what you envisioned for internal registers?

I think we should return the allocated register from RequestInternalRegister and free it with the existing ReleaseTemporaryRegister (i. e. no need for ConsumeInternalRegisters). It is easier to see that the release order is correct this way - it looks to me that the current code has a problem with this.

@AndyAyersMS
Copy link
Member Author

is this what you envisioned for internal registers?

I think we should return the allocated register from RequestInternalRegister and free it with the existing ReleaseTemporaryRegister (i. e. no need for ConsumeInternalRegisters). It is easier to see that the release order is correct this way - it looks to me that the current code has a problem with this.

Ok, let me revise.

Copilot AI review requested due to automatic review settings February 23, 2026 22:04
@AndyAyersMS AndyAyersMS marked this pull request as ready for review February 23, 2026 22:05
@AndyAyersMS
Copy link
Member Author

FYI @dotnet/jit-contrib
@EgorBo you said you wanted to review something in Wasm?

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

// For long multiply with overflow, call the helper.
if (tree->TypeIs(TYP_LONG))
{
helper = tree->IsUnsigned() ? CORINFO_HELP_ULMUL_OVF : CORINFO_HELP_LMUL_OVF;
Copy link
Member

Choose a reason for hiding this comment

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

I wonder if it'd be simpler and maybe not slower to use helpers for all kinds of overflows? maybe emscripten or whatever tool would be able to optimize it better than us?

Copy link
Member

Choose a reason for hiding this comment

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

V8 does do inlining in its optimized codegen for wasm, but I don't know how aggressive it is or what the heuristics are like. It's also possible we could ship WABT's wasm-opt and run it on our binaries to optimize them, but I don't know if it would inline our helpers

Copy link
Member Author

Choose a reason for hiding this comment

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

We should consider it, but we'd need to add helpers. I'll leave a TODO.

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

Labels

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.

4 participants