4 ms·
People keep saying "O3 has bugs," but that's not true. At least no more bugs than O2. It did and does more aggressively expose UB code, but that isn't why peopl
by nickelpro 10mo ago
People keep saying "O3 has bugs," but that's not true. At least no more bugs than O2. It did and does more aggressively expose UB code, but that isn't why people avoid O3.
You generally avoid O3 because it's slower. Slower to compile, and slower to run. Aggressively unrolling loops and larger inlining windows bloat code size to the degree it impacts icache.
The optimization levels aren't "how fast do you want to code to go", they're "how aggressive do you want the optimizer to be." The most aggressive optimizations are largely unproven and left in O3 until they are generally useful, at which point they move to O2.
- SubjectToChange 10mo agoMore aggressive optimization is necessarily going to be more error prone. In particular, the fact that -O3 is "the path less traveled" means that a higher number of latent bugs exist. That said, if code breaks under -O3, then either it needs to be fixed or a bug report needs to be filed.
- uecker 10mo agoI would say there is a fair share of cases where programmers were told it is UB when it actually was a compiler bug - or non-conformance.
- saagarjha 10mo agoThat share is a vanishingly small fraction of cases.
- uecker 10mo agoI am not sure. I saw quite a few of these bugs where programmers were told it is UB but it isn't. For example, people showed me extern void g(int x); int f(int a, int b) { g(b ? 42 : 43); return a / b; } as an example on how compilers exploit "time-travelling" UB to optimize code, but it is just a compiler bug that got fixed once I reported it: https://developercommunity.visualstudio.com/t/Invalid-optimization-in-CC/10337428?q=muecker https://developercommunity.visualstudio.com/t/Invalid-optimi... Other compilers have similar issues.
- nickelpro 10mo agoYou're an expert, you're overestimating the competence of the median programmer. That's a great bug you found, and of course it is a compiler bug, not UB. 99.9% of the bugs I've dealt with of this sort were just pointer aliasing. Or just use-after-free. Or just buffer overruns. The median programmer, especially in the good ol' days, wrote UB code about once every 6-10 hours.
- uecker 10mo agoSure. All I am saying is that there are still plenty of compiler bugs related to optimization, which is reason enough for me to recommend being careful with optimization in contexts where correctness is important.
- saagarjha 10mo agoI agree that compilers have issues and that you have clearly run into some of them. I disagree with whether they are are more common than writing UB.
- uecker 10mo agoOh, I didn't meant to imply that there are more common, just that they are common enough to be careful with optimizations.
- saagarjha 10mo agoSure, I guess? In my experience I turn on the optimizer mostly without fear because I know that if, in the rare case I need to track down an optimizer bug, it would look the same as my process for identifying any other sort of crazy bug and in this case it will at least have a straightforward resolution.