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

From Gentoo Wiki
Jump to:navigation Jump to:search

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="-O2 -pipe -march=native"
#COMMON_FLAGS="-Ofast -pipe -march=native -flto=full" #most aggressive (i think idk how lto works)
#COMMON_FLAGS="-O2 -pipe -march=native -ftree-vectorize" #my personal flags atm

CFLAGS="${COMMON_FLAGS}"
CXXFLAGS="${COMMON_FLAGS}"
FFLAGS="${COMMON_FLAGS}"
FCFLAGS="${COMMON_FLAGS}"
#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.
CGO_CFLAGS="${COMMON_FLAGS}"
CGO_CXXFLAGS="${COMMON_FLAGS}"
CGO_FFLAGS="${COMMON_FLAGS}"
GCO_FCFLAGS="${COMMON_FLAGS}"
CGO_LDFLAGS="${LDFLAGS}"

## 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.
GOAMD64="v1"
GO386="sse2"
GOARM="6"
GOPPC64="power8"
GOMIPS="hardfloat"
GOMIPS64="hardfloat"

### 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.
-O4 -CpPENTIUMM


More info on flags

COMMON_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.

LDFLAGS:

* 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.

Rust:

* rustc -C help
* rustc --print target-cpus
* https://users.rust-lang.org/t/error-lto-can-only-be-run-for-executables-cdylibs-and-static-library-outputs/73369 <- Forum about LTO

Go:

* https://go.dev/doc/install/source#environment <- This will explain what you need to set to optimize for your specific arch
* https://forum.golangbridge.org/t/how-to-generate-code-for-a-particular-cpu/26852 <- Relevant forum thread
* https://github.com/Quasilyte/gccgo_vs_gc <- First search result for "gccgo vs gc"
* https://github.com/golang/go/wiki/MinimumRequirements#amd64 Want to know which value to set for amd64? Good luck, this is all I can find.

Free Pascal:

* https://wiki.freepascal.org/Optimization


TODO

Ada:

* 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 should find the most common and see if I can implement them.

Zig:

* Zig also has (optimization) flags, but how Zig programs are built (build.zig) don'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?

Haskell:

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