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
24 changes: 24 additions & 0 deletions docs/experiments/native-global-srv-heap-replay.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,18 @@ ctest --preset dev --no-tests=error -R "^native_global_srv_heap_replay(_warp|_ha
load (`buffer_load_format_*`) through the heap descriptor. After the change, the test passes with 84 assertions. The
run against the earlier commit `7582c01d` fails at the `window_loop` shape (old text "heap byte window in a program
with a backward branch"). The run stops there.
- Formatted loads through the byte window (#3023): `tbuffer_load_format_*` through the rewritten descriptor now reads
the same raw view as `buffer_load_dword` (`src/gfx1013_compute_mtbuf.inl` picks `byte_window_srv` when word 1 is the
stride-cleared word). Before, it was refused with "non-contiguous resource descriptor". On the local capture with the
raw contract forced and a typed heap load prototyped (both uncommitted), 861 of the 2,689 requests stopped first at
this refusal; with the change they move on to other, already-known refusals. `native_global_srv_heap_replay_hardware`
passes with 94 assertions (record, compile and replay of a 32_UINT `tbuffer_load_format_x` through the heap window
for selections 0 and 1, the cross-slot mismatch control, the loop guard for the formatted load, and a compile-only
8_8_8_8_UNORM case). `native_mtbuf_hardware` repeats its 11-format value check through a byte window of its table SRV
and matches, including the element at the end of the view that returns 0 (`tests/native_store_replay.cpp` accepts
the window binding as the same raw view). `native_direct_container` compiles both formats through a compute table
window and refuses the window without the byte-count product. Against `837728dd` the heap check fails at the
`twindow_loop` shape (text "non-contiguous resource descriptor").

## Limits

Expand All @@ -132,6 +144,18 @@ ctest --preset dev --no-tests=error -R "^native_global_srv_heap_replay(_warp|_ha
byte-address rewrite consumes it. The fixture's descriptor words are never evaluated, so a stride above 1 and the
size rule of the contract are not exercised. The HLSL path's `heap_stride_srv` (`src/gfx1013_compute_native.cpp`,
used by `tests/native_compute_grid.cpp`) is unchanged.
- A formatted load (`tbuffer_load_format_*`) bounds its element with the size of the desktop raw view
(`getDimensions`), not with the guest window's byte count, and returns 0 for the whole element when it does not fit.
A runtime that binds a byte window, as a table view or as a GPU-selected heap slot, must create the raw view with
exactly the guest window's byte count. The descriptor's DST_SEL (word 3) is not applied: only the identity channel
order is translated, and it is not checked. In the startup dispatch, the in-process route
(`translate_compute_natively`, `src/startup_stubs/d3d12_compute_dispatch.inl`) binds only `raw_vector_srv` inputs
and falls through to the manifest route for a byte window. A published manifest names a table byte window as
`srv <i> buf table <param> <byte>`, as for the raw byte window; the dispatch then binds the table descriptor as a raw
view of
`max(stride, 1) * num_records` bytes (`indirect_table_buffer`, `src/startup_stubs/d3d12_draw.inl`), the guest
window's byte count, or of the title's own view size for a view token. No fixture runs this manifest path for a
window.
- Typed loads (`buffer_load_format_*`) through a heap descriptor and scalar loads through the rewritten descriptor stay
refused. The startup runtime
does not pass a contract yet, so none of this reaches a title run (#3023).
Expand Down
9 changes: 7 additions & 2 deletions src/gfx1013_compute_mtbuf.inl
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
// MTBUF uses the instruction's explicit format and the descriptor's byte range.
// Format numbers follow tools/disasm_rdna2.py MTBUF_FORMATS. Conversion and
// unaligned addressing follow gfx2hlsl.tbuffer / ldu, cross-checked against
// Victory 9376fd6 format_load. Only the runtime-checked identity raw SRV is used.
// Victory 9376fd6 format_load. Only the runtime-checked identity raw SRV is used, or
// its byte window (the byte-address rewrite of the descriptor, read as the same raw view).
enum class ChannelNumber { unorm, snorm, uscaled, sscaled, uint_, sint, float_ };
struct MtbufFamily {
unsigned first, last, bits, components;
Expand Down Expand Up @@ -53,7 +54,11 @@ void formatted_byte_load(const Inst &i, const State &s) {
refuse(i.pc, "MTBUF destination tuple overflow");
const unsigned width = family->bits;
const auto numeric = static_cast<ChannelNumber>(unsigned(family->first_number) + format - family->first);
const auto handle = resource(i, s, (i.b >> 16 & 31) * 4, ComputeViewKind::raw_vector_srv, 4);
// The byte-address rewrite of a descriptor (stride cleared in word 1) is read as the same raw view, as for MUBUF.
const unsigned base = (i.b >> 16 & 31) * 4;
const auto handle = resource(i, s, base, s.at(base + 1).type == Symbol::Type::zero_stride_word
? ComputeViewKind::byte_window_srv
: ComputeViewKind::raw_vector_srv, 4);
const unsigned scalar_offset = i.b >> 24;
auto addr = to_string(i.a & 4095);
if (scalar_offset != 125 && scalar_offset != 128)
Expand Down
13 changes: 13 additions & 0 deletions tests/native_direct_container_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,19 @@ def atomic_program(instructions):
broken[10] = 0xbf800000 # Leave NUM_RECORDS in elements instead of bytes.
check("byte-count-missing-product", "gfx1013-container-ps",
patched_pixel(len(broken), broken, table=True), "non-contiguous resource descriptor")
# The same rewrite in compute, read by MTBUF loads through s[28:31] (v_mov_b32 v5, 0 as the offset):
# tbuffer_load_format_x v8, v5, s[28:31], 0 format:[BUF_FMT_32_UINT] offen and
# tbuffer_load_format_xyzw v[8:11], v5, s[28:31], 0 format:[BUF_FMT_8_8_8_8_UNORM] offen.
tbuffer_view = [*byte_view[:13], 0x7e0a0280]
check("compute-tbuffer-byte-window", cs, compute(words(*tbuffer_view, 0xe8a01000, 0x80070805, end), table=True))
check("compute-tbuffer-byte-window-unorm", cs,
compute(words(*tbuffer_view, 0xe9c31000, 0x80070805, end), table=True))
# Over-acceptance control only: without the byte-count product the window fields do not match.
tbuffer_broken = tbuffer_view.copy()
tbuffer_broken[10] = 0xbf800000
check("compute-tbuffer-byte-count-missing-product", cs,
compute(words(*tbuffer_broken, 0xe8a01000, 0x80070805, end), table=True),
"non-contiguous resource descriptor")
# Convert v11 to a half in the high word of v6, retaining v6.low.
# Widen the selected high word back from a scalar source into v7.
halves = [0x7e0c02ff, 0xa5a53600, 0x7e1602ff, 0x3f801000, 0x7e0c14f9, 0x06150b,
Expand Down
55 changes: 35 additions & 20 deletions tests/native_global_srv_heap_replay_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,14 +243,18 @@ def without(prefix):
load = words_list.index(0xe0301000) # buffer_load_dword v2, v1, s[20:23], 0 offen
store = words_list.index(0xe0701000) # buffer_store_dword v2, v1, s[12:15], 0 offen

def window(rewritten=rewrite, prefix=(), store_word=0x80030201):
def window(rewritten=rewrite, prefix=(), store_word=0x80030201, load_words=(0xe0301000, 0x80060201)):
"""PROGRAM with the rewrite after the heap load and the load through s[24:27]."""
program = list(words_list)
program[load + 1] = 0x80060201 # buffer_load_dword v2, v1, s[24:27], 0 offen
program[load:load + 2] = load_words # default: buffer_load_dword v2, v1, s[24:27], 0 offen
program[store + 1] = store_word
return program[:after_wait] + list(prefix) + list(rewritten) + program[after_wait:]

WINDOW = window()
# tbuffer_load_format_x v2, v1, s[24:27], 0 format:[BUF_FMT_32_UINT] offen: a formatted load through the window.
# 32_UINT converts nothing, so the output is the slot data as for WINDOW.
TBUFFER_LOAD = (0xe8a01000, 0x80060201)
TWINDOW = window(load_words=TBUFFER_LOAD)
second_load = window([0xbe980320 if w == 0xbe980314 else w for w in rewrite])
second_load[after_wait - 1:after_wait - 1] = [0xf4080801, 0x22000000] # before the s_waitcnt
# The branch word sits at after_wait + 1; its target is next pc + 4 * simm16, here the s_lshl_b32 at word lshl.
Expand All @@ -275,6 +279,9 @@ def window(rewritten=rewrite, prefix=(), store_word=0x80030201):
# s_cmp_lg_u32 s16, s16 and s_cbranch_scc1 back to the s_lshl_b32 before the heap load (never taken): the
# loop contains the heap load, which would run again for another selection
"window_loop": (loop_around_load, "heap byte window loaded inside a loop"),
# the same loop around the heap load, read by the formatted load
"twindow_loop": (window(prefix=loop_around_load[after_wait:after_wait + 2], load_words=TBUFFER_LOAD),
"heap byte window loaded inside a loop"),
}
for name, (program, expected) in shapes.items():
directory = base / ("shape_" + name)
Expand Down Expand Up @@ -308,6 +315,10 @@ def window_request(name, program):
# does not contain the load, which runs once
"window_loop_after_load": (window(prefix=(0xbf071010, 0xbf85fffe)),
"a loop after the heap load does not refuse the window, a .cso is published"),
# tbuffer_load_format_xyzw v[2:5], v1, s[24:27], 0 format:[BUF_FMT_8_8_8_8_UNORM] offen: a converting format
# through the window (values through a window are checked by native_mtbuf_check.py on a table window)
"twindow_unorm": (window(load_words=(0xe9c31000, 0x80060201)),
"a converting formatted load through the window compiles and publishes a .cso"),
}
for name, (program, message) in compiled_shapes.items():
directory, window_stem = window_request(name, program)
Expand All @@ -316,25 +327,29 @@ def window_request(name, program):
code, out, err = run([compile_dxil, sdk, cache, "gfx1013-container-cs", "--requests", directory, out_dir])
require(code == 0 and "requests=1 compiled=1 refused=0" in out and
(out_dir / (window_stem[len("req_"):] + ".cs.cso")).exists(), "compile_dxil: " + message)
window_outputs = {}
for n in (0, 1):
directory, window_stem = window_request(f"window{n}", WINDOW)
code, out, err = run([fixture, "record", sdk, cache, directory / (window_stem + ".cdx"), adapter, n, "ok"])
require(code == 0 and "output matches slot %d data" % n in out,
f"record window select {n}: u0 equals the slot {n} data read through the rewritten descriptor")
out_dir = base / f"window{n}_out"
code, out, err = run([compile_dxil, sdk, cache, "gfx1013-container-cs", "--requests", directory, out_dir])
cso = out_dir / (window_stem[len("req_"):] + ".cs.cso")
require(code == 0 and "requests=1 compiled=1 refused=0" in out and cso.exists(),
f"compile_dxil window select {n}: compiled and published a .cso")
window_outputs[n] = (directory, window_stem, cso)
# The raw load (window) and the formatted load (twindow) through the rewritten descriptor.
for label, program in (("window", WINDOW), ("twindow", TWINDOW)):
window_outputs = {}
for n in (0, 1):
directory, window_stem = window_request(f"{label}{n}", program)
code, out, err = run([fixture, "record", sdk, cache, directory / (window_stem + ".cdx"), adapter, n, "ok"])
require(code == 0 and "output matches slot %d data" % n in out,
f"record {label} select {n}: u0 equals the slot {n} data read through the rewritten descriptor")
out_dir = base / f"{label}{n}_out"
code, out, err = run([compile_dxil, sdk, cache, "gfx1013-container-cs", "--requests", directory, out_dir])
cso = out_dir / (window_stem[len("req_"):] + ".cs.cso")
require(code == 0 and "requests=1 compiled=1 refused=0" in out and cso.exists(),
f"compile_dxil {label} select {n}: compiled and published a .cso")
window_outputs[n] = (directory, window_stem, cso)
code, out, err = run([fixture, "replay", sdk, cache, directory / (window_stem + ".cdx"), cso,
directory / (window_stem + ".views"), adapter, "ok", n])
require(code == 0 and "output matches slot %d data" % n in out,
f"replay {label} select {n}: output equals slot {n} data")
directory, window_stem, cso = window_outputs[0]
code, out, err = run([fixture, "replay", sdk, cache, directory / (window_stem + ".cdx"), cso,
directory / (window_stem + ".views"), adapter, "ok", n])
require(code == 0 and "output matches slot %d data" % n in out, f"replay window select {n}: output equals slot {n} data")
directory, window_stem, cso = window_outputs[0]
code, out, err = run([fixture, "replay", sdk, cache, directory / (window_stem + ".cdx"), cso,
directory / (window_stem + ".views"), adapter, "ok", 1])
require(code == 1 and "mismatch word 0" in out, "replay window select 0 compared with slot 1 data reports a mismatch")
directory / (window_stem + ".views"), adapter, "ok", 1])
require(code == 1 and "mismatch word 0" in out,
f"replay {label} select 0 compared with slot 1 data reports a mismatch")

missing = base / "missing" / (stem + ".views")
code, out, err = replay(selections[0], outputs[0], missing, "refused", 0)
Expand Down
19 changes: 15 additions & 4 deletions tests/native_mtbuf_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,19 @@
PACKED = struct.pack("<6I", 0x7fff8000, 0x40008001, 0x007fff81, 0xfc003c00, 0x0001bc00, 0x42987ec5)


def program(unsigned):
# The byte-address rewrite of the SRV descriptor s[32:35] in place (LLVM 15): s_waitcnt lgkmcnt(0);
# s_bfe_u32 s40, s33, 0xe0010 (stride); s_max_u32 s40, s40, 1; s_mul_i32 s34, s34, s40 (byte count);
# s_and_b32 s33, s33, 0xc000ffff (stride cleared); s_and_b32 s35, s35, 0xcfffffff; s_or_b32 s35, s35, 0x30000000.
BYTE_WINDOW = [0xbf8cc07f, 0x93a8ff21, 0x000e0010, 0x84a88128, 0x93222822, 0x8721ff21, 0xc000ffff,
0x8723ff23, 0xcfffffff, 0x8823ff23, 0x30000000]


def program(unsigned, window=False):
# Load a raw SRV and UAV from separate table parameters. One writer per group.
# With window, every MTBUF load reads the byte window of the SRV (same raw view, same expected values).
words = [sop1(3, 20, 4), sop1(3, 21, 128), sop1(3, 22, 5), sop1(3, 23, 128),
0xf4080000 | 32 << 6 | 10, 0xfa000020, 0xf4080000 | 36 << 6 | 11, 0xfa000020,
*(BYTE_WINDOW if window else ()),
scalar(0x1e, 7, 6, 135), vmove(8, 7), sop1(3, 126, 129)]
pos = 0
for index, (fmt, channels, disp) in enumerate(CASES):
Expand Down Expand Up @@ -76,8 +85,8 @@ def main(cli, gpu, sdk):
start = 8 + 32 * group
row = expected(group)
reference[start:start + len(row)] = row
for unsigned in (False, True):
words = program(unsigned)
for unsigned, window in ((False, False), (True, False), (False, True)):
words = program(unsigned, window)
marker = write_request(work, words, root.read_bytes(), sa)
dest = work / "result.bin"
run = subprocess.run([gpu, sdk, marker, work/"cache", "0", dest, "--direct"])
Expand All @@ -90,6 +99,7 @@ def main(cli, gpu, sdk):
assert differences and set(differences) <= {8, 9, 40, 41}, differences
else:
assert not differences, [(i, hex(actual[i]), hex(reference[i])) for i in differences]
words = program(False) # the guards below change the plain (non-window) program
insn = next(i for i, w in enumerate(words) if w >> 26 == 0x3a)
for op, error in ((words[insn] | (1 << 13), "without index"),
(0xe8001000 | 24 << 19 | 3 << 16, "absent component")):
Expand All @@ -99,7 +109,8 @@ def main(cli, gpu, sdk):
reject = subprocess.run([cli, sdk, work/"cache", "gfx1013-direct-cs@0", marker, work/"bad.cso"],
capture_output=True, text=True)
assert reject.returncode and error in reject.stderr and not (work/"bad.cso").exists(), reject.stderr
print(f"MTBUF matched {2 * len(row)} result words and all guards; changed format, index and component controls passed.")
print(f"MTBUF matched {2 * len(row)} result words, also through the SRV byte window, and all guards; "
"changed format, index and component controls passed.")


if __name__ == "__main__":
Expand Down
3 changes: 2 additions & 1 deletion tests/native_store_replay.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@ int wmain(int argc, wchar_t **argv) {
if (position>=hd.NumDescriptors) return 2;
auto cpu=heap->GetCPUDescriptorHandleForHeapStart();cpu.ptr+=UINT64(position)*step;
Window w;w.write=compute_buffer_uav(b.kind);
const bool vector_bytes = b.kind == ComputeViewKind::raw_vector_srv;
// A byte window is the byte-address rewrite of the same raw table view.
const bool vector_bytes = b.kind == ComputeViewKind::raw_vector_srv || b.kind == ComputeViewKind::byte_window_srv;
const bool root_bytes = b.kind == ComputeViewKind::root_buffer_srv;
if (!w.write && b.kind!=ComputeViewKind::typed_buffer_srv && b.kind!=ComputeViewKind::buffer_srv && !vector_bytes && !root_bytes) return 2;
const bool raw=root_bytes || vector_bytes || b.kind==ComputeViewKind::buffer_srv || b.kind==ComputeViewKind::raw_buffer_uav;
Expand Down
Loading