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
12 changes: 12 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1936,6 +1936,18 @@ if(TARGET compile_dxil AND EXISTS "${DXC_BIN_DIR}/dxcompiler.dll" AND EXISTS "${
"${CMAKE_SOURCE_DIR}/tests/native_direct_container_check.py" --graphics-sampler-pso
"${DXC_BIN_DIR}" "$<TARGET_FILE:native_recording_check>")
set_tests_properties(native_graphics_comparison_pso_warp PROPERTIES SKIP_RETURN_CODE 77)
# Cycle 1 of the global SRV work: ResourceDescriptorHeap[index] from a native DXIL module against a DXC reference.
add_executable(native_heap_handle_probe tests/native_heap_handle_probe.cpp)
target_include_directories(native_heap_handle_probe PRIVATE src)
target_compile_features(native_heap_handle_probe PRIVATE cxx_std_17)
target_link_libraries(native_heap_handle_probe PRIVATE x2w_shader_compiler dxgi d3d12)
set_property(TARGET native_heap_handle_probe PROPERTY MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")
foreach(adapter IN ITEMS warp hardware)
add_test(NAME native_heap_handle_probe_${adapter} COMMAND "${INTAKE_PYTHON}" -B
"${CMAKE_SOURCE_DIR}/tests/native_heap_handle_probe_check.py" "$<TARGET_FILE:native_heap_handle_probe>"
"${DXC_BIN_DIR}" ${adapter})
set_tests_properties(native_heap_handle_probe_${adapter} PROPERTIES SKIP_RETURN_CODE 77 TIMEOUT 120)
endforeach()
add_test(NAME native_recording_pages_warp COMMAND native_recording_check)
add_test(NAME native_dispatch_states_warp COMMAND native_recording_check --subresources)
set_tests_properties(native_dispatch_states_warp PROPERTIES SKIP_RETURN_CODE 77)
Expand Down
102 changes: 102 additions & 0 deletions docs/experiments/native-global-srv-heap-replay.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
# GPU-selected global SRV heap slots: capture and replay of view types

Issue #2908, branch `cpp-migration`. Builds on the heap read in the direct C++ DXIL path from #2912
(`ResourceDescriptorHeap[slot]` for a raw buffer read, `src/gfx1013_direct_compute.inl` `heap_resource`).

## 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
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`.
- 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.
- `src/startup_stubs/native_global_srv.h` writes and reads version 2 of the view metadata, which adds the buffer size:

```
x2w-global-srv-views 2
select <slot>
need <type>
slot <n> <type> <stride> <bytes>
```

- `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
`.views` file is not part of the `.cdx`/`.root` content key: it describes one captured dispatch.

## Meaning of "native recording" and "runtime path" here

The issue's reproduction names `native_recording_check` and `native_dispatch_states_warp`, which test
`x2w_startup::NativeRecording` (descriptor-table pages) and `x2w_startup::NativeDispatchStates` (dispatch barriers). On
`cpp-migration` only tests use these classes. The D3D12 stand-in translates slot 67 requests natively since #2958
(opt-in `X2W_NATIVE_SHADERS`) through `translate_compute` without a heap view contract, so a GPU-selected heap read is
still refused there; passing captured views into that path is follow-up work. The fixture's `record`
mode records the dispatch with both classes and applies the view guard before `Dispatch`; that is the native recording
path checked here, and its unchanged UAV sentinels are the "runtime path" sentinels of the issue. The startup runtime
itself is not covered.

The slot is selected by GPU data. The `record` mode reads the selector back from its GPU buffer before recording the
dispatch (one copy to a readback buffer and one fence per recorded dispatch) and uses that value as the captured
selection, so the guard checks the slot the GPU input actually selects, before the shader runs.

## Fixture

`tests/native_global_srv_heap_replay.cpp` with `tests/native_global_srv_heap_replay_check.py`. A source-built compute
container (two root inputs: a descriptor table in s[0:1], the descriptor heap in s[2:3]; instruction words assembled with
LLVM 15 `clang -cc1as -triple amdgcn-amd-amdhsa -target-cpu gfx1013 -show-encoding`) loads the selector from t0, loads
the descriptor of `ResourceDescriptorHeap[selector]`, reads 64 words through it and stores them to u0. Heap slots: 0 and
1 raw buffers with different data, 2 a Texture2D, 3 a typed buffer. u0 holds 72 words; the last 8 and, on a refused
selection, all 72 must keep the sentinel `0x69696969`.

The check runs, in order:

1. `compile_dxil --requests` on the request without `.views`: refused with the heap text, no `.cso`.
2. `record` for selections 0 and 1: fenced output equals the selected slot's data, sentinels intact, `.views` captured.
Selections 2 and 3: "global SRV slot 2 has view type texture, the shader needs raw_buffer" (and `typed_buffer` for 3)
before the dispatch, nothing dispatched, sentinels intact.
3. Discrimination control: `record --no-guard` on slot 0 expecting a refusal sees the changed sentinels (exit 3). A
texture slot is not dispatched without the guard: reading a texture descriptor as a raw buffer is undefined in D3D12.
4. `compile_dxil --requests` on each captured selection: 0 and 1 compile, 2 and 3 are refused with the slot/type text
and publish no `.cso`.
5. `replay` of the compiled `.cso` for 0 and 1 matches the CPU reference; the `.views` of 2 and 3 are refused before the
dispatch with sentinels intact.
6. Mutation control: replaying selection 0 against the slot 1 reference reports a mismatch.
7. Negative controls on both `compile_dxil` and `replay`: slot 0 rewritten to `texture`, no `need` line, no `select`
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.

## Result

```
cmake --preset dev
cmake --build --preset dev
ctest --preset dev --no-tests=error -R "^native_global_srv_heap_replay(_warp|_hardware)?$" -V
```

- `native_global_srv_heap_replay_hardware`: passed, 70 assertions (NVIDIA GeForce RTX 3080, driver 32.0.16.2124,
Windows 11 build 26200, MSVC toolset 14.51.36231, Windows SDK 10.0.26100 DXC, D3D12 debug layer not enabled).
- `native_global_srv_heap_replay_warp`: skipped (77), "adapter has no 32-lane waves (4..4)". The native module declares
`WaveSize=32`; WARP offers 4-lane waves only. The skip is not evidence for the fixture.
- `ctest --preset dev --no-tests=error -R ^native_ -j 1`: same result as before the change apart from the two new tests
(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".

## Limits

- Only raw buffer heap reads are translated; other view classes are refused with a named diagnostic.
- 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,
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.
- The captured `select` must be the slot index the dispatch addresses; producing it from a title capture is outside this
fixture.
11 changes: 8 additions & 3 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::byte_window_srv)));
auto kind = static_cast<ComputeViewKind>(in.count(static_cast<unsigned>(ComputeViewKind::heap_buffer_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 @@ -307,12 +307,17 @@ size_t index_graphics_interfaces(const fs::path &source, const fs::path &cache)
return count;
}
NativeComputeShader prepare_direct_compute(const std::filesystem::path &input, unsigned entry, bool container,
const std::filesystem::path &cache, bool &cache_hit) {
const std::filesystem::path &cache, bool &cache_hit,
HeapViewContract heap_view) {
if (!container && heap_view != HeapViewContract::none)
throw std::runtime_error("a heap view contract needs a container request");
ComputeRequest request{};
ComputeContainer packed;
Bytes identity = X2W_NATIVE_SHADER_REVISION;
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";
if (container) {
packed = read_compute_container(input);
identity += shader_digest(packed.bytes) + shader_digest(packed.root);
Expand All @@ -334,7 +339,7 @@ NativeComputeShader prepare_direct_compute(const std::filesystem::path &input, u
}
}
if (!cache_hit) {
program = container ? translate_compute_container(std::move(packed)) : translate_compute(request, entry, true);
program = container ? translate_compute_container(std::move(packed), heap_view) : translate_compute(request, entry, true);
publish(path, key, encode(program));
}
return program;
Expand Down
3 changes: 2 additions & 1 deletion src/direct_compute_cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ size_t index_graphics_interfaces(const std::filesystem::path &source, const std:
// Signature component (f32/u32/i32) for supported typed IA and RTV formats.
unsigned dxil_io_component(unsigned dxgi_format);
NativeComputeShader prepare_direct_compute(const std::filesystem::path &input, unsigned entry, bool container,
const std::filesystem::path &cache, bool &cache_hit);
const std::filesystem::path &cache, bool &cache_hit,
HeapViewContract heap_view = HeapViewContract::none);
DirectGraphicsPair prepare_direct_graphics(std::string_view a, std::string_view b,
const std::filesystem::path &cache, bool &cache_hit,
std::string_view root = {}, std::string_view input_layout = {},
Expand Down
19 changes: 19 additions & 0 deletions src/dxil_module.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class DxilModule {
};
std::ostringstream body, setup, globals;
bool uses_wave = false;
bool heap_used = false;
unsigned features = 0;
struct Signature {
Text name;
Expand Down Expand Up @@ -141,6 +142,18 @@ class DxilModule {
<< " })\n";
return h;
}
// A handle taken from the descriptor heap at a run-time index (SM 6.6 ResourceDescriptorHeap[index]), annotated with
// the view class the shader reads: `kind` is the DXIL resource kind (11 raw buffer, 2 Texture2D) and `props` the
// second ResourceProperties word. No resource record is declared: the binding is the whole heap.
Text heap_handle(const Text &index, unsigned kind, unsigned props, bool uav = false) {
heap_used = true;
const Text h = "%heap" + std::to_string(sequence++);
body << " " << h << "b = call %dx.types.Handle @dx.op.createHandleFromHeap(i32 218, i32 " << index
<< ", i1 false, i1 true)\n"
<< " " << h << " = call %dx.types.Handle @dx.op.annotateHandle(i32 216, %dx.types.Handle " << h
<< "b, %dx.types.ResourceProperties { i32 " << (kind + (uav ? 4096 : 0)) << ", i32 " << props << " })\n";
return h;
}
Text finish_compute(unsigned x, unsigned y, unsigned z, const Text &stage = "cs") {
std::ostringstream out;
out << "target datalayout = "
Expand Down Expand Up @@ -258,6 +271,12 @@ class DxilModule {
out << globals.str() << "define void @main() {\nentry:\n" << setup.str() << body.str() << "}\n";
for (auto &[name, decl] : declarations)
out << decl << "\n";
if (heap_used) {
out << "declare %dx.types.Handle @dx.op.createHandleFromHeap(i32,i32,i1,i1) nounwind readnone\n";
if (resources.empty())
out << "declare %dx.types.Handle "
"@dx.op.annotateHandle(i32,%dx.types.Handle,%dx.types.ResourceProperties) nounwind readnone\n";
}
if (!resources.empty())
out << "declare %dx.types.Handle @dx.op.createHandleFromBinding(i32,%dx.types.ResBind,i32,i1) nounwind "
"readnone\n"
Expand Down
10 changes: 6 additions & 4 deletions src/gfx1013_compute_native.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1396,7 +1396,8 @@ void lower_blocks(Emit &e) {
#include "gfx1013_direct_compute.inl"
#include "gfx1013_direct_graphics.inl"
} // namespace
static NativeComputeShader translate_program(const ComputeRequest &request, unsigned entry, bool direct_dxil, const GuestShaderStage *stage) {
static NativeComputeShader translate_program(const ComputeRequest &request, unsigned entry, bool direct_dxil, const GuestShaderStage *stage,
HeapViewContract heap_view) {
if (entry == 36 && ((word(request.code, 0) >> 16) != 0xd580 || (word(request.code, 12) >> 16) != 0xd580 ||
(word(request.code, 24) >> 16) != 0xd580 || (word(request.code, 36) >> 16) != 0xbfa0))
refuse(entry, "unrecognized compute header");
Expand Down Expand Up @@ -1509,6 +1510,7 @@ static NativeComputeShader translate_program(const ComputeRequest &request, unsi
if (direct_dxil) {
DirectCompute compiler{emit};
compiler.wave64 = wave64;
compiler.heap_view = heap_view;
if (threads % (wave64 ? 64 : 32))
for (const auto &i : program.code)
if (i.name == "v_readlane_b32" || i.name == "v_writelane_b32")
Expand Down Expand Up @@ -1688,10 +1690,10 @@ void x2w::apply_scalar_table_layout(ComputeViewBinding &binding, const D3D12_ROO
}

x2w::NativeComputeShader x2w::translate_compute(const ComputeRequest &request, unsigned entry, bool direct_dxil) {
return translate_program(request, entry, direct_dxil, nullptr);
return translate_program(request, entry, direct_dxil, nullptr, HeapViewContract::none);
}

x2w::NativeComputeShader x2w::translate_compute_container(ComputeContainer container) {
x2w::NativeComputeShader x2w::translate_compute_container(ComputeContainer container, HeapViewContract heap_view) {
auto stages = read_guest_shader_container(container.bytes);
GuestShaderStage *compute = nullptr;
for (auto &stage : stages) {
Expand All @@ -1703,7 +1705,7 @@ x2w::NativeComputeShader x2w::translate_compute_container(ComputeContainer conta
ComputeRequest request{};
request.code = std::move(compute->code);
request.root = std::move(container.root);
return translate_program(request, 0, true, compute);
return translate_program(request, 0, true, compute, heap_view);
}

x2w::NativeComputeShader x2w::translate_graphics_container(const std::string &bytes,bool pixel) {
Expand Down
12 changes: 10 additions & 2 deletions src/gfx1013_compute_native.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,12 @@ enum class ComputeViewKind {
cube_comparison_srv,
indexed_atomic_uav,
d16_sample_srv,
byte_window_srv
byte_window_srv,
heap_buffer_srv // A raw buffer read through ResourceDescriptorHeap[slot]; the slot is a GPU value.
};
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_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 @@ -106,9 +109,14 @@ struct NativeComputeShader {
bool vertex_fetch = false;
std::array<unsigned, 8> pixel_writes{};
};
// 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).
enum class HeapViewContract : unsigned { none, raw_buffer, other };
NativeComputeShader translate_compute(const ComputeRequest &request, unsigned entry_byte_offset,
bool direct_dxil = false);
NativeComputeShader translate_compute_container(ComputeContainer container);
NativeComputeShader translate_compute_container(ComputeContainer container,
HeapViewContract heap_view = HeapViewContract::none);
struct NativeVertexElement {
std::string semantic;
unsigned index, format, slot, offset, classification, step_rate;
Expand Down
Loading
Loading