Skip to content

Commit

Permalink
AVX-512 detection for AMD
Browse files Browse the repository at this point in the history
  • Loading branch information
sadko4u committed Dec 12, 2024
1 parent 5fda461 commit b2cf1ea
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 0 deletions.
10 changes: 10 additions & 0 deletions include/private/dsp/arch/x86/cpuid.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,16 @@
#define X86_CPUID7_INTEL_ECX_AVX512VBMI (1 << 1)

#define X86_CPUID7_AMD_EBX_AVX2 (1 << 5)
#define X86_CPUID7_AMD_EBX_AVX512F (1 << 16)
#define X86_CPUID7_AMD_EBX_AVX512DQ (1 << 17)
#define X86_CPUID7_AMD_EBX_AVX512IFMA (1 << 21)
#define X86_CPUID7_AMD_EBX_AVX512PF (1 << 26)
#define X86_CPUID7_AMD_EBX_AVX512ER (1 << 27)
#define X86_CPUID7_AMD_EBX_AVX512CD (1 << 28)
#define X86_CPUID7_AMD_EBX_AVX512BW (1 << 30)
#define X86_CPUID7_AMD_EBX_AVX512VL (1 << 31)

#define X86_CPUID7_AMD_ECX_AVX512VBMI (1 << 1)

//-------------------------------------------------------------------------
// Function 80000001
Expand Down
1 change: 1 addition & 0 deletions include/private/dsp/arch/x86/features.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@
FEAT_FAST_MOVS, // Processor implements optimized MOVS instruction
FEAT_FAST_AVX, // Fast AVX implementation
FEAT_FAST_FMA3, // Fast FMA3 implementation
FEAT_FAST_AVX512, // Fast AVX-512 implementation
FEAT_BELOW_ZEN3 // CPU has AMD architecture and is below Zen3
};

Expand Down
5 changes: 5 additions & 0 deletions src/main/x86/avx512.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@

void dsp_init(const cpu_features_t *f)
{
// Enable AVX-512 only for CPUs that really support it well
const bool favx512 = feature_check(f, FEAT_FAST_AVX512);
if (!favx512)
return;

const bool vl = (f->features & (CPU_OPTION_AVX512F | CPU_OPTION_AVX512VL)) ==
(CPU_OPTION_AVX512F | CPU_OPTION_AVX512VL);

Expand Down
38 changes: 38 additions & 0 deletions src/main/x86/x86.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,30 @@
if (info.ebx & X86_CPUID7_AMD_EBX_AVX2)
f->features |= CPU_OPTION_AVX2;
}

// Additional check for AVX512 support
if ((xcr0 & XCR_FLAGS_AVX512) == XCR_FLAGS_AVX512)
{
if (info.ebx & X86_CPUID7_AMD_EBX_AVX512F)
f->features |= CPU_OPTION_AVX512F;
if (info.ebx & X86_CPUID7_AMD_EBX_AVX512DQ)
f->features |= CPU_OPTION_AVX512DQ;
if (info.ebx & X86_CPUID7_AMD_EBX_AVX512IFMA)
f->features |= CPU_OPTION_AVX512IFMA;
if (info.ebx & X86_CPUID7_AMD_EBX_AVX512PF)
f->features |= CPU_OPTION_AVX512PF;
if (info.ebx & X86_CPUID7_AMD_EBX_AVX512ER)
f->features |= CPU_OPTION_AVX512ER;
if (info.ebx & X86_CPUID7_AMD_EBX_AVX512CD)
f->features |= CPU_OPTION_AVX512CD;
if (info.ebx & X86_CPUID7_AMD_EBX_AVX512BW)
f->features |= CPU_OPTION_AVX512BW;
if (info.ebx & X86_CPUID7_AMD_EBX_AVX512VL)
f->features |= CPU_OPTION_AVX512VL;

if (info.ecx & X86_CPUID7_AMD_ECX_AVX512VBMI)
f->features |= CPU_OPTION_AVX512VBMI;
}
}

// FUNCTION 0x80000001
Expand Down Expand Up @@ -542,6 +566,20 @@
}
break;

case FEAT_FAST_AVX512:
if (f->vendor == CPU_VENDOR_INTEL) // Any Intel CPU seems to be good enough with AVX-512
return true;
// Only starting with ZEN 1 architecture AMD's implementation of AVX is fast enough
if ((f->vendor == CPU_VENDOR_AMD) || (f->vendor == CPU_VENDOR_HYGON))
{
if (f->family < AMD_FAMILY_ZEN_3_4)
return false;
if (f->family == AMD_FAMILY_DHYANA)
return false;
return true;
}
break;

case FEAT_BELOW_ZEN3: // Test that this is AMD and below Zen 3 architecture
if ((f->vendor == CPU_VENDOR_AMD) || (f->vendor == CPU_VENDOR_HYGON))
{
Expand Down

0 comments on commit b2cf1ea

Please sign in to comment.