3 ms·
> I wonder if things like volatile and lock-free algorithms would behave any differently or even break. I don't understand how do you think it could change or
by chrisseaton 6y ago
> I wonder if things like volatile and lock-free algorithms would behave any differently or even break.
I don't understand how do you think it could change or break a volatile or lock-free algorithm?
It's a transparent optimisation - it doesn't change observable behaviour. Just like how register renaming, top-of-stack-caching, and so on and so on don't change observable behaviour.
The only difference it would make is timing.
- userbinator 6y agoSo there is logic to determine if a different thread has modified the value between those instructions?
- aclindsa 6y agoNo - there doesn't have to be. As long as there are no barriers or other instructions which restrict memory ordering relative to particular instructions in the dynamic instruction stream, it doesn't matter what the actual interleaving is between different cores as long as there is a given execution order which is valid for the observable side effects.
- cma 6y agoX64 has stronger guarantees than that without barriers doesn't it?
- PixelOfDeath 6y agox86 guarants that memory writes to different locations are seen in order by other cores. If you write memory first to address A and then to B. Another core never can see the B change without also seeing the A change at any moment in time. But in this case there is only a single memory location. So there is no ordering that could be violated in the first place.
- hansvm 6y agoYes, quite a bit stronger.
- chrisseaton 6y agoIn guarantees the order of visibility of writes to shared locations. It dose not guarantee the granularity of updates to the shared locations, which is the essential difference here. How do you observe the difference between the three instructions just naturally happening to all run before your next instruction completes, and them eliding the memory operation as is happening in this optimisation? It's like the ABA problem - how do you differentiate between just never happening to see a B out of chance, and B being elided instead? You can't.
- gpderetta 6y ago#StoreLoad still requires a barrier even on x86.
- chrisseaton 6y agoBefore you ask if there is logic to determine if another thread modified those values, first ask yourself if you can write a program where you can observe the difference if there was this logic or not. I can't write such a program. Can you? How would you ensure that another thread was scheduled between these instructions in order to observe some intermediate state? If I just told you the OS would never schedule between these instructions again is that breaking any contract or something you can observe somehow? How would you tell? If we can't such a program, then the logic isn't needed.
- dan-robertson 6y agoIf you have multiple cores then the OS’s scheduler doesn’t matter much. In one thread, do a loop of: loop: movq [rsi], 10 addq [rsi], 5 cmpq [rsi], 15 jeq loop (Or however you write that with correct assembler). In another thread with the same value in rsi, do: loop: movq [rsi], 100 jmp loop Ensure these threads run on different cores. You would expect the loop in the first thread to eventually terminate. The probability distribution of the time it takes to terminate would probably be informative. Would it terminate on this and chip? How would the distribution be different?
- byefruit 6y agoIf you are relying on that behaviour then your code is already broken on most modern x86 processors due to their memory ordering implementations. The store to the address at rsi will actually go in to the processor's store buffer and the subsequent add is allowed to retrieve the value from the store buffer _before_ it's drained to main memory. Under TSO, AMD's memory mirroring implementation here is totally fine. It could potentially change observable behaviour if you could turn it on or off but this is irrelevant given you wil see changes in observable behaviour for that same snippet of code depending on which microarchitecture of AMD or Intel processor you choose to run it on. It's worth having a read on the details for different memory models and how they relate to implements. This seems like a good guide: https://www.cs.utexas.edu/~bornholt/post/memory-models.html https://www.cs.utexas.edu/~bornholt/post/memory-models.html
- avianes 6y agoThe cache-coherency must already ensure that. Memory mirroring conditions should include the permission to read (or write, depending on usage) the data cache-line.