shader: lower v_add_nc_i32/v_sub_nc_i32/v_add_nc_i16/v_sub_nc_i16 (saturating under clamp) #71
No reviewers
Labels
No labels
accessibility
bug
documentation
duplicate
enhancement
good first issue
help wanted
invalid
question
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/uiliannn--VItoriaBr!71
Loading…
Reference in a new issue
No description provided.
Delete branch "github-bot/gh-71"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
@lcg007xd-c on GitHub · 2026-11-25 21:37 UTC · original
lcg007xd-c:shader-signed-add-sub→main· +145 −1 · 2 files · 1 commits · openWhat is broken
The signed adds decode but do not translate:
v_add_nc_i32v_sub_nc_i32v_add_nc_i16v_sub_nc_i16A program using one fails with
v_add_nc_i32 is not supported(and likewise for the others). I found them by translating a one-instruction program for every VOP3 opcode the decoder names.Semantics
D.i = S0.i ± S1.iand says each "supports saturation (signed … integer domain)". That is the VOP3 clamp bit.clampforllvm.sadd.satandllvm.ssub.saton gfx10 (v_add_nc_i32 … clamp,v_sub_nc_i16 … clamp).The change (
emit/lower.rs)valu_op): add or subtract. With clamp, overflow is detected from signs: the result's sign differs froma's, andb's sign agrees witha's for an add or differs for a subtract. On overflow the result is the limit ona's side (0x80000000or0x7FFFFFFF). This needs no 64-bit integers. An output modifier is still refused.valu16_op, besidev_add_nc_u16): the sign-extended halves are added in i32, where they cannot overflow. With clamp the result is limited to [−32768, 32767]. Thenwrite_halfwrites the selected half and keeps the other.Tests
emit::lower::tests::signed_adds_translate_with_and_without_clamp(every host): each encoding decodes to its name, and a program that stores the result translates both clamped and unclamped. Without thelower.rschange it fails:v_add_nc_i32 clamp 0: GFX10 code at 0x24: v_add_nc_i32 is not supported.valu_tests::signed_adds_wrap_and_saturate_under_clamp_like_rdna2(Windows/GPU): three cases per opcode, each run with and without clamp. They cover overflow at both ends (wrapping vs saturating) and an ordinary negative result. The 16-bit inputs have junk in their high halves.lli. I also compared against Rust'ssaturating_add/saturating_subandclampon 56 edge pairs for each 32-bit form, and on a 16 × 22 grid of 16-bit pairs forv_sub_nc_i16clamp. All match.Run here (Linux, Rust 1.98.1):
cargo test -p victory-shader --lib --locked: 188 passed, 0 failed, 2 ignored.CARGO_FEATURE_PURE=1 cargo clippy -p victory-shader --all-targets --target x86_64-pc-windows-msvc -- -D warnings: clean.Not run here: the GPU case, since I have no Windows machine.
🤖 Generated with Claude Code
VOP3 0x37F, 0x376, 0x30D and 0x30E decoded but failed to translate ("v_add_nc_i32 is not supported"). They are the signed adds: without clamp the same bits as the unsigned ones, and with clamp the result saturates to the signed range (AMD's RDNA2 ISA: "Supports saturation (signed integer domain)"; LLVM selects them for llvm.sadd.sat and llvm.ssub.sat). The 32-bit forms detect overflow from the operands' and result's signs; the 16-bit forms add the sign-extended halves in i32 and clamp to [-32768, 32767]. Co-Authored-By: Claude Opus 5.5 <[email protected]>View command line instructions
Manual merge helper
Use this merge commit message when completing the merge manually.
Checkout
From your project repository, check out a new branch and test the changes.Merge
Merge the changes and update on Forgejo.