[Wasm R2R] Fix unsigned add/sub overflow codegen#129106
Open
AndyAyersMS wants to merge 1 commit into
Open
Conversation
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>
Member
Author
|
PTAL @dotnet/wasm-contrib |
Contributor
|
Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch |
Contributor
There was a problem hiding this comment.
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_ADDto use an unsigned compare (result < op1) to detect unsigned addition overflow. - Update overflow-checked
GT_SUBto 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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.