0

I see this thread, and I had the same question, but this one isn't really answered: GCC standard optimizations behavior

I'm trying to figure out exactly what flag is causing an incredible boost in performance, in O1. I first found out which flags are set, using g++ -O1 -Q --help=optimizers and then got each of the enabled ones and used them to compile with g++. But the output results were different (the binary itself was of difference sizes).

How do I handpick optimizations for g++ or is this not possible?

Community
  • 1
  • 1
Rahul Manne
  • 1,229
  • 10
  • 20
  • Why would you want to handpick them, rather than let the optimizer do its job, if it is giving you an incredible boost in performance? – Matt Jordan Apr 15 '16 at 20:50
  • 2
    Not all the optimizations included in -O* are available as stand-alone switches. – SergeyA Apr 15 '16 at 20:53
  • @MattJordan I want to find out what is causing the performance improvement. I strongly suspect that it's a result of the cache being used better, but I can't easily scale the problem, so the only other way to find out why is to find the optimization that affects the runtime the most, and see if it is causing the program to have better locality. If it does, then it is somewhat clear that the improvement is the result of it being bandwidth-bound. Otherwise, it's something else, but knowing which optimization caused it will help. – Rahul Manne Apr 15 '16 at 20:55
  • 1
    Look at the generated code? – Alan Stokes Apr 15 '16 at 21:55
  • @AlanStokes I was hoping to find an easier way... – Rahul Manne Apr 15 '16 at 21:59

1 Answers1

3

Not all optimizations have individual flags, so no combination of them will generate the same code as using -O1 or any other of the general optimization enabling options (-Os, -O2, etc...). Also I imagine that a lot of the specific optimization options are ignored when you use -O0 (the default) because they require passes that are skipped if optimization hasn't generally enabled.

To try to narrow down your performance increase you can try using -O1 and then selectively disabling optimizations. For example:

g++ -O1 -fno-peephole -fno-tree-cselim -fno-var-tracking ...

You still might not have better luck this way though. It might be multiple optimizations in combination are producing your performance increase. It could also be the result of optimizations not covered by any specific flag.

I also doubt that better cache locality resulted in your "incredible boost in performance". If so it was likely a coincidence, especially at -O1. Big performance increases usually come about because GCC was able eliminate a chunk of your code either because it didn't actually have any net effect, always resulted in the same value being computed or it invoked undefined behaviour.

Ross Ridge
  • 38,414
  • 7
  • 81
  • 112