Skip to content

Build: hash the translator sources of the tree being built - #2789

Closed
Kalmarv wants to merge 1 commit into
xcl-dev:mainfrom
Kalmarv:fix/build-script-source-root
Closed

Kalmarv wants to merge 1 commit into
xcl-dev:mainfrom
Kalmarv:fix/build-script-source-root

Conversation

@Kalmarv

@Kalmarv Kalmarv commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

crates/xcl/build.rs hashes the translator sources into XCL_TRANSLATOR_SOURCE_ID, XCL_PER_LANE_SOURCE_ID and XCL_DIRECT_DXIL_SOURCE_ID, which key the translation cache. It took their root from env!("CARGO_MANIFEST_DIR"), the path of the tree that compiled the build script. Cargo names a path package's artifacts by its path relative to the workspace, so worktrees of the repo that share a CARGO_TARGET_DIR share one build-script binary. When the binary was compiled in tree A and then reran for tree B, it hashed A's sources: B's code got A's ids, and B could load A's cached translations. The script now reads CARGO_MANIFEST_DIR at run time, which Cargo sets to the package being built. The rerun-if-changed paths were already relative to the package. Found during frame-rate A/B runs, where two worktrees on one target dir shared a DXIL cache.

What is in it

  • Build: hash the translator sources of the tree being built: crates/xcl/build.rs reads CARGO_MANIFEST_DIR with std::env::var_os instead of env! (3 comment lines, 2 changed).

Tested

  • Repro, cargo check -p xcl --lib: two worktrees of main b49be18, where B adds one comment line to src/gpu/dxil/kernel.rs. A is checked first in a fresh shared target dir, then B; B's own target dir is the reference. XCL_DIRECT_DXIL_SOURCE_ID from the build script's output:

    without this change with it
    A, shared target dir f0d04875e42b84d5 f0d04875e42b84d5
    B after touching its gpu/dxil/kernel.rs, shared target dir (Cargo reruns the script) f0d04875e42b84d5 (A's) 869ae4235dcd0787
    B, its own target dir 869ae4235dcd0787 869ae4235dcd0787
  • Windows suite at ed13531, debug, cargo test --workspace --no-fail-fast -- --test-threads=4: 1,431 passed, 4 failed, 47 ignored, 25 SKIPPED notices (21 no wave64, 3 gpu: direct RDNA2 -> DXIL backend (no HLSL / DXC), behind XCL_DIRECT_DXIL #1426 refusals, 1 no heap windows). The three e2e failures were a missing xcl_testtitle.exe (no cargo build --workspace first); after the build all three passed. hle::d3d12x::cpu_writes::tests::writes_land_after_the_queue failed once with left: [7, 9] right: [7, 9] and passed 3 of 3 on rerun: it waits only for the first word to read 7 before comparing both, so the second word can still be in flight. This change does not touch it.

  • Windows 11 Pro 26200, RTX 5070 Ti (NVIDIA 610.74), rustc 1.99.0, DXC 1.9.2609.

Tested by others

  • Thanks to @ettehn (Build: hash the translator sources of the tree being built #2789 (comment)): Windows 11 26200, Rust 1.97.1 MSVC, each revision's build.rs compiled once with rustc and run with CARGO_MANIFEST_DIR pointing at a second tree, as when Cargo reuses the build-script binary. With this PR, a comment marker in recomp/emit.rs, recomp/ssa.rs or dxil/kernel.rs of the second tree changed only XCL_TRANSLATOR_SOURCE_ID, XCL_PER_LANE_SOURCE_ID or XCL_DIRECT_DXIL_SOURCE_ID respectively, and one in dxil/ops_tests.rs changed none; main's binary changed no id for any marker; run against the first tree, the PR's binary kept the first tree's ids. Not covered by that probe: whether Cargo reruns the script, GPU tests, a title run.

Regression run

  • Title smoke pair, 60 s each, RTX 5070 Ti, NVIDIA 610.74, release builds, beside other jobs on the same machine. Because this change gives every XCL_*_SOURCE_ID a new value, the PR's build starts with an empty translation cache, so one untimed 60 s run of the PR's build came first, then the usual warm-up run of main, then the pair. Frames 1,250 against 1,281 (98 %), peak commit 23,013 against 20,842 MB (110 %), no crash or device removal.

    main, b49be18:

    xcl-smoke v1
    commit: b49be1836844fe0b0cdb3211dce3cb7fb4e21ac7
    gpu: NVIDIA GeForce RTX 5070 Ti
    title: ea3b5e4d
    seconds: 60
    exit: 0x0
    frames shown: 1281 (PresentX 1282)
    presents per 2 s (last 3): 23 1 15
    device removals: 0
    crashes: 0
    peak commit: 20842 MB
    

    This PR, ed13531:

    xcl-smoke v1
    commit: ed13531a2869626a89d0b4b8755fa15c33b0d928
    gpu: NVIDIA GeForce RTX 5070 Ti
    title: ea3b5e4d
    seconds: 60
    exit: 0x0
    frames shown: 1250 (PresentX 1250)
    presents per 2 s (last 3): 16 37 4
    device removals: 0
    crashes: 0
    peak commit: 23013 MB
    
  • An earlier pair without the PR warm-up failed the commit check: 20,517 against 14,280 MB (144 %), frames 1,394 against 1,549. Its logs show the two sides took different paths, not a slower PR: main stayed on a static screen after about 20 s (no file reads, flat commit), while the PR side went on loading the world (4,688 DirectStorage requests in its last 2 s, 19,215 shader creations against 8,750). Compute pipeline time was similar (1,184 ms against 1,465 ms).

Not tested

  • What this does not fix: when B's files are all older than A's last build, Cargo reports B's xcl as Fresh and rebuilds nothing, so B's build is A's code and ids (seen in the same repro, before B was touched). That comes from Cargo's mtime check, which no build script can change; a separate CARGO_TARGET_DIR per worktree avoids both.
  • Linux and macOS builds.

AI use

  • Claude Code, Claude Opus 5.5: wrote the change, the repro, ran the tests and wrote this description.

🤖 Generated with Claude Code

build.rs took the source root from env!("CARGO_MANIFEST_DIR"), fixed when the
script is compiled. Worktrees that share a CARGO_TARGET_DIR share that binary
(cargo names it by the package's path relative to its workspace), so the
XCL_*_SOURCE_ID values hashed the first tree's sources, and a cached
translation of one tree could load in another. Read CARGO_MANIFEST_DIR at run
time instead.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ettehn

ettehn commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

Independent Windows check of all three source IDs on ed13531a2869626a89d0b4b8755fa15c33b0d928 against b49be1836844fe0b0cdb3211dce3cb7fb4e21ac7, Windows 11 build 26200, Rust 1.97.1 MSVC.

I compiled each revision’s build.rs once with rustc --edition=2021, then ran each executable with CARGO_MANIFEST_DIR pointing at the second tree to simulate reuse of the build-script executable. Both unmodified trees produced the same IDs. I added and removed a comment marker in one file at a time in the second tree.

Marker in second tree Old executable PR executable
recomp/emit.rs No ID changed Only XCL_TRANSLATOR_SOURCE_ID changed
recomp/ssa.rs No ID changed Only XCL_PER_LANE_SOURCE_ID changed
dxil/kernel.rs No ID changed Only XCL_DIRECT_DXIL_SOURCE_ID changed
dxil/ops_tests.rs No ID changed No ID changed

All four controls passed. The PR executable still produced the original IDs when invoked against the first tree while the markers were present in the second. This covers the build script’s source-root selection, all three hash families, and the test-file exclusion. Cargo’s decision to rerun the script, GPU tests, and retail smoke were outside this probe.

OpenAI Codex assisted with the probe and report. The exact model identifier was unavailable in this session.

@Kalmarv

Kalmarv commented Nov 26, 2026

Copy link
Copy Markdown
Contributor Author

@ettehn thank you. Running the build script against a second tree is a cleaner check than my Cargo repro, and it covers all three ids plus the test-file exclusion. It is under Tested by others now. Cargo rerunning the script is covered by the repro in Tested, and the title run by Regression run.

AI use: Claude Code, Claude Opus 5.5 wrote this reply.

@Prime1997 Prime1997 mentioned this pull request Nov 26, 2026
@GodlyBDS

Copy link
Copy Markdown

I found a concrete translated-cache identity mismatch in one archived retail replay built before this fix. The release build based on 082944135cd2b96288470a595861fa608eee08dc plus a local shader patch shared its Cargo target with other worktrees. Its build log records compilation from the intended checkout.

I recomputed the source IDs from the archived source revision and patch, then derived the translated-cache namespace using the recorded DXC toolchain and direct-compute mode. The archived executable and its cache growth show the two identities below.

HLSL source ID Direct DXIL source ID Translated-cache namespace
Expected from the archived source 51729fcdc367b260 2fefb4d6dc50c458 a385e401270fa087
Embedded in the executable and used at runtime 53dd401d9485a8ed 2fefb4d6dc50c458 2be2f94e3e3614c1

The runtime added 220 files to the second namespace and zero to the expected one. This establishes a mismatched cache identity in that replay. Individual shader-cache hits and rendering impact remain unresolved.

As a control, I checked a different historical pair that also used a shared Cargo target. The e877812 base and the same tree plus the b59d263 texture-placement change both embedded the expected HLSL, per-lane and direct DXIL IDs, and both used the expected namespace 37baea7df2f92a9b.

AI use — OpenAI Codex (GPT-6 family) audited preserved source patches, executables and cache metadata and prepared this report.

@Decodere

Copy link
Copy Markdown
Contributor

Independent Windows 11 / Cargo 1.100 & RTX 4060 (8 GB) release verification of PR #2789 at ed13531a2869626a89d0b4b8755fa15c33b0d928 (parent & merge-base origin/main b49be1836844fe0b0cdb3211dce3cb7fb4e21ac7). End-to-end Cargo shared-CARGO_TARGET_DIR two-worktree reproduction, targeted unit tests, and paired 60 s smoke only; not a full source review.

Setup

  • GPU: NVIDIA GeForce RTX 4060 (8 GB / 7956 MiB reported by D3D12), driver 32.0.16.2114 (NVIDIA 621.14, WDDM 3.2)
  • CPU / RAM / OS: Intel Core i5-11400F (6 cores / 12 threads), 32 GB RAM (31.9 GiB usable), Windows 11 Home 26200
  • Toolchain / caches: rustc 1.100.0 (0ef1bc386 2026-11-09) / cargo 1.100.0 MSVC, DXC 1.9.2609.5, release builds, quiet machine (zero concurrent builds/tests during timed runs)
  • Workload: Two detached Git worktrees (Tree A, Tree B) sharing one CARGO_TARGET_DIR, plus retail workload title: 29640639 via scripts/smoke.ps1 -Seconds 60 (XCL_NETWORK=0, default flags, no input)

End-to-end Cargo 1.100 shared-CARGO_TARGET_DIR two-worktree check (b49be183 vs ed13531a)

Both detached worktrees were created before compiling build.rs in Tree A (cargo check -vv --release -p xcl --lib --message-format=json, build-script-build fresh: False). One comment line was then appended in Tree B to each hashed source set (crates/xcl/src/gpu/dxbc.rs, crates/xcl/src/gpu/recomp/lower.rs, and crates/xcl/src/gpu/dxil/bitstream.rs) without touching build.rs, forcing Cargo to mark build-script-run Dirty while reusing Tree A's compiled build-script-build binary (fresh: True):

Revision & step build-script-build fresh Cargo status XCL_TRANSLATOR_SOURCE_ID XCL_PER_LANE_SOURCE_ID XCL_DIRECT_DXIL_SOURCE_ID
Base b49be183 — Tree A False Dirty (build.rs) 699c1853e347d9bb 39cdba116441e869 f0d04875e42b84d5
Base b49be183 — Tree B (edited) True Dirty (dxbc.rs) 699c1853e347d9bb (stale) 39cdba116441e869 (stale) f0d04875e42b84d5 (stale)
PR #2789 ed13531a — Tree A False Dirty (build.rs) 699c1853e347d9bb 39cdba116441e869 f0d04875e42b84d5
PR #2789 ed13531a — Tree B (edited) True Dirty (dxbc.rs) a053e0d91b599a57 ada8bcf9eadbcc95 456b729fc3a9dc5d
PR #2789 ed13531a — back to Tree A (older mtime) True Fresh (see note) a053e0d91b599a57 ada8bcf9eadbcc95 456b729fc3a9dc5d
  • Bug reproduced & fixed: On b49be183, reusing Tree A's build-script binary in Tree B kept all three XCL_*_SOURCE_ID values frozen at Tree A's hashes despite Tree B's edited translator sources. On ed13531a, Tree A keeps exact hash parity with b49be183, and reusing the same compiled binary in Tree B recomputes all three IDs from Tree B.
  • Observed Cargo shared-CARGO_TARGET_DIR fingerprint caveat (FYI, outside env! fix): Immediately switching back to untouched Tree A (whose file mtimes were older than Tree B's build) under the same CARGO_TARGET_DIR caused Cargo 1.100.0 to report Fresh xcl v0.1.0 on the shared build/xcl/f34613a8fd345a29 fingerprint, skip re-running build.rs, and replay Tree B's cached IDs into Tree A. A quick empirical check adding cargo:rerun-if-env-changed=CARGO_MANIFEST_DIR did not change this (Cargo checks its own inherited process environment rather than the per-crate CARGO_MANIFEST_DIR it injects into the build script). That remaining case is a Cargo-level shared-target-dir mtime/fingerprint limitation when switching back to an older-mtime worktree without triggering a rebuild, not a flaw in this PR's runtime CARGO_MANIFEST_DIR read.

Targeted release unit tests & build (ed13531a)

  • cargo test --release -p xcl --lib source_hash: 2 passed, 0 failed, 0 ignored (0.00 s, exit 0).
  • cargo build --release -p xcl --bin xcl: clean release build (exit 0).

Paired 60 s RTX 4060 smoke (b49be183 vs ed13531a)

Both runs held steady 60/2 s pacing with 0 device removals and 0 crashes (ed13531a telemetry: 6,175 MB peak / 6,114 MB end local VRAM, 4,650 MB end non-local VRAM).

Base origin/main (b49be183):

xcl-smoke v1
commit: b49be1836844fe0b0cdb3211dce3cb7fb4e21ac7
gpu: NVIDIA GeForce RTX 4060
title: 29640639
seconds: 60
exit: 0x0
frames shown: 1518 (PresentX 1518)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 14018 MB

PR #2789 head (ed13531a):

xcl-smoke v1
commit: ed13531a2869626a89d0b4b8755fa15c33b0d928
gpu: NVIDIA GeForce RTX 4060
title: 29640639
seconds: 60
exit: 0x0
frames shown: 1557 (PresentX 1557)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 13969 MB

Limits of coverage

  • Tested only on native Windows 11 with rustc 1.100.0 / cargo 1.100.0 MSVC and a single NVIDIA RTX 4060 (8 GB); no Linux/GNU toolchain or cross-compilation test on this host.
  • Full workspace test suite was not re-run (only source_hash, the live Cargo two-worktree repro, release build, and 60 s retail smoke).

AI use

Ran the Cargo two-worktree repro, source_hash unit tests, release build, and paired 60 s smoke runs with assistance from Google Antigravity (Gemini 4 Neon); all hashes, Cargo -vv statuses, test counts, and smoke blocks were extracted directly from cargo, xcl.log, and scripts/smoke.ps1 outputs.

@throny

throny commented Nov 26, 2026

Copy link
Copy Markdown
Collaborator

Two contributors checked independently and found that this PR makes the same change as #2411: #1607, group D130. They suggest keeping #2411. If you agree, closing this PR or joining the other one would save the maintainers a review. If the two differ in a way the checks missed, a note in #1607 helps.

This is a hint from the community triage in #1607, not a maintainer decision.

AI use: Claude Code (Claude Opus 5.5), posted by a script from the #1607 table.

@Kalmarv

Kalmarv commented Nov 26, 2026

Copy link
Copy Markdown
Contributor Author

Closing this in favour of #2411, which was opened first and makes the same change: build.rs reads CARGO_MANIFEST_DIR when it runs instead of env! at compile time (one line, the same for all three source ids). Thanks to @throny for the #1607 triage hint, and to @ettehn, @Decodere and @GodlyBDS for checking this one: the code under test is the same as #2411's, so those reproductions apply to it too.

AI use: Claude Code, Claude Opus 5.5 compared the two diffs and wrote this comment.

@Kalmarv Kalmarv closed this Nov 26, 2026
rootw0rm pushed a commit to rootw0rm/xcl that referenced this pull request Nov 26, 2026
…v#2705 (manifest-dir runtime read, FSPX precompile, workers_for_commit)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants