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
5 changes: 5 additions & 0 deletions docs/experiments.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

New notes go in `docs/experiments/<topic>.md` (see [the folder guide](experiments/README.md)); this file keeps the older notes.

## Slot 67 wave size in direct compute

State block A bit 20 of dword 4 selects the guest wave size.
[Capture inference and hardware fixture](experiments/native-slot67-wave-size.md)

## Plain binary16 arithmetic in direct compute

VOP2 and VOP3 add, sub, subrev and mul use binary32 arithmetic and an integer nearest-even conversion back
Expand Down
72 changes: 72 additions & 0 deletions docs/experiments/native-slot67-wave-size.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# Slot 67 wave size in direct compute

Issue #2999. Slot 67 compute requests carry no documented wave size, and direct compute used to compile every
`.c67` request as wave32. Bit 20 of `.sa` dword 4 (little-endian; bits 15:0 are Z) now sets
`ComputeRequest::wave32`: set means wave32, clear means wave64. A `.sa` that is not 28 bytes is still refused.

## Why this bit

This is an inference from a local capture of 2,689 slot 67 requests, not from documentation. The capture was
decoded up to the first `s_endpgm`, and only counts and instruction shape were kept.

- Every program with a `*_saveexec_b32` and no `*_saveexec_b64` has the bit set (1,385 of 1,385). Every program
with a `*_saveexec_b64` and no `*_saveexec_b32` has it clear (565 of 565).
- The next best bit of `.sa` or `.sb` agrees on 87%: bit 5 of dword 3, the X = 32 bit.
- `v_mbcnt_hi_u32_b32` appears only with the bit clear (197 programs). Every bit-clear program that uses
`v_mbcnt_lo_u32_b32` also uses `v_mbcnt_hi_u32_b32`. The 84 bit-set programs that use `v_mbcnt_lo_u32_b32`
never use `v_mbcnt_hi_u32_b32`.
- Group sizes mostly follow the bit. Of the 784 requests with a 64-thread group, 739 have it clear. Of the 1,433
with a 32-thread group, 1,405 have it set.
- Bits 31:16 of dword 4 take only two values in the capture, 0x0000 and 0x0010.

## Translation

The direct DXIL route of `translate_compute` compiles a wave64 request through the wave64 path that
`translate_compute_container` already uses for a stage without `wave32` (two 32-lane host waves per guest wave).
A wave64 request whose group has at most 32 threads stays on the wave32 model, as before the wave size was read.
Its one guest wave only has lanes 0..31, so EXEC's high word is zero and `v_mbcnt_hi` adds nothing for its lanes.
One known difference remains: a wave64 compare or carry-out also writes zero to the high word of an SGPR pair
destination, and the wave32 model leaves that word as it was. The legacy HLSL profile and the container route are
unchanged.

Compute requests keep their `29cs` keys, and revision-28 archives are still read. Published native compute manifests now include `native 1`.
`compile_dxil --watch` rebuilds an older native output under the same key. The stand-in refuses it until
the rebuilt manifest appears, looking again on subsequent dispatches on the ManifestRetry schedule (1 s, doubling up to 32 s). An unmarked
manifest with a `<key>.cs.hlsl` sidecar is a Python translation and stays usable. `--requests` rebuilds
every output, the frontend cache is keyed on a hash of the compiler sources, and the in-process route
translates again in each process. A native publish now removes a `<key>.cs.hlsl` left by the Python translator,
so the pair cannot pass as a Python output. One case is not detected: a directory where an earlier
`compile_dxil` already republished a Python-translated key keeps that key's `.cs.hlsl` next to an unmarked
native manifest. Rerun `--requests` into such a directory once. The reload of a refused
manifest is not covered by a test; each check runs a new process.

Measured with `compile_dxil --jobs 8 gfx1013-direct-cs@36 --requests` on `cpp-migration` `e0d83759`, before and
after:

| Requests | Compiled before | Compiled after | Newly compiled | No longer compiled |
|---|---|---|---|---|
| Capture, 2,689 | 389 | 393 | 46 | 42 |
| One start-up run, 2,718 | 402 | 406 | 46 | 42 |

- The 46 newly compiled requests are wave64 programs with 64-thread groups.
- The 42 that no longer compile are wave64 programs that the wave32 model used to accept. They now stop at
"multi-wave64 exchange requires converged group control flow", a limit of the existing wave64 path.

## Fixture

`tests/native_slot67_wave_size_check.py` builds the same program as three slot 67 requests that differ in this
bit and in the group size. The program stores `v_mbcnt_lo` followed by `v_mbcnt_hi` at each thread's index. The
CPU reference expects:

- with the bit clear and 64-thread groups, `tid % 64`;
- with the bit set and 64-thread groups, `tid % 32`;
- with the bit clear and 32-thread groups, `tid % 32`.

The fixture also checks every untouched buffer word.

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

The fixture returns 77 when replay reports that the required adapter capabilities are unavailable. Source-built
fixtures that relied on the wave32 default now set the bit.
7 changes: 7 additions & 0 deletions docs/startup-runtime.md
Original file line number Diff line number Diff line change
Expand Up @@ -983,6 +983,13 @@ wave size 32, so an adapter without 32-lane waves refuses every program by that
translation is tried once per key and before a published `<key>.txt`. It covers compute only; graphics
pipelines still need a published translation.

Published compute translations from `compile_dxil --requests` and `--watch` carry a `native 1` line in the
`x2w-manifest 2` manifest. The stand-in requires the current native output revision. An unmarked manifest
is accepted with a `<key>.cs.hlsl` sidecar, which identifies a Python translation. `compile_dxil --watch`
rebuilds stale native outputs and leaves Python publications in place. The stand-in retries a stale or
absent manifest on subsequent dispatches on the ManifestRetry schedule (1 s, doubling up to 32 s), so a running process can
pick up a rebuilt output (by the code path; no test covers the reload). Compute request keys stay at `29cs`, and revision-28 archives are still read.

Check with `ctest --preset dev -R '^startup_compute_dispatch_native_(warp|hardware)$' -V`
([`tests/startup_compute_dispatch_native_check.py`](../tests/startup_compute_dispatch_native_check.py);
[route, controls and results](experiments/startup-compute-native-dispatch.md)).
Expand Down
4 changes: 3 additions & 1 deletion src/direct_compute_cache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "gfx1013_isa.h"
#include "native_graphics_bindings.h"
#include "shader_compute_request.h"
#include "native_compute_output.h"
#include "native_shader_revision.h"
#include "shader_dxil_compiler.h"
#include <atomic>
Expand Down Expand Up @@ -809,7 +810,8 @@ std::optional<std::string> format_compute_runtime_manifest(const NativeComputeSh
return std::nullopt;
out << "udsrc " << ud_index++ << " stride " << srv << '\n';
}
out << srv_lines.str() << uav_lines.str() << sampler_lines.str();
out << srv_lines.str() << uav_lines.str() << sampler_lines.str()
<< "native " << native_compute_output_revision << '\n';
return out.str();
}

Expand Down
6 changes: 5 additions & 1 deletion src/gfx1013_compute_native.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1691,7 +1691,11 @@ static NativeComputeShader translate_program(const ComputeRequest &request, unsi
initial[next] = {Symbol::Type::table, compute_resource_heap};
initial[next + 1] = {Symbol::Type::table, compute_sampler_heap};
}
const bool wave64 = stage && !stage->wave32;
// The legacy HLSL profile models wave32. Direct DXIL supports both guest wave sizes. A wave64 request whose
// group has at most 32 threads stays on the wave32 model, as before the wave size was read: its one guest wave
// has lanes 0..31 only, so EXEC's high word is zero and v_mbcnt_hi adds nothing for its lanes. The model
// does not write the zero high word that a wave64 compare or carry-out stores in an SGPR pair destination.
const bool wave64 = stage ? !stage->wave32 : direct_dxil && !request.wave32 && threads > 32;
// The byte-address rewrite of a GPU-selected heap descriptor is read only under the raw_buffer contract.
const bool heap_window = direct_dxil && heap_view == HeapViewContract::raw_buffer;
auto program = parse(request.code, entry, initial, direct_dxil, false, wave64, false, heap_window);
Expand Down
34 changes: 34 additions & 0 deletions src/native_compute_output.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#pragma once

#include <charconv>
#include <string_view>

namespace x2w {
// Bump whenever a native translation of an unchanged request can change meaning.
constexpr unsigned native_compute_output_revision = 1;

enum class NativeComputeOutput { unmarked, current, incompatible };

inline NativeComputeOutput native_compute_output(std::string_view manifest)
{
auto output = NativeComputeOutput::unmarked;
while (!manifest.empty()) {
const auto end = manifest.find('\n');
auto line = manifest.substr(0, end);
manifest.remove_prefix(end == std::string_view::npos ? manifest.size() : end + 1);
if (!line.empty() && line.back() == '\r')
line.remove_suffix(1);
if (line != "native" && line.substr(0, 7) != "native ")
continue;
if (output != NativeComputeOutput::unmarked || line.size() <= 7)
return NativeComputeOutput::incompatible;
unsigned revision = 0;
const auto last = line.data() + line.size();
const auto parsed = std::from_chars(line.data() + 7, last, revision);
if (parsed.ec != std::errc{} || parsed.ptr != last || revision != native_compute_output_revision)
return NativeComputeOutput::incompatible;
output = NativeComputeOutput::current;
}
return output;
}
}
6 changes: 5 additions & 1 deletion src/shader_compute_request.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,11 @@ x2w::ComputeRequest x2w::read_compute_request(const fs::path &marker) {
if (root.empty() || sa.size() != 28 || code.empty() || data.empty() || code.size() % 4)
throw std::runtime_error(sa.size() != 28 ? "compute state block A is not 28 bytes"
: "inconsistent compute request");
return {std::move(code), std::move(data), std::move(root), std::move(desc), std::move(sa), std::move(sb), expected};
// Bit 20 of dword 4 (bits 15:0 are Z): set for wave32, clear for wave64. Inferred from captured requests
// (docs/experiments/native-slot67-wave-size.md).
const bool wave32 = (static_cast<unsigned char>(sa[18]) & 0x10) != 0;
return {std::move(code), std::move(data), std::move(root), std::move(desc), std::move(sa), std::move(sb), expected,
wave32};
}

std::string x2w::read_shader_container_bytes(const fs::path &file) {
Expand Down
1 change: 1 addition & 0 deletions src/shader_compute_request.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ namespace x2w {
struct ComputeRequest {
std::string code, data, root, description, sa, sb;
uint64_t key;
bool wave32 = true;
};
struct ComputeContainer {
std::string bytes, root;
Expand Down
18 changes: 17 additions & 1 deletion src/startup_stubs/d3d12_compute_dispatch.inl
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ struct ComputeTranslation {
};
u64 key = 0;
int state = 0; // 0 not loaded, 1 ready, 2 unavailable
x2w_startup::ManifestRetry manifest_retry; // only an absent manifest can become ready later
x2w_startup::ManifestRetry manifest_retry; // absent and stale manifests can become ready later
std::vector<Slot> uds, srvs, uavs;
x2w_startup::TranslatedSamplers samplers; // static ones only
x2w_startup::ComPtr<ID3D12RootSignature> root;
Expand Down Expand Up @@ -105,6 +105,21 @@ const char* load_compute_translation(ComputeTranslation& tr)
tr.manifest_retry.missed(GetTickCount64());
return nullptr;
}
// A native manifest from another compiler revision, or an unmarked one without the Python translator's
// <key>.cs.hlsl, is stale: it is looked for again on the same schedule as an absent one.
const auto output = x2w::native_compute_output(manifest);
if (output == x2w::NativeComputeOutput::incompatible) {
tr.manifest_retry.missed(GetTickCount64());
return "native translation from another compiler revision; rebuild it";
}
if (output == x2w::NativeComputeOutput::unmarked) {
const DWORD attributes = cache_path(path, L"", tr.key, L".cs.hlsl")
? GetFileAttributesW(path) : INVALID_FILE_ATTRIBUTES;
if (attributes == INVALID_FILE_ATTRIBUTES || (attributes & FILE_ATTRIBUTE_DIRECTORY)) {
tr.manifest_retry.missed(GetTickCount64());
return "native translation without a revision; rebuild it";
}
}
tr.manifest_retry.found();
unsigned declared_uds = 0, line_number = 0;
for (std::size_t at = 0; at < manifest.size(); ++line_number) {
Expand All @@ -121,6 +136,7 @@ const char* load_compute_translation(ComputeTranslation& tr)
if (line != "stage cs") return "not a compute manifest";
continue;
}
if (line.compare(0, 7, "native ") == 0) continue;
unsigned i = 0, x = 0, y = 0, z = 0, w = 0;
int a = 0, b = 0, c = 0, d = 0, used = 0;
char name[16] = "";
Expand Down
1 change: 1 addition & 0 deletions src/startup_stubs/d3d12_xs_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
#include "title_heap_memory.h"
#include "startup_stubs/xmem_section_buffer.h"
#include "native_buffer_store_view.h"
#include "native_compute_output.h"
#ifdef X2W_NATIVE_SHADERS // d3d12_xs_shim only: the C++ shader compiler (startup_stubs/d3d12_compute_dispatch.inl)
#include "gfx1013_compute_native.h"
#include "shader_dxil_compiler.h"
Expand Down
7 changes: 7 additions & 0 deletions tests/cmake/native_slot67_wave_size.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
if(TARGET compile_dxil AND TARGET native_store_replay AND
EXISTS "${DXC_BIN_DIR}/dxcompiler.dll" AND EXISTS "${DXC_BIN_DIR}/dxil.dll")
add_test(NAME native_slot67_wave_size_hardware COMMAND "${INTAKE_PYTHON}" -B
"${CMAKE_CURRENT_SOURCE_DIR}/tests/native_slot67_wave_size_check.py"
"$<TARGET_FILE:compile_dxil>" "$<TARGET_FILE:native_store_replay>" "${DXC_BIN_DIR}")
set_tests_properties(native_slot67_wave_size_hardware PROPERTIES SKIP_RETURN_CODE 77 TIMEOUT 60)
endif()
2 changes: 1 addition & 1 deletion tests/native_adjacent_srv_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def main(gpu, sdk):
0xf408040a, 0x16000020, 0xf408080b, 0xfa000000,
scalar(0x1e, 12, 6, 130), vmove(8, 12), sop1(3, 126, 129), vmove(3, 130),
0xe0002000, 0x80040103, 0xe0701000, 0x80080108, 0xbf810000]
sa = struct.pack("<7I", 0, 0, 0, 32 | 1 << 16, 1, 0, 0x80 | 6 << 1)
sa = struct.pack("<7I", 0, 0, 0, 32 | 1 << 16, 1 | (1 << 20), 0, 0x80 | 6 << 1)
for name, second, reason in (
("append", "SRV(t18, space=2)", ""),
("null", "SRV(t18, space=2)", ""),
Expand Down
39 changes: 39 additions & 0 deletions tests/native_compile_dxil_watch_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,45 @@ def main(argv: list[str]) -> int:
assert (d1 / f"{g_key:016x}.txt").stat().st_mtime_ns == txt_mtime
assert (d1 / f"{c_key:016x}.txt").stat().st_mtime_ns == cs_mtime

# Seed outputs that satisfy the old watcher's completeness check.
cs_manifest = d1 / f"{c_key:016x}.txt"
cs_cso = d1 / f"{c_key:016x}.cs.cso"
cs_hlsl = d1 / f"{c_key:016x}.cs.hlsl"
current_manifest = cs_manifest.read_bytes()
old_manifest = b"".join(line for line in current_manifest.splitlines(keepends=True)
if not line.startswith(b"native "))
assert current_manifest.splitlines().count(b"native 1") == 1
assert not cs_hlsl.exists()
dummy_cso = b"DXBC" + b"\0" * 20 + struct.pack("<III", 36, 1, 0)
for stale_manifest in (old_manifest, old_manifest + b"native 0\n"):
cs_cso.write_bytes(dummy_cso)
cs_manifest.write_bytes(stale_manifest)
refreshed = run_once(cli, sdk, frontend, d1)
assert refreshed.returncode == 0, (refreshed.stdout, refreshed.stderr)
assert f"{c_key:016x}: compiled compute" in refreshed.stdout, refreshed.stdout
assert cs_manifest.read_bytes() == current_manifest
assert cs_cso.read_bytes().startswith(b"DXBC") and cs_cso.read_bytes() != dummy_cso
assert not cs_hlsl.exists()

cs_hlsl.write_bytes(b"// Python output\n")
cs_cso.write_bytes(dummy_cso)
cs_manifest.write_bytes(old_manifest)
python_outputs = (cs_hlsl, cs_cso, cs_manifest)
before = [(path.read_bytes(), path.stat().st_mtime_ns) for path in python_outputs]
skipped = run_once(cli, sdk, frontend, d1)
assert skipped.returncode == 0, (skipped.stdout, skipped.stderr)
assert f"{c_key:016x}:" not in skipped.stdout, skipped.stdout
assert [(path.read_bytes(), path.stat().st_mtime_ns) for path in python_outputs] == before

# A Python output with incomplete bytecode is still pending. The native rebuild removes its sidecar, so
# the stand-in no longer reads the new unmarked-looking pair as a Python output.
cs_cso.write_bytes(dummy_cso[:16])
rebuilt = run_once(cli, sdk, frontend, d1)
assert rebuilt.returncode == 0, (rebuilt.stdout, rebuilt.stderr)
assert f"{c_key:016x}: compiled compute" in rebuilt.stdout, rebuilt.stdout
assert cs_manifest.read_bytes() == current_manifest
assert not cs_hlsl.exists()

# Corrupting/truncating a stage .cso triggers resume and restores valid DXBC.
vs_cso = d1 / f"{g_key:016x}.vs.cso"
orig_vs = vs_cso.read_bytes()
Expand Down
2 changes: 1 addition & 1 deletion tests/native_compute_clear_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def write_request(work, words, root, sa=None):
code = struct.pack(f"<{len(words)}I", *words)
data = b"synthetic"
if sa is None:
sa = struct.pack("<7I", 0, 0, 0, 32 | (8 << 16), 1, 0, (6 << 1) | (1 << 10))
sa = struct.pack("<7I", 0, 0, 0, 32 | (8 << 16), 1 | (1 << 20), 0, (6 << 1) | (1 << 10))
desc = struct.pack("<12Q", 0, 0, len(sa), 0, 0, 0, len(code), 0, len(data), 0, 0, 0)
parts = {".c67":code, ".data":data, ".root":root, ".desc":desc, ".sa":sa, ".sb":b""}
payload = b"x2w-startup-shader-28cs" + b"".join(struct.pack("<Q", len(p))+p for p in parts.values())
Expand Down
2 changes: 1 addition & 1 deletion tests/native_compute_raw_root_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def signature(text):
return binary.read_bytes()

def compile_case(code, root, user=10, error=None):
sa = struct.pack("<7I", 0, 0, 0, 32 | (1 << 16), 1, 0, user << 1)
sa = struct.pack("<7I", 0, 0, 0, 32 | (1 << 16), 1 | (1 << 20), 0, user << 1)
marker = write_request(work, code, root, sa)
result = subprocess.run([exe, sdk, work / "cache", "gfx1013-direct-cs@0",
marker, marker.with_suffix(".cso")], text=True, capture_output=True)
Expand Down
5 changes: 4 additions & 1 deletion tests/native_descriptor_scc_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,10 @@ def root_bytes(srv_count):
capture_output=True, text=True)
assert result.returncode == 0 and f"frontend_cache_hit={hit}" in result.stdout, result.stdout + result.stderr
[manifest] = published.glob("*.txt")
assert manifest.read_text().splitlines() == [
# The native output revision line (#3006) is checked separately; the binding lines stay exact.
lines = manifest.read_text().splitlines()
assert sum(line.startswith("native ") for line in lines) == 1, manifest.read_text()
assert [line for line in lines if not line.startswith("native ")] == [
"x2w-manifest 2", "stage cs", "group 32 1 1", "ud 2",
"udsrc 0 rootconst 1 0", "udsrc 1 stride 0",
"srv 0 buf table 0 32", "uav 0 table 0 64",
Expand Down
2 changes: 1 addition & 1 deletion tests/native_dpp_mirror_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def main(cli, replay, raster, sdk):
root, vs = work/"root", work/"vs.cso"
subprocess.run([dxc,"-T","rootsig_1_1","-E","SIG","-Fo",root,shader],check=True)
subprocess.run([dxc,"-T","vs_6_6","-E","vs","-Fo",vs,shader],check=True)
sa = struct.pack("<7I",0,0,0,32 | 1 << 16,1,0,0x8a)
sa = struct.pack("<7I",0,0,0,32 | 1 << 16,1 | (1 << 20),0,0x8a)
sparse = 0x955ad3b6
for mask, active, fi, zero in ((7,0xffffffff,1,0), (15,0xffffffff,1,0),
(7,sparse,1,0), (7,sparse,0,0), (7,sparse,0,1)):
Expand Down
2 changes: 1 addition & 1 deletion tests/native_dpp_quad_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def reference(op, selection, active, fi, bound_zero):


def request(work, root, program, threads=32):
sa = struct.pack("<7I", 0, 0, 0, threads | 1 << 16, 1, 0, 0x8A)
sa = struct.pack("<7I", 0, 0, 0, threads | 1 << 16, 1 | (1 << 20), 0, 0x8A)
return write_request(work, program, root, sa)


Expand Down
2 changes: 1 addition & 1 deletion tests/native_dpp_row_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def reference(op, direction, amount, fi, bound_zero):


def request(work, root, op, instruction, control, threads=64):
sa = struct.pack("<7I", 0, 0, 0, threads | (1 << 16), 1, 0, 0x8A)
sa = struct.pack("<7I", 0, 0, 0, threads | (1 << 16), 1 | (1 << 20), 0, 0x8A)
return write_request(work, program(op, instruction, control), root, sa)


Expand Down
Loading
Loading