shader: lower v_add_nc_i32/v_sub_nc_i32/v_add_nc_i16/v_sub_nc_i16 (saturating under clamp) #71

Open
github-bot wants to merge 1 commit from github-bot/gh-71 into main AGit
Owner

@⁠lcg007xd-c on GitHub · 2026-11-25 21:37 UTC · original
lcg007xd-c:shader-signed-add-sub → main · +145 −1 · 2 files · 1 commits · open

What is broken

The signed adds decode but do not translate:

Opcode Instruction
VOP3 0x37F v_add_nc_i32
VOP3 0x376 v_sub_nc_i32
VOP3 0x30D v_add_nc_i16
VOP3 0x30E v_sub_nc_i16

A 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

  • AMD's RDNA2 ISA (§12.12, opcodes 781, 782, 886, 895) defines them as D.i = S0.i ± S1.i and says each "supports saturation (signed … integer domain)". That is the VOP3 clamp bit.
  • LLVM selects exactly these instructions with clamp for llvm.sadd.sat and llvm.ssub.sat on gfx10 (v_add_nc_i32 … clamp, v_sub_nc_i16 … clamp).
  • Without clamp, the result is the same bits as the unsigned add and subtract.

The change (emit/lower.rs)

  • 32-bit (in valu_op): add or subtract. With clamp, overflow is detected from signs: the result's sign differs from a's, and b's sign agrees with a's for an add or differs for a subtract. On overflow the result is the limit on a's side (0x80000000 or 0x7FFFFFFF). This needs no 64-bit integers. An output modifier is still refused.
  • 16-bit (in valu16_op, beside v_add_nc_u16): the sign-extended halves are added in i32, where they cannot overflow. With clamp the result is limited to [−32768, 32767]. Then write_half writes the selected half and keeps the other.
  • Clamp stays refused for the other 16-bit integer operations, as before.

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 the lower.rs change 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.
  • CPU run: I ran the same cases on Linux through the CPU harness of #62, which runs the emitted DXIL with LLVM's lli. I also compared against Rust's saturating_add/saturating_sub and clamp on 56 edge pairs for each 32-bit form, and on a 16 × 22 grid of 16-bit pairs for v_sub_nc_i16 clamp. All match.

Run here (Linux, Rust 1.98.1):

  • cargo test -p victory-shader --lib --locked: 188 passed, 0 failed, 2 ignored.
  • Portable clippy and 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

**@⁠lcg007xd-c** on GitHub · 2026-11-25 21:37 UTC · [original](https://github.com/uiliannn/VItoriaBr/pull/71) `lcg007xd-c:shader-signed-add-sub` → `main` · +145 −1 · 2 files · 1 commits · open ## What is broken The signed adds decode but do not translate: | Opcode | Instruction | |---|---| | VOP3 0x37F | `v_add_nc_i32` | | VOP3 0x376 | `v_sub_nc_i32` | | VOP3 0x30D | `v_add_nc_i16` | | VOP3 0x30E | `v_sub_nc_i16` | A 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 - **AMD's RDNA2 ISA** (§12.12, opcodes 781, 782, 886, 895) defines them as `D.i = S0.i ± S1.i` and says each "supports saturation (signed … integer domain)". That is the VOP3 clamp bit. - **LLVM** selects exactly these instructions with `clamp` for `llvm.sadd.sat` and `llvm.ssub.sat` on gfx10 (`v_add_nc_i32 … clamp`, `v_sub_nc_i16 … clamp`). - **Without clamp**, the result is the same bits as the unsigned add and subtract. ## The change (`emit/lower.rs`) - **32-bit** (in `valu_op`): add or subtract. With clamp, overflow is detected from signs: the result's sign differs from `a`'s, and `b`'s sign agrees with `a`'s for an add or differs for a subtract. On overflow the result is the limit on `a`'s side (`0x80000000` or `0x7FFFFFFF`). This needs no 64-bit integers. An output modifier is still refused. - **16-bit** (in `valu16_op`, beside `v_add_nc_u16`): the sign-extended halves are added in i32, where they cannot overflow. With clamp the result is limited to [−32768, 32767]. Then `write_half` writes the selected half and keeps the other. - Clamp stays refused for the other 16-bit integer operations, as before. ## 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 the `lower.rs` change 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. - **CPU run:** I ran the same cases on Linux through the CPU harness of #62, which runs the emitted DXIL with LLVM's `lli`. I also compared against Rust's `saturating_add`/`saturating_sub` and `clamp` on 56 edge pairs for each 32-bit form, and on a 16 × 22 grid of 16-bit pairs for `v_sub_nc_i16` clamp. All match. **Run here** (Linux, Rust 1.98.1): - `cargo test -p victory-shader --lib --locked`: 188 passed, 0 failed, 2 ignored. - Portable clippy and `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](https://claude.com/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]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 03:43:03 +00:00
This pull request has changes conflicting with the target branch.
  • crates/victory-shader/src/emit/lower.rs
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.
git fetch -u origin '+refs/pull/71/head:github-bot/gh-71'
git switch 'github-bot/gh-71'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-71'
git switch 'github-bot/gh-71'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-71'
git switch 'github-bot/gh-71'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-71'
git switch 'main'
git merge --squash 'github-bot/gh-71'
git switch 'main'
git merge --ff-only 'github-bot/gh-71'
git switch 'main'
git merge 'github-bot/gh-71'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.