Gentoo Linux – Get the Correct Compiler and CPU Flags for make.conf

Console with --march and CPU Flags

One of the challenges when installing a new Gentoo Linux is getting the correct compiler and CPU flags for your /etc/portage/make.conf file. Maybe not only while installing the system, but also when optimising things, it is nice to set the correct flags here.

I’m not going into a long and drawn-out explanation of why it is somewhat essential to set the compiler and CPU flags properly, you can google that yourself I guess. It’s just for the sake of optimising the compiler setup on Gentoo systems.

Compiler Flags -march Value

So, let’s find the right -march value:

gcc -### -march=native /usr/include/stdlib.h

This will throw some stuff in your console, including the -march=something which is a bit hard to spot. It’s in the last line, in my case, as you can see, it is "-march=skylake".

thor ~ # gcc -### -march=native /usr/include/stdlib.h
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-pc-linux-gnu/11.2.1/lto-wrapper
Target: x86_64-pc-linux-gnu
Configured with: /var/tmp/portage/sys-devel/gcc-11.2.1_p20220115/work/gcc-11-20220115/configure --host=x86_64-pc-linux-gnu --build=x86_64-pc-linux-gnu --prefix=/usr --bindir=/usr/x86_64-pc-linux-gnu/gcc-bin/11.2.1 --includedir=/usr/lib/gcc/x86_64-pc-linux-gnu/11.2.1/include --datadir=/usr/share/gcc-data/x86_64-pc-linux-gnu/11.2.1 --mandir=/usr/share/gcc-data/x86_64-pc-linux-gnu/11.2.1/man --infodir=/usr/share/gcc-data/x86_64-pc-linux-gnu/11.2.1/info --with-gxx-include-dir=/usr/lib/gcc/x86_64-pc-linux-gnu/11.2.1/include/g++-v11 --with-python-dir=/share/gcc-data/x86_64-pc-linux-gnu/11.2.1/python --enable-languages=c,c++,fortran --enable-obsolete --enable-secureplt --disable-werror --with-system-zlib --enable-nls --without-included-gettext --disable-libunwind-exceptions --enable-checking=release --with-bugurl=https://bugs.gentoo.org/ --with-pkgversion='Gentoo 11.2.1_p20220115 p4' --disable-esp --enable-libstdcxx-time --disable-libstdcxx-pch --enable-shared --enable-threads=posix --enable-__cxa_atexit --enable-clocale=gnu --enable-multilib --with-multilib-list=m32,m64 --disable-fixed-point --enable-targets=all --enable-libgomp --disable-libssp --disable-libada --disable-cet --disable-systemtap --disable-valgrind-annotations --disable-vtable-verify --disable-libvtv --without-zstd --enable-lto --without-isl --enable-default-pie --enable-default-ssp
Thread model: posix
Supported LTO compression algorithms: zlib
gcc version 11.2.1 20220115 (Gentoo 11.2.1_p20220115 p4)
COLLECT_GCC_OPTIONS='-march=native' '-dumpdir' 'a-'
/usr/libexec/gcc/x86_64-pc-linux-gnu/11.2.1/cc1 -quiet /usr/include/stdlib.h "-march=skylake" -mmmx -mpopcnt -msse -msse2 -msse3 -mssse3 -msse4.1 -msse4.2 -mavx -mavx2 -mno-sse4a -mno-fma4 -mno-xop -mfma -mno-avx512f -mbmi -mbmi2 -maes -mpclmul -mno-avx512vl -mno-avx512bw -mno-avx512dq -mno-avx512cd -mno-avx512er -mno-avx512pf -mno-avx512vbmi -mno-avx512ifma -mno-avx5124vnniw -mno-avx5124fmaps -mno-avx512vpopcntdq -mno-avx512vbmi2 -mno-gfni -mno-vpclmulqdq -mno-avx512vnni -mno-avx512bitalg -mno-avx512bf16 -mno-avx512vp2intersect -mno-3dnow -madx -mabm -mno-cldemote -mclflushopt -mno-clwb -mno-clzero -mcx16 -mno-enqcmd -mf16c -mfsgsbase -mfxsr -mhle -msahf -mno-lwp -mlzcnt -mmovbe -mno-movdir64b -mno-movdiri -mno-mwaitx -mno-pconfig -mno-pku -mno-prefetchwt1 -mprfchw -mno-ptwrite -mno-rdpid -mrdrnd -mrdseed -mrtm -mno-serialize -msgx -mno-sha -mno-shstk -mno-tbm -mno-tsxldtrk -mno-vaes -mno-waitpkg -mno-wbnoinvd -mxsave -mxsavec -mxsaveopt -mxsaves -mno-amx-tile -mno-amx-int8 -mno-amx-bf16 -mno-uintr -mno-hreset -mno-kl -mno-widekl -mno-avxvnni --param "l1-cache-size=32" --param "l1-cache-line-size=64" --param "l2-cache-size=12288" "-mtune=skylake" -quiet -dumpdir a--dumpbase stdlib.h -dumpbase-ext .h -o /tmp/cc8EOePC.s "--output-pch=/usr/include/stdlib.h.gch"

This is what needs to be set in /etc/portage/make.conf as part of the compiler flags.

COMMON_FLAGS="-march=skylake -O2 -pipe"
CFLAGS="${COMMON_FLAGS}"
CXXFLAGS="${COMMON_FLAGS}"
FCFLAGS="${COMMON_FLAGS}"
FFLAGS="${COMMON_FLAGS}"

CPU Flags

For this, you need to the app-portage/cpuid2cpuflags package.

Check if it is already installed (Assuming you have app-portage/eix installed, which you definitely should have):

ppfeufer@thor ~ $ eix cpuid2cpuflags
[I] app-portage/cpuid2cpuflags
     Verfügbare Versionen:   11 **9999*l
     Installierte Versionen: 11(15:40:17 20.09.2020)
     Startseite:             https://github.com/mgorny/cpuid2cpuflags
     Beschreibung:           Tool to guess CPU_FLAGS_* flags for the host

If not, install it …

emerge cpuid2cpuflags

Now run the following:

cpuid2cpuflags

This command will put the CPU flags in your terminal, copy them and add them to your /etc/portage/make.conf. They need to be added to the USE flags and as a separate variable, like this:

USE="........" # This is the line with your USE flags

# CPU Flags
MY_CPUFLAGS="aes avx avx2 f16c fma3 mmx mmxext pclmul popcnt sse sse2 sse3 sse4_1 sse4_2 ssse3"
USE="${USE} ${MY_CPUFLAGS}"

CPU_FLAGS_X86="${MY_CPUFLAGS}"

Save the file, and have fun with a more optimised GCC …

Leave a Reply