cpu: lower memory fences, PAUSE and cache hints #620

Merged
danielradosa merged 2 commits from upstream-pr/626 into main 2026-11-22 23:23:28 +00:00
danielradosa commented 2026-11-22 16:20:58 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by danielradosa on 2026-11-22T16:20:58Z.


Change

The shared lowering in tools/ffx_version_query.py now accepts the memory-ordering fences lfence, mfence and sfence, pause, and the cache hints prefetcht0, prefetcht1, prefetcht2, prefetchnta, prefetch, prefetchw, clflush, clflushopt and clwb. Each one lowers to the MSVC intrinsic that emits the same instruction. INTEGER_HEADER now includes immintrin.h, for _mm_clflushopt and _mm_clwb, and gains small wrappers for the four _mm_prefetch hints and for the 3DNow! prefetch.

They add 13 x64 forms to the coverage chart. Open PR #609 also covers lfence and mfence, in its separate SSE2 leaf translator (tools/cpu_sse2_aot.py, as a compiler barrier). Here they go through the shared lowering that cpu_exe_aot.py and the ffx_* and bink_* translators reuse, and emit the instructions themselves. No open PR covers the other 11 forms; if #609 merges first, this PR adds 11 forms to the chart.

Verification

A fence has no effect a single thread can observe. cpu_aot_fences_hints (tests/cpu/fences_hints.asm, 7 exports) therefore loads and stores around each instruction, so a lowering that dropped or moved those accesses would change the result. It compares return values and the buffer against the native DLL and a reference model over 76 cases. The cases include prefetches of a null and an unmapped address, which never fault. Two negative controls stay rejected: a segment-relative prefetcht0 fs:[rcx] and the invalid lock lfence. A host that raises an illegal instruction in the native fixture (for clflushopt, clwb, prefetch or prefetchw) exits 77 instead of claiming the forms.

Windows 11 build 26300.9974, Ryzen 5 7600, MSVC 19.51.36256, Python 3.14.4, on main at 9b0df70:

ctest --preset dev --no-tests=error -R '^cpu_aot_fences_hints$' -V
ctest --preset release --no-tests=error -R '^cpu_aot_fences_hints$' -V
Build Result
dev 76 cases matched, 2 negative controls rejected
release (/O2) 76 cases matched, 2 negative controls rejected
all cpu_ tests, dev 113 of 115 passed; the 2 failures are cpu_native_archive_reader and cpu_native_archive_blast, which need the Zig-built archive_reader.exe (ziglang is not installed on this machine)

tools/form_coverage.py on a JUnit run of main plus this PR and the two companion PRs below lists all 13 forms as covered. x64 coverage goes from 551 (the published chart for 9b0df70) to 573 of 948 with all three.

Evidence and follow-up

The intrinsics also act as compiler barriers, so /O2 does not move the surrounding accesses. The test only shows this for one thread; it says nothing about ordering as another processor observes it.

Companion PRs from the same session, independent of this one and of each other: x87 ftst/fxam/ficom/ficomp/fnop/fnclex (#627), and cmpxchg8b/cmpxchg16b (#628).

AI-assisted.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6544739225 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/626). Originally posted by [danielradosa](https://github.com/danielradosa) on 2026-11-22T16:20:58Z. --- ## Change The shared lowering in `tools/ffx_version_query.py` now accepts the memory-ordering fences `lfence`, `mfence` and `sfence`, `pause`, and the cache hints `prefetcht0`, `prefetcht1`, `prefetcht2`, `prefetchnta`, `prefetch`, `prefetchw`, `clflush`, `clflushopt` and `clwb`. Each one lowers to the MSVC intrinsic that emits the same instruction. `INTEGER_HEADER` now includes `immintrin.h`, for `_mm_clflushopt` and `_mm_clwb`, and gains small wrappers for the four `_mm_prefetch` hints and for the 3DNow! `prefetch`. They add 13 x64 forms to the coverage chart. Open PR #609 also covers `lfence` and `mfence`, in its separate SSE2 leaf translator (`tools/cpu_sse2_aot.py`, as a compiler barrier). Here they go through the shared lowering that `cpu_exe_aot.py` and the `ffx_*` and `bink_*` translators reuse, and emit the instructions themselves. No open PR covers the other 11 forms; if #609 merges first, this PR adds 11 forms to the chart. ## Verification A fence has no effect a single thread can observe. `cpu_aot_fences_hints` (`tests/cpu/fences_hints.asm`, 7 exports) therefore loads and stores around each instruction, so a lowering that dropped or moved those accesses would change the result. It compares return values and the buffer against the native DLL and a reference model over 76 cases. The cases include prefetches of a null and an unmapped address, which never fault. Two negative controls stay rejected: a segment-relative `prefetcht0 fs:[rcx]` and the invalid `lock lfence`. A host that raises an illegal instruction in the native fixture (for `clflushopt`, `clwb`, `prefetch` or `prefetchw`) exits 77 instead of claiming the forms. Windows 11 build 26300.9974, Ryzen 5 7600, MSVC 19.51.36256, Python 3.14.4, on `main` at 9b0df70: ```powershell ctest --preset dev --no-tests=error -R '^cpu_aot_fences_hints$' -V ctest --preset release --no-tests=error -R '^cpu_aot_fences_hints$' -V ``` | Build | Result | |---|---| | `dev` | 76 cases matched, 2 negative controls rejected | | `release` (/O2) | 76 cases matched, 2 negative controls rejected | | all `cpu_` tests, `dev` | 113 of 115 passed; the 2 failures are `cpu_native_archive_reader` and `cpu_native_archive_blast`, which need the Zig-built `archive_reader.exe` (`ziglang` is not installed on this machine) | `tools/form_coverage.py` on a JUnit run of `main` plus this PR and the two companion PRs below lists all 13 forms as covered. x64 coverage goes from 551 (the published chart for 9b0df70) to 573 of 948 with all three. ## Evidence and follow-up The intrinsics also act as compiler barriers, so `/O2` does not move the surrounding accesses. The test only shows this for one thread; it says nothing about ordering as another processor observes it. Companion PRs from the same session, independent of this one and of each other: x87 `ftst`/`fxam`/`ficom`/`ficomp`/`fnop`/`fnclex` (#627), and `cmpxchg8b`/`cmpxchg16b` (#628). _AI-assisted._
upstream-sync closed this pull request 2026-11-24 06:44:01 +00:00
chatgpt-codex-connector[bot commented 2026-11-24 06:48:35 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by chatgpt-codex-connector[bot] on 2026-11-22T16:21:07Z.


Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-22T16:23:57.584439Z d4a31cf PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7082006708 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/626#issuecomment-7082006708). Originally posted by [chatgpt-codex-connector[bot]](https://github.com/chatgpt-codex-connector%5Bbot%5D) on 2026-11-22T16:21:07Z. --- <!-- codex-pull-request-review-summary --> <!-- codex-review-state:v1 {"code_review":{"commit_sha":"d4a31cffce93ecd0258ab7ddbcfb0b30d4fff246","has_any_findings":false,"has_unresolved_findings":false,"status":"completed"},"security_review":{"commit_sha":null,"has_any_findings":null,"has_unresolved_findings":null,"status":null}} --> <details> <summary><strong>Codex Review Summary</strong>: ✅ Completed</summary> This comment shows the latest Codex review activity on this pull request. | Review | Status | Commit | Review trigger | | --- | --- | --- | --- | | 📝 **Code Review** | ✅ **Completed** <relative-time datetime="2026-11-22T16:23:57.584439Z">2026-11-22T16:23:57.584439Z</relative-time> | `d4a31cf` | PR opened | <details> <summary>ℹ️ About Codex in GitHub</summary> <br/> [Your team has set up Codex to review pull requests in this repo](https://chatgpt.com/codex/cloud/settings/general). Reviews are triggered when you - Open a pull request for review - Mark a draft as ready - Comment "@codex review" or "@codex security review". Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. </details> </details>
legacy-code-archaeologist commented 2026-11-24 06:48:38 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by legacy-code-archaeologist on 2026-11-22T18:25:21Z.


Verification of d4a31cf on an Intel Core i7-6820HQ (Skylake client, Intel64 Family 6 Model 94), Windows 11 26100.9168, MSVC/ML64 19.52.37029, dev preset. It merges cleanly with main, and all 115 cpu_ tests pass.

On this host, though, the new test does not check anything beyond its two rejection controls:

ctest --preset dev --no-tests=error -R cpu_aot_fences_hints -V
rejected prefetcht0 fs:[rcx]: unsupported instruction prefix at 0x0: prefetcht0 fs:[rcx]
rejected lock lfence (invalid encoding): unsupported query operation at 0x0: (bad)
skip: this host raises an illegal instruction in clwb_store
1/1 Test #132: cpu_aot_fences_hints .............***Skipped   0.30 sec

Skylake client CPUs do not implement CLWB, so clwb_store raises #UD natively. The check then returns 77 for the whole test. As a result, the MFENCE/LFENCE/SFENCE, PAUSE and prefetch cases are never compared on any host without CLWB, which includes Intel client CPUs before Ice Lake.

A narrower gate would keep those cases running: check CPUID leaf 7, EBX bit 24 (CLWB) up front, skip only clwb_store, and run the remaining exports. Reporting the skipped export by name would also keep the capability skip visible.

AI-assisted: run and summarized with Claude Code; outputs copied from the local runs.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7083590270 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/626#issuecomment-7083590270). Originally posted by [legacy-code-archaeologist](https://github.com/legacy-code-archaeologist) on 2026-11-22T18:25:21Z. --- Verification of `d4a31cf` on an Intel Core i7-6820HQ (Skylake client, `Intel64 Family 6 Model 94`), Windows 11 26100.9168, MSVC/ML64 19.52.37029, `dev` preset. It merges cleanly with `main`, and all 115 `cpu_` tests pass. On this host, though, the new test does not check anything beyond its two rejection controls: ```text ctest --preset dev --no-tests=error -R cpu_aot_fences_hints -V rejected prefetcht0 fs:[rcx]: unsupported instruction prefix at 0x0: prefetcht0 fs:[rcx] rejected lock lfence (invalid encoding): unsupported query operation at 0x0: (bad) skip: this host raises an illegal instruction in clwb_store 1/1 Test #132: cpu_aot_fences_hints .............***Skipped 0.30 sec ``` Skylake client CPUs do not implement `CLWB`, so `clwb_store` raises #UD natively. The check then returns 77 for the **whole** test. As a result, the `MFENCE`/`LFENCE`/`SFENCE`, `PAUSE` and prefetch cases are never compared on any host without `CLWB`, which includes Intel client CPUs before Ice Lake. A narrower gate would keep those cases running: check CPUID leaf 7, EBX bit 24 (`CLWB`) up front, skip only `clwb_store`, and run the remaining exports. Reporting the skipped export by name would also keep the capability skip visible. AI-assisted: run and summarized with Claude Code; outputs copied from the local runs.
R0Y00 commented 2026-11-24 06:48:47 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-22T21:58:08Z.


The Skylake report above shows why the green job alone cannot confirm this fixture: missing CLWB makes the whole test exit 77 before it reaches PAUSE or completes the repetitions. Please keep the required fences/PAUSE cases running to completion and split optional CLWB/CLFLUSHOPT and vendor prefetch forms into separate capability-gated cases with named skip results. Continue treating an unexpected translated-side illegal instruction as a failure. That will make the Windows CI result interpretable across older and newer hosts.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7086449730 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/626#issuecomment-7086449730). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-22T21:58:08Z. --- The Skylake report above shows why the green job alone cannot confirm this fixture: missing CLWB makes the whole test exit 77 before it reaches PAUSE or completes the repetitions. Please keep the required fences/PAUSE cases running to completion and split optional CLWB/CLFLUSHOPT and vendor prefetch forms into separate capability-gated cases with named skip results. Continue treating an unexpected translated-side illegal instruction as a failure. That will make the Windows CI result interpretable across older and newer hosts.
R0Y00 commented 2026-11-24 06:48:50 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-22T23:02:13Z.


I split the cases in 089afa5. cpu_aot_fences_hints always runs the 34 baseline fence, PAUSE and standard prefetch cases and the two rejection controls. AMD prefetch, CLFLUSH, CLFLUSHOPT and CLWB each have a separate CTest. Local native/AOT checks passed all 76 cases at /Od and /O2. A synthetic illegal-instruction control confirmed that only an unavailable optional native case exits 77; the same error in a baseline or lifted case still fails. The two Windows CI jobs are running on the updated branch.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7087263304 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/626#issuecomment-7087263304). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-22T23:02:13Z. --- I split the cases in `089afa5`. `cpu_aot_fences_hints` always runs the 34 baseline fence, PAUSE and standard prefetch cases and the two rejection controls. AMD prefetch, CLFLUSH, CLFLUSHOPT and CLWB each have a separate CTest. Local native/AOT checks passed all 76 cases at `/Od` and `/O2`. A synthetic illegal-instruction control confirmed that only an unavailable optional native case exits 77; the same error in a baseline or lifted case still fails. The two Windows CI jobs are running on the updated branch.
legacy-code-archaeologist commented 2026-11-24 06:48:50 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by legacy-code-archaeologist on 2026-11-22T23:22:44Z.


Re-checked 089afa5 on the same Skylake client CPU (i7-6820HQ, no CLWB), which previously skipped the whole fixture. Windows 11 26100.9168, MSVC/ML64 19.52.37029, dev preset. It merges cleanly with main, and all 132 cpu_ tests pass.

ctest --preset dev --no-tests=error -R "fences|clwb|clflush|prefetch" -V
baseline: 34 cases matched native and translated DLLs; 2 negative controls rejected
1/5 Test #135: cpu_aot_fences_hints ................   Passed
prefetch_amd: 21 cases matched native and translated DLLs
2/5 Test #136: cpu_aot_fences_hints_prefetch_amd ...   Passed
clflush: 7 cases matched native and translated DLLs
3/5 Test #137: cpu_aot_fences_hints_clflush ........   Passed
clflushopt: 7 cases matched native and translated DLLs
4/5 Test #138: cpu_aot_fences_hints_clflushopt .....   Passed
skip: clwb is unavailable in the native fixture (clwb_store)
5/5 Test #139: cpu_aot_fences_hints_clwb ...........***Skipped

The split works as intended on this host. The required fence, PAUSE and standard prefetch cases now run to completion, the optional CLFLUSH, CLFLUSHOPT and AMD-prefetch cases each run on their own, and only CLWB is a named capability skip.

AI-assisted (Claude Code).

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7087528553 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/626#issuecomment-7087528553). Originally posted by [legacy-code-archaeologist](https://github.com/legacy-code-archaeologist) on 2026-11-22T23:22:44Z. --- Re-checked `089afa5` on the same Skylake client CPU (i7-6820HQ, no CLWB), which previously skipped the whole fixture. Windows 11 26100.9168, MSVC/ML64 19.52.37029, `dev` preset. It merges cleanly with `main`, and all 132 `cpu_` tests pass. ```text ctest --preset dev --no-tests=error -R "fences|clwb|clflush|prefetch" -V baseline: 34 cases matched native and translated DLLs; 2 negative controls rejected 1/5 Test #135: cpu_aot_fences_hints ................ Passed prefetch_amd: 21 cases matched native and translated DLLs 2/5 Test #136: cpu_aot_fences_hints_prefetch_amd ... Passed clflush: 7 cases matched native and translated DLLs 3/5 Test #137: cpu_aot_fences_hints_clflush ........ Passed clflushopt: 7 cases matched native and translated DLLs 4/5 Test #138: cpu_aot_fences_hints_clflushopt ..... Passed skip: clwb is unavailable in the native fixture (clwb_store) 5/5 Test #139: cpu_aot_fences_hints_clwb ...........***Skipped ``` The split works as intended on this host. The required fence, PAUSE and standard prefetch cases now run to completion, the optional CLFLUSH, CLFLUSHOPT and AMD-prefetch cases each run on their own, and only CLWB is a named capability skip. AI-assisted (Claude Code).
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
R0Y00/Xbox2Windows!620
No description provided.