Skip to content

Commit 88d0241

Browse files
authored
Fix for macOS 10.13 (error: instruction requires: AVX-512 ISA) (#400)
* Fix for macOS 10.13 and clang-6 issues with AVX-512 instructions. * Renamed USE_AVX512 macro to more correct COMPILER_SUPPORTS_AVX512.
1 parent 9678580 commit 88d0241

File tree

1 file changed

+23
-3
lines changed

1 file changed

+23
-3
lines changed

cbits/measure_off.c

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,27 @@
1717
#include <stdatomic.h>
1818
#endif
1919

20+
/*
21+
Clang-6 does not enable proper -march flags for assembly modules
22+
which leads to "error: instruction requires: AVX-512 ISA"
23+
at the assembler phase.
24+
25+
Apple LLVM version 10.0.0 (clang-1000.11.45.5) is based on clang-6
26+
https://en.wikipedia.org/wiki/Xcode#Toolchain_versions
27+
and it's the latest available version on macOS 10.13.
28+
29+
Disable AVX-512 instructions as they are most likely not supported
30+
on the hardware running clang-6.
31+
*/
32+
#if !((defined(__apple_build_version__) && __apple_build_version__ <= 10001145) \
33+
|| (defined(__clang_major__) && __clang_major__ <= 6))
34+
#define COMPILER_SUPPORTS_AVX512
35+
#endif
36+
37+
38+
#if defined(__x86_64__) && defined(COMPILER_SUPPORTS_AVX512)
2039
bool has_avx512_vl_bw() {
21-
#if defined(__x86_64__) && (__GNUC__ >= 6 || defined(__clang_major__))
40+
#if __GNUC__ >= 6 || defined(__clang_major__)
2241
uint32_t eax = 0, ebx = 0, ecx = 0, edx = 0;
2342
__get_cpuid_count(7, 0, &eax, &ebx, &ecx, &edx);
2443
// https://en.wikipedia.org/wiki/CPUID#EAX=7,_ECX=0:_Extended_Features
@@ -30,6 +49,7 @@ bool has_avx512_vl_bw() {
3049
return false;
3150
#endif
3251
}
52+
#endif
3353

3454
/*
3555
measure_off_naive / measure_off_avx / measure_off_sse
@@ -68,7 +88,7 @@ static inline const ssize_t measure_off_naive(const uint8_t *src, const uint8_t
6888
return cnt == 0 ? (ssize_t)(srcend - src) : (ssize_t)(- cnt);
6989
}
7090

71-
#ifdef __x86_64__
91+
#if defined(__x86_64__) && defined(COMPILER_SUPPORTS_AVX512)
7292
__attribute__((target("avx512vl,avx512bw")))
7393
static const ssize_t measure_off_avx(const uint8_t *src, const uint8_t *srcend, size_t cnt)
7494
{
@@ -146,7 +166,7 @@ ssize_t _hs_text_measure_off(const uint8_t *src, size_t off, size_t len, size_t
146166
static _Atomic measure_off_t s_impl = (measure_off_t)NULL;
147167
measure_off_t impl = atomic_load_explicit(&s_impl, memory_order_relaxed);
148168
if (!impl) {
149-
#ifdef __x86_64__
169+
#if defined(__x86_64__) && defined(COMPILER_SUPPORTS_AVX512)
150170
impl = has_avx512_vl_bw() ? measure_off_avx : measure_off_sse;
151171
#else
152172
impl = measure_off_sse;

0 commit comments

Comments
 (0)