How to compile arch kernel with -march=native?

I am struggling to compile a customized arch kernel with specific AMD optimizations with -march=znver2

Whatever I do, it ends up compiling it with -march=x86-64 -mtune=generic

I followed the instructions given here: https://wiki.archlinux.org/title/Kernel/Traditional_compilation
Where it suggests to edit arch/x86/Makefile. I tried that, but somehow this file gets overwritten back to the default settings every time I run makepkg.

I also tried it with these config options:

scripts/config --disable CONFIG_GENERIC_CPU
scripts/config --enable  CONFIG_MZEN2

This works fine for linux-zen but not for the vanilla arch kernel. Something is special about the arch kernel.

The AUR package linux-amd-znver2 is not an option because it uses the unpatched kernel from kernel.org. And I tried to use the same build instructions for the vanilla arch kernel:

build() {
  cd "${_srcname}"
  
  #Force zenv2 architecture optimisation.
  export CFLAGS="-march=znver2 -mtune=znver2 -O2 -pipe -fstack-protector-strong"
  export CXXFLAGS="${CFLAGS}"  
  make ${MAKEFLAGS} LOCALVERSION= bzImage modules
}

But that does not work either. Any idea what I need to do to get a vanilla arch kernel with AMD optimizations?

But then you use ABS (makepkg) and not manual / traditional compilation I assume?

Each time you run makepkg it will extract the sources again. If you want to modify sources prior to build, you could first run “makepkg -o” to extract the sources and then run “makepkg -e” to build after modifying source files.

Yes, arch uses the vanilla kernel sources. There is no CONFIG_MZEN2 in them.
In the linux-zen kernel they have this though.

In my kernel build files I use the graysky2 patch. With that you can use CONFIG_MZEN2 (which in turn makes it compiling with -march=znver2)

Here my current files (5.14.1):

PKGBUILD
config
cpu_patch

GenToo also use a kernel patch, which is adding more CPU options to choose, including native instead of e.g. skylake.

genpatches-(experimental) >>> enable-cpu-optimizations-universal-patch

GenToo Optional Kernel Patches

1 Like

Yeah. Actually it is the graysky2 one mentioned above.

https://gitweb.gentoo.org/proj/linux-patches.git/tree/5010_enable-cpu-optimizations-universal.patch?h=5.14

2 Likes

i missed that :crazy_face:

I forgot to close this thread with solution. The solution has first been mentioned by @moson :
A kernel patch is needed.

I personally use the patch that is also being used by linux-zen:

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.