3

Considering that I'm building GCC from source + mpfr, gmp, mpc, libelf and binutils, is it a good idea to change the optimization flags like so

CFLAGS="-O3" CXXFLAGS="-O3" ./configure

while configuring GCC or any of the other software?

I have a c2duo.

EDIT: I'm worried about the fact that these flags could change the behaviour of those programs/libs.

Jens
  • 69,818
  • 15
  • 125
  • 179
user2485710
  • 9,451
  • 13
  • 58
  • 102
  • 1
    In some cases `-O3` can make the code slower. Typically `-O2` is recommended. – jordanm Jun 14 '13 at 22:34
  • @jordanm gcc running slower when compiled with `-O3` ? How ? – user2485710 Jun 14 '13 at 22:41
  • 1
    @jordanm just so we are clear on this, I'm trying to build gcc from the source here, I'm not building a random c++ program with g++ . So this is true even for GCC itself ? – user2485710 Jun 14 '13 at 23:36
  • 2
    `-O3` makes inlining much more agressive, which bloats the code. More cache misses, more code to read from disk, ... Mostly much slower. The Linux kernel was compiled with `-Os` for a while, as that turned out faster... – vonbrand Jun 15 '13 at 15:24
  • What are you hoping to achieve? If you're worried about changing the behaviour then don't do it, problem solved :) – Jonathan Wakely Jun 16 '13 at 18:01
  • @JonathanWakely a shorter compilation time or a faster gcc, pick one :) – user2485710 Jun 16 '13 at 18:02
  • Compilation time of gcc itself? You won't get that by increasing optimisation, compiling with `-O3` almost always takes longer than compiling with `-O2`, but since you usually only compile it once the time it takes doesn't matter much. For a faster GCC, as the answers below say, if using `-O3` reliably made GCC faster it would be the default. A profiled bootstrap (i.e. using profile-guided optimisations) and/or LTO is a much better way to get a faster GCC. – Jonathan Wakely Jun 16 '13 at 18:05

3 Answers3

4

There are two things which you can do:

  1. If you're natively bootstrapping, you can do a full, and complete profile guided bootstrap. This will build the compiler and dependencies with a bootstrapped compiler providing the third round with the profile information it can use to optimize itself. After configure, do make profiledbootstrap. Note you can place the dependencies and stuff likke binutils and gdb inside the gcc source tree, and they should be built as well in the process.

  2. If you don't want to go through a long profiled bootstrap process, set CFLAGS, CXXFLAGS, CFLAGS_FOR_TARGET, CXXFLAGS_FOR_TARGET, etc. at GCC configure time to:

    -O2 -flto -march=core2
    

    And set LDFLAGS and LDFLAGS_FOR_TARGET to

    -flto
    

This will optimize the most and safest stuff.

Note that all this trouble will probably only result in a tiny speedup in the final executable.

rubenvb
  • 74,642
  • 33
  • 187
  • 332
  • I don't get the thing about including the source for mpfr, gmp, mpc and binutils ( maybe libelf too ? ). This is the content of my gcc tarball http://pastebin.com/Ry6TBMLz I'm supposed to put every extra folder in that root or in the gcc sub-folder ? – user2485710 Jun 16 '13 at 18:08
  • if you extract say to `gcc-4.8.1`, you should be able to add a `gcc-4.8.1/gmp` etc. and have the GCC build system also build GMP etc. It's not entirely bulletproof, but it might just still work on Linux. – rubenvb Jun 16 '13 at 18:11
  • ok, but this requires the bootstrap or if I just type `make` after `./configure` it will work out of the box ? what about libelf ? – user2485710 Jun 16 '13 at 18:13
  • [This line in GCC's configure](https://github.com/mirrors/gcc/blob/master/configure#L2749) tells you what subdirectories' libraries can be built in-tree. `libelf` is part of that. Just doing a `make` after `configure` should work. You might need `--with-gmp --with-mpfr --with-mpc --with-isl --with-cloog --with-libelf` to avoid using your system's libraries for those – rubenvb Jun 16 '13 at 18:19
  • I tried to recompile GCC, it works while using the system where you add the source of the used libraries right in the gcc root, but it doesn't work for the _host_tools_ , for example I tried to add a `binutils` directory with the relate source in it but this doesn't seem to work, it's normal ? how I'm supposed to add a local binutils installation ? I want to provide a different binutils installation from the globally available one. – user2485710 Jun 18 '13 at 20:10
  • @user2485710 I've never got that figured out. Maybe by configuring the binutils `--with-sysroot=/usr` and `--prefix=/your/binutils/install/prefix`, and add `your/binutils/install/prefix/bin` to `PATH` before building GCC. Although [it should still work](http://stackoverflow.com/a/6228588/256138)... – rubenvb Jun 19 '13 at 07:21
2

Measure first, then optimize. In this case, I would first try compiling gcc with the stock compiler and whatever the default config settings are (step 0).

Once I was sure then compile completed cleanly, I would do

make distclean 

or similar, and then measure the time to it took to compile with that stock current compiler. (step 1).

Next, install the new gcc and measure the time the new gcc (and any other tools), compiled with default configs, take to compile themselves. (step 2)

Then, compile with whatever -O optmization levels or other non-default settings you prefer. Once you are getting a clean compile, do 'make distclean' and measure again how long it takes the new default-settings gcc to compile itself with non-default settings (step 3).

Now, you have a -O3 (or whatever) gcc that you can use to compile itself, (step 4) measured in the same way as the other steps.

Finally, compare the timings (being sure you have started from the same base state before each compile). The parts you really care about are step 2 and step 4, but 1 and 3 may also be informative.

Note that this really measures only how fast gcc (or whatever compiler) can compile itself, and if you write code that is very different from that, your mileage may vary - but you can use the same technique to measure and compare the speeds of the various optimization levels when you compile whatever code you compile frequently.

Audrius Meškauskas
  • 20,936
  • 12
  • 75
  • 93
D McKeon
  • 135
  • 6
  • Okay - what are your objectives? Or, trade-offs - what things do you want, and what things are you willing to give up to get them? Time to compile, time to run, size of compiler executables, size of program executables, swappiness, etc.? – D McKeon Jun 15 '13 at 17:10
  • 1
    @AudriusMeškauskas, but since the code being compiled in the answer above is GCC itself, the compilation speed indicates how well the optimized code is performing. – Jonathan Wakely Jun 16 '13 at 18:03
1

EDIT: I'm worried about the fact that this flags could change the behaviour of those programs/libs.

It should not, but sometimes does change/corrupt the behaviour.
If you want to be sure, stick with the well tested default settings.

Note that if other values lile -O1 or -O3 yielded dramatically better performance then those settings would have become standard and would have been tested by now.

Hennes
  • 195
  • 2
  • 8