I'd like to kick off a main memory access, but as long as I get the new value at some point, I'm not too fussy about exactly when the new value arrives. In the meantime, I'd like to continue using the value that's currently in the register, rather than stalling until the memory fetch completes.
Here is a motivating toy example in C. I have a worker loop that continually does some work, and accumulates the results into some storage. Every so often, I'd like to swap where the values are being accumulated to (analogous to log rotation on a Linux server). As long as each computed value gets accumulated to exactly one storage location, I'm happy.
As written, the cpu will stall while the memory fetch happens, because I haven't expressed that I want to use the pre-existing, "wrong/stale" value in the register until the memory fetch completes.
How can I make that clear to the compiler/cpu? It's fine if it requires directly writing assembly. I'm interested in answers that work on any/all architectures.
// does unspecified work and returns an int.
inline int do_work();
// where our main thread accumulates its work
int * accumulator;
// assume that some other thread is concurrently updating this.
// Every so often, we want to switch our accumulator to be a
// newer value taken from here, but we're not fussy about exactly
// when that switch happens.
int * volatile * current_accumulator;
void work_loop() {
int * accumulator = *current_accumulator;
// Here, I want to block once until `*current_accumulator`
// is loaded into a register.
while (1) {
for (int i = 0; i < 100; ++i) {
// This has a dependency on the memory load, and so
// it will stall until the new value is loaded into
// the register. However I don't want that. I want the
// register to update eventually, but I want to always
// be using the value that happens to currently be there.
*accumulator += do_work();
}
accumulator = *current_accumulator;
}
}