Skip to content

Commit

Permalink
undef hardware feature flags if the hardware will never supports it
Browse files Browse the repository at this point in the history
Fixes issue with building MacOS universal2
  • Loading branch information
ashbob999 committed Sep 21, 2024
1 parent 29f9bb4 commit c42ea5e
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 3 deletions.
33 changes: 33 additions & 0 deletions include/stringzilla/stringzilla.h
Original file line number Diff line number Diff line change
Expand Up @@ -1108,24 +1108,30 @@ SZ_PUBLIC void sz_sort_intro(sz_sequence_t *sequence, sz_sequence_comparator_t l
#ifndef SZ_USE_X86_AVX512
#ifdef __AVX512BW__
#define SZ_USE_X86_AVX512 1
#pragma message("SZ_USE_X86_AVX512")
#else
#define SZ_USE_X86_AVX512 0
#pragma message("NOT SZ_USE_X86_AVX512")
#endif
#endif

#ifndef SZ_USE_X86_AVX2
#ifdef __AVX2__
#define SZ_USE_X86_AVX2 1
#pragma message("SZ_USE_X86_AVX2")
#else
#define SZ_USE_X86_AVX2 0
#pragma message("NOT SZ_USE_X86_AVX2")
#endif
#endif

#ifndef SZ_USE_ARM_NEON
#if defined(__ARM_NEON) || defined(_M_ARM64)
#define SZ_USE_ARM_NEON 1
#pragma message("SZ_USE_ARM_NEON")
#else
#define SZ_USE_ARM_NEON 0
#pragma message("NOT SZ_USE_ARM_NEON")
#endif
#endif

Expand All @@ -1134,6 +1140,33 @@ SZ_PUBLIC void sz_sort_intro(sz_sequence_t *sequence, sz_sequence_comparator_t l
#define SZ_USE_ARM_SVE 1
#else
#define SZ_USE_ARM_SVE 0
#pragma message("NOT SZ_USE_ARM_SVE")
#endif
#endif

// Undef the hardware features if the build target never supports them, mainly used when building MacOS universal2 because
// then both x86 & arm feature flags can be set
#ifndef SZ_TARGET_X86
#if !(defined(__i386__) || defined(__amd64__) || defined(_M_IX86) || defined(_M_AMD64))
#undef SZ_USE_X86_AVX2
#define SZ_USE_X86_AVX2 0
#undef SZ_USE_X86_AVX512
#define SZ_USE_X86_AVX512 0
#pragma message("NOT SZ_TARGET_X86")
#else
#pragma message("SZ_TARGET_X86")
#endif
#endif

#ifndef SZ_TARGET_ARM
#if !(defined(__arm__) || defined(__aarch64__) || defined(_M_ARM) || defined(_M_ARM64))
#undef SZ_USE_ARM_NEON
#define SZ_USE_ARM_NEON 0
#undef SZ_USE_ARM_SVE
#define SZ_USE_ARM_SVE 0
#pragma message("NOT SZ_TARGET_ARM")
#else
#pragma message("SZ_TARGET_ARM")
#endif
#endif

Expand Down
9 changes: 6 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,16 +80,19 @@ def darwin_settings() -> Tuple[List[str], List[str], List[Tuple[str]]]:
"-fPIC", # to enable dynamic dispatch
]

print("system", sysconfig.get_platform())

# Apple Clang doesn't support the `-march=native` argument,
# so we must pre-set the CPU generation. Technically the last Intel-based Apple
# product was the 2021 MacBook Pro, which had the "Coffee Lake" architecture.
# During Universal builds, however, even AVX header cause compilation errors.
can_use_avx2 = is_64bit_x86() and sysconfig.get_platform().startswith("universal")
is_building_x86 = is_64bit_x86() and "universal" in sysconfig.get_platform()
is_building_arm = is_64bit_arm() and "universal" in sysconfig.get_platform()
macros_args = [
("SZ_USE_X86_AVX512", "0"),
("SZ_USE_X86_AVX2", "1" if can_use_avx2 else "0"),
("SZ_USE_X86_AVX2", "1" if is_building_x86 else "0"),
("SZ_USE_ARM_SVE", "0"),
("SZ_USE_ARM_NEON", "1" if is_64bit_arm() else "0"),
("SZ_USE_ARM_NEON", "1" if is_building_arm else "0"),
]

return compile_args, link_args, macros_args
Expand Down

0 comments on commit c42ea5e

Please sign in to comment.