Skip to content

Commit a815cd0

Browse files
daviesrobjkbonfield
authored andcommitted
Disable AVX512 on old versions of MacOS.
MacOS before 12.2 (a.k.a. Darwin 21.3) had a bug that could cause random failures on some AVX512 operations due to opmask registers not being restored correctly following interrupts. Add a Darwin version check when testing for AVX512 so it can be kept off where this might cause a problem. See https://community.intel.com/t5/Software-Tuning-Performance/MacOS-Darwin-kernel-bug-clobbers-AVX-512-opmask-register-state/m-p/1327259
1 parent df4617b commit a815cd0

File tree

1 file changed

+29
-1
lines changed

1 file changed

+29
-1
lines changed

htscodecs/rANS_static4x16pr.c

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -844,6 +844,33 @@ static int rans_cpu = 0xFFFF; // all
844844
# define UNUSED
845845
#endif
846846

847+
#if defined(__APPLE__)
848+
/*
849+
MacOS before 12.2 (a.k.a. Darwin 21.3) had a bug that could cause random
850+
failures on some AVX512 operations due to opmask registers not being restored
851+
correctly following interrupts. For simplicity, check that the major version
852+
is 22 or higher before using AVX512.
853+
See https://community.intel.com/t5/Software-Tuning-Performance/MacOS-Darwin-kernel-bug-clobbers-AVX-512-opmask-register-state/m-p/1327259
854+
*/
855+
856+
#include <sys/utsname.h>
857+
static inline int not_ancient_darwin(void) {
858+
static long version = 0;
859+
if (!version) {
860+
struct utsname uname_info;
861+
if (uname(&uname_info) == 0) {
862+
version = strtol(uname_info.release, NULL, 10);
863+
}
864+
}
865+
return version >= 22;
866+
}
867+
#else
868+
static inline int not_ancient_darwin(void) {
869+
return 1;
870+
}
871+
#endif
872+
873+
847874
// CPU detection is performed once. NB this has an assumption that we're
848875
// not migrating between processes with different instruction stes, but
849876
// to date the only systems I know of that support this don't have different
@@ -916,7 +943,8 @@ static void htscodecs_tls_cpu_init(void) {
916943
#endif
917944
#if defined(bit_AVX512F)
918945
// AVX512 depends on bits 5:7 of XCR0
919-
if ((xcr0 & xcr0_can_use_avx512) == xcr0_can_use_avx512)
946+
if ((xcr0 & xcr0_can_use_avx512) == xcr0_can_use_avx512
947+
&& not_ancient_darwin())
920948
have_avx512f = ebx & bit_AVX512F;
921949
#endif
922950
}

0 commit comments

Comments
 (0)