mirror of
https://github.com/ubiquitarianism/leonida.git
synced 2026-11-26 23:05:53 +01:00
[PR #392] rdna2spirv: saturate v_mul/v_mad_{i32_i24,u32_u24} under VOP3 clamp (SHADER-MUL24-CLAMP) #444
Labels
No labels
bug
pull-request
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
starred/leonida#444
Loading…
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
📋 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)
b7498f3rdna2spirv: 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-CLAMPProblem
tools/rdna2spirv.pysilently ignored the VOP3clampmodifier on all four 24-bit integer multiply and multiply-add instructions:v_mul_i32_i24v_mul_u32_u24v_mad_i32_i24v_mad_u32_u24On native AMD RDNA2 hardware (
gfx1036), VOP3clampsaturates the exact widened 64-bit product (plus the 32-bit addend formad, including cases where a >32-bit product cancels back into signed 32-bit range) to[0x80000000, 0x7FFFFFFF]fori24or[0, 0xFFFFFFFF]foru24. The translator previously emitted wrapping 32-bitOpIMul/OpIAddregardless ofclamp.Fix
v_mul_i32_i24,v_mul_u32_u24,v_mad_i32_i24, andv_mad_u32_u24intools/rdna2spirv.pyand addGen.clamp_mul24()usingOpSMulExtended/OpUMulExtended+ 64-bit add withOpIAddCarry(and sign extension ofaccfor signedmad) to check 32-bit range fit and saturate accurately.clampis absent.tests/semantic/test_alu_mul24_clamp.pywith directed overflow, upper-8-bit masking, signed cancellation-back-into-range, randomized matrix, andspirv-valchecks.docs/design.md(Integer clamprow).Verification
spirv-val:python -I tests/semantic/test_alu_mul24_clamp.py-> 6/6 OK (includingspirv-val --target-env vulkan1.2)python -I tests/semantic/test_alu_mul.py-> OKpython -I tests/semantic/test_alu_mul_hi_24.py-> OKtools/hw_oracle->['0x7fffffff', '0x80000000', '0xffffffff', '0xffff0000', '0x7ffffffe', '0x80000001', '0x80000000', '0xffffffff']vk_compute_test.exe:--wave64-emulate): 8/8 exact dwords中文翻译
在协调帖 #14 中认领的 ID:
SHADER-MUL24-CLAMP问题描述
tools/rdna2spirv.py在以下四条 24 位整数乘法与乘加指令上静默忽略了 VOP3clamp修饰符:v_mul_i32_i24v_mul_u32_u24v_mad_i32_i24v_mad_u32_u24在原生 AMD RDNA2(
gfx1036)硬件上,VOP3clamp会基于精确的 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契约表中补充硬件实测结论。验证结果
spirv-val:test_alu_mul24_clamp.py、test_alu_mul.py、test_alu_mul_hi_24.py全部通过。['0x7fffffff', '0x80000000', '0xffffffff', '0xffff0000', '0x7ffffffe', '0x80000001', '0x80000000', '0xffffffff']--wave64-emulate)8/8 精确一致。🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.