diff --git a/docs/experiments/native-compile-dxil-watch.md b/docs/experiments/native-compile-dxil-watch.md index 1c5b5f8167..c7c28b745e 100644 --- a/docs/experiments/native-compile-dxil-watch.md +++ b/docs/experiments/native-compile-dxil-watch.md @@ -11,6 +11,10 @@ containers are skipped, truncated or missing stage `.cso` files are resumed, `.psin` additions/updates/removals trigger rechecks, pre-existing `.fail` entries are retried once on the first pass, and unchanged `.fail` entries are skipped on subsequent passes until the marker or `.psin` is rewritten. +- The published `.psinh` digest is taken from the base `.psin`/`.ps57in` pair. An `.rtv` variant resolves the + same base sidecars as its mtime check, so a deliberate or preserved-mtime base sidecar change withdraws the + variant too. The digest is computed before and after the graphic translation; a change during compilation + refuses the publish instead of pairing new bytes with DXIL made from the old ones. ## Verification @@ -19,6 +23,7 @@ cmake --preset release cmake --build --preset release --target compile_dxil ctest --preset release --no-tests=error -V -R "^native_compile_dxil_watch$" +ctest --preset release --no-tests=error -j 1 -R "^(native_startup_request_cache_controls|native_rtv_variant_controls)$" ``` ## Limits diff --git a/docs/experiments/native-startup-request-cache.md b/docs/experiments/native-startup-request-cache.md index b946aab35f..b7dcce8db1 100644 --- a/docs/experiments/native-startup-request-cache.md +++ b/docs/experiments/native-startup-request-cache.md @@ -22,6 +22,10 @@ the startup runtime cache loaders on WARP and hardware without manual renaming o `.ps.cso`, `.gs.cso` for `gfx1013-container-rect`, and `.cs.cso` for compute), withdraws any prior `.txt` before rewriting stages or on refusal, records `.psdep` when `.psin` was used, and atomically publishes `.txt` after all stage `.cso` files are in place. + - The watch and batch writers also publish `.psinh`, an FNV-1a digest of the base `.psin`/`.ps57in` + extension, length and bytes. A `.rtv` variant resolves its base sidecars, so the digest and mtime checks + invalidate the variant with its base. The digest is computed before and after translation; a sidecar that + changes during the compile refuses the manifest write. ## Verification and negative controls diff --git a/tests/native_startup_request_cache_check.py b/tests/native_startup_request_cache_check.py index f6154395bb..802cb10c87 100644 --- a/tests/native_startup_request_cache_check.py +++ b/tests/native_startup_request_cache_check.py @@ -29,6 +29,13 @@ COMPUTE_RSRC2 = C.USER7 | C.TGID_X | C.TGID_Y | C.TGID_Z | C.tidig(3) +def put_variant(cache: Path, base: int, formats: tuple[int, ...]) -> int: + line = "rtv1 " + " ".join(map(str, (base, *formats))) + key = G.fnv(line.encode("ascii")) + (cache / f"req_{key:016x}.rtv").write_text(line, encoding="ascii") + return key + + def fullscreen_vs_words() -> bytes: vop1 = lambda op, dst, src: 0x7E000000 | dst << 17 | op << 9 | src vop2 = lambda op, dst, src0, vsrc1: op << 25 | dst << 17 | vsrc1 << 9 | src0 @@ -301,7 +308,82 @@ def expect_graphics_refusal(label: str, setup, expected_stderr: str) -> None: assert not (bad_cs_dir / f"{bad_cs_key:016x}.txt").exists() assert not (bad_cs_dir / f"{bad_cs_key:016x}.cs.cso").exists() - print("native_startup_request_cache controls: current and archived graphics (.psin) + compute (.c67) cache entries and 8 refusal controls passed") + # 7. Watch invalidation: sidecar content, not its timestamp, decides whether a published manifest is current. + watch_cache = work / "watch-sidecar" + watch_cache.mkdir() + watch_key = put_graphics_request(watch_cache, VS_WORDS, ps_words(4), g_root, TARGET_MASKS) + watch_manifest = watch_cache / f"{watch_key:016x}.txt" + watch_psin = watch_cache / f"req_{watch_key:016x}.psin" + watch_sidecar = watch_cache / f"req_{watch_key:016x}.ps57in" + watch_fail = watch_cache / f"{watch_key:016x}.fail" + watch_digest = watch_cache / f"{watch_key:016x}.psinh" + + variant_key = put_variant(watch_cache, watch_key, (28,)) + variant_manifest = watch_cache / f"{variant_key:016x}.txt" + + def run_watch() -> list[str]: + result = subprocess.run( + [cli, "--watch", sdk, frontend, watch_cache, "--once"], + capture_output=True, text=True, + ) + assert result.returncode == 0, (result.stdout, result.stderr) + return result.stdout.splitlines() + + first_lines = run_watch() + assert any(line.endswith(": compiled graphics") for line in first_lines), first_lines + assert watch_manifest.is_file() and watch_digest.is_file() + assert f"{variant_key:016x}: compiled graphics" in first_lines, first_lines + assert variant_manifest.is_file(), variant_manifest + published_ns = watch_manifest.stat().st_mtime_ns + variant_ns = variant_manifest.stat().st_mtime_ns + + # Unchanged sidecar bytes: the published manifest is a cache hit and is not rewritten. + second_lines = run_watch() + assert not any("compiled graphics" in line for line in second_lines), second_lines + assert watch_manifest.stat().st_mtime_ns == published_ns + assert variant_manifest.stat().st_mtime_ns == variant_ns + + # A contradictory slot-57 sidecar with an older mtime must withdraw the stale manifest. + conflicting = b"PSI1" + struct.pack("(data); + for (size_t i = 0; i < size; ++i) { + hash ^= bytes[i]; + hash *= 1099511628211ull; + } +} + +static std::optional graphics_psin_path(const fs::path& marker); + +static bool pixel_sidecar_hash(const fs::path& marker, std::string& digest) { + uint64_t hash = 1469598103934665603ull; + bool any = false; + fs::path psin = marker; + if (marker.extension() == L".rtv") { + const auto base = graphics_psin_path(marker); + if (!base) + return false; + psin = *base; + } else { + psin.replace_extension(L".psin"); + } + for (const auto& path : {psin, fs::path(psin).replace_extension(L".ps57in")}) { + std::error_code ec; + if (!fs::is_regular_file(path, ec)) + continue; + std::ifstream file(path, std::ios::binary); + if (!file) + return false; + const std::vector bytes((std::istreambuf_iterator(file)), std::istreambuf_iterator()); + const auto name = path.extension().wstring(); + sidecar_hash_bytes(hash, name.data(), name.size() * sizeof(wchar_t)); + const uint64_t count = bytes.size(); + sidecar_hash_bytes(hash, &count, sizeof(count)); + sidecar_hash_bytes(hash, bytes.data(), bytes.size()); + any = true; + } + if (!any) + return false; + char text[17]{}; + std::snprintf(text, sizeof(text), "%016llx", static_cast(hash)); + digest = text; + return true; +} + +static bool read_published_digest(const fs::path& path, std::string& digest) { + std::ifstream file(path, std::ios::binary); + if (!file) + return false; + digest.assign((std::istreambuf_iterator(file)), std::istreambuf_iterator()); + return !digest.empty(); +} + static void publish_atomic(const fs::path& destination, const void* data, size_t size) { static std::atomic serial{}; auto temp=destination; @@ -717,7 +772,16 @@ 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) { const auto manifest_mtime = fs::last_write_time(manifest_path, ec); - if (!ec && !(psin_mtime.has_value() && *psin_mtime > manifest_mtime)) { + bool sidecar_changed = false; + if (!req.compute) { + std::string sidecar_digest; + if (pixel_sidecar_hash(req.marker, sidecar_digest)) { + std::string published_digest; + sidecar_changed = !read_published_digest(fs::path(stem) += L".psinh", published_digest) || + published_digest != sidecar_digest; + } + } + if (!ec && !sidecar_changed && !(psin_mtime.has_value() && *psin_mtime > manifest_mtime)) { if (req.compute) { if (has_complete_dxbc(fs::path(stem) += L".cs.cso")) return false; @@ -797,6 +861,8 @@ static const char* compile_watch_item(x2w::ShaderDxilCompiler& compiler, const f fs::remove(fail_path, ignored); return "compiled compute"; } + std::string sidecar_before; + const bool sidecar_present_before = pixel_sidecar_hash(req.marker, sidecar_before); auto input = x2w::read_graphics_request(req.marker); bool hit = false; auto pair = x2w::prepare_direct_graphics(input.a, input.b, cache_dir, hit, input.root, input.input_layout, @@ -809,6 +875,12 @@ static const char* compile_watch_item(x2w::ShaderDxilCompiler& compiler, const f : "RTV variant has no runtime manifest: its vertex inputs, target types or bindings are not described"); auto vs_dxil = compile_to_dxil(compiler, pair.vertex.dxil_ir, L"vs_6_6", cache_dir, true, sink); auto ps_dxil = compile_to_dxil(compiler, pair.pixel.dxil_ir, L"ps_6_6", cache_dir, true, sink); + std::string sidecar_after; + const bool sidecar_present_after = pixel_sidecar_hash(req.marker, sidecar_after); + if (sidecar_present_before != sidecar_present_after || sidecar_before != sidecar_after) { + fs::remove(manifest_path, ignored); + throw std::runtime_error("pixel input sidecar changed during compilation"); + } publish_atomic(fs::path(stem) += L".vs.cso", vs_dxil.data(), vs_dxil.size()); publish_atomic(fs::path(stem) += L".ps.cso", ps_dxil.data(), ps_dxil.size()); const fs::path psdep_path = fs::path(stem) += L".psdep"; @@ -816,6 +888,10 @@ static const char* compile_watch_item(x2w::ShaderDxilCompiler& compiler, const f publish_atomic(psdep_path, input.pixel_input_description.data(), input.pixel_input_description.size()); else fs::remove(psdep_path, ignored); + if (sidecar_present_before) + publish_atomic(fs::path(stem) += L".psinh", sidecar_before.data(), sidecar_before.size()); + else + fs::remove(fs::path(stem) += L".psinh", ignored); publish_atomic(manifest_path, manifest->data(), manifest->size()); fs::remove(fail_path, ignored); return "compiled graphics"; @@ -1085,6 +1161,7 @@ int wmain(int argc, wchar_t** argv) { if(graphics_batch) { reserve_output(fs::path(stems[i]) += L".ps.cso"); reserve_output(fs::path(stems[i]) += L".psdep"); + reserve_output(fs::path(stems[i]) += L".psinh"); if(rectangles) reserve_output(fs::path(stems[i]) += L".gs.cso"); } } @@ -1107,6 +1184,8 @@ int wmain(int argc, wchar_t** argv) { const fs::path psdep_path = fs::path(stems[i]) += L".psdep"; std::error_code ignored; fs::remove(manifest_path, ignored); + std::string sidecar_before; + const bool sidecar_present_before = pixel_sidecar_hash(inputs[i], sidecar_before); auto input = x2w::read_graphics_request(inputs[i]); bool hit = false; auto pair = x2w::prepare_direct_graphics(input.a, input.b, argv[2], hit, input.root, input.input_layout, input.pixel_entry, @@ -1134,6 +1213,10 @@ int wmain(int argc, wchar_t** argv) { } auto vs_dxil = compile_to_dxil(compiler, pair.vertex.dxil_ir, L"vs_6_6", argv[2], true, report); auto ps_dxil = compile_to_dxil(compiler, pair.pixel.dxil_ir, L"ps_6_6", argv[2], true, report); + std::string sidecar_after; + const bool sidecar_present_after = pixel_sidecar_hash(inputs[i], sidecar_after); + if (sidecar_present_before != sidecar_present_after || sidecar_before != sidecar_after) + throw std::runtime_error("pixel input sidecar changed during compilation"); if (!gs_dxil.empty()) publish_atomic(fs::path(stems[i]) += L".gs.cso", gs_dxil.data(), gs_dxil.size()); publish_atomic(outputs[i], vs_dxil.data(), vs_dxil.size()); @@ -1141,6 +1224,10 @@ int wmain(int argc, wchar_t** argv) { if (manifest) { if (!input.pixel_input_description.empty()) publish_atomic(psdep_path, input.pixel_input_description.data(), input.pixel_input_description.size()); + if (sidecar_present_before) + publish_atomic(fs::path(stems[i]) += L".psinh", sidecar_before.data(), sidecar_before.size()); + else + fs::remove(fs::path(stems[i]) += L".psinh", ignored); publish_atomic(manifest_path, manifest->data(), manifest->size()); } fs::remove(fs::path(stems[i]) += L".fail", ignored);