[PR #399] [CLOSED] rdna2spirv: support VOP3 clamp across the SAD/QSAD/MQSAD family with gfx1036 hardware evidence (SHADER-QSAD-CLAMP) #451

Closed
opened 2026-11-26 20:17:55 +01:00 by 4ld0 · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/ubiquitarianism/leonida/pull/399
Author: @tahffo68hk-hash
Created: 11/24/2026
Status: ❌ Closed

Base: main ← Head: fix/shader-qsad-clamp


📝 Commits (1)

  • d740bdf rdna2spirv: support VOP3 clamp saturation on v_qsad_pk_u16_u8, v_mqsad_pk_u16_u8, and v_mqsad_u32_u8 (SHADER-QSAD-CLAMP)

📊 Changes

4 files changed (+73 additions, -23 deletions)

View changed files

📝 docs/design.md (+1 -1)
📝 tests/semantic/test_alu_mqsad_u32.py (+28 -8)
📝 tests/semantic/test_alu_qsad.py (+17 -3)
📝 tools/rdna2spirv.py (+27 -11)

📄 Description

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

Problem

tools/rdna2spirv.py rejected v_qsad_pk_u16_u8, v_mqsad_pk_u16_u8, and v_mqsad_u32_u8 with Unsupported whenever VOP3 clamp was set.

On native AMD RDNA2 hardware (gfx1036), VOP3 clamp saturates each packed output lane independently:

  • v_qsad_pk_u16_u8 / v_mqsad_pk_u16_u8: each of the four 16-bit lanes saturates to [0, 0xFFFF].
  • v_mqsad_u32_u8: each of the four 32-bit lanes saturates to [0, 0xFFFFFFFF].

(Note: v_sad_* / v_msad_u8 are intentionally untouched so this PR does not overlap with #275.)

Fix

  • In tools/rdna2spirv.py:
    • For v_qsad_pk_u16_u8 and v_mqsad_pk_u16_u8 with clamp, saturate each 16-bit lane with UMin total 0xFFFF instead of masking & 0xFFFF.
    • For v_mqsad_u32_u8 with clamp, accumulate the four masked byte-diffs (at most 1020) and add to accs[k] via OpIAddCarry, selecting 0xFFFFFFFF on carry.
  • Update the clamp tests in tests/semantic/test_alu_qsad.py and tests/semantic/test_alu_mqsad_u32.py to verify saturating, boundary, and non-saturating lanes across all three opcodes.
  • Document the hardware-verified rule in docs/design.md.

Verification

  • CPU semantic tests:
    • python -I tests/semantic/test_alu_qsad.py -> 6/6 OK
    • python -I tests/semantic/test_alu_mqsad_u32.py -> 29/29 OK
  • Hardware differential verification (Native AMD RDNA2 vs. Translated Vulkan + spirv-val):
    • Native reference: AMD GFX1036 (RDNA2) via tools/hw_oracle -> 8 dwords across all three opcodes (v_qsad_pk_u16_u8, v_mqsad_pk_u16_u8, v_mqsad_u32_u8).
    • Translated execution: NVIDIA GeForce RTX 5060 Ti (driver 617.42, Windows 11) via vk_compute_test.exe:
      • Wave32: 8/8 exact dwords (spirv-val PASS)
      • Wave64 (--wave64-emulate): 8/8 exact dwords (spirv-val PASS)
  • No game files used (synthetic assembly only).
  • AI-assisted: Yes (Antigravity).
中文翻译

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

问题描述

tools/rdna2spirv.py 在 v_qsad_pk_u16_u8、v_mqsad_pk_u16_u8 和 v_mqsad_u32_u8 携带 VOP3 clamp 修饰符时直接抛出 Unsupported。

经原生 AMD RDNA2(gfx1036)硬件实测,VOP3 clamp 会对每个打包通道独立饱和:

  • v_qsad_pk_u16_u8 / v_mqsad_pk_u16_u8:4 个 16 位通道各自饱和至 [0, 0xFFFF]。
  • v_mqsad_u32_u8:4 个 32 位通道各自饱和至 [0, 0xFFFFFFFF]。

(注:未改动 v_sad_* / v_msad_u8,避免与 #275 冲突。)

修复方案

  • 在 tools/rdna2spirv.py 中对上述三条指令实现基于硬件实测的 clamp 饱和逻辑(16 位 UMin 0xFFFF 与 32 位 OpIAddCarry 饱和)。
  • 更新 tests/semantic/test_alu_qsad.py 与 tests/semantic/test_alu_mqsad_u32.py 中的 clamp 回归测试。
  • 在 docs/design.md 契约表中记录硬件实测结论。

验证结果

  • CPU 语义测试:test_alu_qsad.py(6/6)与 test_alu_mqsad_u32.py(29/29)全部通过。
  • 跨架构硬件差分验证(原生 AMD GFX1036 vs 翻译后 NVIDIA RTX 5060 Ti,驱动 617.42):Wave32 8/8 精确一致,Wave64 模拟(--wave64-emulate)8/8 精确一致,spirv-val 全部通过。
  • 仅使用手写合成汇编,无游戏文件。
  • AI 辅助:是(Antigravity)。

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/ubiquitarianism/leonida/pull/399 **Author:** [@tahffo68hk-hash](https://github.com/tahffo68hk-hash) **Created:** 11/24/2026 **Status:** ❌ Closed **Base:** `main` ← **Head:** `fix/shader-qsad-clamp` --- ### 📝 Commits (1) - [`d740bdf`](https://github.com/ubiquitarianism/leonida/commit/d740bdfebbd35bd31a6c086910b90c23f04476e0) rdna2spirv: support VOP3 clamp saturation on v_qsad_pk_u16_u8, v_mqsad_pk_u16_u8, and v_mqsad_u32_u8 (SHADER-QSAD-CLAMP) ### 📊 Changes **4 files changed** (+73 additions, -23 deletions) <details> <summary>View changed files</summary> 📝 `docs/design.md` (+1 -1) 📝 `tests/semantic/test_alu_mqsad_u32.py` (+28 -8) 📝 `tests/semantic/test_alu_qsad.py` (+17 -3) 📝 `tools/rdna2spirv.py` (+27 -11) </details> ### 📄 Description Claimed in the coordination issue as: `SHADER-QSAD-CLAMP` ## Problem `tools/rdna2spirv.py` rejected `v_qsad_pk_u16_u8`, `v_mqsad_pk_u16_u8`, and `v_mqsad_u32_u8` with `Unsupported` whenever VOP3 `clamp` was set. On native AMD RDNA2 hardware (`gfx1036`), VOP3 `clamp` saturates each packed output lane independently: - `v_qsad_pk_u16_u8` / `v_mqsad_pk_u16_u8`: each of the four 16-bit lanes saturates to `[0, 0xFFFF]`. - `v_mqsad_u32_u8`: each of the four 32-bit lanes saturates to `[0, 0xFFFFFFFF]`. (Note: `v_sad_*` / `v_msad_u8` are intentionally untouched so this PR does not overlap with #275.) ## Fix - In `tools/rdna2spirv.py`: - For `v_qsad_pk_u16_u8` and `v_mqsad_pk_u16_u8` with `clamp`, saturate each 16-bit lane with `UMin total 0xFFFF` instead of masking `& 0xFFFF`. - For `v_mqsad_u32_u8` with `clamp`, accumulate the four masked byte-diffs (at most `1020`) and add to `accs[k]` via `OpIAddCarry`, selecting `0xFFFFFFFF` on carry. - Update the `clamp` tests in `tests/semantic/test_alu_qsad.py` and `tests/semantic/test_alu_mqsad_u32.py` to verify saturating, boundary, and non-saturating lanes across all three opcodes. - Document the hardware-verified rule in `docs/design.md`. ## Verification - **CPU semantic tests**: - `python -I tests/semantic/test_alu_qsad.py` -> 6/6 OK - `python -I tests/semantic/test_alu_mqsad_u32.py` -> 29/29 OK - **Hardware differential verification (Native AMD RDNA2 vs. Translated Vulkan + `spirv-val`)**: - Native reference: AMD GFX1036 (RDNA2) via `tools/hw_oracle` -> 8 dwords across all three opcodes (`v_qsad_pk_u16_u8`, `v_mqsad_pk_u16_u8`, `v_mqsad_u32_u8`). - Translated execution: NVIDIA GeForce RTX 5060 Ti (driver 617.42, Windows 11) via `vk_compute_test.exe`: - Wave32: **8/8 exact dwords** (`spirv-val` PASS) - Wave64 (`--wave64-emulate`): **8/8 exact dwords** (`spirv-val` PASS) - No game files used (synthetic assembly only). - AI-assisted: Yes (Antigravity). <details> <summary>中文翻译</summary> 在协调帖 #14 中认领的 ID:`SHADER-QSAD-CLAMP` ## 问题描述 `tools/rdna2spirv.py` 在 `v_qsad_pk_u16_u8`、`v_mqsad_pk_u16_u8` 和 `v_mqsad_u32_u8` 携带 VOP3 `clamp` 修饰符时直接抛出 `Unsupported`。 经原生 AMD RDNA2(`gfx1036`)硬件实测,VOP3 `clamp` 会对每个打包通道独立饱和: - `v_qsad_pk_u16_u8` / `v_mqsad_pk_u16_u8`:4 个 16 位通道各自饱和至 `[0, 0xFFFF]`。 - `v_mqsad_u32_u8`:4 个 32 位通道各自饱和至 `[0, 0xFFFFFFFF]`。 (注:未改动 `v_sad_*` / `v_msad_u8`,避免与 #275 冲突。) ## 修复方案 - 在 `tools/rdna2spirv.py` 中对上述三条指令实现基于硬件实测的 `clamp` 饱和逻辑(16 位 `UMin 0xFFFF` 与 32 位 `OpIAddCarry` 饱和)。 - 更新 `tests/semantic/test_alu_qsad.py` 与 `tests/semantic/test_alu_mqsad_u32.py` 中的 `clamp` 回归测试。 - 在 `docs/design.md` 契约表中记录硬件实测结论。 ## 验证结果 - CPU 语义测试:`test_alu_qsad.py`(6/6)与 `test_alu_mqsad_u32.py`(29/29)全部通过。 - 跨架构硬件差分验证(原生 AMD GFX1036 vs 翻译后 NVIDIA RTX 5060 Ti,驱动 617.42):Wave32 **8/8 精确一致**,Wave64 模拟(`--wave64-emulate`)**8/8 精确一致**,`spirv-val` 全部通过。 - 仅使用手写合成汇编,无游戏文件。 - AI 辅助:是(Antigravity)。 </details> --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
4ld0 2026-11-26 20:17:55 +01:00
Sign in to join this conversation.
No labels
bug
pull-request
No milestone
No project
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.

Dependencies

No dependencies set

Reference
starred/leonida#451
No description provided.