Skip to content

Commit 914873b

Browse files
committed
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'
2 parents 24244df + ad9b861 commit 914873b

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

arch/x86/Kconfig.cpu

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,30 @@ config MATOM
245245

246246
endchoice
247247

248+
config CC_HAS_MARCH_NATIVE
249+
# This flag might not be available in cross-compilers:
250+
def_bool $(cc-option, -march=native)
251+
# LLVM 18 has an easily triggered internal compiler error in core
252+
# networking code with '-march=native' on certain systems:
253+
# https://github.com/llvm/llvm-project/issues/72026
254+
# LLVM 19 introduces an optimization that resolves some high stack
255+
# usage warnings that only appear wth '-march=native'.
256+
depends on CC_IS_GCC || CLANG_VERSION >= 190100
257+
258+
config X86_NATIVE_CPU
259+
bool "Build and optimize for local/native CPU"
260+
depends on X86_64
261+
depends on CC_HAS_MARCH_NATIVE
262+
help
263+
Optimize for the current CPU used to compile the kernel.
264+
Use this option if you intend to build the kernel for your
265+
local machine.
266+
267+
Note that such a kernel might not work optimally on a
268+
different x86 machine.
269+
270+
If unsure, say N.
271+
248272
config X86_GENERIC
249273
bool "Generic x86 support"
250274
depends on X86_32

arch/x86/Makefile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,8 +173,13 @@ else
173173
# Use -mskip-rax-setup if supported.
174174
KBUILD_CFLAGS += $(call cc-option,-mskip-rax-setup)
175175

176+
ifdef CONFIG_X86_NATIVE_CPU
177+
KBUILD_CFLAGS += -march=native
178+
KBUILD_RUSTFLAGS += -Ctarget-cpu=native
179+
else
176180
KBUILD_CFLAGS += -march=x86-64 -mtune=generic
177181
KBUILD_RUSTFLAGS += -Ctarget-cpu=x86-64 -Ztune-cpu=generic
182+
endif
178183

179184
KBUILD_CFLAGS += -mno-red-zone
180185
KBUILD_CFLAGS += -mcmodel=kernel

0 commit comments

Comments
 (0)