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
19 changes: 19 additions & 0 deletions docs/experiments/native-global-srv-heap-replay.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,16 @@ The check runs, in order:
for selections 0 and 1 (output equals the slot's data), compiled by `compile_dxil --requests`, replayed with the
same result, and replaying selection 0 against the slot 1 reference reports a mismatch.

Section 11 of the check (#3224) forms the heap descriptor address the way a waterfall loop over a lane-selected slot
does: `s_add_u32 s24, s2, s17`, `s_add_u32 s25, 0, src_scc`, `s_load_dwordx4 s[20:23], s[24:25], 0x0` instead of the
SGPR offset of the load. The compiler tracks SCC as the carry of that add and reads the pair as the same indexed heap
descriptor, under the same contract. Without `.views` it is refused with the heap text. The program is recorded,
compiled and replayed for selections 0 and 1 like PROGRAM, with the slot 1 mismatch control. The other operand order
with `s_addc_u32 s25, 0, 0` compiles. These stay refused: a constant offset (`unbound numeric SGPR s2`), `s_mov_b32
s25, 0` or an `s_cmp` between the adds (`heap descriptor address high word is not the carry of its offset add`) and an
immediate offset on the load. `tests/native_heap_offset_add_check.py` compiles the same form from a slot 67 request with
both heap flags: it reaches the heap text like the SGPR-offset form, and the controls keep their refusals.

## Result

```
Expand Down Expand Up @@ -122,6 +132,15 @@ ctest --preset dev --no-tests=error -R "^native_global_srv_heap_replay(_warp|_ha
load (`buffer_load_format_*`) through the heap descriptor. After the change, the test passes with 84 assertions. The
run against the earlier commit `7582c01d` fails at the `window_loop` shape (old text "heap byte window in a program
with a backward branch"). The run stops there.
- Added heap address (#3224), dev build on `main` `8007a539` plus the change, NVIDIA GeForce RTX 4090 (driver
32.0.16.1088), Windows 11 build 26300, Windows SDK 10.0.26100 DXC, D3D12 debug layer not enabled:
`native_global_srv_heap_replay_hardware` passed, 118 assertions. `native_heap_offset_add` passed, 5 of 5. `_warp`
skipped (77) as above.
- With the compiler sources of `8007a539` (before the change), the replay fails at the first section 11 assertion
(`unbound numeric SGPR s2`) and `native_heap_offset_add` fails 3 of 5 (`unbound numeric SGPR s5`).
- With the high-word check removed, both tests fail at the `mov_high` controls.
- `ctest --preset dev --no-tests=error -R "native_|startup_compute|compile_dxil" -j 8`: 415 tests, 404 passed and
11 skipped (77, WARP).

## Limits

Expand Down
49 changes: 47 additions & 2 deletions src/gfx1013_compute_native.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,8 @@ static std::optional<unsigned> carry_destination(const Inst &i) {
}
struct Symbol {
enum class Type { unknown, constant, masked, table, descriptor, indexed_descriptor, root_address, primitive_mask,
stride, byte_stride, zero_stride_word, byte_count, clear_oob, byte_oob, lane_mask, uniform_mask, unbound } type{};
stride, byte_stride, zero_stride_word, byte_count, clear_oob, byte_oob, lane_mask, uniform_mask,
heap_offset, heap_carry, unbound } type{};
Word value = 0, offset = 0;
unsigned index_mask = 0, definition = 0;
bool numeric() const { return type == Type::unknown || type == Type::constant || type == Type::masked || type == Type::uniform_mask; }
Expand All @@ -469,6 +470,8 @@ struct State {
// Encoded VGPR and guest lane. A join can lose the identity while
// retaining the fact that some path omitted the runtime lane write.
std::map<std::pair<unsigned, unsigned>, std::optional<Symbol>> lane_spills;
// SCC, tracked only as the carry of a heap offset add (see heap_offset_add).
Symbol scc{};
Symbol &operator[](unsigned n) { return scalar[n]; }
const Symbol &operator[](unsigned n) const { return scalar[n]; }
const Symbol &at(unsigned n) const { return scalar.at(n); }
Expand Down Expand Up @@ -561,6 +564,30 @@ Symbol source(const Inst &i, unsigned code, const State &s) {
return {Symbol::Type::constant, i.literal};
return {};
}
// The low word of a resource-heap descriptor address formed by adding a byte offset, as in a waterfall loop over a
// lane-selected slot: s_add_u32 lo, <resource heap>, <byte offset SGPR>, then the carry into the high word. Returns the
// operand index of the offset. A constant offset is not matched: the selected slot must come from GPU data.
static std::optional<unsigned> heap_offset_add(const Inst &i, const State &s) {
if (i.name != "s_add_u32" || i.dst > 105) return {};
for (unsigned k = 0; k < 2; ++k) {
const auto heap = source(i, i.src[k], s), offset = source(i, i.src[1 - k], s);
if (heap.type == Symbol::Type::table && heap.value == compute_resource_heap && !heap.offset &&
i.src[1 - k] <= 105 && offset.numeric() && offset.type != Symbol::Type::constant)
return 1 - k;
}
return {};
}
// The high word of that address: s_add_u32 hi, 0, src_scc or s_addc_u32 hi, 0, 0.
static bool heap_offset_carry(const Inst &i) {
return i.dst <= 105 && ((i.name == "s_add_u32" && ((i.src[0] == 128 && i.src[1] == 253) ||
(i.src[0] == 253 && i.src[1] == 128))) ||
(i.name == "s_addc_u32" && i.src[0] == 128 && i.src[1] == 128));
}
// s[base:base+1] holds the resource-heap base plus a byte offset with its own carry in the high word.
static bool heap_offset_address(const State &s, unsigned base) {
return base + 1 < s.size() && s[base].type == Symbol::Type::heap_offset &&
s[base + 1].type == Symbol::Type::heap_carry && s[base + 1].definition == s[base].definition;
}
// Derive the rewritten byte-address V# symbolically. Each field carries its
// source table/slot identity. Numeric consumption still requires real bits.
// heap_window: the direct path under HeapViewContract::raw_buffer also rewrites a
Expand Down Expand Up @@ -599,6 +626,10 @@ State transfer(const Inst &i, State s, bool wave64, bool graphics, bool heap_win
refuse(i.pc, "invalid SGPR");
s[n] = v;
};
const Symbol carry = s.scc;
if (i.enc == Encoding::Sop1 || i.enc == Encoding::Sop2 || i.enc == Encoding::Sopc || i.enc == Encoding::Sopk)
s.scc = {};
const auto heap_add = direct && !graphics ? heap_offset_add(i, s) : std::nullopt;
if (direct && !graphics) {
if (i.name == "v_writelane_b32") {
if (auto lane = guest_spill_lane(i, wave64)) {
Expand Down Expand Up @@ -632,6 +663,13 @@ State transfer(const Inst &i, State s, bool wave64, bool graphics, bool heap_win
(i.src[0] == 193 || i.src[0] == 128) && (i.src[1] == 193 || i.src[1] == 128)) {
set(i.dst,{Symbol::Type::uniform_mask,i.pc,0});
set(i.dst+1,{Symbol::Type::uniform_mask,i.pc,1});
} else if (heap_add) {
const auto offset = source(i, i.src[*heap_add], s);
set(i.dst, {Symbol::Type::heap_offset, compute_resource_heap, 0,
offset.type == Symbol::Type::masked ? offset.value : ~0u, i.pc});
s.scc = {Symbol::Type::heap_carry, compute_resource_heap, 0, 0, i.pc};
} else if (direct && !graphics && carry.type == Symbol::Type::heap_carry && heap_offset_carry(i)) {
set(i.dst, carry);
} else if (auto field = byte_descriptor_field(i,s,heap_window)) {
set(i.dst,*field);
} else if (i.enc == Encoding::FixedSetup) {
Expand All @@ -643,9 +681,12 @@ State transfer(const Inst &i, State s, bool wave64, bool graphics, bool heap_win
const unsigned offset_reg = i.b >> 25;
const Symbol index = offset_reg < smem_sgpr_limit(!graphics && !wave64) ? s[offset_reg] : Symbol{};
unsigned n = memory_count(i);
const bool heap_address = i.op == 2 && i.b >> 25 == 125 && heap_offset_address(s, i.src[0]);
for (unsigned j = 0; j < n; ++j) {
Symbol v{};
if (i.op == 2 && i.b >> 25 != 125 && base.type == Symbol::Type::table) {
if (heap_address) {
v = {Symbol::Type::indexed_descriptor, compute_resource_heap, (i.b & 0xfffff) + 4 * j, base.index_mask, i.pc};
} else if (i.op == 2 && i.b >> 25 != 125 && base.type == Symbol::Type::table) {
v = {Symbol::Type::indexed_descriptor, base.value, (i.b & 0xfffff) + 4*j,
index.type == Symbol::Type::masked || index.type == Symbol::Type::constant ? index.value : ~0u, i.pc};
} else if (i.op < 8 && base.type == Symbol::Type::table)
Expand Down Expand Up @@ -845,6 +886,10 @@ Program parse(const string &code, unsigned entry, const State &initial, bool gra
changed = true;
}
}
if (!(in.scc == after.scc) && !(in.scc == Symbol{})) {
in.scc = {};
changed = true;
}
for (auto &[key, value] : in.lane_spills) {
const auto other = after.lane_spills.find(key);
if (value && (other == after.lane_spills.end() || !(other->second == value))) {
Expand Down
25 changes: 25 additions & 0 deletions src/gfx1013_direct_compute.inl
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,23 @@ struct DirectCompute {
const unsigned limit = smem_sgpr_limit(!graphics_input && !wave64);
const auto &low = s.at(i.src[0]);
const bool root = low.type == Symbol::Type::root_address;
if (low.type == Symbol::Type::heap_offset) {
// A resource-heap address formed by s_add_u32 (heap_offset_add): the same slot read as the SGPR-offset
// load below, with the byte offset in the low word.
if (!heap_offset_address(s, i.src[0]))
refuse(i.pc, "heap descriptor address high word is not the carry of its offset add");
if (i.op != 2)
refuse(i.pc, "heap descriptor address must load one descriptor with s_load_dwordx4");
refuse_heap_contract(i.pc);
if (so != 125)
refuse(i.pc, "heap descriptor address with a second offset register");
if (i.b & 0xfffff)
refuse(i.pc, "heap descriptor load with an immediate offset addresses another slot than the captured selection");
auto index = ir.binary("lshr", read(i.src[0]), "5");
for (unsigned j = 0; j < 4; ++j)
write(i.dst + j, index);
return;
}
if (!root && low.type == Symbol::Type::table && low.value == compute_resource_heap) {
// Reads from the whole descriptor heap: only the indexed descriptor load of a captured raw buffer slot.
if (i.op >= 8)
Expand Down Expand Up @@ -2301,6 +2318,14 @@ struct DirectCompute {
}
void arithmetic(const Inst &i, const State &state, const string *permuted = nullptr, bool compute = false) {
auto n = i.name;
if (compute) {
if (const auto offset = heap_offset_add(i, state)) {
// The heap base is slot 0 of ResourceDescriptorHeap: the low word keeps the byte offset, the carry is 0.
write(i.dst, operand32(i, i.src[*offset]));
write(128, "0");
return;
}
}
if (compute && (n == "v_dot4_i32_i8" || n == "v_dot4_u32_u8")) {
// Integer dot products use both packed words as-is. Require the
// canonical VOP3P OP_SEL/OP_SEL_HI and zero NEG_LO/NEG_HI.
Expand Down
7 changes: 7 additions & 0 deletions tests/cmake/native_heap_offset_add.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# A slot 67 resource-heap descriptor address formed by s_add_u32 with its carry (#3224).
if(TARGET compile_dxil AND EXISTS "${DXC_BIN_DIR}/dxcompiler.dll" AND EXISTS "${DXC_BIN_DIR}/dxil.dll")
add_test(NAME native_heap_offset_add COMMAND "${INTAKE_PYTHON}" -B
"${CMAKE_SOURCE_DIR}/tests/native_heap_offset_add_check.py"
"$<TARGET_FILE:compile_dxil>" "${DXC_BIN_DIR}")
set_tests_properties(native_heap_offset_add PROPERTIES TIMEOUT 60)
endif()
65 changes: 64 additions & 1 deletion tests/native_global_srv_heap_replay_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
ResourceDescriptorHeap[slot] and stores them to u0. Slots 0 and 1 are raw buffers with different data, slot 2 is a
texture, slot 3 a typed buffer. The fixture's `record` mode captures the GPU selector and the slot view types into a
req_<key>.views file and refuses the incompatible slots before the dispatch; `compile_dxil` and the fixture's `replay`
mode consume the same file offline. Exit 77 when the fixture reports a capability skip.
mode consume the same file offline. Section 11 forms the heap descriptor address by adding the byte offset to the heap
base (s_add_u32, carry into the high word) instead of the SGPR offset of the load. Exit 77 when the fixture reports a
capability skip.
"""
import pathlib
import shutil
Expand Down Expand Up @@ -393,4 +395,65 @@ def raw_load_request(name, width=1, *, offen=False, soffset=18, imm=0, flags=0,
(code, out, err), _, _, _, cso = raw_load_request(name, **addressing)
require(code == 1 and diagnostic in err and not cso.exists(), f"{name}: refused with '{diagnostic}'")

# 11. The heap descriptor address formed by adding the byte offset to the heap base, as a waterfall loop over a
# lane-selected slot does, instead of the SGPR offset of the load (encodings checked with tools/disasm_rdna2.py):
# s_add_u32 s24, s2, s17 ; s_add_u32 s25, 0, src_scc ; s_load_dwordx4 s[20:23], s[24:25], 0x0
# It reads the same slot: recorded, compiled offline and replayed like PROGRAM.
ADD_LO, ADD_HI, ADDED_LOAD = 0x80181102, 0x8019fd80, [0xf408050c, 0xfa000000]

def added(lo=ADD_LO, between=(), hi=ADD_HI, load_words=ADDED_LOAD):
return words_list[:heap_load] + [lo, *between, hi, *load_words] + words_list[heap_load + 2:]

ADDED = added()
directory, added_stem = shader_request("added_nometa", ADDED)
out_dir = base / "added_nometa_out"
code, out, err = run([compile_dxil, sdk, cache, "gfx1013-container-cs", "--requests", directory, out_dir])
require(code == 1 and HEAP_REFUSAL in err and not any(out_dir.glob("*.cso")),
"compile_dxil added address without .views refuses the heap read with the slot/type text")
added_outputs = {}
for n in (0, 1):
directory, added_stem = shader_request(f"added{n}", ADDED)
code, out, err = run([fixture, "record", sdk, cache, directory / (added_stem + ".cdx"), adapter, n, "ok"])
require(code == 0 and "output matches slot %d data" % n in out,
f"record added select {n}: u0 equals the slot {n} data read through the added address")
out_dir = base / f"added{n}_out"
code, out, err = run([compile_dxil, sdk, cache, "gfx1013-container-cs", "--requests", directory, out_dir])
cso = out_dir / (added_stem[len("req_"):] + ".cs.cso")
require(code == 0 and "requests=1 compiled=1 refused=0" in out and cso.exists(),
f"compile_dxil added select {n}: compiled and published a .cso")
added_outputs[n] = (directory, cso)
code, out, err = run([fixture, "replay", sdk, cache, directory / (added_stem + ".cdx"), cso,
directory / (added_stem + ".views"), adapter, "ok", n])
require(code == 0 and "output matches slot %d data" % n in out, f"replay added select {n}: output equals slot {n} data")
directory, cso = added_outputs[0]
code, out, err = run([fixture, "replay", sdk, cache, directory / (added_stem + ".cdx"), cso,
directory / (added_stem + ".views"), adapter, "ok", 1])
require(code == 1 and "mismatch word 0" in out, "replay added select 0 compared with slot 1 data reports a mismatch")

high_word = "heap descriptor address high word is not the carry of its offset add"
added_shapes = {
# s_add_u32 s24, s17, s2 and s_addc_u32 s25, 0, 0: the other operand order and carry form
"added_swapped_addc": (added(lo=0x80180211, hi=0x82198080), None),
# s_add_u32 s24, s2, 0x40: a constant offset names a slot that is not the captured GPU selection
"added_constant": (added(lo=0x8018ff02, between=(0x00000040,)), "unbound numeric SGPR s2"),
# s_mov_b32 s25, 0: the high word drops the carry of the offset add
"added_mov_high": (added(hi=0xbe990380), high_word),
# s_cmp_lg_u32 s16, s16 between the adds: SCC is no longer the carry
"added_scc_clobbered": (added(between=(0xbf071010,)), high_word),
# s_load_dwordx4 s[20:23], s[24:25], 0x20
"added_immediate": (added(load_words=[0xf408050c, 0xfa000020]),
"heap descriptor load with an immediate offset addresses another slot than the captured selection"),
}
for name, (program, expected) in added_shapes.items():
directory, shape_stem = shader_request("shape_" + name, program)
(directory / (shape_stem + ".views")).write_text(good, newline="\n")
out_dir = base / ("shape_" + name + "_out")
code, out, err = run([compile_dxil, sdk, cache, "gfx1013-container-cs", "--requests", directory, out_dir])
if expected is None:
require(code == 0 and "requests=1 compiled=1 refused=0" in out and any(out_dir.glob("*.cso")),
f"compile_dxil shape '{name}': compiled")
else:
require(code == 1 and expected in err and not any(out_dir.glob("*.cso")),
f"compile_dxil shape '{name}': refused with '{expected}'")

print("native_global_srv_heap_replay: all checks passed")
Loading
Loading