Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions docs/experiments.md
Original file line number Diff line number Diff line change
Expand Up @@ -5784,14 +5784,18 @@ Run `ctest --preset dev -R '^compute_(heap_store_controls|store_aot_heap_view_st

<a id="packed-d16-heap-view-stores-to-float4-and-float1"></a>

### Packed D16 heap-view stores to FLOAT1, FLOAT2 and FLOAT4
<a id="packed-d16-heap-view-stores-to-float1-float2-and-float4"></a>

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. `buffer_store_format_d16_x` takes one source VGPR, converts its low 16 bits (`& 0xffffu`) via `f16tof32`, and writes one 4-byte binary32 word into a live identity `BUF_FMT_32_FLOAT` view (format 22, `0x01016fac`), enforcing 4-byte offset/stride/base alignment and `(bytes - view.x - offset - 4u) / stride` bounds without a raw UAV read-modify-write. A view whose format differs from the required component count or numeric type leaves the output untouched. The packed three-component `d16_xyz` form is refused. [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)
### Packed D16 heap-view stores

The heap-store CTest selection above runs all four programs (`UINT4`, `D16-to-FLOAT4`, `D16_X-to-FLOAT1` and `D16_XY-to-FLOAT2`). Each D16 fixture checks 320 words across its heap-view cases (with `D16_X` additionally checking four binary16 payload patterns with poisoned high halves: `1.0`..`1.00293`, `-2.0`.., minimum positive subnormal + `+0.0`, and `-0.0` + negative subnormals, plus `UINT1` and `FLOAT4` mismatch rejections and untouched adjacent sentinel dwords). Eleven compiled shader mutations check addressing, view selection, and half-float extraction, including swapping high for low bits, checking the wrong format word, and writing packed values directly.
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. `buffer_store_format_d16_x` takes one source VGPR, converts its low 16 bits (`& 0xffffu`) via `f16tof32`, and writes one 4-byte binary32 word into a live identity `BUF_FMT_32_FLOAT` view (format 22, `0x01016fac`), enforcing 4-byte offset/stride/base alignment and `(bytes - view.x - offset - 4u) / stride` bounds without a raw UAV read-modify-write. A view whose format differs from the required component count or numeric type leaves the output 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 the UINT4 program and all four packed D16 widths. Each D16 fixture checks 320 words across its heap-view cases (with `D16_X` additionally checking four binary16 payload patterns with poisoned high halves: `1.0`..`1.00293`, `-2.0`.., minimum positive subnormal + `+0.0`, and `-0.0` + negative subnormals, plus `UINT1` and `FLOAT4` mismatch rejections and untouched adjacent sentinel dwords). Eleven compiled shader mutations check addressing, view selection, and half-float extraction, including swapping high for low bits, checking the wrong format word, and writing packed values directly.

For `buffer_store_format_d16_xy`, the low and high halves of one VGPR become X and Y of an identity `BUF_FMT_32_32_FLOAT` view (format 64, `0x01040fac`). One `Store2` writes the two binary32 values. With a 512-byte u0 and an instruction offset of four, a base of 500 writes words 126 and 127. A base of 504 leaves all 320 readback words untouched. The fixture also checks negative zero, the smallest positive binary16 subnormal and −2.0 in Y. Swapping the halves, duplicating X into Y and admitting a partial element must each produce its own first mismatched word.

With `buffer_store_format_d16_xyz`, X and Y come from the first VGPR and Z comes from the low half of the second. A live identity `BUF_FMT_32_32_32_FLOAT` view (format 74, `0x0104afac`) receives three 32-bit float words. The readback changes the unused high half independently and checks the guard immediately after Z. With a four-byte instruction offset and a 512-byte limit, base 496 writes words 125–127 and base 500 leaves every word untouched. The fixture also checks that two- and four-channel FLOAT views and a three-channel UINT view leave u0 unchanged.

### 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.
Expand Down
79 changes: 64 additions & 15 deletions tests/compute_heap_store_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
FLOAT1 = 0x01016FAC # gfx10 format 22 (BUF_FMT_32_FLOAT), identity selectors and RESOURCE_LEVEL set
UINT1 = 0x01014FAC # gfx10 format 20 (BUF_FMT_32_UINT)
FLOAT2 = 0x01040FAC # gfx10 format 64 (BUF_FMT_32_32_FLOAT)
FLOAT3 = 0x0104AFAC # gfx10 format 74 (BUF_FMT_32_32_32_FLOAT)
UINT3 = 0x01048FAC # gfx10 format 72 (BUF_FMT_32_32_32_UINT)
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"
Expand All @@ -36,6 +38,9 @@
]
D16_XY_STORE = D16_X_STORE.replace("d16_x ", "d16_xy ")
D16_XY_PROGRAM = base.variant(D16_X_PROGRAM, (D16_X_STORE, D16_XY_STORE))
D16_XYZ_STORE = D16_STORE.replace("d16_xyzw", "d16_xyz")
D16_XYZ_PROGRAM = base.variant(D16_PROGRAM, (D16_STORE, D16_XYZ_STORE),
("v_mov_b32_e32 v11, 0x80000001", "v_mov_b32_e32 v11, s25"))
# 0x3c00..0x3c03 are consecutive binary16 values starting at 1.0.
D16_X = (0x3f800000, 0x3f802000, 0x3f804000, 0x3f806000)
# High half 0x4200 poisons each word so leaking high bits or missing & 0xffffu shifts fails readback.
Expand Down Expand Up @@ -92,6 +97,21 @@
("base past the declared extent", (640, 12 << 16, 4, FLOAT2, 1), ()),
("live slot beyond the declared count", (12, 12 << 16, 4, FLOAT2, 1), ()),
)
# Each accepted three-dword texel has an adjacent guard, including the one at the declared u0 limit.
D16_XYZ_CASES = (
("four float3 records", (12, 16 << 16, 4, FLOAT3, 1), (4, 8, 12, 16)),
("second base and short count", (116, 20 << 16, 2, FLOAT3, 1), (30, 35)),
("released view", (12, 16 << 16, 4, FLOAT3, 2), ()),
("last complete float3", (496, 16 << 16, 4, FLOAT3, 1), (125,)),
("only eight bytes left", (500, 16 << 16, 4, FLOAT3, 1), ()),
("wrong UINT3 format", (12, 16 << 16, 4, UINT3, 1), ()),
("short stride", (12, 12 << 16, 4, FLOAT3, 1), ()),
("misaligned base", (14, 16 << 16, 4, FLOAT3, 1), ()),
("misaligned stride", (12, 18 << 16, 4, FLOAT3, 1), ()),
("unsupported high address", (12, (16 << 16) | 1, 4, FLOAT3, 1), ()),
("base past the declared extent", (640, 16 << 16, 4, FLOAT3, 1), ()),
("live slot beyond the declared count", (12, 16 << 16, 4, FLOAT3, 1), ()),
)
BINDING = dict(table_reg=7, descriptor_offset=0x30, constant_descriptor=0x40, source_descriptor=0x50, group_reg=11)


Expand All @@ -104,6 +124,8 @@ def controls():
assert base.aot.descriptor_format_word(22) == FLOAT1
assert base.aot.descriptor_format_word(20) == UINT1
assert base.aot.descriptor_format_word(64) == FLOAT2
assert base.aot.descriptor_format_word(74) == FLOAT3
assert base.aot.descriptor_format_word(72) == UINT3
# Pin the translator call here. draw() exercises the generated guards against actual UAV contents.
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)
Expand All @@ -129,8 +151,13 @@ def controls():
refuses("Unverified ALU operand", lower, D16_X_PROGRAM[:7] + D16_X_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]"))))
xyz = lower(D16_XYZ_PROGRAM)
assert f"{FLOAT3:#x}u" in xyz and "outputData.Store3(" in xyz and "outputData.Load(" not in xyz
for registers in ("v10", "v[10:12]"):
refuses("Unverified raw UAV store", lower,
base.variant(D16_XYZ_PROGRAM, (D16_XYZ_STORE, D16_XYZ_STORE.replace("v[10:11]", registers))))
refuses("Heap store view requires buffer_store_format_xyzw", lower,
base.variant(D16_PROGRAM, (D16_STORE, D16_STORE.replace("d16_xyzw", "d16_xyz"))))
base.variant(PROGRAM, (STORE, STORE.replace("xyzw v[10:13]", "xyz v[10:12]"))))
xy = lower(D16_XY_PROGRAM)
assert f"{FLOAT2:#x}u" in xy and "outputData.Store2(" in xy and "outputData.Load(" not in xy
refuses("Unverified raw UAV store", lower,
Expand Down Expand Up @@ -166,8 +193,9 @@ def verify_binary(directory, code, library, program=PROGRAM):


def draw(directory, compiler, renderer, backend, mode):
program = {"uint4": PROGRAM, "d16_xyzw": D16_PROGRAM, "d16_x": D16_X_PROGRAM, "d16_xy": D16_XY_PROGRAM}[mode]
cases = {"d16_x": D16_X_CASES, "d16_xy": D16_XY_CASES}.get(mode, CASES)
program = {"uint4": PROGRAM, "d16_xyzw": D16_PROGRAM, "d16_x": D16_X_PROGRAM,
"d16_xy": D16_XY_PROGRAM, "d16_xyz": D16_XYZ_PROGRAM}[mode]
cases = {"d16_x": D16_X_CASES, "d16_xy": D16_XY_CASES, "d16_xyz": D16_XYZ_CASES}.get(mode, CASES)
source = lower(program)
table = [0] * WORDS
for slot, (_, record, _) in enumerate(cases):
Expand All @@ -181,13 +209,18 @@ def compile_shader(text):
hlsl.write_text(text, encoding="ascii")
subprocess.run([compiler, "-T", "cs_6_0", "-E", "main", "-Fo", cso, hlsl], check=True)

def run(slot, payload=D16_X_PAYLOADS[0][1], expected_x=D16_X, table_words=table, expected_y=0x40400000):
default_payload = 0x42000001 if mode == "d16_xyz" else D16_X_PAYLOADS[0][1]

def run(slot, payload=default_payload, expected_x=D16_X, table_words=table, expected_y=0x40400000,
expected_z=0x33800000):
result = [base.SENTINEL] * WORDS
for thread, start in enumerate(cases[slot][2]):
if mode == "d16_x":
result[start] = expected_x[thread]
elif mode == "d16_xy":
result[start:start + 2] = [expected_x[thread], expected_y]
elif mode == "d16_xyz":
result[start:start + 3] = [D16_X[thread], 0xc0000000, expected_z]
elif mode == "d16_xyzw":
# D16 y, z, w are -2, the smallest positive binary16 subnormal, and negative zero.
result[start:start + 4] = [D16_X[thread], 0xc0000000, 0x33800000, 0x80000000]
Expand Down Expand Up @@ -224,15 +257,22 @@ def run(slot, payload=D16_X_PAYLOADS[0][1], expected_x=D16_X, table_words=table,
result = run(0, payload=payload, expected_y=expected_y)
assert result.returncode == 0, (hex(payload), result.stderr)
extra += 1
for wrong_format in (FLOAT4, base.aot.descriptor_format_word(62)): # UINT2
wrong_view = table.copy()
wrong_view[TABLE_BYTE // 4 + 5 * 8 + 3] = wrong_format
result = run(5, table_words=wrong_view)
assert result.returncode == 0, (hex(wrong_format), result.stderr)
elif mode == "d16_xyz":
for payload, z in ((0x7e010001, 0x33800000), (0x3c008000, 0x80000000), (0x4200c000, 0xc0000000)):
result = run(0, payload=payload, expected_z=z)
assert result.returncode == 0, (hex(payload), result.stderr)
extra += 1
width_str = {"d16_x": "4u", "d16_xy": "8u"}.get(mode, "16u")
format_guard = {"d16_x": f"{FLOAT1:#x}u", "d16_xy": f"{FLOAT2:#x}u"}.get(mode, "formatWord")
wrong_stride = {"d16_x": 8, "d16_xy": 12}.get(mode, 24)
mismatched_formats = {"d16_xy": (FLOAT4, base.aot.descriptor_format_word(62)), "d16_xyz": (FLOAT2, FLOAT4)}
for wrong_format in mismatched_formats.get(mode, ()):
wrong_view = table.copy()
wrong_view[TABLE_BYTE // 4 + 5 * 8 + 3] = wrong_format
result = run(5, table_words=wrong_view)
assert result.returncode == 0, (hex(wrong_format), result.stderr)
extra += 1
width_str = {"d16_x": "4u", "d16_xy": "8u", "d16_xyz": "12u"}.get(mode, "16u")
format_guard = {"d16_x": f"{FLOAT1:#x}u", "d16_xy": f"{FLOAT2:#x}u",
"d16_xyz": f"{FLOAT3:#x}u"}.get(mode, "formatWord")
wrong_stride = {"d16_x": 8, "d16_xy": 12, "d16_xyz": 16}.get(mode, 24)
mutations = (
("view.x + index * stride + offset", "index * stride + offset", 0),
(" || index >= view.z", "", 1),
Expand Down Expand Up @@ -263,6 +303,15 @@ def run(slot, payload=D16_X_PAYLOADS[0][1], expected_x=D16_X, table_words=table,
("offset + 8u > bytes - view.x", "offset + 4u > bytes - view.x", 4,
"word=127 expected=c5c5c5c5 actual=3f800000"),
)
elif mode == "d16_xyz":
mutations += (
("value.y & 0xffffu", "value.y >> 16u", 0, "word=6 expected=33800000 actual=40400000"),
("Store3(view.x + index * stride + offset, asuint(f16tof32(halfWords)))",
"Store4(view.x + index * stride + offset, uint4(asuint(f16tof32(halfWords)), 0u))", 0,
"word=7 expected=c5c5c5c5 actual=00000000"),
("offset + 12u > bytes - view.x", "offset + 8u > bytes - view.x", 4,
"word=126 expected=c5c5c5c5 actual=3f800000"),
)
for old, new, slot, *first_mismatch in mutations:
assert source.count(old) == 1, old
compile_shader(source.replace(old, new))
Expand All @@ -272,7 +321,7 @@ def run(slot, payload=D16_X_PAYLOADS[0][1], expected_x=D16_X, table_words=table,
words = [line for line in result.stderr.splitlines() if line.startswith("word=")]
assert words[:1] == first_mismatch, (old, result.stderr)
kind = {"uint4": "UINT4", "d16_xyzw": "D16-to-FLOAT4", "d16_x": "D16_X-to-FLOAT1",
"d16_xy": "D16_XY-to-FLOAT2"}[mode]
"d16_xy": "D16_XY-to-FLOAT2", "d16_xyz": "D16_XYZ-to-FLOAT3"}[mode]
print(f"{backend} {kind}: {len(cases) + extra} heap-store cases and {len(mutations)} shader mutations checked")
return 0

Expand All @@ -286,13 +335,13 @@ def run(slot, payload=D16_X_PAYLOADS[0][1], expected_x=D16_X, table_words=table,
with tempfile.TemporaryDirectory() as tmp:
directory = Path(tmp)
if mode == "draw":
for kind in ("uint4", "d16_xyzw", "d16_x", "d16_xy"):
for kind in ("uint4", "d16_xyzw", "d16_x", "d16_xy", "d16_xyz"):
result = draw(directory, *args, kind)
if result:
sys.exit(result)
sys.exit(0)
assembler, objcopy, library = args
for program in (PROGRAM, D16_PROGRAM, D16_X_PROGRAM, D16_XY_PROGRAM):
for program in (PROGRAM, D16_PROGRAM, D16_X_PROGRAM, D16_XY_PROGRAM, D16_XYZ_PROGRAM):
code = base.assemble(directory, assembler, objcopy, program, cpu="gfx1013")
verify_binary(directory, code, library, program)
else:
Expand Down
13 changes: 10 additions & 3 deletions tools/compute_store_aot.py
Original file line number Diff line number Diff line change
Expand Up @@ -716,6 +716,11 @@ def heap_store_view_hlsl(table_offset, view_count, output_bytes, *, components=4
else:
write = (" outputData.Store2(view.x + index * stride + offset, "
"asuint(f16tof32(uint2(value & 0xffffu, value >> 16u))));")
elif components == 3:
sig = "void store_heap_xyz_d16(uint slot, uint index, uint offset, uint2 value)"
fmt_check = f" if (view.w != {descriptor_format_word(74):#x}u || (view.y & 0xc000ffffu) != 0u) return;"
write = (" uint3 halfWords = uint3(value.x & 0xffffu, value.x >> 16u, value.y & 0xffffu);\n"
" outputData.Store3(view.x + index * stride + offset, asuint(f16tof32(halfWords)));")
else:
sig = "void store_heap_words4(uint slot, uint index, uint offset, bool d16, uint4 value)"
fmt_check = (f" uint formatWord = d16 ? {descriptor_format_word(77):#x}u : {descriptor_format_word(75):#x}u;\n"
Expand Down Expand Up @@ -2738,8 +2743,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 not in ("xyzw", "d16_xyzw", "d16_xy", "d16_x"):
raise ValueError("Heap store view requires buffer_store_format_xyzw or a D16 x, xy or xyzw store")
if channels != "xyzw" and not packed:
raise ValueError("Heap store view requires buffer_store_format_xyzw or a D16 formatted store")
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")
Expand Down Expand Up @@ -2873,6 +2878,8 @@ def lower(instructions, group, *, table_reg, descriptor_offset=None, group_reg=N
heap_store_components = components
if channels in ("d16_x", "d16_xy"):
stores.append(f" store_heap_word_d16({slot}, {address}, {offset}u, {words[0]});")
elif channels == "d16_xyz":
stores.append(f" store_heap_xyz_d16({slot}, {address}, {offset}u, uint2({', '.join(words)}));")
else:
supplied = words + ["0u"] * (4 - len(words))
mode = "true" if packed else "false"
Expand Down Expand Up @@ -3003,7 +3010,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 or expanded D16 FLOAT1/FLOAT2/FLOAT4 store view from t0 "
help="select a UINT4 or expanded D16 FLOAT1/FLOAT2/FLOAT3/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")
Expand Down
Loading