From 8de2e2c28914838be485a0f68de9ac08c27c6e6b Mon Sep 17 00:00:00 2001 From: Rohit Arul Raj Date: Fri, 14 Mar 2025 20:50:50 +0530 Subject: [PATCH 1/3] JDK-8317976: Enable optimized SIMD sort for AMD Zen 4 & Zen 5 --- src/hotspot/cpu/x86/matcher_x86.hpp | 3 ++- src/hotspot/cpu/x86/stubGenerator_x86_64.cpp | 10 +++++++--- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/src/hotspot/cpu/x86/matcher_x86.hpp b/src/hotspot/cpu/x86/matcher_x86.hpp index b311f4144b2bf..4a43848de9e23 100644 --- a/src/hotspot/cpu/x86/matcher_x86.hpp +++ b/src/hotspot/cpu/x86/matcher_x86.hpp @@ -263,7 +263,8 @@ // Is SIMD sort supported for this CPU? static bool supports_simd_sort(BasicType bt) { - if (VM_Version::supports_avx512dq()) { + if ((VM_Version::is_intel() || (VM_Version::is_amd() && (VM_Version::cpu_family() > 0x19))) + && VM_Version::supports_avx512dq()) { return true; } else if (VM_Version::supports_avx2() && !is_double_word_type(bt)) { diff --git a/src/hotspot/cpu/x86/stubGenerator_x86_64.cpp b/src/hotspot/cpu/x86/stubGenerator_x86_64.cpp index d2a0c81b2c98a..b832ae3fb3081 100644 --- a/src/hotspot/cpu/x86/stubGenerator_x86_64.cpp +++ b/src/hotspot/cpu/x86/stubGenerator_x86_64.cpp @@ -4315,7 +4315,7 @@ void StubGenerator::generate_compiler_stubs() { // Load x86_64_sort library on supported hardware to enable SIMD sort and partition intrinsics - if (VM_Version::is_intel() && (VM_Version::supports_avx512dq() || VM_Version::supports_avx2())) { + if (VM_Version::supports_avx512dq() || VM_Version::supports_avx2()) { void *libsimdsort = nullptr; char ebuf_[1024]; char dll_name_simd_sort[JVM_MAXPATHLEN]; @@ -4326,10 +4326,14 @@ void StubGenerator::generate_compiler_stubs() { if (libsimdsort != nullptr) { log_info(library)("Loaded library %s, handle " INTPTR_FORMAT, JNI_LIB_PREFIX "simdsort" JNI_LIB_SUFFIX, p2i(libsimdsort)); - snprintf(ebuf_, sizeof(ebuf_), VM_Version::supports_avx512dq() ? "avx512_sort" : "avx2_sort"); + snprintf(ebuf_, sizeof(ebuf_), + ((VM_Version::is_intel() || (VM_Version::is_amd() && (VM_Version::cpu_family() > 0x19))) + && VM_Version::supports_avx512dq()) ? "avx512_sort" : "avx2_sort"); StubRoutines::_array_sort = (address)os::dll_lookup(libsimdsort, ebuf_); - snprintf(ebuf_, sizeof(ebuf_), VM_Version::supports_avx512dq() ? "avx512_partition" : "avx2_partition"); + snprintf(ebuf_, sizeof(ebuf_), + ((VM_Version::is_intel() || (VM_Version::is_amd() && (VM_Version::cpu_family() > 0x19))) + && VM_Version::supports_avx512dq()) ? "avx512_partition" : "avx2_partition"); StubRoutines::_array_partition = (address)os::dll_lookup(libsimdsort, ebuf_); } } From 420119111231c4224bbaac0a37c8cc8f3e13bc8f Mon Sep 17 00:00:00 2001 From: Rohit Arul Raj Date: Tue, 18 Mar 2025 02:14:31 +0530 Subject: [PATCH 2/3] create a separate method to check for cpu's supporting avx512 version of simd sort --- src/hotspot/cpu/x86/matcher_x86.hpp | 3 +-- src/hotspot/cpu/x86/stubGenerator_x86_64.cpp | 8 ++------ src/hotspot/cpu/x86/vm_version_x86.hpp | 6 ++++++ 3 files changed, 9 insertions(+), 8 deletions(-) diff --git a/src/hotspot/cpu/x86/matcher_x86.hpp b/src/hotspot/cpu/x86/matcher_x86.hpp index 4a43848de9e23..ab5c72e8f7b39 100644 --- a/src/hotspot/cpu/x86/matcher_x86.hpp +++ b/src/hotspot/cpu/x86/matcher_x86.hpp @@ -263,8 +263,7 @@ // Is SIMD sort supported for this CPU? static bool supports_simd_sort(BasicType bt) { - if ((VM_Version::is_intel() || (VM_Version::is_amd() && (VM_Version::cpu_family() > 0x19))) - && VM_Version::supports_avx512dq()) { + if (VM_Version::supports_avx512_simd_sort()) { return true; } else if (VM_Version::supports_avx2() && !is_double_word_type(bt)) { diff --git a/src/hotspot/cpu/x86/stubGenerator_x86_64.cpp b/src/hotspot/cpu/x86/stubGenerator_x86_64.cpp index b832ae3fb3081..2cf0bd71993a4 100644 --- a/src/hotspot/cpu/x86/stubGenerator_x86_64.cpp +++ b/src/hotspot/cpu/x86/stubGenerator_x86_64.cpp @@ -4326,14 +4326,10 @@ void StubGenerator::generate_compiler_stubs() { if (libsimdsort != nullptr) { log_info(library)("Loaded library %s, handle " INTPTR_FORMAT, JNI_LIB_PREFIX "simdsort" JNI_LIB_SUFFIX, p2i(libsimdsort)); - snprintf(ebuf_, sizeof(ebuf_), - ((VM_Version::is_intel() || (VM_Version::is_amd() && (VM_Version::cpu_family() > 0x19))) - && VM_Version::supports_avx512dq()) ? "avx512_sort" : "avx2_sort"); + snprintf(ebuf_, sizeof(ebuf_), VM_Version::supports_avx512_simd_sort() ? "avx512_sort" : "avx2_sort"); StubRoutines::_array_sort = (address)os::dll_lookup(libsimdsort, ebuf_); - snprintf(ebuf_, sizeof(ebuf_), - ((VM_Version::is_intel() || (VM_Version::is_amd() && (VM_Version::cpu_family() > 0x19))) - && VM_Version::supports_avx512dq()) ? "avx512_partition" : "avx2_partition"); + snprintf(ebuf_, sizeof(ebuf_), VM_Version::supports_avx512_simd_sort() ? "avx512_partition" : "avx2_partition"); StubRoutines::_array_partition = (address)os::dll_lookup(libsimdsort, ebuf_); } } diff --git a/src/hotspot/cpu/x86/vm_version_x86.hpp b/src/hotspot/cpu/x86/vm_version_x86.hpp index 9bd3116cf84de..187a8a86e519c 100644 --- a/src/hotspot/cpu/x86/vm_version_x86.hpp +++ b/src/hotspot/cpu/x86/vm_version_x86.hpp @@ -432,6 +432,8 @@ class VM_Version : public Abstract_VM_Version { enum Extended_Family { // AMD CPU_FAMILY_AMD_11H = 0x11, + CPU_FAMILY_AMD_17H = 0x17, /* Zen1 & Zen2 */ + CPU_FAMILY_AMD_19H = 0x19, /* Zen3 & Zen4 */ // ZX CPU_FAMILY_ZX_CORE_F6 = 6, CPU_FAMILY_ZX_CORE_F7 = 7, @@ -771,6 +773,10 @@ class VM_Version : public Abstract_VM_Version { // static bool cpu_supports_evex() { return (_cpu_features & CPU_AVX512F) != 0; } + static bool supports_avx512_simd_sort() { + // Disable AVX512 version of SIMD Sort on AMD Zen4 Processors + return ((is_intel() || (is_amd() && (cpu_family() > CPU_FAMILY_AMD_19H))) && supports_avx512dq()); } + // Intel features static bool is_intel_family_core() { return is_intel() && extended_cpu_family() == CPU_FAMILY_INTEL_CORE; } From b369de6f9b0327a2090f9ea44f11ff2940e7095a Mon Sep 17 00:00:00 2001 From: Rohit Arul Raj Date: Thu, 27 Mar 2025 17:25:12 +0530 Subject: [PATCH 3/3] Refactor 'supports_avx512_simd_sort' code to make it easily readable --- src/hotspot/cpu/x86/vm_version_x86.hpp | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/hotspot/cpu/x86/vm_version_x86.hpp b/src/hotspot/cpu/x86/vm_version_x86.hpp index 187a8a86e519c..cc5c6c1c63992 100644 --- a/src/hotspot/cpu/x86/vm_version_x86.hpp +++ b/src/hotspot/cpu/x86/vm_version_x86.hpp @@ -774,8 +774,15 @@ class VM_Version : public Abstract_VM_Version { static bool cpu_supports_evex() { return (_cpu_features & CPU_AVX512F) != 0; } static bool supports_avx512_simd_sort() { - // Disable AVX512 version of SIMD Sort on AMD Zen4 Processors - return ((is_intel() || (is_amd() && (cpu_family() > CPU_FAMILY_AMD_19H))) && supports_avx512dq()); } + if (supports_avx512dq()) { + // Disable AVX512 version of SIMD Sort on AMD Zen4 Processors. + if (is_amd() && cpu_family() == CPU_FAMILY_AMD_19H) { + return false; + } + return true; + } + return false; + } // Intel features static bool is_intel_family_core() { return is_intel() &&