diff --git a/docs/experiments/native-direct-fma-f16.md b/docs/experiments/native-direct-fma-f16.md index 1a6078e817..2450d2489c 100644 --- a/docs/experiments/native-direct-fma-f16.md +++ b/docs/experiments/native-direct-fma-f16.md @@ -6,16 +6,18 @@ - **Exact single-rounded FMA (`fused_half_rne()`):** Reuses the shared 32-bit integer `fused_half_rne(a, b, c)` helper (introduced in #3151 for `v_pk_fma_f16`) so `a * b + c` computes an exact product, aligns the addend with a sticky bit, and rounds once to nearest-even binary16 rather than double-rounding through binary32. -- **High-half preservation:** Every operation reads `16`-bit sources (preserving `S0..S2[15:0]` / `K[15:0]` / `D[15:0]`), +- **High-half preservation by default:** The default forms read `S0..S2[15:0]` / `K[15:0]` / `D[15:0]`, writes the result into `D[15:0]`, and keeps `D[31:16]` unchanged. - **Modifiers & MODE contract:** Supports VOP3 `abs`, `neg`, and `clamp` (`[0.0, 1.0]` saturation); refuses DPP, SDWA, - `omod`, high-half `op_sel`, third-source modifiers/`src2` on `v_fmac_f16_e64`, and non-default FP16 MODE writes + `omod`, high-half `op_sel` and third-source modifiers/`src2` on `v_fmac_f16_e64`, and non-default FP16 MODE writes (`half_binary` in `DirectCompute::lower()`). - **Test adjustment:** Updated the unlowered-instruction refusal placeholder in [`tests/native_f16_alu_check.py`](../../tests/native_f16_alu_check.py) from `v_fmac_f16` to `v_pk_fmac_f16`, and added `@dx.op.unaryBits.i32(i32 33, ...)` (`FirstbitHi`) evaluation to its IR interpreter so `native_fma_f16_ir` evaluates `fused_half_rne()` directly. +Direct compute also accepts VOP3 `v_fma_f16` with `op_sel`. Bits 0 to 2 select the high half of the respective VGPR source before abs and neg. Bit 3 selects the high destination half and preserves its low half. A clear destination selector writes the low half and preserves the high half. Selected non-VGPR sources and clamp combined with `op_sel` are refused. The [half-selection fixture](../../tests/native_fma_f16_opsel_check.py) checks six captured forms in IR and direct GPU replay. + ## Fixture [`tests/native_fma_f16_check.py`](../../tests/native_fma_f16_check.py) via diff --git a/src/gfx1013_direct_compute.inl b/src/gfx1013_direct_compute.inl index b788b763d4..9e4cf20ce0 100644 --- a/src/gfx1013_direct_compute.inl +++ b/src/gfx1013_direct_compute.inl @@ -1606,8 +1606,13 @@ struct DirectCompute { refuse(i.pc, "unsupported " + string(n) + " SDWA form"); if (i.scale) refuse(i.pc, "unsupported " + string(n) + " omod"); - if (i.wide && (i.a & 0x7800)) + const unsigned op_sel = i.wide ? (i.a >> 11) & 15u : 0; + if (op_sel && (n != "v_fma_f16" || i.enc != Encoding::Vop3 || i.clamp)) refuse(i.pc, "unsupported " + string(n) + " high-half op_sel"); + for (unsigned j = 0; j < 3; ++j) { + if ((op_sel & (1u << j)) && i.src[j] < 256) + refuse(i.pc, "unsupported v_fma_f16 high-half op_sel on non-VGPR source"); + } if (n == "v_fmac_f16" && i.wide && (((i.abs | i.neg) & 4) || i.src[2])) refuse(i.pc, "unsupported v_fmac_f16 third-source modifier"); if (!half_mode_known || ((mode_bits ^ 0xf0u) & 0x8000ccu)) @@ -1615,7 +1620,9 @@ struct DirectCompute { auto half_bits = [&](unsigned j) { const unsigned r = i.src[j]; string bits; - if (r >= 240 && r <= 248) { + if (op_sel & (1u << j)) { + bits = ir.binary("lshr", operand32(i, r), "16"); + } else if (r >= 240 && r <= 248) { constexpr unsigned halves[]{0x3800, 0xb800, 0x3c00, 0xbc00, 0x4000, 0xc000, 0x4400, 0xc400, 0x3118}; bits = to_string(halves[r - 240]); @@ -1645,8 +1652,10 @@ struct DirectCompute { auto low = ir.value("or i1 " + nan + ", " + cmp("ne", ir.binary("and", result, "32768"), "0")); result = select(low, "0", select(cmp("ugt", result, "15359"), "15360", result)); } - auto kept = ir.binary("and", read(i.dst), "4294901760"); - vector_write(i.dst, ir.binary("or", kept, ir.binary("and", result, "65535"))); + const bool dst_high = (op_sel & 8u) != 0; + auto kept = ir.binary("and", read(i.dst), dst_high ? "65535" : "4294901760"); + auto placed = dst_high ? ir.binary("shl", result, "16") : ir.binary("and", result, "65535"); + vector_write(i.dst, ir.binary("or", kept, placed)); return true; } const auto rule = alu_rules.find(n); diff --git a/tests/cmake/native_fma_f16_opsel.cmake b/tests/cmake/native_fma_f16_opsel.cmake new file mode 100644 index 0000000000..56692ba2ea --- /dev/null +++ b/tests/cmake/native_fma_f16_opsel.cmake @@ -0,0 +1,14 @@ +# Sorts after native_f16_alu.cmake, which defines native_compute_ir_dump. +if(TARGET native_compute_ir_dump AND EXISTS "${INTAKE_PYTHON}") + add_test(NAME native_fma_f16_opsel_ir COMMAND "${INTAKE_PYTHON}" -B + "${CMAKE_CURRENT_SOURCE_DIR}/tests/native_fma_f16_opsel_check.py" ir + "$") + set_tests_properties(native_fma_f16_opsel_ir PROPERTIES TIMEOUT 60) +endif() +if(TARGET native_store_replay AND EXISTS "${INTAKE_PYTHON}" AND + EXISTS "${DXC_BIN_DIR}/dxcompiler.dll" AND EXISTS "${DXC_BIN_DIR}/dxil.dll") + add_test(NAME native_fma_f16_opsel_hardware COMMAND "${INTAKE_PYTHON}" -B + "${CMAKE_CURRENT_SOURCE_DIR}/tests/native_fma_f16_opsel_check.py" hardware + "$" "${DXC_BIN_DIR}") + set_tests_properties(native_fma_f16_opsel_hardware PROPERTIES SKIP_RETURN_CODE 77 TIMEOUT 60) +endif() diff --git a/tests/native_fma_f16_check.py b/tests/native_fma_f16_check.py index afb4d8e704..7eb4c24b53 100644 --- a/tests/native_fma_f16_check.py +++ b/tests/native_fma_f16_check.py @@ -206,7 +206,7 @@ def refusal_programs(): (F.vop2(OP_FMAC, 3, 249, 2) + [0x06060601, 0xBF810000], 'unimplemented direct SDWA operand profile'), (vop3_tern(OP_FMA_VOP3, 3, 257, 258, 260, omod=1) + [0xBF810000], 'unsupported v_fma_f16 omod'), (vop3_tern(OP_FMAC_VOP3, 3, 257, 258, 0, omod=1) + [0xBF810000], 'unsupported v_fmac_f16 omod'), - (vop3_tern(OP_FMA_VOP3, 3, 257, 258, 260, op_sel=1) + [0xBF810000], 'unsupported v_fma_f16 high-half op_sel'), + (vop3_tern(OP_FMA_VOP3, 3, 244, 258, 260, op_sel=1) + [0xBF810000], 'unsupported v_fma_f16 high-half op_sel on non-VGPR source'), (vop3_tern(OP_FMAC_VOP3, 3, 257, 258, 0, op_sel=8) + [0xBF810000], 'unsupported v_fmac_f16 high-half op_sel'), (vop3_tern(OP_FMAC_VOP3, 3, 257, 258, 0, neg=4) + [0xBF810000], 'unsupported v_fmac_f16 third-source modifier'), (vop3_tern(OP_FMAC_VOP3, 3, 257, 258, 260) + [0xBF810000], 'unsupported v_fmac_f16 third-source modifier'), diff --git a/tests/native_fma_f16_opsel_check.py b/tests/native_fma_f16_opsel_check.py new file mode 100644 index 0000000000..83e9b5f4ba --- /dev/null +++ b/tests/native_fma_f16_opsel_check.py @@ -0,0 +1,98 @@ +"""Direct VOP3 v_fma_f16 source and destination half selection.""" +from pathlib import Path +import struct +import subprocess +import sys +import tempfile + +import f16_packed_model as model +from native_compute_clear_check import write_request +import native_f16_alu_check as F +from native_fma_f16_check import OP_FMA_VOP3, OP_FMAC, vop3_tern + + +SOURCES = (0x40003C00, 0x44004200, 0x4C004500) # (high, low): (2, 1), (4, 3), (16, 5) +DST = 0xABCD1234 +END = 0xBF810000 + + +def fma(srcs, op_sel, *, clamp=0): + return vop3_tern(OP_FMA_VOP3, 3, *srcs, op_sel=op_sel, clamp=clamp) + + +def program(): + # s5 supplies the group X ID (0 or 1). Lane 0 alone writes each dword. + # Case i in group g stores at byte 512*g + 4*i relative to the UAV window, + # which starts at readback word 8, so the file word index is 8 + 128*g + i. + # The preamble sets EXEC to 1 before the per-lane VGPR initialization. + code = F.preamble() + F.mov(1, SOURCES[0]) + F.mov(2, SOURCES[1]) + F.mov(4, SOURCES[2]) + want = [] + for op_sel in (1, 2, 3, 15, 14, 12, 8): + srcs = (257, 258, 260) + words = SOURCES + if op_sel == 8: + srcs = (244, 258, 243) # inline binary16 2.0 and -1.0 + words = (0x4000, SOURCES[1], 0xBC00) + halves = [model.pick(word, op_sel & (1 << j)) for j, word in enumerate(words)] + result = model.half_op('fma', *halves) + code += F.mov(3, DST) + fma(srcs, op_sel) + F.store(4 * len(want), 3) + want.append(((result << 16) | (DST & 0xFFFF)) if op_sel & 8 else ((DST & 0xFFFF0000) | result)) + assert want == [0xABCD4980, 0xABCD4880, 0xABCD4A80, 0x4E001234, 0x4D001234, 0x4CC01234, 0x45001234] + return code + [END], want + + +def refusals(): + return ( + (F.smov(10, SOURCES[0]) + fma((10, 258, 260), 1), + 'unsupported v_fma_f16 high-half op_sel on non-VGPR source'), + (F.vop2(OP_FMAC, 3, 250, 2) + [0xFF00B101], 'unsupported DPP arithmetic'), + (fma((257, 258, 260), 8, clamp=1), 'unsupported v_fma_f16 high-half op_sel'), + ) + + +def ir(dumper, work, code, want): + run = F.dump(dumper, work, code, 'fma-f16-opsel') + assert run.returncode == 0, run.stderr + stores = sorted(F.interpret(run.stdout)) + assert stores == [(4 * i, word) for i, word in enumerate(want)], stores + for i, (snippet, why) in enumerate(refusals()): + run = F.dump(dumper, work, F.preamble() + snippet + [END], f'refused-{i}') + assert run.returncode != 0 and why in run.stderr, (why, run.returncode, run.stderr) + print(f'native_fma_f16_opsel IR matched {len(want)} cases and {len(refusals())} refusals') + return 0 + + +def hardware(replay, sdk, work, code, want): + shader, root = work / 'root.hlsl', work / 'root.bin' + shader.write_text('#define SIG "DescriptorTable(UAV(u0,numDescriptors=8))"') + subprocess.run([Path(sdk) / 'dxc.exe', '-T', 'rootsig_1_1', '-E', 'SIG', '-Fo', root, shader], check=True) + # .sa dword 4, bit 20 requests wave32. + sa = struct.pack('<7I', 0, 0, 0, 32 | (1 << 16), 1 | (1 << 20), 0, 0x8A) + marker = write_request(work, code, root.read_bytes(), sa) + output = work / 'out.bin' + run = subprocess.run([replay, sdk, marker, work / 'cache', '0', output, '--direct']) + if run.returncode == 77: + return 77 + run.check_returncode() + got = struct.unpack(f'<{F.WORDS}I', output.read_bytes()) + expected = {slot: value for slots in F.group_offsets(len(want)) + for slot, value in zip(slots, want)} + bad = [(i, hex(word), hex(expected.get(i, F.SENTINEL))) + for i, word in enumerate(got) if word != expected.get(i, F.SENTINEL)] + assert not bad, bad[:8] + print(f'native_fma_f16_opsel hardware matched {len(want)} cases across {F.GROUPS} groups') + return 0 + + +def main(mode, tool, sdk=None): + code, want = program() + with tempfile.TemporaryDirectory() as tmp: + work = Path(tmp) + if mode == 'ir': + return ir(tool, work, code, want) + assert mode == 'hardware' and sdk is not None + return hardware(tool, sdk, work, code, want) + + +if __name__ == '__main__': + sys.exit(main(*sys.argv[1:]))