Bug 97637 - Compiling with LTO causes a bigger binary
Summary: Compiling with LTO causes a bigger binary
Status: RESOLVED INVALID
Alias: None
Product: gcc
Classification: Unclassified
Component: lto (show other bugs)
Version: 10.2.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2020-10-30 07:34 UTC by Christer Solskogen
Modified: 2023-02-13 08:53 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2020-10-30 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Christer Solskogen 2020-10-30 07:34:02 UTC
I've been experimenting with amiberry (https://github.com/midwan/amiberry/tree/dev) a Amiga emulator to compile it with LTO. The emulator requires a ARM machine (Raspberry Pi will do)

The final binary are 0.4MB bigger when compiled with LTO. 
I've added "flto -march=native" to CFLAGS and "flto=$(shell nproc) $(CFLAGS) -fuse-linker-plugin -fuse-ld=gold -Wl,--sort-common" to LDFLAGS.

I've tried with both gold and ld.bfd.
Comment 1 Richard Biener 2020-10-30 08:34:41 UTC
You can try lowering the inliner budget via --param inline-unit-growth which
defaults to 40 (a 40% increase due to inlining).  Likely only LTO exposes
inlining opportunities and -O2 is not -Os. (just guessing you use -O2)

The absolute number is also uninteresting - what's the relative change?
Comment 2 Christer Solskogen 2020-10-30 09:13:20 UTC
You are correct. I've replaced Ofast with O2 (but it doesn't seem to matter that much) - with the default inline-unit-growth the binary gets 5% bigger.

With inline-unit-growth=20 the binary gets 5%~ smaller. So that helped!
Comment 3 Jakub Jelinek 2020-10-30 09:33:07 UTC
Note, the main objectives of -O2 as well as -Ofast are code speed, code size is only secondary (because making code much larger might make it also slower).
If you care primarily about code size, you should be using -Os.
Comment 4 Christer Solskogen 2020-10-30 10:03:47 UTC
Okay, so LTO together with O2/O3 or Ofast will not help code size that much. 
I was worried that something was wrong with how GCC was configured or the command line parameters I was using since the binary increased in size.
Comment 5 Jakub Jelinek 2020-10-30 10:06:31 UTC
Sometimes it may shrink the code a lot, it really depends on the code.  Just that the question whether a particular transformation will make code faster or not is the primary question to ask, unless compiling with -Os or unless a particular code is e.g. through PGO determined to be cold.