4 ms·
> No, if the compiler can reason that that there is no aliasing between pointers, there is no need to emit a load. Without strict aliasing? Then you destroyed
by pcwalton 11y ago
> No, if the compiler can reason that that there is no aliasing between pointers, there is no need to emit a load.
Without strict aliasing? Then you destroyed a bunch of optimizations that lower for loops to memset. 4x performance loss if AVX would have been used in memset.
See: http://blog.llvm.org/2011/05/what-every-c-programmer-should-know.html http://blog.llvm.org/2011/05/what-every-c-programmer-should-... ("Violating Type Rules")
> What I don't want is for the compiler to silently omit the load and all the rest of the code within that function on the theory that once undefined behavior has been encountered it no longer has any obligations, not even the obligation to inform the programmer that it has perversely optimized the function to nothingness.
No, you need that optimization too. After aggressive inlining, especially of templated code like the STL in C++, it's often the case that large swathes of code are never dynamically reached, and people want that dead code to be removed. However, the only—or at least the easiest—way the compiler can tell that this is the case is by observing that, for example, a null pointer would have to be dereferenced to get to that dead code. Therefore the compiler is doing you a favor by eliminating that obviously dead code.
Again, all of these optimizations do not stem from compiler authors being language lawyers for the fun of it. They stem from their customers filing bugs on real-world missed optimizations.
- nkurz 11y agoWithout strict aliasing? Generally, I think strict aliasing is a fine thing. It would have been nicer to have "restrict" be the default and "alias" being an optional specifier, but likely there was no way to do this without breaking too much existing code. Warnings are often nice, but I don't recall having any issues with compilers optimizing based on assumptions of lack of aliasing. especially of templated code like the STL in C++ I'm an opinionated C programmer, but have no opinion of how C++ should handle things. I'm only considering the impact on C. If the STL requires an unfriendly C++ compiler, I'm OK with that, but this doesn't affect my thinking about C compilers. However, the only—or at least the easiest—way the compiler can tell that this is the case is by observing that, for example, a null pointer would have to be dereferenced to get to that dead code. Or, for example, that a signed integer addition would have to overflow. Or a full-width shift. Or a variety of other things that have a might make sense in both the programmers mental model and on the processor on which the compiled program will be run. Therefore the compiler is doing you a favor by eliminating that obviously dead code. Do you really believe that the compiler is doing you a favor by silently removing a check for signed integer overflow that you explicitly included? If so, we have found (at least one of) the point(s) of our disagreement! If on the other hand you feel that removing security related error checking is an unfortunate negative consequence of a necessary optimization, perhaps we can find a way to mitigate the consequences?
- nkurz 11y agoHere's a timely example of the behavior I'm referring to: https://github.com/websnarf/bstrlib/issues/8 https://github.com/websnarf/bstrlib/issues/8 I'd be happy to concede that this is not good programming practice, but I do not accept that the compiler is doing us a favor by silently creating this security problem. I do appreciate that gcc at least offers a warning for some of these cases.