Shaders: a signed 64-bit operand sign-extends its 32-bit literal #447

Merged
Dufftwt merged 1 commit from upstream-pr/447 into main 2026-11-25 00:16:40 +00:00
Dufftwt commented 2026-11-24 03:31:05 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Dufftwt on 2026-11-24T03:31:05Z.


What's wrong

A literal (the 32-bit constant word that follows an instruction) can feed a 64-bit operand. The hardware widens it to 64 bits:

  • sign-extended when the operand is signed (i64);
  • zero-extended when it is unsigned or untyped (u64, b64).

LLVM's AMDGPU operand rules spell this out, and LLVM PR #70274 discusses it: a negative literal is "correctly sign extended or incorrectly zero extended" depending on the operand's type.

The lowering always zero-extends:

  • in src64, the vector path: // Literals and the null register: zero-extended.;
  • in the scalar path's half, where a literal's high dword is 0.

So every signed 64-bit operation with a negative literal sees a large positive number instead:

v_cmp_lt_i64 vcc, v[1:2], -100        compares with 0x00000000_FFFFFF9C = 4294967196
  v[1:2] = 0          -> hardware 0 (0 < -100 is false),   ours 1
v_cmp_eq_i64 vcc, v[1:2], -100
  v[1:2] = -100       -> hardware 1,                        ours 0
s_ashr_i64 s[6:7], -100, 4
  high dword          -> hardware 0xFFFFFFFF,               ours 0

The affected instructions are v_cmp_*_i64 / v_cmpx_*_i64, v_ashrrev_i64 (its 64-bit source) and s_ashr_i64.

What the fix does

  • In src64, a literal of an *_i64 instruction is sign-extended. Inline constants already were, and other literals stay zero-extended as before.
  • In the scalar path's half, a literal's high dword is the sign of the literal for *_i64 instructions. It stays 0 otherwise.

How it behaves after the fix

Signed 64-bit compares and arithmetic shifts with a negative literal see the same value the hardware does. Unsigned and bit operands (*_u64, *_b64), inline constants and register operands don't change.

Tests

The new test signed_64_bit_operands_sign_extend_their_literal (valu_tests.rs, run on the host GPU and WARP) checks:

  • v_cmp_eq_i64 with the literal -100, for v[1:2] = -100 (1), 0x00000000_FFFFFF9C (0) and 0 (0);
  • v_cmp_lt_i64 with -100, for -200 (1), -50 (0) and 0 (0);
  • s_ashr_i64 s[6:7], -100, 4, whose high dword must be 0xFFFFFFFF.

I ran cargo test --workspace on GitHub's windows-latest with Rust 1.98.1 from rust-toolchain.toml (the runner has a hardware D3D12 device as well as WARP):

  • This PR: everything passes. victory-shader has 70 passed, the other crates have 0 failed.
  • The new test alone on main: it fails at its first case: hardware: v_cmp_eq_i64 [ffffff9c, ffffffff, 0, 0] gives 0 where the hardware rule gives 1.
  • I ran with --test-threads=1. With 4 threads, the shader crate's GPU tests sometimes crash on that runner (#181, #244).

Not tested: a title shader with such a literal.

Related: #163 also adds literal sign-extension to these two functions, but only for v_mad_i64_i32 and s_bfe_i64 (plus f64 literals in the high dword). It touches the same match arms, so whichever merges second needs a small rebase; the two changes are compatible.

<!-- greyfinch-sync:Marvisak/VIctory:issue:6573980019 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/447). Originally posted by [Dufftwt](https://github.com/Dufftwt) on 2026-11-24T03:31:05Z. --- ## What's wrong A literal (the 32-bit constant word that follows an instruction) can feed a 64-bit operand. The hardware widens it to 64 bits: - **sign-extended** when the operand is signed (`i64`); - **zero-extended** when it is unsigned or untyped (`u64`, `b64`). LLVM's AMDGPU operand rules spell this out, and LLVM PR #70274 discusses it: a negative literal is "correctly sign extended or incorrectly zero extended" depending on the operand's type. The lowering **always zero-extends**: - in `src64`, the vector path: `// Literals and the null register: zero-extended.`; - in the scalar path's `half`, where a literal's high dword is 0. So every signed 64-bit operation with a negative literal sees a large positive number instead: ``` v_cmp_lt_i64 vcc, v[1:2], -100 compares with 0x00000000_FFFFFF9C = 4294967196 v[1:2] = 0 -> hardware 0 (0 < -100 is false), ours 1 v_cmp_eq_i64 vcc, v[1:2], -100 v[1:2] = -100 -> hardware 1, ours 0 s_ashr_i64 s[6:7], -100, 4 high dword -> hardware 0xFFFFFFFF, ours 0 ``` The affected instructions are `v_cmp_*_i64` / `v_cmpx_*_i64`, `v_ashrrev_i64` (its 64-bit source) and `s_ashr_i64`. ## What the fix does - In `src64`, a literal of an `*_i64` instruction is sign-extended. Inline constants already were, and other literals stay zero-extended as before. - In the scalar path's `half`, a literal's high dword is the sign of the literal for `*_i64` instructions. It stays 0 otherwise. ## How it behaves after the fix Signed 64-bit compares and arithmetic shifts with a negative literal see the same value the hardware does. Unsigned and bit operands (`*_u64`, `*_b64`), inline constants and register operands don't change. ## Tests The new test `signed_64_bit_operands_sign_extend_their_literal` (`valu_tests.rs`, run on the host GPU and WARP) checks: - `v_cmp_eq_i64` with the literal -100, for `v[1:2]` = -100 (1), 0x00000000_FFFFFF9C (0) and 0 (0); - `v_cmp_lt_i64` with -100, for -200 (1), -50 (0) and 0 (0); - `s_ashr_i64 s[6:7], -100, 4`, whose high dword must be 0xFFFFFFFF. I ran `cargo test --workspace` on GitHub's `windows-latest` with Rust 1.98.1 from `rust-toolchain.toml` (the runner has a hardware D3D12 device as well as WARP): - **This PR:** everything passes. `victory-shader` has 70 passed, the other crates have 0 failed. - **The new test alone on `main`:** it fails at its first case: `hardware: v_cmp_eq_i64 [ffffff9c, ffffffff, 0, 0]` gives `0` where the hardware rule gives `1`. - I ran with `--test-threads=1`. With 4 threads, the shader crate's GPU tests sometimes crash on that runner (#181, #244). **Not tested:** a title shader with such a literal. Related: #163 also adds literal sign-extension to these two functions, but only for `v_mad_i64_i32` and `s_bfe_i64` (plus `f64` literals in the high dword). It touches the same match arms, so whichever merges second needs a small rebase; the two changes are compatible.
mercuryVM commented 2026-11-24 06:53:06 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by mercuryVM on 2026-11-24T05:35:41Z.

Review: APPROVED


Tested 7d04943 on Windows 11, RTX 5070, MSVC: cargo test --workspace --no-fail-fast passes (265 passed, 0 failed). victory translate <title exe> --validate --pipelines gives the same counts before and after: 12 of 12 programs emitted, 12 validated, 12 pipelines.

  • Both the vector src64 path and the scalar half path now sign-extend a literal when the mnemonic ends in _i64. Unsigned and bit operands still zero-extend.
  • Shift amounts (s_ashr_i64 src1, v_ashrrev_i64 src0) are masked to 6 bits, so it makes no difference which extension they get.
  • The new test's v_cmp_eq_i64 case [MINUS_100, 0, 0, 0] → 0 is a good check, since it is exactly the value the old zero-extension produced.
  • Keying on the name suffix depends on mnemonics staying consistent. Any later instruction with a signed 64-bit operand whose name does not end in _i64 (for example v_mad_i64_i32 src2, which is not lowered today) will need the same handling.

AI-assisted (Claude Code).

<!-- greyfinch-sync:Marvisak/VIctory:review:6034218105 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/447#pullrequestreview-6034218105). Originally posted by [mercuryVM](https://github.com/mercuryVM) on 2026-11-24T05:35:41Z. **Review:** APPROVED --- Tested 7d04943 on Windows 11, RTX 5070, MSVC: `cargo test --workspace --no-fail-fast` passes (265 passed, 0 failed). `victory translate <title exe> --validate --pipelines` gives the same counts before and after: 12 of 12 programs emitted, 12 validated, 12 pipelines. - Both the vector `src64` path and the scalar `half` path now sign-extend a literal when the mnemonic ends in `_i64`. Unsigned and bit operands still zero-extend. - Shift amounts (`s_ashr_i64` src1, `v_ashrrev_i64` src0) are masked to 6 bits, so it makes no difference which extension they get. - The new test's `v_cmp_eq_i64` case `[MINUS_100, 0, 0, 0]` → 0 is a good check, since it is exactly the value the old zero-extension produced. - Keying on the name suffix depends on mnemonics staying consistent. Any later instruction with a signed 64-bit operand whose name does not end in `_i64` (for example `v_mad_i64_i32` src2, which is not lowered today) will need the same handling. AI-assisted (Claude Code).
slint6614-dev commented 2026-11-24 11:04:09 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by slint6614-dev on 2026-11-24T10:16:42Z.


AMD hardware check, Windows 11, Adrenalin driver: RX 9070 (RDNA4) and Ryzen 9600X iGPU (RDNA2, gfx1036), each picked as the hardware device of run_cases (local adapter-selection patch, not part of this PR); cargo test -p victory-shader --release valu_tests at the PR head. Negative control: the PR's non-test files reverted to its merge base, its new tests kept.

  • With this PR, signed_64_bit_operands_sign_extend_their_literal pass on both AMD GPUs.
  • Negative control: it fail on both GPUs without the fix, so the tests do catch the change on AMD hardware.
<!-- greyfinch-sync:Marvisak/VIctory:comment:7128558395 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/447#issuecomment-7128558395). Originally posted by [slint6614-dev](https://github.com/slint6614-dev) on 2026-11-24T10:16:42Z. --- AMD hardware check, Windows 11, Adrenalin driver: RX 9070 (RDNA4) and Ryzen 9600X iGPU (RDNA2, gfx1036), each picked as the hardware device of `run_cases` (local adapter-selection patch, not part of this PR); `cargo test -p victory-shader --release valu_tests` at the PR head. Negative control: the PR's non-test files reverted to its merge base, its new tests kept. - With this PR, `signed_64_bit_operands_sign_extend_their_literal` pass on both AMD GPUs. - Negative control: it fail on both GPUs without the fix, so the tests do catch the change on AMD hardware.
catminou4 commented 2026-11-24 14:03:59 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by catminou4 on 2026-11-24T13:20:34Z.

Review: APPROVED


Matches the AMDGPU operand rules: a 32-bit literal in a signed 64-bit operand sign-extends (i64::from(x as i32)), while unsigned/bit operands keep zero-extension — both call sites (v_cmp_*_i64 family and the generic i64 operand path) are covered, and the cmp-helper hunk mirrors the existing inline/special-operand handling. The comment citing LLVM's AMDGPU operand rules is exactly the right reference.

The valu_tests case is well constructed: v_cmp_eq_i64/v_cmp_lt_i64 against -100 (sign-extended) plus s_ashr_i64 checking the high dword — it would catch a zero-extension regression.

Verified locally: test-merge with current main (ffe310e) clean; cargo test -p victory-shader --locked (macOS arm64, rustc 1.98.1) green — the new valu_tests execution cases are #[cfg(windows)]-gated like the rest of that suite, so they will run on the Windows CI lane, not here.: 96 passed / 0 failed / 1 ignored (execution tests Windows-gated).

<!-- greyfinch-sync:Marvisak/VIctory:review:6039912457 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/447#pullrequestreview-6039912457). Originally posted by [catminou4](https://github.com/catminou4) on 2026-11-24T13:20:34Z. **Review:** APPROVED --- Matches the AMDGPU operand rules: a 32-bit literal in a *signed* 64-bit operand sign-extends (`i64::from(x as i32)`), while unsigned/bit operands keep zero-extension — both call sites (`v_cmp_*_i64` family and the generic i64 operand path) are covered, and the cmp-helper hunk mirrors the existing inline/special-operand handling. The comment citing LLVM's AMDGPU operand rules is exactly the right reference. The `valu_tests` case is well constructed: `v_cmp_eq_i64`/`v_cmp_lt_i64` against -100 (sign-extended) plus `s_ashr_i64` checking the high dword — it would catch a zero-extension regression. Verified locally: test-merge with current main (`ffe310e`) clean; `cargo test -p victory-shader --locked` (macOS arm64, rustc 1.98.1) green — the new `valu_tests` execution cases are `#[cfg(windows)]`-gated like the rest of that suite, so they will run on the Windows CI lane, not here.: **96 passed / 0 failed / 1 ignored** (execution tests Windows-gated).
upstream-sync closed this pull request 2026-11-25 01:03:46 +00:00
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
Marvisak/VIctory!447
No description provided.