user2485710
user2485710

Reputation: 9801

Is it a good idea to change the optimization flags while building GCC from source?

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.

Upvotes: 3

Views: 1248

Answers (3)

D McKeon
D McKeon

Reputation: 133

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.

Upvotes: 2

rubenvb
rubenvb

Reputation: 76519

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.

Upvotes: 4

Hennes
Hennes

Reputation: 195

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.

Upvotes: 1

Related Questions