[Wasm RyuJit] Add binary operation implementations to codegenwasm.cpp#122119
[Wasm RyuJit] Add binary operation implementations to codegenwasm.cpp#122119
Conversation
|
Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch |
Added implementations for binary operations in the Wasm RyuJit backend: - Added GT_SUB, GT_MUL, GT_DIV, GT_MOD, GT_UDIV, GT_UMOD to genCodeForTreeNode switch - Added GT_OR, GT_XOR, GT_AND, GT_LSH, GT_RSH, GT_RSZ, GT_ROL, GT_ROR to genCodeForTreeNode switch - Added all corresponding PackOperAndType cases in genCodeForBinary for: - Integer operations (i32/i64): sub, mul, div_s, div_u, rem_s, rem_u, and, or, xor, shl, shr_s, shr_u, rotl, rotr - Floating point operations (f32/f64): sub, mul, div These map WASM instructions from instrswasm.h to GT_xxx gentree enums from gtlist.h. Co-authored-by: kg <198130+kg@users.noreply.github.com>
|
cc @dotnet/jit-contrib |
There was a problem hiding this comment.
Pull request overview
This PR implements basic binary operations for the WebAssembly RyuJit backend, building upon the instruction definitions added in PR #122081. The implementation adds code generation support for 15 GenTree operations by routing them through the genCodeForBinary function.
Key Changes
- Added 14 new GT operation cases to the switch statement in
genCodeForTreeNode(GT_SUB through GT_ROR) - Implemented 38 PackOperAndType mappings in
genCodeForBinarycovering integer, floating-point, bitwise, shift, and rotation operations for TYP_INT, TYP_LONG, TYP_FLOAT, and TYP_DOUBLE types
|
@copilot please update the cases in genCodeForBinary as follows: case PackOperAndType(GT_ADD, TYP_INT):
if (treeNode->gtOverflow()) NYI_WASM("Overflow checks");
ins = INS_i32_add;
break; |
(It needs to be only add, sub, mul - see e. g. |
You're right, I was thinking of the other checks for div and rem (divide by zero) - though i guess maybe we'd get a trap? |
|
Yeah, that's also needed. But there is no way to "assert" it, it just need to be added like a Edit: I think in the end this means that what we should leave in this method is basically only add/sub/mul/and/or/xor. The rest should get their own methods. |
I think the next step (probably for the sake of not confusing copilot, a separate PR?) is to split the relevant ops out into new methods. But if you'd like I can try and make it happen in this one, I can always clean it up manually. |
Whatever works better for you :). |
Added gtOverflow() checks with NYI_WASM("Overflow checks") for:
- GT_ADD (TYP_INT, TYP_LONG)
- GT_SUB (TYP_INT, TYP_LONG)
- GT_MUL (TYP_INT, TYP_LONG)
- GT_DIV (TYP_INT, TYP_LONG)
- GT_MOD (TYP_INT, TYP_LONG)
Floating-point operations do not check overflow as requested.
Co-authored-by: kg <198130+kg@users.noreply.github.com>
Added overflow checks as requested in commit 7b223fc. The checks are now in place for GT_ADD, GT_SUB, GT_MUL, GT_DIV, and GT_MOD for both TYP_INT and TYP_LONG types. |
|
@copilot Please remove the overflow checks for GT_MOD and GT_DIV in genCodeForBinary in codegenwasm.cpp, I had you add them in error. The others can stay. |
Removed gtOverflow() checks from GT_DIV and GT_MOD operations as requested. Only GT_ADD, GT_SUB, and GT_MUL retain overflow checks per OperMayOverflow. Co-authored-by: kg <198130+kg@users.noreply.github.com>
Removed overflow checks from GT_DIV and GT_MOD in commit fa3c96e. Only GT_ADD, GT_SUB, and GT_MUL now have overflow checks. |
|
@copilot Please move the cases for the shifts - #ifdef TARGET_WASM
void genCodeForShiftOrRotate(GenTreeOp* treeNode);
#endifAnd then please update |
- Created new genCodeForShiftOrRotate member function - Moved GT_LSH, GT_RSH, GT_RSZ, GT_ROL, GT_ROR from genCodeForBinary - Added declaration in codegen.h with #ifdef TARGET_WASM - Updated genCodeForTreeNode to call genCodeForShiftOrRotate for shifts Co-authored-by: kg <198130+kg@users.noreply.github.com>
Completed in commit 8c63dfa. Created genCodeForShiftOrRotate with all shift/rotate operations (GT_LSH, GT_RSH, GT_RSZ, GT_ROL, GT_ROR), added the ifdef'd declaration to codegen.h, and updated genCodeForTreeNode to call the new function. |
|
@copilot please update
The comment should say something like 'TODO: zero-extend the 2nd operand for shifts and rotates as needed when the 1st and 2nd operand are different types'. |
Added TODO-WASM comment documenting the need to zero-extend the 2nd operand for shifts and rotates when operands have different types. The shift operand width in IR is TYP_INT but WASM operations require matching widths, so extension may be needed for TYP_LONG operations. Co-authored-by: kg <198130+kg@users.noreply.github.com>
Added TODO-WASM comment in commit b608464. The comment documents the need to zero-extend the 2nd operand for shifts and rotates when operands have different types, as the shift operand width in IR is TYP_INT but WASM operations require matching widths. |
|
/ba-g OSX lanes are still stuck due to ongoing issue and this PR only impacts WASM. |
Changes Made
Latest Update
Added TODO-WASM comment in
genCodeForShiftOrRotatedocumenting the need to zero-extend the 2nd operand for shifts and rotates when the 1st and 2nd operands have different types. This addresses SingleAccretion's feedback about the shift operand width requirements.Previous Updates
CodeGen::genCodeForShiftOrRotatewith same structure asgenCodeForBinarygenCodeForBinary#ifdef TARGET_WASMdeclaration incodegen.hgenCodeForTreeNodeto route shifts to the new functiontreeNode->gtOverflow()checks for GT_ADD, GT_SUB, and GT_MULOriginal prompt
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.