From b289d6aabaab863a0e7b81e5331b09206b6302d1 Mon Sep 17 00:00:00 2001 From: tedin7 Date: Tue, 24 Nov 2026 23:56:46 +0100 Subject: [PATCH 1/2] feat: translate s_bfm_b64 in the startup gfx2hlsl pass Part of #1723. Constant operands fold to two exact words; run-time operands build the mask without a 32-bit shift count of 32 or more and reuse shl64. Refuses m0, vcc, exec and modifiers. --- tests/startup_gfx2hlsl_bfm_b64_check.py | 89 +++++++++++++++++++++++++ tools/startup/gfx2hlsl.py | 33 +++++++++ 2 files changed, 122 insertions(+) create mode 100644 tests/startup_gfx2hlsl_bfm_b64_check.py diff --git a/tests/startup_gfx2hlsl_bfm_b64_check.py b/tests/startup_gfx2hlsl_bfm_b64_check.py new file mode 100644 index 000000000..c8182cba4 --- /dev/null +++ b/tests/startup_gfx2hlsl_bfm_b64_check.py @@ -0,0 +1,89 @@ +"""s_bfm_b64 in tools/startup/gfx2hlsl.py (Part of #1723), the 64-bit twin of s_bfm_b32. + +usage: startup_gfx2hlsl_bfm_b64_check.py + +Pure Python, no GPU. Constant operands fold to two exact 32-bit words; run-time operands emit one block +that reads both sources before writing either destination register; unsupported shapes are refused. +""" +import sys +from pathlib import Path + +HERE = Path(__file__).resolve().parent +sys.path.insert(0, str(HERE)) +import startup_gfx2hlsl_scalar_m0_check as M # noqa: E402 + +G = M.G +SOP2_BFM_B64 = 0x25 +DST = 20 # s[20:21] +CONST_W, CONST_O = 6, 7 + + +def run(width_sym, offset_sym, *, dst=DST, w=CONST_W, o=CONST_O): + stage = M.ProbeStage() + stage.sym[CONST_W], stage.sym[CONST_O] = ('const', width_sym), ('const', offset_sym) + code = M.encode_sop2(SOP2_BFM_B64, dst, w, o) + _, name, asm, _ = M.D.decode_instruction(code, 0) + assert name == 's_bfm_b64', name + stage.one(name, asm) + return stage + + +def words(stage): + lo, hi = stage.sym[DST], stage.sym[DST + 1] + assert lo[0] == hi[0] == 'const', (lo, hi) + assert stage.lines == ['s%d = 0x%08xu;' % (DST, lo[1]), 's%d = 0x%08xu;' % (DST + 1, hi[1])], stage.lines + return lo[1], hi[1] + + +def controls(): + # (width, offset) -> (low word, high word), written out by hand from the 64-bit definition. + cases = ( + (0, 0, 0x00000000, 0x00000000), + (1, 0, 0x00000001, 0x00000000), + (16, 0, 0x0000ffff, 0x00000000), + (32, 0, 0xffffffff, 0x00000000), # the width the 32-bit op cannot express + (33, 0, 0xffffffff, 0x00000001), + (63, 0, 0xffffffff, 0x7fffffff), + (64, 0, 0x00000000, 0x00000000), # only bits [5:0] count: width 64 is width 0 + (1, 63, 0x00000000, 0x80000000), + (4, 30, 0xc0000000, 0x00000003), # the mask crosses the word boundary + (8, 32, 0x00000000, 0x000000ff), + (32, 16, 0xffff0000, 0x0000ffff), + (16, 100, 0x00000000, 0x000ffff0), # offset 100 -> 36: bits 36..51 + (1, 64, 0x00000001, 0x00000000), # only bits [5:0] of the offset count: offset 64 is offset 0 + ) + for width, offset, lo, hi in cases: + got = words(run(width, offset)) + assert got == (lo, hi), (width, offset, '%08x %08x' % got) + + # A run-time source keeps the mask in run-time code: both sources are read inside one block, so a + # destination that overlaps a source (s[6:7] = bfm(s6, s7)) cannot feed a half-written value back. + stage = M.ProbeStage() + _, name, asm, _ = M.D.decode_instruction(M.encode_sop2(SOP2_BFM_B64, 8, 8, 128 + 2), 0) + stage.one(name, asm) + assert stage.sym.get(8) is None and stage.sym.get(9) is None, stage.sym + block = [line for line in stage.lines if 'shl64' in line] + assert len(block) == 1 and block[0].count('s8 = ') == 1 and block[0].count('s9 = ') == 1, stage.lines + assert block[0].index('ud') < block[0].index('s8 = '), block[0] + assert {8, 9} <= stage.used_s, stage.used_s + + # SCC is not touched. + assert not any('scc' in line for line in stage.lines), stage.lines + + for dst in (M.M0, 106): + try: + run(1, 1, dst=dst) + except G.Unsupported: + pass + else: + raise AssertionError('destination %d should be refused' % dst) + + +def main(): + controls() + print('startup s_bfm_b64 controls passed') + return 0 + + +if __name__ == '__main__': + sys.exit(main()) diff --git a/tools/startup/gfx2hlsl.py b/tools/startup/gfx2hlsl.py index 5df469afa..dc098eab2 100644 --- a/tools/startup/gfx2hlsl.py +++ b/tools/startup/gfx2hlsl.py @@ -920,6 +920,39 @@ def op_s_bfm_b32(self, ops, mods): self.set_s(d, '(((1u << (%s & 31)) - 1) << (%s & 31))' % (self.u(ops[1]), self.u(ops[2]))) return None + def op_s_bfm_b64(self, ops, mods): + """64-bit bitfield mask: D = ((1 << S0[5:0]) - 1) << S1[5:0]; both sources are 32-bit, SCC is untouched.""" + if mods or len(ops) != 3: + raise Unsupported('s_bfm_b64 with %d operands and modifiers %s' % (len(ops), mods)) + dst = reg_range(ops[0]) + if not dst or dst[0] != 's' or dst[2] != dst[1] + 1 or dst[2] > 105: + raise Unsupported('s_bfm_b64 destination %s' % ops[0]) + vals = [] + for o in ops[1:3]: + r = reg_range(o) + if r and r[0] == 's': + vals.append(self.is_const(r[1])) + else: + try: + vals.append(int(o, 0)) + except ValueError: + vals.append(None) + d = dst[1] + if None not in vals: + value = (((1 << (vals[0] & 63)) - 1) << (vals[1] & 63)) & 0xffffffffffffffff + self.set_s(d, '0x%08xu' % (value & 0xffffffff), ('const', value & 0xffffffff)) + self.set_s(d + 1, '0x%08xu' % (value >> 32), ('const', value >> 32)) + return None + # The mask words are built so no 32-bit shift sees a count of 32 or more, then shl64 moves them; + # both sources are read before either destination register is written (D may overlap S0/S1). + width, offset = self.u(ops[1]), self.u(ops[2]) + self.emit('{ uint bw = (%s) & 63u; uint2 t = shl64(bw >= 32u ? 0xffffffffu : ((1u << (bw & 31u)) - 1u),' + ' bw > 32u ? ((1u << ((bw - 32u) & 31u)) - 1u) : 0u, %s); s%d = t.x; s%d = t.y; }' % (width, offset, d, d + 1)) + for r in (d, d + 1): + self.sym.pop(r, None) + self.used_s.add(r) + return None + def op_s_load_dwordx8(self, ops, mods): self.op_s_load_dwordx4(ops, mods, 8) From ce590a3a5001c24d44bbed080866fefc320fa8e4 Mon Sep 17 00:00:00 2001 From: tedin7 Date: Tue, 24 Nov 2026 23:56:52 +0100 Subject: [PATCH 2/2] ci: register the s_bfm_b64 controls test --- CMakeLists.txt | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index 2f273b5e6..b38b61d6a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2320,6 +2320,8 @@ if(EXISTS "${INTAKE_PYTHON}") "$" ${adapter} $<$:${STARTUP_FXC}>) set_tests_properties(startup_inline_sampler_${adapter} PROPERTIES TIMEOUT 120 SKIP_RETURN_CODE 77) endforeach() + add_test(NAME startup_gfx2hlsl_bfm_b64_controls COMMAND "${INTAKE_PYTHON}" -B + "${CMAKE_SOURCE_DIR}/tests/startup_gfx2hlsl_bfm_b64_check.py") add_test(NAME startup_gfx2hlsl_scalar_m0_controls COMMAND "${INTAKE_PYTHON}" -B "${CMAKE_SOURCE_DIR}/tests/startup_gfx2hlsl_scalar_m0_check.py" controls) foreach(adapter IN ITEMS warp hardware)