Skip to content

Fix AVX detection #6460

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions Zend/zend_cpuinfo.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,38 @@ static void __zend_cpuid(uint32_t func, uint32_t subfunc, zend_cpu_info *cpuinfo
}
#endif

/* Function based on compiler-rt implementation. */
static unsigned get_xcr0_eax() {
#if defined(__GNUC__) || defined(__clang__)
// Check xgetbv; this uses a .byte sequence instead of the instruction
// directly because older assemblers do not include support for xgetbv and
// there is no easy way to conditionally compile based on the assembler used.
unsigned eax, edx;
__asm__(".byte 0x0f, 0x01, 0xd0" : "=a"(eax), "=d"(edx) : "c"(0));
return eax;
#elif defined(ZEND_WIN32) && defined(_XCR_XFEATURE_ENABLED_MASK)
return _xgetbv(_XCR_XFEATURE_ENABLED_MASK);
#else
return 0;
#endif
}

static zend_bool is_avx_supported() {
if (!(cpuinfo.ecx & ZEND_CPU_FEATURE_AVX)) {
/* No support for AVX */
return 0;
}
if (!(cpuinfo.ecx & ZEND_CPU_FEATURE_OSXSAVE)) {
/* The operating system does not support XSAVE. */
return 0;
}
if ((get_xcr0_eax() & 0x6) != 0x6) {
/* XCR0 SSE and AVX bits must be set. */
return 0;
}
return 1;
}

void zend_cpu_startup(void)
{
if (!cpuinfo.initialized) {
Expand All @@ -95,6 +127,11 @@ void zend_cpu_startup(void)
} else {
cpuinfo.ebx = 0;
}

if (!is_avx_supported()) {
cpuinfo.edx &= ~ZEND_CPU_FEATURE_AVX;
cpuinfo.ebx &= ~(ZEND_CPU_FEATURE_AVX2 & ~ZEND_CPU_EBX_MASK);
}
}
}

Expand Down