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
15 changes: 8 additions & 7 deletions docs/experiments/native-direct-global-load.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ the Python path. Before, the frontend refused it with `unsupported instruction g
## The form

ADDR = SADDR + zext(VADDR) + sext(OFFSET[11:0]) (RDNA 2 ISA, FLAT/GLOBAL addressing). SADDR must be the unadvanced
root address of a root SRV, with its matching high half: the same symbol and binding (`root_buffer_srv`) the
scalar root loads of this path already use. The load becomes a raw buffer load from that view.
root address, with its matching high half. A root CBV or SRV is the `root_buffer_srv` binding the scalar root
loads of this path already use. A root UAV is `root_buffer_uav`. The load becomes a raw buffer load from that view.

- The 32-bit byte offset zext(VADDR) + OFFSET is guarded to [0, 2^32): a positive offset reads 0 when VADDR is
above 2^32 - 1 - OFFSET, a negative one reads 0 when VADDR is below -OFFSET. Above the range, the unguarded
Expand All @@ -22,11 +22,12 @@ scalar root loads of this path already use. The load becomes a raw buffer load f
- The binding's minimum size grows to the immediate offset plus 4 bytes for a positive offset; other loads keep
the size the scalar loads need.

Refused by name: the `off` form (no SADDR), the FLAT and scratch segments, other opcodes (stores,
atomics), the `lds` form, an SADDR that is not a root address, a replaced high half, and an unaligned immediate
offset. An SADDR advanced by scalar arithmetic is refused earlier, as an unbound numeric SGPR; `gfx2hlsl.py` accepts
a pointer advanced by a known literal. Graphics stages never decode the FLAT encoding and keep
`unsupported instruction` (not covered by the fixture).
Refused by name: the `off` form (a VGPR address pair, not a root offset), the FLAT and scratch segments,
sub-DWORD opcodes and atomics, the `lds` form, an SADDR that is not a root address, a replaced high half, and an
unaligned immediate offset. DWORD stores through a root UAV are a separate lowering
([root-UAV global stores](native-direct-global-store.md)). An SADDR advanced by scalar arithmetic is refused
earlier, as an unbound numeric SGPR; `gfx2hlsl.py` accepts a pointer advanced by a known literal. Graphics stages
never decode the FLAT encoding and keep `unsupported instruction` (not covered by the fixture).

## Limits

Expand Down
49 changes: 49 additions & 0 deletions docs/experiments/native-direct-global-store.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# Root-UAV global stores in the direct C++ compute path

The direct compute frontend (`src/gfx1013_compute_native.cpp`, `src/gfx1013_direct_compute.inl`) lowers
`global_store_dword`, `global_store_dwordx2`, `global_store_dwordx3` and `global_store_dwordx4` in the `saddr`
form. The same root UAV can be read with `global_load_dword` and the wider loads that the root-SRV path already
accepts. A root CBV or SRV stays load-only.

ADDR = SADDR + zext(VADDR) + sext(OFFSET[11:0]). SADDR is the unadvanced root UAV pair. The 32-bit VGPR is the
byte offset. The immediate must be a multiple of 4. The low two address bits are dropped. A store whose first or
last DWORD would wrap a 32-bit offset is skipped. A load in that case returns 0. `glc`, `slc` and `dlc` are
cache hints and are accepted. The binding's minimum size is the largest positive immediate plus the transfer
width.

Still refused: the `off` form (the address is a VGPR pair, not a root plus a 32-bit offset), FLAT and scratch,
an LDS result, byte and short ops, atomics, `saddr` 127, a store through a root that is not a UAV, an unaligned
immediate, a data range past v255, and a scalar address that is not the matching root pair.

The view has to cover every byte the program writes. An address outside that view does not reach the guest
memory that would have been there.

This is a direct-translation lowering. The startup runtime manifest and the native dispatch path have no
case for `root_buffer_uav`, as they have none for `root_buffer_srv`. A program that reaches those paths is
refused with `UAV kind root_buffer_uav` rather than dispatched.

## Fixture

`tests/native_global_store_check.py`

```powershell
ctest --preset dev --no-tests=error -R '^native_global_store_encodings$' -V
ctest --preset dev --no-tests=error -R '^native_global_store_hardware$' -V
```

`encodings` only disassembles the source-built words. `hardware` needs `compile_dxil`, `native_store_replay` and
shader model 6.6 with wave32. A missing wave32 adapter is a skip (77), not a pass.

The readback checks one root UAV across two groups: the four store widths, an address that aliases its data, a
negative immediate, even lanes under a narrowed EXEC, a guarded load of vaddr 0xfffffff4 offset 16, and a
poison store at that address which must not appear. An x4 at offset 16 uses a data-dependent vaddr whose first
DWORD stays inside the 32-bit range and whose last DWORD does not; the whole store is skipped. The desktop
compiler prints that bound as a signed i32 (`-29`, or `-28` if `ule` becomes `ult`) and drops the compare when
the address is a constant. The address add uses the same `-28`, so the check matches only the compare. Dropping
`(count - 1) * 4` from `last_offset` changes the ule bound to `-17` (`2^32 - 1 - 16`); `-16` is that mutant's
`ult` form. The hardware test fails on either. That store's base is near 2^32, outside the view, so the
readback cannot show the mistake.
After the stores and `s_waitcnt`, each lane loads the DWORD it wrote at the start of its record and writes it
to a free slot. A second program binds two root SRVs and one root UAV and must report `t0`, `t1` and `u0`.
Swapping the two dwords of the x2 reference changes the expected buffer. The x4 at immediate 2044 asks for a
2060-byte view and is not dispatched.
6 changes: 3 additions & 3 deletions src/direct_compute_cache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,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_typed_srv)));
auto kind = static_cast<ComputeViewKind>(in.count(static_cast<unsigned>(ComputeViewKind::root_buffer_uav)));
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;
Expand Down Expand Up @@ -898,9 +898,9 @@ static const char *view_kind_name(ComputeViewKind kind) {
"comparison_texture_srv", "half2_texture_uav", "half4_texture_uav", "cube_sample_srv", "cube_comparison_srv",
"indexed_atomic_uav", "d16_sample_srv", "byte_window_srv", "heap_buffer_srv", "heap_sampler",
"uint_texture_uav", "float_texture_uav", "float2_texture_uav", "float3_texture_uav", "packed_d16_x_uav",
"indexed_raw_srv"};
"indexed_raw_srv", "heap_typed_srv", "root_buffer_uav"};
// ponytail: catches a renamed last kind, not one appended after it; extend both together.
static_assert(std::size(names) == static_cast<size_t>(ComputeViewKind::indexed_raw_srv) + 1);
static_assert(std::size(names) == static_cast<size_t>(ComputeViewKind::root_buffer_uav) + 1);
const auto index = static_cast<size_t>(kind);
return index < std::size(names) ? names[index] : "unknown";
}
Expand Down
47 changes: 33 additions & 14 deletions src/gfx1013_compute_native.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -271,14 +271,24 @@ Inst decode(const string &bytes, unsigned pc,bool direct=false, bool graphics=fa
i.enc = Encoding::Mubuf;
i.dst = (i.b >> 8) & 255;
} else if (direct && !graphics && (a & 0xfc000000u) == 0xdc000000u) {
// FLAT encoding, segment 2 (GLOBAL), with an SGPR-pair base:
// ADDR = SADDR + zext(VADDR) + sext(OFFSET).
// SADDR 125 is `off` on gfx10. LDS, the other segments and opcodes stay refused.
// FLAT encoding. Global DWORD loads and stores use an SGPR-pair base:
// ADDR = SADDR + zext(VADDR) + sext(OFFSET). SADDR 125 is the VGPR-pair `off` form.
i.b = take();
i.op = (a >> 18) & 127;
const unsigned saddr = (i.b >> 16) & 127;
if (((a >> 14) & 3) != 2 || i.op < 12 || i.op > 15 || (a & 0x2000u) || saddr == 125 || saddr == 127)
refuse(pc, "direct global memory supports only DWORD loads with a scalar address pair");
const unsigned segment = (a >> 14) & 3;
const bool dword_load = i.op >= 12 && i.op <= 15;
const bool dword_store = i.op >= 0x1c && i.op <= 0x1f;
if (segment != 2)
refuse(pc, "direct flat or scratch memory is not a global root access");
if (a & 0x2000u)
refuse(pc, "direct global memory does not lower an LDS result");
if (saddr == 125)
refuse(pc, "direct global memory with a VGPR address pair is not a root buffer offset");
if (saddr == 127)
refuse(pc, "direct global memory scalar address is not an SGPR pair");
if (!(dword_load || dword_store))
refuse(pc, "direct global memory supports only DWORD loads and stores with a scalar address pair");
i.enc = Encoding::Mubuf;
i.name = encoded.name;
i.dst = i.b >> 24;
Expand Down Expand Up @@ -591,13 +601,15 @@ static void clear_vector_spills(const Inst &i, State &s) {
}
// FSSM root descriptors carry a guest address in two SGPRs. Preserve the
// identity of each half through scalar moves and CFG joins (Victory resource.rs).
void bind_root_address(State &state, const ShaderRootInput &input, unsigned limit) {
// resource_class 0 is a root CBV or SRV. resource_class 1 is a root UAV. Both halves carry it so a
// copied pair still matches.
void bind_root_address(State &state, const ShaderRootInput &input, unsigned limit, unsigned resource_class = 0) {
const unsigned at = input.user_data;
if (input.sgpr_count != 2 || at >= limit || limit - at < 2 || input.root_parameter >= 16 ||
if (resource_class > 1 || input.sgpr_count != 2 || at >= limit || limit - at < 2 || input.root_parameter >= 16 ||
input.offset_dwords || state[at].type != Symbol::Type::unbound || state[at+1].type != Symbol::Type::unbound)
refuse(0, "invalid FSSM root descriptor pair");
for (unsigned half = 0; half < 2; ++half)
state[at + half] = {Symbol::Type::root_address, input.root_parameter, half};
state[at + half] = {Symbol::Type::root_address, input.root_parameter, half, resource_class};
}
Symbol source(const Inst &i, unsigned code, const State &s) {
if (code < 128)
Expand Down Expand Up @@ -1827,7 +1839,9 @@ static NativeComputeShader translate_program(const ComputeRequest &request, unsi
// occupies two consecutive SGPRs, even when only later parameters are used.
if (next > user || user - next < 2)
refuse(entry, "root descriptor pair exceeds user SGPRs");
bind_root_address(initial, {1, 0, 2, 0, static_cast<uint8_t>(next), static_cast<uint8_t>(i)}, user);
const unsigned root_class = param.ParameterType == D3D12_ROOT_PARAMETER_TYPE_UAV ? 1u : 0u;
bind_root_address(initial, {1, 0, 2, 0, static_cast<uint8_t>(next), static_cast<uint8_t>(i)}, user,
root_class);
next += 2;
} else
refuse(entry, "root-address parameter");
Expand Down Expand Up @@ -1917,13 +1931,17 @@ static NativeComputeShader translate_program(const ComputeRequest &request, unsi
if (param.ParameterType == D3D12_ROOT_PARAMETER_TYPE_DESCRIPTOR_TABLE)
apply_scalar_table_layout(binding, param.DescriptorTable);
}
if (binding.kind != ComputeViewKind::root_buffer_srv) continue;
if (binding.kind != ComputeViewKind::root_buffer_srv &&
binding.kind != ComputeViewKind::root_buffer_uav)
continue;
if (binding.root_parameter >= desc.NumParameters)
refuse(entry, "missing compute root descriptor");
const auto &param = desc.pParameters[binding.root_parameter];
if ((param.ParameterType != D3D12_ROOT_PARAMETER_TYPE_CBV &&
param.ParameterType != D3D12_ROOT_PARAMETER_TYPE_SRV) ||
param.ShaderVisibility != D3D12_SHADER_VISIBILITY_ALL)
const bool root_uav = binding.kind == ComputeViewKind::root_buffer_uav;
const bool type_ok = root_uav ? param.ParameterType == D3D12_ROOT_PARAMETER_TYPE_UAV
: (param.ParameterType == D3D12_ROOT_PARAMETER_TYPE_CBV ||
param.ParameterType == D3D12_ROOT_PARAMETER_TYPE_SRV);
if (!type_ok || param.ShaderVisibility != D3D12_SHADER_VISIBILITY_ALL)
refuse(entry, "compute root descriptor type or visibility");
}
}
Expand Down Expand Up @@ -2019,7 +2037,8 @@ uint fpClass(uint v){
bool x2w::apply_reflected_table_use(ComputeViewBinding &binding, const GuestShaderStage &stage,
std::optional<unsigned> packed_sgpr) {
if (binding.array_count != 1 || binding.kind == ComputeViewKind::heap_stride_srv ||
binding.kind == ComputeViewKind::root_buffer_srv || binding.root_parameter == compute_resource_heap)
binding.kind == ComputeViewKind::root_buffer_srv || binding.kind == ComputeViewKind::root_buffer_uav ||
binding.root_parameter == compute_resource_heap)
return false;
const ShaderResourceUse *match = nullptr;
for (const auto &use : stage.resources) {
Expand Down
7 changes: 5 additions & 2 deletions src/gfx1013_compute_native.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ enum class ComputeViewKind {
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.
heap_typed_srv // A one-channel 32-bit typed buffer read the same way (HeapViewContract::typed_dword).
heap_typed_srv, // A one-channel 32-bit typed buffer read the same way (HeapViewContract::typed_dword).
// A raw buffer bound as a root UAV. The guest address is the root pointer plus a VGPR offset.
root_buffer_uav
};
inline unsigned exact_texture_uav_channels(ComputeViewKind kind) {
switch (kind) {
Expand Down Expand Up @@ -78,7 +80,8 @@ struct IndexedAtomicView {
inline bool compute_uav(ComputeViewKind kind) {
return kind == ComputeViewKind::texture_uav || kind == ComputeViewKind::half_scalar_uav ||
kind == ComputeViewKind::half2_texture_uav || kind == ComputeViewKind::half4_texture_uav ||
kind == ComputeViewKind::uint_texture_uav || exact_texture_uav_channels(kind) || compute_buffer_uav(kind);
kind == ComputeViewKind::uint_texture_uav || kind == ComputeViewKind::root_buffer_uav ||
exact_texture_uav_channels(kind) || compute_buffer_uav(kind);
}
struct ComputeViewBinding {
ComputeViewKind kind;
Expand Down
Loading
Loading