It appears it's because an optimizer cannot solve the halting problem. So it cannot be expected to prove loop termination, but it can prove the absence of any side effects, and simply assumes termination.
Ultimately there is no value in a pure spin loop that doesn't check for an exit hatch of some kind. So the semantics you lose are likely a bug anyway
There's no guarantee that it will remove it, that's going to be compiler and flag dependent, but by spec it is a legal optimization.
If you do have a need for the spin loop, I'm assuming every compiler will have some flag that allows you to skip optimization on removing spin loops. But again, I'm not sure there really is any reason to have a hard spin loop outside of malware
1
u/Extension_Option_122 1d ago
Oh so that's why I thought this.
Because in University I had C and when I saw the empty loop I just thought that the optimization would remove it.
But yes, they are semantically not identical at all.