User:Aslantis/Lesser known optimization flags for make.conf

From Gentoo Wiki
Jump to:navigation Jump to:search
You've heard it before, you'll hear it again: aggressive optimization flags can cause build and runtime errors. You have been warned.
I type before I think, so don't just copy and paste anything from this page without understanding what you're doing! :D

This is my list of certain optimization flags you can set in your make.conf. I decided to start this after learning about optimization flags that I didn't know you could set for rust, and got curious about other possibilities so decided to start a running list!!

If you know any other flags you can set for further optimizations, I'd appreciate if you added them to this page!! (or at least let me know about them in my talk page.)

I have specific info about each variable/language at the bottom. I have the sane optimization levels uncommented, and if applicable the most "aggressive" optimizations commented out directly below. Please read the warnings at the bottom about these "aggressive" optimizations, and please reproduce bugs without these flags before reporting.

FILE portage/make.conf
## Common flags for C, C++, Fortran 77, and Fortran 9x respectively
COMMON_FLAGS="-O3 -pipe -march=native -flto=$(nproc)" #Note that -O3 can sometimes compile _slower_ binaries than -O2, and may use more ram
#COMMON_FLAGS="-Ofast -pipe -march=native -flto=$(nproc)" #I believe this is as aggressive as possible. -Ofast _will_ break your system when used globally!!

#LDFLAGS="${LDFLAGS} -Wl,--strip-all" #Not needed, as portage does this automatically, and stripping should be handled by $FEATURES

# For C programs built as modules from Go.

## Rust. -O3 is safe, as undefined behavior isn't really a thing for safe rust.
RUSTFLAGS="-C target-cpu=native -C opt-level=3 -C strip=symbols"
#RUSTFLAGS="${RUSTFLAGS} -C lto=fat -C embed-bitcode=yes -C prefer-dynamic=no" #Needed for LTO. atm most builds fail with this
#CARGO_PROFILE_RELEASE_LTO="true" #don't know how to make portage use rust profiles yet :p

## Golang. Its arch targeting is unconventional, _Please_ see its more info section below as there is no "native".
# Each architecture (defined in $GOARCH) has its own unique way to specify a minimum target, read below.
# You _*WILL*_ have to change these values by hand!! $GOHOSTARCH and $GOHOSTOS won't have values yet.
GOOS="${GOHOSTOS}" #value example: "linux"
GOARCH="${GOHOSTARCH}" #value example: "amd64" < note how the value is lowercase
# Arch-specific minimum target tuning. Having things set for another arch is harmless. I've set the below to Go's default values.

## USE & USE EXPAND variables

For (Free) Pascal optimizations create/edit this file:

FILE /etc/fpc.cfg
# -Cp<CPU> is which CPU to target. To get a list of supported options run `fpc -ic`.
# There is a chance -O4 may cause problems.

More info on flags


* GCC_optimization <- Read this in its entirety, at least before setting any flags higher than -O2. I'm not going to paraphrase, the devs have done a good job at explaining :)
* Safe_CFLAGS has a list of -march and other flags for almost all consumer CPUs, if you still have it set to native look at this.


* It's never a good idea to override LDFLAGS. At the least include the variable in itself (as shown above) to not override your profile's default settings for this.
* Portage by default strips binaries, so having --strip-all in LDFLAGS is redundant, using portage's FEATURES var would be a better way to manipulate this.


* rustc -C help
* rustc --print target-cpus
* <- Forum about LTO


All optimizations shown here are for gc, aka, dev-lang/go. Optimizations for gccgo are included in my other userpage here
* <- This will explain what you need to set to optimize for your specific arch
* <- Relevant forum thread
* Want to know which value to set for amd64? Good luck, this is all I can find.

Free Pascal:




* Ada's `gprbuild` build system seems to not have any standardized way to handle (optimization) flags via environment variables. This implies that there are several different environment vars formatted different ways. In the future I'll find the most common and see if I can implement them.


* Zig also has (optimization) flags, but the way Zig programs are built (build.zig) doesn't seem to have a standard way to listen to environment vars for individual flags. I've done an unnecessary amount of thinking of how to accomplish this, perhaps an official zig eclass, maybe some kind of patch to build.zig that makes it listen to user-chosen flags?


* shoot I forgot how haskell worked I'll be back