From 74121bcb295bf794278a343a5736b971de06bdbb Mon Sep 17 00:00:00 2001 From: MrHua269 Date: Sun, 12 Jan 2025 14:11:21 +0800 Subject: [PATCH] Pufferfish SIMD Utilities --- .../0005-Pufferfish-SIMD-Utilities.patch | 92 +++++++++++++++++++ .../optimizations/SIMDConfig.java.patch | 56 +++++++++++ 2 files changed, 148 insertions(+) create mode 100644 luminol-api/paper-patches/features/0005-Pufferfish-SIMD-Utilities.patch create mode 100644 luminol-server/paper-patches/files/src/main/java/me/earthme/luminol/config/modules/optimizations/SIMDConfig.java.patch diff --git a/luminol-api/paper-patches/features/0005-Pufferfish-SIMD-Utilities.patch b/luminol-api/paper-patches/features/0005-Pufferfish-SIMD-Utilities.patch new file mode 100644 index 0000000..165ea97 --- /dev/null +++ b/luminol-api/paper-patches/features/0005-Pufferfish-SIMD-Utilities.patch @@ -0,0 +1,92 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: MrHua269 +Date: Sun, 12 Jan 2025 14:00:28 +0800 +Subject: [PATCH] Pufferfish SIMD Utilities + + +diff --git a/src/main/java/gg/pufferfish/pufferfish/simd/SIMDChecker.java b/src/main/java/gg/pufferfish/pufferfish/simd/SIMDChecker.java +new file mode 100644 +index 0000000000000000000000000000000000000000..f00c008c03e2533f568085838cf13cb9b5b32cd9 +--- /dev/null ++++ b/src/main/java/gg/pufferfish/pufferfish/simd/SIMDChecker.java +@@ -0,0 +1,39 @@ ++package gg.pufferfish.pufferfish.simd; ++ ++import jdk.incubator.vector.FloatVector; ++import jdk.incubator.vector.IntVector; ++import jdk.incubator.vector.VectorSpecies; ++import org.slf4j.Logger; ++ ++/** ++ * Basically, java is annoying and we have to push this out to its own class. ++ */ ++@Deprecated ++public class SIMDChecker { ++ ++ @Deprecated ++ public static boolean canEnable(Logger logger) { ++ try { ++ if (SIMDDetection.getJavaVersion() < 17 || SIMDDetection.getJavaVersion() > 21) { ++ return false; ++ } else { ++ SIMDDetection.testRun = true; ++ ++ VectorSpecies ISPEC = IntVector.SPECIES_PREFERRED; ++ VectorSpecies FSPEC = FloatVector.SPECIES_PREFERRED; ++ ++ logger.info("Max SIMD vector size on this system is {} bits (int)", ISPEC.vectorBitSize()); ++ logger.info("Max SIMD vector size on this system is " + FSPEC.vectorBitSize() + " bits (float)"); ++ ++ if (ISPEC.elementSize() < 2 || FSPEC.elementSize() < 2) { ++ logger.warn("SIMD is not properly supported on this system!"); ++ return false; ++ } ++ ++ return true; ++ } ++ } catch (NoClassDefFoundError | Exception ignored) {} // Basically, we don't do anything. This lets us detect if it's not functional and disable it. ++ return false; ++ } ++ ++} +diff --git a/src/main/java/gg/pufferfish/pufferfish/simd/SIMDDetection.java b/src/main/java/gg/pufferfish/pufferfish/simd/SIMDDetection.java +new file mode 100644 +index 0000000000000000000000000000000000000000..cd953435a6179eaae7c9cc250a791cae26c5567b +--- /dev/null ++++ b/src/main/java/gg/pufferfish/pufferfish/simd/SIMDDetection.java +@@ -0,0 +1,35 @@ ++package gg.pufferfish.pufferfish.simd; ++ ++import org.slf4j.Logger; ++ ++@Deprecated ++public class SIMDDetection { ++ ++ public static boolean isEnabled = false; ++ public static boolean versionLimited = false; ++ public static boolean testRun = false; ++ ++ @Deprecated ++ public static boolean canEnable(Logger logger) { ++ try { ++ return SIMDChecker.canEnable(logger); ++ } catch (NoClassDefFoundError | Exception ignored) { ++ return false; ++ } ++ } ++ ++ @Deprecated ++ public static int getJavaVersion() { ++ // https://stackoverflow.com/a/2591122 ++ String version = System.getProperty("java.version"); ++ if(version.startsWith("1.")) { ++ version = version.substring(2, 3); ++ } else { ++ int dot = version.indexOf("."); ++ if(dot != -1) { version = version.substring(0, dot); } ++ } ++ version = version.split("-")[0]; // Azul is stupid ++ return Integer.parseInt(version); ++ } ++ ++} diff --git a/luminol-server/paper-patches/files/src/main/java/me/earthme/luminol/config/modules/optimizations/SIMDConfig.java.patch b/luminol-server/paper-patches/files/src/main/java/me/earthme/luminol/config/modules/optimizations/SIMDConfig.java.patch new file mode 100644 index 0000000..2f696dc --- /dev/null +++ b/luminol-server/paper-patches/files/src/main/java/me/earthme/luminol/config/modules/optimizations/SIMDConfig.java.patch @@ -0,0 +1,56 @@ +--- /dev/null ++++ b/src/main/java/me/earthme/luminol/config/modules/optimizations/SIMDConfig.java +@@ -1,0 +_,53 @@ ++package me.earthme.luminol.config.modules.optimizations; ++ ++import com.electronwill.nightconfig.core.file.CommentedFileConfig; ++import com.mojang.logging.LogUtils; ++import gg.pufferfish.pufferfish.simd.SIMDDetection; ++import me.earthme.luminol.config.ConfigInfo; ++import me.earthme.luminol.config.DoNotLoad; ++import me.earthme.luminol.config.EnumConfigCategory; ++import me.earthme.luminol.config.IConfigModule; ++import org.slf4j.Logger; ++ ++public class SIMDConfig implements IConfigModule { ++ @DoNotLoad ++ private static final Logger LOGGER = LogUtils.getLogger(); ++ @ConfigInfo(baseName = "enabled") ++ public static boolean enabled = true; ++ ++ @Override ++ public EnumConfigCategory getCategory() { ++ return EnumConfigCategory.OPTIMIZATIONS; ++ } ++ ++ @Override ++ public String getBaseName() { ++ return "use_simd"; ++ } ++ ++ @Override ++ public void onLoaded(CommentedFileConfig configInstance) { ++ if (!enabled){ ++ return; ++ } ++ ++ // Attempt to detect vectorization ++ try { ++ SIMDDetection.isEnabled = SIMDDetection.canEnable(LOGGER); ++ SIMDDetection.versionLimited = SIMDDetection.getJavaVersion() < 17; ++ } catch (NoClassDefFoundError | Exception ignored) { ++ ignored.printStackTrace(); ++ } ++ ++ if (SIMDDetection.isEnabled) { ++ LOGGER.info("SIMD operations detected as functional. Will replace some operations with faster versions."); ++ } else if (SIMDDetection.versionLimited) { ++ LOGGER.warn("Will not enable SIMD! These optimizations are only safely supported on Java 17+."); ++ } else { ++ LOGGER.warn("SIMD operations are available for your server, but are not configured!"); ++ LOGGER.warn("To enable additional optimizations, add \"--add-modules=jdk.incubator.vector\" to your startup flags, BEFORE the \"-jar\"."); ++ LOGGER.warn("If you have already added this flag, then SIMD operations are not supported on your JVM or CPU."); ++ LOGGER.warn("Debug: Java: {}, test run: {}", System.getProperty("java.version"), SIMDDetection.testRun); ++ } ++ } ++}