Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's wrong with -O3 (optimization level 3)? [duplicate]

I noticed that in QT Creator the default optimization level for the release version is -O2. I was wondering: why not -O3? I've read here on Stack Overflow that it can be dangerous or "bug exposing", but what are those optimizations flag which are considered to be more riskful than helpful and why?

Optimization level 3 flags (on GCC):

  • -fgcse-after-reload
  • -finline-functions
  • -fipa-cp-clone
  • -fpredictive-commoning
  • -ftree-vectorize
  • -funswitch-loops
like image 366
nyarlathotep108 Avatar asked Mar 07 '16 10:03

nyarlathotep108


People also ask

What does O3 optimization do?

-O3 instructs the compiler to optimize for the performance of generated code and disregard the size of the generated code, which might result in an increased code size. It also degrades the debug experience compared to -O2 .

How do I stop a GCC warning?

If the value of y is always 1, 2 or 3, then x is always initialized, but GCC doesn't know this. To suppress the warning, you need to provide a default case with assert(0) or similar code. This option also warns when a non-volatile automatic variable might be changed by a call to longjmp .

How many levels of optimization are there?

An optimization level is chosen with the command line option -O LEVEL , where LEVEL is a number from 0 to 3. The effects of the different optimization levels are described below: -O0 or no -O option (default)


1 Answers

Aside from compiler bugs, this is probably a myth. It's the -Ofast option that's risky, because that one doesn't even guarantee that standards-complying programs will not break.

Just as a practical example, here's a quick search of libraries, in no particular order, inside the Android Open Source Project (AOSP)—which is probably enough of a "real production codebase"—that use -O3:

cblas, openssh, libmpeg2, libavc, lvvm: MCJIT, jpeg, zlib, lz4, regex-re2, libpng, libutf, (and more)

Other code in the AOSP simply tries to optimize for size (still, Android), so it explicitly uses -Os. But much of that code still uses all these libraries—for which performance is a bigger consideration than size. Notice, too, that correctness is probably a big issue especially for the likes of openssh, which is incidentally mentioned above.

Keep in mind that programmers always tend to get more suspicious of optimized code. When you're not set on writing standards-compliant code, and delve into the territory of undefined & unspecified behavior, there's theoretically nothing to prevent the compiler from generating different results between different configurations (such as optimization level).

So when you mostly do your work on one particular level (debug) you tend to take that as your basic point of reference, and then, when you switch, it's easy to simply blame the optimizer—which, as far as it's concerned, might do a whole lot to remain standards-compliant, unlike our own code.

like image 91
Yam Marcovic Avatar answered Oct 02 '22 15:10

Yam Marcovic