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
32 changes: 27 additions & 5 deletions docs/experiments/native-global-srv-heap-replay.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 @@ -132,10 +133,31 @@ ctest --preset dev --no-tests=error -R "^native_global_srv_heap_replay(_warp|_ha
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 122 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;
ten compile refusals cover a two-channel load, 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 @@ -156,8 +178,8 @@ ctest --preset dev --no-tests=error -R "^native_global_srv_heap_replay(_warp|_ha
`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
- 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.
6 changes: 4 additions & 2 deletions src/direct_compute_cache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ 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::heap_buffer_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)));
if (!b.array_count || b.array_position >= b.array_count ||
Expand Down Expand Up @@ -317,7 +317,9 @@ NativeComputeShader prepare_direct_compute(const std::filesystem::path &input, u
identity += container ? "/container/cs_6_6" : "/slot67/cs_6_6";
word(identity, container ? 0 : entry);
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";
if (container) {
packed = read_compute_container(input);
identity += shader_digest(packed.bytes) + shader_digest(packed.root);
Expand Down
15 changes: 12 additions & 3 deletions src/gfx1013_compute_native.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,14 @@ enum class ComputeViewKind {
indexed_atomic_uav,
d16_sample_srv,
byte_window_srv,
heap_buffer_srv // A raw buffer read through ResourceDescriptorHeap[slot]; the slot is a GPU value.
heap_buffer_srv, // A raw buffer read through ResourceDescriptorHeap[slot]; the slot is a GPU value.
heap_typed_srv // A one-channel 32-bit typed buffer read the same way (HeapViewContract::typed_dword).
};
inline constexpr unsigned compute_resource_heap = ~0u;
// 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::indexed_atomic_uav;
Expand Down Expand Up @@ -117,7 +120,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
24 changes: 19 additions & 5 deletions src/gfx1013_direct_compute.inl
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ struct DirectCompute {
}
}
}
// Refuses a GPU-selected heap read that the contract does not cover. raw_buffer is the only class that is translated.
// Refuses a GPU-selected heap read that the contract does not cover. raw_buffer and typed_dword are translated.
void refuse_heap_contract(unsigned pc) const {
if (heap_view == HeapViewContract::none)
refuse(pc, "direct resource-heap load requires the selected GPU slot and its view type");
Expand All @@ -283,11 +283,18 @@ struct DirectCompute {
// dispatch before the shader runs. The binding names the heap (no table register); the pipeline indexes it directly.
// A byte window is the byte-address rewrite of that descriptor (word 0 copied, word 1 without stride, word 2 the
// byte count, word 3 with the raw out-of-bounds mode; see byte_descriptor_field): read as the same raw slot view.
// Under typed_dword the slot is a one-channel 32-bit typed buffer instead: a typed load of one channel (an element
// index), read as R32_UINT; raw reads and byte windows are refused.
string heap_resource(const Inst &i, const State &s, unsigned base, ComputeViewKind kind, unsigned bytes) {
const auto &symbol = s.at(base);
refuse_heap_contract(i.pc);
const bool typed = heap_view == HeapViewContract::typed_dword;
if (typed && kind != ComputeViewKind::typed_buffer_srv)
refuse(i.pc, "a GPU-selected heap slot is read as a typed dword buffer SRV only");
if (typed && (i.op & 3))
refuse(i.pc, "typed heap load reads one channel only");
const bool window = kind == ComputeViewKind::byte_window_srv;
if (kind != ComputeViewKind::raw_vector_srv && kind != ComputeViewKind::buffer_srv && !window)
if (!typed && kind != ComputeViewKind::raw_vector_srv && kind != ComputeViewKind::buffer_srv && !window)
refuse(i.pc, "a GPU-selected heap slot is read as a raw buffer SRV only");
if (symbol.offset % 32 || s[base].type != Symbol::Type::indexed_descriptor)
refuse(i.pc, "unaligned heap descriptor");
Expand All @@ -310,13 +317,20 @@ struct DirectCompute {
refuse(i.pc, "non-contiguous resource descriptor");
}
auto &bindings = analysis.result.bindings;
auto it = std::find_if(bindings.begin(), bindings.end(), [](const auto &b) {
return b.kind == ComputeViewKind::heap_buffer_srv;
const auto heap_kind = typed ? ComputeViewKind::heap_typed_srv : ComputeViewKind::heap_buffer_srv;
auto it = std::find_if(bindings.begin(), bindings.end(), [heap_kind](const auto &b) {
return b.kind == heap_kind;
});
if (it == bindings.end())
bindings.push_back({ComputeViewKind::heap_buffer_srv, compute_resource_heap, 0, 0, bytes});
bindings.push_back({heap_kind, compute_resource_heap, 0, 0, bytes});
else
it->required_bytes = (std::max)(it->required_bytes, bytes);
if (typed) {
// A typed buffer of one U32 component (TypedBuffer, props: component type 5 + count * 256); the load reads
// element 0 of its ResRet only. No raw-buffer flag.
ir.features |= 0x40000000;
return ir.heap_handle(ir.binary("add", read(base), to_string(symbol.offset / 32)), 10, 5 + 1 * 256);
}
// 16: raw buffers. 0x40000000: the module reads ResourceDescriptorHeap (DXC sets both for the HLSL reference,
// see tests/native_heap_handle_probe_check.py); the validator refuses a module whose flags do not match.
ir.features |= 16 | 0x40000000;
Expand Down
52 changes: 49 additions & 3 deletions src/startup_stubs/native_global_srv.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@
// The view type of a global (descriptor heap) SRV slot that a dispatch can select from GPU data.
//
// A shader that picks its slot at run time can only be recorded and replayed if the type of every slot it may pick is
// known: the capture keeps {slot, type, stride} for each slot of the heap, and the dispatch is refused, before any
// shader runs, when the selected slot holds another type than the one the shader reads. No heap-indexed data read is
// translated here; this header only classifies, serializes and checks.
// known: the capture keeps {slot, type, stride, bytes} for each slot of the heap (and the 32-bit channel count of a
// typed buffer slot), and the dispatch is refused, before any shader runs, when the selected slot holds another type
// than the one the shader reads. No heap-indexed data read is translated here; this header only classifies,
// serializes and checks, and names the heap view contract a request allows (global_srv_contract).
//
// A .views file describes the one dispatch it was captured from. It is not part of the .cdx/.root content key of the
// request it sits next to.
Expand Down Expand Up @@ -43,6 +44,7 @@ struct GlobalSrvView {
GlobalSrvType type = GlobalSrvType::none;
unsigned stride = 0; // Bytes per element for a buffer view (the value native_view_stride gives), 0 for a texture.
std::uint64_t bytes = 0; // Size of the buffer in bytes, 0 for a texture. Set by the caller from the resource description.
unsigned dword_channels = 0; // Typed buffer of 32-bit channels: their count (typed_dword_channels), else 0.
};

// Classifies the description a title passed for an SRV (slot 18 of the device interface). False: not a view this
Expand All @@ -62,6 +64,7 @@ inline bool classify_global_srv_view(unsigned heap_slot, const void *bytes, std:
view.type = desc.Buffer.StructureByteStride ? GlobalSrvType::structured_buffer
: (desc.Buffer.Flags & D3D12_BUFFER_SRV_FLAG_RAW) ? GlobalSrvType::raw_buffer
: GlobalSrvType::typed_buffer;
if (view.type == GlobalSrvType::typed_buffer) view.dword_channels = typed_dword_channels(desc);
return true;
}

Expand All @@ -78,6 +81,10 @@ inline std::string encode_global_srv_request(const GlobalSrvRequest &request) {
out << "x2w-global-srv-views 2\nselect " << request.selected << "\nneed " << global_srv_type_name(request.needed) << '\n';
for (const auto &view : request.views)
out << "slot " << view.slot << ' ' << global_srv_type_name(view.type) << ' ' << view.stride << ' ' << view.bytes << '\n';
// After the slot lines: the 32-bit channel count of each typed buffer slot that has one.
for (const auto &view : request.views)
if (view.type == GlobalSrvType::typed_buffer && view.dword_channels)
out << "dword " << view.slot << ' ' << view.dword_channels << '\n';
return out.str();
}

Expand Down Expand Up @@ -121,6 +128,19 @@ inline bool decode_global_srv_request(const std::string &text, GlobalSrvRequest
return false;
}
request.views.push_back(view);
} else if (key == "dword") {
// Names an earlier typed buffer slot line, once, with 1-4 channels.
unsigned slot = 0, channels = 0;
GlobalSrvView *named = nullptr;
if (fields >> slot >> channels)
for (auto &view : request.views)
if (view.slot == slot) named = &view;
if (!named || named->type != GlobalSrvType::typed_buffer || named->dword_channels || channels < 1 ||
channels > 4) {
error = "global SRV view metadata: unreadable dword line";
return false;
}
named->dword_channels = channels;
} else {
error = "global SRV view metadata: unknown line '" + key + "'";
return false;
Expand All @@ -145,4 +165,30 @@ inline std::string refuse_global_srv_selection(const std::vector<GlobalSrvView>
}
return "global SRV slot " + std::to_string(selected) + " has no captured view type";
}

// The class of heap reads a request's metadata lets the translator emit (x2w::HeapViewContract::none, raw_buffer,
// typed_dword). Call after refuse_global_srv_selection accepted the selection. A typed request needs one-channel 32-bit
// typed buffers: the checks are necessary conditions on the captured resources only; the view's element range and the
// guest descriptor fields stay the caller's guarantee.
enum class GlobalSrvContract { none, raw_buffer, typed_dword };

inline GlobalSrvContract global_srv_contract(const GlobalSrvRequest &request, std::string &error) {
const auto refuse = [&error](std::string text) {
error = std::move(text) + "; only raw and one-channel 32-bit typed heap reads are translated";
return GlobalSrvContract::none;
};
if (request.needed == GlobalSrvType::raw_buffer) return GlobalSrvContract::raw_buffer;
if (request.needed != GlobalSrvType::typed_buffer)
return refuse(std::string("GPU-selected heap slot view type ") + global_srv_type_name(request.needed) +
" is not raw_buffer or typed_buffer");
// Every captured typed buffer slot, the selected one included, must be a one-channel 32-bit buffer.
for (const auto &view : request.views) {
if (view.type != GlobalSrvType::typed_buffer) continue;
if (view.dword_channels != 1 || view.stride != 4 || !view.bytes || view.bytes % 4)
return refuse("GPU-selected heap slot " + std::to_string(view.slot) +
" is not a one-channel 32-bit typed buffer" +
(view.dword_channels ? "" : " (no 32-bit channel metadata)"));
}
return GlobalSrvContract::typed_dword;
}
} // namespace x2w_startup
Loading
Loading