thatrajeevkr opened PR #11583 from thatrajeevkr:fix/riscv-uadd-overflow to bytecodealliance:main:
Implements ISLE lowering for uadd_overflow.i64 in the RISC-V backend, enabling Cranelift to handle unsigned addition with overflow for 64-bit, sub-64-bit, and 128-bit types.
i64: add + sltu to detect overflow
Sub-64-bit types: zero-extend, then add + sltu
i128: multiword add with carry propagation
Fixes unsupported feature error when compiling Wasm modules with large address offsets
Fix #11540
thatrajeevkr requested cfallin for a review on PR #11583.
thatrajeevkr requested wasmtime-compiler-reviewers for a review on PR #11583.
bjorn3 submitted PR review.
bjorn3 created PR review comment:
I don't think this would work. rv_sltu does a 64bit unsigned less than comparison, but if you add two 32bit integers into a 64bit register you will never overflow the full 64bit and thus
tmp_x + tmp_ywill never be less thantmp_x. What LLVM does instead is to use a 32bit add (addw), in which casesltuwould work just fine. That way also onlyxwould need to be zero extended I think. https://rust.godbolt.org/z/zz5avj33h
thatrajeevkr updated PR #11583.
thatrajeevkr submitted PR review.
thatrajeevkr created PR review comment:
Thanks for the input, I have put in a small fix for this.
thatrajeevkr requested bjorn3 for a review on PR #11583.
cfallin submitted PR review:
This looks good -- thanks!
In addition to the little formatting nit below, could you update our runtest for this instruction so that it tests riscv64 too?
cfallin created PR review comment:
Tiny nit, but let's have a blank line between the section header comment and the
;; For i64 ...start of this rule.
thatrajeevkr updated PR #11583.
thatrajeevkr submitted PR review.
thatrajeevkr created PR review comment:
just made this change now!
thatrajeevkr updated PR #11583.
cfallin submitted PR review:
Thanks!
cfallin has enabled auto merge for PR #11583.
cfallin commented on PR #11583:
@thatrajeevkr it looks like the runtest failed because it also contains narrower-than-32-bit cases; in theory you could implement those as well, but probably the easier fix is to split the runtest file into two (keep the preamble and split the test functions and expectations), with 64/32-bit cases separate from 16/8-bit cases (perhaps in a file called
uadd_overflow_narrow.clif)?
cfallin edited a comment on PR #11583:
@thatrajeevkr it looks like the runtest failed because it also contains narrower-than-32-bit cases; in theory you could implement those as well, but probably the easier fix is to split the runtest file into two (keep the preamble and split the test functions and expectations), with 128/64/32-bit cases separate from 16/8-bit cases (perhaps in a file called
uadd_overflow_narrow.clif) and then enable riscv64 only for 128/64/32 bits?
cfallin has disabled auto merge for PR #11583.
thatrajeevkr updated PR #11583.
cfallin commented on PR #11583:
@thatrajeevkr It looks like an i128 test is failing now, showing overflow when there is none -- mind taking a look?
thatrajeevkr updated PR #11583.
cfallin submitted PR review.
cfallin created PR review comment:
Thinking about this tweak -- I suspect that the second arm of the OR was supposed to cover the case where the high-half sum wraps around back to the original value when carry is set?
That is, normally we can see if
x + yoverflows by checking if the sum is less thanx(has wrapped around). The closest it can get is ifyis all-ones (-1), then we get tox - 1; so strict less than (sltu, i.e., set-less-than unsigned) is appropriate.But for
x + y + carry, it's possible that the sum wraps around all the way back tox, socarry && (x + y + carry == x)is the appropriate second case, I think? So overallsum_hi_with_carry < x_hi || (carry && sum_high_with_carry == x_hi)?
thatrajeevkr submitted PR review.
thatrajeevkr created PR review comment:
overflow XReg (rv_or (rv_sltu sum_hi_with_carry x_hi) (rv_and carry (rv_eq sum_hi_with_carry x_hi)))how about this method?
cfallin submitted PR review.
cfallin created PR review comment:
That looks right; and I think the remaining failure should cover the case that this handles!
thatrajeevkr updated PR #11583.
cfallin submitted PR review.
cfallin created PR review comment:
@thatrajeevkr actually it appears that
rv_eqisn't a defined constructor -- see the latest test failures. You should be able tocargo checklocally (as that error message states) to iterate until it builds at least, before going through a CI roundtrip. (We also have docs on cross-compiling and testing with qemu if you want to iterate on the runtest locally, but that's much more involved...)
thatrajeevkr submitted PR review.
thatrajeevkr created PR review comment:
I found a fix for this which builds fine in my local, I will commit those changes.
thatrajeevkr updated PR #11583.
cfallin merged PR #11583.
Last updated: Dec 06 2025 at 06:05 UTC