CLFLUSH vs CLFLUSHOPT vs CLWB: Three Ways to Ask the CPU to Flush a Cache Line

2026-07-23

When you write to memory, the store lands in the cache — not DRAM, not persistent memory, not the NIC's coherent buffer. If you need that write to actually escape the cache hierarchy, you need a flush. x86 gives you three instructions, and they are wildly different in cost and semantics.

CLFLUSH (1998, SSE2): The original. Evicts the target cache line from every level of the hierarchy on every core. It's strongly ordered — CLFLUSH serializes with all loads and stores to any address. In practice this behaves almost like an implicit fence: your out-of-order engine drains, then the flush proceeds. Cost: roughly 100-500 cycles per line, plus the pipeline stall.

CLFLUSHOPT (2016, Skylake): Same eviction behavior, but weakly ordered. CLFLUSHOPTs to different cache lines can pipeline. You still need an SFENCE at the end to guarantee ordering with subsequent stores, but flushing 64 lines no longer means 64 sequential 300-cycle stalls. Throughput improvement of 5-10x when flushing ranges.

CLWB (2016, Skylake-SP): Cache Line Write Back. Writes the dirty line to memory but keeps it in cache in the Exclusive state. This is the killer feature for persistent memory (Optane) and any workload that flushes-then-re-reads. Also weakly ordered, also needs SFENCE.

Concrete example — persistent memory log append:

Rule of thumb: Never write a CLFLUSH loop in new code. If you don't need the line again, use CLFLUSHOPT + SFENCE. If you might, use CLWB + SFENCE. The SFENCE is what makes the flush observable to subsequent stores — the flush instruction alone only guarantees the writeback started, not that it's globally visible.

The gotcha: CLWB on pre-Ice Lake parts was implemented as CLFLUSHOPT internally — the line got evicted anyway. Only from Ice Lake (2019) onward does CLWB actually preserve the cache state. Check CPUID before assuming you're getting the fast path.

Key Takeaway: CLFLUSH serializes and evicts, CLFLUSHOPT pipelines and evicts, CLWB pipelines and keeps the line hot — pick based on whether you'll re-read the data, and always pair the weakly-ordered variants with SFENCE.

All newsletters