From c901a1f28d0a61cc6bd1a801eb908d26a67e1e32 Mon Sep 17 00:00:00 2001 From: Leyukaka <45659323+Leyukaka@users.noreply.github.com> Date: Sat, 28 Nov 2026 23:02:50 +0100 Subject: [PATCH] tooling: translate requests written during a compile_dxil watch pass first compile_dxil --watch sorted pending requests by marker path and compiled the whole list before scanning again, so a request written during a pass waited for the whole backlog. As in gfx2hlsl.py (#3396), requests whose ready marker is newer than the watcher's start now go first, newest first, in batches of up to four keys per worker, with a rescan between batches at most once per second. --requests keeps its order; outputs are unchanged. Refs #3392. --- docs/startup-runtime.md | 3 +- tests/native_compile_dxil_watch_check.py | 29 +++++++++ tools/startup/compile_dxil.cpp | 75 +++++++++++++++++------- 3 files changed, 84 insertions(+), 23 deletions(-) diff --git a/docs/startup-runtime.md b/docs/startup-runtime.md index f448bb21f5..f119e7ba2c 100644 --- a/docs/startup-runtime.md +++ b/docs/startup-runtime.md @@ -962,7 +962,8 @@ without `-Render` is ignored with a warning). The watcher waits 2 seconds betwee the watcher started in newest-first order ahead of the older backlog. It processes batches of up to four keys per configured worker and rescans between batches at most once per second, so a pipeline created during the run is translated within seconds and the runtime picks it up on a later draw -instead of on the next run. The runtime +instead of on the next run. `compile_dxil --watch` uses the same ready-marker priority, batch size +and rescan cadence. The runtime looks for an absent manifest again 1 second after the first miss and doubles the wait with each further miss up to 32 seconds. A translation published during the run is therefore used on a draw or Dispatch at most 32 seconds later, and a key that never gets a translation costs one diff --git a/tests/native_compile_dxil_watch_check.py b/tests/native_compile_dxil_watch_check.py index a8bbbbb973..67b1ab2c9a 100644 --- a/tests/native_compile_dxil_watch_check.py +++ b/tests/native_compile_dxil_watch_check.py @@ -219,6 +219,35 @@ def main(argv: list[str]) -> int: assert rs.stdout == rp.stdout, (rs.stdout, rp.stdout) assert G.pending_keys(str(d_parallel), skip_failed=True) == [] + # A fresh marker must jump ahead of a backlog larger than the four-key batch. + # Making every marker old reproduces the legacy path order with the same inputs. + backlog = list(range(1, 6)) + watch_orders = {} + watch_outputs = {} + now = time.time() + for label, fresh_mtime in (("legacy", now - 60), ("fresh", now + 3600)): + order_dir = work / f"order_{label}" + order_dir.mkdir() + for key in backlog: + touch(order_dir / f"req_{key:016x}.c67", b"missing request sidecars", now - 60) + fresh_key = put_compute_request(order_dir, c_code, c_root, c_sa) + assert fresh_key > backlog[-1], f"fixture must put fresh key last in path order: {fresh_key:016x}" + touch(order_dir / f"req_{fresh_key:016x}.c67", mtime=fresh_mtime) + result = run_once(cli, sdk, frontend, order_dir) + assert result.returncode == 0 and not result.stderr, (result.stdout, result.stderr) + assert f"{fresh_key:016x}: compiled compute" in result.stdout, result.stdout + watch_orders[label] = [int(line.split(":", 1)[0], 16) for line in result.stdout.splitlines()] + watch_outputs[label] = { + path.name: path.read_bytes() for path in order_dir.iterdir() if not path.name.startswith("req_") + } + assert set(watch_outputs[label]) == { + *(f"{key:016x}.fail" for key in backlog), f"{fresh_key:016x}.txt", f"{fresh_key:016x}.cs.cso" + } + assert watch_orders["legacy"] == [*backlog, fresh_key], watch_orders + assert fresh_key not in watch_orders["legacy"][:4], watch_orders + assert watch_orders["fresh"] == [fresh_key, *backlog], watch_orders + assert watch_outputs["fresh"] == watch_outputs["legacy"], "watch order changed output bytes" + # RTV variants in --once: compile base + variants, publish rtv1 manifest + .psdep, and recheck on base .psin update. d_rtv = work / "once_rtv" d_rtv.mkdir() diff --git a/tools/startup/compile_dxil.cpp b/tools/startup/compile_dxil.cpp index 7728f227e6..b9908bf75a 100644 --- a/tools/startup/compile_dxil.cpp +++ b/tools/startup/compile_dxil.cpp @@ -712,6 +712,7 @@ struct WatchRequest { std::wstring key; fs::path marker; bool compute = false; + fs::file_time_type marker_mtime{}; }; static bool read_small_file(const fs::path& path, size_t max_bytes, std::string& out) { @@ -726,11 +727,12 @@ static bool read_small_file(const fs::path& path, size_t max_bytes, std::string& return !size || static_cast(file.read(out.data(), size)); } -static bool is_watch_request_pending(const fs::path& request_dir, const WatchRequest& req, bool skip_failed) { +static bool is_watch_request_pending(const fs::path& request_dir, WatchRequest& req, bool skip_failed) { std::error_code ec; const auto marker_mtime = fs::last_write_time(req.marker, ec); if (ec) return false; + req.marker_mtime = marker_mtime; const fs::path stem = request_dir / req.key; const fs::path manifest_path = fs::path(stem) += L".txt"; const fs::path fail_path = fs::path(stem) += L".fail"; @@ -806,7 +808,9 @@ static bool is_watch_request_pending(const fs::path& request_dir, const WatchReq return true; } -static std::vector collect_watch_requests(const fs::path& request_dir, bool skip_failed) { +static std::vector collect_watch_requests(const fs::path& request_dir, bool skip_failed, + fs::file_time_type watch_start, + const std::set& attempted) { std::vector found; for (const auto& file : fs::directory_iterator(request_dir)) { std::error_code ec; @@ -821,7 +825,7 @@ static std::vector collect_watch_requests(const fs::path& request_ if (stem.size() != 20 || stem.rfind(L"req_", 0) != 0) continue; const auto key = stem.substr(4); - if (!is_lower_hex16(key)) + if (!is_lower_hex16(key) || attempted.count(key)) continue; found.push_back({key, fs::weakly_canonical(path), is_compute}); } @@ -840,6 +844,12 @@ static std::vector collect_watch_requests(const fs::path& request_ if (is_watch_request_pending(request_dir, req, skip_failed)) pending.push_back(std::move(req)); } + const auto recent_end = std::stable_partition(pending.begin(), pending.end(), [watch_start](const WatchRequest& req) { + return req.marker_mtime > watch_start; + }); + std::stable_sort(pending.begin(), recent_end, [](const WatchRequest& a, const WatchRequest& b) { + return a.marker_mtime > b.marker_mtime; + }); return pending; } @@ -892,6 +902,7 @@ static const char* compile_watch_item(x2w::ShaderDxilCompiler& compiler, const f } static int run_watch(int argc, wchar_t** argv) { + const auto watch_start = fs::file_time_type::clock::now(); fs::path sdk_dir, cache_dir, request_dir; int positional = 0; int jobs = 1; @@ -941,21 +952,40 @@ static int run_watch(int argc, wchar_t** argv) { x2w::ShaderDxilCompiler coordinator(sdk_dir); bool skip_failed = false; + const auto batch_size = static_cast(jobs) * 4; while (true) { + std::set attempted; std::vector pending; - try { - pending = collect_watch_requests(request_dir, skip_failed); - } catch (const fs::filesystem_error& error) { - if (once) - throw; - std::cerr << "watch scan error, retrying: " << error.what() << '\n'; - std::this_thread::sleep_for(std::chrono::milliseconds(interval_ms)); - continue; - } - if (!pending.empty()) { - std::vector outcomes(pending.size()); + size_t offset = 0; + bool scan_failed = false; + std::optional last_scan; + while (true) { + const auto now = Clock::now(); + if (!last_scan || now - *last_scan >= std::chrono::seconds(1)) { + last_scan = now; + try { + pending = collect_watch_requests(request_dir, skip_failed, watch_start, attempted); + } catch (const fs::filesystem_error& error) { + if (once) + throw; + std::cerr << "watch scan error, retrying: " << error.what() << '\n'; + scan_failed = true; + break; + } + offset = 0; + } + std::vector batch; + // The first pass can retry existing failures, including after a rescan. + while (offset < pending.size() && batch.size() < batch_size) { + const auto& req = pending[offset++]; + attempted.insert(req.key); + batch.push_back(&req); + } + if (batch.empty()) + break; + std::vector outcomes(batch.size()); auto process_item = [&](x2w::ShaderDxilCompiler& compiler, size_t idx) { - const auto& req = pending[idx]; + const auto& req = *batch[idx]; try { outcomes[idx] = compile_watch_item(compiler, cache_dir, request_dir, req); } catch (const std::exception& error) { @@ -970,9 +1000,9 @@ static int run_watch(int argc, wchar_t** argv) { outcomes[idx] = "refused: " + summary; } }; - const int worker_count = (std::min)(jobs, static_cast(pending.size())); + const int worker_count = (std::min)(jobs, static_cast(batch.size())); if (worker_count <= 1) { - for (size_t idx = 0; idx < pending.size(); ++idx) + for (size_t idx = 0; idx < batch.size(); ++idx) process_item(coordinator, idx); } else { std::atomic next{}; @@ -981,17 +1011,17 @@ static int run_watch(int argc, wchar_t** argv) { for (int w = 0; w < worker_count; ++w) { workers.emplace_back(std::async(std::launch::async, [&]() { auto compiler = coordinator.new_worker(); - for (size_t idx = next++; idx < pending.size(); idx = next++) + for (size_t idx = next++; idx < batch.size(); idx = next++) process_item(compiler, idx); })); } for (auto& w : workers) w.get(); } - for (size_t idx = 0; idx < pending.size(); ++idx) { + for (size_t idx = 0; idx < batch.size(); ++idx) { std::string key_ascii; - key_ascii.reserve(pending[idx].key.size()); - for (wchar_t ch : pending[idx].key) + key_ascii.reserve(batch[idx]->key.size()); + for (wchar_t ch : batch[idx]->key) key_ascii.push_back(static_cast(ch)); std::cout << key_ascii << ": " << outcomes[idx] << '\n'; } @@ -999,7 +1029,8 @@ static int run_watch(int argc, wchar_t** argv) { } if (once) return 0; - skip_failed = true; + if (!scan_failed) + skip_failed = true; std::this_thread::sleep_for(std::chrono::milliseconds(interval_ms)); } } catch (const std::exception& error) {