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

Merged
QemmHD merged 1 commit from upstream-pr/1493 into main 2026-11-25 10:01:35 +00:00
QemmHD commented 2026-11-25 01:53:15 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by QemmHD on 2026-11-25T01:53:15Z.


Claimed in the coordination issue as: SHADER-VALU-LITERAL-SEXT (https://github.com/ubiquitarianism/leonida/issues/14#issuecomment-7149621293)

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

<!-- greyfinch-sync:ubiquitarianism/leonida:issue:5483597127 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1493). Originally posted by [QemmHD](https://github.com/QemmHD) on 2026-11-25T01:53:15Z. --- Claimed in the coordination issue as: SHADER-VALU-LITERAL-SEXT (https://github.com/ubiquitarianism/leonida/issues/14#issuecomment-7149621293) 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
ecox982745 commented 2026-11-25 02:15:40 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by ecox982745 on 2026-11-25T02:15:40Z.


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.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7150504545 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/1493#issuecomment-7150504545). Originally posted by [ecox982745](https://github.com/ecox982745) on 2026-11-25T02:15:40Z. --- 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.
QemmHD commented 2026-11-25 02:22:40 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by QemmHD on 2026-11-25T02:22:40Z.


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).

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7150644383 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/1493#issuecomment-7150644383). Originally posted by [QemmHD](https://github.com/QemmHD) on 2026-11-25T02:22:40Z. --- 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 commented 2026-11-25 02:28:19 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by QemmHD on 2026-11-25T02:28:19Z.


@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).

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7150766089 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/1493#issuecomment-7150766089). Originally posted by [QemmHD](https://github.com/QemmHD) on 2026-11-25T02:28:19Z. --- @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).
tiguan26 commented 2026-11-25 03:34:44 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by tiguan26 on 2026-11-25T03:34:44Z.

Review: APPROVED


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).

<!-- greyfinch-sync:ubiquitarianism/leonida:review:6051471814 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1493#pullrequestreview-6051471814). Originally posted by [tiguan26](https://github.com/tiguan26) on 2026-11-25T03:34:44Z. **Review:** APPROVED --- 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).
ytsodacan commented 2026-11-25 03:35:56 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by ytsodacan on 2026-11-25T03:35:56Z.


[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.
<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7152063723 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/1493#issuecomment-7152063723). Originally posted by [ytsodacan](https://github.com/ytsodacan) on 2026-11-25T03:35:56Z. --- **[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.
QemmHD commented 2026-11-25 10:01:32 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by QemmHD on 2026-11-25T10:01:32Z.


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).

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7159120792 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/1493#issuecomment-7159120792). Originally posted by [QemmHD](https://github.com/QemmHD) on 2026-11-25T10:01:32Z. --- 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).
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
ProjectLeonida/mandy!1493
No description provided.