6 ms·
Optimizations aren't supposed to change the meaning of your code. And specifically for C, unsafe optimizations are supposed to only apply with -O3. Level 2 is s
by marcosdumay 5y ago
Optimizations aren't supposed to change the meaning of your code. And specifically for C, unsafe optimizations are supposed to only apply with -O3. Level 2 is supposed to be safe.
- foxfluff 5y ago> And specifically for C, unsafe optimizations are supposed to only apply with -O3. Level 2 is supposed to be safe. Citation needed? AIUI optimization levels are entirely up to the compiler. GCC's man page says nothing about the safety of these levels. > Optimizations aren't supposed to change the meaning of your code. And they generally won't. The trouble is when the meaning of your code wasn't well defined to begin with (or you thought it meant something it didn't).
- marcosdumay 5y ago> GCC's man page says nothing about the safety of these levels. Well, it used to.
- josephcsible 5y agoImagine you and the compiler are parties to a contract. Your obligation is to not let UB happen. The compiler's obligation is to not change the meaning of your code. Insisting that the compiler should uphold its end of the deal when you don't uphold yours is like saying if you sign a contract to buy widgets, you should have to pay even if the vendor fails to deliver them.
- marcosdumay 5y ago> Your obligation is to not let UB happen. Sorry, no can do. If that's the contract than C is a completely useless language.
- Filligree 5y ago> Sorry, no can do. If that's the contract than C is a completely useless language. That is the contract. Your inference may be correct, but it does no good to insist that C should behave differently than it does. There are better options.
- mpweiher 5y agoNope, that's not the contract.
- Filligree 5y agoI have always been told that, if a program contains UB, then the computer is justified in doing anything at all. This would naturally imply I am required to avoid any UB. Do you disagree with this?
- marcosdumay 5y agoThere's no contract. Nobody ever shared a piece of paper saying "your software must not use UB". The closest thing to a "contract" a language has is training material and reference books. And guess what, nearly all of C material mostly ignore UB, because enumerating it all is completely impractical even for reference books. Most of the C software in use on the wild was created before the hyper-optimization trend of C compilers, when your program did mostly what you said, and if what you said is undefined, it would still behave in some way that is close to a literal reading of the code. For most of the C software that exists, the modern treatment of UB is a huge post-facto unilateral contract change.
- plorkyeran 5y agoThe C specification is the contract between you and the compiler.
- mpweiher 5y agoYep. The creators of the standard spoke to this: "The three-fold definition of compliance is used to broaden the population of conforming programs and distinguish between conforming programs using a single implementation and portable conforming programs. A strictly conforming program is another term for a maximally portable program. The goal is to give the programmer a fighting chance to make powerful C programs that are also highly portable, without demeaning perfectly useful C programs that happen not to be portable. Thus the adverb strictly. By defining conforming implementations in terms of the programs they accept, the Standard leaves open the door for a broad class of extensions as part of a conforming implementation. By defining both conforming hosted and conforming freestanding implementations, the Standard recognizes the use of C to write such programs as operating systems and ROM-based applications, as well as more conventional hosted applications. Beyond this two-level scheme, no additional subsetting is defined for C, since the Committee felt strongly that too many levels dilutes the effectiveness of a standard. Conforming program is thus the most tolerant of all categories, since only one conforming implementation need accept a program to rule it conforming. The primary limitation on this license is §2.1.1.3. " It turns out it's not an all-or-nothing thing. Not being "strictly conforming" is not a "breach of the contract" by the developer, and thus license for the compiler to do anything they want. See also: "Existing code is important, existing implementations are not. A large body of C code exists of considerable commercial value. Every attempt has been made to ensure that the bulk of this code will be acceptable to any implementation conforming to the Standard. The Committee did not want to force most programmers to modify their C programs just to have them accepted by a conforming translator. " http://port70.net/~nsz/c/c89/rationale/a.html http://port70.net/~nsz/c/c89/rationale/a.html
- dooglius 5y agoI didn't sign a contract though, the compilers just do dumb things and I am at their mercy. If I were negotiating a contract then I wouldn't allow all the cases of UB.
- CRConrad 5y ago"I didn't sign a contract though, Linux just does dumb things and I am at its mercy." The license is still the closest thing you've got to a contract with the makers of Linux. Same with a language specification.
- deleted 5y ago[deleted]
- gpderetta 5y agoWriting a C compiler that does a trivial translation to ASM is not very hard, in fact there are already quite a few. I don't understand why people don't use that if that's what they want.
- josephcsible 5y agoIronically, it's because the code they generate is too slow.
- Pet_Ant 5y agoThis is the best expression of the idea I have heard! "Do what I mean!" "Well, if your code does UB then it doesn't mean anything" In fact, the behaviour of code is only limited to defined behaviours... if you stray... then it's undefined.
- dooglius 5y agoI recall reading recently that gcc moved to making overflow undefined at -O0
- klodolph 5y agoThis is definitely untrue. The difference between -O1, -O2, -O3, and -Os is as follows: - O1 includes optimizations that only require a small amount of additional compilation time. (Fast compilation, code is much faster than -O0.) - O2 includes O1, and additional optimizations that use a medium amount of compilation time. (Medium compilation, reasonably fast code.) - O3 includes O2, and additional optimizations that take more compilation time. (Slower compilation, faster, larger code. Unrolled loops and the like.) It often makes sense to compile hot inner loops with O3 because the compiler can do some fairly aggressive vectorization, if you know what you are doing... and the code remains correct in either case, if it was correct in the first place. It's basically "free" vectorization, when it works. - Os includes O2, except flags that increase code size. (Medium compilation, code is slightly slower and smaller than O2.) The only optimization level that actually changes the meaning of correct code is -Ofast, which is rarely used. It's described in the manual with the phrase "Disregard strict standards compliance". ALL levels of optimization will change the meaning of your code if your code relies on undefined behavior. For example, https://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html https://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html > -faggressive-loop-optimizations > This option tells the loop optimizer to use language constraints to derive bounds for the number of iterations of a loop. This assumes that loop code does not invoke undefined behavior by for example causing signed integer overflows or out-of-bound array accesses. The bounds for the number of iterations of a loop are used to guide loop unrolling and peeling and loop exit test optimizations. This option is enabled by default. It's always enabled.