From 068d9f01130289d14d16c2c49b15e637f772f2ac Mon Sep 17 00:00:00 2001 From: Andrew Au Date: Sat, 1 Mar 2025 17:41:53 +0000 Subject: [PATCH 1/4] Use minipal_getcpufeatures to detect for AVX --- src/coreclr/gc/vxsort/isa_detection.cpp | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/coreclr/gc/vxsort/isa_detection.cpp b/src/coreclr/gc/vxsort/isa_detection.cpp index 93c7288663c42f..42d81fa8fe9b5d 100644 --- a/src/coreclr/gc/vxsort/isa_detection.cpp +++ b/src/coreclr/gc/vxsort/isa_detection.cpp @@ -6,6 +6,8 @@ #ifdef TARGET_WINDOWS #include #include +#else +#include #endif #include "do_vxsort.h" @@ -84,10 +86,10 @@ SupportedISA DetermineSupportedISA() 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; From e7edd2e08a4cbae9da223308a3b16c78f759c1d0 Mon Sep 17 00:00:00 2001 From: Andrew Au Date: Sat, 1 Mar 2025 11:58:44 -0800 Subject: [PATCH 2/4] Link with minipal --- src/coreclr/gc/CMakeLists.txt | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/coreclr/gc/CMakeLists.txt b/src/coreclr/gc/CMakeLists.txt index b7b79013fc45b4..1ea5814a5ed212 100644 --- a/src/coreclr/gc/CMakeLists.txt +++ b/src/coreclr/gc/CMakeLists.txt @@ -91,6 +91,11 @@ if(CLR_CMAKE_TARGET_ARCH_AMD64) list(APPEND GC_LINK_LIBRARIES gc_vxsort ) + if(CLR_CMAKE_HOST_UNIX) + list(APPEND GC_LINK_LIBRARIES + minipal + ) + endif(CLR_CMAKE_HOST_UNIX) endif(CLR_CMAKE_TARGET_ARCH_AMD64) From 20740cf510d61dc6dea2cfd7675ab711af4f366f Mon Sep 17 00:00:00 2001 From: Adeel <3840695+am11@users.noreply.github.com> Date: Sun, 2 Mar 2025 01:36:01 +0200 Subject: [PATCH 3/4] Use OS-agnostic helper --- src/coreclr/gc/CMakeLists.txt | 6 +- src/coreclr/gc/vxsort/isa_detection.cpp | 75 +------------------------ 2 files changed, 2 insertions(+), 79 deletions(-) diff --git a/src/coreclr/gc/CMakeLists.txt b/src/coreclr/gc/CMakeLists.txt index 1ea5814a5ed212..56956ed25f0496 100644 --- a/src/coreclr/gc/CMakeLists.txt +++ b/src/coreclr/gc/CMakeLists.txt @@ -90,12 +90,8 @@ set (GC_LINK_LIBRARIES ${GC_LINK_LIBRARIES} gc_pal) if(CLR_CMAKE_TARGET_ARCH_AMD64) list(APPEND GC_LINK_LIBRARIES gc_vxsort + minipal ) - if(CLR_CMAKE_HOST_UNIX) - list(APPEND GC_LINK_LIBRARIES - minipal - ) - endif(CLR_CMAKE_HOST_UNIX) endif(CLR_CMAKE_TARGET_ARCH_AMD64) diff --git a/src/coreclr/gc/vxsort/isa_detection.cpp b/src/coreclr/gc/vxsort/isa_detection.cpp index 42d81fa8fe9b5d..b069c8be9bee04 100644 --- a/src/coreclr/gc/vxsort/isa_detection.cpp +++ b/src/coreclr/gc/vxsort/isa_detection.cpp @@ -2,15 +2,9 @@ // The .NET Foundation licenses this file to you under the MIT license. #include "common.h" +#include "do_vxsort.h" -#ifdef TARGET_WINDOWS -#include -#include -#else #include -#endif - -#include "do_vxsort.h" enum class SupportedISA { @@ -19,71 +13,6 @@ enum class SupportedISA 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() { int cpuFeatures = minipal_getcpufeatures(); @@ -100,8 +29,6 @@ SupportedISA DetermineSupportedISA() } } -#endif // defined(TARGET_UNIX) - static bool s_initialized; static SupportedISA s_supportedISA; From 19072a97fb9b25a899ae2c55edf67e75471760eb Mon Sep 17 00:00:00 2001 From: Adeel <3840695+am11@users.noreply.github.com> Date: Mon, 3 Mar 2025 09:14:40 +0200 Subject: [PATCH 4/4] Link minipal in sample --- src/coreclr/gc/sample/CMakeLists.txt | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/coreclr/gc/sample/CMakeLists.txt b/src/coreclr/gc/sample/CMakeLists.txt index 1f297fd2313329..2f0181f4a55d09 100644 --- a/src/coreclr/gc/sample/CMakeLists.txt +++ b/src/coreclr/gc/sample/CMakeLists.txt @@ -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)