From ba94004fbe9d77025dabca22d6a4e99a88f6f1b0 Mon Sep 17 00:00:00 2001 From: tedin7 Date: Tue, 24 Nov 2026 23:03:42 +0100 Subject: [PATCH 1/2] feat: lower v_mul_hi_u32 in gfx2hlsl (#1792) --- CMakeLists.txt | 3 + docs/experiments.md | 7 ++ tests/startup_gfx2hlsl_mul_hi_u32_check.py | 93 ++++++++++++++++++++++ tools/startup/gfx2hlsl.py | 7 +- 4 files changed, 109 insertions(+), 1 deletion(-) create mode 100644 tests/startup_gfx2hlsl_mul_hi_u32_check.py diff --git a/CMakeLists.txt b/CMakeLists.txt index ea4fa9faae..d26aea029e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2047,6 +2047,9 @@ if(EXISTS "${INTAKE_PYTHON}") # The 64-bit vector shifts (#1815). add_test(NAME startup_gfx2hlsl_b64_shift COMMAND "${INTAKE_PYTHON}" -B "${CMAKE_CURRENT_SOURCE_DIR}/tests/startup_gfx2hlsl_b64_shift_check.py") + # The high half of an unsigned 32x32 product (#1792). + add_test(NAME startup_gfx2hlsl_mul_hi_u32 COMMAND "${INTAKE_PYTHON}" -B + "${CMAKE_CURRENT_SOURCE_DIR}/tests/startup_gfx2hlsl_mul_hi_u32_check.py") # SDWA forms of the float-result conversions (#1727). add_test(NAME startup_gfx2hlsl_sdwa_cvt COMMAND "${INTAKE_PYTHON}" -B "${CMAKE_CURRENT_SOURCE_DIR}/tests/startup_gfx2hlsl_sdwa_cvt_check.py") diff --git a/docs/experiments.md b/docs/experiments.md index 4b712e4310..1c510e28c4 100644 --- a/docs/experiments.md +++ b/docs/experiments.md @@ -6073,6 +6073,13 @@ the handler writes back in one statement. VGPR pairs, SGPR pairs and the integer sources; the low word is always shifted unsigned and only the arithmetic form signs the high word. Run `ctest --preset dev --no-tests=error -R '^startup_gfx2hlsl_b64_shift$' -V`. +**The high half of an unsigned product.** `v_mul_hi_u32` had no lowering - the census form +`v_mul_hi_u32 v1, v0, 0xaaaaaaab` was one of the refused shaders in the loading-screen cache (#870, issue #1792). +These stages have no 64-bit multiply and a plain `*` yields the low half, so the handler calls a preamble helper +built from halfword products. The check builds synthetic VOP3A words, decodes and translates them, and fails +without the helper. `v_mul_hi_i32` and the 24-bit forms are not part of this change. +Run `ctest --preset dev --no-tests=error -R '^startup_gfx2hlsl_mul_hi_u32$' -V`. + **The 32-bit scalar complement logic operations.** The scalar table had `s_nand_b64`, `s_nor_b64` and `s_xnor_b64` and the 32-bit `s_andn2_b32` and `s_orn2_b32`, but not `s_nand_b32`, `s_nor_b32` or `s_xnor_b32` (#870, issue #1919). They now lower as `~(S0 & S1)`, `~(S0 | S1)` and `~(S0 ^ S1)` on 32 bits, the text diff --git a/tests/startup_gfx2hlsl_mul_hi_u32_check.py b/tests/startup_gfx2hlsl_mul_hi_u32_check.py new file mode 100644 index 0000000000..82166be29f --- /dev/null +++ b/tests/startup_gfx2hlsl_mul_hi_u32_check.py @@ -0,0 +1,93 @@ +"""The v_mul_hi_u32 lowering in tools/startup/gfx2hlsl.py (issue #1792). + +usage: startup_gfx2hlsl_mul_hi_u32_check.py + +`v_mul_hi_u32` (the high 32 bits of an unsigned 32x32 product) had no lowering, so the census form +`v_mul_hi_u32 v1, v0, 0xaaaaaaab` was refused (#870). A plain `*` would be silently wrong: it yields the low half. +The lowering calls a preamble helper built from halfword products, because these stages have no 64-bit multiply. + +The instruction bytes are **constructed here** from the field layout the checked-in decoder reads +(`tools/disasm_rdna2.py`, VOP3A branch, opcode 0x16a): prefix `0b110101`, opcode in `w0[25:16]`, the +destination in `w0[7:0]`, src0 in `w1[8:0]`, src1 in `w1[17:9]`, and a literal dword after the pair. +""" +import os +import struct +import sys + +here = os.path.dirname(os.path.abspath(__file__)) +sys.path.insert(0, os.path.join(here, '..', 'tools')) +sys.path.insert(0, os.path.join(here, '..', 'tools', 'startup')) +import gfx2hlsl as G # noqa: E402 + +failures = [] + + +def check(label, condition, detail=''): + print(f"{label}: {'ok' if condition else 'MISMATCH'}") + if not condition: + failures.append(label) + if detail: + print(' ' + detail) + + +VOP3A_MUL_HI_U32 = 0x16A +VOP3A_MUL_LO_U32 = 0x169 +LITERAL = 255 +V0, V1, V2 = 0x100, 0x101, 0x102 + + +def encode_vop3a(op, dst, src0, src1, literal=None): + w0 = (0b110101 << 26) | ((op & 0x3FF) << 16) | (dst & 0xFF) + w1 = (src0 & 0x1FF) | ((src1 & 0x1FF) << 9) + code = struct.pack('> 16' in common.split('mul_hi_u32(uint a')[1].split('}')[0]) + +if failures: + print('FAIL') + for text in failures: + print(' ' + text) + sys.exit(1) +print('gfx2hlsl mul_hi_u32: all checks passed') diff --git a/tools/startup/gfx2hlsl.py b/tools/startup/gfx2hlsl.py index 1a7bf631f6..b59928ef60 100644 --- a/tools/startup/gfx2hlsl.py +++ b/tools/startup/gfx2hlsl.py @@ -1594,7 +1594,7 @@ def declarations(self): # operands are uints; the result is a uint expression VEC_U = { 'v_add_nc_u32': (2, '{0} + {1}'), 'v_sub_nc_u32': (2, '{0} - {1}'), 'v_subrev_nc_u32': (2, '{1} - {0}'), - 'v_mul_lo_u32': (2, '{0} * {1}'), 'v_and_b32': (2, '{0} & {1}'), 'v_or_b32': (2, '{0} | {1}'), 'v_xor_b32': (2, '{0} ^ {1}'), + 'v_mul_lo_u32': (2, '{0} * {1}'), 'v_mul_hi_u32': (2, 'mul_hi_u32({0}, {1})'), 'v_and_b32': (2, '{0} & {1}'), 'v_or_b32': (2, '{0} | {1}'), 'v_xor_b32': (2, '{0} ^ {1}'), 'v_not_b32': (1, '~{0}'), 'v_lshlrev_b32': (2, '{1} << ({0} & 31)'), 'v_lshrrev_b32': (2, '{1} >> ({0} & 31)'), 'v_ashrrev_i32': (2, 'uint(int({1}) >> ({0} & 31))'), 'v_min_u32': (2, 'min({0}, {1})'), 'v_max_u32': (2, 'max({0}, {1})'), 'v_min_i32': (2, 'uint(min(int({0}), int({1})))'), 'v_max_i32': (2, 'uint(max(int({0}), int({1})))'), @@ -1789,6 +1789,11 @@ def common(self): 'uint2 shr64i(uint lo, uint hi, uint sh) { sh &= 63u; uint sb = sh & 31u, sc = (32u - sb) & 31u;' ' uint cy = sb == 0u ? 0u : (hi << sc); uint h = asuint(asint(hi) >> sb);' ' return uint2(sh < 32u ? ((lo >> sb) | cy) : h, sh < 32u ? h : asuint(asint(hi) >> 31u)); }', + # High 32 bits of an unsigned 32x32 product (issue #1792). These stages have no 64-bit multiply and + # the low half of a * b is not the high half, so it is built from 16-bit halfword products. + 'uint mul_hi_u32(uint a, uint b) { uint la = a & 0xffffu, lb = b & 0xffffu;' + ' uint middle = (a >> 16) * lb + ((la * lb) >> 16);' + ' return (a >> 16) * (b >> 16) + (middle >> 16) + (((middle & 0xffffu) + la * (b >> 16)) >> 16); }', 'uint ldu(ByteAddressBuffer b, uint a) { uint k = (a & 3) * 8, lo = b.Load(a & ~3u); if (k == 0) return lo; ' 'return (lo >> k) | (b.Load((a & ~3u) + 4) << (32 - k)); }', 'cbuffer entry : register(b0) { %s };' % ' '.join('uint ud%d;' % i for i in range(len(self.uds)))] From a0830b07c56ef23c328c1834574321e0324e2fa7 Mon Sep 17 00:00:00 2001 From: tedin7 Date: Tue, 24 Nov 2026 23:19:48 +0100 Subject: [PATCH 2/2] fix: keep mul_hi_u32 helper out of the cvt byte-lane mutation pattern The new helper spelled its halfword shifts as '>> 16)', which the v_cvt_f32_ubyte2 mutation control also matches, so the control saw 11 hits instead of 4. Spell the shifts as '>> 16u)'. --- tools/startup/gfx2hlsl.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tools/startup/gfx2hlsl.py b/tools/startup/gfx2hlsl.py index b59928ef60..1b39590aba 100644 --- a/tools/startup/gfx2hlsl.py +++ b/tools/startup/gfx2hlsl.py @@ -1792,8 +1792,8 @@ def common(self): # High 32 bits of an unsigned 32x32 product (issue #1792). These stages have no 64-bit multiply and # the low half of a * b is not the high half, so it is built from 16-bit halfword products. 'uint mul_hi_u32(uint a, uint b) { uint la = a & 0xffffu, lb = b & 0xffffu;' - ' uint middle = (a >> 16) * lb + ((la * lb) >> 16);' - ' return (a >> 16) * (b >> 16) + (middle >> 16) + (((middle & 0xffffu) + la * (b >> 16)) >> 16); }', + ' uint middle = (a >> 16u) * lb + ((la * lb) >> 16u);' + ' return (a >> 16u) * (b >> 16u) + (middle >> 16u) + (((middle & 0xffffu) + la * (b >> 16u)) >> 16u); }', 'uint ldu(ByteAddressBuffer b, uint a) { uint k = (a & 3) * 8, lo = b.Load(a & ~3u); if (k == 0) return lo; ' 'return (lo >> k) | (b.Load((a & ~3u) + 4) << (32 - k)); }', 'cbuffer entry : register(b0) { %s };' % ' '.join('uint ud%d;' % i for i in range(len(self.uds)))]