Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Optimization levels in LLVM and Clang

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.

like image 427
Gabriel Southern Avatar asked Apr 24 '12 02:04

Gabriel Southern


People also ask

Does LLVM optimize?

LLVM currently does not optimize well loads and stores of large aggregate types (i.e. structs and arrays). As an alternative, consider loading individual fields from memory. Aggregates that are smaller than the largest (performant) load or store instruction supported by the targeted hardware are well supported.

What is LLVM optimization?

DESCRIPTION. The opt command is the modular LLVM optimizer and analyzer. It takes LLVM source files as input, runs the specified optimizations or analyses on it, and then outputs the optimized file.

What is the difference between Clang and LLVM?

Here Clang is the frontend and LLVM is the backend. LLVM defines a common intermediate representation (IR) based on the single static assignment (SSA) form. This makes many optimizations to be easily performed on the IR.

Does Clang optimize better than GCC?

GCC consistently outperformance Clang on all optimization levels. 32 Bit Performance is on a bit lower side with respect to corresponding 64-bit compilers & optimization levels. This can be attributed to being able to utilize the RAM properly. The contrast between O0 & Other optimization levels is very visible.


2 Answers

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.

like image 121
osgx Avatar answered Sep 21 '22 20:09

osgx


It may have been a mistake in the Makefile that llvm-gcc did not recognize as an error but clang did.

like image 36
Dan Avatar answered Sep 18 '22 20:09

Dan