Skip to content

[Wasm R2R] Fix unsigned add/sub overflow codegen#129106

Open
AndyAyersMS wants to merge 1 commit into
dotnet:mainfrom
AndyAyersMS:wasm-unsigned-overflow
Open

[Wasm R2R] Fix unsigned add/sub overflow codegen#129106
AndyAyersMS wants to merge 1 commit into
dotnet:mainfrom
AndyAyersMS:wasm-unsigned-overflow

Conversation

@AndyAyersMS
Copy link
Copy Markdown
Member

genCodeForBinaryOverflow in codegenwasm.cpp was emitting the signed XOR-based overflow algorithm for GT_ADD and GT_SUB regardless of treeNode->IsUnsigned(). Fix this.

Contributes to #128234.

genCodeForBinaryOverflow in codegenwasm.cpp was emitting the signed
XOR-based overflow algorithm for GT_ADD and GT_SUB regardless of
treeNode->IsUnsigned(). For add.ovf.un / sub.ovf.un the signed check
only fires when the two operand sign bits match, so unsigned overflows
where the operands have different high bits were silently wrapped
without throwing OverflowException.

Branch on IsUnsigned() in both cases. For unsigned add, overflow iff
result < op1 (unsigned compare). For unsigned sub, overflow iff
op1 < op2 (unsigned compare). The existing signed algorithm is
preserved unchanged in the else branch. GT_MUL already had the same
IsUnsigned()-based split, so the pattern is consistent with the rest
of the function.

Contributes to dotnet#128234.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings June 8, 2026 00:32
@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 Jun 8, 2026
@AndyAyersMS
Copy link
Copy Markdown
Member Author

PTAL @dotnet/wasm-contrib
fyi @dotnet/jit-contrib

@dotnet-policy-service
Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch
See info in area-owners.md if you want to be subscribed.

Copy link
Copy Markdown
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 corrects WASM-specific JIT codegen for overflow-checked GT_ADD/GT_SUB when the IR node is marked unsigned, by emitting unsigned overflow detection rather than the existing signed (XOR/sign-bit) algorithm. This aligns the generated Wasm for add.ovf.un / sub.ovf.un semantics and should address incorrect codegen in R2R scenarios.

Changes:

  • Update overflow-checked GT_ADD to use an unsigned compare (result < op1) to detect unsigned addition overflow.
  • Update overflow-checked GT_SUB to use an unsigned compare (op1 < op2) to detect unsigned subtraction overflow.
  • Keep the existing signed overflow logic (XOR-based sign checks) for signed add/sub cases.

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.

2 participants