16

If I want to compile my project with -flto is it enough to have built gcc with --enable-gold or do I also need to build gold and replace ld with it? And do I need any other flags? Ie I'm doing this

gcc -flto one.c two.c
chew socks
  • 1,406
  • 2
  • 17
  • 37

1 Answers1

22

According to https://gcc.gnu.org/wiki/LinkTimeOptimization#Requirements,

Despite the "link time" name, LTO does not need to use any special linker features. The basic mechanism needed is the detection of GIMPLE sections inside object files. This is currently implemented in collect2. Therefore, LTO will work on any linker already supported by GCC.

Furthermore, the GCC documentation for -fuse-linker-plugin says:

This option is enabled by default when LTO support in GCC is enabled and GCC was configured for use with a linker supporting plugins (GNU ld 2.21 or newer or gold).

So you don't need gold at all, even if you want to use the special "linker plugin" feature to pick up optimization information from object files in library archives.


There are usage examples in the -flto documentation. Either

      gcc -o myprog -flto -O2 foo.c bar.c

or

      gcc -c -O2 -flto foo.c
      gcc -c -O2 -flto bar.c
      gcc -o myprog -flto -O2 foo.o bar.o

will work.


As of GCC 4.9, you don't even need -flto for linking:

The only important thing to keep in mind is that to enable link-time optimizations you need to use the GCC driver to perform the link-step. GCC then automatically performs link-time optimization if any of the objects involved were compiled with the -flto.

And as of GCC 5:

Contrary to earlier GCC releases, the optimization and target options passed on the link command line are ignored.

melpomene
  • 84,125
  • 8
  • 85
  • 148
  • 1
    I'm using ld 2.24 and it says that `-plugin` is an unrecognized option. Also, using your last example, if I leave `-flto` off of the last stage the md5sum of the resulting executable is the same as with `-flto`. – chew socks Jul 28 '15 at 22:44
  • Where are you getting `-plugin` from? – melpomene Jul 28 '15 at 22:45
  • @chewsocks Which version of gcc are you using? – melpomene Jul 28 '15 at 22:48
  • I thought it was necessary to have an `ld` which supported plugins, which I tested by doing `ld -plugin`. From reading more of the `gcc` docs it looks like if compilation was done with `-flto` and `gcc` is used for the link stage then link time optimization is done anyway. – chew socks Jul 28 '15 at 22:52
  • gcc version 6.0.0 20150529 (experimental) (GCC) – chew socks Jul 28 '15 at 22:52
  • @chewsocks Try `ld -plugin asdf`. The `-plugin` option requires an argument or you'll get an unhelpful error. And yes, linker plugins are only needed to support the case of LTO information embedded in library archives. Everything else just works regardless. – melpomene Jul 28 '15 at 22:56
  • @chewsocks As for the md5sum thing, it's possible that gcc enables the necessary LTO options automatically, if I'm reading https://gcc.gnu.org/gcc-5/changes.html#general right (in particular: "Contrary to earlier GCC releases, the optimization and target options passed on the link command line are ignored."). – melpomene Jul 28 '15 at 23:00
  • I wasn't aware of that change in, but in the document you linked earlier I found "The only important thing to keep in mind is that to enable link-time optimizations you need to use the GCC driver to perform the link-step. GCC then automatically performs link-time optimization if any of the objects involved were compiled with the -flto command-line option." Which I was just able to confirm by experiment. – chew socks Jul 28 '15 at 23:06
  • You are right about `ld`. `gold` is nicer and tells you that you need an argument after `-plugin`. – chew socks Jul 28 '15 at 23:08
  • Given what you found about the change after `gcc-5`, do you think adding `-fwhole-program` is unnecessary now? – chew socks Jul 28 '15 at 23:14
  • Yeah, the documentation says "This option should not be used in combination with `-flto`. Instead relying on a linker plugin should provide safer and more precise information." so I wouldn't specify `-fwhole-program` manually. – melpomene Jul 28 '15 at 23:16
  • It does say that in one place, but in another location it says "If the program does not require any symbols to be exported, it is possible to combine -flto and -fwhole-program to allow the interprocedural optimizers to use more aggressive assumptions which may lead to improved optimization opportunities. Use of -fwhole-program is not needed when linker plugin is active (see -fuse-linker-plugin)." – chew socks Jul 28 '15 at 23:20
  • It also appears that `-fuse-linker-plugin` is enabled by default if the linker can use it "This option is enabled by default when LTO support in GCC is enabled and GCC was configured for use with a linker supporting plugins (GNU ld 2.21 or newer or gold)." – chew socks Jul 28 '15 at 23:21