Skip to content

Commit

Permalink
(take 2) Detect when AVX is disabled via OSXSAVE (#1184)
Browse files Browse the repository at this point in the history
**Issue:**
There were some bugs in the previous PR: #1182

**Description of changes:**
* Fix bug where high and low bits of XGETBV were reversed
* Add back AVX2 feature detection (accidentally omitted from previous PR)
* VPCLMULQDQ also depends on AVX being usable
  • Loading branch information
graebm authored Jan 24, 2025
1 parent dac5ad6 commit 34013d5
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
10 changes: 5 additions & 5 deletions source/arch/intel/asm/cpuid.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ uint64_t aws_run_xgetbv(uint32_t xcr) {
/* NOTE: we could have used the _xgetbv() intrinsic in <immintrin.h>, but it's missing from GCC < 9.0:
* https://gcc.gnu.org/bugzilla/show_bug.cgi?id=71659 */

/* xgetbv writes high and low of 64bit value to EAX:EDX */
uint32_t eax;
uint32_t edx;
__asm__ __volatile__("xgetbv" : "=a"(eax), "=d"(edx) : "c"(xcr));
return (((uint64_t)eax) << 32) | edx;
/* xgetbv writes high and low of 64bit value to EDX:EAX */
uint32_t xcrhigh;
uint32_t xcrlow;
__asm__ __volatile__("xgetbv" : "=a"(xcrlow), "=d"(xcrhigh) : "c"(xcr));
return (((uint64_t)xcrhigh) << 32) | xcrlow;
}
7 changes: 5 additions & 2 deletions source/arch/intel/cpuid.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,7 @@ static void s_cache_cpu_features(void) {
return;
}
aws_run_cpuid(0x7, 0x0, abcd);
s_cpu_features[AWS_CPU_FEATURE_BMI2] = abcd[1] & (1 << 8); /* bmi2 = EBX[bit 8] */
s_cpu_features[AWS_CPU_FEATURE_VPCLMULQDQ] = abcd[2] & (1 << 10); /* vpclmulqdq = ECX[bit 10] */
s_cpu_features[AWS_CPU_FEATURE_BMI2] = abcd[1] & (1 << 8); /* bmi2 = EBX[bit 8] */

/* NOTE: It SHOULD be impossible for a CPU to support AVX2 without supporting AVX.
* But we've received crash reports where the AVX2 feature check passed
Expand All @@ -90,6 +89,10 @@ static void s_cache_cpu_features(void) {
* We don't know for sure what was up with those machines, but this extra
* check should stop them from running our AVX/AVX2 code paths. */
if (feature_avx) {
if (avx_usable) {
s_cpu_features[AWS_CPU_FEATURE_AVX2] = abcd[1] & (1 << 5); /* AVX2 = EBX[bit 5] */
s_cpu_features[AWS_CPU_FEATURE_VPCLMULQDQ] = abcd[2] & (1 << 10); /* vpclmulqdq = ECX[bit 10] */
}
if (avx512_usable) {
s_cpu_features[AWS_CPU_FEATURE_AVX512] = abcd[1] & (1 << 16); /* AVX-512 Foundation = EBX[bit 16] */
}
Expand Down

0 comments on commit 34013d5

Please sign in to comment.