From 5971d0110b982506d1fa18d8d0fa8be55608f475 Mon Sep 17 00:00:00 2001 From: RavenCurseBurg <360931527+RavenCurseBurg@users.noreply.github.com> Date: Wed, 25 Nov 2026 14:28:50 +0800 Subject: [PATCH] compute: expand packed D16 into FLOAT4 heap views --- docs/experiments.md | 6 ++++ tests/compute_heap_store_check.py | 56 ++++++++++++++++++++++++------- tools/compute_store_aot.py | 22 +++++++----- 3 files changed, 62 insertions(+), 22 deletions(-) diff --git a/docs/experiments.md b/docs/experiments.md index 031316c4fd..5692fa09f2 100644 --- a/docs/experiments.md +++ b/docs/experiments.md @@ -5260,6 +5260,12 @@ The view must be live and describe format 75 (`BUF_FMT_32_32_32_32_UINT`) with i Run `ctest --preset dev -R '^compute_(heap_store_controls|store_aot_heap_view_store_(warp|hardware))$' --output-on-failure`. The fixture checks all 320 readback words across twelve cases. These include distinct live ranges, one texel ending exactly at the u0 limit, and a partial texel that must leave the sentinel intact. Eight mutations exercise the base, stride, record count, format, lifetime, output extent and slot limit. Removing the per-index extent check lets the next thread write past the last complete texel. +### Packed D16 heap-view stores to FLOAT4 + +With the same `--heap-store-view` binding, gfx1013 `buffer_store_format_d16_xyzw` reads X/Y from the low/high halves of the first VGPR and Z/W from the second. A live identity `BUF_FMT_32_32_32_32_FLOAT` view (format 77) receives four binary32 words. Two source VGPRs still require 16 writable bytes in the destination. An ordinary UINT4 record selected by this opcode leaves the sentinel region untouched. [AMD RDNA 2 instruction set](https://www.amd.com/content/dam/amd/en/documents/radeon-tech-docs/instruction-set-architectures/rdna2-shader-instruction-set-architecture.pdf) ยท [HLSL half-to-float conversion](https://learn.microsoft.com/en-us/windows/win32/direct3dhlsl/f16tof32) + +The heap-store CTest selection above runs both programs. The D16 fixture checks twelve cases with 320 words each. Its reference includes consecutive half values starting at 1, a negative value, the smallest positive half subnormal and negative zero. Eleven compiled shader mutations check addressing, view selection and conversion, including using the low X half for Y, taking W from the wrong VGPR and writing packed values directly. + ### Heap-view buffer loads The gfx1013 compute store profile can now take the t0 V# of a `buffer_load_format_x/xy/xyz/xyzw` from a run-time view record instead of a static descriptor. `--heap-view SLOT_WORD TABLE_OFFSET COUNT BUFFER_BYTES` selects it. It is slice 2 of item A of #2022 and builds on the format decoder of [descriptor-format buffer loads](#descriptor-format-buffer-loads). It excludes `--load-format-constant`: the format now comes from the record. diff --git a/tests/compute_heap_store_check.py b/tests/compute_heap_store_check.py index 02141c86bb..e3f5080974 100644 --- a/tests/compute_heap_store_check.py +++ b/tests/compute_heap_store_check.py @@ -14,13 +14,21 @@ UINT4 = 0x0104BFAC # gfx10 format 75, identity XYZW selectors and RESOURCE_LEVEL set FLOAT4 = 0x0104DFAC STORE = "buffer_store_format_xyzw v[10:13], v8, s[20:23], 0 idxen offset:4" -PROGRAM = [ +D16_STORE = "buffer_store_format_d16_xyzw v[10:11], v8, s[20:23], 0 idxen offset:4" +SETUP = [ "s_mov_b32 s12, s7", "s_mov_b32 s13, 0", "s_load_dwordx4 s[20:23], s[12:13], 0x30", "s_load_dwordx4 s[16:19], s[12:13], 0x40", "v_lshl_add_u32 v8, s11, 2, v0", "s_waitcnt lgkmcnt(0)", "s_buffer_load_dwordx4 s[24:27], s[16:19], null", "s_waitcnt lgkmcnt(0)", +] +PROGRAM = SETUP + [ "v_mov_b32_e32 v10, v8", "v_mov_b32_e32 v11, 0x12345678", "v_mov_b32_e32 v12, 0x80000001", "v_mov_b32_e32 v13, 0x3f800001", STORE, "s_endpgm", ] +D16_PROGRAM = SETUP + [ + "v_xor_b32_e32 v10, 0xc0003c00, v8", "v_mov_b32_e32 v11, 0x80000001", D16_STORE, "s_endpgm", +] +# 0x3c00..0x3c03 are consecutive binary16 values starting at 1.0. +D16_X = (0x3f800000, 0x3f802000, 0x3f804000, 0x3f806000) # V# words 0..3 and live state, then the expected *word addresses* of the four component stores. # The addresses are literal to keep the reference independent of the generated address calculation. CASES = ( @@ -48,7 +56,8 @@ def lower(program=PROGRAM, **options): def controls(): assert base.aot.descriptor_format_word(75) == UINT4 # Pin the translator call here. draw() exercises the generated guards against actual UAV contents. - assert "store_heap_uint4(params[0].x, tid.x, 4u, uint4(tid.x," in lower() + assert "store_heap_words4(params[0].x, tid.x, 4u, false, uint4(tid.x," in lower() + assert "store_heap_words4(params[0].x, tid.x, 4u, true," in lower(D16_PROGRAM) refuses = base.rejects refuses("Unsupported formatted buffer store target or address mode", lower, heap_store_view=None) refuses("Invalid heap store view binding", lower, stride=24) @@ -61,6 +70,10 @@ def controls(): refuses("Heap store view slot constant is not ready", lower, PROGRAM[:7] + PROGRAM[8:]) refuses("Heap store view requires buffer_store_format_xyzw", lower, base.variant(PROGRAM, (STORE, STORE.replace("xyzw v[10:13]", "xy v[10:11]")))) + refuses("Heap store view requires buffer_store_format_xyzw", lower, + base.variant(D16_PROGRAM, (D16_STORE, D16_STORE.replace("d16_xyzw", "d16_xyz")))) + refuses("Unverified raw UAV store", lower, + base.variant(D16_PROGRAM, (D16_STORE, D16_STORE.replace("v[10:11]", "v[10:13]")))) refuses("Unsupported formatted buffer store target or address mode", lower, base.variant(PROGRAM, (STORE, STORE.replace(" idxen", " offen")))) refuses("Unverified raw UAV store", lower, base.variant(PROGRAM, (STORE, STORE.replace("s[20:23]", "s[16:19]")))) @@ -68,22 +81,25 @@ def controls(): base.variant(PROGRAM, (STORE, STORE.replace(", v8,", ", v12,")))) -def verify_binary(directory, code, library): +def verify_binary(directory, code, library, program=PROGRAM): decoded = base.aot.labelled(base.disassemble(code, library, offsets=True, cpu=b"gfx1013")) - assert lower(decoded) == lower() + expected = lower(program) + assert lower(decoded) == expected container, output = directory / "store.dxbc", directory / "store.hlsl" container.write_bytes(base.store_container(code, GROUP)) subprocess.run([sys.executable, "-B", base.aot.__file__, container, "--output", output, "--llvm", library, *base.binding_cli(BINDING), "--heap-store-view", *map(str, VIEW)], check=True) - assert output.read_text(encoding="ascii") == lower() + assert output.read_text(encoding="ascii") == expected -def draw(directory, compiler, renderer, backend): - source = lower() +def draw(directory, compiler, renderer, backend, d16): + source = lower(D16_PROGRAM if d16 else PROGRAM) table = [0] * WORDS for slot, (_, record, _) in enumerate(CASES): start = TABLE_BYTE // 4 + slot * 8 table[start:start + 5] = record + if d16: + table[start + 3] = FLOAT4 if record[3] == UINT4 else UINT4 hlsl, cso = directory / "store.hlsl", directory / "store.cso" def compile_shader(text): @@ -93,7 +109,9 @@ def compile_shader(text): def run(slot): result = [base.SENTINEL] * WORDS for thread, start in enumerate(CASES[slot][2]): - result[start:start + 4] = [thread, 0x12345678, 0x80000001, 0x3F800001] + # D16 y, z, w are -2, the smallest positive binary16 subnormal, and negative zero. + result[start:start + 4] = ([D16_X[thread], 0xc0000000, 0x33800000, 0x80000000] if d16 else + [thread, 0x12345678, 0x80000001, 0x3F800001]) case = directory / "store.case" case.write_bytes(struct.pack("<321I", 1, *result) + struct.pack("<16I", slot, *([0] * 15)) + struct.pack("<320I", *table)) @@ -111,16 +129,23 @@ def run(slot): ("uint stride = view.y >> 16u;", "uint stride = 24u;", 1), ("const uint bytes = 512u;", "const uint bytes = 1280u;", 4), ("if (index > (bytes - view.x - offset - 16u) / stride) return;", "", 3), - (f"view.w != {UINT4:#x}u || ", "", 5), + ("view.w != formatWord || ", "", 5), ("!= 1u) return;", "== 0u) return;", 2), (f"slot >= {VIEW[2]}u", f"slot > {VIEW[2]}u", VIEW[2]), ) + if d16: + mutations += ( + ("value.x >> 16u", "value.x", 0), + ("value.y >> 16u", "value.x >> 16u", 0), + ("if (d16) value =", "if (false) value =", 0), + ) for old, new, slot in mutations: assert source.count(old) == 1, old compile_shader(source.replace(old, new)) result = run(slot) assert result.returncode == 1 and "Compute readback differs" in result.stderr, (old, result.stderr) - print(f"{backend}: {len(CASES)} heap-store cases and {len(mutations)} shader mutations checked") + kind = "D16-to-FLOAT4" if d16 else "UINT4" + print(f"{backend} {kind}: {len(CASES)} heap-store cases and {len(mutations)} shader mutations checked") return 0 @@ -133,10 +158,15 @@ def run(slot): with tempfile.TemporaryDirectory() as tmp: directory = Path(tmp) if mode == "draw": - sys.exit(draw(directory, *args)) + for d16 in (False, True): + result = draw(directory, *args, d16) + if result: + sys.exit(result) + sys.exit(0) assembler, objcopy, library = args - code = base.assemble(directory, assembler, objcopy, PROGRAM, cpu="gfx1013") - verify_binary(directory, code, library) + for program in (PROGRAM, D16_PROGRAM): + code = base.assemble(directory, assembler, objcopy, program, cpu="gfx1013") + verify_binary(directory, code, library, program) else: sys.exit("usage: compute_heap_store_check.py controls | binary " "| draw ") diff --git a/tools/compute_store_aot.py b/tools/compute_store_aot.py index 725c4a9a8c..2ea40ee995 100644 --- a/tools/compute_store_aot.py +++ b/tools/compute_store_aot.py @@ -651,18 +651,20 @@ def heap_view_hlsl(table_offset, view_count, buffer_bytes): def heap_store_view_hlsl(table_offset, view_count, output_bytes): - """Store four UINT words through a live side-table record for the fixed raw u0 binding.""" - return f"""void store_heap_uint4(uint slot, uint index, uint offset, uint4 value) {{ + """Store raw UINT4 or four expanded half floats through the live side-table record.""" + return f"""void store_heap_words4(uint slot, uint index, uint offset, bool d16, uint4 value) {{ if (slot >= {view_count}u) return; uint record = {table_offset}u + slot * {HEAP_VIEW_RECORD_BYTES}u; if (inputData.Load(record + 16u) != {HEAP_VIEW_LIVE}u) return; uint4 view = inputData.Load4(record); - if (view.w != {descriptor_format_word(75):#x}u || (view.y & 0xc000ffffu) != 0u) return; + uint formatWord = d16 ? {descriptor_format_word(77):#x}u : {descriptor_format_word(75):#x}u; + if (view.w != formatWord || (view.y & 0xc000ffffu) != 0u) return; uint stride = view.y >> 16u; if (((view.x | stride) & 3u) != 0u || stride < offset + 16u || index >= view.z) return; const uint bytes = {output_bytes}u; if (view.x > bytes || offset + 16u > bytes - view.x) return; if (index > (bytes - view.x - offset - 16u) / stride) return; + if (d16) value = asuint(f16tof32(uint4(value.x, value.x >> 16u, value.y, value.y >> 16u))); outputData.Store4(view.x + index * stride + offset, value); }} """ @@ -693,7 +695,7 @@ def lower(instructions, group, *, table_reg, descriptor_offset=None, group_reg=N word index per formatted load, in program order, read by load_descriptor_format (see descriptor_load_hlsl). heap_view=(slot_word, table_offset, view_count, buffer_bytes) makes the same loads take the V# from a run-time view record instead of a constant format word (see heap_view_hlsl). It excludes load_format_constant. - heap_store_view uses the same t0 record layout for one indexed four-component UINT store to u0. + heap_store_view uses the same t0 record layout for one indexed UINT4 or packed D16-to-FLOAT4 store to u0. Its fifth argument is the writable byte extent of u0. Root UAV descriptors carry no length. gfx1013 uses RGBA32 FLOAT for full-mask image_store or image_load_mip. When both are used, image_srv_descriptor supplies the separate t0 descriptor. @@ -2459,8 +2461,8 @@ def lower(instructions, group, *, table_reg, descriptor_offset=None, group_reg=N if not idxen or offen: raise ValueError("Unsupported formatted buffer store target or address mode") if heap_store_view is not None: - if channels != "xyzw": - raise ValueError("Heap store view requires buffer_store_format_xyzw") + if channels not in ("xyzw", "d16_xyzw"): + raise ValueError("Heap store view requires buffer_store_format_xyzw or buffer_store_format_d16_xyzw") if not any(tag == ("constant_word", heap_slot_word) and reg not in pending for reg, tag in scalar.items()): raise ValueError("Heap store view slot constant is not ready") @@ -2504,7 +2506,7 @@ def lower(instructions, group, *, table_reg, descriptor_offset=None, group_reg=N # The format word is not known here, so a descriptor store reserves four bytes per # channel. d16_xyzw is two VGPRs and sixteen bytes. The caller-declared UINT path # still reserves one DWORD per data VGPR. - span = 4 * (components if descriptor_store else count) + span = 4 * (components if descriptor_store or heap_store_view is not None else count) address = f"{offset}u" addr = m["addr"] addr_regs = list(map(int, re.findall(r"\d+", addr))) @@ -2591,7 +2593,9 @@ def lower(instructions, group, *, table_reg, descriptor_offset=None, group_reg=N words[-1] = f"((outputData.Load({final_address}) & 0xffff0000u) | ({words[-1]} & 0xffffu))" if heap_store_view is not None: slot = hlsl_word(("constant_word", heap_slot_word)) - stores.append(f" store_heap_uint4({slot}, {address}, {offset}u, uint4({', '.join(words)}));") + supplied = words + ["0u"] * (4 - len(words)) + mode = "true" if packed else "false" + stores.append(f" store_heap_words4({slot}, {address}, {offset}u, {mode}, uint4({', '.join(supplied)}));") elif descriptor_store: supplied = words + ["0u"] * (4 - len(words)) stores.append(" store_descriptor_format(" @@ -2706,7 +2710,7 @@ def main(): "SLOT_WORD; the record table is at byte TABLE_OFFSET of the BUFFER_BYTES-byte t0 and holds COUNT views") parser.add_argument("--heap-store-view", nargs=5, type=lambda n: int(n, 0), metavar=("SLOT_WORD", "TABLE_OFFSET", "COUNT", "TABLE_BUFFER_BYTES", "OUTPUT_BYTES"), - help="select a UINT4 store view from the t0 table and bound writes by OUTPUT_BYTES of u0") + help="select a UINT4 or D16-to-FLOAT4 store view from t0 and bound writes by OUTPUT_BYTES of u0") parser.add_argument("--mubuf-d16-xyz-mode", choices=("preserve-w",), help="Experimental RGBA16 UINT store with caller-verified preservation of W") parser.add_argument("--image-descriptor", type=lambda n: int(n, 0),