Skip to content

Commit

Permalink
Use minipal_getcpufeatures to detect for AVX (#113032)
Browse files Browse the repository at this point in the history
  • Loading branch information
cshung authored Mar 4, 2025
1 parent 9aa8d95 commit 6d344b3
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 77 deletions.
1 change: 1 addition & 0 deletions src/coreclr/gc/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ set (GC_LINK_LIBRARIES ${GC_LINK_LIBRARIES} gc_pal)
if(CLR_CMAKE_TARGET_ARCH_AMD64)
list(APPEND GC_LINK_LIBRARIES
gc_vxsort
minipal
)
endif(CLR_CMAKE_TARGET_ARCH_AMD64)

Expand Down
4 changes: 3 additions & 1 deletion src/coreclr/gc/sample/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ if(CLR_CMAKE_TARGET_WIN32)
${STATIC_MT_CRT_LIB}
${STATIC_MT_VCRT_LIB}
kernel32.lib
advapi32.lib)
advapi32.lib
minipal
)
endif(CLR_CMAKE_TARGET_WIN32)

add_definitions(-DVERIFY_HEAP)
Expand Down
81 changes: 5 additions & 76 deletions src/coreclr/gc/vxsort/isa_detection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,92 +2,23 @@
// The .NET Foundation licenses this file to you under the MIT license.

#include "common.h"

#ifdef TARGET_WINDOWS
#include <intrin.h>
#include <windows.h>
#endif

#include "do_vxsort.h"

#include <minipal/cpufeatures.h>

enum class SupportedISA
{
None = 0,
AVX2 = 1 << (int)InstructionSet::AVX2,
AVX512F = 1 << (int)InstructionSet::AVX512F
};

#if defined(TARGET_AMD64) && defined(TARGET_WINDOWS)

SupportedISA DetermineSupportedISA()
{
// register definitions to make the following code more readable
enum reg
{
EAX = 0,
EBX = 1,
ECX = 2,
EDX = 3,
COUNT = 4
};

// bit definitions to make code more readable
enum bits
{
OCXSAVE = 1<<27,
AVX = 1<<28,
AVX2 = 1<< 5,
AVX512F = 1<<16,
AVX512DQ = 1<<17,
};
int reg[COUNT];

__cpuid(reg, 0);
if (reg[EAX] < 7)
return SupportedISA::None;

__cpuid(reg, 1);

// both AVX and OCXSAVE feature flags must be enabled
if ((reg[ECX] & (OCXSAVE|AVX)) != (OCXSAVE | AVX))
return SupportedISA::None;

// get xcr0 register
DWORD64 xcr0 = _xgetbv(0);

// get OS XState info
DWORD64 FeatureMask = GetEnabledXStateFeatures();

// get processor extended feature flag info
__cpuidex(reg, 7, 0);

// check if all of AVX2, AVX512F and AVX512DQ are supported by both processor and OS
if ((reg[EBX] & (AVX2 | AVX512F | AVX512DQ)) == (AVX2 | AVX512F | AVX512DQ) &&
(xcr0 & 0xe6) == 0xe6 &&
(FeatureMask & (XSTATE_MASK_AVX | XSTATE_MASK_AVX512)) == (XSTATE_MASK_AVX | XSTATE_MASK_AVX512))
{
return (SupportedISA)((int)SupportedISA::AVX2 | (int)SupportedISA::AVX512F);
}

// check if AVX2 is supported by both processor and OS
if ((reg[EBX] & AVX2) &&
(xcr0 & 0x06) == 0x06 &&
(FeatureMask & XSTATE_MASK_AVX) == XSTATE_MASK_AVX)
{
return SupportedISA::AVX2;
}

return SupportedISA::None;
}

#elif defined(TARGET_UNIX)

SupportedISA DetermineSupportedISA()
{
__builtin_cpu_init();
if (__builtin_cpu_supports("avx2"))
int cpuFeatures = minipal_getcpufeatures();
if ((cpuFeatures & XArchIntrinsicConstants_Avx2) != 0)
{
if (__builtin_cpu_supports("avx512f"))
if ((cpuFeatures & XArchIntrinsicConstants_Avx512) != 0)
return (SupportedISA)((int)SupportedISA::AVX2 | (int)SupportedISA::AVX512F);
else
return SupportedISA::AVX2;
Expand All @@ -98,8 +29,6 @@ SupportedISA DetermineSupportedISA()
}
}

#endif // defined(TARGET_UNIX)

static bool s_initialized;
static SupportedISA s_supportedISA;

Expand Down

0 comments on commit 6d344b3

Please sign in to comment.