rdna2spirv: support VOP3 clamp on v_mad_u64_u32 and v_mad_i64_i32 (SHADER-MAD64-CLAMP) #398

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

@⁠tahffo68hk-hash on GitHub · 2026-11-24 04:35 UTC · original
tahffo68hk-hash:fix/shader-mad64-clamp → main · +64 −5 · 3 files · 1 commits · open

Claimed in the coordination issue as: SHADER-MAD64-CLAMP

Problem

tools/rdna2spirv.py rejected v_mad_u64_u32 and v_mad_i64_i32 with Unsupported whenever VOP3 clamp was set.

On native AMD RDNA2 hardware (gfx1036), VOP3 clamp saturates the 64-bit destination pair v[d:d+1] to 0xFFFFFFFFFFFFFFFF (for v_mad_u64_u32) or [0x8000000000000000, 0x7FFFFFFFFFFFFFFF] (for v_mad_i64_i32), while sdst continues to report the exact bit 64 (unsigned carry out / exact 65-bit sign).

Fix

  • Allow clamp in v_mad_u64_u32 / v_mad_i64_i32 in tools/rdna2spirv.py:
    • Unsigned: saturate (r_lo, r_hi) to (0xFFFFFFFF, 0xFFFFFFFF) when bit64 != 0.
    • Signed: detect 64-bit signed overflow via bit64 != sr (exact 65-bit sign differs from wrapped sign) and saturate to INT64_MIN / INT64_MAX accordingly.
    • Keep sdst reporting the unmodified bit64 lane mask.
  • Add Mad64Tests.test_clamp_saturation_preserves_sdst in tests/semantic/test_alu_mad64.py (directed + randomized cases).
  • Document the hardware-verified rule in docs/design.md.

Verification

  • CPU semantic tests: python -I tests/semantic/test_alu_mad64.py -> all tests OK.
  • Hardware differential verification (Native AMD RDNA2 vs. Translated Vulkan + spirv-val):
    • Native reference: AMD GFX1036 (RDNA2) via tools/hw_oracle -> 12 dwords across u64 overflow/exact-max and i64 positive/negative overflow + sdst bits.
    • Translated execution: NVIDIA GeForce RTX 5060 Ti (driver 617.42, Windows 11) via vk_compute_test.exe:
      • Wave32: 12/12 exact dwords (spirv-val PASS)
      • Wave64 (--wave64-emulate): 12/12 exact dwords (spirv-val PASS)
  • No game files used (synthetic assembly only).
  • AI-assisted: Yes (Antigravity).
中文翻译

在协调帖 #14 中认领的 ID:SHADER-MAD64-CLAMP

问题描述

tools/rdna2spirv.py 在 v_mad_u64_u32 与 v_mad_i64_i32 携带 VOP3 clamp 修饰符时直接抛出 Unsupported。

经原生 AMD RDNA2(gfx1036)硬件实测,VOP3 clamp 会将 64 位目标寄存器对 v[d:d+1] 饱和至 0xFFFFFFFFFFFFFFFF(无符号)或 [0x8000000000000000, 0x7FFFFFFFFFFFFFFF](有符号),同时 sdst 仍保持报告真实的第 64 位(无符号进位 / 精确 65 位符号位)。

修复方案

  • 在 tools/rdna2spirv.py 中支持 v_mad_u64_u32 与 v_mad_i64_i32 的 clamp 饱和,同时保留 sdst 原始掩码输出。
  • 在 tests/semantic/test_alu_mad64.py 中新增定向及随机回归测试(test_clamp_saturation_preserves_sdst)。
  • 在 docs/design.md 契约表中记录硬件实测结论。

验证结果

  • CPU 语义测试:tests/semantic/test_alu_mad64.py 全部通过。
  • 跨架构硬件差分验证(原生 AMD GFX1036 vs 翻译后 NVIDIA RTX 5060 Ti,驱动 617.42):Wave32 12/12 精确一致,Wave64 模拟(--wave64-emulate)12/12 精确一致,spirv-val 全部通过。
  • 仅使用手写合成汇编,无游戏文件。
  • AI 辅助:是(Antigravity)。
**@⁠tahffo68hk-hash** on GitHub · 2026-11-24 04:35 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/398) `tahffo68hk-hash:fix/shader-mad64-clamp` → `main` · +64 −5 · 3 files · 1 commits · open Claimed in the coordination issue as: `SHADER-MAD64-CLAMP` ## Problem `tools/rdna2spirv.py` rejected `v_mad_u64_u32` and `v_mad_i64_i32` with `Unsupported` whenever VOP3 `clamp` was set. On native AMD RDNA2 hardware (`gfx1036`), VOP3 `clamp` saturates the 64-bit destination pair `v[d:d+1]` to `0xFFFFFFFFFFFFFFFF` (for `v_mad_u64_u32`) or `[0x8000000000000000, 0x7FFFFFFFFFFFFFFF]` (for `v_mad_i64_i32`), while `sdst` continues to report the exact bit 64 (unsigned carry out / exact 65-bit sign). ## Fix - Allow `clamp` in `v_mad_u64_u32` / `v_mad_i64_i32` in `tools/rdna2spirv.py`: - Unsigned: saturate `(r_lo, r_hi)` to `(0xFFFFFFFF, 0xFFFFFFFF)` when `bit64 != 0`. - Signed: detect 64-bit signed overflow via `bit64 != sr` (exact 65-bit sign differs from wrapped sign) and saturate to `INT64_MIN` / `INT64_MAX` accordingly. - Keep `sdst` reporting the unmodified `bit64` lane mask. - Add `Mad64Tests.test_clamp_saturation_preserves_sdst` in `tests/semantic/test_alu_mad64.py` (directed + randomized cases). - Document the hardware-verified rule in `docs/design.md`. ## Verification - **CPU semantic tests**: `python -I tests/semantic/test_alu_mad64.py` -> all tests OK. - **Hardware differential verification (Native AMD RDNA2 vs. Translated Vulkan + `spirv-val`)**: - Native reference: AMD GFX1036 (RDNA2) via `tools/hw_oracle` -> 12 dwords across u64 overflow/exact-max and i64 positive/negative overflow + `sdst` bits. - Translated execution: NVIDIA GeForce RTX 5060 Ti (driver 617.42, Windows 11) via `vk_compute_test.exe`: - Wave32: **12/12 exact dwords** (`spirv-val` PASS) - Wave64 (`--wave64-emulate`): **12/12 exact dwords** (`spirv-val` PASS) - No game files used (synthetic assembly only). - AI-assisted: Yes (Antigravity). <details> <summary>中文翻译</summary> 在协调帖 #14 中认领的 ID:`SHADER-MAD64-CLAMP` ## 问题描述 `tools/rdna2spirv.py` 在 `v_mad_u64_u32` 与 `v_mad_i64_i32` 携带 VOP3 `clamp` 修饰符时直接抛出 `Unsupported`。 经原生 AMD RDNA2(`gfx1036`)硬件实测,VOP3 `clamp` 会将 64 位目标寄存器对 `v[d:d+1]` 饱和至 `0xFFFFFFFFFFFFFFFF`(无符号)或 `[0x8000000000000000, 0x7FFFFFFFFFFFFFFF]`(有符号),同时 `sdst` 仍保持报告真实的第 64 位(无符号进位 / 精确 65 位符号位)。 ## 修复方案 - 在 `tools/rdna2spirv.py` 中支持 `v_mad_u64_u32` 与 `v_mad_i64_i32` 的 `clamp` 饱和,同时保留 `sdst` 原始掩码输出。 - 在 `tests/semantic/test_alu_mad64.py` 中新增定向及随机回归测试(`test_clamp_saturation_preserves_sdst`)。 - 在 `docs/design.md` 契约表中记录硬件实测结论。 ## 验证结果 - CPU 语义测试:`tests/semantic/test_alu_mad64.py` 全部通过。 - 跨架构硬件差分验证(原生 AMD GFX1036 vs 翻译后 NVIDIA RTX 5060 Ti,驱动 617.42):Wave32 **12/12 精确一致**,Wave64 模拟(`--wave64-emulate`)**12/12 精确一致**,`spirv-val` 全部通过。 - 仅使用手写合成汇编,无游戏文件。 - AI 辅助:是(Antigravity)。 </details>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 08:57:49 +00:00
Author
Owner

@⁠ubiquitarianism on GitHub · 2026-11-24 05:08 UTC · original

The code looks correct, but the contract table would contradict itself. docs/design.md:349 still says clamp is rejected for v_mad_u64_u32/v_mad_i64_i32, and this PR only appends to the 'Integer clamp' row. Please change that row to the new rule, and commit a reproducible gfx103x case (int_hw_probe.py or the hw_oracle asm with raw output). The measured words are only in the PR text.

**@⁠ubiquitarianism** on GitHub · 2026-11-24 05:08 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/398#issuecomment-7122670116) The code looks correct, but the contract table would contradict itself. `docs/design.md:349` still says `clamp` is rejected for `v_mad_u64_u32`/`v_mad_i64_i32`, and this PR only appends to the 'Integer clamp' row. Please change that row to the new rule, and commit a reproducible gfx103x case (`int_hw_probe.py` or the `hw_oracle` asm with raw output). The measured words are only in the PR text.
This pull request has changes conflicting with the target branch.
  • docs/design.md
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/398/head:github-bot/gh-398'
git switch 'github-bot/gh-398'

Merge

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