Store-to-load forwarding is a crucial performance optimization in modern CPUs that allows the processor to bypass the memory hierarchy when a load instruction reads data that was recently written by a store instruction. Instead of waiting for the store to write to cache/memory and then reading it back, the CPU forwards the data directly from the store buffer to the load.
What is Store-to-Load Forwarding?
In modern out-of-order processors:
Store instructions write their data to a store buffer (also called store queue) before eventually writing to the cache/memory
Load instructions check both the cache AND the store buffer for their data
When a load finds its data in the store buffer (from a previous store that hasn't yet written to cache), the data is forwarded directly from the store buffer to the load, bypassing the cache entirely.
How It Works
Here's the typical flow:
Store Execution: STORE [Address A], Value
The store writes its address and data to the store buffer
The actual write to cache happens later (stores are buffered)
Load Execution: LOAD R1, [Address A]
The load first checks the store buffer for any pending stores to Address A
If found, the data is forwarded directly
If not found, the load reads from cache/memory
When Is It Useful?
Store-to-load forwarding is particularly beneficial in several common scenarios:
1. Stack Operations
asm
PUSH RAX ; Store RAX to [RSP-8]
...
POP RBX ; Load from [RSP-8] to RBX
Without forwarding, the POP would have to wait for the PUSH to write to cache.
2. Local Variable Access
c
void function() {
int x = 42; // Store to stack
int y = x + 1; // Load from stack (same location)
}
3. Structure Updates
c
struct Point {
int x, y;
};
point.x = 10; // Store
int val = point.x; // Load - can be forwarded
4. Function Call Sequences
c
obj->method = new_method; // Store
obj->method(); // Load - needs the updated pointer
Performance Benefits
Latency Reduction
Without forwarding: ~4-5 cycles (store) + ~4-5 cycles (load from L1) = ~8-10 cycles
With forwarding: ~4-5 cycles total (overlapped execution)
Cache Bandwidth Savings
Reduces unnecessary cache writes and reads
Frees up cache ports for other operations
Memory Ordering Satisfaction
Maintains correct program semantics without stalling the pipeline
When Store-to-Load Forwarding Fails
Not all store-load pairs can be forwarded. Common failure cases include:
1. Size Mismatch
asm
MOV [addr], EAX ; Store 4 bytes
MOV RBX, [addr] ; Load 8 bytes - forwarding may fail
2. Alignment Issues
asm
MOV [addr], EAX ; Store to addr
MOV EBX, [addr+1] ; Load from addr+1 - partial overlap
3. Multiple Overlapping Stores
asm
MOV BYTE [addr], AL ; Store 1 byte
MOV BYTE [addr+1], AH ; Store 1 byte
MOV EBX, [addr] ; Load 4 bytes - needs data from multiple stores
When forwarding fails, the load must wait for the store(s) to commit to cache, causing a store-to-load forwarding stall (typically 10-15 cycles penalty).
Hardware Implementation
The store buffer typically includes:
Address CAM (Content Addressable Memory): For fast address matching
Data storage: Holds the store data
Byte enables: Tracks which bytes are valid
Age/ordering logic: Ensures correct ordering when multiple stores to the same address exist
Example: Matrix Transpose
c
// Without optimization
for (i = 0; i < N; i++) {
for (j = 0; j < N; j++) {
temp = matrix[i][j]; // Load
transposed[j][i] = temp; // Store
verify = transposed[j][i]; // Load - can be forwarded!
}
}
Best Practices for Developers
Align your data: Ensure stores and loads use the same size and alignment
Avoid partial accesses: Don't mix byte/word/dword accesses to the same location
Use appropriate data types: Match the natural size of your operations
Profile for forwarding stalls: Modern performance counters can detect forwarding failures
Summary
Store-to-load forwarding is a critical optimization that:
Reduces effective memory latency by 50-70% for dependent store-load pairs
Improves performance for stack operations, local variables, and recently-modified data
Requires careful programming to avoid forwarding failures due to size/alignment mismatches
It's especially important in code with frequent memory updates followed by reads, which is common in object-oriented programming, stack-heavy code, and data structure manipulation.