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

Closed
lcg007xd-c wants to merge 0 commits from upstream-pr/71 into main
lcg007xd-c commented 2026-11-25 21:37:40 +00:00 (Migrated from github.com)

Imported from uiliannn/VItoriaBr. Originally posted by lcg007xd-c on 2026-11-25T21:37:40Z.


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

<!-- greyfinch-sync:uiliannn/VItoriaBr:issue:5499597213 --> Imported from [uiliannn/VItoriaBr](https://github.com/uiliannn/VitoriaBr2.0/pull/71). Originally posted by [lcg007xd-c](https://github.com/lcg007xd-c) on 2026-11-25T21:37:40Z. --- ## 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)

Pull request closed

Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
uiliannn/VItoriaBr!71
No description provided.