diff --git a/.github/workflows/windows.yml b/.github/workflows/windows.yml index c1e7556d4..da1b9e52f 100644 --- a/.github/workflows/windows.yml +++ b/.github/workflows/windows.yml @@ -83,6 +83,7 @@ jobs: (["tests/startup_flat_param_check.py", "binary", "llvm-mc-15"], {}), (["tests/startup_gfx2hlsl_bit_alu_check.py", "binary", "llvm-mc-15"], {}), (["tests/startup_gfx2hlsl_mad_check.py", "binary", "llvm-mc-15"], {}), + (["tests/startup_gfx2hlsl_swap_check.py", "binary", "llvm-mc-15"], {}), (["tests/startup_gfx2hlsl_shift64_check.py", "binary", "llvm-mc-15"], {}), (["tests/startup_gfx2hlsl_vopc_sdwa_check.py", "binary", "llvm-mc-15"], {}), (["tests/startup_gfx2hlsl_vopc_sdwa_check.py", "decode", "libLLVM-15.so.1"], {}), @@ -205,6 +206,7 @@ jobs: tests/startup_flat_param_check.py \ tests/startup_gfx2hlsl_bit_alu_check.py \ tests/startup_gfx2hlsl_mad_check.py \ + tests/startup_gfx2hlsl_swap_check.py \ tests/startup_gfx2hlsl_shift64_check.py \ tests/startup_gfx2hlsl_vopc_sdwa_check.py \ tests/startup_gfx2hlsl_sop2_complement_check.py \ diff --git a/AGENTS.md b/AGENTS.md index 1bbf850e3..ac9172173 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -149,6 +149,7 @@ python3 -m venv out/pe-env ./out/pe-env/bin/python -B tests/startup_image_load_mip_check.py ./out/pe-env/bin/python -B tests/startup_gfx2hlsl_operand_count_check.py ./out/pe-env/bin/python -B tests/startup_gfx2hlsl_mad_check.py +./out/pe-env/bin/python -B tests/startup_gfx2hlsl_swap_check.py ./out/pe-env/bin/python -B tests/startup_image_2d_array_check.py ./out/pe-env/bin/python -B tests/startup_host_cpu_check.py ./out/pe-env/bin/python -B tests/startup_gfx2hlsl_capture_check.py diff --git a/CMakeLists.txt b/CMakeLists.txt index 68ce26e01..e8925309d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2075,6 +2075,9 @@ if(EXISTS "${INTAKE_PYTHON}") # gfx2hlsl refuses a binary32 or integer vector operation with a source too few or too many. add_test(NAME startup_gfx2hlsl_operand_count COMMAND "${INTAKE_PYTHON}" -B "${CMAKE_CURRENT_SOURCE_DIR}/tests/startup_gfx2hlsl_operand_count_check.py") + # gfx2hlsl lowers v_swap_b32 (VOP1): CPU evaluation of the translated text, refusals of every form llvm-mc rejects. + add_test(NAME startup_gfx2hlsl_swap COMMAND "${INTAKE_PYTHON}" -B + "${CMAKE_CURRENT_SOURCE_DIR}/tests/startup_gfx2hlsl_swap_check.py") # VOP1/VOP2 sub-dword addressing (SDWA) in the translator (#1141), checked against the pixel AOT # lowering of the same selections. add_test(NAME startup_gfx2hlsl_sdwa COMMAND "${INTAKE_PYTHON}" -B diff --git a/docs/experiments.md b/docs/experiments.md index e194149bd..6ec592291 100644 --- a/docs/experiments.md +++ b/docs/experiments.md @@ -11452,6 +11452,12 @@ ctest --preset release --no-tests=error -R '^startup_image_gather_lz(_warp|_hard The binary32 table (`VEC_F`) applies the same operand-count rule. Before this change, a form one source short (`v_add_f32 v1, v2`) raised `IndexError` in `Stage.one` instead of `Unsupported`. A form with one source too many (`v_med3_f32 v1, v2, v3, v4, v5`) translated with the extra source dropped. `tests/startup_gfx2hlsl_operand_count_check.py` (`startup_gfx2hlsl_operand_count`) checks every operation of both tables: one source fewer and one source more are refused, and the exact count translates. It also checks that the constant and accumulator forms (`v_fmac_f32`, `v_mac_f32`, `v_fmamk_f32`, `v_fmaak_f32`, `v_madmk_f32`, `v_madak_f32`) keep translating with their usual operands. As a control, it loads the translator with the new `VEC_F` guard removed and requires both old failures to come back. +## Startup translator: v_swap_b32 + +`tools/startup/gfx2hlsl.py` now lowers `v_swap_b32` (VOP1, opcode 101), which it used to refuse. Both VGPRs exchange their old values, so the lowering reads the destination into a temporary before either register is written: `{ uint t = v1; v1 = v2; v2 = t; }`. The RDNA 2 ISA allows no input or output modifier on it, and LLVM 15 assembles only the 32-bit VOP1 spelling for gfx1013 (the `_e64`, `_dpp` and `_sdwa` forms and a scalar, constant, negated or 64-bit operand are rejected), so the translator refuses all of those and a modifier. A register-to-itself swap assembles and keeps its value. A proven-uniform register keeps that proof across the swap only while the exec mask is known. + +`tests/startup_gfx2hlsl_swap_check.py` (`startup_gfx2hlsl_swap`) decodes the documented VOP1 words, evaluates the translated text of seven programs on the CPU against a plain exchange of two list entries, and checks the refusals. Two mutations (write before read, only one destination written) are loaded from the translator source and must fail the fixtures. `python -B tests/startup_gfx2hlsl_swap_check.py binary llvm-mc-15` also reassembles the source lines. This is translation only; no draw was run. + ## Non-fused multiply-add in the startup translator The gfx1013 `v_mad_f32`, `v_mac_f32`, `v_madmk_f32`, `v_madak_f32` and diff --git a/tests/startup_gfx2hlsl_swap_check.py b/tests/startup_gfx2hlsl_swap_check.py new file mode 100644 index 000000000..c835dcd53 --- /dev/null +++ b/tests/startup_gfx2hlsl_swap_check.py @@ -0,0 +1,233 @@ +"""v_swap_b32 in tools/startup/gfx2hlsl.py. + +usage: startup_gfx2hlsl_swap_check.py [binary ] + +(no arguments) pure Python: decode the VOP1 words, translate, evaluate the translated text on the CPU, refusals +binary also reassemble the source lines with LLVM 15 (gfx1013) and compare them with the words built here + +Semantics (RDNA 2 ISA, VOP1 opcode 101, "V_SWAP_B32: Swap operands. Input and output modifiers not supported; this is an +untyped operation."; the SDWA and DPP limitation lists in section 12.19 also name V_SWAP_B32): VDST and SRC0 are both +VGPRs and exchange their old values, so both are read before either is written. The reference below is a plain Python +exchange of two list entries, not the lowering's expression. LLVM 15 for gfx1013 assembles only the 32-bit VOP1 form: +the e64, dpp and sdwa spellings, a modifier, a scalar or constant or negated or 64-bit operand are all rejected by +llvm-mc, so the translator refuses them too. + +The translated text is run by a small evaluator over the statements the fixture programs produce. Two mutations of the +lowering (the first write happens before the read; only one destination is written) are loaded from the translator +source and must read wrong words, so the check cannot pass with a wrong lowering. +""" +import re +import sys +from pathlib import Path + +HERE = Path(__file__).resolve().parent +sys.path.insert(0, str(HERE.parent / 'tools' / 'startup')) +sys.path.insert(0, str(HERE.parent / 'tools')) +import gfx2hlsl as G # noqa: E402 +import disasm_rdna2 as D # noqa: E402 + +MASK = 0xffffffff +LOWERING = " body = '{ uint t = v%d; v%d = v%d; v%d = t; }' % (dst, dst, src, src)" +MUTATIONS = { + # the destination is written before the source is read: both registers end with the source's value + 'write before read': " body = '{ v%d = v%d; uint t = v%d; v%d = t; }' % (dst, src, dst, src)", + # only the destination is written: the source keeps its value + 'one destination': " body = '{ uint t = v%d; v%d = v%d; v%d = v%d; }' % (dst, dst, src, src, src)", +} +failures = [] + + +def check(label, condition, detail=''): + print('%s: %s' % (label, 'ok' if condition else 'MISMATCH ' + detail)) + if not condition: + failures.append(label) + + +def probe(module, exec_known=True, uniform=()): + class Probe(module.Stage): + """A Stage with no pipeline: enough state for single instructions to be translated.""" + + def __init__(self): + self.lines, self.sym = [], {} + self.used_s, self.used_v, self.uniform_v = set(), set(), set(uniform) + self.exec_known = exec_known + return Probe() + + +def translate(module, source, **kwargs): + stage = probe(module, **kwargs) + for line in source: + stage.one(line.split(' ', 1)[0], line) + return stage + + +def evaluate(lines, regs): + """Run translated statements (assignments, a temporary in braces, an optional exec guard) on a register dict.""" + regs = dict(regs) + for line in lines: + line = re.sub(r'^if \(exec & 1\) ', '', line) + for statement in re.sub(r'[{}]', '', line).split(';'): + statement = re.sub(r'^\s*uint ', '', statement.strip()) + if not statement: + continue + target, _, expression = statement.partition(' = ') + expression = re.sub(r'\b(0x[0-9a-f]+)u\b', r'\1', expression) + if not re.fullmatch(r'[0-9a-fxtv +^-]*', expression) or not re.fullmatch(r't|v\d+', target): + raise AssertionError('evaluator does not know: %s' % statement) + regs[target] = eval(expression, {}, regs) & MASK + return regs + + +def vop1_word(vdst, src): + """VOP1 word: 0x7e000000 | vdst << 17 | opcode 101 << 9 | src0 (0x100 + VGPR).""" + return (0x7e000000 | vdst << 17 | 101 << 9 | (0x100 + src)).to_bytes(4, 'little') + + +def swap_ref(values, dst, src): + """Reference: exchange two list entries.""" + values = list(values) + values[dst], values[src] = values[src], values[dst] + return values + + +SEED = {1: 0x11111111, 2: 0x22222222, 3: 0x33333333, 4: 0x44444444, 255: 0xdeadbeef, 0: 0x00000001} + + +def mov(reg): + return 'v_mov_b32 v%d, 0x%08x' % (reg, SEED[reg]) + + +def run(module, program): + stage = translate(module, program) + return evaluate(stage.lines, {}), stage + + +def cases(module): + """(label, program, expected registers) for the positive fixtures.""" + s = SEED + return [ + ('plain swap, then both registers are read', + [mov(1), mov(2), 'v_swap_b32 v1, v2', 'v_sub_nc_u32 v3, v1, v2'], + {'v1': s[2], 'v2': s[1], 'v3': (s[2] - s[1]) & MASK}), + ('operand order does not matter', + [mov(1), mov(2), 'v_swap_b32 v2, v1', 'v_sub_nc_u32 v3, v1, v2'], + {'v1': s[2], 'v2': s[1], 'v3': (s[2] - s[1]) & MASK}), + ('self swap keeps the value', + [mov(3), 'v_swap_b32 v3, v3', 'v_xor_b32 v4, v3, v3'], + {'v3': s[3], 'v4': 0}), + ('a register written by another instruction first', + [mov(1), mov(2), 'v_add_nc_u32 v1, v1, v2', 'v_swap_b32 v1, v2'], + {'v1': s[2], 'v2': (s[1] + s[2]) & MASK}), + ('two swaps rotate three registers', + [mov(1), mov(2), mov(3), 'v_swap_b32 v1, v2', 'v_swap_b32 v2, v3'], + {'v1': s[2], 'v2': s[3], 'v3': s[1]}), + ('the same swap twice restores both', + [mov(1), mov(2), 'v_swap_b32 v1, v2', 'v_swap_b32 v1, v2'], + {'v1': s[1], 'v2': s[2]}), + ('highest and lowest VGPR', + [mov(255), mov(0), 'v_swap_b32 v255, v0'], + {'v255': s[0], 'v0': s[255]}), + ] + + +def run_checks(module): + """The positive fixtures against the reference; returns the labels of the mismatching ones.""" + bad = [] + for label, program, expected in cases(module): + try: + regs, stage = run(module, program) + except Exception as error: # noqa: BLE001 report which exception a mutated or old translator raises + bad.append((label, type(error).__name__ + ': ' + str(error))) + continue + got = {k: regs.get(k) for k in expected} + if got != expected: + bad.append((label, 'got %r expected %r' % (got, expected))) + return bad + + +def refused(module, name, line, **kwargs): + try: + probe(module, **kwargs).one(name, line) + except module.Unsupported as error: + return str(error) + except Exception as error: # noqa: BLE001 + return 'other exception %s' % type(error).__name__ + return None + + +# 1. the documented VOP1 encoding decodes to the spelling the translator reads, then translates +for dst, src in ((1, 2), (3, 3), (255, 0), (0, 255)): + n, name, asm, _ = D.decode_instruction(vop1_word(dst, src), 0) + check('decode v%d, v%d' % (dst, src), (n, name, asm) == (4, 'v_swap_b32', 'v_swap_b32 v%d, v%d' % (dst, src)), + repr((n, name, asm))) + +# 2. translated text against the reference +bad = run_checks(G) +check('fixtures: translated text matches the reference exchange (%d programs)' % len(cases(G)), not bad, repr(bad)) +ref = swap_ref([SEED[1], SEED[2], SEED[3]], 0, 1) +check('reference helper exchanges two entries', ref == [SEED[2], SEED[1], SEED[3]]) + +# 3. the emitted statement reads before it writes and names both registers +stage = translate(G, ['v_swap_b32 v1, v2']) +check('text of one swap', stage.lines == ['{ uint t = v1; v1 = v2; v2 = t; }'], repr(stage.lines)) +check('both registers are marked used', {1, 2} <= stage.used_v, repr(stage.used_v)) +stage = translate(G, ['v_swap_b32 v1, v2'], exec_known=False) +check('unknown exec guards the swap', stage.lines == ['if (exec & 1) { uint t = v1; v1 = v2; v2 = t; }'], + repr(stage.lines)) + +# 4. uniformity travels with the value, and is dropped when exec is unknown +stage = translate(G, ['v_swap_b32 v1, v2'], uniform=(1,)) +check('uniform flag moves from v1 to v2', stage.uniform_v == {2}, repr(stage.uniform_v)) +stage = translate(G, ['v_swap_b32 v2, v1'], uniform=(1,)) +check('uniform flag moves with the operand order reversed', stage.uniform_v == {2}, repr(stage.uniform_v)) +stage = translate(G, ['v_swap_b32 v1, v2'], uniform=(1, 2)) +check('two uniform registers stay uniform', stage.uniform_v == {1, 2}, repr(stage.uniform_v)) +stage = translate(G, ['v_swap_b32 v1, v2'], exec_known=False, uniform=(1, 2)) +check('unknown exec drops the uniform proof', stage.uniform_v == set(), repr(stage.uniform_v)) + +# 5. refusals: every form llvm-mc rejects, and the operands the lowering does not model +for label, name, line in ( + ('clamp', 'v_swap_b32', 'v_swap_b32 v1, v2 clamp'), + ('omod', 'v_swap_b32', 'v_swap_b32 v1, v2 mul:2'), + ('op_sel', 'v_swap_b32', 'v_swap_b32 v1, v2 op_sel:[1,0]'), + ('scalar source', 'v_swap_b32', 'v_swap_b32 v1, s2'), + ('scalar destination', 'v_swap_b32', 'v_swap_b32 s2, v1'), + ('inline constant', 'v_swap_b32', 'v_swap_b32 v1, 1'), + ('negated source', 'v_swap_b32', 'v_swap_b32 v1, -v2'), + ('64-bit destination', 'v_swap_b32', 'v_swap_b32 v[1:2], v3'), + ('64-bit source', 'v_swap_b32', 'v_swap_b32 v1, v[2:3]'), + ('VGPR 256', 'v_swap_b32', 'v_swap_b32 v256, v1'), + ('one operand', 'v_swap_b32', 'v_swap_b32 v1'), + ('three operands', 'v_swap_b32', 'v_swap_b32 v1, v2, v3'), + ('dpp', 'v_swap_b32_dpp', 'v_swap_b32_dpp v1, v2 quad_perm:[1,0,3,2] row_mask:0xf bank_mask:0xf'), + ('sdwa', 'v_swap_b32_sdwa', 'v_swap_b32_sdwa v1, v2 dst_sel:DWORD dst_unused:UNUSED_PAD src0_sel:DWORD'), + ('decoder record of the VOP3A spelling', 'v_swap_b32_unsupported', 'v_swap_b32 ')): + message = refused(G, name, line) + check('refused: %s' % label, message is not None and not message.startswith('other exception'), repr(message)) +# the e64 words decode to the unsupported record, which has no handler +n, name, asm, _ = D.decode_instruction((0xd5e50001).to_bytes(4, 'little') + (0x102).to_bytes(4, 'little'), 0) +check('VOP3A words of v_swap_b32 decode to the unsupported record', (n, name) == (8, 'v_swap_b32_unsupported'), + repr((n, name, asm))) +check('...and that record is refused', refused(G, name, asm) is not None) + +# 6. mutation controls: load the translator with one wrong lowering and require the fixtures to fail +source = Path(G.__file__).read_text(encoding='utf-8') +check('control: the lowering line is in the translator once', source.count(LOWERING) == 1) +for label, wrong in MUTATIONS.items(): + mutated = {'__file__': G.__file__, '__name__': 'gfx2hlsl_mutated'} + exec(compile(source.replace(LOWERING, wrong), G.__file__, 'exec'), mutated) + + class Module: + Stage, Unsupported = mutated['Stage'], mutated['Unsupported'] + bad = run_checks(Module) + check('control: "%s" fails the fixtures (%d of %d)' % (label, len(bad), len(cases(G))), bool(bad)) + +# 7. optional: the source lines assemble to the words built here +if sys.argv[1:2] == ['binary']: + from startup_flat_param_check import assemble # noqa: E402 + for dst, src in ((1, 2), (3, 3), (255, 0)): + built = assemble(sys.argv[2], 'v_swap_b32 v%d, v%d\n' % (dst, src)) + check('llvm-mc v_swap_b32 v%d, v%d' % (dst, src), built == vop1_word(dst, src), built.hex()) + +print('v_swap_b32 checks: %d failed' % len(failures)) +sys.exit(1 if failures else 0) diff --git a/tools/startup/gfx2hlsl.py b/tools/startup/gfx2hlsl.py index 5df469afa..1ef59786f 100644 --- a/tools/startup/gfx2hlsl.py +++ b/tools/startup/gfx2hlsl.py @@ -1301,6 +1301,26 @@ def op_v_readfirstlane_b32(self, ops, mods): def op_v_mov_b32(self, ops, mods): self.set_v(reg_range(ops[0])[1], self.u(ops[1])) + def op_v_swap_b32(self, ops, mods): + # VOP1 only (llvm-mc rejects the e64, DPP and SDWA spellings for gfx1013). Both registers exchange + # their old values, so one is read into a temporary before either is written. The ISA allows no + # input or output modifier and no scalar or constant operand: both operands are plain VGPRs. + if mods: + raise Unsupported('v_swap_b32 with %s' % mods) + if len(ops) != 2: + raise Unsupported('v_swap_b32 with %d operands, expected 2' % len(ops)) + regs = [reg_range(o) for o in ops] + if any(not r or r[0] != 'v' or r[1] != r[2] or r[1] > 255 for r in regs): + raise Unsupported('v_swap_b32 operands %s, %s: expected two single VGPRs' % (ops[0], ops[1])) + dst, src = regs[0][1], regs[1][1] + body = '{ uint t = v%d; v%d = v%d; v%d = t; }' % (dst, dst, src, src) + # Uniformity travels with the value; under an unknown exec mask neither register keeps the proof. + uniform = (dst in self.uniform_v and self.exec_known, src in self.uniform_v and self.exec_known) + for reg, flag in zip((src, dst), uniform): + (self.uniform_v.add if flag else self.uniform_v.discard)(reg) + self.used_v.update((dst, src)) + self.emit(body if self.exec_known else 'if (exec & 1) ' + body) + def op_v_cndmask_b32(self, ops, mods): # The select takes no output modifier: a clamp or mul on it would change the value, so a # modifier that reaches here is refused rather than dropped from the emitted text.