From 81711d0004ba28e5614b87db1e75ac61aa7749ed Mon Sep 17 00:00:00 2001 From: lukaslw-lab <291379150+lukaslw-lab@users.noreply.github.com> Date: Mon, 30 Nov 2026 03:28:04 +0100 Subject: [PATCH] compile_dxil: index the graphics interface catalog on several threads --index-graphics-interfaces DIR CACHE [--jobs N] (1-64, default 1). Workers read, parse and publish the containers; their keys are merged afterwards in sorted file order, so the catalog is byte-identical for any N. The first failure in file order still fails the install before a new index is written. Refs #3534 Co-Authored-By: Claude Opus 5.5 --- src/direct_compute_cache.cpp | 66 +++++++++++++------ src/direct_compute_cache.h | 4 +- ...native_graphics_interface_library_check.py | 10 +++ tools/startup/compile_dxil.cpp | 11 +++- 4 files changed, 66 insertions(+), 25 deletions(-) diff --git a/src/direct_compute_cache.cpp b/src/direct_compute_cache.cpp index e87118cca3..2806ca64db 100644 --- a/src/direct_compute_cache.cpp +++ b/src/direct_compute_cache.cpp @@ -17,6 +17,7 @@ #include #include #include +#include #include #include @@ -297,31 +298,54 @@ GuestShaderStage reviewed_graphics_stage(std::string_view raw, GpuStage kind, co return match_graphics_stage(raw, blob, exact, kind); } -size_t index_graphics_interfaces(const fs::path &source, const fs::path &cache) { +size_t index_graphics_interfaces(const fs::path &source, const fs::path &cache, unsigned jobs) { const auto dir = cache / "interfaces" / "library"; + std::vector files; + for (const auto &entry : fs::recursive_directory_iterator(source)) + if (entry.is_regular_file() && entry.path().extension() == L".cdxil") files.push_back(entry.path()); + std::sort(files.begin(), files.end()); + // Workers read and publish containers; keys are merged below in file order. + struct Indexed { Bytes id; std::vector keys; std::exception_ptr error; }; + std::vector results(files.size()); + std::atomic next{}; + auto work = [&] { + for (size_t i = next++; i < files.size(); i = next++) { + auto &out = results[i]; + try { + const auto blob = read_shader_container_bytes(files[i]); + const auto stages = read_guest_shader_container(blob); + out.id = shader_digest(blob); + for (const auto &stage : stages) { + const bool mesh = stage.stage == GpuStage::ms; + if (!mesh && stage.stage != GpuStage::vs && stage.stage != GpuStage::ps) continue; + out.keys.push_back((mesh ? "M" : "F") + shader_digest(stage.code)); + if (!stage.exports.empty()) out.keys.push_back("F" + shader_digest(stage.full_precision_code())); + if (embedded_graphics_id(stage.code)) + out.keys.push_back((mesh ? "N" : "I") + shader_digest(std::string_view(stage.code).substr(0,24))); + } + if (!out.keys.empty()) publish(dir / (out.id + ".stage"), out.id, blob); + } catch (...) { + out.error = std::current_exception(); + } + } + }; + const auto workers = (std::min)(static_cast((std::max)(jobs, 1u)), files.size()); + if (workers <= 1) { + work(); + } else { + std::vector threads; + for (size_t w = 0; w < workers; ++w) threads.emplace_back(work); + for (auto &thread : threads) thread.join(); + } InterfaceMap index; size_t count = 0; - for (const auto &entry : fs::recursive_directory_iterator(source)) { - if (!entry.is_regular_file() || entry.path().extension() != L".cdxil") continue; - auto blob = read_shader_container_bytes(entry.path()); - auto stages = read_guest_shader_container(blob); - const auto id = shader_digest(blob); - auto add = [&](const Bytes &key) { - auto [it, fresh] = index.emplace(key,id); - if (!fresh && it->second != id) it->second.assign(64,'-'); - }; - bool graphics = false; - for (const auto &stage : stages) { - const bool mesh = stage.stage == GpuStage::ms; - if (!mesh && stage.stage != GpuStage::vs && stage.stage != GpuStage::ps) continue; - graphics = true; - add((mesh ? "M" : "F") + shader_digest(stage.code)); - if (!stage.exports.empty()) add("F" + shader_digest(stage.full_precision_code())); - if (embedded_graphics_id(stage.code)) - add((mesh ? "N" : "I") + shader_digest(std::string_view(stage.code).substr(0,24))); + for (const auto &result : results) { + if (result.error) std::rethrow_exception(result.error); + if (result.keys.empty()) continue; + for (const auto &key : result.keys) { + auto [it, fresh] = index.emplace(key, result.id); + if (!fresh && it->second != result.id) it->second.assign(64,'-'); } - if (!graphics) continue; - publish(dir / (id + ".stage"), id, blob); ++count; } if (!count) throw std::runtime_error("no graphics .cdxil containers in interface directory"); diff --git a/src/direct_compute_cache.h b/src/direct_compute_cache.h index 548a2109d7..4f6c69bab7 100644 --- a/src/direct_compute_cache.h +++ b/src/direct_compute_cache.h @@ -25,7 +25,9 @@ ComputePackCounts build_compute_frontend_pack(const std::filesystem::path &reque unsigned entry, size_t part_limit = compute_pack_part_limit); // Part n of the pack whose part 0 is file: compute-0.pack -> compute-0..pack. std::filesystem::path compute_pack_part(const std::filesystem::path &file, unsigned n); -size_t index_graphics_interfaces(const std::filesystem::path &source, const std::filesystem::path &cache); +// jobs: worker threads (1 = in this thread). The catalog does not depend on it. +size_t index_graphics_interfaces(const std::filesystem::path &source, const std::filesystem::path &cache, + unsigned jobs = 1); // A logical match identifies the source shader. Only exact matches establish // the compiled entry ABI, instruction offsets, and resource register layout. GuestShaderStage reviewed_graphics_stage(std::string_view raw, GpuStage kind, diff --git a/tests/native_graphics_interface_library_check.py b/tests/native_graphics_interface_library_check.py index d5475858ca..f022934fa9 100644 --- a/tests/native_graphics_interface_library_check.py +++ b/tests/native_graphics_interface_library_check.py @@ -54,9 +54,19 @@ def batch(folder, output, **kwargs): other = base/"other" publish(other, words(*prefix,0xbf800000,0xbf800000,*branch,0xf800180f,0,end),vs,root) batch(other,base/"ambiguous",expected="ambiguous reviewed graphics interfaces") + # Workers do not change the catalog, the ambiguous key included. + def tree(folder): + root = folder / "interfaces" / "library" + return {p.relative_to(root): p.read_bytes() for p in root.rglob("*") if p.is_file()} + run("--index-graphics-interfaces", library, base / "one") + out = run("--index-graphics-interfaces", library, base / "three", "--jobs", "3") + assert "indexed_graphics_containers=3" in out, out + assert tree(base / "one") == tree(base / "three") + run("--index-graphics-interfaces", library, base / "zero", "--jobs", "0", expected="--jobs must be") # A failed rebuild must leave the published generation usable. (library/"broken.cdxil").write_bytes(b"bad") assert subprocess.run([compiler,*install],capture_output=True).returncode + assert subprocess.run([compiler,*install,"--jobs","3"],capture_output=True).returncode batch(requests,base/"retained") # Two reflected parameter rows. The fixed PS uses P1/P2 for x diff --git a/tools/startup/compile_dxil.cpp b/tools/startup/compile_dxil.cpp index 658b89f045..04a9ac38f4 100644 --- a/tools/startup/compile_dxil.cpp +++ b/tools/startup/compile_dxil.cpp @@ -1544,9 +1544,14 @@ int wmain(int argc, wchar_t** argv) { return 1; } } - if (argc == 4 && std::wstring_view(argv[1]) == L"--index-graphics-interfaces") { + if (argc > 1 && std::wstring_view(argv[1]) == L"--index-graphics-interfaces") { + if (argc != 4 && !(argc == 6 && std::wstring_view(argv[4]) == L"--jobs")) { + std::cerr << "usage: compile_dxil --index-graphics-interfaces REVIEWED_DIRECTORY CACHE_DIRECTORY [--jobs N]\n"; + return 2; + } try { - const auto stages = x2w::index_graphics_interfaces(argv[2],argv[3]); + const unsigned jobs = argc == 6 ? static_cast(parse_jobs_count(argv[5])) : 1u; + const auto stages = x2w::index_graphics_interfaces(argv[2],argv[3],jobs); std::cout << "indexed_graphics_containers=" << stages << "\n"; return 0; } catch (const std::exception &error) { @@ -1575,7 +1580,7 @@ int wmain(int argc, wchar_t** argv) { "Use --refusals REQUEST_DIRECTORY [--jobs N] [--json] [--mesh-layout FILE] for a read-only refusal summary.\n" "Use --watch SDK_DIRECTORY CACHE_DIRECTORY REQUEST_DIRECTORY [--jobs N] [--interval MS] [--once] to translate pending requests.\n" "Use --rekey OLD_CACHE_DIR NEW_REQUEST_DIR [--jobs N] to copy requests under the current cache revision keys.\n" - "Use --index-graphics-interfaces REVIEWED_DIRECTORY CACHE_DIRECTORY to index native graphics interfaces.\n" + "Use --index-graphics-interfaces REVIEWED_DIRECTORY CACHE_DIRECTORY [--jobs N] to index native graphics interfaces.\n" "Use --prepare-compute-pack REQUEST_DIRECTORY CACHE_DIRECTORY ENTRY_BYTE_OFFSET for captured compute.\n"; return 2; }