探究_mm_clevict内在函数及clevict0、clevict1指令的用途——兼析其与_mm_cldemote的差异
_mm_clevict Intrinsic and clevict0/clevict1 Instructions? As someone who’s worked with Intel’s low-level cache intrinsics, let’s break down exactly what these tools do, their practical uses, and how they differ from _mm_cldemote.
Core Function & Purpose
First, let’s recap the official definition from Intel’s docs:
void _mm_clevict (const void * ptr, int level)
#include <immintrin.h>
指令:clevict0 m8、clevict1 m8
CPUID标识:KNCNI
This intrinsic (backed by the clevict0/clevict1 instructions) lets you explicitly evict the cache line containing the address ptr points to from a specific cache level:
- When
level = 0: Evicts the target cache line from the L1 cache entirely - When
level = 1: Evicts the target cache line from the L2 cache entirely
Practical Use Cases
Here are the scenarios where you’d reach for this intrinsic:
- Optimize High-Throughput Workloads: If you’re processing one-time-use data (like network packets or batch data transformations), manually evicting stale cache lines prevents L1/L2 from getting clogged with useless data. This keeps frequently accessed critical data in the fastest caches, avoiding costly cache misses.
- Trim Cache Footprint for Memory-Intensive Apps: For applications working with large datasets, selectively evicting non-critical lines keeps your working set small enough to fit in lower-level caches. This avoids repeated slow fetches from main memory.
- Security Hardening: In security-sensitive contexts, evicting sensitive data from cache can help mitigate side-channel attacks that exploit cache state to leak information. This is a niche use case but critical for specialized security software.
Key Differences from _mm_cldemote
These two intrinsics are often confused, but they serve distinct purposes:
_mm_cldemotedemotes a cache line to a lower-level cache (e.g., L1 → L2, or L2 → L3) instead of evicting it. The data stays in the cache hierarchy—it just moves to a slower, larger cache to free up space in the faster, smaller one. Use this when you might need the data later but don’t need it in L1/L2 right now._mm_clevictremoves the cache line from the specified level entirely. If the line existed in lower caches (like L3), it might remain there, but the targeted L1/L2 cache no longer holds it. Use this when you’re 100% sure you won’t need that data again soon.
To put it plainly:
- Use
_mm_clevictto fully free up L1/L2 space for new, immediately needed data. - Use
_mm_cldemoteto make space in L1/L2 while keeping data accessible in lower caches as a fallback.
内容的提问来源于stack exchange,提问作者Alex Guteniev

