Skip to content

Commit 6d344b3

Browse files
authored
Use minipal_getcpufeatures to detect for AVX (#113032)
1 parent 9aa8d95 commit 6d344b3

File tree

3 files changed

+9
-77
lines changed

3 files changed

+9
-77
lines changed

src/coreclr/gc/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ set (GC_LINK_LIBRARIES ${GC_LINK_LIBRARIES} gc_pal)
9090
if(CLR_CMAKE_TARGET_ARCH_AMD64)
9191
list(APPEND GC_LINK_LIBRARIES
9292
gc_vxsort
93+
minipal
9394
)
9495
endif(CLR_CMAKE_TARGET_ARCH_AMD64)
9596

src/coreclr/gc/sample/CMakeLists.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,9 @@ if(CLR_CMAKE_TARGET_WIN32)
4444
${STATIC_MT_CRT_LIB}
4545
${STATIC_MT_VCRT_LIB}
4646
kernel32.lib
47-
advapi32.lib)
47+
advapi32.lib
48+
minipal
49+
)
4850
endif(CLR_CMAKE_TARGET_WIN32)
4951

5052
add_definitions(-DVERIFY_HEAP)

src/coreclr/gc/vxsort/isa_detection.cpp

Lines changed: 5 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -2,92 +2,23 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
#include "common.h"
5-
6-
#ifdef TARGET_WINDOWS
7-
#include <intrin.h>
8-
#include <windows.h>
9-
#endif
10-
115
#include "do_vxsort.h"
126

7+
#include <minipal/cpufeatures.h>
8+
139
enum class SupportedISA
1410
{
1511
None = 0,
1612
AVX2 = 1 << (int)InstructionSet::AVX2,
1713
AVX512F = 1 << (int)InstructionSet::AVX512F
1814
};
1915

20-
#if defined(TARGET_AMD64) && defined(TARGET_WINDOWS)
21-
22-
SupportedISA DetermineSupportedISA()
23-
{
24-
// register definitions to make the following code more readable
25-
enum reg
26-
{
27-
EAX = 0,
28-
EBX = 1,
29-
ECX = 2,
30-
EDX = 3,
31-
COUNT = 4
32-
};
33-
34-
// bit definitions to make code more readable
35-
enum bits
36-
{
37-
OCXSAVE = 1<<27,
38-
AVX = 1<<28,
39-
AVX2 = 1<< 5,
40-
AVX512F = 1<<16,
41-
AVX512DQ = 1<<17,
42-
};
43-
int reg[COUNT];
44-
45-
__cpuid(reg, 0);
46-
if (reg[EAX] < 7)
47-
return SupportedISA::None;
48-
49-
__cpuid(reg, 1);
50-
51-
// both AVX and OCXSAVE feature flags must be enabled
52-
if ((reg[ECX] & (OCXSAVE|AVX)) != (OCXSAVE | AVX))
53-
return SupportedISA::None;
54-
55-
// get xcr0 register
56-
DWORD64 xcr0 = _xgetbv(0);
57-
58-
// get OS XState info
59-
DWORD64 FeatureMask = GetEnabledXStateFeatures();
60-
61-
// get processor extended feature flag info
62-
__cpuidex(reg, 7, 0);
63-
64-
// check if all of AVX2, AVX512F and AVX512DQ are supported by both processor and OS
65-
if ((reg[EBX] & (AVX2 | AVX512F | AVX512DQ)) == (AVX2 | AVX512F | AVX512DQ) &&
66-
(xcr0 & 0xe6) == 0xe6 &&
67-
(FeatureMask & (XSTATE_MASK_AVX | XSTATE_MASK_AVX512)) == (XSTATE_MASK_AVX | XSTATE_MASK_AVX512))
68-
{
69-
return (SupportedISA)((int)SupportedISA::AVX2 | (int)SupportedISA::AVX512F);
70-
}
71-
72-
// check if AVX2 is supported by both processor and OS
73-
if ((reg[EBX] & AVX2) &&
74-
(xcr0 & 0x06) == 0x06 &&
75-
(FeatureMask & XSTATE_MASK_AVX) == XSTATE_MASK_AVX)
76-
{
77-
return SupportedISA::AVX2;
78-
}
79-
80-
return SupportedISA::None;
81-
}
82-
83-
#elif defined(TARGET_UNIX)
84-
8516
SupportedISA DetermineSupportedISA()
8617
{
87-
__builtin_cpu_init();
88-
if (__builtin_cpu_supports("avx2"))
18+
int cpuFeatures = minipal_getcpufeatures();
19+
if ((cpuFeatures & XArchIntrinsicConstants_Avx2) != 0)
8920
{
90-
if (__builtin_cpu_supports("avx512f"))
21+
if ((cpuFeatures & XArchIntrinsicConstants_Avx512) != 0)
9122
return (SupportedISA)((int)SupportedISA::AVX2 | (int)SupportedISA::AVX512F);
9223
else
9324
return SupportedISA::AVX2;
@@ -98,8 +29,6 @@ SupportedISA DetermineSupportedISA()
9829
}
9930
}
10031

101-
#endif // defined(TARGET_UNIX)
102-
10332
static bool s_initialized;
10433
static SupportedISA s_supportedISA;
10534

0 commit comments

Comments
 (0)