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
60 changes: 53 additions & 7 deletions docs/experiments/native-global-srv-heap-replay.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ Issue #2908, branch `cpp-migration`. Builds on the heap read in the direct C++ D
## What changed

- The direct compute translation takes a view contract for a GPU-selected heap slot (`x2w::HeapViewContract`:
`none`, `raw_buffer`, `other`). An SMEM descriptor load from the resource heap is refused unless the contract is
`raw_buffer`; without a contract the existing text stays: "direct resource-heap load requires the selected GPU slot and
`none`, `raw_buffer`, `typed_dword`, `other`). An SMEM descriptor load from the resource heap is refused unless the
contract is `raw_buffer` or `typed_dword`; without a contract the existing text stays: "direct resource-heap load requires the selected GPU slot and
its view type". A scalar buffer load from the heap base, a heap descriptor load with an immediate offset or a constant
index (both could address another slot than the captured selection), and a numeric use of a GPU-selected slot's
stride in the direct path are refused with their own texts. The contract is part of the frontend cache key when it is
Expand All @@ -31,12 +31,13 @@ Issue #2908, branch `cpp-migration`. Builds on the heap read in the direct C++ D
select <slot>
need <type>
slot <n> <type> <stride> <bytes>
dword <n> <channels> (after the slot lines; typed buffers of 32-bit channels only, #3023)
```

- `compile_dxil ... gfx1013-container-cs` reads an optional `req_<key>.views` next to the `.cdx` (single file and
`--requests`). It decodes the file, refuses a selection whose slot holds another view type than `need`, refuses a `need`
other than `raw_buffer`, translates with the `raw_buffer` contract and prints `global_srv select=<n> need=<type>`. Every
refusal happens before the `.cso` is published. Without the file a heap-reading shader is refused as before. The
other than `raw_buffer` or a one-channel 32-bit `typed_buffer` (#3023), translates with the matching contract and
prints `global_srv select=<n> need=<type>`. Every refusal happens before the `.cso` is published. Without the file a heap-reading shader is refused as before. The
`.views` file is not part of the `.cdx`/`.root` content key: it describes one captured dispatch.

## Meaning of "native recording" and "runtime path" here
Expand Down Expand Up @@ -141,10 +142,44 @@ ctest --preset dev --no-tests=error -R "^native_global_srv_heap_replay(_warp|_ha
- With the high-word check removed, both tests fail at the `mov_high` controls.
- `ctest --preset dev --no-tests=error -R "native_|startup_compute|compile_dxil" -j 8`: 415 tests, 404 passed and
11 skipped (77, WARP).
- Formatted loads through the byte window (original #3034 result for #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").
- Typed heap loads (#3023): under the new `HeapViewContract::typed_dword` a one-channel typed load
(`buffer_load_format_x`, an `idxen` element index) through the GPU-selected heap descriptor reads
`ResourceDescriptorHeap[slot]` as a typed buffer of one U32 component (binding kind `heap_typed_srv`, cache identity
`/heap-view/typed_dword`). `.views` gains a `dword <slot> <channels>` line for each typed buffer slot of 32-bit
channels; `compile_dxil` and the fixture derive the contract from `need typed_buffer` when every captured typed
buffer slot is a one-channel 32-bit buffer (`x2w_startup::global_srv_contract`). `record` takes
`--need typed_buffer`. `native_global_srv_heap_replay_hardware` passes with 160 assertions: the typed load is
recorded for slot 3 (`R32_UINT`), compiled, compiled again from the frontend cache, and replayed (output equals the
slot 3 data); replaying it against slot 0 reports a mismatch; selections 0 and 2 are refused before the dispatch;
thirteen compile refusals cover a two-channel load, an element index with `offen`, an immediate offset or a
nonzero `soffset` (refused by the decoder before the heap read), a raw load and a `tbuffer` load under the typed contract, the typed
load under the raw contract, and metadata without the `dword` line, with a channel count of 5, with a `dword` line for
a raw slot, a duplicate or an early `dword` line, or with another selection. On the local capture (counts only) all 906 typed heap loads behind the raw contract have this
one-channel `idxen` shape; with `typed_dword` forced by an uncommitted switch, 895 of those programs then stop at a
raw heap read of another slot: they read the heap both ways, which one captured selection does not cover.

## Limits

- Only raw buffer heap reads are translated; other view classes are refused with a named diagnostic.
- Only raw buffer heap reads (`raw_buffer`) and one-channel 32-bit typed buffer heap reads (`typed_dword`) are
translated, one class per program; other view classes are refused with a named diagnostic.
- Under `typed_dword` the translator sees no descriptor values. The caller guarantees that every selectable slot is an
`R32_UINT`, `R32_SINT` or `R32_FLOAT` buffer view bound as `R32_UINT` over the same element range, that the guest
descriptor has stride 4, a 32-bit data format, identity `dst_sel_x`, swizzle and `add_tid` disabled, and that its
`num_records` is the view's element count (reads past it return 0). `.views` records the resource size, not the
view's first element or element count, so `global_srv_contract` checks necessary conditions only. Only `R32_UINT`
slots are exercised; `R32_SINT` and `R32_FLOAT` are accepted but untested. No runtime binds a typed heap slot yet.
- The selected slot's index is per-lane VGPR data in general; reads stay inside the raw buffer view, which returns 0
out of bounds. No static index bound is checked.
- Replay regenerates the slot contents deterministically from the slot index; `.views` captures type, stride and size,
Expand All @@ -153,8 +188,19 @@ 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.
- Typed loads (`buffer_load_format_*`) through a heap descriptor and scalar loads through the rewritten descriptor stay
refused. The startup runtime
- A formatted load (`tbuffer_load_format_*`) bounds its element with the size of the desktop raw view
(`getDimensions`) and returns 0 for the whole element when it does not fit. A runtime that binds a byte window
must create the raw view with the exact guest window's byte count. The heap replay uses identity channel order.
A published table manifest marks the MTBUF window with `srv <i> buf table <param> <byte> identity_dst_sel`, including
after a frontend cache hit. The compute binder checks word 3's DST_SEL of the hardware descriptor or the
component mapping of a slot 18 buffer SRV before binding the raw view for the exact range. The in-process
route (`X2W_NATIVE_SHADERS`, `translate_compute_natively`) binds the byte window itself and carries the same
identity requirement into its slot, so the same checks run before its dispatch.
- `startup_compute_mtbuf_window_{hardware,warp}` compares 12 pairs and the surrounding guards through the published
path for both a hardware descriptor and a buffer SRV token. A 44-byte window holds five full pairs. The partial
pair and the later pairs read zero. Swapped X/Y selectors refuse before Dispatch, and the output keeps its sentinel.
- Multi-channel typed heap loads, typed loads under the raw contract 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).
- The captured `select` must be the slot index the dispatch addresses; producing it from a title capture is outside this
fixture.
15 changes: 10 additions & 5 deletions src/direct_compute_cache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ Bytes encode(const NativeComputeShader &program) {
for (const auto &b : program.bindings) {
word(out, static_cast<uint32_t>(b.kind));
for (unsigned n : {b.root_parameter, b.byte_offset, b.shader_register, b.required_bytes, b.array_count,
b.array_position, b.image_dimension, unsigned(b.scalar_table)})
b.array_position, b.image_dimension, unsigned(b.scalar_table), unsigned(b.requires_identity_dst_sel)})
word(out, n);
}
word(out, static_cast<uint32_t>(program.constants.size()));
Expand Down Expand Up @@ -135,9 +135,10 @@ NativeComputeShader decode(std::string_view bytes) {
unsigned n = in.count(1000000);
shader.bindings.reserve(n);
for (unsigned j = 0; j < n; ++j) {
auto kind = static_cast<ComputeViewKind>(in.count(static_cast<unsigned>(ComputeViewKind::indexed_raw_srv)));
auto kind = static_cast<ComputeViewKind>(in.count(static_cast<unsigned>(ComputeViewKind::heap_typed_srv)));
ComputeViewBinding b{kind, in.word(), in.word(), in.word(), in.word(), in.word(), in.word(), in.count(7)};
b.scalar_table = static_cast<ComputeViewBinding::ScalarTable>(in.count(unsigned(ComputeViewBinding::ScalarTable::srv)));
b.requires_identity_dst_sel = in.count(1) != 0;
const bool sampler_heap = kind == ComputeViewKind::heap_sampler;
if (!b.array_count || b.array_position >= b.array_count ||
(sampler_heap ? b.root_parameter != compute_sampler_heap
Expand Down Expand Up @@ -371,7 +372,9 @@ NativeComputeShader prepare_direct_compute(const fs::path &input, unsigned entry
Bytes identity = X2W_NATIVE_SHADER_REVISION "/container/cs_6_6";
word(identity, 0);
if (heap_view != HeapViewContract::none)
identity += heap_view == HeapViewContract::raw_buffer ? "/heap-view/raw_buffer" : "/heap-view/other";
identity += heap_view == HeapViewContract::raw_buffer ? "/heap-view/raw_buffer"
: heap_view == HeapViewContract::typed_dword ? "/heap-view/typed_dword"
: "/heap-view/other";
identity += shader_digest(packed.bytes) + shader_digest(packed.root);
return cached_compute_ir(cache, shader_digest(identity), cache_hit,
[&] { return translate_compute_container(std::move(packed), heap_view); });
Expand Down Expand Up @@ -955,8 +958,10 @@ std::optional<std::string> format_compute_runtime_manifest(const NativeComputeSh
return refuse("SRV registers not contiguous");
if (b.kind == ComputeViewKind::buffer_srv || b.kind == ComputeViewKind::raw_vector_srv ||
b.kind == ComputeViewKind::byte_window_srv) {
srv_lines << "srv " << b.shader_register << " buf table " << b.root_parameter << ' ' << b.byte_offset
<< '\n';
srv_lines << "srv " << b.shader_register << " buf table " << b.root_parameter << ' ' << b.byte_offset;
if (b.requires_identity_dst_sel)
srv_lines << " identity_dst_sel";
srv_lines << '\n';
} else if (b.kind == ComputeViewKind::texture_srv && b.array_count == 1 &&
(b.image_dimension == 0 || b.image_dimension == 1 || b.image_dimension == 2 ||
b.image_dimension == 5)) {
Expand Down
10 changes: 8 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 @@ -60,7 +61,12 @@ void formatted_byte_load(const Inst &i, const State &s, const string &byte_windo
unsigned requested_bits=0;
for (unsigned j=0;j<count;++j) requested_bits+=component_width(j);
const auto numeric = static_cast<ChannelNumber>(unsigned(family->first_number) + format - family->first);
const auto handle = byte_window.empty() ? resource(i, s, (i.b >> 16 & 31) * 4, ComputeViewKind::raw_vector_srv, 4) : byte_window;
// The byte-address rewrite of a descriptor (stride cleared in word 1) is read as the same raw view, as for MUBUF.
// A caller that already bound a byte window (the graphics path) passes its handle instead.
const unsigned base = (i.b >> 16 & 31) * 4;
const bool window = byte_window.empty() && s.at(base + 1).type == Symbol::Type::zero_stride_word;
const auto kind = window ? ComputeViewKind::byte_window_srv : ComputeViewKind::raw_vector_srv;
const auto handle = byte_window.empty() ? resource(i, s, base, kind, 4, nullptr, window) : byte_window;
const unsigned scalar_offset = i.b >> 24;
auto addr = to_string(i.a & 4095);
if (scalar_offset != 125 && scalar_offset != 128)
Expand Down
16 changes: 13 additions & 3 deletions src/gfx1013_compute_native.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ enum class ComputeViewKind {
float2_texture_uav,
float3_texture_uav,
packed_d16_x_uav, // Single low-halfword payload; desktop buffer view determines the element size.
indexed_raw_srv // Fixed-stride record read. required_bytes is one complete record and its checked stride.
indexed_raw_srv, // Fixed-stride record read. required_bytes is one complete record and its checked stride.
heap_typed_srv // A one-channel 32-bit typed buffer read the same way (HeapViewContract::typed_dword).
};
inline unsigned exact_texture_uav_channels(ComputeViewKind kind) {
switch (kind) {
Expand All @@ -63,7 +64,9 @@ inline constexpr unsigned compute_resource_heap = ~0u;
// the directly indexed sampler heap; entry n is title byte offset 16 * n
inline constexpr unsigned compute_sampler_heap = ~0u - 1;
// A binding of the whole descriptor heap: it has no table register and no root parameter.
inline bool compute_heap_binding(ComputeViewKind kind) { return kind == ComputeViewKind::heap_buffer_srv; }
inline bool compute_heap_binding(ComputeViewKind kind) {
return kind == ComputeViewKind::heap_buffer_srv || kind == ComputeViewKind::heap_typed_srv;
}
inline bool compute_buffer_uav(ComputeViewKind kind) {
return kind == ComputeViewKind::raw_buffer_uav || kind == ComputeViewKind::typed_buffer_uav ||
kind == ComputeViewKind::half_buffer_uav || kind == ComputeViewKind::packed_d16_x_uav ||
Expand All @@ -87,6 +90,7 @@ struct ComputeViewBinding {
unsigned image_dimension = 1; // ISA dimension field; 1 denotes Texture2D.
enum class ScalarTable { unknown, reflected_cbv, declared_cbv, srv };
ScalarTable scalar_table = ScalarTable::unknown;
bool requires_identity_dst_sel = false;
};
bool apply_reflected_table_use(ComputeViewBinding &binding, const GuestShaderStage &stage,
std::optional<unsigned> packed_sgpr = {});
Expand Down Expand Up @@ -151,7 +155,13 @@ struct NativeComputeShader {
// every slot the program can select is a raw view whose size is the guest range num_records * max(stride, 1); the
// translator does not check that, nor the swizzle bits (word 1 bits 31:30) or the other word 3 fields, and a program
// with two heap loads from different selectors is outside what one captured selection covers.
enum class HeapViewContract : unsigned { none, raw_buffer, other };
// typed_dword: only typed loads (buffer_load_format_x, an idxen element index or none, no offsets) read the heap; raw
// loads and byte windows are refused. The caller guarantees that every slot the program can select is a typed buffer SRV
// of R32_UINT, R32_SINT or R32_FLOAT (default mapping, no flags, no structure stride), bound as R32_UINT over the same
// resource and element range, and that the guest descriptor it stands for has stride 4, a 32-bit UINT/SINT/FLOAT data
// format, identity dst_sel_x, swizzle and add_tid disabled, and the out-of-bounds rule "index >= num_records reads 0",
// with num_records equal to the view's element count. The translator checks none of these values.
enum class HeapViewContract : unsigned { none, raw_buffer, typed_dword, other };
NativeComputeShader translate_compute(const ComputeRequest &request, unsigned entry_byte_offset,
bool direct_dxil = false);
NativeComputeShader translate_compute_container(ComputeContainer container,
Expand Down
Loading
Loading