# Building world or kernel with LTO



## PaddyMac (Jul 18, 2020)

I've searched online and found very few resources about enabling linktime optimization when building the FreeBSD kernel, world, or enabling globally for ports. And what is out there is mostly outdated. What would be the correct way to enable LTO? Is it still necessary to change the defaults for things like AR, NM, or RANLIB? Or should it simply work by adding -flto=thin to CFLAGS and COPTFLAGS? Has there been any testing on using LTO?


----------



## Alain De Vos (Jul 18, 2020)

And how to do this within poudriere jail?


----------



## unitrunker (Jul 18, 2020)

LTO is useful for static libraries. I don't think it helps much with shared libraries.


----------



## diizzy (Jul 18, 2020)

It can help quite a bit to reduce file size on shared libraries and *possibly* increase performance however you can also most likely expect to see breakage. It's not supported (tested) either kernel and/or world and is usually added to both CFLAGS and LDFLAGS.
Ref: 
https://wiki.debian.org/LTO , http://lists.infradead.org/pipermail/openwrt-devel/2019-November/025464.html

I would guess that you need to handtune kernel and world instead of globally enabling it if you want it to work.

A few ports already supports LTO (such as FFmpeg) but it's not enabled by default.


----------



## unitrunker (Jul 19, 2020)

Thanks dizzy. 8 % is quite a win for dnsmasq. I could not find actual "so" library examples. Since Google is promoting LTO in Linux, maybe they'll sponsor a GSOC effort next year.


----------



## diizzy (Jul 19, 2020)

Keep in mind that GCC != LLVM/CLANG and they can perform quite different.
As an example: For the few applications I've tested it on O2 vs O3 using Clang doesn't seem to make much difference at all (if any).

I gave this a go using ffmpeg, just for fun and got some interesting results... (13-CURRENT)


```
=== LTO disabled ===
-rw-r--r--  1 root  wheel    19M Jul 19 01:50 libavcodec.a
-rwxr-xr-x  1 root  wheel    14M Jul 19 01:50 libavcodec.so.58.91.100
-rw-r--r--  1 root  wheel    42K Jul 19 01:50 libavdevice.a
-rwxr-xr-x  1 root  wheel    31K Jul 19 01:50 libavdevice.so.58.10.100
-rw-r--r--  1 root  wheel   5.8M Jul 19 01:50 libavfilter.a
-rwxr-xr-x  1 root  wheel   3.6M Jul 19 01:50 libavfilter.so.7.85.100
-rw-r--r--  1 root  wheel   4.6M Jul 19 01:50 libavformat.a
-rwxr-xr-x  1 root  wheel   2.3M Jul 19 01:50 libavformat.so.58.45.100
-rw-r--r--  1 root  wheel   216K Jul 19 01:50 libavresample.a
-rwxr-xr-x  1 root  wheel   144K Jul 19 01:50 libavresample.so.4.0.0
-rw-r--r--  1 root  wheel   810K Jul 19 01:50 libavutil.a
-rwxr-xr-x  1 root  wheel   524K Jul 19 01:50 libavutil.so.56.51.100
-rw-r--r--  1 root  wheel   145K Jul 19 01:50 libpostproc.a
-rwxr-xr-x  1 root  wheel   128K Jul 19 01:50 libpostproc.so.55.7.100
-rw-r--r--  1 root  wheel   193K Jul 19 01:50 libswresample.a
-rwxr-xr-x  1 root  wheel   127K Jul 19 01:50 libswresample.so.3.7.100
-rw-r--r--  1 root  wheel   1.0M Jul 19 01:50 libswscale.a
-rwxr-xr-x  1 root  wheel   730K Jul 19 01:50 libswscale.so.5.7.100

=== LTO enabled ===
-rw-r--r--  1 root  wheel    40M Jul 19 01:58 libavcodec.a
-rwxr-xr-x  1 root  wheel    14M Jul 19 01:58 libavcodec.so.58.91.100
-rw-r--r--  1 root  wheel    89K Jul 19 01:58 libavdevice.a
-rwxr-xr-x  1 root  wheel    30K Jul 19 01:58 libavdevice.so.58.10.100
-rw-r--r--  1 root  wheel    10M Jul 19 01:58 libavfilter.a
-rwxr-xr-x  1 root  wheel   3.7M Jul 19 01:58 libavfilter.so.7.85.100
-rw-r--r--  1 root  wheel   8.8M Jul 19 01:58 libavformat.a
-rwxr-xr-x  1 root  wheel   2.6M Jul 19 01:58 libavformat.so.58.45.100
-rw-r--r--  1 root  wheel   352K Jul 19 01:58 libavresample.a
-rwxr-xr-x  1 root  wheel   140K Jul 19 01:58 libavresample.so.4.0.0
-rw-r--r--  1 root  wheel   1.6M Jul 19 01:58 libavutil.a
-rwxr-xr-x  1 root  wheel   553K Jul 19 01:58 libavutil.so.56.51.100
-rw-r--r--  1 root  wheel   674K Jul 19 01:58 libpostproc.a
-rwxr-xr-x  1 root  wheel   129K Jul 19 01:58 libpostproc.so.55.7.100
-rw-r--r--  1 root  wheel   336K Jul 19 01:58 libswresample.a
-rwxr-xr-x  1 root  wheel   123K Jul 19 01:58 libswresample.so.3.7.100
-rw-r--r--  1 root  wheel   2.6M Jul 19 01:58 libswscale.a
-rwxr-xr-x  1 root  wheel   700K Jul 19 01:58 libswscale.so.5.7.100

=== LTO (thin) enabled - Not run-tested! ===
-rw-r--r--  1 root  wheel    38M Jul 19 02:21 libavcodec.a
-rwxr-xr-x  1 root  wheel    14M Jul 19 02:21 libavcodec.so.58.91.100
-rw-r--r--  1 root  wheel    89K Jul 19 02:21 libavdevice.a
-rwxr-xr-x  1 root  wheel    30K Jul 19 02:21 libavdevice.so.58.10.100
-rw-r--r--  1 root  wheel   9.6M Jul 19 02:21 libavfilter.a
-rwxr-xr-x  1 root  wheel   3.7M Jul 19 02:21 libavfilter.so.7.85.100
-rw-r--r--  1 root  wheel   8.8M Jul 19 02:21 libavformat.a
-rwxr-xr-x  1 root  wheel   2.6M Jul 19 02:21 libavformat.so.58.45.100
-rw-r--r--  1 root  wheel   282K Jul 19 02:21 libavresample.a
-rwxr-xr-x  1 root  wheel   142K Jul 19 02:21 libavresample.so.4.0.0
-rw-r--r--  1 root  wheel   1.6M Jul 19 02:21 libavutil.a
-rwxr-xr-x  1 root  wheel   550K Jul 19 02:21 libavutil.so.56.51.100
-rw-r--r--  1 root  wheel   664K Jul 19 02:21 libpostproc.a
-rwxr-xr-x  1 root  wheel   129K Jul 19 02:21 libpostproc.so.55.7.100
-rw-r--r--  1 root  wheel   301K Jul 19 02:21 libswresample.a
-rwxr-xr-x  1 root  wheel   124K Jul 19 02:21 libswresample.so.3.7.100
-rw-r--r--  1 root  wheel   2.1M Jul 19 02:21 libswscale.a
-rwxr-xr-x  1 root  wheel   718K Jul 19 02:21 libswscale.so.5.7.100
```


----------



## UnitedMarsupials (Mar 2, 2021)

> It's not supported (tested) either kernel and/or world


For what it may be worth, Linus just announced enabling LTO in his kernel (version 5.12), so now BSD world, which started using clang much earlier, needs to catch up


----------



## Mjölnir (Mar 12, 2021)

I would very much appreciate if you guys could manage to improve the readabilty of your posts by using BB-codes oocasionally... You don't have to be strict, I'm not nitpicking, but writing just pure plaintext is much harder to read.  Thank you.


----------



## Deleted member 66267 (Mar 13, 2021)

UnitedMarsupials said:


> For what it may be worth, Linus just announced enabling LTO in his kernel (version 5.12), so now BSD world, which started using clang much earlier, needs to catch up


We don't need to hurry. I follow phoronix daily so I know a LTO/PGO Linux kernel caused many problems, even minor.


----------



## Mjölnir (Mar 13, 2021)

failure said:


> We don't need to hurry. I follow phoronix daily so I know a LTO/PGO Linux kernel caused many problems, even minor.


And rightly so. That's _the BeaSDie_'s hiking trail.  The hikers have a proverb: _"we've got no time for cutoffs"_.  Stay cool & let them Lx'ers (intentionally ambiguous) run around nervously.


----------

