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
47 changes: 42 additions & 5 deletions docs/experiments/native-global-srv-heap-replay.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,18 @@ Issue #2908, branch `cpp-migration`. Builds on the heap read in the direct C++ D
`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
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 stride read of a GPU-selected slot in the
direct path are refused with their own texts. The contract is part of the frontend cache key when it is not `none`.
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
not `none`.
- Byte-address rewrite (#3023): under `raw_buffer` the direct path tracks the rewrite of the loaded heap descriptor that
`byte_descriptor_field` already models for table descriptors (`s_bfe_u32 ... 0xe0010` stride, `s_max_u32 ... 1`,
`s_mul_i32` byte count, `s_and_b32 ... 0xc000ffff`, `s_and_b32 ... 0xcfffffff`, `s_or_b32 ... 0x30000000`, word 0
copied). A raw load through the rewritten descriptor reads the same `ResourceDescriptorHeap[slot]` raw view. The words
must all come from one heap load (same pc). As a precaution, a backward branch whose loop contains that heap load
refuses the window, because the loop would run the load again for another selection. Loops elsewhere in the program
do not refuse it. The caller's contract: every slot the
program can select is a raw view of exactly `num_records * max(stride, 1)` bytes; the translator does not check that,
nor the swizzle bits or the other word 3 fields. Without the contract the first refusal stays as before.
- Modules that read `ResourceDescriptorHeap` now also set shader flag `0x40000000`, as DXC does for the HLSL reference.
Without it the validator refuses the module ("Flags must match usage. Validation failed."), measured by removing the
flag and running the hardware test.
Expand Down Expand Up @@ -70,7 +80,15 @@ The check runs, in order:
line, no line for the selected slot, `select 7`, and (replay) a missing `.views` file. Each gives an explicit
diagnostic, no `.cso`, and intact sentinels.
8. Shader shapes refused by `compile_dxil` even with valid metadata: a constant heap index, a heap descriptor load with
an immediate offset, and a stride read of the selected slot.
an immediate offset, a stride read used as a number (`unbound numeric SGPR s24`), and four byte-address rewrites the
raw slot view cannot stand for: a wrong stride mask (`unbound numeric SGPR s21`), word 0 from a second heap load
(`non-contiguous resource descriptor`), a store through the rewritten descriptor (`a GPU-selected heap slot is read
as a raw buffer SRV only`) and a never-taken backward branch to (or before) the heap load (`heap byte window loaded
inside a loop`).
9. The byte-address rewrite: a stride read that nothing uses compiles, a never-taken backward branch after the heap
load (the loop does not contain the load) compiles, and the program with the full rewrite is recorded
for selections 0 and 1 (output equals the slot's data), compiled by `compile_dxil --requests`, replayed with the
same result, and replaying selection 0 against the slot 1 reference reports a mismatch.

## Result

Expand All @@ -88,6 +106,20 @@ ctest --preset dev --no-tests=error -R "^native_global_srv_heap_replay(_warp|_ha
(one known failure, `native_wave_request_hardware`, #2915, addressed by #2928).
- Revert control: with the heap refusal restored for every contract, `record` for selection 0 exits 4 with "direct
resource-heap load requires the selected GPU slot and its view type".
- Byte-address rewrite (#3023): `native_global_srv_heap_replay_hardware` passed, 83 assertions, on the same machine and
driver. The check run against `compile_dxil` and the fixture built at `999e17cd` (before the change) fails at
`stride_read` (old text "a GPU-selected heap slot stride read is not translated with a view contract") and, with the
shapes skipped, at "an unused stride read compiles" (the run stops there, so the full-rewrite record step was not
reached on that build). `ctest --preset dev --no-tests=error -R
"native_|startup_compute_dispatch|compute_heap" -j 6`: 147 tests, 142 passed, 4 skipped (77, WARP), 1 failed
(`native_cvt_pk_u8_hardware`, failing before the change as well). `compile_dxil --refusals` on a local start-up
capture gives the same output as before (it translates without a contract).
- Loop guard narrowed: the first version of the guard refused the window for any backward branch in the program.
With the contract forced locally (an uncommitted switch), it refused 921 of the 2,689 requests of the same capture.
With the guard limited to loops around the heap load, none of these is refused for a loop; 906 then stop at a typed
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.

## Limits

Expand All @@ -96,7 +128,12 @@ ctest --preset dev --no-tests=error -R "^native_global_srv_heap_replay(_warp|_ha
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,
not contents.
- In the direct DXIL path a stride read of a GPU-selected heap slot is refused. The HLSL path's `heap_stride_srv`
(`src/gfx1013_compute_native.cpp`, used by `tests/native_compute_grid.cpp`) is unchanged.
- In the direct DXIL path a GPU-selected slot's stride is never a number: it is tracked symbolically and only the
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
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.
23 changes: 15 additions & 8 deletions src/gfx1013_compute_native.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -437,12 +437,15 @@ Symbol source(const Inst &i, unsigned code, const State &s) {
unsigned memory_count(const Inst &i) { return 1u << (i.op & 7); }
// Derive the rewritten byte-address V# symbolically. Each field carries its
// source table/slot identity. Numeric consumption still requires real bits.
static std::optional<Symbol> byte_descriptor_field(const Inst &i, const State &s) {
// heap_window: the direct path under HeapViewContract::raw_buffer also rewrites a
// descriptor loaded from the GPU-selected heap slot (its words keep the load pc).
static std::optional<Symbol> byte_descriptor_field(const Inst &i, const State &s, bool heap_window = false) {
using T = Symbol::Type;
if (i.enc != Encoding::Sop2 || i.dst > 105) return {};
auto a = source(i, i.src[0], s), b = source(i, i.src[1], s);
auto component = [](const Symbol &v, unsigned ch) {
return v.type == T::descriptor && v.offset % 32 == ch*4 && v.value != compute_resource_heap;
auto component = [heap_window](const Symbol &v, unsigned ch) {
return v.offset % 32 == ch*4 && ((v.type == T::descriptor && v.value != compute_resource_heap) ||
(heap_window && v.type == T::indexed_descriptor && v.value == compute_resource_heap));
};
auto field = [](Symbol v, T kind) {
v.type = kind;
Expand All @@ -464,7 +467,7 @@ static std::optional<Symbol> byte_descriptor_field(const Inst &i, const State &s
return field(a,T::byte_oob);
return {};
}
State transfer(const Inst &i, State s, bool wave64, bool graphics) {
State transfer(const Inst &i, State s, bool wave64, bool graphics, bool heap_window = false) {
auto set = [&](unsigned n, Symbol v) {
if (n >= s.size())
refuse(i.pc, "invalid SGPR");
Expand All @@ -483,7 +486,7 @@ State transfer(const Inst &i, State s, bool wave64, bool graphics) {
(i.src[0] == 193 || i.src[0] == 128) && (i.src[1] == 193 || i.src[1] == 128)) {
set(i.dst,{Symbol::Type::uniform_mask,i.pc,0});
set(i.dst+1,{Symbol::Type::uniform_mask,i.pc,1});
} else if (auto field = byte_descriptor_field(i,s)) {
} else if (auto field = byte_descriptor_field(i,s,heap_window)) {
set(i.dst,*field);
} else if (i.enc == Encoding::FixedSetup) {
for (unsigned r = i.dst; r < i.dst + i.op && r < s.size(); ++r) s[r] = {};
Expand Down Expand Up @@ -621,7 +624,8 @@ std::map<unsigned, Inst> fixed_vertex_loads(const string &code) {
}
return found ? result : std::map<unsigned, Inst>{};
}
Program parse(const string &code, unsigned entry, const State &initial, bool graph, bool graphics=false, bool wave64=false, bool fixed_vertex=false) {
Program parse(const string &code, unsigned entry, const State &initial, bool graph, bool graphics=false, bool wave64=false, bool fixed_vertex=false,
bool heap_window=false) {
Program p;
p.graphics_wave64 = graphics && wave64;
if ((entry & 3) || entry >= code.size() || code.size() > 1u << 20)
Expand Down Expand Up @@ -670,7 +674,7 @@ Program parse(const string &code, unsigned entry, const State &initial, bool gra
auto k = work.front();
work.pop_front();
auto &i = p.code[k];
auto after = transfer(i, p.before[k], wave64, graphics);
auto after = transfer(i, p.before[k], wave64, graphics, heap_window);
auto merge = [&](size_t t) {
bool changed = !p.reached[t];
auto &in = p.before[t];
Expand Down Expand Up @@ -1502,7 +1506,9 @@ static NativeComputeShader translate_program(const ComputeRequest &request, unsi
if (!stage && (UINT(layout->Desc_1_1.Flags) & 0xc00u) == 0xc00u && next + 2 == user)
initial[next] = {Symbol::Type::table, compute_resource_heap};
const bool wave64 = stage && !stage->wave32;
auto program = parse(request.code, entry, initial, direct_dxil, false, wave64);
// The byte-address rewrite of a GPU-selected heap descriptor is read only under the raw_buffer contract.
const bool heap_window = direct_dxil && heap_view == HeapViewContract::raw_buffer;
auto program = parse(request.code, entry, initial, direct_dxil, false, wave64, false, heap_window);
Emit emit{{}, program,layout->Desc_1_1};
emit.result.constants = constants;
if (!lds)
Expand All @@ -1513,6 +1519,7 @@ static NativeComputeShader translate_program(const ComputeRequest &request, unsi
DirectCompute compiler{emit};
compiler.wave64 = wave64;
compiler.heap_view = heap_view;
compiler.heap_window = heap_window;
if (threads % (wave64 ? 64 : 32))
for (const auto &i : program.code)
if (i.name == "v_readlane_b32" || i.name == "v_writelane_b32")
Expand Down
5 changes: 5 additions & 0 deletions src/gfx1013_compute_native.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,11 @@ struct NativeComputeShader {
// The view class a GPU-selected descriptor heap slot is read as. none: the slot and its view type are not known, so a
// heap read is refused. raw_buffer: the capture says the selected slot is a raw buffer SRV. other: the capture names a
// view type that is not translated (refused when the shader reads the heap).
// Under raw_buffer the direct path also reads the byte-address rewrite of the selected descriptor (stride read,
// max(stride, 1), byte count, stride cleared, raw out-of-bounds mode) as the same raw view. The caller guarantees that
// 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 };
NativeComputeShader translate_compute(const ComputeRequest &request, unsigned entry_byte_offset,
bool direct_dxil = false);
Expand Down
25 changes: 22 additions & 3 deletions src/gfx1013_direct_compute.inl
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ struct DirectCompute {
bool wave64 = false;
// The view class a GPU-selected heap slot is read as; see HeapViewContract.
HeapViewContract heap_view = HeapViewContract::none;
// heap_view is raw_buffer and the path is direct: the byte-address rewrite of a heap descriptor is tracked.
bool heap_window = false;
// Binary16 add, sub, subrev and mul are lowered only while MODE stays at
// the contract this path implements: round-to-nearest-even, f16 denormals
// kept on input and output, and FP16_OVFL clear. mode_bits holds the
Expand Down Expand Up @@ -279,16 +281,31 @@ struct DirectCompute {
// A raw buffer SRV chosen by a GPU value in the whole descriptor heap: ResourceDescriptorHeap[slot]. The view class the
// shader reads is the one this handle is annotated with (raw buffer); a slot holding another class is refused by the
// 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.
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);
if (kind != ComputeViewKind::raw_vector_srv && kind != ComputeViewKind::buffer_srv)
const bool window = kind == ComputeViewKind::byte_window_srv;
if (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");
// Precaution: the rewritten words are compared by the pc of their heap load (symbol.definition), which a loop
// around that load executes again for another selection. A loop that does not contain the load runs it once.
// The scan also sees unreached code up to s_endpgm, which can only refuse more.
if (window)
for (const auto &inst : analysis.program.code)
if (inst.branch() && inst.target() <= symbol.definition && symbol.definition <= inst.pc)
refuse(i.pc, "heap byte window loaded inside a loop");
constexpr Symbol::Type window_words[] = {Symbol::Type::indexed_descriptor, Symbol::Type::zero_stride_word,
Symbol::Type::byte_count, Symbol::Type::byte_oob};
for (unsigned j = 1; j < 4; ++j) {
Symbol expected = symbol;
expected.offset += 4 * j;
if (window)
expected.type = window_words[j]; // field() already cleared the word offset
else
expected.offset += 4 * j;
if (!(s.at(base + j) == expected))
refuse(i.pc, "non-contiguous resource descriptor");
}
Expand Down Expand Up @@ -1472,7 +1489,7 @@ struct DirectCompute {
} else write_ballot(*dst, carry);
return;
}
if (auto field = byte_descriptor_field(i,state)) {
if (auto field = byte_descriptor_field(i,state,heap_window)) {
// Keep derived words symbolic, including the status bit until a
// later instruction overwrites it. Never let a numeric branch
// observe a placeholder for this field or its SCC.
Expand Down Expand Up @@ -1640,6 +1657,8 @@ struct DirectCompute {
descriptor.offset % 32 == 4 && field.type == Symbol::Type::constant && field.value == 0xe0010) {
// Reachable only through a heap descriptor load, which needs the raw_buffer contract. A raw buffer view
// has no stride to read, so the stride of a GPU-selected slot stays untranslated in the direct path.
// With the raw_buffer contract a stride read into s0-s105 is tracked symbolically
// (byte_descriptor_field) and does not reach here.
refuse_heap_contract(i.pc);
refuse(i.pc, "a GPU-selected heap slot stride read is not translated with a view contract");
}
Expand Down
Loading
Loading