From 70c4b571e91b8ea398ab256d9789e2b4e2142133 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=A8=E5=AE=B6=E5=B8=85?= <1245206726@qq.com> Date: Thu, 26 Nov 2026 00:21:21 +0800 Subject: [PATCH] startup: lower v_mul_hi_u32 in gfx2hlsl --- .github/workflows/windows.yml | 1 + AGENTS.md | 1 + CMakeLists.txt | 3 + tests/startup_gfx2hlsl_mul_hi_u32_check.py | 94 ++++++++++++++++++++++ tools/startup/gfx2hlsl.py | 3 +- 5 files changed, 101 insertions(+), 1 deletion(-) create mode 100644 tests/startup_gfx2hlsl_mul_hi_u32_check.py diff --git a/.github/workflows/windows.yml b/.github/workflows/windows.yml index d8ce37144..e2b2b41d7 100644 --- a/.github/workflows/windows.yml +++ b/.github/workflows/windows.yml @@ -220,6 +220,7 @@ jobs: tests/startup_gfx2hlsl_cvt_alu_check.py \ tests/startup_gfx2hlsl_cvt_pk_u8_check.py \ tests/startup_gfx2hlsl_int_med3_check.py \ + tests/startup_gfx2hlsl_mul_hi_u32_check.py \ tests/startup_gfx2hlsl_cselect_mask_check.py \ tests/startup_gfx2hlsl_mask_overwrite_check.py \ tests/startup_gfx2hlsl_image_get_lod_check.py \ diff --git a/AGENTS.md b/AGENTS.md index 2af03946a..1d0c683cd 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -188,6 +188,7 @@ python3 -m venv out/pe-env ./out/pe-env/bin/python -B tests/startup_gfx2hlsl_sdwa_cvt_check.py ./out/pe-env/bin/python -B tests/startup_gfx2hlsl_sopk_accum_check.py ./out/pe-env/bin/python -B tests/startup_gfx2hlsl_int_med3_check.py +./out/pe-env/bin/python -B tests/startup_gfx2hlsl_mul_hi_u32_check.py ./out/pe-env/bin/python -B tests/startup_gfx2hlsl_cselect_mask_check.py ./out/pe-env/bin/python -B tests/startup_gfx2hlsl_mask_overwrite_check.py ./out/pe-env/bin/python -B tests/startup_gfx2hlsl_image_get_lod_check.py diff --git a/CMakeLists.txt b/CMakeLists.txt index 417d15551..d05ce53a4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3066,6 +3066,9 @@ if(EXISTS "${INTAKE_PYTHON}") # Startup shader translator: signed and unsigned three-source integer min, max and median. add_test(NAME startup_gfx2hlsl_int_med3_controls COMMAND "${INTAKE_PYTHON}" -B "${CMAKE_SOURCE_DIR}/tests/startup_gfx2hlsl_int_med3_check.py") + # Startup shader translator: unsigned 32-bit high-half vector multiply (v_mul_hi_u32). + add_test(NAME startup_gfx2hlsl_mul_hi_u32_controls COMMAND "${INTAKE_PYTHON}" -B + "${CMAKE_SOURCE_DIR}/tests/startup_gfx2hlsl_mul_hi_u32_check.py") # Startup shader translator: s_cselect_b32 into the wave32 lane masks vcc_lo and exec_lo. add_test(NAME startup_gfx2hlsl_cselect_mask_controls COMMAND "${INTAKE_PYTHON}" -B "${CMAKE_SOURCE_DIR}/tests/startup_gfx2hlsl_cselect_mask_check.py") 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 000000000..8675eb442 --- /dev/null +++ b/tests/startup_gfx2hlsl_mul_hi_u32_check.py @@ -0,0 +1,94 @@ +"""Translation controls for unsigned 32-bit high-half multiply (v_mul_hi_u32) in gfx2hlsl. + + (no arguments) translation controls, pure Python +""" +import sys +from pathlib import Path + +sys.path.insert(0, str(Path(__file__).resolve().parents[1] / 'tools' / 'startup')) +import gfx2hlsl # noqa: E402 + + +MASK = 0xffffffff +PAIRS = [ + (0, 0), + (0, MASK), + (1, MASK), + (0x80000000, 2), + (0x10000, 0x10000), + (0xffff, 0xffff), + (0x8000ffff, 0x8000ffff), + (0xffffffff, 0xffffffff), + (0x12345678, 0x9abcdef0), + (0x7fffffff, 0x80000001), +] + + +def hlsl_mul_hi_u32(a, b): + """Exact 32-bit evaluation of the mul_hi_u32 HLSL helper emitted by gfx2hlsl.""" + la = a & 0xffff + lb = b & 0xffff + middle = (((a >> 16) * lb) & MASK) + (((la * lb) & MASK) >> 16) + assert middle <= MASK, (a, b, middle) + mid_lo = (middle & 0xffff) + ((la * (b >> 16)) & MASK) + assert mid_lo <= MASK, (a, b, mid_lo) + result = (((a >> 16) * (b >> 16)) & MASK) + (middle >> 16) + (mid_lo >> 16) + assert result <= MASK, (a, b, result) + return result + + +def translate(asm): + stage = gfx2hlsl.Stage.__new__(gfx2hlsl.Stage) + stage.lines = [] + stage.used_v = set() + stage.uniform_v = set() + stage.exec_known = True + stage.one(asm.split()[0], asm) + return stage.lines + + +def refused(asm, reason): + try: + translate(asm) + except gfx2hlsl.Unsupported as exc: + assert str(exc) == reason, (asm, str(exc)) + else: + raise AssertionError('accepted %s' % asm) + + +def main(): + for suffix in ('', '_e64'): + asm = 'v_mul_hi_u32%s v1, v2, v3' % suffix + lines = translate(asm) + assert lines == ['v1 = mul_hi_u32(v2, v3);'], (asm, lines) + + # Inline-constant and literal sources go through the shared u() path as 32-bit words. + lines = translate('v_mul_hi_u32 v1, v2, 0xaaaaaaab') + assert lines == ['v1 = mul_hi_u32(v2, 0xaaaaaaabu);'], lines + lines = translate('v_mul_hi_u32_e64 v1, -1, v2') + assert lines == ['v1 = mul_hi_u32(0xffffffffu, v2);'], lines + + # Verify the emitted mul_hi_u32 HLSL helper matches (a * b) >> 32 without 32-bit intermediate overflow. + for a, b in PAIRS: + for x, y in ((a, b), (b, a)): + expected = (x * y) >> 32 + actual = hlsl_mul_hi_u32(x, y) + assert actual == expected, (hex(x), hex(y), hex(actual), hex(expected)) + + # Modifiers, float-style operand modifiers, and wrong operand counts are refused. + refused('v_mul_hi_u32_e64 v1, v2, v3 clamp', "v_mul_hi_u32 with ['clamp']") + refused('v_mul_hi_u32_e64 v1, -v2, v3', "operand '-v2'") + refused('v_mul_hi_u32 v1, v2', 'v_mul_hi_u32 with 2 operands, expected 3') + + # Negative control: removing the entry from VEC_U must refuse translation. + entry = gfx2hlsl.VEC_U.pop('v_mul_hi_u32') + try: + refused('v_mul_hi_u32 v1, v2, v3', 'v_mul_hi_u32') + finally: + gfx2hlsl.VEC_U['v_mul_hi_u32'] = entry + + print('PASS: startup_gfx2hlsl_mul_hi_u32_check') + + +if __name__ == '__main__': + main() diff --git a/tools/startup/gfx2hlsl.py b/tools/startup/gfx2hlsl.py index eccba084e..78b9e44e6 100644 --- a/tools/startup/gfx2hlsl.py +++ b/tools/startup/gfx2hlsl.py @@ -4764,7 +4764,8 @@ 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})))'),