Safe CFLAGS

From Gentoo Wiki
Jump to: navigation, search

This article attempts to summarize 'safe' settings for CPU flags on Gentoo Linux.

Automatic CPU detection

A recommended default choice for CFLAGS or CXXFLAGS is to use -march=native. This enables auto-detection of the CPU's architecture. A possible entry might look like:

FILE /etc/portage/make.conf
CFLAGS="-O2 -pipe -march=native"
CXXFLAGS="${CFLAGS}"

Under certain circumstances it is not possible (or not recommended) to use -march=native, for example when using distcc. Consult the distcc page about how to set up CFLAGS/CXXFLAGS correctly.

Note
Additional information can be found at the GCC optimization page.
Warning
Do not use -march=native or -mtune=native in the CFLAGS or CXXFLAGS variables of make.conf when compiling with distcc.

Manual

Finding the CPU

To identify the model of the CPU, take a look inside /proc/cpuinfo for the "cpu family" and "model" numbers like so:

user $grep -m1 -A3 "vendor_id" /proc/cpuinfo

Once this information is found match the CPU to one listed on this page in order to find out the suggested "safe" CFLAGS.

Below is a list of CFLAGS which are to be considered "safe" for the given processors. These are the settings that should be used, especially when unsure which CFLAGS the processor needs.

x86/amd64

Intel

Skylake

Core i3/i5/i7 & Xeon E3/E5 *V5
vendor_id       : GenuineIntel
cpu family      : 6
model           : 94
model name      : Intel(R) Core(TM) i7-6700K CPU @ 4.00GHz
FILE /etc/portage/make.conf
CHOST="x86_64-pc-linux-gnu"
CFLAGS="-march=broadwell -O2 -pipe"
CXXFLAGS="${CFLAGS}"
Note
Skylake march support has been introduced with GCC 6. As Gentoo is still using an earlier version of GCC, we use -march=broadwell.
Note
Even though they fit the family and model, this set of CFLAGS does NOT work for Skylake Pentium CPUs (e. g. G4400) producing invalid code. -march=native works.

Haswell

Core i3/i5/i7 & Xeon E3/E5/E7 *V3
vendor_id	: GenuineIntel
cpu family	: 6
model		: 60
model name	: Intel(R) Xeon(R) CPU E3-1271 v3 @ 3.60GHz
…
model           : 60
model name      : Intel(R) Core(TM) i7-4770 CPU @ 3.40GHz
FILE /etc/portage/make.conf
CHOST="x86_64-pc-linux-gnu"
CFLAGS="-march=haswell -O2 -pipe"
CXXFLAGS="${CFLAGS}"

Ivy Bridge

Core i3/i5/i7 & Xeon E3/E5/E7 *V2
vendor_id       : GenuineIntel
cpu family      : 6
model           : 58
model name      : Intel(R) Core(TM) i7-3610QM CPU @ 2.30GHz 
FILE /etc/portage/make.conf
CHOST="x86_64-pc-linux-gnu"
CFLAGS="-march=ivybridge -O2 -pipe"
CXXFLAGS="${CFLAGS}"
Pentium
vendor_id	: GenuineIntel
cpu family	: 6
model		: 58
model name	: Intel(R) Pentium(R) CPU G2020 @ 2.90GHz
FILE /etc/portage/make.conf
CHOST="x86_64-pc-linux-gnu"
CFLAGS="-march=ivybridge -mno-avx -mno-aes -mno-rdrnd -O2 -pipe"
CXXFLAGS="${CFLAGS}"

Sandy Bridge

Core i3/i5/i7 & Xeon E3/E5/E7
vendor_id	: GenuineIntel
cpu family	: 6
…
model		: 42
model name	: Intel(R) Core(TM) i5-2400 CPU @ 3.10GHz
…
model		: 45
model name	: Intel(R) Core(TM) i7-3930K CPU @ 3.20GHz
… 
model		: 42
model name	: Intel(R) Xeon(R) CPU E31245 @ 3.30GHz
… 
model           : 45
model name      : Intel(R) Xeon(R) CPU E5-2407 0 @ 2.20GHz
FILE /etc/portage/make.conf
CHOST="x86_64-pc-linux-gnu"
CFLAGS="-march=sandybridge -O2 -pipe"
CXXFLAGS="${CFLAGS}"
Pentium
vendor_id	: GenuineIntel
cpu family	: 6
model		: 42
model name	: Intel(R) Pentium(R) CPU B960 @ 2.20GHz
FILE /etc/portage/make.conf
CHOST="x86_64-pc-linux-gnu"
CFLAGS="-march=sandybridge -mno-avx -mno-aes -mno-rdrnd -O2 -pipe"
CXXFLAGS="${CFLAGS}"

Nehalem

Core i3/i5/i7
vendor_id	: GenuineIntel
cpu family	: 6
model		: 30
model name	: Intel(R) Core(TM) i5-750 CPU @ 2.66GHz
FILE /etc/portage/make.conf
CHOST="x86_64-pc-linux-gnu"
CFLAGS="-march=nehalem -O2 -pipe"
CXXFLAGS="${CFLAGS}"

Westmere

Core i3/i5/i7
vendor_id	: GenuineIntel
cpu family	: 6
model		: 37
model name	: Intel(R) Core(TM) i5-650 CPU @ 3.20GHz
FILE /etc/portage/make.conf
CHOST="x86_64-pc-linux-gnu"
CFLAGS="-march=westmere -O2 -pipe"
CXXFLAGS="${CFLAGS}"

Intel Core

vendor_id       : GenuineIntel
cpu family      : 6
…
model		: 15
model name	: Intel(R) Core(TM)2 Duo CPU     T7500  @ 2.20GHz
…
model           : 15
model name      : Intel(R) Xeon(R) CPU            3040  @ 1.86GHz
FILE /etc/portage/make.conf
CHOST="x86_64-pc-linux-gnu"
CFLAGS="-march=core2 -O2 -pipe"
CXXFLAGS="${CFLAGS}"

Older microarchitecture

Pentium M (Dothan)
vendor_id	: GenuineIntel
cpu family	: 6
model		: 13
model name	: Intel(R) Pentium(R) M processor 2.13GHz
FILE /etc/portage/make.conf
CHOST="i686-pc-linux-gnu"
CFLAGS="-O2 -march=pentium-m -pipe -fomit-frame-pointer"
CXXFLAGS="${CFLAGS}"
Pentium 4 (Prescott)
  • 64-bit capable models: 505, 505J, 506, 511, 516, 517, 519K, 521, 524, 531, 541, 551, 561, 571, 6xx, and the 3.73(3)GHz Pentium 4 Extreme Edition.
Note
Check the /proc/cpuinfo for the lm flag to detect 64-bit CPUs:
user $grep lm /proc/cpuinfo
vendor_id	: GenuineIntel
cpu family	: 15
model		: 4
model name	: Intel(R) Pentium(R) 4 CPU XXXGHz
FILE /etc/portage/make.conf
CHOST="x86_64-pc-linux-gnu"
CFLAGS="-O2 -march=nocona -pipe"
CXXFLAGS="${CFLAGS}"
  • All other Prescotts
vendor_id	: GenuineIntel
cpu family	: 15
model		: 3
model name	: Intel(R) Pentium(R) 4 CPU XXXGHz
FILE /etc/portage/make.conf
CHOST="i686-pc-linux-gnu"
CFLAGS="-O2 -march=prescott -pipe"
CXXFLAGS="${CFLAGS}"

AMD

Ryzen X 1X00 (Zen)

vendor_id	: AuthenticAMD
cpu family	: 23
model		: 1
model name	: AMD Ryzen 7 1800X Eight-Core Processor
FILE /etc/portage/make.conf
CHOST="x86_64-pc-linux-gnu"
CFLAGS="-O2 -march=haswell -pipe"
CXXFLAGS="${CFLAGS}"
Note
Znver1 march support has been introduced with GCC 6. As Gentoo is still using an earlier version of GCC, we use -march=haswell. That setting should be functional and safe for older GCCs.

A6/A8/A9/A10/A12-8XXX/9XXX (Excavator)

Carrizo, Bristol Ridge & Stoney Ridge
vendor_id	: AuthenticAMD
cpu family	: 21
model		: 96 or 101 or 112
model name	: AMD A12-9800 RADEON R7, 12 COMPUTE CORES 4C+8G
FILE /etc/portage/make.conf
CHOST="x86_64-pc-linux-gnu"
CFLAGS="-O2 -march=bdver4 -pipe"
CXXFLAGS="${CFLAGS}"

A4/A6/A8/A10-7XXX/8XXX (Steamroller)

Kaveri & Godavari
vendor_id	: AuthenticAMD
cpu family	: 21
model		: 48 or 56
model name	: AMD A10-7850K Radeon R7, 12 Compute Cores 4C+8G
FILE /etc/portage/make.conf
CHOST="x86_64-pc-linux-gnu"
CFLAGS="-O2 -march=bdver3 -pipe"
CXXFLAGS="${CFLAGS}"
Note
Various lowpower AMD APUs branded as AX-7XXX (eg. A4-7210) don't belong to cpu family 21!

A4/A6/A8/A10-4XXX/5XXX/6XXX (Piledriver)

Trinity & Richland
vendor_id	: AuthenticAMD
cpu family	: 21
model		: 16 or 19
model name	: AMD A8-4500M APU with Radeon(tm) HD Graphics
FILE /etc/portage/make.conf
CHOST="x86_64-pc-linux-gnu"
CFLAGS="-O2 -march=bdver2 -pipe"
CXXFLAGS="${CFLAGS}"
Note
Various lowpower AMD APUs branded as AX-5XXX/6XXX (eg. A4-5000, A4-5100 or A6-6310) don't belong to cpu family 21!

A4/A6/A8-3XXX/3XXXM (12h)

Llano
vendor_id	: AuthenticAMD
cpu family	: 18
model		: 1
model name	: AMD A8-3500M APU with Radeon(tm) HD Graphics
FILE /etc/portage/make.conf
CHOST="x86_64-pc-linux-gnu"
CFLAGS="-O2 -march=amdfam10 -mcx16 -mpopcnt -pipe"
CXXFLAGS="${CFLAGS}"

FX-XXXX (Bulldozer & Piledriver)

vendor_id	: AuthenticAMD
cpu family	: 21
model		: 1 or 2
model name	: AMD FX(tm)-8150 Eight-Core Processor
Note
Make sure and check the number listed by model on your system, the -march flag should be bdverX where X is the model number.
Note
Various AMD APUs branded as FX don't match model 1 or 2! They should use settings listed for AMD APU model given by the cpu family and model.
FILE /etc/portage/make.conf
CHOST="x86_64-pc-linux-gnu"
CFLAGS="-O2 -march=bdver1 -pipe"
CXXFLAGS="${CFLAGS}"

Phenom/Phenom II/Athlon II/Sempron (10h)

Agena, Deneb, Thuban & derivates
vendor_id	: AuthenticAMD
cpu family	: 16
model		: X
model name	: AMD Phenom(tm) II X6 1090T Processor
FILE /etc/portage/make.conf
CHOST="x86_64-pc-linux-gnu"
CFLAGS="-O2 -march=amdfam10 -pipe"
CXXFLAGS="${CFLAGS}"

Geode LX

vendor_id	: AuthenticAMD
cpu family	: 5
model		: 10
model name	: Geode(TM) Integrated Processor by AMD PCS
FILE /etc/portage/make.conf
CHOST="i486-pc-linux-gnu"
CFLAGS="-Os -pipe -march=geode -mmmx -m3dnow -fomit-frame-pointer"
CXXFLAGS="${CFLAGS}"

arm

Note
To identify the respective ARM core of the SoC on your board, List of ARM microarchitectures and List of applications of ARM cores on Wikipedia may help.

Cortex-A

ARMv7-A/Cortex-A9 MPCore

with optional VFPv3 FPU
processor       : 0
model name      : ARMv7 Processor rev 0 (v7l)
BogoMIPS        : 2.00
Features        : half thumb fastmult vfp edsp vfpv3 vfpv3d16 tls 
CPU implementer : 0x41
CPU architecture: 7
CPU variant     : 0x1
CPU part        : 0xc09
CPU revision    : 0

processor       : 1
model name      : ARMv7 Processor rev 0 (v7l)
BogoMIPS        : 2.00
Features        : half thumb fastmult vfp edsp vfpv3 vfpv3d16 tls 
CPU implementer : 0x41
CPU architecture: 7
CPU variant     : 0x1
CPU part        : 0xc09
CPU revision    : 0

Hardware        : NVIDIA Tegra SoC (Flattened Device Tree)
Revision        : 0000
Serial          : 0000000000000000
FILE /etc/portage/make.conf
CHOST="armv7a-hardfloat-linux-gnueabi"
CFLAGS="-O2 -march=cortex-a9 -mfpu=vfpv3-d16 -mfloat-abi=hard -pipe -fomit-frame-pointer"
CXXFLAGS="${CFLAGS}"
Note
This ARM core (equipped with the optional vfpv3d16 FPU but missing the NEON extension) is used in the Toshiba AC100/Dynabook AZ/Compal Paz00 Board.

ARMv8-A/BCM2837

Note
This is the Broadcom chip used in the Raspberry Pi 3 Model B.
AArch32 with neon FPU
processor	: 0-3
model name	: ARMv7 Processor rev 4 (v7l)
BogoMIPS	: 38.40
Features	: half thumb fastmult vfp edsp neon vfpv3 tls vfpv4 idiva idivt vfpd32 lpae evtstrm crc32 
CPU implementer	: 0x41
CPU architecture: 7
CPU variant	: 0x0
CPU part	: 0xd03
CPU revision	: 4

Hardware	: BCM2709
Revision        : 0000
Serial          : 0000000000000000
FILE /etc/portage/make.conf
CHOST="armv7a-hardfloat-linux-gnueabi"
CFLAGS="-O2 -pipe -march=armv7-a -mfpu=neon-vfpv4 -mfloat-abi=hard"
CXXFLAGS="${CFLAGS}"
AArch64
processor	: 0-3
BogoMIPS	: 38.40
Features	: fp asimd evtstrm crc32
CPU implementer	: 0x41
CPU architecture: 8
CPU variant	: 0x0
CPU part	: 0xd03
CPU revision	: 4
FILE /etc/portage/make.conf
CHOST="aarch64-unknown-linux-gnu"
CFLAGS="-march=armv8-a+crc -mtune=cortex-a53 -O2 -pipe"
CXXFLAGS="${CFLAGS}"


ARM11

ARMv6/ARM1176JZF-S

processor	: 0
model name	: ARMv6-compatible processor rev 7 (v6l)
BogoMIPS	: 697.95
Features	: half thumb fastmult vfp edsp java tls 
CPU implementer	: 0x41
CPU architecture: 7
CPU variant	: 0x0
CPU part	: 0xb76
CPU revision	: 7

Hardware	: BCM2835
Revision	: 0000
Serial		: 000000000XXXXXXX
FILE /etc/portage/make.conf
CHOST="armv6j-hardfloat-linux-gnueabi"
CFLAGS="-O2 -pipe -march=arm1176jzf-s -mfpu=vfp -mfloat-abi=hard"
CXXFLAGS="${CFLAGS}"
Note
This ARM core is used in the first generation of the Raspberry Pi.

ARMv6/ARM1136JF-S

Processor       : ARMv6-compatible processor rev 5 (v6l)
BogoMIPS        : 791.34
Features        : swp half thumb fastmult vfp edsp java 
CPU implementer : 0x41
CPU architecture: 6TEJ
CPU variant     : 0x1
CPU part        : 0xb36
CPU revision    : 5

Hardware        : IMAPX200
Revision        : 0000
Serial          : 0000000000000000
FILE /etc/portage/make.conf
CHOST="armv6j-hardfloat-linux-gnueabi"
CFLAGS="-Os -march=arm1136jf-s -mfpu=vfp -mfloat-abi=hard -pipe -fomit-frame-pointer"
CXXFLAGS="${CFLAGS}"

ppc/ppc64

Note
-march=native almost never works on PowerPC.

POWER8

processor       : 0
cpu             : POWER8E (raw), altivec supported
clock           : 3026.000000MHz
revision        : 2.1 (pvr 004b 0201)

timebase        : 512000000
platform        : pSeries
model           : IBM pSeries (emulated by qemu)
machine         : CHRP IBM pSeries (emulated by qemu)
Note
Currently Gentoo does not support POWER8, but safe CFLAGS for it would look like the following.
FILE /etc/portage/make.conf
CHOST="powerpc64le-linux-gnu"
CFLAGS="-mcpu=power8 -O2 -pipe -mabi=altivec -maltivec"
CXXFLAGS="${CFLAGS}"

Cell

processor	: 0
cpu		: Cell Broadband Engine, altivec supported
clock		: 3192.000000MHz
revision	: 5.1 (pvr 0070 0501)

processor	: 1
cpu		: Cell Broadband Engine, altivec supported
clock		: 3192.000000MHz
revision	: 5.1 (pvr 0070 0501)

timebase	: 79800000
platform	: PS3
model		: SonyPS3
FILE /etc/portage/make.conf
CHOST="powerpc-unknown-linux-gnu"
CFLAGS="-mcpu=cell -O2 -pipe -mabi=altivec -maltivec"
CXXFLAGS="${CFLAGS}"
Note
GCC's -mspe and -mabi=spe options are not targetting PS3 systems and IBM Cell. Instead, those options are dedicated to IBM e500. More info: https://lists.debian.org/debian-devel/2011/06/msg00592.html https://wiki.debian.org/PowerPCSPEPort

G4

PPC 7447A

processor	: 0
cpu		: 7447A, altivec supported
clock		: 1666.666000MHz
revision	: 1.5 (pvr 8003 0105)
bogomips	: 33.28
timebase	: 8320000
platform	: PowerMac
model		: PowerBook5,9
machine		: PowerBook5,9
motherboard	: PowerBook5,9 MacRISC3 Power Macintosh 
detected as	: 287 (PowerBook G4 17")
pmac flags	: 00000018
L2 cache	: 512K unified
pmac-generation	: NewWorld
FILE /etc/portage/make.conf
CHOST="powerpc-unknown-linux-gnu"
CFLAGS="-mcpu=7450 -O2 -maltivec -mabi=altivec -fno-strict-aliasing -pipe"
CXXFLAGS="${CFLAGS}"

G3 (PPC 7XX)

processor       : 0
cpu             : 740/750
clock           : 400.000000MHz
revision        : 131.0 (pvr 0008 8300)
bogomips        : 49.93
timebase        : 24966218
platform        : PowerMac
model           : PowerBook3,1
machine         : PowerBook3,1
motherboard     : PowerBook3,1 MacRISC2 MacRISC Power Macintosh
detected as     : 70 (PowerBook Pismo)
pmac flags      : 0000001f
L2 cache        : 1024K unified
pmac-generation : NewWorld
FILE /etc/portage/make.conf
CHOST="powerpc-unknown-linux-gnu"
CFLAGS="-mcpu=750 -Os -pipe -fno-strict-aliasing"
CXXFLAGS="${CFLAGS}"

m68k

See also

External resources