Gentoo Websites Logo
Go to: Gentoo Home Documentation Forums Lists Bugs Planet Store Wiki Get Gentoo!
Bug 685634 - {sys-devel/gcc,dev-lang/gnat-gpl}: add a USE for building GCC with LTO
Summary: {sys-devel/gcc,dev-lang/gnat-gpl}: add a USE for building GCC with LTO
Status: RESOLVED FIXED
Alias: None
Product: Gentoo Linux
Classification: Unclassified
Component: Current packages (show other bugs)
Hardware: All Linux
: Normal enhancement (vote)
Assignee: Gentoo Toolchain Maintainers
URL:
Whiteboard:
Keywords: PATCH
Depends on:
Blocks: lto
  Show dependency tree
 
Reported: 2019-05-11 15:53 UTC by Shane Peelar
Modified: 2024-03-11 22:47 UTC (History)
3 users (show)

See Also:
Package list:
Runtime testing required: ---


Attachments
Patch to enable building GCC itself with LTO (0001-sys-devel-gcc-dev-lang-gnat-gpl-add-a-USE-for-buildi.patch,2.88 KB, patch)
2019-05-11 15:53 UTC, Shane Peelar
Details | Diff
build.log using new ebuild (partial log) (build.log.bz2,122.66 KB, application/x-bzip)
2019-05-12 14:08 UTC, Shane Peelar
Details
Updated patch to build GCC with LTO (0001-sys-devel-gcc-build-lto-lean.patch,2.73 KB, patch)
2019-05-13 14:15 UTC, Shane Peelar
Details | Diff
Updated patch: bootstrap-lto-lean, >=gcc-9.1, no gnat-gpl (0001-sys-devel-gcc-build-lto-lean-v2.patch,2.04 KB, patch)
2019-05-13 14:50 UTC, Shane Peelar
Details | Diff
bootstrap-lto, >=gcc-9.1, no gnat-gpl (0001-sys-devel-gcc-build-lto.patch,2.03 KB, patch)
2019-05-13 21:27 UTC, Shane Peelar
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Shane Peelar 2019-05-11 15:53:45 UTC
Created attachment 576036 [details, diff]
Patch to enable building GCC itself with LTO

GCC as of version 4.4 supports building itself with Link Time Optimizations enabled. Since the infrastructure was already present in toolchain.eclass, I simply modified it to expose the functionality via the lto USE flag. A check is present to ensure that GCC is new enough to enable this functionality.

Ping @gentoo/toolchain

Package-Manager: Portage-2.3.66, Repoman-2.3.12
Comment 1 Sergei Trofimovich (RETIRED) gentoo-dev 2019-05-11 22:06:23 UTC
> confgcc+=( --with-build-config=bootstrap-lto )

toolchain.eclass uses `GCC_MAKE_TARGET` to specify build config, namely:

        # default target
        if is_crosscompile || tc-is-cross-compiler ; then
                # 3 stage bootstrapping doesnt quite work when you cant run the
                # resulting binaries natively ^^;
                GCC_MAKE_TARGET=${GCC_MAKE_TARGET-all}
        else
                if tc_version_is_at_least 3.3 && use_if_iuse pgo; then
                        GCC_MAKE_TARGET=${GCC_MAKE_TARGET-profiledbootstrap}
                else
                        GCC_MAKE_TARGET=${GCC_MAKE_TARGET-bootstrap-lean}
                fi
        fi

It is equivalent to --with-build-config=.

You might need to expand it to add `bootstrap-lto-lean` and do something about `profiledbootstrap` (USE="lto pgo"). I don't immediately see the equivalent in https://gcc.gnu.org/install/build.html
Comment 2 Shane Peelar 2019-05-12 14:05:11 UTC
Are you sure?  Using this patch, I get the following output in my compile log:

~~~
/tmp/portage/sys-devel/gcc-9.1.0/work/build/./prev-gcc/xg++ ... -fprofile-generate -flto=jobserver ...
~~~

It seems that the build config and the make targets may be be slightly different from one another.  I'm unsure exactly how the machinery works, but the documentation seems to suggest this as well:

https://gcc.gnu.org/install/build.html

See section:

~~~
...
‘bootstrap-lto-lean’

    This option is similar to bootstrap-lto, but is intended for faster build by only using LTO in the final bootstrap stage. With ‘make profiledbootstrap’ the LTO frontend is trained only on generator files.

...
~~~

Which seems to suggest that `--with-build-config=bootstrap-lto[-lean]` and `make profiledbootstrap` can be used at the same time.

Indeed, I'm getting both LTO and `-fprofile-generate` and `-fprofile-use` in my build log.  I will attach a partial log here in case it's useful.
Comment 3 Shane Peelar 2019-05-12 14:08:09 UTC
Created attachment 576182 [details]
build.log using new ebuild (partial log)
Comment 4 Shane Peelar 2019-05-12 14:31:56 UTC
Also, this is an interesting mailing list thread:

https://www.mail-archive.com/gcc-patches@gcc.gnu.org/msg210066.html

Quote:

> >>>>>> Please mention in both bootstrap-lto-lean.mk and the documentation
> >>>>>> that the intended make target for this config is profiledbootstrap
> >>>>>> since for non-profiledbootstrap it ends up not using LTO at all.  A 
> >>>>>> "lean"
> >>>>>> mode for non-profiledbootstrap would need to set up things to
> >>>>>> use LTO only for stage3 which means not doing a bootstrap comparison
> >>>>>> which means we could "skip" stage2 as well here.
Comment 5 Sergei Trofimovich (RETIRED) gentoo-dev 2019-05-12 19:08:48 UTC
Interesting. Yeah, bootstrap/final distinction makes sense.

I suggest enabling IUSE=lto only for gcc-9.1 for now and extend it to older versions when gcc-9.1 gets stable. That way we'll avoid gcc rebuild for majority of stable users.
Comment 6 Shane Peelar 2019-05-13 14:15:25 UTC
Created attachment 576458 [details, diff]
Updated patch to build GCC with LTO

I agree -- also, it seems like using bootstrap-lto-lean is the way to go, as there aren't any benefits in using LTO in the earlier bootstrapping stages.  I've amended the patch to only apply to GCC 9.1 and use bootstrap-lto-lean as a result.
Comment 7 Shane Peelar 2019-05-13 14:26:39 UTC
OK -- there seems to be a minor hiccup with applying LTO only to GCC 9.1 and above, which is that dev-lang/gnat-gpl doesn't use a high enough version of GCC to ever actually have IUSE=lto.  This seems to make repoman unhappy.  What's the best course of action to avoid breakage here?
Comment 8 Shane Peelar 2019-05-13 14:50:52 UTC
Created attachment 576466 [details, diff]
Updated patch: bootstrap-lto-lean, >=gcc-9.1, no gnat-gpl

Found the problem -- it was that I had incorrectly changed dev-lang/gnat-gpl's metadata.xml.  Should be fixed now.
Comment 9 nivedita 2019-05-13 20:01:57 UTC
Just a comment on bootstrap-lto vs bootstrap-lto-lean although it shouldn't be a big deal.

The distinction between them is more than bootstrap vs bootstrap-lean. In a non-lto build with bootstrap-lean stage2 and stage3 are still compared: what the lean does is to delete stage1 compiler once stage2 is built since it's not needed any more, and delete stage2 once stage3 is built and compared with stage2.

Without PGO, in bootstrap-lto, stage2 and stage3 are both built with LTO and compared. With bootstrap-lto-lean since stage2 is not built with LTO, they cannot be compared, so you lose some double-checking. With regular -O2 compiling this should not be a great loss, though you may want to keep the compare if we eventually allow overriding the flags.

With PGO, no comparison is done in any case, so that part doesn't matter. However, notice the "With ‘make profiledbootstrap’ the LTO frontend is trained only on generator files." That means you potentially lose some profiling coverage as the full compiler is not built with LTO in stage3 (this is the stage that creates the profiling data), only a set of helper programs.
Comment 10 Shane Peelar 2019-05-13 21:06:02 UTC
Ah yes, the config files seem to indicate such as well.  It sounds like bootstrap-lto is the safest and possibly most performant option.
Comment 11 Shane Peelar 2019-05-13 21:27:12 UTC
Created attachment 576516 [details, diff]
bootstrap-lto, >=gcc-9.1, no gnat-gpl

Same as previous patch, but uses bootstrap-lto instead.  I'm not marking the other patch as obsolete because there may be reasons to use bootstrap-lto-lean that I'm not aware of.
Comment 12 Larry the Git Cow gentoo-dev 2019-05-13 22:38:28 UTC
The bug has been referenced in the following commit(s):

https://gitweb.gentoo.org/repo/gentoo.git/commit/?id=ee307a9f5d9e77ce1dbb3655d4ba7f12fffaf554

commit ee307a9f5d9e77ce1dbb3655d4ba7f12fffaf554
Author:     Shane Peelar <lookatyouhacker@gmail.com>
AuthorDate: 2019-05-08 18:55:53 +0000
Commit:     Sergei Trofimovich <slyfox@gentoo.org>
CommitDate: 2019-05-13 22:38:22 +0000

    {sys-devel/gcc,dev-lang/gnat-gpl}: add a USE for building GCC with LTO
    
    Bug: https://bugs.gentoo.org/685634
    
    Signed-off-by: Shane Peelar <lookatyouhacker@gmail.com>
    Signed-off-by: Sergei Trofimovich <slyfox@gentoo.org>

 eclass/toolchain.eclass    | 6 ++++++
 sys-devel/gcc/metadata.xml | 1 +
 2 files changed, 7 insertions(+)
Comment 13 Sergei Trofimovich (RETIRED) gentoo-dev 2019-05-13 22:38:52 UTC
Pushed as-is. Thank you!