Skip to content
Open
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
3 changes: 2 additions & 1 deletion CMakePresets.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@
"xmem_placeholder_split_check", "xmem_map_split_first_check", "startup_dstorage_break_window_check",
"xmem_page_runs_device_check", "linear_buffer_backing_check",
"bcpack_formats_check", "startup_stub_consumer", "startup_inline_retire", "startup_resource_retire_title", "startup_table_lock",
"native_compute_ir_dump", "native_pixel_ir_dump", "native_store_replay", "native_raw_buffer_view_check"
"native_compute_ir_dump", "native_pixel_ir_dump", "native_store_replay", "native_raw_buffer_view_check",
"native_heap_token_texture"
]
},
{ "name": "release", "configurePreset": "release" },
Expand Down
2 changes: 2 additions & 0 deletions docs/experiments.md
Original file line number Diff line number Diff line change
Expand Up @@ -18356,3 +18356,5 @@ ctest --preset dev --no-tests=error -j1 -R '^native_pixel_atomic_(warp|hardware)
```

Repeat with the release preset. The source-built fixture verifies 13 actual draws and four refusal controls per adapter/configuration, including contention, guard words, active out-of-view, odd raster edges and real WQM quad-neighbor reads with and without GLC. WQM+GLC records returned old values through a second exact atomic in a private lane slot and checks a valid serialization of the varying addends. Local RX 7900 XT/WARP Debug/Release integration over main 4b0350e passed 52 draws, 3328 DWORD and 1664 pixel comparisons, and 16 refusal controls. The debug layer was unavailable; actual title rendering is not established.

See [GPU-selected heap textures through view tokens in native compute](experiments/native-heap-token-textures.md) (#3023 stage 3, translator only) for the `heaptok` manifest lines the runtime still refuses. The same page covers GPU-selected raw buffers read through view tokens (`baseraw` and `nullraw` words).
141 changes: 141 additions & 0 deletions docs/experiments/native-heap-token-textures.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
# GPU-selected heap textures through view tokens in native compute (translator, #3023 stage 3, PR 1)

Issue #3023. The native compute translator accepts a texture that the program loads with `s_load_dwordx8` from the directly
indexed resource heap at a byte offset chosen by GPU data (a "heap token texture"). This change is the translator, the
manifest writer, the reader refusal and the fixtures. The runtime still refuses the new manifest lines by name ("heap token
textures are not bound yet"), so no dispatch behaviour changes. The runtime side (binding the token memory, the dimension
table and the host descriptor ranges) is a later change.

## Why a token

The guest computes the descriptor address itself and the title copies descriptors with its own memory copies, so the
translator cannot mirror heap slots. What the title keeps at the descriptor address of a registered view is a 32-byte view
token: word 0 the magic `0x56573258`, word 1 the view id (a content token has bit 31 set and is not an id). The shader reads
those two words at the guest byte address and maps the id to a host descriptor.

## What the translator does

- A program is eligible when every reached `s_load_dwordx8` from the resource heap with an SGPR offset (a token load) is
used only as the T# of `image_load`, `image_load_mip`, `image_sample_l`, `image_sample_lz`, `image_gather4_lz` (2D) or
`image_get_resinfo` of a 1D, 2D or 2D-array view, without D16. A copy, a numeric read, an image field read, a store, a
comparison, cube or 3D use or a partial use makes the program not eligible; it then keeps today's analysis and its
refusal text. An eligible program is analysed a second time with the byte address kept in the destination SGPRs
(`heap_tokens` in `transfer()` and `parse()`).
- The load handler requires an immediate offset and a proven SGPR offset (`constant` or `masked`) that are multiples of 4,
writes `so + imm` to the eight words, and bypasses the heap view contract: the contract exists because the runtime cannot
vouch for the view kind of a GPU-selected slot, and this route checks it on every execution.
- Per image instruction of dimension `d`: `valid = word0 == magic && id < count && dims[min(id, count - 1)] == 1 + d`;
the handle is `ResourceDescriptorHeap[valid ? base(d) + id : null]` (kind 1, 2 or 7, `nonuniform` false: every lane loads
the same address). When `!valid`, the first lane of the wave adds one to a miss counter (`waveIsFirstLane`, `atomicBinOp`), but only when the wave has an
active guest lane (`waveActiveBallot` of EXEC is not zero); a wave that reaches the instruction with EXEC = 0 counts nothing.
- Five more `b0` words (seven with a raw buffer, see below) follow the stride words: `base1d`, `base2d`, `base2da`, `count`, `null`. Three runtime-provided views
(root parameter `compute_heap_token`): the token memory (`heap_token_srv`, a raw SRV), the dimension table
(`heap_token_dims_srv`, a raw SRV with one uint per view id: 0 none, else 1 + the image dimension) and the miss counter
(`heap_token_miss_uav`, a raw UAV). They take the first SRV and UAV registers after the stride views, because the module
names the registers while it is lowered.
- Manifest lines: `srv N heaptok`, `srv N heaptokdims`, `uav N heaptokmiss`,
`udsrc K heaptok base1d|base2d|base2da|count|null`, and in the seven-word form (raw buffers, below) also
`udsrc K heaptok baseraw|nullraw`.
`native_compute_output_revision` is 3 (see Revision below). The native-only route (no manifest reader) refuses a program
with token words with the same text as the manifest reader, `heap token textures are not bound yet`. The root signature
of the pipeline sets `CBV_SRV_UAV_HEAP_DIRECTLY_INDEXED` for them.

## Tests

- `native_heap_token_texture_controls`: compiles source-written requests; checks the published manifest, the module's heap
handle, wave-first-lane and atomic add, and each consumer or address shape that keeps today's refusal text or gets a new one
(`heap token byte offset has no proven 4-byte aligned range`, `unaligned heap token immediate byte offset`).
- `native_heap_token_texture_warp` and `_hardware`: the rig runs 1D, 2D and 2D-array programs on a host heap with one range
per dimension; a valid token gives the texel of its view and no miss; a wrong magic, an id equal to the count, the largest
id, an id of another dimension, a content token, id 0 and an empty token give the null texel and one miss per wave. WARP has
no 32-lane waves and reports 77.
- `native_compute_manifest_reasons` covers the writer; `startup_compute_dispatch_texture` the reader refusal.

## Open

The null slot has one word; a null view of another dimension than the instruction's is not defined by D3D12 for dynamic
indexing, so the runtime change has to provide a null slot that every dimension may read (the fixture uses a null view of the
dimension under test).

# GPU-selected raw buffers through view tokens (PR 1c)

The same token route also reads a raw buffer that the program loads as a V# with `s_load_dwordx4` from the resource heap at
a GPU-selected byte offset, or through an address formed by `s_add_u32` (a waterfall loop over a lane-selected slot, #3224).
Only reads of that V# in the byte-window form and scalar buffer loads from it are translated; everything else keeps today's
path and refusal text. Like the texture route, this translates only where no heap view contract applies
(`HeapViewContract::none`); under a contract the existing `heap_buffer_srv` path is unchanged.

## What the translator does

- Candidates are the reached `s_load_dwordx4` from the resource heap with an SGPR offset, and the same load through an
address formed by `s_add_u32` whose high word is the carry of that add (the emitter writes it as 0). Each candidate is
judged on its own (`heap_token_buffer_eligible`). It is eligible when its four words are read only as (a) the V# of a raw
`buffer_load_dword`, `x2`, `x3` or `x4` with OFFEN and without IDXEN, with words 1 to 3 the rewritten fields of the same
load (the byte window: stride cleared, byte count, raw out-of-bounds mode), (b) the base of an `s_buffer_load_dword{,x2,x4,x8,x16}`
of the four unrewritten words, (c) the `s_bfe_u32`, `s_max_u32`, `s_mul_i32`, `s_and_b32` and `s_or_b32` of the rewrite
itself. A copy or numeric read of a word or of a rewritten field, a store, IDXEN, a typed, format or `tbuffer` load, a V#
without the rewrite, a partial V# and a word that a join lost make the candidate not eligible. It is then dropped and keeps
today's path and text; it does not take the texture route off the program. An address formed by `s_add_u32` that is not an
eligible candidate keeps the old rejection of the program for the texture route.
- The analysis of a program with a candidate runs with the byte-window tracking (`heap_window`). If no candidate is
eligible, the program is analysed again exactly as before, so texture-only programs and every other program translate to
the same module and manifest (only the revision line changes).
- The seven token words and the descriptor strides of the program must fit the 32-word entry layout. The strides are known
only while lowering, so a program that does not fit is translated again with the buffer route off: its buffer loads keep
today's path and text.
- The load handler writes the byte address (`so + imm`, or the low word of the added address: the byte offset from heap
slot 0, not a slot index) to the four words. The analysis requires an address that is a multiple of 32, judged on the
state at the load: the heap pointer's high word is the constant 0, `imm % 32 == 0`, an SGPR offset that is constant or
masked with `(mask & 31) == 0`, and no overflow of offset plus immediate; on an added address `imm == 0` and the same mask.
A candidate without that shape is not eligible and keeps today's path and text (the lowering repeats the checks as a
safeguard). The words of the V# must also be exactly the symbols the lowering compares.
- The consumer reads the token at that address: `valid = word0 == magic && id < count && dims[min(id, count - 1)] == 0x20`, and
uses `ResourceDescriptorHeap[valid ? baseraw + id : nullraw]` as a raw buffer. A miss is counted as for textures. No
`heap_buffer_srv` binding is created.
- Two more `b0` words follow the five texture words, only in a program with an eligible buffer load: `baseraw`, `nullraw`
(`NativeComputeShader::heap_token_word_count` is 0, 5 or 7). The manifest names them `udsrc K heaptok baseraw|nullraw`.
- The dimension table gains the code `0x20`: a raw buffer view (next to 0 none, else 1 + the image dimension).
The production writer of that table is the runtime change (PR 2); the fixture writes it for its two raw ids.

## Revision

`native_compute_output_revision` is 3. Revision 2 (#3544) published texture token loads that were refused before; every revision-1 manifest then reads as another compiler revision and is rebuilt once, also those without a token load. Revision 3 does the same for raw buffer token loads. `X2W_NATIVE_SHADER_REVISION` hashes the translator sources but not
`native_compute_pipeline.cpp` or `native_compute_output.h`, and the manifest of a program with a token buffer has a new
shape, so the marker is bumped by hand. A request the previous translator refused published nothing, so it is translated
again by the next run (checked: the previous `compile_dxil` refuses the fixture request and leaves its cache empty, the
new one compiles it from the same cache folder).

## Runtime assumptions and failure modes

- Carry of an added address: the high word of the `s_add_u32` pair is taken as 0. This relies on the same heap-relative
model as the existing heap_offset path (the model is relative to the heap base, so the high word is the carry of the byte
offset alone, which is 0 for offsets below 2^32; the emitter writes it as 0); no runtime carry check.
- A1: a token in the bound view heap's memory names the view the title created for that slot. The runtime (PR 2) builds the
raw view per id from the view record, so its byte range is that view's range. Words 2 (num_records) and 3 of the title's
own V# are not read: the raw view's range applies, and PR 2 must make it equal the range the title's V# describes.
Nothing in the translator or the shader checks that.
- Failure modes, all read the null raw view (zeros) and count a miss, never a wrong-kind view: no token (the title wrote a raw
V# itself, or the slot was never written because the token write is gated on a title-backed writable view), a wrong magic,
`id >= count`, a content token (word 1 carries flags), a texture id (dimension code not 0x20).
- Not caught: a stale token whose id was released and re-used for another buffer view; the shader then reads the new buffer.
PR 2 must not re-use ids while a stale token can exist (no id reuse, or a generation in word 2 or 3 that this route checks
once PR 2 defines it).
- A2: an out-of-bounds raw buffer read returns 0 (the D3D12 raw buffer bounds rule, an external specification not verified in
this repository; the fixture checks it for lanes past the end of the first raw view on one adapter).
- The runtime reader still refuses every `heaptok` line ("heap token textures are not bound yet"), including `baseraw` and
`nullraw`; programs of this change stay refused at dispatch until PR 2.

## Tests

- `native_heap_token_texture_controls`: eligible programs (SGPR offset, added address, scalar buffer load, other widths, an
immediate offset, a texture next to a buffer) and the published manifest; controls that keep today's text (plain V# without
the rewrite, IDXEN, format, `tbuffer`, store, a numeric read of word 2, a copy of a rewritten word, a partial V#, a join, an
immediate offset that breaks the descriptor layout), address shapes without a proven range (overflow, a high word that is
not the constant 0, an unmasked or misaligned offset, an immediate on an added address; all dropped, today's text), a byte
window load in a loop (dropped by the analysis: the lowering would refuse it with `heap byte window loaded inside a loop`
and take the whole program down), the word budget (25 descriptor strides fit next to seven token words, 26 do not and the
buffer route is dropped), and the mixed cases (a texture on the token route next to a buffer load, an unread load or a
loop that keeps today's refusal).
- `native_heap_token_texture_warp` and `_hardware`: modes `rawbuf`, `rawbuf_waterfall` and `sbuf` with two raw views (64 and
128 dwords) and a texture id in the dimension table: valid tokens read the view (lanes past the first view get 0), every
other case gives zeros and one miss per wave. Results are compared as dwords.
Loading
Loading