Skip to content

cpu: lower memory fences, PAUSE and cache hints - #626

Merged
R0Y00 merged 2 commits into
R0Y00:mainfrom
danielradosa:cpu/fences-cache-hints
Nov 22, 2026
Merged

R0Y00 merged 2 commits into
R0Y00:mainfrom
danielradosa:cpu/fences-cache-hints

Conversation

@danielradosa

@danielradosa danielradosa commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

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.

The shared lowering now accepts LFENCE, MFENCE, SFENCE and PAUSE, and the
cache hints PREFETCHT0, PREFETCHT1, PREFETCHT2, PREFETCHNTA, PREFETCH,
PREFETCHW, CLFLUSH, CLFLUSHOPT and CLWB, each through the MSVC intrinsic
that emits the same instruction. INTEGER_HEADER includes immintrin.h and
gains wrappers for the four PREFETCHh hints and for PREFETCH.

A fence has no effect one thread can observe, so cpu_aot_fences_hints
checks that each of 7 exports keeps the loads and stores around the
instruction, including prefetches of a null and an unmapped address,
against the native DLL and a reference model over 76 cases. A
segment-relative prefetch and the invalid LOCK LFENCE stay rejected. A
host that raises an illegal instruction in the native fixture exits 77.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 22, 2026 •

Copy link
Copy Markdown
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.

@legacy-code-archaeologist

Copy link
Copy Markdown
Contributor

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.

R0Y00 commented Nov 22, 2026

Copy link
Copy Markdown
Owner

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 Nov 22, 2026

Copy link
Copy Markdown
Owner

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

Copy link
Copy Markdown
Contributor

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).

@R0Y00
R0Y00 merged commit d9c34c8 into R0Y00:main Nov 22, 2026
5 checks passed
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.

3 participants