Add the CONFIG_X86_NATIVE_CPU=y build option to locally optimize the kernel

build with '-march=native'.
 
 Signed-off-by: Ingo Molnar <mingo@kernel.org>
 -----BEGIN PGP SIGNATURE-----
 
 iQJFBAABCgAvFiEEBpT5eoXrXCwVQwEKEnMQ0APhK1gFAmgy+q4RHG1pbmdvQGtl
 cm5lbC5vcmcACgkQEnMQ0APhK1gtng//US0TNfQpSoZSL7ezle4/H5F4VdKq0a8u
 FN7LbjSFPtOQCOmWJfcj3AvYMEiZURVqwyc2MNxIPqunaXT9gHVCrKJ1ZilhjPn4
 UW6XEaDoNxXtKOBf9RiCAjAYkWr4IIKSgz7JrZaQLXfwzwP8Ux0MLm4kjatjEMnb
 kLoYPJf7eO32QCfO2qIwEb2THtxpbLJijmEg/QdAjmshKyz4tQ1ecQ/7KjondDK5
 hTnRQsKqucBMH8BwZ9Wx49HRWjmzeZ41lzgHaOj8R2pXFQNmcfdogvh0+BuGanGP
 2vZCJGYqmr2h4HHcHLJdioZBEkrHSTnCsfu5GIjQq9jzLum4634TXxNq8MklzAEt
 YC8EvOam/LaaEyDdEDUXMlEeZszQHDL8Gr+F5JBM6iDF/1zq0bYSm0498/ShQbvk
 /oVPKQQSixwrEPSVvowpJacCT6kagOhiEaV1cAglRETZYWtZ5yIdwhvpOn4ur1Gq
 cQxpaMvzy2O6zy0HepMf/B+mr6So+gmWD+tiu1qfuRozJwdZ7ruUMAZBd2fv/+lC
 xWv42JV3CYp5sLy/7uld3UnLDGYRZjh20Cl8HETT1VIqvR5MPXG1HuuwbaAccO0h
 /nnVxDka5W4VccyN6vAXH+PzDKCOIu9Pke8CQDtNiYvotVlRiwFGVxwqubMsci7M
 xMfTKNsU4aw=
 =WXzD
 -----END PGP SIGNATURE-----

Merge tag 'x86-build-2025-05-25' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip

Pull x86 build updates from Ingo Molnar:
 "Add the CONFIG_X86_NATIVE_CPU=y build option to locally optimize the
  kernel build with '-march=native'"

* tag 'x86-build-2025-05-25' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip:
  x86/kbuild/64: Restrict clang versions that can use '-march=native'
  x86/kbuild/64: Test for the availability of the -mtune=native compiler flag
  x86/kbuild/64: Add the CONFIG_X86_NATIVE_CPU option to locally optimize the kernel with '-march=native'
This commit is contained in:
Linus Torvalds 2025-05-26 21:41:14 -07:00
commit 914873bc7d
2 changed files with 29 additions and 0 deletions

View file

@ -245,6 +245,30 @@ config MATOM
endchoice
config CC_HAS_MARCH_NATIVE
# This flag might not be available in cross-compilers:
def_bool $(cc-option, -march=native)
# LLVM 18 has an easily triggered internal compiler error in core
# networking code with '-march=native' on certain systems:
# https://github.com/llvm/llvm-project/issues/72026
# LLVM 19 introduces an optimization that resolves some high stack
# usage warnings that only appear wth '-march=native'.
depends on CC_IS_GCC || CLANG_VERSION >= 190100
config X86_NATIVE_CPU
bool "Build and optimize for local/native CPU"
depends on X86_64
depends on CC_HAS_MARCH_NATIVE
help
Optimize for the current CPU used to compile the kernel.
Use this option if you intend to build the kernel for your
local machine.
Note that such a kernel might not work optimally on a
different x86 machine.
If unsure, say N.
config X86_GENERIC
bool "Generic x86 support"
depends on X86_32

View file

@ -173,8 +173,13 @@ else
# Use -mskip-rax-setup if supported.
KBUILD_CFLAGS += $(call cc-option,-mskip-rax-setup)
ifdef CONFIG_X86_NATIVE_CPU
KBUILD_CFLAGS += -march=native
KBUILD_RUSTFLAGS += -Ctarget-cpu=native
else
KBUILD_CFLAGS += -march=x86-64 -mtune=generic
KBUILD_RUSTFLAGS += -Ctarget-cpu=x86-64 -Ztune-cpu=generic
endif
KBUILD_CFLAGS += -mno-red-zone
KBUILD_CFLAGS += -mcmodel=kernel