A question: Why is it the CPU architecture that is weakly ordered, if it's the compiler that is reordering the statements? Couldn't you have a compiler on a weakly ordered arch that preserved order, and a compiler on x86 for example that could reorder your statements?
Isn't it the language spec / compiler that is in charge of this, rather than the CPU? I'd like to know more about this.
Both the compiler and the CPU can reorder code, as long as the results don't change from the perspective of the code that is executing.
For example, a compiler could take the following code:
global_x = 1;
global_y = 2;
global_x = 3;
And change it into:
global_x = 3;
global_y = 2;
Compilers on all platforms reorder statements like this. You can prevent this by making your variables `volatile`, but 99% of the time when you write the word "volatile" in your code you're making a mistake.
This is not about the compiler reordering statements. This happens in hardware. If a core executes two store instructions, another core might see them in any order.
Isn't it the language spec / compiler that is in charge of this, rather than the CPU? I'd like to know more about this.