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
27 changes: 24 additions & 3 deletions docs/experiments/native-direct-global-load.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ 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 (wider loads, stores,
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
Expand All @@ -34,7 +34,6 @@ a pointer advanced by a known literal. Graphics stages never decode the FLAT enc
before the buffer for a negative byte offset, or past the end of the view. The translation then differs from
the guest unless the runtime binds a view that covers every address the program reads. `gfx2hlsl.py` has the
same limit. How the runtime sizes the view on `cpp-migration` was not checked here.
- Only the one-dword load is lowered.

## Fixture

Expand All @@ -50,7 +49,29 @@ LLVM 15.0.7: `clang -cc1as -triple amdgcn -target-cpu gfx1013 -filetype asm -sho
16 and -8, a VADDR of 0xfffffff0 or above with offset 16 (guarded to 0), and an unaligned VADDR. References with
the offset dropped or the upper guard dropped must differ from the readback.

Measured on 27.11.2026, `cpp-migration` `e3670100` plus this change, Windows 10.0.26100.9711 (24H2): the refusals
The original single-DWORD change was measured on 27.11.2026, `cpp-migration` `e3670100`, Windows 10.0.26100.9711 (24H2): the refusals
and the module checks passed. The dispatch reported a skip (77) on the NVIDIA Quadro M1000M (driver 32.0.15.7371),
because `native_store_replay` needs shader model 6.6 with wave32. The readback is not measured. With the frontend
change reverted, the first refusal check fails on `unsupported instruction global_load_dword`.

## Multi-DWORD loads

`global_load_dwordx2`, `global_load_dwordx3`, and `global_load_dwordx4` use
the same root-address proof and write consecutive VGPRs under EXEC. The
lowering computes the address once before writing any destination, including
when the destination aliases VADDR. The last DWORD must also fit the 32-bit
view offset. The minimum view size accounts for the full transfer width.
Reads across the end of the bound view return zero for the out-of-range
components. The view must cover every guest address whose value is needed.

The same fixture hand-assembles the three wider forms. On `88cddb09` the
compiler refused all three at guest offset `+0x38`. On RTX 4070, driver
`32.0.16.2124` and Windows 11 build `26300.10075`, the updated fixture
checks the address/destination alias, the end of a 64-word view, negative
offsets with sparse EXEC, and positive overflow. Reversing the returned
registers changes 128 words for x2 and x3 and 256 for x4 across two groups.
The direct module requires shader model 6.6 and wave32.

The address and width follow the [AMD RDNA 2 ISA, §9.2](https://www.amd.com/content/dam/amd/en/documents/radeon-tech-docs/instruction-set-architectures/rdna2-shader-instruction-set-architecture.pdf)
and [LLVM 21.1.0 GFX1030 FLAT syntax](https://releases.llvm.org/21.1.0/docs/AMDGPU/AMDGPUAsmGFX1030.html#flat),
release commit `3623fe661ae35c6c80ac221f14d85be76aa870f1` under Apache-2.0 WITH LLVM-exception.
10 changes: 5 additions & 5 deletions src/gfx1013_compute_native.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -232,16 +232,16 @@ Inst decode(const string &bytes, unsigned pc,bool direct=false, bool graphics=fa
i.name = encoded.name;
i.dst = (i.b >> 8) & 255;
} else if (direct && !graphics && (a & 0xfc000000u) == 0xdc000000u) {
// FLAT encoding, segment 2 (GLOBAL). Only the one-dword load with an SGPR-pair base (the saddr form,
// ADDR = SADDR + zext(VADDR) + sext(OFFSET)) is lowered, as gfx2hlsl.py op_global_load_dword does.
// 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.
i.b = take();
i.op = (a >> 18) & 127;
const unsigned saddr = (i.b >> 16) & 127;
if (((a >> 14) & 3) != 2 || i.op != 12 || (a & 0x2000u) || saddr == 125 || saddr == 127)
refuse(pc, "direct global memory supports only global_load_dword with a scalar address pair");
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");
i.enc = Encoding::Mubuf;
i.name = "global_load_dword";
i.name = encoded.name;
i.dst = i.b >> 24;
} else if ((a & 0xfc000000u) == 0xd8000000) {
i.enc = Encoding::Lds;
Expand Down
43 changes: 24 additions & 19 deletions src/gfx1013_direct_compute.inl
Original file line number Diff line number Diff line change
Expand Up @@ -427,12 +427,15 @@ struct DirectCompute {
}
#include "gfx1013_compute_mtbuf.inl"

// global_load_dword in the saddr form (#2962): ADDR = SADDR + zext(VADDR) + sext(OFFSET[11:0]) (RDNA 2 ISA,
// 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.
// The byte offset zext(VADDR) + OFFSET is guarded to [0, 2^32) before it becomes a 32-bit view offset, so a
// wrapped address reads zero instead of an in-bounds word (gfx2hlsl.py op_global_load_dword). The low two
// address bits are dropped, as for buffer_load_dword in this path (DWORD alignment mode).
// Guard the first and last DWORD offsets 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");
const unsigned base = (i.b >> 16) & 127;
if (base > 104 || (base & 1))
refuse(i.pc, "global load base must be an aligned SGPR pair");
Expand All @@ -445,7 +448,7 @@ struct DirectCompute {
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 = offset > 0 ? unsigned(offset) + 4 : 4;
const unsigned span = static_cast<unsigned>((std::max)(offset, 0)) + count * 4;
auto &bindings = analysis.result.bindings;
auto binding = std::find_if(bindings.begin(), bindings.end(), [&](const auto &b) {
return b.kind == ComputeViewKind::root_buffer_srv && b.root_parameter == low.value;
Expand All @@ -458,23 +461,25 @@ struct DirectCompute {
ir.features |= 16;
const auto h = ir.handle({0, binding->shader_register, 11});
const auto vaddr = read(256 + (i.b & 255));
string in_range, addr = vaddr;
if (offset > 0) {
in_range = cmp("ule", vaddr, to_string(0xffffffffu - unsigned(offset)));
addr = ir.binary("add", vaddr, to_string(offset));
} else if (offset < 0) {
string in_range;
if (offset < 0)
in_range = cmp("uge", vaddr, to_string(-offset));
addr = ir.binary("sub", vaddr, to_string(-offset));
}
addr = ir.binary("and", addr, "4294967292");
const auto ret = load_buffer(h, addr, true, 1);
auto value = ir.value("extractvalue %dx.types.ResRet.i32 " + ret + ", 0");
if (!in_range.empty())
value = ir.value("select i1 " + in_range + ", i32 " + value + ", i32 0");
vector_write(256 + i.dst, value);
const int last_offset = offset + static_cast<int>((count - 1) * 4);
if (last_offset > 0) {
const auto upper = cmp("ule", vaddr, to_string(0xffffffffu - static_cast<unsigned>(last_offset)));
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);
for (unsigned j = 0; j < count; ++j) {
auto value = ir.value("extractvalue %dx.types.ResRet.i32 " + ret + ", " + to_string(j));
if (!in_range.empty())
value = ir.value("select i1 " + in_range + ", i32 " + value + ", i32 0");
vector_write(256 + i.dst + j, value);
}
}
void memory(const Inst &i, const State &s) {
if (i.name == "global_load_dword") {
if (i.name.substr(0, 17) == "global_load_dword") {
global_load(i, s);
return;
}
Expand Down
2 changes: 1 addition & 1 deletion tests/cmake/native_global_load.cmake
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Direct C++ compute: global_load_dword through a root SRV address (#2962). Refusals and the module's guards run
# Direct C++ compute: GLOBAL DWORD loads through a root SRV address. Refusals and the module's guards run
# everywhere; the dispatch readback needs shader model 6.6 with wave32 and otherwise reports a skip (77).
if(EXISTS "${INTAKE_PYTHON}" AND TARGET compile_dxil AND TARGET native_store_replay)
add_test(NAME native_global_load_hardware COMMAND "${INTAKE_PYTHON}" -B
Expand Down
99 changes: 83 additions & 16 deletions tests/native_global_load_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
import tempfile

from native_compute_clear_check import write_request
from native_dpp_mirror_check import vop2
from native_scalar_lds_check import sop1, vmove


def words(hexes):
Expand Down Expand Up @@ -53,19 +55,23 @@ def words(hexes):
EPILOGUE = words('70008cbf 001070e008140a80 041070e008150a80 081070e008160a80 0c1070e008170a80 '
'101070e008180a80 000081bf')
REFUSALS = [
('008030dc01007d14', 'global_load_dword with a scalar address pair'), # v[1:2], off
('008030dc01007d14', 'DWORD loads with a scalar address pair'), # 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
('008034dc01000414', 'global_load_dword with a scalar address pair'), # global_load_dwordx2
('008070dc01140400', 'global_load_dword with a scalar address pair'), # global_store_dword
('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
('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', 'global_load_dword with a scalar address pair'), # the lds bit
('000030dc01007d14', 'global_load_dword with a scalar address pair'), # flat_load_dword (segment 0)
('004030dc01007d14', 'global_load_dword with a scalar address pair'), # scratch_load_dword (segment 1)
('0080c8dc01140400', 'global_load_dword with a scalar address pair'), # global_atomic_add
('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
]
POLICY = '009033dc01000414' # global_load_dword v20, v1, s[4:5] glc slc dlc: accepted
INACTIVE = 0x13579bdf
WIDE_OPS = {2: 13, 3: 15, 4: 14}


def reference(drop_offset=False, drop_wrap_guard=False):
Expand All @@ -85,6 +91,48 @@ def word(byte):
return expected


def wide_program(width, case, *, reverse=False):
code = PROLOGUE.copy()
dst, coord, offset = 20, 1, 0
if case == 'alias':
dst, offset = 1, 16
elif case == 'tail':
code += [vop2(0x25, 1, 255, 1), 244]
elif case == 'sparse':
offset = -4
for j in range(width):
code += [vmove(dst + j, 255), INACTIVE]
code += [sop1(3, 126, 255), 0x55555555]
elif case == 'wrap':
coord, offset = 2, 16
elif case == 'extent':
offset = 2044
else:
raise ValueError(case)
code += [0xdc008000 | WIDE_OPS[width] << 18 | (offset & 0xfff), dst << 24 | 4 << 16 | coord]
code.append(0xbf8c0070) # s_waitcnt vmcnt(0) lgkmcnt(0)
if case == 'sparse':
code.append(sop1(3, 126, 193))
for j in range(width):
source = dst + (width - 1 - j if reverse else j)
code += [0xe0701000 | (j * 4), 0x800a0008 | source << 8]
return code + [0xbf810000]


def wide_reference(width, case):
expected = [0x69696969] * 2064
for lane in range(64):
local = lane % 32
if case == 'sparse' and local % 2:
values = [INACTIVE] * width
else:
start = {'alias': local + 4, 'tail': local + 61, 'sparse': local - 1, 'wrap': 1 << 30}[case]
values = [0x51aa0000 + start + j if start >= 0 and start + j < 64 else 0 for j in range(width)]
base = 8 + lane * 8
expected[base:base + width] = values
return expected


def main(compiler, replay, sdk):
with tempfile.TemporaryDirectory() as tmp:
work = Path(tmp)
Expand All @@ -102,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, dwordx2, store, a replaced high half, an advanced pointer, '
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')

code = PROLOGUE + words(' '.join(LOADS.values())) + EPILOGUE
Expand All @@ -116,14 +164,17 @@ def compile_words(code):
counts = [dump.count(t) for t in ('rawBufferLoad.i32(i32 139', 'icmp ule i32', 'icmp uge i32')]
assert counts == [6, 2, 1], counts
print('module: 6 raw buffer loads, 2 upper-bound and 1 lower-bound guards')
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)
assert result.returncode == 0, (result.stdout, result.stderr)
actual = list(struct.unpack('<2064I', dest.read_bytes()))
def replay_words(request):
dest = work / 'readback.bin'
result = subprocess.run([replay, sdk, request, work / 'cache', '0', dest, '--direct'],
capture_output=True, text=True)
if result.returncode == 77:
print(result.stdout + result.stderr)
sys.exit(77)
assert result.returncode == 0, (result.stdout, result.stderr)
return list(struct.unpack('<2064I', dest.read_bytes()))

actual = replay_words(marker)
expected = reference()
diff = [(i, hex(a), hex(b)) for i, (a, b) in enumerate(zip(actual, expected)) if a != b]
assert not diff, diff[:10]
Expand All @@ -132,6 +183,22 @@ def compile_words(code):
assert mutant != expected and actual != mutant, label
print('global_load_dword: 320 words of two groups matched (offsets 0, 16, -8, the 2^32 guard and an '
'unaligned address); the offset-dropped and wrap-guard-dropped references differ')
for width in WIDE_OPS:
for case in ('alias', 'tail', 'sparse', 'wrap'):
marker, run = compile_words(wide_program(width, case))
assert run.returncode == 0, (width, case, run.stdout, run.stderr)
got, expected = replay_words(marker), wide_reference(width, case)
diff = [(i, hex(a), hex(b)) for i, (a, b) in enumerate(zip(got, expected, strict=True)) if a != b]
assert not diff, (width, case, diff[:10])
_, extent = compile_words(wide_program(width, 'extent'))
assert extent.returncode == 0 and f't0 root=0 bytes=0 min_size={2044 + width * 4}' in extent.stdout, (
width, extent.stdout, extent.stderr)
marker, run = compile_words(wide_program(width, 'alias', reverse=True))
assert run.returncode == 0, run.stderr
wrong = replay_words(marker)
changed = sum(a != b for a, b in zip(wrong, wide_reference(width, 'alias'), strict=True))
assert changed == 64 * (width - (width % 2)), (width, changed)
print(f'global_load_dwordx{width}: alias, tail, sparse EXEC and wrap match; reversing results changes {changed} words')


if __name__ == '__main__':
Expand Down
Loading