5 ms·
> even on that imaginary machine the compiler is allowed to perform "as if" transformations which change what happens Yeah, but beginner programmers won't be t
by zasdffaa 4y ago
> even on that imaginary machine the compiler is allowed to perform "as if" transformations which change what happens
Yeah, but beginner programmers won't be told that. I can imagine even quite advanced programmers may be amazed because many do well in industry without digging deeply into their tools (for better or worse, there's often no need).
A few years ago I discovered that CPUs equally don't do what the code tells them - they do out-of-order to a shocking degree, have store forwarding and shadow registers (code writes a value x to register r1 doesn't mean x gets written to r1) etc. etc. and my jaw was hanging open. Nobody knows anything until they find out.
- tialaramex 4y agoIt's much worse than that, the CPU is stuck in the real world which is obliged to obey Mother Nature's laws despite the fact - especially at that scale - we find them unintuitive. The abstract machine can have intuitive behaviour because we choose how it works, a real machine not so much. For example, humans behave as though "universal time" is a thing, even if they know intellectually that this isn't so. The CPU has no choice though, there is and can only be local time. Events definitely happen in order locally but with no larger coherency. So if you have two CPU cores, those aren't the same location and they necessarily don't experience the same time. When writing a program with concurrency (e.g. threads), your C compiler emits machine code which, based on the author's understanding of the CPU documentation, will perform the necessary dance if you expressed that's what you wanted so that it seems there's a larger coherent sense of time. This is called Sequential Consistency. If you forgot to express that properly or you get it wrong, the C standard says your program has "Undefined Behaviour" because the machine code emitted does something which makes sense locally to each CPU core but may not make any sense to you at all. In general humans cannot reason successfully about what their program does in this situation anyway, it's just a complete head fuck, so the C decision to mark this "Undefined behaviour", anything could happen, is actually a reasonable one.
- puffoflogic 4y agoThis is not a correct explanation of why there is not a global order of memory operations on modern CPUs. It has nothing at all to do with the passage of time on different cores. Nothing.
- zasdffaa 4y agoTrue, and yet isn't it a perfect example of plausible but wrong mental models, just as "but it's ?: so it gets translated to cmov" is plausible but wrong.
- tialaramex 4y agoI'm intrigued by how far off it is from literally impossible. How many orders of magnitude are we from the point where rather than cache semantics forbidding it for practical reasons, you just couldn't physically deliver global order. If it was practical people would do it because it makes it much easier to write programs. So, it's not practical today and yet your parent insists it's possible anyway and I wonder by how much.