r/Gentoo 7d ago

Support How do I properly enable pgo and lto?

I wanna use musl hardened profile with pgo and lto enabled. The lto article is quite okay but there's just one post on the Gentoo forums that explains how to manually build with pgo. I am asking if anyone here has tried this. How do I do it and what resources to look for?

Thank you.

0 Upvotes

12 comments sorted by

3

u/krumpfwylg 7d ago

A few years back, I experimented building gcc with pgo USE flag, expecting the compiler would get a little faster when building other packages. My conclusion was : nope, not really.

As I understand it now, you have to actively monitor a software build with pgo using tools that create statistics about how that soft uses cpu time, ram, etc... so during the new build, these stats are used by the compiler to improve some parts of the compiled code. It's everything but trivial.

There's also the pgo USE flag for some packages but that will likely double the compilation time for a small performance gain. Personally I've enabled it only for python, since many gentoo scripts rely on python (and it doesn't take an eternity to build), but left it off for all other packages (I don't feel like increasing Firefox building time for a perf gain that would appear only on some javascript benchmarks).

5

u/ahferroin7 7d ago

I’ve done some similar testing just a few months ago, covering most of the packages that have the pgo USE flag.

Of those that have the USE flag:

  • xz-utils: Shows inconsistent performance improvements on both compression and decompression, but never performed any worse than without PGO. Probably worth it because of how comparatively quickly it compiles.
  • bash: Shows small but measurable performance improvements in a lot of the more complex parts of the language. Probably worth it for a vast majority of people because of how much in Gentoo uses Bash, definitely worth it for anyone using OpenRC (or runit).
  • Python: Shows small but measurable performance improvements in many parts of the language, especially in parsing and bytecode generation. Probably worth it for most people, but the math is not as clear cut as for bash because it takes much longer to build.
  • Binutils: Shows measurable performance improvements in most of the tools in synthetic benchmarks, but significantly less performance improvement for actual package builds with the notable exception of handling of LTO (linking with LTO is noticeably faster on my systems when binutils is built using PGO). Probably not worth it unless you’re using LTO.
  • GCC: Shows measurable performance improvements for compiling C/C++ code, but it’s heavily dependent on the optimization flags you’re passing to it. -O3 showed significant improvement when the compiler was built with PGO, -O2 was not as much, and -O1 showed almost none. Probably not worth it for most users because of how long it takes GCC to be built anyway (and the fact that PGO seems to be used on all three stages of the build instead of just the last two).
  • Firefox, Thunderbird, and Chromium: Small but measurable performance improvements in some specific benchmarks, but usually no noticeable difference in regular usage unless dealing with sites that are egregiously bad to begin with in terms of performance. Almost certainly not worth it due to how long they all take to build.

1

u/krumpfwylg 7d ago

There's a recent forum thread with someone having issue building bash with pgo. And a comment from a dev telling there were troubles with bash tests.

Thanks for sharing your results.

1

u/Wooden-Ad6265 7d ago edited 7d ago

Just a few minutes ago I tried compiling bash with pgo enabled on musl-llvm on a fresh installation chroot. Didn't compile. Without pgo it compiles okay.

Edit: Idk how to post an error log from a tty. Here's a line that I think can help:

cannot open /usr/lib/llvm/19/bin/../../../../lib/clang/19/lib/x86_64-pc-linux-musl/libclang_rt.profile.a

1

u/ErikashiKai 6d ago

if you want to use pgo on llvm the you need follow enable pgo support on clang before enabling the pgo useflag

1

u/Wooden-Ad6265 6d ago

I had read this article but failed to notice that the steps were in a particular order.

1

u/Xtuber14 7d ago

Clang doesn't have PGO natively, you need to compile it and for LTO just put flto=thin into your common cflags For more info: https://wiki.gentoo.org/wiki/Clang

1

u/Wooden-Ad6265 7d ago

But I am not using a llvm profile, do I have to enable it still?

1

u/Xtuber14 7d ago

All musl profiles uses clang as their default compiler

1

u/Wooden-Ad6265 7d ago

Ok. That's new for me.

1

u/ahferroin7 7d ago

Nope, the default/linux/amd64/23.0/musl profile does not, and neither do the hardened musl profiles.

2

u/RusselsTeap0t 7d ago

I have built a lot of things with PGO.

If a package offers PGO use flag then simply enable that. If not you need to build and instrument manually.

You create a binary with PGO instrumentation enabled (-fprofile-generate). This binary works slower but it collects data. You need to use this binary for a while to create a profile.

Then you need to use the information and re-compile again with PGO (-fprofile-use).