fix(cmake): resolve universal binary and multi-arch macro collision on Darwin (#223) - #254
Open
wittkung wants to merge 1 commit into
Open
fix(cmake): resolve universal binary and multi-arch macro collision on Darwin (#223)#254wittkung wants to merge 1 commit into
wittkung wants to merge 1 commit into
Conversation
|
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
Author
|
@googlebot I signed it! |
…n Darwin When building macOS universal binaries (e.g. -DCMAKE_OSX_ARCHITECTURES="arm64;x86_64"), CMake's single-pass feature detection probes the host compiler, which incorrectly disables target-specific SIMD macros (SNAPPY_HAVE_NEON, SNAPPY_HAVE_SSSE3, etc.) across all slices. Override configure-time single-architecture probes on Apple platforms with compiler-defined target architecture macros (__arm64__, __x86_64__, __SSSE3__, __SSE4_2__, __BMI2__, __ARM_FEATURE_CRC32) so that each slice is compiled with native hardware acceleration.
wittkung
force-pushed
the
fix/darwin-universal-binary-arch-macros
branch
from
August 18, 2026 09:47
79bacc8 to
1d982e1
Compare
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.
Summary
Resolve Universal Binary (
arm64;x86_64) SIMD feature macro collision on Apple/Darwin platforms (Fixes #223).Background & Appreciation
Historical Context & Prior Decisions (#223)
In Issue #223 ("Unable to create universal binaries"), community members highlighted the difficulty of producing multi-architecture fat binaries on macOS (such as
x86_64+arm64for Mac App Store distribution, universal CLI tooling, or iOS/macOS frameworks).Historically, Snappy transitioned from Autotools to CMake, adopting
check_cxx_source_compiles()inCMakeLists.txtto probe for hardware instruction extensions at configure time:SNAPPY_HAVE_SSSE3(probes<tmmintrin.h>for_mm_shuffle_epi8)SNAPPY_HAVE_X86_CRC32(probes<immintrin.h>for_mm_crc32_u32)SNAPPY_HAVE_BMI2(probes<immintrin.h>for_bzhi_u32)SNAPPY_HAVE_NEON(probes<arm_neon.h>forvqtbl1q_u8)SNAPPY_HAVE_NEON_CRC32(probes<arm_acle.h>for__crc32cw)The configure-time results are then stamped as boolean constants into
cmake/config.h.invia#cmakedefine01.Root Cause Analysis
When CMake is configured for Universal Binaries (e.g.
-DCMAKE_OSX_ARCHITECTURES="arm64;x86_64"), Apple Clang is invoked with-arch arm64 -arch x86_64simultaneously during eachcheck_cxx_source_compiles()test:The ARM64 NEON Probe:
#include <arm_neon.h>for both slices.arm64, thex86_64slice fails compilation immediately (fatal error: 'arm_neon.h' file not found).#define SNAPPY_HAVE_NEON 0and#define SNAPPY_HAVE_NEON_CRC32 0.The x86 SSSE3 / SSE4.2 / BMI2 Probes:
#include <tmmintrin.h>for both slices.x86_64, thearm64slice fails compilation (fatal error: 'tmmintrin.h' file not found).#define SNAPPY_HAVE_SSSE3 0,#define SNAPPY_HAVE_X86_CRC32 0, and#define SNAPPY_HAVE_BMI2 0.The Consequence:
Every macOS Universal Binary built via standard CMake silently had all hardware acceleration stripped out for both architectures. Both Apple Silicon and Intel slices silently fell back to unaccelerated scalar C++, resulting in a 50% to 80% throughput penalty without any build warning or error.
Proposed Solution
In
cmake/config.h.in, whendefined(__APPLE__)is detected, we override the configure-time single-pass boolean flags with compiler-builtin target architecture macros (__arm64__,__x86_64__,__SSSE3__,__SSE4_2__,__BMI2__,__ARM_FEATURE_CRC32):Key Properties & Invariants
#cmakedefine01behavior is 100% untouched.tbl.16b/vqtbl1q_u8on ARM64,pshufb/_mm_shuffle_epi8on x86_64).__BMI2__and__SSE4_2__are enabled only when target architecture flags are explicitly passed or enabled by the toolchain, avoiding illegal instruction faults on legacy hardware).-DCMAKE_OSX_ARCHITECTURESusing the combined compiler invocation, resolving multi-slice hardware features at compile-time viaconfig.h.inis the established canonical pattern across top-tier portable C/C++ libraries (consistent with projects like zlib-ng, libjpeg-turbo, and libdeflate).Verification / How Has This Been Tested
1. Universal Binary Build & Architecture Inspection
2. Disassembly & Hardware Instruction Preservation Assertion
3. Native Regression Suite
Happy to make any adjustments or refine formatting according to maintainer preferences!