From d4dddc329c84f2e32020f67e32e3bb066b358106 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:33:39 +0800 Subject: [PATCH] startup: lower v_fma_mix_f32 and op_sel on v_fma_mix in gfx2hlsl --- .github/workflows/windows.yml | 1 + AGENTS.md | 1 + CMakeLists.txt | 3 + tests/startup_gfx2hlsl_fma_mix_check.py | 152 ++++++++++++++++++++++++ tools/startup/gfx2hlsl.py | 72 ++++++++--- 5 files changed, 214 insertions(+), 15 deletions(-) create mode 100644 tests/startup_gfx2hlsl_fma_mix_check.py diff --git a/.github/workflows/windows.yml b/.github/workflows/windows.yml index d8ce371448..1aef748ddc 100644 --- a/.github/workflows/windows.yml +++ b/.github/workflows/windows.yml @@ -224,6 +224,7 @@ jobs: tests/startup_gfx2hlsl_mask_overwrite_check.py \ tests/startup_gfx2hlsl_image_get_lod_check.py \ tests/startup_gfx2hlsl_add_sub_co_check.py \ + tests/startup_gfx2hlsl_fma_mix_check.py \ tests/startup_gfx2hlsl_saveexec_same_reg_check.py \ tests/startup_gfx2hlsl_compare_dst_check.py \ tests/startup_gfx2hlsl_capture_check.py \ diff --git a/AGENTS.md b/AGENTS.md index 2af03946af..f4a58ce312 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -192,6 +192,7 @@ python3 -m venv out/pe-env ./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 ./out/pe-env/bin/python -B tests/startup_gfx2hlsl_add_sub_co_check.py +./out/pe-env/bin/python -B tests/startup_gfx2hlsl_fma_mix_check.py ./out/pe-env/bin/python -B tests/startup_gfx2hlsl_saveexec_same_reg_check.py ./out/pe-env/bin/python -B tests/startup_gfx2hlsl_compare_dst_check.py ./out/pe-env/bin/python -B tests/startup_flat_param_check.py diff --git a/CMakeLists.txt b/CMakeLists.txt index 417d155511..054e02239b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3084,6 +3084,9 @@ if(EXISTS "${INTAKE_PYTHON}") # Startup shader translator: v_add/sub/subrev_co_u32 against ISA references. add_test(NAME startup_gfx2hlsl_add_sub_co_controls COMMAND "${INTAKE_PYTHON}" -B "${CMAKE_SOURCE_DIR}/tests/startup_gfx2hlsl_add_sub_co_check.py") + # Startup shader translator: v_fma_mix_f32 and op_sel on v_fma_mix{lo,hi}_f16. + add_test(NAME startup_gfx2hlsl_fma_mix_controls COMMAND "${INTAKE_PYTHON}" -B + "${CMAKE_SOURCE_DIR}/tests/startup_gfx2hlsl_fma_mix_check.py") # Startup shader translator: s_*_saveexec with D == S0 reads S0 before writing D. add_test(NAME startup_gfx2hlsl_saveexec_same_reg_controls COMMAND "${INTAKE_PYTHON}" -B "${CMAKE_SOURCE_DIR}/tests/startup_gfx2hlsl_saveexec_same_reg_check.py") diff --git a/tests/startup_gfx2hlsl_fma_mix_check.py b/tests/startup_gfx2hlsl_fma_mix_check.py new file mode 100644 index 0000000000..0df16fdb7b --- /dev/null +++ b/tests/startup_gfx2hlsl_fma_mix_check.py @@ -0,0 +1,152 @@ +"""Translation controls for v_fma_mix_f32 and op_sel on v_fma_mix{lo,hi}_f16 in gfx2hlsl. + +usage: startup_gfx2hlsl_fma_mix_check.py + +Synthetic VOP3P words only; spellings are asserted from the translator's decoder. Covers f32/f16 +operand widening, op_sel high-half extraction, abs/neg after widening, clamp, untouched-half +preservation for f16 destinations, odd-d16 low16 tracking, refusals, and a negative control. +""" +import struct +import sys +from pathlib import Path + +HERE = Path(__file__).resolve().parent +sys.path.insert(0, str(HERE.parent / 'tools' / 'startup')) +import gfx2hlsl as G # noqa: E402 + +failures = 0 + + +def check(what, ok, detail=''): + global failures + print('%s %s%s' % ('ok ' if ok else 'FAIL', what, '' if ok or not detail else ': %s' % (detail,))) + if not ok: + failures += 1 + + +class Pipe: + root = {'params': [], 'flags': 0} + + def ud(self, source): + return 0 + + +def vop3p_fma_mix(op, vdst, src0, src1, src2, op_sel=(0, 0, 0), op_sel_hi=(0, 0, 0), + neg=(0, 0, 0), abs_=(0, 0, 0), clamp=False): + w0 = ( + 0xcc000000 + | (op << 16) + | (clamp << 15) + | (op_sel_hi[2] << 14) + | (op_sel[2] << 13) + | (op_sel[1] << 12) + | (op_sel[0] << 11) + | (abs_[2] << 10) + | (abs_[1] << 9) + | (abs_[0] << 8) + | vdst + ) + w1 = ( + (neg[2] << 31) + | (neg[1] << 30) + | (neg[0] << 29) + | (op_sel_hi[1] << 28) + | (op_sel_hi[0] << 27) + | (src2 << 18) + | (src1 << 9) + | src0 + ) + return [w0, w1] + + +def stage(low16=()): + s = G.Stage(Pipe(), 'ps', struct.pack('> 16)) * (-f16tof32(v19)) + F(v20)));'], lines) + + # Inline f16 constant with op_sel_hi=1 and high-half selection on mixlo/mixhi_f16. + _, text, lines = translate(vop3p_fma_mix(0x21, 22, 242, 240, 256 + 21, + op_sel=(0, 1, 0), op_sel_hi=(1, 1, 0))) # 242=1.0, 240=0.5 + check('decodes v_fma_mixlo_f16 with op_sel', text == 'v_fma_mixlo_f16 v22, 1.0, 0.5, v21 op_sel:[0,1,0] op_sel_hi:[1,1,0]', text) + check('v_fma_mixlo_f16 preserves upper half and shifts inline f16 op_sel=1', + lines == ['v22 = (v22 & 0xffff0000u) | f32tof16(f16tof32(0x3c00u) * f16tof32(0x3800u >> 16) + F(v21));'], lines) + + _, text, lines = translate(vop3p_fma_mix(0x22, 22, 256 + 19, 256 + 23, 256 + 20, + op_sel=(1, 0, 0), op_sel_hi=(1, 0, 0), + neg=(1, 0, 0), abs_=(1, 0, 0), clamp=True)) + check('decodes v_fma_mixhi_f16 with op_sel and clamp', + text == 'v_fma_mixhi_f16 v22, -|v19|, v23, v20 op_sel:[1,0,0] op_sel_hi:[1,0,0] clamp', text) + check('v_fma_mixhi_f16 preserves lower half and saturates', + lines == ['v22 = (v22 & 0xffffu) | (f32tof16(saturate((-abs(f16tof32(v19 >> 16))) * F(v23) + F(v20))) << 16);'], lines) + + # Zero literal and inline negative zero remain valid even when op_sel_hi bit is set. + _, _, lines = translate('v_fma_mix_f32 v2, 1.0, s38, -0 op_sel:[0,1,0] op_sel_hi:[0,1,0]') + check('-0 under f32 op_sel_hi=0 lowers cleanly', lines == ['v2 = U(1.0 * f16tof32(s38 >> 16) + 0.0);'], lines) + s_low, _, lines = translate('v_fma_mix_f32 v4, v4, 1.0, 0 op_sel_hi:[1,0,0]', low16=(4,)) + check('v_fma_mix_f32 reads low16 safely when op_sel=0 and clears low16_only on f32 write', + 4 not in s_low.whole_read_v and 4 not in s_low.low16_only_v and + lines == ['v4 = U(f16tof32(v4) * 1.0 + F(0x00000000u));'], (s_low.whole_read_v, s_low.low16_only_v, lines)) + + # Reading high half (op_sel=1) on an odd-d16 low16-only VGPR must fail whole-word read check. + for label, fn in ( + ('high-half read of low16-only v4 refused', + lambda: translate('v_fma_mix_f32 v5, v4, 1.0, 0 op_sel:[1,0,0] op_sel_hi:[1,0,0]', low16=(4,))), + ('op_sel=1 without op_sel_hi refused', + lambda: translate('v_fma_mix_f32 v5, v4, 1.0, 0 op_sel:[1,0,0]')), + ('output modifier mul:2 refused', + lambda: translate('v_fma_mix_f32 v5, v4, 1.0, 0 mul:2')), + ('SGPR destination refused', + lambda: translate('v_fma_mix_f32 s5, v4, 1.0, 0')), + ('wrong operand count refused', + lambda: translate('v_fma_mix_f32 v5, v4, 1.0')), + ): + try: + fn() + except G.Unsupported: + check(label, True) + else: + check(label, False, 'accepted') + + # Negative control: removing op_v_fma_mix_f32 from Stage must refuse translation. + saved = G.Stage.op_v_fma_mix_f32 + delattr(G.Stage, 'op_v_fma_mix_f32') + try: + translate('v_fma_mix_f32 v12, v1, v2, v3') + except G.Unsupported: + check('negative control: removed handler refuses v_fma_mix_f32', True) + else: + check('negative control: removed handler refuses v_fma_mix_f32', False, 'accepted') + finally: + G.Stage.op_v_fma_mix_f32 = saved + + sys.exit(1 if failures else 0) + + +if __name__ == '__main__': + main() diff --git a/tools/startup/gfx2hlsl.py b/tools/startup/gfx2hlsl.py index eccba084ee..931dc0e120 100644 --- a/tools/startup/gfx2hlsl.py +++ b/tools/startup/gfx2hlsl.py @@ -447,6 +447,9 @@ def parse(asm): FLOATS = {'0.5': 0.5, '-0.5': -0.5, '1.0': 1.0, '-1.0': -1.0, '2.0': 2.0, '-2.0': -2.0, '4.0': 4.0, '-4.0': -4.0, '0.15915494': 0.15915494} +INLINE_F16 = {'0.5': 0x3800, '-0.5': 0xb800, '1.0': 0x3c00, '-1.0': 0xbc00, + '2.0': 0x4000, '-2.0': 0xc000, '4.0': 0x4400, '-4.0': 0xc400, + '0.15915494': 0x3118} def fbits(x): @@ -4240,34 +4243,73 @@ def note_attr(self, tok, how): self.attrs.setdefault(attr, set()).add(how) return attr, component - def mix(self, ops, mods, high): - """v_fma_mix{lo,hi}_f16: fma of operands that are f32 or f16 halves; the f16 result goes to one half.""" - sel = [0, 0, 0] + def mix(self, ops, mods, dest_mode): + """v_fma_mix_f32 / v_fma_mix{lo,hi}_f16: fma of operands that are f32 or f16 halves.""" + name = 'v_fma_mix_f32' if dest_mode == 'f32' else 'v_fma_mix%s_f16' % dest_mode + if len(ops) != 4: + raise Unsupported('%s with %d operands, expected 4' % (name, len(ops))) + dst = reg_range(ops[0]) + if not dst or dst[0] != 'v' or dst[1] != dst[2] or dst[1] > 255: + raise Unsupported('%s destination %s' % (name, ops[0])) + op_sel = [0, 0, 0] + op_sel_hi = [0, 0, 0] + seen = set() + clamp = False for m in mods: - if m.startswith('op_sel_hi:'): - sel = [int(x) for x in m[11:-1].split(',')] - elif m.startswith('op_sel:'): - raise Unsupported('op_sel on fma_mix') - def operand(tok, is_half): + if m == 'clamp' and 'clamp' not in seen: + seen.add('clamp') + clamp = True + continue + sel_match = re.fullmatch(r'(op_sel|op_sel_hi):\[([01],[01],[01])\]', m) + if not sel_match or sel_match.group(1) in seen: + raise Unsupported('%s with %s' % (name, m)) + seen.add(sel_match.group(1)) + bits = [int(x) for x in sel_match.group(2).split(',')] + if sel_match.group(1) == 'op_sel': + op_sel = bits + else: + op_sel_hi = bits + if any(hi and not f16 for hi, f16 in zip(op_sel, op_sel_hi)): + raise Unsupported('op_sel high half without op_sel_hi on fma_mix') + + def operand(tok, is_half, is_hi): if not is_half or tok in ('-0', '0'): return self.f(tok) - return 'f16tof32(%s)' % self.u(tok, low16_only=True) - expr = '%s * %s + %s' % tuple(operand(t, s) for t, s in zip(ops[1:4], sel)) - if 'clamp' in mods: + neg, base_tok = self.split_neg(tok) + absolute = base_tok.startswith('|') and base_tok.endswith('|') + if absolute: + base_tok = base_tok[1:-1] + if base_tok in INLINE_F16: + word = '0x%04xu' % INLINE_F16[base_tok] + else: + word = self.u(base_tok, low16_only=not is_hi) + val = 'f16tof32(%s >> 16)' % word if is_hi else 'f16tof32(%s)' % word + if absolute: + val = 'abs(%s)' % val + return '(-%s)' % val if neg else val + + expr = '%s * %s + %s' % tuple(operand(t, f16, hi) for t, f16, hi in zip(ops[1:4], op_sel_hi, op_sel)) + d = dst[1] + if dest_mode == 'f32': + self.set_vf(d, expr, ('clamp',) if clamp else ()) + return + if clamp: expr = 'saturate(%s)' % expr - d = reg_range(ops[0])[1] self.used_v.add(d) - if high: + if dest_mode == 'hi': self.set_v(d, '(v%d & 0xffffu) | (f32tof16(%s) << 16)' % (d, expr)) else: keep_low16 = d in self.low16_only_v self.set_v(d, '(v%d & 0xffff0000u) | f32tof16(%s)' % (d, expr), low16_only=keep_low16) + def op_v_fma_mix_f32(self, ops, mods): + self.mix(ops, mods, 'f32') + def op_v_fma_mixlo_f16(self, ops, mods): - self.mix(ops, mods, False) + self.mix(ops, mods, 'lo') def op_v_fma_mixhi_f16(self, ops, mods): - self.mix(ops, mods, True) + self.mix(ops, mods, 'hi') def i64_words(self, tok): """(low, high) uint expressions of a 64-bit integer source: a VGPR/SGPR pair or an inline int."""