From ecd2646aaa24ac95b97333db348216a92073f14a Mon Sep 17 00:00:00 2001 From: Leyukaka <45659323+Leyukaka@users.noreply.github.com> Date: Fri, 27 Nov 2026 18:31:04 +0100 Subject: [PATCH] shader: read the wave size of slot 67 compute requests; refuse and rebuild stale native outputs Port of #3006 onto main. Bit 20 of .sa dword 4 now sets ComputeRequest::wave32 (set: wave32, clear: wave64). The direct DXIL route compiles a wave64 request with more than 32 threads per group through the existing wave64 path; a wave64 group of at most 32 threads stays on the wave32 model. Request keys and the revision-29 / revision-28 archive handling are unchanged. Native compute manifests end with 'native ' (src/native_compute_output.h). The stand-in refuses a native manifest from another revision, or an unmarked one without the Python translator's .cs.hlsl, and looks again on the ManifestRetry schedule of an absent manifest. compile_dxil --watch rebuilds both, and a native publish removes a leftover .cs.hlsl. Source-built fixtures that relied on the wave32 default now set the bit, and fixtures that copy a Python translation also copy its sidecar. AI-assisted. --- docs/experiments.md | 5 ++ docs/experiments/native-slot67-wave-size.md | 72 +++++++++++++++++++ docs/startup-runtime.md | 7 ++ src/direct_compute_cache.cpp | 4 +- src/gfx1013_compute_native.cpp | 6 +- src/native_compute_output.h | 34 +++++++++ src/shader_compute_request.cpp | 6 +- src/shader_compute_request.h | 1 + src/startup_stubs/d3d12_compute_dispatch.inl | 18 ++++- src/startup_stubs/d3d12_xs_device.cpp | 1 + tests/cmake/native_slot67_wave_size.cmake | 7 ++ tests/native_adjacent_srv_check.py | 2 +- tests/native_compile_dxil_watch_check.py | 39 ++++++++++ tests/native_compute_clear_check.py | 2 +- tests/native_compute_raw_root_check.py | 2 +- tests/native_descriptor_scc_check.py | 5 +- tests/native_dpp_mirror_check.py | 2 +- tests/native_dpp_quad_check.py | 2 +- tests/native_dpp_row_check.py | 2 +- tests/native_global_load_check.py | 2 +- tests/native_indexed_atomic_check.py | 2 +- tests/native_lane_access_check.py | 2 +- tests/native_mtbuf_check.py | 2 +- tests/native_pack_b32_f16_check.py | 2 +- tests/native_scalar_lds_check.py | 10 +-- tests/native_slot67_wave_size_check.py | 69 ++++++++++++++++++ tests/native_startup_request_cache_check.py | 2 + tests/native_wave_request_check.py | 4 +- tests/startup_compute_dispatch_check.py | 25 +++++++ ...startup_compute_root_srv_dispatch_check.py | 2 + tests/startup_compute_translate_check.py | 4 +- tools/startup/compile_dxil.cpp | 18 +++++ 32 files changed, 336 insertions(+), 25 deletions(-) create mode 100644 docs/experiments/native-slot67-wave-size.md create mode 100644 src/native_compute_output.h create mode 100644 tests/cmake/native_slot67_wave_size.cmake create mode 100644 tests/native_slot67_wave_size_check.py diff --git a/docs/experiments.md b/docs/experiments.md index 6c30d84af6..928518b4f8 100644 --- a/docs/experiments.md +++ b/docs/experiments.md @@ -2,6 +2,11 @@ New notes go in `docs/experiments/.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 diff --git a/docs/experiments/native-slot67-wave-size.md b/docs/experiments/native-slot67-wave-size.md new file mode 100644 index 0000000000..fb745305a5 --- /dev/null +++ b/docs/experiments/native-slot67-wave-size.md @@ -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 `.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 `.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. diff --git a/docs/startup-runtime.md b/docs/startup-runtime.md index b5e2e009fd..c627ec9760 100644 --- a/docs/startup-runtime.md +++ b/docs/startup-runtime.md @@ -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 `.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 `.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)). diff --git a/src/direct_compute_cache.cpp b/src/direct_compute_cache.cpp index 85e9bb2443..3ebf00d4e8 100644 --- a/src/direct_compute_cache.cpp +++ b/src/direct_compute_cache.cpp @@ -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 @@ -809,7 +810,8 @@ std::optional 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(); } diff --git a/src/gfx1013_compute_native.cpp b/src/gfx1013_compute_native.cpp index 11ffc7b2ae..3ea27e4ec4 100644 --- a/src/gfx1013_compute_native.cpp +++ b/src/gfx1013_compute_native.cpp @@ -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); diff --git a/src/native_compute_output.h b/src/native_compute_output.h new file mode 100644 index 0000000000..24b8ccd3ba --- /dev/null +++ b/src/native_compute_output.h @@ -0,0 +1,34 @@ +#pragma once + +#include +#include + +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; +} +} diff --git a/src/shader_compute_request.cpp b/src/shader_compute_request.cpp index 31cf4be795..bd9e785b1b 100644 --- a/src/shader_compute_request.cpp +++ b/src/shader_compute_request.cpp @@ -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(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) { diff --git a/src/shader_compute_request.h b/src/shader_compute_request.h index 90470c2729..1e1cd39dd8 100644 --- a/src/shader_compute_request.h +++ b/src/shader_compute_request.h @@ -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; diff --git a/src/startup_stubs/d3d12_compute_dispatch.inl b/src/startup_stubs/d3d12_compute_dispatch.inl index 94bddbb902..59dda9a95b 100644 --- a/src/startup_stubs/d3d12_compute_dispatch.inl +++ b/src/startup_stubs/d3d12_compute_dispatch.inl @@ -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 uds, srvs, uavs; x2w_startup::TranslatedSamplers samplers; // static ones only x2w_startup::ComPtr root; @@ -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 + // .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) { @@ -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] = ""; diff --git a/src/startup_stubs/d3d12_xs_device.cpp b/src/startup_stubs/d3d12_xs_device.cpp index 4750e8f40b..e24f2acda5 100644 --- a/src/startup_stubs/d3d12_xs_device.cpp +++ b/src/startup_stubs/d3d12_xs_device.cpp @@ -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" diff --git a/tests/cmake/native_slot67_wave_size.cmake b/tests/cmake/native_slot67_wave_size.cmake new file mode 100644 index 0000000000..d9b4773a17 --- /dev/null +++ b/tests/cmake/native_slot67_wave_size.cmake @@ -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" + "$" "$" "${DXC_BIN_DIR}") + set_tests_properties(native_slot67_wave_size_hardware PROPERTIES SKIP_RETURN_CODE 77 TIMEOUT 60) +endif() diff --git a/tests/native_adjacent_srv_check.py b/tests/native_adjacent_srv_check.py index d2a8ae6dc6..12e1708db3 100644 --- a/tests/native_adjacent_srv_check.py +++ b/tests/native_adjacent_srv_check.py @@ -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)", ""), diff --git a/tests/native_compile_dxil_watch_check.py b/tests/native_compile_dxil_watch_check.py index 4f5b61a712..9342f23c89 100644 --- a/tests/native_compile_dxil_watch_check.py +++ b/tests/native_compile_dxil_watch_check.py @@ -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(" int: cs_manifest = cache / f"{cs_key:016x}.txt" assert cs_cso.read_bytes().startswith(b"DXBC") assert cs_manifest.stat().st_mtime_ns >= cs_cso.stat().st_mtime_ns + assert not (cache / f"{cs_key:016x}.cs.hlsl").exists() assert not (cache / f"req_{cs_key:016x}.cso").exists() assert cs_manifest.read_text(encoding="ascii") == ( "x2w-manifest 2\n" @@ -189,6 +190,7 @@ def check_controls(cli: Path, sdk: Path) -> int: "udsrc 1 rootconst 0 1\n" "srv 0 buf table 1 64\n" "uav 0 table 1 32\n" + "native 1\n" ) assert G.pending_keys(str(cache), skip_failed=False) == [], "published entries not recognised as complete" diff --git a/tests/native_wave_request_check.py b/tests/native_wave_request_check.py index 0965e3e047..cdab046316 100644 --- a/tests/native_wave_request_check.py +++ b/tests/native_wave_request_check.py @@ -165,7 +165,7 @@ def main(cli, gpu, sdk): source.write_text('#define SIG "DescriptorTable(SRV(t0,numDescriptors=16)),DescriptorTable(UAV(u0,numDescriptors=16))"') root = work / "root.bin" subprocess.run([Path(sdk) / "dxc.exe", "-T", "rootsig_1_1", "-E", "SIG", "-Fo", root, source], check=True) - sa = struct.pack("<7I", 0, 0, 0, 8 | (8 << 16), 1, 0, 0x8d8c) + sa = struct.pack("<7I", 0, 0, 0, 8 | (8 << 16), 1 | (1 << 20), 0, 0x8d8c) # s4 and s5 are the two guest tables. The byte offsets deliberately differ # from the desktop registers that the translator assigns to them. words = [ @@ -206,7 +206,7 @@ def main(cli, gpu, sdk): source.write_text('#define SIG "DescriptorTable(SRV(t0,numDescriptors=16)),' 'DescriptorTable(UAV(u0,numDescriptors=16)),RootConstants(num32BitConstants=4,b0)"') subprocess.run([Path(sdk) / "dxc.exe", "-T", "rootsig_1_1", "-E", "SIG", "-Fo", root, source], check=True) - sa = struct.pack("<7I", 0, 0, 0, 32 | (1 << 16), 1, 0, 0x198) + sa = struct.pack("<7I", 0, 0, 0, 32 | (1 << 16), 1 | (1 << 20), 0, 0x198) # One 32-lane group per tile. s6/s7 are root constants and s12/s13 are the group IDs. code = [ 0xbe940304, 0xbe950380, 0xbe960305, 0xbe970380, diff --git a/tests/startup_compute_dispatch_check.py b/tests/startup_compute_dispatch_check.py index 33ca1733c4..638325881f 100644 --- a/tests/startup_compute_dispatch_check.py +++ b/tests/startup_compute_dispatch_check.py @@ -22,6 +22,8 @@ 7. slot 35 selects the correct table at descriptor index 2 instead of the swapped pair at the heap start; setting the same heap again preserves the table, but changing the compute root or view heap after binding must refuse at the table lookup, not an earlier check. +8. published output compatibility: the same bytecode is refused with either stale native manifest + and runs with the current native marker or with the Python sidecar. Exit 77 only if the initial probe cannot use the adapter or required heaps. """ import os @@ -78,6 +80,7 @@ def main(consumer, dxc, adapter): with tempfile.TemporaryDirectory() as temporary: directory = Path(temporary) cache, env = prepare_case(directory, adapter) + env.pop('X2W_NATIVE_SHADERS', None) # Exercise published translations. untouched, expected = [C.SENTINEL] * C.WORDS, C.expected('A') assert expected != untouched @@ -98,6 +101,28 @@ def main(consumer, dxc, adapter): text=True, env=dict(os.environ, X2W_DXC=dxc)).stdout assert f'{key:016x}: ok' in out, out + manifest = cache / f'{key:016x}.txt' + hlsl = cache / f'{key:016x}.cs.hlsl' + python_manifest = manifest.read_bytes() + python_hlsl = hlsl.read_bytes() + assert not any(line.startswith(b'native ') for line in python_manifest.splitlines()) + hlsl.unlink() + for marker, reason in ( + (b'', 'native translation without a revision; rebuild it'), + (b'native 0\n', 'native translation from another compiler revision; rebuild it'), + ): + manifest.write_bytes(python_manifest + marker) + got, result = dispatch(directory, consumer, env, 'dispatch') + assert got == untouched, 'a stale native output wrote the output buffer' + assert len(lines(result, 'Dispatch(3, 2, 2) refused: ' + reason)) == 1, result.stderr[-3000:] + + # Reuse working bytecode to isolate the manifest gate from native adapter requirements. + manifest.write_bytes(python_manifest + b'native 1\n') + got, result = dispatch(directory, consumer, env, 'dispatch') + assert got == expected and not lines(result, 'refused:'), result.stderr[-3000:] + hlsl.write_bytes(python_hlsl) + manifest.write_bytes(python_manifest) + got, result = dispatch(directory, consumer, env, 'dispatch') wrong = [i for i in range(C.WORDS) if got[i] != expected[i]] assert not wrong, 'word=%d got=%08x expected=%08x (%d words differ)\n%s' % ( diff --git a/tests/startup_compute_root_srv_dispatch_check.py b/tests/startup_compute_root_srv_dispatch_check.py index 177462f9c6..e9022ef842 100644 --- a/tests/startup_compute_root_srv_dispatch_check.py +++ b/tests/startup_compute_root_srv_dispatch_check.py @@ -185,6 +185,8 @@ def check_single_root_srv(base, consumer, dxc, adapter): sub_cache, sub_env = setup_case(sub, two_srvs=False, adapter=adapter) (sub_cache / f"{key:016x}.txt").write_text(bad_manifest, encoding="utf-8") (sub_cache / f"{key:016x}.cs.cso").write_bytes(cso_path.read_bytes()) + # The Python translator's sidecar marks these unmarked manifests as Python output, not stale native output. + (sub_cache / f"{key:016x}.cs.hlsl").write_bytes(hlsl_path.read_bytes()) got, result = run(sub, consumer, sub_env, "dispatch-root-srv") assert got == [SENTINEL] * WORDS assert lines(result, f"compute pipeline {key:016x} not translated: {expected_reason}"), result.stderr[-3000:] diff --git a/tests/startup_compute_translate_check.py b/tests/startup_compute_translate_check.py index dbf5d63248..069b6b2a9d 100644 --- a/tests/startup_compute_translate_check.py +++ b/tests/startup_compute_translate_check.py @@ -29,8 +29,8 @@ def state_a(group, rsrc2): - # The observed offsets: dword 3 = X | Y << 16, dword 4 = Z, dword 6 = COMPUTE_PGM_RSRC2. - return struct.pack('<7I', 0, 0, 0, group[0] | group[1] << 16, group[2], 0, rsrc2) + # Dword 3 holds X and Y, dword 4 holds Z and the wave32 bit, and dword 6 holds COMPUTE_PGM_RSRC2. + return struct.pack('<7I', 0, 0, 0, group[0] | group[1] << 16, group[2] | (1 << 20), 0, rsrc2) def write_request(cache, code, state, data=DATA, root=None, desc_sizes=None): diff --git a/tools/startup/compile_dxil.cpp b/tools/startup/compile_dxil.cpp index 34f7cafcaa..b8a6b80a0f 100644 --- a/tools/startup/compile_dxil.cpp +++ b/tools/startup/compile_dxil.cpp @@ -1,5 +1,6 @@ #include "shader_dxil_compiler.h" #include "direct_compute_cache.h" +#include "native_compute_output.h" #include "gfx1013_pixel.h" #include "gfx1013_sampled_pixel.h" #include "gfx1013_vertex_id.h" @@ -645,6 +646,19 @@ static bool is_watch_request_pending(const fs::path& request_dir, const WatchReq const bool has_manifest = fs::is_regular_file(manifest_path, ec); if (has_manifest && !lost) { + if (req.compute) { + // A native manifest from another revision, or an unmarked one without the Python translator's + // .cs.hlsl, is stale. Other outputs, and a manifest that cannot be read, go on to the usual + // timestamp and bytecode checks. + if (std::ifstream file{manifest_path, std::ios::binary}) { + const std::string manifest((std::istreambuf_iterator(file)), std::istreambuf_iterator()); + const auto output = x2w::native_compute_output(manifest); + const bool python = fs::is_regular_file(fs::path(stem) += L".cs.hlsl", ec); + if (output == x2w::NativeComputeOutput::incompatible || + (output == x2w::NativeComputeOutput::unmarked && !python)) + return true; + } + } const auto manifest_mtime = fs::last_write_time(manifest_path, ec); if (!ec && !(psin_mtime.has_value() && *psin_mtime > manifest_mtime)) { if (req.compute) { @@ -715,6 +729,9 @@ static const char* compile_watch_item(x2w::ShaderDxilCompiler& compiler, const f fs::remove(manifest_path, ignored); std::ostringstream sink; if (req.compute) { + // A Python translation's sidecar must not outlive its manifest: the stand-in reads an unmarked manifest + // next to a .cs.hlsl as the Python translator's output. + fs::remove(fs::path(stem) += L".cs.hlsl", ignored); bool hit = false; auto program = x2w::prepare_direct_compute(req.marker, 0, false, cache_dir, hit, x2w::HeapViewContract::none); auto manifest = x2w::format_compute_runtime_manifest(program); @@ -1001,6 +1018,7 @@ int wmain(int argc, wchar_t** argv) { const fs::path manifest_path = fs::path(stems[i]) += L".txt"; std::error_code ignored; fs::remove(manifest_path, ignored); + fs::remove(fs::path(stems[i]) += L".cs.hlsl", ignored); // see compile_watch_item auto manifest = compile_one(compiler, job_profile(i), inputs[i], outputs[i], argv[2], report, nullptr, true); if (manifest) publish_atomic(manifest_path, manifest->data(), manifest->size()); fs::remove(fs::path(stems[i]) += L".fail", ignored);