[PR #392] rdna2spirv: saturate v_mul/v_mad_{i32_i24,u32_u24} under VOP3 clamp (SHADER-MUL24-CLAMP) #444

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

📋 Pull Request Information

Original PR: https://github.com/ubiquitarianism/leonida/pull/392
Author: @tahffo68hk-hash
Created: 11/24/2026
Status: 🔄 Open

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


📝 Commits (1)

  • b7498f3 rdna2spirv: saturate v_mul/v_mad_{i32_i24,u32_u24} under VOP3 clamp (SHADER-MUL24-CLAMP)

📊 Changes

3 files changed (+283 additions, -39 deletions)

View changed files

📝 docs/design.md (+1 -1)
➕ tests/semantic/test_alu_mul24_clamp.py (+212 -0)
📝 tools/rdna2spirv.py (+70 -38)

📄 Description

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

Problem

tools/rdna2spirv.py silently ignored the VOP3 clamp modifier on all four 24-bit integer multiply and multiply-add instructions:

  • v_mul_i32_i24
  • v_mul_u32_u24
  • v_mad_i32_i24
  • v_mad_u32_u24

On native AMD RDNA2 hardware (gfx1036), VOP3 clamp saturates the exact widened 64-bit product (plus the 32-bit addend for mad, including cases where a >32-bit product cancels back into signed 32-bit range) to [0x80000000, 0x7FFFFFFF] for i24 or [0, 0xFFFFFFFF] for u24. The translator previously emitted wrapping 32-bit OpIMul / OpIAdd regardless of clamp.

Fix

  • Unify v_mul_i32_i24, v_mul_u32_u24, v_mad_i32_i24, and v_mad_u32_u24 in tools/rdna2spirv.py and add Gen.clamp_mul24() using OpSMulExtended/OpUMulExtended + 64-bit add with OpIAddCarry (and sign extension of acc for signed mad) to check 32-bit range fit and saturate accurately.
  • Preserve existing wrapping behavior when clamp is absent.
  • Add tests/semantic/test_alu_mul24_clamp.py with directed overflow, upper-8-bit masking, signed cancellation-back-into-range, randomized matrix, and spirv-val checks.
  • Document the hardware-confirmed contract in docs/design.md (Integer clamp row).

Verification

  • CPU semantic + spirv-val:
    • python -I tests/semantic/test_alu_mul24_clamp.py -> 6/6 OK (including spirv-val --target-env vulkan1.2)
    • python -I tests/semantic/test_alu_mul.py -> OK
    • python -I tests/semantic/test_alu_mul_hi_24.py -> OK
  • Hardware differential verification (Native AMD RDNA2 vs. Translated Vulkan):
    • Native reference: AMD GFX1036 (RDNA2) via tools/hw_oracle -> ['0x7fffffff', '0x80000000', '0xffffffff', '0xffff0000', '0x7ffffffe', '0x80000001', '0x80000000', '0xffffffff']
    • Translated execution: NVIDIA GeForce RTX 5060 Ti (driver 617.42, Windows 11) via vk_compute_test.exe:
      • Wave32: 8/8 exact dwords
      • Wave64 (--wave64-emulate): 8/8 exact dwords
  • No game files used (synthetic assembly only).
  • AI-assisted: Yes (Antigravity).
中文翻译

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

问题描述

tools/rdna2spirv.py 在以下四条 24 位整数乘法与乘加指令上静默忽略了 VOP3 clamp 修饰符:

  • v_mul_i32_i24
  • v_mul_u32_u24
  • v_mad_i32_i24
  • v_mad_u32_u24

在原生 AMD RDNA2(gfx1036)硬件上,VOP3 clamp 会基于精确的 64 位宽乘积(mad 加上 32 位加数,包含超过 32 位的乘积与有符号加数抵消后回到 int32 范围内的边界情况)执行饱和:有符号 i24 饱和到 [0x80000000, 0x7FFFFFFF],无符号 u24 饱和到 [0, 0xFFFFFFFF]。翻译器此前无视 clamp 直接回绕。

修复方案

  • 在 tools/rdna2spirv.py 中统一处理上述四条指令,并新增 Gen.clamp_mul24():通过 OpSMulExtended/OpUMulExtended 与 OpIAddCarry 计算精确 64 位结果并判断是否落在 32 位范围内,超出时执行饱和;未带 clamp 时保持原有回绕语义。
  • 新增 tests/semantic/test_alu_mul24_clamp.py,覆盖溢出饱和、高 8 位屏蔽、48 位乘积抵消回 int32 范围、随机矩阵及 spirv-val 校验。
  • 在 docs/design.md 的 Integer clamp 契约表中补充硬件实测结论。

验证结果

  • CPU 语义测试与 spirv-val:test_alu_mul24_clamp.py、test_alu_mul.py、test_alu_mul_hi_24.py 全部通过。
  • 跨架构硬件差分验证:
    • 原生 AMD GFX1036(RDNA2):8 个定向用例输出 ['0x7fffffff', '0x80000000', '0xffffffff', '0xffff0000', '0x7ffffffe', '0x80000001', '0x80000000', '0xffffffff']
    • 翻译后 NVIDIA GeForce RTX 5060 Ti(驱动 617.42,Windows 11):Wave32 8/8 精确一致,Wave64 模拟(--wave64-emulate)8/8 精确一致。
  • 仅使用手写合成汇编,无游戏文件。
  • 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/392 **Author:** [@tahffo68hk-hash](https://github.com/tahffo68hk-hash) **Created:** 11/24/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `fix/shader-mul24-clamp` --- ### 📝 Commits (1) - [`b7498f3`](https://github.com/ubiquitarianism/leonida/commit/b7498f3c1ef2d3ac9eef8f8c94540e05e815ab9e) rdna2spirv: saturate v_mul/v_mad_{i32_i24,u32_u24} under VOP3 clamp (SHADER-MUL24-CLAMP) ### 📊 Changes **3 files changed** (+283 additions, -39 deletions) <details> <summary>View changed files</summary> 📝 `docs/design.md` (+1 -1) ➕ `tests/semantic/test_alu_mul24_clamp.py` (+212 -0) 📝 `tools/rdna2spirv.py` (+70 -38) </details> ### 📄 Description Claimed in the coordination issue as: `SHADER-MUL24-CLAMP` ## Problem `tools/rdna2spirv.py` silently ignored the VOP3 `clamp` modifier on all four 24-bit integer multiply and multiply-add instructions: - `v_mul_i32_i24` - `v_mul_u32_u24` - `v_mad_i32_i24` - `v_mad_u32_u24` On native AMD RDNA2 hardware (`gfx1036`), VOP3 `clamp` saturates the exact widened 64-bit product (plus the 32-bit addend for `mad`, including cases where a >32-bit product cancels back into signed 32-bit range) to `[0x80000000, 0x7FFFFFFF]` for `i24` or `[0, 0xFFFFFFFF]` for `u24`. The translator previously emitted wrapping 32-bit `OpIMul` / `OpIAdd` regardless of `clamp`. ## Fix - Unify `v_mul_i32_i24`, `v_mul_u32_u24`, `v_mad_i32_i24`, and `v_mad_u32_u24` in `tools/rdna2spirv.py` and add `Gen.clamp_mul24()` using `OpSMulExtended`/`OpUMulExtended` + 64-bit add with `OpIAddCarry` (and sign extension of `acc` for signed `mad`) to check 32-bit range fit and saturate accurately. - Preserve existing wrapping behavior when `clamp` is absent. - Add `tests/semantic/test_alu_mul24_clamp.py` with directed overflow, upper-8-bit masking, signed cancellation-back-into-range, randomized matrix, and `spirv-val` checks. - Document the hardware-confirmed contract in `docs/design.md` (`Integer clamp` row). ## Verification - **CPU semantic + `spirv-val`**: - `python -I tests/semantic/test_alu_mul24_clamp.py` -> 6/6 OK (including `spirv-val --target-env vulkan1.2`) - `python -I tests/semantic/test_alu_mul.py` -> OK - `python -I tests/semantic/test_alu_mul_hi_24.py` -> OK - **Hardware differential verification (Native AMD RDNA2 vs. Translated Vulkan)**: - Native reference: AMD GFX1036 (RDNA2) via `tools/hw_oracle` -> `['0x7fffffff', '0x80000000', '0xffffffff', '0xffff0000', '0x7ffffffe', '0x80000001', '0x80000000', '0xffffffff']` - Translated execution: NVIDIA GeForce RTX 5060 Ti (driver 617.42, Windows 11) via `vk_compute_test.exe`: - Wave32: **8/8 exact dwords** - Wave64 (`--wave64-emulate`): **8/8 exact dwords** - No game files used (synthetic assembly only). - AI-assisted: Yes (Antigravity). <details> <summary>中文翻译</summary> 在协调帖 #14 中认领的 ID:`SHADER-MUL24-CLAMP` ## 问题描述 `tools/rdna2spirv.py` 在以下四条 24 位整数乘法与乘加指令上静默忽略了 VOP3 `clamp` 修饰符: - `v_mul_i32_i24` - `v_mul_u32_u24` - `v_mad_i32_i24` - `v_mad_u32_u24` 在原生 AMD RDNA2(`gfx1036`)硬件上,VOP3 `clamp` 会基于精确的 64 位宽乘积(`mad` 加上 32 位加数,包含超过 32 位的乘积与有符号加数抵消后回到 int32 范围内的边界情况)执行饱和:有符号 `i24` 饱和到 `[0x80000000, 0x7FFFFFFF]`,无符号 `u24` 饱和到 `[0, 0xFFFFFFFF]`。翻译器此前无视 `clamp` 直接回绕。 ## 修复方案 - 在 `tools/rdna2spirv.py` 中统一处理上述四条指令,并新增 `Gen.clamp_mul24()`:通过 `OpSMulExtended`/`OpUMulExtended` 与 `OpIAddCarry` 计算精确 64 位结果并判断是否落在 32 位范围内,超出时执行饱和;未带 `clamp` 时保持原有回绕语义。 - 新增 `tests/semantic/test_alu_mul24_clamp.py`,覆盖溢出饱和、高 8 位屏蔽、48 位乘积抵消回 int32 范围、随机矩阵及 `spirv-val` 校验。 - 在 `docs/design.md` 的 `Integer clamp` 契约表中补充硬件实测结论。 ## 验证结果 - CPU 语义测试与 `spirv-val`:`test_alu_mul24_clamp.py`、`test_alu_mul.py`、`test_alu_mul_hi_24.py` 全部通过。 - 跨架构硬件差分验证: - 原生 AMD GFX1036(RDNA2):8 个定向用例输出 `['0x7fffffff', '0x80000000', '0xffffffff', '0xffff0000', '0x7ffffffe', '0x80000001', '0x80000000', '0xffffffff']` - 翻译后 NVIDIA GeForce RTX 5060 Ti(驱动 617.42,Windows 11):Wave32 **8/8 精确一致**,Wave64 模拟(`--wave64-emulate`)**8/8 精确一致**。 - 仅使用手写合成汇编,无游戏文件。 - AI 辅助:是(Antigravity)。 </details> --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
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#444
No description provided.