From 04252273305f9f6f437a926be0023129e649890e Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Sat, 28 Nov 2026 08:47:48 +0000 Subject: [PATCH 1/6] compute: lower root-UAV global DWORD stores in the direct path Co-authored-by: SimSalabimse --- docs/experiments/native-direct-global-load.md | 15 +- .../experiments/native-direct-global-store.md | 36 +++ src/direct_compute_cache.cpp | 4 +- src/gfx1013_compute_native.cpp | 47 ++- src/gfx1013_compute_native.h | 7 +- src/gfx1013_direct_compute.inl | 88 ++++-- src/native_buffer_store_view.h | 2 +- tests/cmake/native_global_store.cmake | 12 + tests/native_global_load_check.py | 17 +- tests/native_global_store_check.py | 280 ++++++++++++++++++ tests/native_store_replay.cpp | 7 +- 11 files changed, 459 insertions(+), 56 deletions(-) create mode 100644 docs/experiments/native-direct-global-store.md create mode 100644 tests/cmake/native_global_store.cmake create mode 100644 tests/native_global_store_check.py diff --git a/docs/experiments/native-direct-global-load.md b/docs/experiments/native-direct-global-load.md index fe6efbff56..80df18482f 100644 --- a/docs/experiments/native-direct-global-load.md +++ b/docs/experiments/native-direct-global-load.md @@ -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 @@ -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 diff --git a/docs/experiments/native-direct-global-store.md b/docs/experiments/native-direct-global-store.md new file mode 100644 index 0000000000..b30c4f0445 --- /dev/null +++ b/docs/experiments/native-direct-global-store.md @@ -0,0 +1,36 @@ +# 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. + +## 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. 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. diff --git a/src/direct_compute_cache.cpp b/src/direct_compute_cache.cpp index f13051b045..89dc706717 100644 --- a/src/direct_compute_cache.cpp +++ b/src/direct_compute_cache.cpp @@ -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(in.count(static_cast(ComputeViewKind::heap_typed_srv))); + auto kind = static_cast(in.count(static_cast(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(in.count(unsigned(ComputeViewBinding::ScalarTable::srv))); b.requires_identity_dst_sel = in.count(1) != 0; @@ -898,7 +898,7 @@ 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(ComputeViewKind::indexed_raw_srv) + 1); const auto index = static_cast(kind); diff --git a/src/gfx1013_compute_native.cpp b/src/gfx1013_compute_native.cpp index 887f35cc5a..e222c69c44 100644 --- a/src/gfx1013_compute_native.cpp +++ b/src/gfx1013_compute_native.cpp @@ -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; @@ -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) @@ -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(next), static_cast(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(next), static_cast(i)}, user, + root_class); next += 2; } else refuse(entry, "root-address parameter"); @@ -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 ¶m = 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"); } } @@ -2019,7 +2037,8 @@ uint fpClass(uint v){ bool x2w::apply_reflected_table_use(ComputeViewBinding &binding, const GuestShaderStage &stage, std::optional 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) { diff --git a/src/gfx1013_compute_native.h b/src/gfx1013_compute_native.h index 136b2fc754..23855e0e1c 100644 --- a/src/gfx1013_compute_native.h +++ b/src/gfx1013_compute_native.h @@ -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) { @@ -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; diff --git a/src/gfx1013_direct_compute.inl b/src/gfx1013_direct_compute.inl index 0f67b3c63d..e8ef1e07fa 100644 --- a/src/gfx1013_direct_compute.inl +++ b/src/gfx1013_direct_compute.inl @@ -592,31 +592,43 @@ struct DirectCompute { return ir.handle({0, binding->shader_register, 11}); } - // Global DWORD loads in the saddr form: ADDR = SADDR + zext(VADDR) + sext(OFFSET[11:0]) (RDNA 2 ISA, - // FLAT/GLOBAL addressing), with SADDR the unadvanced root address of a root SRV, as for the scalar root loads. - // Guard the first and last DWORD offsets against 32-bit wrap. The low two address bits are dropped, + // Global DWORD memory in the saddr form: ADDR = SADDR + zext(VADDR) + sext(OFFSET[11:0]) (RDNA 2 ISA, + // FLAT/GLOBAL addressing). SADDR is the unadvanced root address. A CBV or SRV is read; a UAV is read or + // written. Guard the first and last DWORD against 32-bit wrap. The low two address bits are dropped, // as for buffer_load_dword in this path (DWORD alignment mode). - void global_load(const Inst &i, const State &s) { - constexpr unsigned widths[]{1, 2, 4, 3}; // GFX10 opcodes 12..15. - const unsigned count = widths[i.op - 12]; - if (i.dst + count > 256) - refuse(i.pc, "global load destination register range"); + void global_root(const Inst &i, const State &s, bool store, unsigned count, string &handle, string &aligned, + string &in_range) { + const char *what = store ? "global store" : "global load"; const unsigned base = (i.b >> 16) & 127; if (base > 104 || (base & 1)) - refuse(i.pc, "global load base must be an aligned SGPR pair"); + refuse(i.pc, string(what) + " base must be an aligned SGPR pair"); const auto &low = s.at(base); if (low.type != Symbol::Type::root_address) - refuse(i.pc, "global load base is not a root buffer address"); - const Symbol high{Symbol::Type::root_address, low.value, 1}; + refuse(i.pc, string(what) + " base is not a root buffer address"); + const Symbol high{Symbol::Type::root_address, low.value, 1u, low.index_mask}; if (low.offset || !(s.at(base + 1) == high)) - refuse(i.pc, "global load base is an advanced or mismatched root address"); + refuse(i.pc, string(what) + " base is an advanced or mismatched root address"); + if (store && low.index_mask != 1) + refuse(i.pc, "global store base is not a root UAV"); const int offset = int(Word(i.a) << 20) >> 20; if (offset % 4) - refuse(i.pc, "global load at an unaligned immediate offset"); - const unsigned span = static_cast((std::max)(offset, 0)) + count * 4; - const auto h = root_buffer_handle(low.value, span); + refuse(i.pc, string(what) + " at an unaligned immediate offset"); + const unsigned span = static_cast((std::max)(offset, 0)) + count * 4u; + const bool uav = low.index_mask == 1; + const auto kind = uav ? ComputeViewKind::root_buffer_uav : ComputeViewKind::root_buffer_srv; + auto &bindings = analysis.result.bindings; + auto binding = std::find_if(bindings.begin(), bindings.end(), [&](const auto &b) { + return b.kind == kind && b.root_parameter == low.value && b.byte_offset == 0; + }); + if (binding == bindings.end()) { + const unsigned reg = uav ? analysis.uav++ : analysis.srv++; + bindings.push_back({kind, low.value, 0u, reg, span}); + binding = std::prev(bindings.end()); + } else + binding->required_bytes = (std::max)(binding->required_bytes, span); + ir.features |= 16; + handle = ir.handle({unsigned(uav), binding->shader_register, 11u}); const auto vaddr = read(256 + (i.b & 255)); - string in_range; if (offset < 0) in_range = cmp("uge", vaddr, to_string(-offset)); const int last_offset = offset + static_cast((count - 1) * 4); @@ -625,7 +637,16 @@ struct DirectCompute { in_range = in_range.empty() ? upper : ir.value("and i1 " + in_range + ", " + upper); } const auto addr = offset ? ir.binary("add", vaddr, to_string(offset)) : vaddr; - const auto ret = load_buffer(h, ir.binary("and", addr, "4294967292"), true, count); + aligned = ir.binary("and", addr, "4294967292"); + } + void global_load(const Inst &i, const State &s) { + constexpr unsigned widths[]{1u, 2u, 4u, 3u}; // GFX10 opcodes 12..15. + const unsigned count = widths[i.op - 12]; + if (i.dst + count > 256) + refuse(i.pc, "global load destination register range"); + string handle, aligned, in_range; + global_root(i, s, false, count, handle, aligned, in_range); + const auto ret = load_buffer(handle, aligned, true, count); for (unsigned j = 0; j < count; ++j) { auto value = ir.value("extractvalue %dx.types.ResRet.i32 " + ret + ", " + to_string(j)); if (!in_range.empty()) @@ -633,11 +654,40 @@ struct DirectCompute { vector_write(256 + i.dst + j, value); } } + void global_store(const Inst &i, const State &s) { + constexpr unsigned widths[]{1u, 2u, 4u, 3u}; // GFX10 opcodes 0x1c..0x1f. + const unsigned count = widths[i.op - 0x1c]; + const unsigned data = (i.b >> 8) & 255; + if (data + count > 256) + refuse(i.pc, "global store data register range"); + string handle, aligned, in_range; + global_root(i, s, true, count, handle, aligned, in_range); + std::array values; + for (unsigned j = 0; j < count; ++j) + values[j] = read(256 + data + j); + string pred = active(); + if (!in_range.empty()) + pred = ir.value("and i1 " + pred + ", " + in_range); + const string tag = to_string(label_number++); + ir.body << " br i1 " << pred << ", label %gstore" << tag << ", label %gstoreEnd" << tag << "\ngstore" << tag + << ":\n"; + std::vector> args{{"%dx.types.Handle", handle}, {"i32", aligned}, {"i32", "undef"}}; + for (unsigned j = 0; j < 4; ++j) + args.emplace_back("i32", j < count ? values[j] : "undef"); + args.emplace_back("i8", to_string((1u << count) - 1u)); + args.emplace_back("i32", "4"); + ir.invoke("void", "rawBufferStore.i32", 140, args, "nounwind"); + ir.body << " br label %gstoreEnd" << tag << "\ngstoreEnd" << tag << ":\n"; + } void memory(const Inst &i, const State &s) { if (i.name.substr(0, 17) == "global_load_dword") { global_load(i, s); return; } + if (i.name.substr(0, 18) == "global_store_dword") { + global_store(i, s); + return; + } if (i.enc == Encoding::Mubuf && i.name.substr(0, 8) == "tbuffer_") { formatted_byte_load(i, s); return; @@ -744,9 +794,9 @@ struct DirectCompute { unsigned off = i.b & 0xfffff, n = memory_count(i); string h; if (root) { - const Symbol high{Symbol::Type::root_address, low.value, 1}; + const Symbol high{Symbol::Type::root_address, low.value, 1u, low.index_mask}; const bool stride_one = i.op >= 8 && s.at(i.src[0] + 1) == - Symbol{Symbol::Type::root_stride_one_high, low.value, 1}; + Symbol{Symbol::Type::root_stride_one_high, low.value, 1u}; if (low.offset || (!(s.at(i.src[0] + 1) == high) && !stride_one)) refuse(i.pc, "mismatched root address high half"); // Like Victory's Source::Built, retain the root identity through diff --git a/src/native_buffer_store_view.h b/src/native_buffer_store_view.h index 8e2ec3c099..5c631e58f0 100644 --- a/src/native_buffer_store_view.h +++ b/src/native_buffer_store_view.h @@ -54,7 +54,7 @@ inline bool native_buffer_store_view(const ComputeViewBinding &binding, D3D12_UN } if (view.Buffer.StructureByteStride) return false; UINT stride = binding.required_bytes; - if (binding.kind == ComputeViewKind::raw_buffer_uav) { + if (binding.kind == ComputeViewKind::raw_buffer_uav || binding.kind == ComputeViewKind::root_buffer_uav) { if (view.Format != DXGI_FORMAT_R32_TYPELESS || view.Buffer.Flags != D3D12_BUFFER_UAV_FLAG_RAW || view.Buffer.FirstElement % (D3D12_RAW_UAV_SRV_BYTE_ALIGNMENT / 4)) return false; diff --git a/tests/cmake/native_global_store.cmake b/tests/cmake/native_global_store.cmake new file mode 100644 index 0000000000..c02ff86cbd --- /dev/null +++ b/tests/cmake/native_global_store.cmake @@ -0,0 +1,12 @@ +# Direct C++ compute: GLOBAL DWORD stores through a root UAV. +# encodings checks the source-built words. hardware compiles them and reads the buffer back. +if(EXISTS "${INTAKE_PYTHON}") + add_test(NAME native_global_store_encodings COMMAND "${INTAKE_PYTHON}" -B + "${CMAKE_CURRENT_SOURCE_DIR}/tests/native_global_store_check.py" encodings) +endif() +if(EXISTS "${INTAKE_PYTHON}" AND TARGET compile_dxil AND TARGET native_store_replay) + add_test(NAME native_global_store_hardware COMMAND "${INTAKE_PYTHON}" -B + "${CMAKE_CURRENT_SOURCE_DIR}/tests/native_global_store_check.py" "$" + "$" "${DXC_BIN_DIR}") + set_tests_properties(native_global_store_hardware PROPERTIES SKIP_RETURN_CODE 77 TIMEOUT 120) +endif() diff --git a/tests/native_global_load_check.py b/tests/native_global_load_check.py index 2c989f023e..4fc7d8d2f5 100644 --- a/tests/native_global_load_check.py +++ b/tests/native_global_load_check.py @@ -55,19 +55,19 @@ def words(hexes): EPILOGUE = words('70008cbf 001070e008140a80 041070e008150a80 081070e008160a80 0c1070e008170a80 ' '101070e008180a80 000081bf') REFUSALS = [ - ('008030dc01007d14', 'DWORD loads with a scalar address pair'), # v[1:2], off + ('008030dc01007d14', 'VGPR address pair is not a root buffer offset'), # v[1:2], off ('008030dc01001414', 'global load base is not a root buffer address'), # s[20:21], a table pointer ('028030dc01000414', 'global load at an unaligned immediate offset'), # offset:2 ('008034dc010004ff', 'global load destination register range'), # x2 beginning at v255 ('00803cdc010004fe', 'global load destination register range'), # x3 beginning at v254 ('008038dc010004fd', 'global load destination register range'), # x4 beginning at v253 - ('008070dc01140400', 'DWORD loads with a scalar address pair'), # global_store_dword + ('008070dc01140400', 'global store base is not a root buffer address'), # global_store_dword through the table pointer ('800385be 008030dc01000414', 'global load base is an advanced or mismatched root address'), # s_mov_b32 s5, 0 ('04900480 008030dc01000414', 'unbound numeric SGPR s4'), # s_add_u32 s4, s4, 16 on the root address - ('00a030dc01000414', 'DWORD loads with a scalar address pair'), # the lds bit - ('000030dc01007d14', 'DWORD loads with a scalar address pair'), # flat_load_dword (segment 0) - ('004030dc01007d14', 'DWORD loads with a scalar address pair'), # scratch_load_dword (segment 1) - ('0080c8dc01140400', 'DWORD loads with a scalar address pair'), # global_atomic_add + ('00a030dc01000414', 'does not lower an LDS result'), # the lds bit + ('000030dc01007d14', 'flat or scratch memory is not a global root access'), # flat_load_dword (segment 0) + ('004030dc01007d14', 'flat or scratch memory is not a global root access'), # scratch_load_dword (segment 1) + ('0080c8dc01140400', 'only DWORD loads and stores with a scalar address pair'), # global_atomic_add ] POLICY = '009033dc01000414' # global_load_dword v20, v1, s[4:5] glc slc dlc: accepted INACTIVE = 0x13579bdf @@ -150,8 +150,9 @@ def compile_words(code): assert run.returncode and message in run.stderr, (code, message, run.stdout, run.stderr) _, run = compile_words(PROLOGUE + words(POLICY) + EPILOGUE) assert run.returncode == 0, run.stderr - print('refused: off, a table pointer, offset:2, VGPR overflows, store, a replaced high half, an advanced pointer, ' - 'lds, flat and scratch segments, an atomic; glc slc dlc accepted') + print('refused: off, a table pointer, offset:2, VGPR overflows, a store through a table pointer, ' + 'a replaced high half, an advanced pointer, lds, flat and scratch segments, an atomic; ' + 'glc slc dlc accepted') code = PROLOGUE + words(' '.join(LOADS.values())) + EPILOGUE marker, run = compile_words(code) diff --git a/tests/native_global_store_check.py b/tests/native_global_store_check.py new file mode 100644 index 0000000000..145ad39833 --- /dev/null +++ b/tests/native_global_store_check.py @@ -0,0 +1,280 @@ +"""DWORD GLOBAL stores through a root UAV in the direct C++ compute path. + + native_global_store_check.py encodings + native_global_store_check.py + +ADDR = SADDR + zext(VADDR) + sext(OFFSET[11:0]) (RDNA 2 ISA, FLAT/GLOBAL). SADDR is the unadvanced root UAV. +The VGPR is a 32-bit byte offset. The direct frontend lowers global_store_dword, dwordx2, dwordx3 and dwordx4 +on that form, and a global_load_dword from the same UAV. A 64-bit VGPR address (saddr off), flat, scratch, +LDS, atomics, sub-DWORD ops, a non-UAV root, an unaligned immediate and a data register that runs past v255 +stay refused. + +Words are built from the FLAT bit layout in tools/disasm_rdna2.py. `encodings` checks those words with the +disassembler and checks that a swapped x2 reference is not the one the readback uses. The dispatch needs +shader model 6.6 with wave32 (native_store_replay returns 77 otherwise). + +Two groups of 32 lanes. Lane id N = group * 32 + lane. The root UAV is initially 0x69696969. Each lane's +48-byte record at byte 48*N is: + + +0 dword tag + +4 dword tag+1 + +8 dwordx2 tag+2, tag+3 + +16 dwordx3 tag+4, tag+5, tag+6 + +28 untouched 0x69696969 + +32 dwordx4 tag, tag+1, tag+2, tag+3 + +tag = 0xA1000000 | N. Other regions, still inside the view: + + byte 3584+4*N the address itself (data register aliases the address) + byte 4092+4*N tag+1 with vaddr 4096+4*N and offset -4 + byte 5120+4*N tag on even lanes; odd lanes stay 0x69696969 (EXEC low bits 0x55555555) + byte 5888 0, from a load of vaddr 0xfffffff4 offset 16 (the wrap guard) + +The guard used by the root loads keeps a positive offset only when VADDR <= 2^32-1-offset. 0xfffffff4 is past +that limit for offset 16, so the load result is 0 and the poison store at the same address must not land. The readback includes the 8-dword guard +native_store_replay places in front of the view. +""" +from pathlib import Path +import struct +import subprocess +import sys +import tempfile + +HERE = Path(__file__).resolve().parent +ROOT = HERE.parent +sys.path.insert(0, str(HERE)) +sys.path.insert(0, str(ROOT / 'tools')) + +import disasm_rdna2 as D # noqa: E402 +from native_compute_clear_check import write_request +from native_dpp_mirror_check import vop2 +from native_scalar_lds_check import scalar, sop1, vmove + + +SENTINEL = 0x69696969 +TAG = 0xA1000000 +POISON = 0xEEEEEEEE +GUARD = 8 +VIEW_DWORDS = 2048 +READBACK = (VIEW_DWORDS + 2 * GUARD) +RECORD = 48 +ALIAS = 3584 +NEG_BASE = 4096 +SPARSE = 5120 +LOAD_SLOT = 5888 +# s[4:5] root UAV, s6 = TGID_X. Wave32, one wave per group. +SA = struct.pack('<7I', 0, 0, 0, 32 | 1 << 16, 1 | (1 << 20), 0, 0x80 | 6 << 1) + + +def encode_global(op, vdst, addr, saddr, data=0, offset=0, glc=0, slc=0, dlc=0, lds=0, seg=2): + w0 = (0xdc000000 | ((op & 0x7f) << 18) | ((slc & 1) << 17) | ((glc & 1) << 16) + | ((seg & 3) << 14) | ((lds & 1) << 13) | ((dlc & 1) << 12) | (offset & 0xfff)) + w1 = ((vdst & 0xff) << 24) | ((saddr & 0x7f) << 16) | ((data & 0xff) << 8) | (addr & 0xff) + return [w0, w1] + + +def program(): + code = [ + scalar(0x1e, 9, 6, 128 + 5), # s_lshl_b32 s9, s6, 5 + vop2(0x25, 9, 9, 0), # v_add_nc_u32 v9, s9, v0 + vop2(0x1a, 1, 128 + 5, 9), # v_lshlrev_b32 v1, 5, v9 + vop2(0x1a, 18, 128 + 4, 9), # v_lshlrev_b32 v18, 4, v9 + vop2(0x25, 1, 256 + 18, 1), # v_add_nc_u32 v1, v18, v1 + vop2(0x1c, 10, 9, 0), # v_or_b32 v10, s9, v0 + vop2(0x1c, 10, 255, 10), TAG, # v_or_b32 v10, tag, v10 + ] + for n in range(1, 8): + code.append(vop2(0x25, 10 + n, 128 + n, 10)) # v_add_nc_u32 v(10+n), n, v10 + code += [ + vmove(2, 255), 0xfffffff4, # v_mov_b32 v2, 0xfffffff4 + vmove(3, 255), POISON, + vmove(5, 255), LOAD_SLOT, + vop2(0x1a, 19, 128 + 2, 9), # v_lshlrev_b32 v19, 2, v9 + vop2(0x25, 6, 255, 19), NEG_BASE, # v6 = 4096 + 4*N + vop2(0x25, 7, 255, 19), ALIAS, + vop2(0x25, 8, 255, 19), SPARSE, + ] + code += encode_global(0x0c, 4, 2, 4, offset=16) # global_load_dword v4, v2, s[4:5] offset:16 + code.append(0xbf8c0070) # s_waitcnt vmcnt(0) lgkmcnt(0) + code += encode_global(0x1c, 0, 5, 4, data=4) # global_store_dword v5, v4, s[4:5] + code += encode_global(0x1c, 0, 1, 4, data=10, glc=1, slc=1, dlc=1) + code += encode_global(0x1c, 0, 1, 4, data=11, offset=4) + code += encode_global(0x1d, 0, 1, 4, data=12, offset=8) + code += encode_global(0x1f, 0, 1, 4, data=14, offset=16) + code += encode_global(0x1e, 0, 1, 4, data=10, offset=32) + code += encode_global(0x1c, 0, 6, 4, data=11, offset=-4) + code += encode_global(0x1c, 0, 7, 4, data=7) # address register is the data + code += encode_global(0x1c, 0, 2, 4, data=3, offset=16) # poison, must not store + code += [sop1(3, 126, 255), 0x55555555] + code += encode_global(0x1c, 0, 8, 4, data=10) + code += [sop1(3, 126, 193), 0xbf810000] + return code + + +def extent_program(): + return encode_global(0x1e, 0, 1, 4, data=10, offset=2044) + [0xbf810000] + + +def reference(): + expected = [SENTINEL] * READBACK + writes = {} + + def put(byte, value): + if byte % 4 or byte < 0 or byte // 4 >= VIEW_DWORDS: + raise AssertionError(hex(byte)) + index = GUARD + byte // 4 + if index in writes and writes[index] != value: + raise AssertionError((byte, hex(writes[index]), hex(value))) + writes[index] = value + expected[index] = value + + for lane in range(64): + tag = TAG | lane + base = lane * RECORD + values = [tag + n for n in range(8)] + for dword, value in enumerate((values[0], values[1], values[2], values[3], values[4], values[5], values[6])): + put(base + 4 * dword, value) + for dword, value in enumerate(values[:4]): + put(base + 32 + 4 * dword, value) + put(NEG_BASE + 4 * lane - 4, values[1]) + put(ALIAS + 4 * lane, ALIAS + 4 * lane) + if lane % 2 == 0: + put(SPARSE + 4 * lane, tag) + put(LOAD_SLOT, 0) + swapped = expected[:] + # The x2 pair is dwords 2 and 3 of the record. Swapping them must change the buffer. + for lane in range(64): + index = GUARD + (lane * RECORD) // 4 + swapped[index + 2], swapped[index + 3] = swapped[index + 3], swapped[index + 2] + if swapped == expected: + raise AssertionError('swapped x2 reference matches') + if POISON in expected: + raise AssertionError('poison is in the reference') + return expected + + +def encodings(): + text = [] + blob = struct.pack('<' + 'I' * len(program()), *program()) + at = 0 + while at < len(blob): + size, name, asm, _ = D.decode_instruction(blob, at) + text.append(asm) + at += size + joined = '\n'.join(text) + for needle in ( + 'global_load_dword v4, v2, s[4:5] offset:16', + 'global_store_dword v5, v4, s[4:5]', + 'global_store_dword v1, v10, s[4:5] glc slc dlc', + 'global_store_dword v1, v11, s[4:5] offset:4', + 'global_store_dwordx2 v1, v[12:13], s[4:5] offset:8', + 'global_store_dwordx3 v1, v[14:16], s[4:5] offset:16', + 'global_store_dwordx4 v1, v[10:13], s[4:5] offset:32', + 'global_store_dword v6, v11, s[4:5] offset:-4', + 'global_store_dword v7, v7, s[4:5]', + 'global_store_dword v2, v3, s[4:5] offset:16', + 'global_store_dword v8, v10, s[4:5]', + 's_waitcnt vmcnt(0) lgkmcnt(0)', + 's_endpgm', + ): + if needle not in joined: + raise AssertionError(needle + '\n' + joined) + negatives = { + 'global_store_dword v[1:2], v10, off': encode_global(0x1c, 0, 1, 0x7d, data=10), + 'global_store_dword v1, v10, s[4:5] offset:2': encode_global(0x1c, 0, 1, 4, data=10, offset=2), + 'global_store_dwordx4 v1, v[253:256], s[4:5]': encode_global(0x1e, 0, 1, 4, data=253), + 'global_store_dwordx3 v1, v[254:256], s[4:5]': encode_global(0x1f, 0, 1, 4, data=254), + 'global_store_dword v1, v10, s5': encode_global(0x1c, 0, 1, 5, data=10), + 'global_store_dword v1, v10, exec_hi': encode_global(0x1c, 0, 1, 127, data=10), + 'global_store_byte v1, v10, s[4:5]': encode_global(0x18, 0, 1, 4, data=10), + 'global_atomic_add v1, v10, s[4:5]': encode_global(0x32, 0, 1, 4, data=10), + 'global_load_dword v1, s[4:5] lds': encode_global(0x0c, 4, 1, 4, lds=1), + 'flat_store_dword v[1:2], v10': encode_global(0x1c, 0, 1, 4, data=10, seg=0), + 'scratch_store_dword off, v10, s4': encode_global(0x1c, 0, 1, 4, data=10, seg=1), + } + for asm, words in negatives.items(): + blob = struct.pack('<2I', *words) + got = D.decode_instruction(blob, 0)[2] + if got != asm: + raise AssertionError((asm, got)) + reference() + print('encodings: dword, x2, x3 and x4 stores, a UAV load, alias, negative offset and the wrap poison') + + +def main(compiler, replay, sdk): + encodings() + with tempfile.TemporaryDirectory() as tmp: + work = Path(tmp) + + def root_sig(text): + source, binary = work / 'root.hlsl', work / 'root.bin' + source.write_text('#define SIG "%s"' % text) + subprocess.run([Path(sdk) / 'dxc.exe', '-T', 'rootsig_1_1', '-E', 'SIG', '-Fo', binary, source], check=True) + return binary.read_bytes() + + uav = root_sig('UAV(u0)') + srv = root_sig('SRV(t0)') + + def compile_words(code, root): + marker = write_request(work, code, root, SA) + return marker, subprocess.run([compiler, sdk, work / 'cache', 'gfx1013-direct-cs@0', marker, + marker.with_suffix('.cso')], capture_output=True, text=True) + + negatives = [ + (encode_global(0x1c, 0, 1, 0x7d, data=10), 'VGPR address pair is not a root buffer offset'), + (encode_global(0x1c, 0, 1, 4, data=10, offset=2), 'unaligned immediate offset'), + (encode_global(0x1e, 0, 1, 4, data=253), 'global store data register range'), + (encode_global(0x1f, 0, 1, 4, data=254), 'global store data register range'), + (encode_global(0x1c, 0, 1, 5, data=10), 'aligned SGPR pair'), + (encode_global(0x1c, 0, 1, 127, data=10), 'scalar address is not an SGPR pair'), + (encode_global(0x18, 0, 1, 4, data=10), 'only DWORD loads and stores with a scalar address pair'), + (encode_global(0x32, 0, 1, 4, data=10), 'only DWORD loads and stores with a scalar address pair'), + (encode_global(0x0c, 4, 1, 4, lds=1), 'does not lower an LDS result'), + (encode_global(0x1c, 0, 1, 4, data=10, seg=0), 'flat or scratch memory is not a global root access'), + (encode_global(0x1c, 0, 1, 4, data=10, seg=1), 'flat or scratch memory is not a global root access'), + ([sop1(3, 5, 128)] + encode_global(0x1c, 0, 1, 4, data=10), 'advanced or mismatched root address'), + ([scalar(0, 4, 4, 128 + 16)] + encode_global(0x1c, 0, 1, 4, data=10), 'unbound numeric SGPR s4'), + ] + body = program() + for code, message in negatives: + _, run = compile_words(code + [0xbf810000], uav) + if run.returncode == 0 or message not in (run.stdout + run.stderr): + raise AssertionError((message, run.returncode, run.stdout, run.stderr)) + _, srv_run = compile_words(body, srv) + if srv_run.returncode == 0 or 'global store base is not a root UAV' not in (srv_run.stdout + srv_run.stderr): + raise AssertionError((srv_run.returncode, srv_run.stdout, srv_run.stderr)) + print('refused: VGPR address pair, unaligned offset, data past v255, an odd base, saddr 127, ' + 'a byte store, an atomic, LDS, flat, scratch, a replaced high half, an advanced pointer, an SRV root') + + _, extent = compile_words(extent_program(), uav) + if extent.returncode != 0 or 'u0 root=0 bytes=0 min_size=2060' not in extent.stdout: + raise AssertionError((extent.stdout, extent.stderr)) + marker, run = compile_words(body, uav) + if run.returncode != 0 or 'u0 root=0 bytes=0 min_size=48' not in run.stdout: + raise AssertionError((run.stdout, run.stderr)) + print('module: one root UAV, min_size 48, x4 at offset 2044 asks for 2060 bytes') + + dest = work / 'readback.bin' + result = subprocess.run([replay, sdk, marker, work / 'cache', '0', dest, '--direct'], + capture_output=True, text=True) + if result.returncode == 77: + print(result.stdout + result.stderr) + sys.exit(77) + if result.returncode != 0: + raise AssertionError((result.stdout, result.stderr)) + actual = list(struct.unpack('<%dI' % READBACK, dest.read_bytes())) + expected = reference() + diff = [(i, hex(a), hex(b)) for i, (a, b) in enumerate(zip(actual, expected, strict=True)) if a != b] + if diff: + raise AssertionError(diff[:12]) + if POISON in actual: + raise AssertionError('poison store became visible') + print('global_store_dword/x2/x3/x4: record, alias, negative offset, sparse EXEC, UAV load wrap guard; ' + 'poison store absent') + + +if __name__ == '__main__': + if len(sys.argv) == 2 and sys.argv[1] == 'encodings': + encodings() + else: + main(*sys.argv[1:]) diff --git a/tests/native_store_replay.cpp b/tests/native_store_replay.cpp index fe933ec1c0..04c08511d6 100644 --- a/tests/native_store_replay.cpp +++ b/tests/native_store_replay.cpp @@ -28,7 +28,7 @@ UINT constant(UINT index) { UINT format_width(ComputeViewKind kind, UINT bytes) { if (kind == ComputeViewKind::typed_buffer_srv) return 16; if (kind == ComputeViewKind::buffer_srv || kind == ComputeViewKind::root_buffer_srv || - kind == ComputeViewKind::raw_buffer_uav) return 4; + kind == ComputeViewKind::root_buffer_uav || kind == ComputeViewKind::raw_buffer_uav) return 4; return bytes; } DXGI_FORMAT typed_format(unsigned bytes, bool half, bool reference) { @@ -160,12 +160,13 @@ int wmain(int argc, wchar_t **argv) { const UINT position=reference?b.root_parameter*256+b.byte_offset/32:UINT(windows.size()); if (position>=hd.NumDescriptors) return 2; auto cpu=heap->GetCPUDescriptorHandleForHeapStart();cpu.ptr+=UINT64(position)*step; - Window w;w.write=compute_buffer_uav(b.kind); + const bool root_uav = b.kind == ComputeViewKind::root_buffer_uav; + Window w;w.write=compute_buffer_uav(b.kind) || root_uav; // A byte window is the byte-address rewrite of the same raw table view. const bool vector_bytes = b.kind == ComputeViewKind::raw_vector_srv || b.kind == ComputeViewKind::byte_window_srv; const bool root_bytes = b.kind == ComputeViewKind::root_buffer_srv; if (!w.write && b.kind!=ComputeViewKind::typed_buffer_srv && b.kind!=ComputeViewKind::buffer_srv && !vector_bytes && !root_bytes) return 2; - const bool raw=root_bytes || vector_bytes || b.kind==ComputeViewKind::buffer_srv || b.kind==ComputeViewKind::raw_buffer_uav; + const bool raw=root_bytes || root_uav || vector_bytes || b.kind==ComputeViewKind::buffer_srv || b.kind==ComputeViewKind::raw_buffer_uav; const bool indexed = b.kind == ComputeViewKind::indexed_atomic_uav; const UINT stride = indexed && b.byte_offset == 32 ? 16 : indexed && b.byte_offset == 64 ? 12 : format_width(b.kind,b.required_bytes); From 7c9565f86f11003cada6e7e768ebf9d5d2f6fecd Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Sat, 28 Nov 2026 09:28:02 +0000 Subject: [PATCH 2/6] compute: expect the root-SRV global store refusal Co-authored-by: SimSalabimse --- tests/native_global_load_check.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/native_global_load_check.py b/tests/native_global_load_check.py index 4fc7d8d2f5..1d80a2e096 100644 --- a/tests/native_global_load_check.py +++ b/tests/native_global_load_check.py @@ -61,7 +61,7 @@ def words(hexes): ('008034dc010004ff', 'global load destination register range'), # x2 beginning at v255 ('00803cdc010004fe', 'global load destination register range'), # x3 beginning at v254 ('008038dc010004fd', 'global load destination register range'), # x4 beginning at v253 - ('008070dc01140400', 'global store base is not a root buffer address'), # global_store_dword through the table pointer + ('008070dc01140400', 'global store base is not a root UAV'), # global_store_dword v1, v20, s[4:5] (root SRV) ('800385be 008030dc01000414', 'global load base is an advanced or mismatched root address'), # s_mov_b32 s5, 0 ('04900480 008030dc01000414', 'unbound numeric SGPR s4'), # s_add_u32 s4, s4, 16 on the root address ('00a030dc01000414', 'does not lower an LDS result'), # the lds bit @@ -150,7 +150,7 @@ def compile_words(code): assert run.returncode and message in run.stderr, (code, message, run.stdout, run.stderr) _, run = compile_words(PROLOGUE + words(POLICY) + EPILOGUE) assert run.returncode == 0, run.stderr - print('refused: off, a table pointer, offset:2, VGPR overflows, a store through a table pointer, ' + print('refused: off, a table pointer, offset:2, VGPR overflows, a store through the root SRV, ' 'a replaced high half, an advanced pointer, lds, flat and scratch segments, an atomic; ' 'glc slc dlc accepted') From cde473e4faeef8f762e36e0bd8254caab8bfefc8 Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Sat, 28 Nov 2026 20:20:30 +0000 Subject: [PATCH 3/6] compute: check the wide wrap guard and root UAV register assignment Co-authored-by: SimSalabimse --- .../experiments/native-direct-global-store.md | 13 ++++- src/gfx1013_direct_compute.inl | 9 ++- tests/native_global_store_check.py | 55 ++++++++++++++++--- 3 files changed, 65 insertions(+), 12 deletions(-) diff --git a/docs/experiments/native-direct-global-store.md b/docs/experiments/native-direct-global-store.md index b30c4f0445..03a5e88a7a 100644 --- a/docs/experiments/native-direct-global-store.md +++ b/docs/experiments/native-direct-global-store.md @@ -18,6 +18,10 @@ immediate, a data range past v255, and a scalar address that is not the matching 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` @@ -32,5 +36,10 @@ shader model 6.6 with wave32. A missing wave32 adapter is a skip (77), not a pas 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. 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. +poison store at that address which must not appear. An x4 at vaddr 0xffffffe8 offset 16 has its first DWORD +inside the 32-bit range and its last DWORD past it; the whole store is skipped. The DXIL upper bound for that +store is 4294967267 (`2^32 - 1 - (16 + 12)`). Dropping `(count - 1) * 4` from `last_offset` removes that +constant, so the hardware test fails. 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. diff --git a/src/gfx1013_direct_compute.inl b/src/gfx1013_direct_compute.inl index e8ef1e07fa..69e53005bc 100644 --- a/src/gfx1013_direct_compute.inl +++ b/src/gfx1013_direct_compute.inl @@ -481,15 +481,17 @@ struct DirectCompute { raw ? 0u : width, 0, count}, indexed && count > 1 ? read(base) : ""); } - string load_buffer(const string &h, const string &addr, bool raw, unsigned count) { + string load_buffer(const string &h, const string &addr, bool raw, unsigned count, bool readonly = true) { using Arg = std::pair; std::vector args{{"%dx.types.Handle", h}, {"i32", addr}, {"i32", "undef"}}; if (raw) { args.emplace_back("i8", to_string((1u << count) - 1)); args.emplace_back("i32", "4"); } + // A UAV load must not be readonly: the same module stores that resource, and a readonly + // declaration would not observe those stores (s_waitcnt itself lowers to nothing). return ir.invoke("%dx.types.ResRet.i32", raw ? "rawBufferLoad.i32" : "bufferLoad.i32", raw ? 139 : 68, args, - "nounwind readonly"); + readonly ? "nounwind readonly" : "nounwind"); } void buffer_atomic(const Inst &i, const State &s, const string &pixel_predicate = {}) { // The operation set and VGPR ordering follow gfx2hlsl.MUBUF_ATOMIC_OPS. @@ -646,7 +648,8 @@ struct DirectCompute { refuse(i.pc, "global load destination register range"); string handle, aligned, in_range; global_root(i, s, false, count, handle, aligned, in_range); - const auto ret = load_buffer(handle, aligned, true, count); + const bool uav_load = s.at((i.b >> 16) & 127).index_mask == 1; + const auto ret = load_buffer(handle, aligned, true, count, !uav_load); for (unsigned j = 0; j < count; ++j) { auto value = ir.value("extractvalue %dx.types.ResRet.i32 " + ret + ", " + to_string(j)); if (!in_range.empty()) diff --git a/tests/native_global_store_check.py b/tests/native_global_store_check.py index 145ad39833..7104e005e7 100644 --- a/tests/native_global_store_check.py +++ b/tests/native_global_store_check.py @@ -29,10 +29,12 @@ byte 4092+4*N tag+1 with vaddr 4096+4*N and offset -4 byte 5120+4*N tag on even lanes; odd lanes stay 0x69696969 (EXEC low bits 0x55555555) byte 5888 0, from a load of vaddr 0xfffffff4 offset 16 (the wrap guard) + byte 6400+4*N the tag loaded back from byte 48*N after the stores and s_waitcnt -The guard used by the root loads keeps a positive offset only when VADDR <= 2^32-1-offset. 0xfffffff4 is past -that limit for offset 16, so the load result is 0 and the poison store at the same address must not land. The readback includes the 8-dword guard -native_store_replay places in front of the view. +An x4 store of 0xEEEEEEEE at vaddr 0xffffffe8 offset 16 must not land. Its first DWORD is at 0xfffffff8, still +inside the 32-bit range, and its last DWORD starts at 0x100000004. The upper guard is VADDR <= 2^32-1-(offset+12), +which is 4294967267. Narrowing that to `last_offset = offset` accepts this VADDR and drops 4294967267 from the +DXIL. The readback includes the 8-dword guard native_store_replay places in front of the view. """ from pathlib import Path import struct @@ -62,8 +64,12 @@ NEG_BASE = 4096 SPARSE = 5120 LOAD_SLOT = 5888 +RELOAD = 6400 +X4_WRAP = 0xffffffe8 # s[4:5] root UAV, s6 = TGID_X. Wave32, one wave per group. SA = struct.pack('<7I', 0, 0, 0, 32 | 1 << 16, 1 | (1 << 20), 0, 0x80 | 6 << 1) +# s[4:5] and s[6:7] root SRVs, s[8:9] root UAV. No group id. +PAIR_SA = struct.pack('<7I', 0, 0, 0, 32 | (1 << 16), 1 | (1 << 20), 0, 10 << 1) def encode_global(op, vdst, addr, saddr, data=0, offset=0, glc=0, slc=0, dlc=0, lds=0, seg=2): @@ -89,10 +95,16 @@ def program(): vmove(2, 255), 0xfffffff4, # v_mov_b32 v2, 0xfffffff4 vmove(3, 255), POISON, vmove(5, 255), LOAD_SLOT, + vmove(24, 255), X4_WRAP, + vmove(20, 255), POISON, + vmove(21, 255), POISON, + vmove(22, 255), POISON, + vmove(23, 255), POISON, vop2(0x1a, 19, 128 + 2, 9), # v_lshlrev_b32 v19, 2, v9 vop2(0x25, 6, 255, 19), NEG_BASE, # v6 = 4096 + 4*N vop2(0x25, 7, 255, 19), ALIAS, vop2(0x25, 8, 255, 19), SPARSE, + vop2(0x25, 25, 255, 19), RELOAD, ] code += encode_global(0x0c, 4, 2, 4, offset=16) # global_load_dword v4, v2, s[4:5] offset:16 code.append(0xbf8c0070) # s_waitcnt vmcnt(0) lgkmcnt(0) @@ -105,9 +117,14 @@ def program(): code += encode_global(0x1c, 0, 6, 4, data=11, offset=-4) code += encode_global(0x1c, 0, 7, 4, data=7) # address register is the data code += encode_global(0x1c, 0, 2, 4, data=3, offset=16) # poison, must not store + code += encode_global(0x1e, 0, 24, 4, data=20, offset=16) # x4 whose last dword wraps code += [sop1(3, 126, 255), 0x55555555] code += encode_global(0x1c, 0, 8, 4, data=10) - code += [sop1(3, 126, 193), 0xbf810000] + code += [sop1(3, 126, 193)] # s_mov_b32 exec_lo, -1 + code.append(0xbf8c0070) # s_waitcnt vmcnt(0) lgkmcnt(0) + code += encode_global(0x0c, 4, 1, 4) # global_load_dword v4, v1, s[4:5] + code += encode_global(0x1c, 0, 25, 4, data=4) # store the loaded tag + code.append(0xbf810000) return code @@ -115,6 +132,12 @@ def extent_program(): return encode_global(0x1e, 0, 1, 4, data=10, offset=2044) + [0xbf810000] +def register_program(): + # s[4:5] and s[6:7] are root SRVs, s[8:9] is a root UAV. Two SRVs make srv++ observable. + return (encode_global(0x0c, 1, 0, 4) + encode_global(0x0c, 2, 0, 6) + + encode_global(0x1c, 0, 0, 8, data=1) + [0xbf810000]) + + def reference(): expected = [SENTINEL] * READBACK writes = {} @@ -140,6 +163,7 @@ def put(byte, value): put(ALIAS + 4 * lane, ALIAS + 4 * lane) if lane % 2 == 0: put(SPARSE + 4 * lane, tag) + put(RELOAD + 4 * lane, tag) put(LOAD_SLOT, 0) swapped = expected[:] # The x2 pair is dwords 2 and 3 of the record. Swapping them must change the buffer. @@ -173,7 +197,10 @@ def encodings(): 'global_store_dword v6, v11, s[4:5] offset:-4', 'global_store_dword v7, v7, s[4:5]', 'global_store_dword v2, v3, s[4:5] offset:16', + 'global_store_dwordx4 v24, v[20:23], s[4:5] offset:16', 'global_store_dword v8, v10, s[4:5]', + 'global_load_dword v4, v1, s[4:5]', + 'global_store_dword v25, v4, s[4:5]', 's_waitcnt vmcnt(0) lgkmcnt(0)', 's_endpgm', ): @@ -191,6 +218,9 @@ def encodings(): 'global_load_dword v1, s[4:5] lds': encode_global(0x0c, 4, 1, 4, lds=1), 'flat_store_dword v[1:2], v10': encode_global(0x1c, 0, 1, 4, data=10, seg=0), 'scratch_store_dword off, v10, s4': encode_global(0x1c, 0, 1, 4, data=10, seg=1), + 'global_load_dword v1, v0, s[4:5]': encode_global(0x0c, 1, 0, 4), + 'global_load_dword v2, v0, s[6:7]': encode_global(0x0c, 2, 0, 6), + 'global_store_dword v0, v1, s[8:9]': encode_global(0x1c, 0, 0, 8, data=1), } for asm, words in negatives.items(): blob = struct.pack('<2I', *words) @@ -214,9 +244,10 @@ def root_sig(text): uav = root_sig('UAV(u0)') srv = root_sig('SRV(t0)') + both = root_sig('SRV(t0), SRV(t1), UAV(u0)') - def compile_words(code, root): - marker = write_request(work, code, root, SA) + def compile_words(code, root, sa=SA): + marker = write_request(work, code, root, sa) return marker, subprocess.run([compiler, sdk, work / 'cache', 'gfx1013-direct-cs@0', marker, marker.with_suffix('.cso')], capture_output=True, text=True) @@ -252,7 +283,17 @@ def compile_words(code, root): marker, run = compile_words(body, uav) if run.returncode != 0 or 'u0 root=0 bytes=0 min_size=48' not in run.stdout: raise AssertionError((run.stdout, run.stderr)) - print('module: one root UAV, min_size 48, x4 at offset 2044 asks for 2060 bytes') + # x4 at offset 16 guards the last dword, 12 bytes past the first. `last_offset = offset` + # would emit 4294967279, the same bound as the single-dword poison store. + dump = subprocess.run([Path(sdk) / 'dxc.exe', '-dumpbin', marker.with_suffix('.cso')], + capture_output=True, text=True, check=True).stdout + if '4294967267' not in dump: + raise AssertionError('x4 wrap guard does not cover the last dword') + _, regs = compile_words(register_program(), both, PAIR_SA) + for line in ('t0 root=0 bytes=0 min_size=4', 't1 root=1 bytes=0 min_size=4', 'u0 root=2 bytes=0 min_size=4'): + if regs.returncode != 0 or line not in regs.stdout: + raise AssertionError((line, regs.stdout, regs.stderr)) + print('module: one root UAV, min_size 48, x4 wrap bound 4294967267, registers t0 t1 u0') dest = work / 'readback.bin' result = subprocess.run([replay, sdk, marker, work / 'cache', '0', dest, '--direct'], From 8a22edcfb1f38a1608f39d13b9c7c00eaba9588c Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Sat, 28 Nov 2026 20:40:55 +0000 Subject: [PATCH 4/6] compute: keep the x4 wrap compare visible to the desktop compiler Co-authored-by: SimSalabimse --- .../experiments/native-direct-global-store.md | 16 ++++--- tests/native_global_store_check.py | 45 ++++++++++++++----- 2 files changed, 44 insertions(+), 17 deletions(-) diff --git a/docs/experiments/native-direct-global-store.md b/docs/experiments/native-direct-global-store.md index 03a5e88a7a..fed9f62d2f 100644 --- a/docs/experiments/native-direct-global-store.md +++ b/docs/experiments/native-direct-global-store.md @@ -36,10 +36,12 @@ shader model 6.6 with wave32. A missing wave32 adapter is a skip (77), not a pas 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 vaddr 0xffffffe8 offset 16 has its first DWORD -inside the 32-bit range and its last DWORD past it; the whole store is skipped. The DXIL upper bound for that -store is 4294967267 (`2^32 - 1 - (16 + 12)`). Dropping `(count - 1) * 4` from `last_offset` removes that -constant, so the hardware test fails. 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. +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. Dropping `(count - 1) * 4` from `last_offset` changes the bound to `-16`, so the +hardware test fails. 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. diff --git a/tests/native_global_store_check.py b/tests/native_global_store_check.py index 7104e005e7..06f8f39dd8 100644 --- a/tests/native_global_store_check.py +++ b/tests/native_global_store_check.py @@ -31,12 +31,15 @@ byte 5888 0, from a load of vaddr 0xfffffff4 offset 16 (the wrap guard) byte 6400+4*N the tag loaded back from byte 48*N after the stores and s_waitcnt -An x4 store of 0xEEEEEEEE at vaddr 0xffffffe8 offset 16 must not land. Its first DWORD is at 0xfffffff8, still -inside the 32-bit range, and its last DWORD starts at 0x100000004. The upper guard is VADDR <= 2^32-1-(offset+12), -which is 4294967267. Narrowing that to `last_offset = offset` accepts this VADDR and drops 4294967267 from the -DXIL. The readback includes the 8-dword guard native_store_replay places in front of the view. +An x4 store of 0xEEEEEEEE uses vaddr 0xffffffe4 + (group << 2) + (lane & 3) and offset 16. Every lane's +first DWORD stays inside the 32-bit range and its last DWORD starts at or past 2^32, so the store must not land. +The desktop compiler prints that i32 bound as a signed immediate and deletes the compare when the address is a +constant, so the address stays data-dependent. Narrowing `last_offset` to `offset` changes the bound from -29 +to -16. The store's base is near 2^32, outside this view, so a readback of the view cannot see that mistake. +The readback includes the 8-dword guard native_store_replay places in front of the view. """ from pathlib import Path +import re import struct import subprocess import sys @@ -65,7 +68,8 @@ SPARSE = 5120 LOAD_SLOT = 5888 RELOAD = 6400 -X4_WRAP = 0xffffffe8 +# First DWORD of the x4 still fits in 32 bits; the last does not. See wide_wrap_bound. +X4_BASE = 0xffffffe4 # s[4:5] root UAV, s6 = TGID_X. Wave32, one wave per group. SA = struct.pack('<7I', 0, 0, 0, 32 | 1 << 16, 1 | (1 << 20), 0, 0x80 | 6 << 1) # s[4:5] and s[6:7] root SRVs, s[8:9] root UAV. No group id. @@ -95,7 +99,11 @@ def program(): vmove(2, 255), 0xfffffff4, # v_mov_b32 v2, 0xfffffff4 vmove(3, 255), POISON, vmove(5, 255), LOAD_SLOT, - vmove(24, 255), X4_WRAP, + vmove(27, 6), # v_mov_b32 v27, s6 + vop2(0x1a, 27, 128 + 2, 27), # v_lshlrev_b32 v27, 2, v27 + vop2(0x1b, 26, 128 + 3, 0), # v_and_b32 v26, 3, v0 + vop2(0x25, 24, 256 + 26, 27), # v_add_nc_u32 v24, v26, v27 + vop2(0x25, 24, 255, 24), X4_BASE, # v24 = 0xffffffe4 + (group << 2) + (lane & 3) vmove(20, 255), POISON, vmove(21, 255), POISON, vmove(22, 255), POISON, @@ -132,6 +140,12 @@ def extent_program(): return encode_global(0x1e, 0, 1, 4, data=10, offset=2044) + [0xbf810000] +def wide_wrap_bound(dump): + # 2^32-1-(16+12) = 4294967267, printed by the desktop compiler as i32 -29. + # ule X, C is also written ult X, C+1 (-28). `last_offset = offset` emits -16 instead. + return re.search(r'(? Date: Sun, 29 Nov 2026 12:26:23 +0000 Subject: [PATCH 5/6] compute: match the x4 wrap compare, not the address add Co-authored-by: SimSalabimse --- .../experiments/native-direct-global-store.md | 6 +++-- tests/native_global_store_check.py | 25 +++++++++++++------ 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/docs/experiments/native-direct-global-store.md b/docs/experiments/native-direct-global-store.md index fed9f62d2f..51ca468a50 100644 --- a/docs/experiments/native-direct-global-store.md +++ b/docs/experiments/native-direct-global-store.md @@ -39,8 +39,10 @@ negative immediate, even lanes under a narrowed EXEC, a guarded load of vaddr 0x 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. Dropping `(count - 1) * 4` from `last_offset` changes the bound to `-16`, so the -hardware test fails. That store's base is near 2^32, outside the view, so the readback cannot show the mistake. +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 diff --git a/tests/native_global_store_check.py b/tests/native_global_store_check.py index 06f8f39dd8..22b6c3945d 100644 --- a/tests/native_global_store_check.py +++ b/tests/native_global_store_check.py @@ -34,8 +34,10 @@ An x4 store of 0xEEEEEEEE uses vaddr 0xffffffe4 + (group << 2) + (lane & 3) and offset 16. Every lane's first DWORD stays inside the 32-bit range and its last DWORD starts at or past 2^32, so the store must not land. The desktop compiler prints that i32 bound as a signed immediate and deletes the compare when the address is a -constant, so the address stays data-dependent. Narrowing `last_offset` to `offset` changes the bound from -29 -to -16. The store's base is near 2^32, outside this view, so a readback of the view cannot see that mistake. +constant, so the address stays data-dependent. Narrowing `last_offset` to `offset` changes the ule bound from +-29 to -17 (2^32-1-16). -16 is only the ult form of that mutant. The address add itself uses -28, so the +check matches the compare, not the add. The store's base is near 2^32, outside this view, so a readback of +the view cannot see that mistake. The readback includes the 8-dword guard native_store_replay places in front of the view. """ from pathlib import Path @@ -141,9 +143,14 @@ def extent_program(): def wide_wrap_bound(dump): - # 2^32-1-(16+12) = 4294967267, printed by the desktop compiler as i32 -29. - # ule X, C is also written ult X, C+1 (-28). `last_offset = offset` emits -16 instead. - return re.search(r'(? Date: Sun, 29 Nov 2026 13:04:12 +0000 Subject: [PATCH 6/6] compute: extend the view-kind name bound with the appended root UAV Co-authored-by: SimSalabimse --- src/direct_compute_cache.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/direct_compute_cache.cpp b/src/direct_compute_cache.cpp index 89dc706717..114468cb25 100644 --- a/src/direct_compute_cache.cpp +++ b/src/direct_compute_cache.cpp @@ -900,7 +900,7 @@ static const char *view_kind_name(ComputeViewKind kind) { "uint_texture_uav", "float_texture_uav", "float2_texture_uav", "float3_texture_uav", "packed_d16_x_uav", "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(ComputeViewKind::indexed_raw_srv) + 1); + static_assert(std::size(names) == static_cast(ComputeViewKind::root_buffer_uav) + 1); const auto index = static_cast(kind); return index < std::size(names) ? names[index] : "unknown"; }