From 6b81c11fb8ff544fcaba9cb1743121509bde1b58 Mon Sep 17 00:00:00 2001 From: tedin7 Date: Wed, 25 Nov 2026 01:06:08 +0100 Subject: [PATCH 1/4] shader: lower s_pack_ll_b32_b16, s_pack_lh_b32_b16 and s_absdiff_i32 in gfx2hlsl Part of #1723. s_absdiff_i32 subtracts the smaller signed source from the larger, so an overflowing difference wraps like the hardware. SCC is unchanged by the pack forms and is result != 0 for s_absdiff_i32. --- CMakeLists.txt | 3 ++ tests/startup_gfx2hlsl_pack_absdiff_check.py | 55 ++++++++++++++++++++ tools/startup/gfx2hlsl.py | 8 ++- 3 files changed, 65 insertions(+), 1 deletion(-) create mode 100644 tests/startup_gfx2hlsl_pack_absdiff_check.py diff --git a/CMakeLists.txt b/CMakeLists.txt index ed00bb5c12..b147c99ef9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3075,6 +3075,9 @@ if(EXISTS "${INTAKE_PYTHON}") # Scalar SOPK accumulate: s_addk_i32 signed-overflow SCC and s_mulk_i32 against an integer reference. add_test(NAME startup_gfx2hlsl_sopk_accum COMMAND "${INTAKE_PYTHON}" -B "${CMAKE_CURRENT_SOURCE_DIR}/tests/startup_gfx2hlsl_sopk_accum_check.py") + # s_pack_ll_b32_b16, s_pack_lh_b32_b16 and s_absdiff_i32 against an integer reference (#1723). + add_test(NAME startup_gfx2hlsl_pack_absdiff COMMAND "${INTAKE_PYTHON}" -B + "${CMAKE_CURRENT_SOURCE_DIR}/tests/startup_gfx2hlsl_pack_absdiff_check.py") # gfx2hlsl run-time sampler heap indexing (#1143): translation controls, then a desktop draw with # DXC ps_6_0 and, when the Windows SDK's FXC is found, the ps_5_1 the startup tools compile with. find_program(STARTUP_FXC fxc HINTS "${DXC_BIN_DIR}") diff --git a/tests/startup_gfx2hlsl_pack_absdiff_check.py b/tests/startup_gfx2hlsl_pack_absdiff_check.py new file mode 100644 index 0000000000..4f06612158 --- /dev/null +++ b/tests/startup_gfx2hlsl_pack_absdiff_check.py @@ -0,0 +1,55 @@ +"""s_pack_ll_b32_b16, s_pack_lh_b32_b16 and s_absdiff_i32 in tools/startup/gfx2hlsl.py (Part of #1723). + +Pure Python. Each form is built as a SOP2 word, decoded, translated, and the emitted lines run on the integer +evaluator of startup_gfx2hlsl_bit_alu_check.py over edge values (the encoder, forms and value set come from +startup_gfx2hlsl_sop2_complement_check.py). The references use plain integer arithmetic from the ISA meaning: + + s_pack_ll_b32_b16: D = (S0 & 0xffff) | (S1 << 16) SCC unchanged + s_pack_lh_b32_b16: D = (S0 & 0xffff) | (S1 & 0xffff0000) SCC unchanged + s_pack_hh_b32_b16: D = (S0 >> 16) | (S1 & 0xffff0000) SCC unchanged + s_absdiff_i32: D = |S0 - S1| as signed 32 bits (INT_MIN stays INT_MIN); SCC = (D != 0) + +Lane-mask operands of the pack forms are refused (the per-lane projection has no whole word to read). +""" +import sys +from pathlib import Path + +HERE = Path(__file__).resolve().parent +sys.path.insert(0, str(HERE)) +import startup_gfx2hlsl_sop2_complement_check as C # noqa: E402 + +MASK = C.MASK + + +def signed(x): + return x - (1 << 32) if x & 0x80000000 else x + + +REFERENCES = { + 's_pack_ll_b32_b16': (lambda a, b: (a & 0xffff) | ((b << 16) & MASK), False), + 's_pack_lh_b32_b16': (lambda a, b: (a & 0xffff) | (b & 0xffff0000), False), + 's_pack_hh_b32_b16': (lambda a, b: (a >> 16) | (b & 0xffff0000), False), + 's_absdiff_i32': (lambda a, b: abs(signed(a) - signed(b)) & MASK, True), +} + + +def main(): + for name, (reference, scc) in REFERENCES.items(): + C.OPCODE[name] = next(op for op, (n, _) in C.D.SOP2_OPS.items() if n == name) + bad = C.first_mismatch(name, reference, updates_scc=scc) + C.check('%s value and SCC' % name, bad is None, str(bad)) + if name.startswith('s_pack'): + for form in C.MASK_FORMS: + _, stage = C.attempt(C.code(name, form)) + C.check('%s refuses lane mask form %r' % (name, form), stage.lines[0].startswith('refused: ')) + # a wrong reference must be caught: swapped sources + swapped = C.first_mismatch('s_pack_lh_b32_b16', lambda a, b: REFERENCES['s_pack_lh_b32_b16'][0](b, a), False) + C.check('swapped pack_lh reference is caught', swapped is not None) + if C.failures: + print('FAILED: %s' % ', '.join(C.failures)) + sys.exit(1) + print('startup_gfx2hlsl_pack_absdiff_check: ok') + + +if __name__ == '__main__': + main() diff --git a/tools/startup/gfx2hlsl.py b/tools/startup/gfx2hlsl.py index d38afdbd54..6534982bd4 100644 --- a/tools/startup/gfx2hlsl.py +++ b/tools/startup/gfx2hlsl.py @@ -2429,7 +2429,7 @@ def low(tok): word_complement = base in ('s_nand_b32', 's_nor_b32', 's_xnor_b32') lane_complement = False # s_mul_hi_u32 needs the whole 32-bit word of each source, like the halfword pack. - word_pack = base in ('s_pack_hh_b32_b16', 's_sext_i32_i8', 's_sext_i32_i16', 's_mul_hi_u32') + word_pack = base in PACK_B16 or base in ('s_sext_i32_i8', 's_sext_i32_i16', 's_mul_hi_u32') if shift_add or word_complement or word_pack: # The per-lane mask cannot supply the full word needed by these operations. s_mul_hi_u32 into an # SGPR that held a mask replaces it with a plain word (set_s drops the mark), so only vcc/exec count. @@ -4952,6 +4952,7 @@ def declarations(self): SCC_UNMODELLED = frozenset(('s_add_i32', 's_add_u32', 's_sub_i32', 's_sub_u32', 's_min_i32', 's_min_u32', 's_max_i32', 's_max_u32')) +PACK_B16 = frozenset(('s_pack_ll_b32_b16', 's_pack_lh_b32_b16', 's_pack_hh_b32_b16')) SCA = { 's_mov_b32': ('{0}', None), 's_mov_b64': ('{0}', None), 's_movk_i32': ('{0}', None), 's_and_b32': ('{0} & {1}', '$ != 0'), 's_or_b32': ('{0} | {1}', '$ != 0'), 's_xor_b32': ('{0} ^ {1}', '$ != 0'), @@ -4972,7 +4973,12 @@ def declarations(self): # s_sext_i32_iN: sign-extend the low N bits of S0; SCC unchanged. 's_sext_i32_i8': ('uint(int({0} << 24) >> 24)', None), 's_sext_i32_i16': ('uint(int({0} << 16) >> 16)', None), + # s_pack_*_b32_b16: two halfwords of the sources into one word; SCC unchanged. + 's_pack_ll_b32_b16': ('({0} & 0xffffu) | ({1} << 16)', None), + 's_pack_lh_b32_b16': ('({0} & 0xffffu) | ({1} & 0xffff0000u)', None), 's_pack_hh_b32_b16': ('({0} >> 16) | ({1} & 0xffff0000u)', None), + # s_absdiff_i32: the larger signed source minus the smaller, wrapping on 32 bits; SCC = result != 0. + 's_absdiff_i32': ('(int({0}) > int({1}) ? {0} - {1} : {1} - {0})', '$ != 0'), # s_bfm_b32: a mask of S0[4:0] ones shifted left by S1[4:0]; SCC unchanged. 's_bfm_b32': ('(((1u << ({0} & 31)) - 1) << ({1} & 31))', None), # s_lshlN_add_u32: low 32 bits of (S0 << N) + S1. SHIFT_ADD also supplies SCC for wide overflow. From 5e8687dbfa5465062461a7047184cccbc729c5ad Mon Sep 17 00:00:00 2001 From: tedin7 Date: Wed, 25 Nov 2026 02:04:30 +0100 Subject: [PATCH 2/4] shader: wrap s_absdiff_i32 difference, refuse lane-mask and descriptor sources --- tests/startup_gfx2hlsl_bit_alu_check.py | 2 ++ tests/startup_gfx2hlsl_pack_absdiff_check.py | 20 ++++++++++++++++---- tools/startup/gfx2hlsl.py | 9 +++++---- 3 files changed, 23 insertions(+), 8 deletions(-) diff --git a/tests/startup_gfx2hlsl_bit_alu_check.py b/tests/startup_gfx2hlsl_bit_alu_check.py index 3018f12a75..2d308f1cc3 100644 --- a/tests/startup_gfx2hlsl_bit_alu_check.py +++ b/tests/startup_gfx2hlsl_bit_alu_check.py @@ -267,6 +267,8 @@ def call(name, args): return v, True if name in ('asuint', 'uint'): return v, False + if name == 'abs': + return (-v if v & 0x80000000 else v) & MASK, True if name == 'min': assert not s and not rest[0][1] return min(v, rest[0][0]), False diff --git a/tests/startup_gfx2hlsl_pack_absdiff_check.py b/tests/startup_gfx2hlsl_pack_absdiff_check.py index 4f06612158..15a4fd0bde 100644 --- a/tests/startup_gfx2hlsl_pack_absdiff_check.py +++ b/tests/startup_gfx2hlsl_pack_absdiff_check.py @@ -7,9 +7,9 @@ s_pack_ll_b32_b16: D = (S0 & 0xffff) | (S1 << 16) SCC unchanged s_pack_lh_b32_b16: D = (S0 & 0xffff) | (S1 & 0xffff0000) SCC unchanged s_pack_hh_b32_b16: D = (S0 >> 16) | (S1 & 0xffff0000) SCC unchanged - s_absdiff_i32: D = |S0 - S1| as signed 32 bits (INT_MIN stays INT_MIN); SCC = (D != 0) + s_absdiff_i32: D = |wrap32(S0 - S1)| (INT_MIN stays INT_MIN); SCC = (D != 0) -Lane-mask operands of the pack forms are refused (the per-lane projection has no whole word to read). +Lane-mask operands of the pack forms and s_absdiff_i32 are refused (the per-lane projection has no whole word to read). """ import sys from pathlib import Path @@ -29,7 +29,7 @@ def signed(x): 's_pack_ll_b32_b16': (lambda a, b: (a & 0xffff) | ((b << 16) & MASK), False), 's_pack_lh_b32_b16': (lambda a, b: (a & 0xffff) | (b & 0xffff0000), False), 's_pack_hh_b32_b16': (lambda a, b: (a >> 16) | (b & 0xffff0000), False), - 's_absdiff_i32': (lambda a, b: abs(signed(a) - signed(b)) & MASK, True), + 's_absdiff_i32': (lambda a, b: abs(signed((a - b) & MASK)) & MASK, True), } @@ -38,10 +38,22 @@ def main(): C.OPCODE[name] = next(op for op, (n, _) in C.D.SOP2_OPS.items() if n == name) bad = C.first_mismatch(name, reference, updates_scc=scc) C.check('%s value and SCC' % name, bad is None, str(bad)) - if name.startswith('s_pack'): + if name.startswith('s_pack') or name == 's_absdiff_i32': for form in C.MASK_FORMS: _, stage = C.attempt(C.code(name, form)) C.check('%s refuses lane mask form %r' % (name, form), stage.lines[0].startswith('refused: ')) + # the wrapped difference of 0x80000000 and 1 is 0x7fffffff; a compare-first lowering gives 0x80000001 + C.check('absdiff overflow reference', REFERENCES['s_absdiff_i32'][0](0x80000000, 1) == 0x7fffffff) + # a symbolic descriptor source must be refused before SCC is left stale + for name in REFERENCES: + stage = C.Probe() + stage.sym[1] = ('ptr', 0, 0) + try: + stage.one(name, '%s s3, s1, s2' % name) + except C.G.Unsupported: + C.check('%s refuses a descriptor source' % name, not stage.lines) + else: + C.check('%s refuses a descriptor source' % name, False, str(stage.lines)) # a wrong reference must be caught: swapped sources swapped = C.first_mismatch('s_pack_lh_b32_b16', lambda a, b: REFERENCES['s_pack_lh_b32_b16'][0](b, a), False) C.check('swapped pack_lh reference is caught', swapped is not None) diff --git a/tools/startup/gfx2hlsl.py b/tools/startup/gfx2hlsl.py index 6534982bd4..53f240cb60 100644 --- a/tools/startup/gfx2hlsl.py +++ b/tools/startup/gfx2hlsl.py @@ -2430,7 +2430,8 @@ def low(tok): lane_complement = False # s_mul_hi_u32 needs the whole 32-bit word of each source, like the halfword pack. word_pack = base in PACK_B16 or base in ('s_sext_i32_i8', 's_sext_i32_i16', 's_mul_hi_u32') - if shift_add or word_complement or word_pack: + word_absdiff = base == 's_absdiff_i32' + if shift_add or word_complement or word_pack or word_absdiff: # The per-lane mask cannot supply the full word needed by these operations. s_mul_hi_u32 into an # SGPR that held a mask replaces it with a plain word (set_s drops the mark), so only vcc/exec count. mask_dest = (dname in ('exec', 'exec_lo', 'exec_hi', 'vcc', 'vcc_lo', 'vcc_hi') if base == 's_mul_hi_u32' @@ -2470,7 +2471,7 @@ def src_sym(t): self.scc_effect = 'descriptor' if self.tracks_uniformity(): self.scc_uniform = False - if shift_add or word_complement: + if shift_add or word_complement or word_absdiff: raise Unsupported('scc from descriptor data') if word_pack: raise Unsupported('%s on descriptor data' % base) @@ -4977,8 +4978,8 @@ def declarations(self): 's_pack_ll_b32_b16': ('({0} & 0xffffu) | ({1} << 16)', None), 's_pack_lh_b32_b16': ('({0} & 0xffffu) | ({1} & 0xffff0000u)', None), 's_pack_hh_b32_b16': ('({0} >> 16) | ({1} & 0xffff0000u)', None), - # s_absdiff_i32: the larger signed source minus the smaller, wrapping on 32 bits; SCC = result != 0. - 's_absdiff_i32': ('(int({0}) > int({1}) ? {0} - {1} : {1} - {0})', '$ != 0'), + # s_absdiff_i32: D = S0 - S1 wrapped to 32 bits, negated if negative (INT_MIN stays); SCC = result != 0. + 's_absdiff_i32': ('asuint(abs(asint({0} - {1})))', '$ != 0'), # s_bfm_b32: a mask of S0[4:0] ones shifted left by S1[4:0]; SCC unchanged. 's_bfm_b32': ('(((1u << ({0} & 31)) - 1) << ({1} & 31))', None), # s_lshlN_add_u32: low 32 bits of (S0 << N) + S1. SHIFT_ADD also supplies SCC for wide overflow. From b354d7f02549d465289852b9fd3b030203bfd24e Mon Sep 17 00:00:00 2001 From: Leyukaka <45659323+Leyukaka@users.noreply.github.com> Date: Wed, 25 Nov 2026 20:10:49 +0100 Subject: [PATCH 3/4] tests: require a signed argument for abs in the gfx2hlsl integer evaluator HLSL abs of a uint is the identity, so an s_absdiff_i32 lowering that dropped asint would still have passed the evaluator. Assert that abs gets a signed value. AI-assisted. --- tests/startup_gfx2hlsl_bit_alu_check.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/startup_gfx2hlsl_bit_alu_check.py b/tests/startup_gfx2hlsl_bit_alu_check.py index 2d308f1cc3..75f4e5e2a4 100644 --- a/tests/startup_gfx2hlsl_bit_alu_check.py +++ b/tests/startup_gfx2hlsl_bit_alu_check.py @@ -268,6 +268,7 @@ def call(name, args): if name in ('asuint', 'uint'): return v, False if name == 'abs': + assert s, 'abs of an unsigned value' # HLSL abs of a uint is the identity return (-v if v & 0x80000000 else v) & MASK, True if name == 'min': assert not s and not rest[0][1] From e2b7dca3960323ee6edc64c5b9454ef9f5ff785c Mon Sep 17 00:00:00 2001 From: Leyukaka <45659323+Leyukaka@users.noreply.github.com> Date: Wed, 25 Nov 2026 20:11:04 +0100 Subject: [PATCH 4/4] tests: list startup_gfx2hlsl_pack_absdiff_check with the other pure-Python checks Add it to the Linux CI Python list and the AGENTS.md list next to startup_gfx2hlsl_sop2_complement_check, whose helpers it imports. AI-assisted. --- .github/workflows/windows.yml | 1 + AGENTS.md | 1 + 2 files changed, 2 insertions(+) diff --git a/.github/workflows/windows.yml b/.github/workflows/windows.yml index a0229f0b06..d23809932b 100644 --- a/.github/workflows/windows.yml +++ b/.github/workflows/windows.yml @@ -266,6 +266,7 @@ jobs: tests/startup_gfx2hlsl_draw_skip_check.py \ tests/startup_gfx2hlsl_sdwa_cvt_check.py \ tests/startup_gfx2hlsl_sop2_complement_check.py \ + tests/startup_gfx2hlsl_pack_absdiff_check.py \ tests/startup_gfx2hlsl_sopk_accum_check.py \ tests/startup_gfx2hlsl_f32_lg_check.py \ tests/startup_gfx2hlsl_f32_nan_check.py \ diff --git a/AGENTS.md b/AGENTS.md index 2851ded0c5..f2b17b9ca5 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -187,6 +187,7 @@ python3 -m venv out/pe-env ./out/pe-env/bin/python -B tests/startup_gfx2hlsl_join_symbols_check.py ./out/pe-env/bin/python -B tests/startup_gfx2hlsl_readfirstlane_recompute_check.py ./out/pe-env/bin/python -B tests/startup_gfx2hlsl_sop2_complement_check.py +./out/pe-env/bin/python -B tests/startup_gfx2hlsl_pack_absdiff_check.py ./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