rdna2spirv: sign-extend the 32-bit literal of v_cmp(x)_*_i64, v_ashrrev_i64, v_mad_i64_i32 (SHADER-VALU-LITERAL-SEXT) #1493

Manually merged
github merged 1 commit from gh-pr/1493/head into gh-pr/1493/base 2026-11-26 09:53:59 +00:00
Owner

@⁠QemmHD on GitHub · 2026-11-25 01:53 UTC · original
ubiquitarianism:claude/valu-literal-sext → main · +185 −6 · 3 files · 1 commits · merged 2026-11-25 10:01 UTC

Claimed in the coordination issue as: SHADER-VALU-LITERAL-SEXT (#14)

Related: #978 (OlaiFG7, the scalar half; no conflict, merges cleanly in either order)

What and why

A 32-bit literal used as a signed 64-bit vector operand is now sign-extended (Gen.valu_imm_i64 / Gen.src64_i64), following LLVM's AMDGPU operand-syntax rule ("Conversion of Integer Values"): i64 → {-1,num.i32}, u64/b64 → {0,num.u32}. llvm-mc prints -100 as 0xffffff9c (both spellings encode to the same bytes on gfx1030), and the translator zero-extended that for every 64-bit VALU source, so:

  • v_cmp_lt_i64 5, 0xffffff9c gave 1 instead of 0;
  • v_ashrrev_i64 v[2:3], 4, 0xffffff9c gave 0x0ffffff9:0 instead of 0xfffffff9:0xffffffff;
  • v_mad_i64_i32 …, 1, 1, 0xffffff9c had a high word of 0 instead of 0xffffffff.

Covered: v_cmp(x)_{lt,le,gt,ge,eq,ne,lg}_i64, v_ashrrev_i64 S1 and v_mad_i64_i32 S2. Unchanged by design: the u64/b64 sources (v_cmp_*_u64, v_lshlrev/lshrrev_b64, v_mad_u64_u32 S2, the qsad forms) still zero-extend; f64 literals keep their high-word rule (already correct); inline constants (-16..64) were already full width. src64_data itself is unchanged.

This is the vector half of #978. Found while comparing with VIctory (Marvisak/VIctory 1134418); written anew here.

  • docs/design.md: a contract-table row, confirmed by the LLVM operand-syntax doc, the llvm-mc encodings and #978's precedent. Not measured on hardware.
  • tests/semantic/test_valu_signed_literal64.py: 8 hand-written tests, no game data. Each signed case uses three spellings (decimal, printed hex, explicit SGPR pair), with controls: a positive literal, an inline constant, and the unsigned ops.

Tested

  • New test: 5 of 8 fail on the previous translator (only the hex spelling was wrong); 8 of 8 pass with the fix.
  • Full semantic suite, rebased onto main 0aecc5efa (after #984): 484 files, 6,262 tests, 0 failed.
  • Repository-consistency set: 580 passed, 5 skipped. compileall is clean. Merges cleanly onto current main.
  • Merged locally with #978: clean, and on the merged tree, #978's scalar literal test, this one, test_alu_ashr_i64 and test_alu_mad64 all pass (90 tests). Once both are in, #978's imm64_signed and this PR's valu_imm_i64 do the same thing, so a follow-up could fold them into one.
  • No GPU run or corpus count (no game files). Windows 11, CPU interpreter.

GPUs tested: n/a (translator; CPU interpreter)

AI-assisted: yes

Checklist

  • Based on current main and merges cleanly.
  • One topic.
  • No game data, keys or decryption.
  • The new translator rule is in the contract table of docs/design.md, with how it was confirmed.

AI-assisted (Claude Code, Claude Opus 5.5).

🤖 Generated with Claude Code

https://claude.ai/code/session_01SR1Fv7eQNgZjM6miWPKjHf

**@⁠QemmHD** on GitHub · 2026-11-25 01:53 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1493) `ubiquitarianism:claude/valu-literal-sext` → `main` · +185 −6 · 3 files · 1 commits · merged 2026-11-25 10:01 UTC Claimed in the coordination issue as: SHADER-VALU-LITERAL-SEXT (https://git.gtahub.st/github/ubiquitarianism--leonida/issues/14) Related: #978 (OlaiFG7, the scalar half; no conflict, merges cleanly in either order) ### What and why A 32-bit literal used as a **signed** 64-bit vector operand is now sign-extended (`Gen.valu_imm_i64` / `Gen.src64_i64`), following LLVM's AMDGPU operand-syntax rule ("Conversion of Integer Values"): i64 → `{-1,num.i32}`, u64/b64 → `{0,num.u32}`. llvm-mc prints `-100` as `0xffffff9c` (both spellings encode to the same bytes on gfx1030), and the translator zero-extended that for every 64-bit VALU source, so: - `v_cmp_lt_i64 5, 0xffffff9c` gave 1 instead of 0; - `v_ashrrev_i64 v[2:3], 4, 0xffffff9c` gave `0x0ffffff9:0` instead of `0xfffffff9:0xffffffff`; - `v_mad_i64_i32 …, 1, 1, 0xffffff9c` had a high word of 0 instead of `0xffffffff`. Covered: `v_cmp(x)_{lt,le,gt,ge,eq,ne,lg}_i64`, `v_ashrrev_i64` S1 and `v_mad_i64_i32` S2. Unchanged by design: the u64/b64 sources (`v_cmp_*_u64`, `v_lshlrev/lshrrev_b64`, `v_mad_u64_u32` S2, the qsad forms) still zero-extend; f64 literals keep their high-word rule (already correct); inline constants (-16..64) were already full width. `src64_data` itself is unchanged. This is the vector half of #978. Found while comparing with VIctory (Marvisak/VIctory `1134418`); written anew here. - `docs/design.md`: a contract-table row, confirmed by the LLVM operand-syntax doc, the llvm-mc encodings and #978's precedent. Not measured on hardware. - `tests/semantic/test_valu_signed_literal64.py`: 8 hand-written tests, no game data. Each signed case uses three spellings (decimal, printed hex, explicit SGPR pair), with controls: a positive literal, an inline constant, and the unsigned ops. ### Tested - **New test:** 5 of 8 fail on the previous translator (only the hex spelling was wrong); 8 of 8 pass with the fix. - **Full semantic suite**, rebased onto main 0aecc5efa (after #984): 484 files, 6,262 tests, 0 failed. - **Repository-consistency set:** 580 passed, 5 skipped. `compileall` is clean. Merges cleanly onto current main. - **Merged locally with #978:** clean, and on the merged tree, #978's scalar literal test, this one, `test_alu_ashr_i64` and `test_alu_mad64` all pass (90 tests). Once both are in, #978's `imm64_signed` and this PR's `valu_imm_i64` do the same thing, so a follow-up could fold them into one. - No GPU run or corpus count (no game files). Windows 11, CPU interpreter. GPUs tested: n/a (translator; CPU interpreter) AI-assisted: yes ### Checklist - [x] Based on current `main` and merges cleanly. - [x] One topic. - [x] No game data, keys or decryption. - [x] The new translator rule is in the contract table of `docs/design.md`, with how it was confirmed. AI-assisted (Claude Code, Claude Opus 5.5). 🤖 Generated with [Claude Code](https://claude.com/claude-code) https://claude.ai/code/session_01SR1Fv7eQNgZjM6miWPKjHf
LLVM's operand syntax widens a 32-bit literal in an i64 operand by sign
extension and in a u64/b64 operand by zero extension. llvm-mc prints -100
as 0xffffff9c, and the vector ops zero-extended that printed form, so
v_cmp_lt_i64 5, 0xffffff9c gave 1, v_ashrrev_i64 4, 0xffffff9c gave
0x0ffffff9:0 and v_mad_i64_i32 1, 1, 0xffffff9c had a zero high word.
The signed sources now go through Gen.valu_imm_i64; the u64/b64 ones
(v_cmp_*_u64, v_lshlrev/lshrrev_b64, v_mad_u64_u32) still zero-extend,
and f64 literals keep their high-word rule. Vector half of #978.

Tested: new tests/semantic/test_valu_signed_literal64.py (decimal, printed
hex and explicit pair agree; unsigned, positive-literal and inline-constant
controls) fails 5/8 on main and passes 8/8; full semantic suite 476 files /
6223 tests before, 477 / 6231 after, 0 failures; repo-consistency pytest
set 578 passed, 5 skipped; compileall clean. CPU only, no hardware.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01SR1Fv7eQNgZjM6miWPKjHf
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 09:53:57 +00:00
github manually merged commit bc2d333495 into gh-pr/1493/base 2026-11-26 09:53:59 +00:00
Author
Owner

@⁠ecox982745 on GitHub · 2026-11-25 02:15 UTC · original

On an RTX 4090 (Windows 11, NVIDIA 616.64), the parent 0aecc5ef gave 444 incorrect results across 2,016 lane outputs. The PR head bc2d3334 matched the independent integer reference on all 2,016.

I assembled 21 synthetic compute shaders with LLVM: seven operations, each using 0xffffff9c, 0x80000000, or 0x7fffffff as the instruction's literal. The other operands came from a buffer at dispatch. The 96 lane inputs per shader included both 64-bit widenings of the negative literals, their immediate neighbors, and every shift count from 0 to 63. All 21 modules passed Vulkan 1.2 SPIR-V validation on both revisions.

Operation Wrong lane outputs on parent Wrong on head
Signed less-than comparison 54 0
Signed equality comparison that updates EXEC (v_cmpx_eq_i64_e32) 6 0
Arithmetic right shift 192 0
Signed multiply-add 192 0
Unsigned compare, logical right shift and unsigned multiply-add 0 0

The EXEC case makes the effect visible directly: with literal 0xffffff9c, the parent activated the lane holding 0x00000000ffffff9c. The head activated the lane holding 0xffffffffffffff9c instead. Each active lane wrote 1, then EXEC was restored before storing the results.

I used the compute harness built from the PR head for both runs. The PR's eight focused CPU tests also passed. These readbacks exercise the translated operations on NVIDIA; native RDNA2 execution was outside this run. AI-assisted verification.

**@⁠ecox982745** on GitHub · 2026-11-25 02:15 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1493#issuecomment-7150504545) On an RTX 4090 (Windows 11, NVIDIA 616.64), the parent `0aecc5ef` gave 444 incorrect results across 2,016 lane outputs. The PR head `bc2d3334` matched the independent integer reference on all 2,016. I assembled 21 synthetic compute shaders with LLVM: seven operations, each using `0xffffff9c`, `0x80000000`, or `0x7fffffff` as the instruction's literal. The other operands came from a buffer at dispatch. The 96 lane inputs per shader included both 64-bit widenings of the negative literals, their immediate neighbors, and every shift count from 0 to 63. All 21 modules passed Vulkan 1.2 SPIR-V validation on both revisions. | Operation | Wrong lane outputs on parent | Wrong on head | | --- | ---: | ---: | | Signed less-than comparison | 54 | 0 | | Signed equality comparison that updates EXEC (`v_cmpx_eq_i64_e32`) | 6 | 0 | | Arithmetic right shift | 192 | 0 | | Signed multiply-add | 192 | 0 | | Unsigned compare, logical right shift and unsigned multiply-add | 0 | 0 | The EXEC case makes the effect visible directly: with literal `0xffffff9c`, the parent activated the lane holding `0x00000000ffffff9c`. The head activated the lane holding `0xffffffffffffff9c` instead. Each active lane wrote 1, then EXEC was restored before storing the results. I used the compute harness built from the PR head for both runs. The PR's eight focused CPU tests also passed. These readbacks exercise the translated operations on NVIDIA; native RDNA2 execution was outside this run. AI-assisted verification.
Author
Owner

@⁠QemmHD on GitHub · 2026-11-25 02:22 UTC · original

Thanks @⁠ecox982745, that's a much stronger check than my CPU tests: 2,016 lane outputs on real hardware, an independent reference, and the unsigned controls unchanged. The v_cmpx_eq_i64_e32 case is especially useful, because my tests covered v_cmpx only through the shared compare path and never ran it directly. The EXEC readback shows that path is right too.

AI-assisted (Claude Code, Claude Opus 5.5).

**@⁠QemmHD** on GitHub · 2026-11-25 02:22 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1493#issuecomment-7150644383) Thanks @⁠ecox982745, that's a much stronger check than my CPU tests: 2,016 lane outputs on real hardware, an independent reference, and the unsigned controls unchanged. The `v_cmpx_eq_i64_e32` case is especially useful, because my tests covered `v_cmpx` only through the shared compare path and never ran it directly. The EXEC readback shows that path is right too. AI-assisted (Claude Code, Claude Opus 5.5).
Author
Owner

@⁠QemmHD on GitHub · 2026-11-25 02:28 UTC · original

@⁠aslan2142: this has independent hardware verification now (ecox982745's RTX 4090 run above: 444 wrong results out of 2,016 on the parent, 0 on this head, unsigned controls unchanged). It passes the full semantic suite on post-#984 main and merges cleanly, also alongside #978. Since it's my own PR, I'll leave the merge to you or another maintainer.

AI-assisted (Claude Code, Claude Opus 5.5).

**@⁠QemmHD** on GitHub · 2026-11-25 02:28 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1493#issuecomment-7150766089) @⁠aslan2142: this has independent hardware verification now (ecox982745's RTX 4090 run above: 444 wrong results out of 2,016 on the parent, 0 on this head, unsigned controls unchanged). It passes the full semantic suite on post-#984 main and merges cleanly, also alongside #978. Since it's my own PR, I'll leave the merge to you or another maintainer. AI-assisted (Claude Code, Claude Opus 5.5).
Author
Owner

@⁠tiguan26 on GitHub · 2026-11-25 03:34 UTC · original · approved these changes

Approving bc2d333. Merged into main bfef489cb: no conflicts. The new test_valu_signed_literal64.py passes, and the full translator suite gives 488 files / 6,287 tests with only test_int32_to_f64 failing, the same spirv-opt crash as on main here (Linux, Python 3.13, no GPU).

Read the change: valu_imm_i64 widens a printed 32-bit literal with bit 31 set to {-1, lo} for the signed operands only. For v_ashrrev_i64 that is a[2], the 64-bit value, not the shift amount. The b64 shifts and u64 operands keep zero-extension, and inline constants are already negative, so they sign-extend through the mask either way. That matches LLVM's AMDGPU operand rule for i64 vs u64/b64.

AI-assisted (Claude).

**@⁠tiguan26** on GitHub · 2026-11-25 03:34 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1493#pullrequestreview-6051471814) · **approved these changes** Approving bc2d333. Merged into main bfef489cb: no conflicts. The new `test_valu_signed_literal64.py` passes, and the full translator suite gives 488 files / 6,287 tests with only `test_int32_to_f64` failing, the same `spirv-opt` crash as on main here (Linux, Python 3.13, no GPU). Read the change: `valu_imm_i64` widens a printed 32-bit literal with bit 31 set to {-1, lo} for the signed operands only. For `v_ashrrev_i64` that is `a[2]`, the 64-bit value, not the shift amount. The b64 shifts and u64 operands keep zero-extension, and inline constants are already negative, so they sign-extend through the mask either way. That matches LLVM's AMDGPU operand rule for i64 vs u64/b64. AI-assisted (Claude).
Author
Owner

@⁠ytsodacan on GitHub · 2026-11-25 03:35 UTC · original · edited 2026-11-25 03:43 UTC

[KEEP]

  • The change looks correct and scoped: tools/rdna2spirv.py adds valu_imm_i64/src64_i64 and applies them only to signed v_cmp(x)_*_i64, v_ashrrev_i64 S1 and v_mad_i64_i32 S2; unsigned ops are unchanged and covered by controls in tests/semantic/test_valu_signed_literal64.py. docs/design.md gets the contract row.
  • Independent RTX 4090 verification is in the comments (444 wrong results on parent, 0 on this head).
  • CI is not finished: change-scope, linux-gpu-tools, macos, no-game-data and windows are all still pending, and the merge state is UNSTABLE. Re-triage once they are green.
  • Overlaps with #978 (scalar half, still open); the author notes both merge cleanly and that imm64_signed and valu_imm_i64 could be folded in a follow-up. A maintainer may want to pick merge order.
**@⁠ytsodacan** on GitHub · 2026-11-25 03:35 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1493#issuecomment-7152063723) · edited 2026-11-25 03:43 UTC **[KEEP]** - The change looks correct and scoped: `tools/rdna2spirv.py` adds `valu_imm_i64`/`src64_i64` and applies them only to signed `v_cmp(x)_*_i64`, `v_ashrrev_i64` S1 and `v_mad_i64_i32` S2; unsigned ops are unchanged and covered by controls in `tests/semantic/test_valu_signed_literal64.py`. `docs/design.md` gets the contract row. - Independent RTX 4090 verification is in the comments (444 wrong results on parent, 0 on this head). - CI is not finished: `change-scope`, `linux-gpu-tools`, `macos`, `no-game-data` and `windows` are all still pending, and the merge state is UNSTABLE. Re-triage once they are green. - Overlaps with #978 (scalar half, still open); the author notes both merge cleanly and that `imm64_signed` and `valu_imm_i64` could be folded in a follow-up. A maintainer may want to pick merge order.
Author
Owner

@⁠QemmHD on GitHub · 2026-11-25 10:01 UTC · original

Merging.

Why merged

  • Fixes a real translator bug: the 32-bit literal of signed 64-bit v_cmp(x)_*_i64 (and the other signed 64-bit VALU forms this PR covers) was zero-extended instead of sign-extended, so comparisons against negative literals gave wrong results.
  • Independent checks:
    • approved by tiguan26;
    • KEEP from ytsodacan's review;
    • independent hardware verification by ecox982745 on an RTX 4090: the parent gave 444 wrong results out of 2,016 lane outputs, this head gives 0, and the unsigned controls are unchanged.
  • Re-tested here merged onto current main (3de22a190) on an RTX 4070 SUPER:
    • the merge is clean;
    • tests/semantic VALU/VCMP files plus the PR's new test: 1,483 passed, 21,175 subtests passed, 0 failed;
    • the full semantic suite on 604ddff0b: 6,287 tests, 0 failed (main: 6,279);
    • the PR's new test fails 5 of 8 against main's translator, as a control;
    • the consistency set is the same as main, whose only failure is the pre-existing test_runtime_paths workflow check.

Why it's good

  • The change is small and scoped: valu_imm_i64/src64_i64 are applied only to the signed 64-bit forms, and the unsigned paths are untouched.
  • It comes with a regression test pinned to the exact encodings.
  • Wrong signed compares silently steer branches and masks in translated shaders, so this removes a class of hard-to-trace wrong results.

AI-assisted (Claude Code, Claude Opus 5.5).

**@⁠QemmHD** on GitHub · 2026-11-25 10:01 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1493#issuecomment-7159120792) Merging. **Why merged** - Fixes a real translator bug: the 32-bit literal of signed 64-bit `v_cmp(x)_*_i64` (and the other signed 64-bit VALU forms this PR covers) was zero-extended instead of sign-extended, so comparisons against negative literals gave wrong results. - Independent checks: - approved by tiguan26; - KEEP from ytsodacan's review; - independent hardware verification by ecox982745 on an RTX 4090: the parent gave 444 wrong results out of 2,016 lane outputs, this head gives 0, and the unsigned controls are unchanged. - Re-tested here merged onto current main (3de22a190) on an RTX 4070 SUPER: - the merge is clean; - `tests/semantic` VALU/VCMP files plus the PR's new test: 1,483 passed, 21,175 subtests passed, 0 failed; - the full semantic suite on 604ddff0b: 6,287 tests, 0 failed (main: 6,279); - the PR's new test fails 5 of 8 against main's translator, as a control; - the consistency set is the same as main, whose only failure is the pre-existing `test_runtime_paths` workflow check. **Why it's good** - The change is small and scoped: `valu_imm_i64`/`src64_i64` are applied only to the signed 64-bit forms, and the unsigned paths are untouched. - It comes with a regression test pinned to the exact encodings. - Wrong signed compares silently steer branches and masks in translated shaders, so this removes a class of hard-to-trace wrong results. AI-assisted (Claude Code, Claude Opus 5.5).
This discussion has been locked. Commenting is limited to contributors.
No description provided.