Makefile: native-tuned default build (clang + -march=native) for plain 'make' (+60% XXH3/XXH128 on AVX-512 hosts) - #1124
Open
ksenxx wants to merge 1 commit into
Open
Conversation
…n 'make' For the fully-default invocation only (no CC=, no CFLAGS=, and no runtime-dispatch build requested), prefer clang when available and add -march=native to the default CFLAGS. On an AVX-512 host this lifts the official benchmark (xxhsum -b, 100KB, pinned, best of 5) from 36.7 GB/s to 58.8 GB/s for XXH3_64b (+60%) and from 36.5 GB/s to 58.5 GB/s for XXH128 (+60%); XXH32/XXH64 are unchanged. Hash values are bit-identical in every configuration. Any explicit choice restores exact upstream behavior: CC=... and CFLAGS=... are never modified, and supplying either turns the tuning off. DISPATCH=1 (or the 'dispatch' target, or LIBXXH_DISPATCH=1) keeps the portable runtime-dispatch build and never receives -march=native; when native tuning is off, DISPATCH still defaults to 1 on x86/x64 exactly like today. Guards: NATIVE=0 opt-out (NATIVE=1 force-request); -march=native is skipped for cross-compilation (-target/--target/-arch/-m32/-mx32/-m16 anywhere in CC/CFLAGS/CPPFLAGS/MOREFLAGS, or CC -dumpmachine differing from uname -m), for compilers that reject the flag, and for multi-word or shell-unsafe CC values (probes only ever execute a plain single-word CC). Also fix gcc-og-test to pin CC=gcc so it keeps testing gcc when the default compiler is substituted. make check passes for default, CC=gcc, NATIVE=0, DISPATCH=1, and explicit-CFLAGS configurations.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Makefile-only change: for the fully-default invocation (plain
make, with noCC=, noCFLAGS=, and no runtime-dispatch build requested), the build nowclang(fallbackclang-18) over the built-inccdefault when clang is installed, and-march=nativeto the defaultCFLAGS(-O3 -march=native), so the whole library — not just the target-attribute-compiled dispatch unit — is compiled for the host ISA.No library source line is touched, and hash values are bit-identical in every configuration.
Why
On AVX-512 hardware, the portable default (SSE2 baseline + runtime dispatch) leaves a large amount of XXH3/XXH128 throughput on the table. Measured with the official benchmark (
xxhsum -b, 100KB,taskset-pinned, best of 5) on an Intel Xeon (Cascade Lake, AVX-512), Ubuntu 24.04, GCC 13.3.0 / clang 18.1.3:make(upstream)make(this PR)About 44 of those 60 points come from
-march=native(AVX-512 code paths for the whole build), and the rest from clang generating a tighter AVX-512 XXH3 kernel than GCC 13 on this class of CPU.Behavior contract (what does not change)
The tuning applies only when the user has expressed no preference at all. Any explicit choice restores exact upstream behavior:
CC=...(command line or environment): no compiler substitution, no-march=native, upstreamDISPATCHdefault.CFLAGS=...: never modified; supplying it also turns the tuning off and restores the upstreamDISPATCHdefault.DISPATCH=1,LIBXXH_DISPATCH=1, or thedispatchtarget: portable runtime-dispatch build, never receives-march=native(this intentionally overrides even an explicitNATIVE=1).DISPATCHstill defaults to1on x86/x64 exactly like today. When native tuning is active it defaults to0, since the whole binary already targets the host ISA.Knobs:
NATIVE=0— never add-march=native(the right setting beforemake installwhen binaries will be copied to other, possibly older, machines).NATIVE=1— request native tuning even together with an explicitCC=....Safety guards
-march=nativeis skipped automatically whenever it can't or shouldn't be used:--target=/-target/-arch/-m32/-mx32/-m16option anywhere inCC/CFLAGS/CPPFLAGS/MOREFLAGS, or$(CC) -dumpmachinereporting a machine different fromuname -m;-march=native(probed with a null compile);CCvalue — the probes only ever execute a plain single-wordCC, checked with pure make string functions (no shell involved), and probe output is sanitized throughtrbefore make compares it.The
CCsubstitution is exported so recursive makes (tests/,tests/bench,tests/collisions) build with the same compiler as the top-level artifacts. One small related fix:gcc-og-testnow pinsCC=gccso it keeps testing GCC when the default compiler is substituted.Testing
make checkpasses for: default,CC=gcc,NATIVE=0,DISPATCH=1, andCFLAGS=-O2configurations.NATIVE=0escape hatch.I realize changing the default build's portability is a real tradeoff (distro packagers and anyone running
make installfor another machine must now passNATIVE=0or explicitCFLAGS, as they typically already do). If you'd prefer the conservative variant — keeping upstream defaults and only adding theNATIVE=1opt-in knob plus the guards — I'm happy to rework the PR that way; the mechanism is identical and the same +60% is then one flag away.