Optimization levels in LLVM and Clang

Gabriel Southern picture Gabriel Southern · Apr 24, 2012 · Viewed 18.9k times · Source

I am working on a project that I had been compiling with LLVM 2.6 and the llvm-gcc front end. I'm trying to test compiling it with LLVM 3.1 and clang. When I did this I got the following error message about -O5 optimization level:

error: invalid value '5' in '-O5'

However, LLVM 2.6 and llvm-gcc have worked fine with the -O5 flag. I saw the following documentation about the Clang optimization levels:

-O0 -O1 -O2 -Os -O3 -O4
       Specify which optimization level to use.  -O0 means "no optimization": this level compiles the
       fastest and generates the most debuggable code.  -O2 is a moderate level of optimization which
       enables most optimizations.  -Os is like -O2 with extra optimizations to reduce code size.  -O3
       is like -O2, except that it enables optimizations that take longer to perform or that may
       generate larger code (in an attempt to make the program run faster).  On supported platforms, -O4
       enables link-time optimization; object files are stored in the LLVM bitcode file format and whole
       program optimization is done at link time. -O1 is somewhere between -O0 and -O2.

So I'm trying to figure out what the -O5 in the Makefile I'm working with was doing in the first place (I didn't write the Makefile). Is this something that changed and used to be used with LLVM? Or is it still a useful feature and I just need to activate it in some other way.

Also in case it's useful the command I'm running that is giving the error is basically:

/bin/clang -g -c -mcmodel=medium -fstrict-aliasing -Wstrict-aliasing -O5 -emit-llvm -fkeep-inline-functions -fno-stack-protector -c -o foo.bc foo.cpp

Also in case it matters I am running on a Linux (Ubuntu 10.04) x86_64 system.

Answer

osgx picture osgx · Apr 24, 2012

Gcc treats any -On for n >= 4 as -O3:

The -O flag will accept any number from 0-9 but only zero through three are implemented, and I don't think that anyone has any plans to ever implement four through nine any time soon. The fact that people use this -O9 idiom is a little disturbing, because it implies that they want every conceivable optimization that gcc could possibly ever offer, even if it made compilation take weeks for no gain.

Bigger levels should not be used, you has just low quality Makefile.

So specifying -O9 is a bit of "but this one goes to eleven!"-type nonsense

So, when gcc compiler is used, you have valid variants of -O: -O0, -O1, -O2, -O3. But driver will convert any -On to -O3 silently.

LLVM (both clang and llvm-gcc variants of driver) supports only levels up to -O4 (-O4 is just the same as -O3 -flto). So you should not to use -O4 without testing, because lto is slower and possibly can break your program.