fix(hw-oracle): invalidate cache when compiler changes in place #12821

Merged
malekaifa merged 1 commit from upstream-pr/12821 into main 2026-11-02 16:13:47 +00:00
malekaifa commented 2026-10-29 11:21:04 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by malekaifa on 2026-10-29T11:21:04Z.


What

The hw-oracle build cache already keys the compiled HSA oracle by the source, compiler command, resolved compiler path, and ROCm root. If the compiler executable is replaced at the same resolved path (for example by an in-place toolchain/package upgrade), that key does not change and oracle() can silently reuse a binary built by the previous compiler.

Include inexpensive file identity for the selected compiler in the cache configuration: resolved path, device/inode, size, mtime and ctime. This catches the normal replacement/update cases without hashing a potentially very large compiler binary on every cache lookup.

The regression keeps CC, PATH, and the resolved compiler path unchanged, replaces the executable at that path, and verifies that the second call rebuilds instead of returning the first cached oracle.

Tested

  • Exact-main negative control on ba5b5330b37724bca4b99f30faab504653635a04: test_replacing_selected_compiler_invalidates_the_cache fails with b'first' != b'other', proving the old cache key reuses the stale build.
  • Windows 11, Python 3.12: tools/hw-oracle/test_hw_oracle.py — 49 tests OK, 5 expected platform skips.
  • Windows 11, Python 3.9: tools/hw-oracle/test_hw_oracle.py — 49 tests OK, 5 expected platform skips.
  • Windows 11, Python 3.12 and 3.9: tools/hw-oracle/test_compare_results.py — 12 tests OK on each.
  • WSL, Python 3.12.3 native filesystem: test_hw_oracle.py — 49 tests OK, 2 expected skips; test_compare_results.py — 12 tests OK.
  • Focused WSL regression plus the existing PATH-selection cache regression both pass after the fix.
  • py -3.12 tools/check_conventions.py --base ba5b5330b37724bca4b99f30faab504653635a04 — passed.
  • git diff --check — clean.
  • Python 3.12/3.9 py_compile — passed.
  • Synthetic merge-tree checks are conflict-free against open hw-oracle PRs #12815, #12348, and #12812. #12815 changes cache-directory selection only; #12348 does not touch oracle()/OracleConfigurationTests; #12812 changes oracle_hip.py and later HIP tests.
  • Final live overlap scan found no open PR or issue implementing same-path compiler replacement invalidation.

Exact head: b7bdfa8996abe65e60bd6d16d3aaa745d8e7b4ac.

Checklist

  • Based on current main; no other open PR implements the same behavior
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw; silent stubs are documented where applicable
  • No notes, investigation .md files or images added to the repository
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes (OpenAI Codex)
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5067059130 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/12821). Originally posted by [malekaifa](https://github.com/malekaifa) on 2026-10-29T11:21:04Z. --- ### What The hw-oracle build cache already keys the compiled HSA oracle by the source, compiler command, resolved compiler path, and ROCm root. If the compiler executable is replaced **at the same resolved path** (for example by an in-place toolchain/package upgrade), that key does not change and `oracle()` can silently reuse a binary built by the previous compiler. Include inexpensive file identity for the selected compiler in the cache configuration: resolved path, device/inode, size, mtime and ctime. This catches the normal replacement/update cases without hashing a potentially very large compiler binary on every cache lookup. The regression keeps `CC`, `PATH`, and the resolved compiler path unchanged, replaces the executable at that path, and verifies that the second call rebuilds instead of returning the first cached oracle. ### Tested - Exact-main negative control on `ba5b5330b37724bca4b99f30faab504653635a04`: `test_replacing_selected_compiler_invalidates_the_cache` fails with `b'first' != b'other'`, proving the old cache key reuses the stale build. - Windows 11, Python 3.12: `tools/hw-oracle/test_hw_oracle.py` — 49 tests OK, 5 expected platform skips. - Windows 11, Python 3.9: `tools/hw-oracle/test_hw_oracle.py` — 49 tests OK, 5 expected platform skips. - Windows 11, Python 3.12 and 3.9: `tools/hw-oracle/test_compare_results.py` — 12 tests OK on each. - WSL, Python 3.12.3 native filesystem: `test_hw_oracle.py` — 49 tests OK, 2 expected skips; `test_compare_results.py` — 12 tests OK. - Focused WSL regression plus the existing PATH-selection cache regression both pass after the fix. - `py -3.12 tools/check_conventions.py --base ba5b5330b37724bca4b99f30faab504653635a04` — passed. - `git diff --check` — clean. - Python 3.12/3.9 `py_compile` — passed. - Synthetic merge-tree checks are conflict-free against open hw-oracle PRs #12815, #12348, and #12812. #12815 changes cache-directory selection only; #12348 does not touch `oracle()`/`OracleConfigurationTests`; #12812 changes `oracle_hip.py` and later HIP tests. - Final live overlap scan found no open PR or issue implementing same-path compiler replacement invalidation. Exact head: `b7bdfa8996abe65e60bd6d16d3aaa745d8e7b4ac`. ### Checklist - [x] Based on current `main`; no other open PR implements the same behavior - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except technical debt - [x] Unimplemented paths throw; silent stubs are documented where applicable - [x] No notes, investigation `.md` files or images added to the repository - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes (OpenAI Codex)
copilot-pull-request-reviewer[bot] commented 2026-10-29 11:24:05 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by copilot-pull-request-reviewer[bot] on 2026-10-29T11:24:05Z.

Review: COMMENTED


No active review issues are recorded for the compiler-aware cache invalidation change.

No open issues 🎉

What changed in this PR

Updates the hardware-oracle build cache to detect compiler changes at the same resolved path, preventing reuse of binaries built with an outdated compiler.

Changes:

  • Adds compiler file identity and timestamps to the cache key without hashing the compiler binary.
  • Adds a regression test verifying that replacing the compiler triggers a rebuild while unchanged compiler calls reuse the cache.
File Description
tools/​hw-oracle/​test_hw_oracle.py Tests cache invalidation after replacing the selected compiler in place.
tools/​hw-oracle/​hw_oracle.py Includes compiler device, inode, size, and timestamps in the cache configuration.

Review effort: Balanced

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5696886388 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/12821#pullrequestreview-5696886388). Originally posted by [copilot-pull-request-reviewer[bot]](https://github.com/apps/copilot-pull-request-reviewer) on 2026-10-29T11:24:05Z. **Review:** COMMENTED --- <!-- ccr-overview-v2 --> ### 🟢 Approval recommended No active review issues are recorded for the compiler-aware cache invalidation change. **No open issues** 🎉 <details> <summary><strong>What changed in this PR</strong></summary> Updates the hardware-oracle build cache to detect compiler changes at the same resolved path, preventing reuse of binaries built with an outdated compiler. **Changes:** - Adds compiler file identity and timestamps to the cache key without hashing the compiler binary. - Adds a regression test verifying that replacing the compiler triggers a rebuild while unchanged compiler calls reuse the cache. | File | Description | | ---- | ----------- | | `tools/​hw-oracle/​test_hw_oracle.py` | Tests cache invalidation after replacing the selected compiler in place. | | `tools/​hw-oracle/​hw_oracle.py` | Includes compiler device, inode, size, and timestamps in the cache configuration. | </details> <sub>Review effort: Balanced</sub>
XairRrR commented 2026-10-29 11:26:19 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by XairRrR on 2026-10-29T11:26:19Z.

Review: APPROVED


Reviewed — approved

The cache key now includes the selected compiler's file identity, the new regression test fails on main and passes with the change, and the existing hw-oracle tests still pass.

What I ran

  • Head: b7bdfa8996abe65e60bd6d16d3aaa745d8e7b4ac
  • main: ba5b5330b37724bca4b99f30faab504653635a04
  • Merged tree (--no-ff merge of head into main): clean, no conflicts.
  • Machine: Arch Linux 7.2.11-zen, GCC 16.2.1, Python 3, Intel Core i9-14900KF. No AMD GPU, so the oracle itself was not run on hardware.
git diff --check origin/main...refs/review/12821
python3 tools/check_conventions.py --base origin/main
ctest --test-dir build -R '^hardware_oracle' --output-on-failure -j4
python3 tools/hw-oracle/test_hw_oracle.py -v
# negative control
git checkout origin/main -- tools/hw-oracle/hw_oracle.py
python3 tools/hw-oracle/test_hw_oracle.py OracleConfigurationTests
git checkout HEAD -- tools/hw-oracle/hw_oracle.py

The change is Python-only and the two ctest entries run the scripts directly, so no C++ build was needed.

Results

Check Result
Merge with main Clean
git diff --check Clean
check_conventions.py --base origin/main All conventions checks passed.
ctest hardware_oracle, hardware_oracle_compare Both passed (0.56 s and 0.63 s)
test_hw_oracle.py -v 49 tests, OK, 1 skipped (comgr from AMD's Windows driver)
Negative control New test fails on main's hw_oracle.py
ctest
1/2 Test #78: hardware_oracle ..................   Passed    0.56 sec
2/2 Test #79: hardware_oracle_compare ..........   Passed    0.63 sec

100% tests passed out of 2
Relevant test lines
test_path_selecting_another_compiler_invalidates_the_cache (__main__.OracleConfigurationTests...) ... ok
test_replacing_selected_compiler_invalidates_the_cache (__main__.OracleConfigurationTests...) ... ok
Ran 49 tests in 0.624s
OK (skipped=1)

Negative control

I restored only tools/hw-oracle/hw_oracle.py from main and kept the PR's test file. The new test then fails:

Output
FAIL: test_replacing_selected_compiler_invalidates_the_cache (__main__.OracleConfigurationTests.test_replacing_selected_compiler_invalidates_the_cache)
  File "tools/hw-oracle/test_hw_oracle.py", line 496, in test_replacing_selected_compiler_invalidates_the_cache
    self.assertEqual(second.read_bytes(), b"other")
AssertionError: b'first' != b'other'

Ran 7 tests in 0.197s
FAILED (failures=1)

This matches the result in the description. The test is a good test of the change. It rewrites the fixture in place with write_text, so the inode stays the same, and b"first" and b"other" are the same length, so the size stays the same too. Only the mtime (set explicitly with os.utime) and the ctime change, which means the test checks the timestamp fields specifically and not just a new inode.

Findings

Blocking

None.

Non-blocking

  • tools/hw-oracle/hw_oracle.py:64-70 only stats compiler[0]. When CC is a wrapper command such as CC="ccache gcc", the identity recorded is the wrapper's, so a compiler upgraded in place behind it still reuses the stale oracle. The plain cc/gcc/clang case works because a package upgrade also replaces the driver binary. This limitation already existed for the resolved path, and I don't think it needs to be fixed in this PR. If you want to cover it, you could stat every word of compiler that shutil.which resolves.
  • Including st_ctime_ns means some metadata-only events also trigger a rebuild, such as chmod, or creating or removing a hard link to the compiler. A rebuild is one compile of oracle.c, so this costs little, and it does catch a cp -p replacement that keeps the mtime. Each such event leaves an unused directory under the cache. Again, this behaviour already existed for other configuration changes.

Not verified

  • Building and running the HSA oracle against a real ROCm installation and AMD GPU. This host has neither, so I only exercised the cache-key logic through the fixture compiler.
  • The Windows results in the description: the new test is skipped on nt, and I only ran on Linux.
<!-- greyfinch-sync:boykopovar/AnyPS5:review:5696912821 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/12821#pullrequestreview-5696912821). Originally posted by [XairRrR](https://github.com/XairRrR) on 2026-10-29T11:26:19Z. **Review:** APPROVED --- # Reviewed — approved The cache key now includes the selected compiler's file identity, the new regression test fails on `main` and passes with the change, and the existing hw-oracle tests still pass. ## What I ran - Head: `b7bdfa8996abe65e60bd6d16d3aaa745d8e7b4ac` - `main`: `ba5b5330b37724bca4b99f30faab504653635a04` - Merged tree (`--no-ff` merge of head into `main`): clean, no conflicts. - Machine: Arch Linux 7.2.11-zen, GCC 16.2.1, Python 3, Intel Core i9-14900KF. No AMD GPU, so the oracle itself was not run on hardware. ```sh git diff --check origin/main...refs/review/12821 python3 tools/check_conventions.py --base origin/main ctest --test-dir build -R '^hardware_oracle' --output-on-failure -j4 python3 tools/hw-oracle/test_hw_oracle.py -v # negative control git checkout origin/main -- tools/hw-oracle/hw_oracle.py python3 tools/hw-oracle/test_hw_oracle.py OracleConfigurationTests git checkout HEAD -- tools/hw-oracle/hw_oracle.py ``` The change is Python-only and the two ctest entries run the scripts directly, so no C++ build was needed. ## Results | Check | Result | | --- | --- | | Merge with `main` | Clean | | `git diff --check` | Clean | | `check_conventions.py --base origin/main` | `All conventions checks passed.` | | ctest `hardware_oracle`, `hardware_oracle_compare` | Both passed (0.56 s and 0.63 s) | | `test_hw_oracle.py -v` | 49 tests, OK, 1 skipped (`comgr from AMD's Windows driver`) | | Negative control | New test fails on `main`'s `hw_oracle.py` | <details><summary>ctest</summary> ``` 1/2 Test #78: hardware_oracle .................. Passed 0.56 sec 2/2 Test #79: hardware_oracle_compare .......... Passed 0.63 sec 100% tests passed out of 2 ``` </details> <details><summary>Relevant test lines</summary> ``` test_path_selecting_another_compiler_invalidates_the_cache (__main__.OracleConfigurationTests...) ... ok test_replacing_selected_compiler_invalidates_the_cache (__main__.OracleConfigurationTests...) ... ok Ran 49 tests in 0.624s OK (skipped=1) ``` </details> ## Negative control I restored only `tools/hw-oracle/hw_oracle.py` from `main` and kept the PR's test file. The new test then fails: <details><summary>Output</summary> ``` FAIL: test_replacing_selected_compiler_invalidates_the_cache (__main__.OracleConfigurationTests.test_replacing_selected_compiler_invalidates_the_cache) File "tools/hw-oracle/test_hw_oracle.py", line 496, in test_replacing_selected_compiler_invalidates_the_cache self.assertEqual(second.read_bytes(), b"other") AssertionError: b'first' != b'other' Ran 7 tests in 0.197s FAILED (failures=1) ``` </details> This matches the result in the description. The test is a good test of the change. It rewrites the fixture in place with `write_text`, so the inode stays the same, and `b"first"` and `b"other"` are the same length, so the size stays the same too. Only the mtime (set explicitly with `os.utime`) and the ctime change, which means the test checks the timestamp fields specifically and not just a new inode. ## Findings ### Blocking None. ### Non-blocking - [`tools/hw-oracle/hw_oracle.py:64-70`](https://github.com/boykopovar/AnyPS5/blob/b7bdfa8996abe65e60bd6d16d3aaa745d8e7b4ac/tools/hw-oracle/hw_oracle.py#L64-L70) only stats `compiler[0]`. When `CC` is a wrapper command such as `CC="ccache gcc"`, the identity recorded is the wrapper's, so a compiler upgraded in place behind it still reuses the stale oracle. The plain `cc`/`gcc`/`clang` case works because a package upgrade also replaces the driver binary. This limitation already existed for the resolved path, and I don't think it needs to be fixed in this PR. If you want to cover it, you could stat every word of `compiler` that `shutil.which` resolves. - Including `st_ctime_ns` means some metadata-only events also trigger a rebuild, such as `chmod`, or creating or removing a hard link to the compiler. A rebuild is one compile of `oracle.c`, so this costs little, and it does catch a `cp -p` replacement that keeps the mtime. Each such event leaves an unused directory under the cache. Again, this behaviour already existed for other configuration changes. ## Not verified - Building and running the HSA oracle against a real ROCm installation and AMD GPU. This host has neither, so I only exercised the cache-key logic through the fixture compiler. - The Windows results in the description: the new test is skipped on `nt`, and I only ran on Linux.
rooklz commented 2026-10-29 13:10:43 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by rooklz on 2026-10-29T13:10:43Z.

Review: APPROVED


Reviewed — approved

Folding the selected compiler's file identity into the cache key fixes the stale-oracle reuse. The new regression fails on main and passes on this head, and I found no regressions on macOS.

What I ran

Head b7bdfa8996abe65e60bd6d16d3aaa745d8e7b4ac, current main 157b02c4f1, macOS arm64 (this adds a platform the body doesn't list; it only covers Windows and WSL).

  • tools/hw-oracle/test_hw_oracle.py on the head: OK (skipped=3) on Python 3.9.6, 3.13.13 and 3.15.1.
  • Negative control: the head's test file next to main's hw_oracle.py → test_replacing_selected_compiler_invalidates_the_cache FAILS with b'first' != b'other'. The other OracleConfigurationTests pass.
  • git merge-tree --write-tree against main is clean. Merging this head with #12815 and with #12812 is also clean.
  • git diff --check is clean. tools/check_conventions.py --base upstream/main passes. tools/pr_overlap.py --branch probed 1406 PRs: 3 share files and all merge cleanly, so no overlap.

Extra probes (outside the PR's test)

  • Atomic replacement with the same size and the same mtime (a new file with an equal-length body, os.utime set to the old timestamp, then os.replace over the selected path) → the second oracle() rebuilds (b'AAAAA' → b'BBBBB'). This is the package-manager or cp -p + rename case, and inode/ctime catch it even though size and mtime both match.
  • CC naming a symlink to the fixture compiler resolves to the target and builds as before.

Mutants

These are single-field removals from the selected tuple, run against OracleConfigurationTests:

mutant result
drop st_dev / st_ino / st_size / st_mtime_ns / st_ctime_ns (each alone) survives
second-resolution times int(st_mtime), int(st_ctime) survives
drop both times killed
path only (main) killed

The single-field survivors are expected: the fields are deliberately redundant, and an in-place rewrite always moves ctime. I don't think pinning each field is worth a fragile test. If you want a second guard, the atomic-rename row above is cheap. It would make ino/ctime the only signal and would keep a future "size + mtime only" simplification from silently passing. I checked that: with selected = (str(path), status.st_size, status.st_mtime_ns), OracleConfigurationTests still passes, but the atomic-rename probe returns the stale b'AAAAA'. Optional; it's not blocking.

AI-assisted: yes

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5698210125 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/12821#pullrequestreview-5698210125). Originally posted by [rooklz](https://github.com/rooklz) on 2026-10-29T13:10:43Z. **Review:** APPROVED --- # Reviewed — approved Folding the selected compiler's file identity into the cache key fixes the stale-oracle reuse. The new regression fails on `main` and passes on this head, and I found no regressions on macOS. ## What I ran Head `b7bdfa8996abe65e60bd6d16d3aaa745d8e7b4ac`, current `main` `157b02c4f1`, macOS arm64 (this adds a platform the body doesn't list; it only covers Windows and WSL). - `tools/hw-oracle/test_hw_oracle.py` on the head: OK (skipped=3) on Python 3.9.6, 3.13.13 and 3.15.1. - Negative control: the head's test file next to `main`'s `hw_oracle.py` → `test_replacing_selected_compiler_invalidates_the_cache` FAILS with `b'first' != b'other'`. The other `OracleConfigurationTests` pass. - `git merge-tree --write-tree` against `main` is clean. Merging this head with #12815 and with #12812 is also clean. - `git diff --check` is clean. `tools/check_conventions.py --base upstream/main` passes. `tools/pr_overlap.py --branch` probed 1406 PRs: 3 share files and all merge cleanly, so no overlap. ## Extra probes (outside the PR's test) - **Atomic replacement with the same size and the same mtime** (a new file with an equal-length body, `os.utime` set to the old timestamp, then `os.replace` over the selected path) → the second `oracle()` rebuilds (`b'AAAAA'` → `b'BBBBB'`). This is the package-manager or `cp -p` + rename case, and inode/ctime catch it even though size and mtime both match. - `CC` naming a symlink to the fixture compiler resolves to the target and builds as before. ## Mutants These are single-field removals from the `selected` tuple, run against `OracleConfigurationTests`: | mutant | result | |---|---| | drop st_dev / st_ino / st_size / st_mtime_ns / st_ctime_ns (each alone) | survives | | second-resolution times `int(st_mtime)`, `int(st_ctime)` | survives | | drop both times | killed | | path only (`main`) | killed | The single-field survivors are expected: the fields are deliberately redundant, and an in-place rewrite always moves ctime. I don't think pinning each field is worth a fragile test. If you want a second guard, the atomic-rename row above is cheap. It would make ino/ctime the only signal and would keep a future "size + mtime only" simplification from silently passing. I checked that: with `selected = (str(path), status.st_size, status.st_mtime_ns)`, `OracleConfigurationTests` still passes, but the atomic-rename probe returns the stale `b'AAAAA'`. Optional; it's not blocking. AI-assisted: yes
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5!12821
No description provided.