From 6da0408cb4d15dd7539a0aa02c25f2cb4f1d1221 Mon Sep 17 00:00:00 2001 From: Fabio Niephaus Date: Thu, 23 Feb 2023 10:23:00 +0100 Subject: [PATCH 1/2] Add `-march` option and target `x86-64-v3` by default. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Gergö Barany --- substratevm/CHANGELOG.md | 1 + .../com/oracle/svm/driver/NativeImage.java | 15 +- .../hosted/CPUFeatureAccessFeatureBase.java | 7 +- .../svm/hosted/NativeImageGenerator.java | 52 ++--- .../hosted/NativeImageGeneratorRunner.java | 4 +- .../oracle/svm/hosted/NativeImageOptions.java | 42 +++- .../com/oracle/svm/hosted/util/CPUType.java | 56 +++++ .../svm/hosted/util/CPUTypeAArch64.java | 184 +++++++++++++++++ .../oracle/svm/hosted/util/CPUTypeAMD64.java | 193 ++++++++++++++++++ 9 files changed, 500 insertions(+), 54 deletions(-) create mode 100644 substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/util/CPUType.java create mode 100644 substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/util/CPUTypeAArch64.java create mode 100644 substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/util/CPUTypeAMD64.java diff --git a/substratevm/CHANGELOG.md b/substratevm/CHANGELOG.md index e2edda5f2207..9adf1883eafd 100644 --- a/substratevm/CHANGELOG.md +++ b/substratevm/CHANGELOG.md @@ -25,6 +25,7 @@ This changelog summarizes major changes to GraalVM Native Image. * (GR-38414) BellSoft implemented the `MemoryPoolMXBean` for the serial and epsilon GCs. * (GR-40641) Dynamic linking of AWT libraries on Linux. * (GR-40463) Red Hat added experimental support for JMX, which can be enabled with the `--enable-monitoring` option (e.g. `--enable-monitoring=jmxclient,jmxserver`). +* (GR-44110) Native Image now targets `x86-64-v3` by default on AMD64 and supports a new `-march` option. Use `-march=compatibility` for best compatibility (previous default) or `-march=native` for best performance if the native executable is deployed on the same machine or on a machine with the same CPU features. To list all available machine types, use `-march=list`. ## Version 22.3.0 * (GR-35721) Remove old build output style and the `-H:±BuildOutputUseNewStyle` option. diff --git a/substratevm/src/com.oracle.svm.driver/src/com/oracle/svm/driver/NativeImage.java b/substratevm/src/com.oracle.svm.driver/src/com/oracle/svm/driver/NativeImage.java index 76f78d0364f9..2758516f392e 100644 --- a/substratevm/src/com.oracle.svm.driver/src/com/oracle/svm/driver/NativeImage.java +++ b/substratevm/src/com.oracle.svm.driver/src/com/oracle/svm/driver/NativeImage.java @@ -1008,15 +1008,15 @@ private int completeImageBuild() { .collect(Collectors.joining(",", oHCLibraryPath, "")); imageBuilderArgs.add(0, clibrariesBuilderArg); - String printFlagsDummyImage = "dummy-image-"; + boolean printFlags = false; if (printFlagsOptionQuery != null) { + printFlags = true; addPlainImageBuilderArg(NativeImage.oH + enablePrintFlags + "=" + printFlagsOptionQuery); addPlainImageBuilderArg(NativeImage.oR + enablePrintFlags + "=" + printFlagsOptionQuery); - addPlainImageBuilderArg(oHName + printFlagsDummyImage + "printFlagsOptionQuery"); } else if (printFlagsWithExtraHelpOptionQuery != null) { + printFlags = true; addPlainImageBuilderArg(NativeImage.oH + enablePrintFlagsWithExtraHelp + "=" + printFlagsWithExtraHelpOptionQuery); addPlainImageBuilderArg(NativeImage.oR + enablePrintFlagsWithExtraHelp + "=" + printFlagsWithExtraHelpOptionQuery); - addPlainImageBuilderArg(oHName + printFlagsDummyImage + "printFlagsWithExtraHelpOptionQuery"); } if (shouldAddCWDToCP()) { @@ -1062,9 +1062,12 @@ private int completeImageBuild() { mainClass = getHostedOptionFinalArgumentValue(imageBuilderArgs, oHClass); boolean buildExecutable = imageBuilderArgs.stream().noneMatch(arg -> arg.contains(oHEnableSharedLibraryFlag)); boolean listModules = imageBuilderArgs.stream().anyMatch(arg -> arg.contains(oH + "+" + "ListModules")); - boolean printFlags = imageBuilderArgs.stream().anyMatch(arg -> arg.contains(enablePrintFlags) || arg.contains(enablePrintFlagsWithExtraHelp)); + printFlags |= imageBuilderArgs.stream().anyMatch(arg -> arg.contains("-march=list")); - if (!printFlags) { + if (printFlags) { + /* Ensure name for bundle support */ + addPlainImageBuilderArg(oHName + "dummy-image"); + } else { List extraImageArgs = new ArrayList<>(); ListIterator leftoverArgsItr = leftoverArgs.listIterator(); while (leftoverArgsItr.hasNext()) { @@ -1133,7 +1136,7 @@ private int completeImageBuild() { Path imageNamePath = Path.of(imageName); Path imageNamePathParent = imageNamePath.getParent(); if (imageNamePathParent != null) { - /* Readjust imageName & imagePath so that imageName is just a simple fileName */ + /* Read just imageName & imagePath so that imageName is just a simple fileName */ imageName = imageNamePath.getFileName().toString(); if (!imageNamePathParent.isAbsolute()) { imageNamePathParent = imagePath.resolve(imageNamePathParent); diff --git a/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/CPUFeatureAccessFeatureBase.java b/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/CPUFeatureAccessFeatureBase.java index b1e7848a3e54..38969b47ca16 100644 --- a/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/CPUFeatureAccessFeatureBase.java +++ b/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/CPUFeatureAccessFeatureBase.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022, 2022, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2022, 2023, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -115,9 +115,10 @@ protected > void initializeCPUFeatureAccessData(Enum[] allC } } if (!unknownFeatures.isEmpty()) { - throw VMError.shouldNotReachHere("Native image does not support the following JVMCI CPU features: " + unknownFeatures); + throw VMError.shouldNotReachHere("The image does not support the following JVMCI CPU features: " + unknownFeatures); } - String errorMessage = "Current target does not support the following CPU features that are required by the image: " + buildtimeCPUFeatures.toString() + "\n\0"; + String errorMessage = "The current machine does not support all of the following CPU features that are required by the image: " + buildtimeCPUFeatures.toString() + "." + + System.lineSeparator() + "Please rebuild the executable with an appropriate setting of the -march option.\0"; var cpuFeatureAccess = createCPUFeatureAccessSingleton(buildtimeCPUFeatures, cpuFeatureEnumToStructOffset, errorMessage.getBytes(StandardCharsets.UTF_8), requiredFeaturesStruct); ImageSingletons.add(CPUFeatureAccess.class, cpuFeatureAccess); } diff --git a/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/NativeImageGenerator.java b/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/NativeImageGenerator.java index 47242e2537a2..6e0b9f14b050 100644 --- a/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/NativeImageGenerator.java +++ b/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/NativeImageGenerator.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2012, 2022, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2012, 2023, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -293,6 +293,8 @@ import com.oracle.svm.hosted.substitute.AnnotationSubstitutionProcessor; import com.oracle.svm.hosted.substitute.DeletedFieldsPlugin; import com.oracle.svm.hosted.substitute.UnsafeAutomaticSubstitutionProcessor; +import com.oracle.svm.hosted.util.CPUTypeAArch64; +import com.oracle.svm.hosted.util.CPUTypeAMD64; import com.oracle.svm.util.ClassUtil; import com.oracle.svm.util.ImageBuildStatistics; import com.oracle.svm.util.ReflectionUtil; @@ -439,17 +441,8 @@ protected SubstrateTargetDescription createTarget() { public static SubstrateTargetDescription createTarget(Platform platform) { if (includedIn(platform, Platform.AMD64.class)) { - Architecture architecture; - EnumSet features = EnumSet.noneOf(AMD64.CPUFeature.class); - if (NativeImageOptions.NativeArchitecture.getValue()) { - features.addAll(((AMD64) GraalAccess.getOriginalTarget().arch).getFeatures()); - } else { - // SSE and SSE2 are added by default as they are required by Graal - features.add(AMD64.CPUFeature.SSE); - features.add(AMD64.CPUFeature.SSE2); - - features.addAll(parseCSVtoEnum(AMD64.CPUFeature.class, NativeImageOptions.CPUFeatures.getValue().values(), AMD64.CPUFeature.values())); - } + EnumSet features = CPUTypeAMD64.getSelectedFeatures(); + features.addAll(parseCSVtoEnum(AMD64.CPUFeature.class, NativeImageOptions.CPUFeatures.getValue().values(), AMD64.CPUFeature.values())); // GR-33542 RTM is only intermittently detected and is not used by Graal features.remove(AMD64.CPUFeature.RTM); // set up the runtime checked cpu features @@ -464,39 +457,21 @@ public static SubstrateTargetDescription createTarget(Platform platform) { } } } - architecture = new AMD64(features, AMD64CPUFeatureAccess.allAMD64Flags()); - assert architecture instanceof AMD64 : "using AMD64 platform with a different architecture"; + AMD64 architecture = new AMD64(features, AMD64CPUFeatureAccess.allAMD64Flags()); int deoptScratchSpace = 2 * 8; // Space for two 64-bit registers: rax and xmm0 return new SubstrateTargetDescription(architecture, true, 16, 0, deoptScratchSpace, runtimeCheckedFeatures); } else if (includedIn(platform, Platform.AARCH64.class)) { - Architecture architecture; - if (NativeImageOptions.NativeArchitecture.getValue()) { - architecture = GraalAccess.getOriginalTarget().arch; - } else { - EnumSet features = EnumSet.noneOf(AArch64.CPUFeature.class); - /* - * FP is added by default, as floating-point operations are required by Graal. - */ - features.add(AArch64.CPUFeature.FP); - /* - * ASIMD is added by default, as it is available in all AArch64 machines with - * floating-port support. - */ - features.add(AArch64.CPUFeature.ASIMD); - - features.addAll(parseCSVtoEnum(AArch64.CPUFeature.class, NativeImageOptions.CPUFeatures.getValue().values(), AArch64.CPUFeature.values())); - - architecture = new AArch64(features, AArch64CPUFeatureAccess.enabledAArch64Flags()); - } - assert architecture instanceof AArch64 : "using AArch64 platform with a different architecture"; + EnumSet features = CPUTypeAArch64.getSelectedFeatures(); + features.addAll(parseCSVtoEnum(AArch64.CPUFeature.class, NativeImageOptions.CPUFeatures.getValue().values(), AArch64.CPUFeature.values())); + AArch64 architecture = new AArch64(features, AArch64CPUFeatureAccess.enabledAArch64Flags()); // runtime checked features are the same as static features on AArch64 for now - EnumSet runtimeCheckedFeatures = ((AArch64) architecture).getFeatures().clone(); + EnumSet runtimeCheckedFeatures = architecture.getFeatures().clone(); int deoptScratchSpace = 2 * 8; // Space for two 64-bit registers: r0 and v0. return new SubstrateTargetDescription(architecture, true, 16, 0, deoptScratchSpace, runtimeCheckedFeatures); } else if (includedIn(platform, Platform.RISCV64.class)) { Class riscv64CPUFeature = RISCV64ReflectionUtil.lookupClass(false, RISCV64ReflectionUtil.featureClass); Architecture architecture; - if (NativeImageOptions.NativeArchitecture.getValue()) { + if (NativeImageOptions.MICRO_ARCHITECTURE_NATIVE.equals(NativeImageOptions.MicroArchitecture.getValue())) { architecture = GraalAccess.getOriginalTarget().arch; } else { Method noneOf = RISCV64ReflectionUtil.lookupMethod(EnumSet.class, "noneOf", Class.class); @@ -504,10 +479,9 @@ public static SubstrateTargetDescription createTarget(Platform platform) { Method parseCSVtoEnum = RISCV64ReflectionUtil.lookupMethod(NativeImageGenerator.class, "parseCSVtoEnum", Class.class, List.class, Enum[].class); parseCSVtoEnum.setAccessible(true); Method addAll = RISCV64ReflectionUtil.lookupMethod(AbstractCollection.class, "addAll", Collection.class); - RISCV64ReflectionUtil.invokeMethod(addAll, features, - RISCV64ReflectionUtil.invokeMethod(parseCSVtoEnum, null, riscv64CPUFeature, NativeImageOptions.CPUFeatures.getValue().values(), riscv64CPUFeature.getEnumConstants())); - + RISCV64ReflectionUtil.invokeMethod(parseCSVtoEnum, null, riscv64CPUFeature, NativeImageOptions.CPUFeatures.getValue().values(), + riscv64CPUFeature.getEnumConstants())); architecture = (Architecture) ReflectionUtil.newInstance(ReflectionUtil.lookupConstructor(RISCV64ReflectionUtil.getArch(false), EnumSet.class, EnumSet.class), features, RISCV64CPUFeatureAccess.enabledRISCV64Flags()); } diff --git a/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/NativeImageGeneratorRunner.java b/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/NativeImageGeneratorRunner.java index fb8a7c9e0a1c..7da6a72b8f40 100644 --- a/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/NativeImageGeneratorRunner.java +++ b/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/NativeImageGeneratorRunner.java @@ -521,13 +521,13 @@ public static void printCPUFeatures(Platform platform) { if (NativeImageGenerator.includedIn(platform, Platform.AMD64.class)) { message.append("All AMD64 CPUFeatures: ").append(Arrays.toString(AMD64.CPUFeature.values())); if (arch instanceof AMD64) { - message.append("\nHost machine AMD64 CPUFeatures: ").append(((AMD64) arch).getFeatures().toString()); + message.append(System.lineSeparator()).append("Host machine AMD64 CPUFeatures: ").append(((AMD64) arch).getFeatures().toString()); } } else { assert NativeImageGenerator.includedIn(platform, Platform.AARCH64.class); message.append("All AArch64 CPUFeatures: ").append(Arrays.toString(AArch64.CPUFeature.values())); if (arch instanceof AArch64) { - message.append("\nHost machine AArch64 CPUFeatures: ").append(((AArch64) arch).getFeatures().toString()); + message.append(System.lineSeparator()).append("Host machine AArch64 CPUFeatures: ").append(((AArch64) arch).getFeatures().toString()); } } System.out.println(message); diff --git a/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/NativeImageOptions.java b/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/NativeImageOptions.java index 423e97e9a0ed..c6b5d4962a5a 100644 --- a/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/NativeImageOptions.java +++ b/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/NativeImageOptions.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2013, 2020, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2013, 2023, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -47,8 +47,12 @@ import com.oracle.svm.core.option.HostedOptionKey; import com.oracle.svm.core.option.LocatableMultiOptionValue; import com.oracle.svm.core.option.SubstrateOptionsParser; +import com.oracle.svm.core.util.InterruptImageBuilding; import com.oracle.svm.core.util.UserError; import com.oracle.svm.hosted.classinitialization.ClassInitializationOptions; +import com.oracle.svm.hosted.util.CPUType; +import com.oracle.svm.hosted.util.CPUTypeAArch64; +import com.oracle.svm.hosted.util.CPUTypeAMD64; public class NativeImageOptions { @@ -58,7 +62,8 @@ public class NativeImageOptions { "target executable, irrespective of whether they are supported by the hosted " + "environment. Note that enabling features not present within the target environment " + "may result in application crashes. The specific options available are target " + - "platform dependent. See --list-cpu-features for feature list.", type = User)// + "platform dependent. See --list-cpu-features for feature list. These features " + + "are in addition to -march.", type = User)// public static final HostedOptionKey CPUFeatures = new HostedOptionKey<>(LocatableMultiOptionValue.Strings.buildWithCommaDelimiter()); @APIOption(name = "list-cpu-features")// @@ -76,8 +81,37 @@ public class NativeImageOptions { "AMD64: 'AVX,AVX2'; AArch64: ''", type = User)// public static final HostedOptionKey RuntimeCheckedCPUFeatures = new HostedOptionKey<>(LocatableMultiOptionValue.Strings.buildWithCommaDelimiter()); - @Option(help = "Overrides CPUFeatures and uses the native architecture, i.e., the architecture of a machine that builds an image. NativeArchitecture takes precedence over CPUFeatures", type = User)// - public static final HostedOptionKey NativeArchitecture = new HostedOptionKey<>(false); + public static final String MICRO_ARCHITECTURE_NATIVE = "native"; + public static final String MICRO_ARCHITECTURE_COMPATIBILITY = "compatibility"; + public static final String MICRO_ARCHITECTURE_LIST = "list"; + + @APIOption(name = "-march")// + @Option(help = "Generate instructions for a specific machine type. Defaults to 'x86-64-v3' on AMD64 and 'armv8-a' on AArch64. " + + "Use -march=" + MICRO_ARCHITECTURE_COMPATIBILITY + " for best compatibility, or -march=" + MICRO_ARCHITECTURE_NATIVE + + " for best performance if the native executable is deployed on the same machine or on a machine with the same CPU features. " + + "To list all available machine types, use -march=" + MICRO_ARCHITECTURE_LIST + ".", type = User)// + public static final HostedOptionKey MicroArchitecture = new HostedOptionKey<>(null) { + protected void onValueUpdate(EconomicMap, Object> values, String oldValue, String newValue) { + if (MICRO_ARCHITECTURE_LIST.equals(newValue)) { + if (System.getProperty("os.arch").equalsIgnoreCase("aarch64")) { + CPUType.print("AArch64", CPUTypeAArch64.values()); + CPUTypeAArch64.printFeatureModifiers(); + } else { + CPUType.print("AMD64", CPUTypeAMD64.values()); + } + throw new InterruptImageBuilding(""); + } + } + }; + + @Option(help = "Uses the native architecture, i.e., the architecture of a machine that builds an image.", type = User, // + deprecated = true, deprecationMessage = "Please use -march=native instead. See --help for details.") // + public static final HostedOptionKey NativeArchitecture = new HostedOptionKey<>(false) { + @Override + protected void onValueUpdate(EconomicMap, Object> values, Boolean oldValue, Boolean newValue) { + MicroArchitecture.update(values, newValue ? MICRO_ARCHITECTURE_NATIVE : null); + } + }; @Option(help = "Print information about classes, methods, and fields that are present in the native image")// public static final HostedOptionKey PrintUniverse = new HostedOptionKey<>(false); diff --git a/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/util/CPUType.java b/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/util/CPUType.java new file mode 100644 index 000000000000..6829fa344e4a --- /dev/null +++ b/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/util/CPUType.java @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2023, 2023, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ +package com.oracle.svm.hosted.util; + +import java.util.Arrays; +import java.util.Comparator; + +public interface CPUType { + + String getName(); + + String getSpecificFeaturesString(); + + CPUType getParent(); + + static void print(String name, CPUType[] values) { + Arrays.sort(values, Comparator.comparing(v -> v.getName())); + System.out.printf("On %s, the following machine types are available:%n%n", name); + for (CPUType m : values) { + String specificFeatures = m.getSpecificFeaturesString(); + String parentText; + if (m.getParent() != null) { + parentText = m.getParent() == null ? "" : "all of '" + m.getParent().getName() + "'"; + if (!specificFeatures.isEmpty()) { + parentText += " + "; + } + } else { + parentText = ""; + } + System.out.printf("'%s'%n CPU features: %s%s%n", m.getName(), parentText, specificFeatures); + } + System.out.println(); + } +} diff --git a/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/util/CPUTypeAArch64.java b/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/util/CPUTypeAArch64.java new file mode 100644 index 000000000000..af2e3fabb407 --- /dev/null +++ b/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/util/CPUTypeAArch64.java @@ -0,0 +1,184 @@ +/* + * Copyright (c) 2023, 2023, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ +package com.oracle.svm.hosted.util; + +import static jdk.vm.ci.aarch64.AArch64.CPUFeature.AES; +import static jdk.vm.ci.aarch64.AArch64.CPUFeature.ASIMD; +import static jdk.vm.ci.aarch64.AArch64.CPUFeature.CRC32; +import static jdk.vm.ci.aarch64.AArch64.CPUFeature.FP; +import static jdk.vm.ci.aarch64.AArch64.CPUFeature.LSE; +import static jdk.vm.ci.aarch64.AArch64.CPUFeature.PMULL; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.EnumSet; +import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +import org.graalvm.nativeimage.Platform; +import org.graalvm.nativeimage.Platforms; + +import com.oracle.graal.pointsto.util.GraalAccess; +import com.oracle.svm.core.option.SubstrateOptionsParser; +import com.oracle.svm.core.util.UserError; +import com.oracle.svm.hosted.NativeImageOptions; + +import jdk.vm.ci.aarch64.AArch64; +import jdk.vm.ci.aarch64.AArch64.CPUFeature; + +/** + * AArch64 CPU types used to implement -march. + *

+ * For reference, see gcc's + * AArch64 Options. + */ +public enum CPUTypeAArch64 implements CPUType { + ARMV8_A("armv8-a", FP, ASIMD), + ARMV8_1_A("armv8.1-a", ARMV8_A, CRC32, LSE), + + // Special symbols + COMPATIBILITY(NativeImageOptions.MICRO_ARCHITECTURE_COMPATIBILITY, ARMV8_A), + NATIVE(NativeImageOptions.MICRO_ARCHITECTURE_NATIVE, getNativeOrEmpty()); + + private static final String AVAILABLE_FEATURE_MODIFIERS = String.join(", ", new String[]{"aes", "lse", "fp", "simd"}); + + private static CPUFeature[] getNativeOrEmpty() { + CPUFeature[] empty = new CPUFeature[0]; + if (GraalAccess.getOriginalTarget().arch instanceof AArch64 arch) { + return arch.getFeatures().toArray(empty); + } else { + return empty; + } + } + + private final String name; + private final CPUTypeAArch64 parent; + private final EnumSet specificFeatures; + + CPUTypeAArch64(String cpuTypeName, CPUFeature... features) { + this(cpuTypeName, null, features); + } + + CPUTypeAArch64(String cpuTypeName, CPUTypeAArch64 cpuTypeParentOrNull, CPUFeature... features) { + name = cpuTypeName; + parent = cpuTypeParentOrNull; + specificFeatures = features.length > 0 ? EnumSet.copyOf(List.of(features)) : EnumSet.noneOf(CPUFeature.class); + assert parent == null || parent.getFeatures().stream().noneMatch(f -> specificFeatures.contains(f)) : "duplicate features detected but not allowed"; + } + + public static String getDefault() { + return ARMV8_A.getName(); + } + + @Override + public String getName() { + return name; + } + + @Override + public CPUTypeAArch64 getParent() { + return parent; + } + + @Override + public String getSpecificFeaturesString() { + return specificFeatures.stream().map(f -> f.name()).collect(Collectors.joining(" + ")); + } + + public EnumSet getFeatures() { + if (parent == null) { + return specificFeatures; + } else { + return EnumSet.copyOf(Stream.concat(parent.getFeatures().stream(), specificFeatures.stream()).toList()); + } + } + + @Platforms(Platform.HOSTED_ONLY.class) + public static EnumSet getSelectedFeatures() { + String value = NativeImageOptions.MicroArchitecture.getValue(); + if (value == null) { + value = ARMV8_A.getName(); // set default + } + return getCPUFeaturesForArch(value); + } + + public static EnumSet getCPUFeaturesForArch(String marchValue) { + String[] archParts = marchValue.split("\\+"); + CPUTypeAArch64 value = typeOf(archParts[0]); + if (value == null) { + throw UserError.abort("Unsupported architecture '%s'. Please adjust '%s'. On AArch64, only %s are available.", + marchValue, + SubstrateOptionsParser.commandArgument(NativeImageOptions.MicroArchitecture, marchValue), + List.of(values()).stream().map(v -> v.name).collect(Collectors.joining(", "))); + } + List features = new ArrayList<>(value.getFeatures()); + processFeatureModifiers(features, archParts); + return EnumSet.copyOf(features); + } + + public static void printFeatureModifiers() { + System.out.printf("%nThe option also supports one or more feature modifiers via the form '-march=arch{+[no]feature}*'. " + + "Example: '%s+lse' enables Large System Extension instructions.%n" + + "The following feature modifiers are available: %s.%n", + ARMV8_1_A.getName(), AVAILABLE_FEATURE_MODIFIERS); + } + + private static void processFeatureModifiers(List features, String[] archParts) { + for (int i = 1; i < archParts.length; i++) { + String part = archParts[i]; + List partFeatures = getFeatures(part); + if (part.startsWith("no")) { + features.removeAll(partFeatures); + } else { + features.addAll(partFeatures); + } + } + } + + private static List getFeatures(String featureModifier) { + return switch (featureModifier) { + case "lse", "nolse" -> List.of(LSE); + case "aes", "noaes" -> List.of(AES, PMULL); + // fp and simd are required + case "fp", "simd" -> Collections.emptyList(); + case "nofp", "nosimd" -> throw UserError.abort("The '%s' CPU feature is required by the Graal compiler and thus cannot be disabled.%s", featureModifier, getUserAction()); + default -> throw UserError.abort("Unsupported AArch64 feature modifier '%s'.%s Only %s are available.", featureModifier, getUserAction(), AVAILABLE_FEATURE_MODIFIERS); + }; + } + + private static String getUserAction() { + return String.format(" Please adjust '%s'.", SubstrateOptionsParser.commandArgument(NativeImageOptions.MicroArchitecture, NativeImageOptions.MicroArchitecture.getValue())); + } + + private static CPUTypeAArch64 typeOf(String marchValue) { + for (CPUTypeAArch64 value : values()) { + if (value.name.equals(marchValue)) { + return value; + } + } + return null; + } +} diff --git a/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/util/CPUTypeAMD64.java b/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/util/CPUTypeAMD64.java new file mode 100644 index 000000000000..4167b11dc31a --- /dev/null +++ b/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/util/CPUTypeAMD64.java @@ -0,0 +1,193 @@ +/* + * Copyright (c) 2023, 2023, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ +package com.oracle.svm.hosted.util; + +import static jdk.vm.ci.amd64.AMD64.CPUFeature.ADX; +import static jdk.vm.ci.amd64.AMD64.CPUFeature.AES; +import static jdk.vm.ci.amd64.AMD64.CPUFeature.AMD_3DNOW_PREFETCH; +import static jdk.vm.ci.amd64.AMD64.CPUFeature.AVX; +import static jdk.vm.ci.amd64.AMD64.CPUFeature.AVX2; +import static jdk.vm.ci.amd64.AMD64.CPUFeature.AVX512BW; +import static jdk.vm.ci.amd64.AMD64.CPUFeature.AVX512CD; +import static jdk.vm.ci.amd64.AMD64.CPUFeature.AVX512DQ; +import static jdk.vm.ci.amd64.AMD64.CPUFeature.AVX512F; +import static jdk.vm.ci.amd64.AMD64.CPUFeature.AVX512VL; +import static jdk.vm.ci.amd64.AMD64.CPUFeature.BMI1; +import static jdk.vm.ci.amd64.AMD64.CPUFeature.BMI2; +import static jdk.vm.ci.amd64.AMD64.CPUFeature.CLMUL; +import static jdk.vm.ci.amd64.AMD64.CPUFeature.CLWB; +import static jdk.vm.ci.amd64.AMD64.CPUFeature.CMOV; +import static jdk.vm.ci.amd64.AMD64.CPUFeature.CX8; +import static jdk.vm.ci.amd64.AMD64.CPUFeature.FLUSHOPT; +import static jdk.vm.ci.amd64.AMD64.CPUFeature.FMA; +import static jdk.vm.ci.amd64.AMD64.CPUFeature.FXSR; +import static jdk.vm.ci.amd64.AMD64.CPUFeature.LZCNT; +import static jdk.vm.ci.amd64.AMD64.CPUFeature.MMX; +import static jdk.vm.ci.amd64.AMD64.CPUFeature.POPCNT; +import static jdk.vm.ci.amd64.AMD64.CPUFeature.SSE; +import static jdk.vm.ci.amd64.AMD64.CPUFeature.SSE2; +import static jdk.vm.ci.amd64.AMD64.CPUFeature.SSE3; +import static jdk.vm.ci.amd64.AMD64.CPUFeature.SSE4_1; +import static jdk.vm.ci.amd64.AMD64.CPUFeature.SSE4_2; +import static jdk.vm.ci.amd64.AMD64.CPUFeature.SSSE3; + +import java.util.EnumSet; +import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +import org.graalvm.nativeimage.Platform; +import org.graalvm.nativeimage.Platforms; + +import com.oracle.graal.pointsto.util.GraalAccess; +import com.oracle.svm.core.jdk.JDK17OrEarlier; +import com.oracle.svm.core.option.SubstrateOptionsParser; +import com.oracle.svm.core.util.UserError; +import com.oracle.svm.hosted.NativeImageOptions; + +import jdk.vm.ci.amd64.AMD64; +import jdk.vm.ci.amd64.AMD64.CPUFeature; + +/** + * AMD64 CPU types used to implement -march. + *

+ * For reference, see gcc's x86 + * Options. + */ +@Platforms(Platform.HOSTED_ONLY.class) +public enum CPUTypeAMD64 implements CPUType { + /** + * Microarchitecture levels. + *

+ * For reference, see + * x86-64: + * Microarchitecture levels. + */ + X86_64("x86-64", CMOV, CX8, FXSR, MMX, SSE, SSE2), + X86_64_V1("x86-64-v1", X86_64), + X86_64_V2("x86-64-v2", X86_64_V1, POPCNT, SSE3, SSE4_1, SSE4_2, SSSE3), + /** + * F16C are not in {@link JDK17OrEarlier}. Leaving link here for cleanup purposes. + */ + X86_64_V3("x86-64-v3", X86_64_V2, AVX, AVX2, BMI1, BMI2, /* F16C, */ FMA, LZCNT), + X86_64_V4("x86-64-v4", X86_64_V3, AVX512F, AVX512BW, AVX512CD, AVX512DQ, AVX512VL), + + // Intel selection + /** + * F16C are not in {@link JDK17OrEarlier}. Leaving link here for cleanup purposes. + */ + HASWELL("haswell", X86_64, AES, SSE3, SSSE3, SSE4_1, SSE4_2, POPCNT, CLMUL, AVX, /* F16C, */ AVX2, BMI1, BMI2, LZCNT, FMA), + SKYLAKE("skylake", HASWELL, ADX, AMD_3DNOW_PREFETCH, FLUSHOPT), + /** + * PKU are not in {@link JDK17OrEarlier}. Leaving link here for cleanup purposes. + */ + SKYLAKE_AVX512("skylake-avx512", SKYLAKE, /* PKU, */ AVX512F, AVX512CD, AVX512VL, AVX512BW, AVX512DQ, CLWB), + + // Special symbols + COMPATIBILITY(NativeImageOptions.MICRO_ARCHITECTURE_COMPATIBILITY, X86_64), + NATIVE(NativeImageOptions.MICRO_ARCHITECTURE_NATIVE, getNativeOrEmpty()); + + private static CPUFeature[] getNativeOrEmpty() { + CPUFeature[] empty = new CPUFeature[0]; + if (GraalAccess.getOriginalTarget().arch instanceof AMD64 arch) { + return arch.getFeatures().toArray(empty); + } else { + return empty; + } + } + + private final String name; + private final CPUTypeAMD64 parent; + private final EnumSet specificFeatures; + + CPUTypeAMD64(String cpuTypeName, CPUFeature... features) { + this(cpuTypeName, null, features); + } + + CPUTypeAMD64(String cpuTypeName, CPUTypeAMD64 cpuTypeParent, CPUFeature... features) { + name = cpuTypeName; + parent = cpuTypeParent; + specificFeatures = features.length > 0 ? EnumSet.copyOf(List.of(features)) : EnumSet.noneOf(CPUFeature.class); + assert parent == null || parent.getFeatures().stream().noneMatch(f -> specificFeatures.contains(f)) : "duplicate features detected but not allowed"; + } + + @Override + public String getName() { + return name; + } + + @Override + public CPUTypeAMD64 getParent() { + return parent; + } + + @Override + public String getSpecificFeaturesString() { + return specificFeatures.stream().map(f -> f.name()).collect(Collectors.joining(" + ")); + } + + public EnumSet getFeatures() { + if (parent == null) { + return specificFeatures; + } else { + return EnumSet.copyOf(Stream.concat(parent.getFeatures().stream(), specificFeatures.stream()).toList()); + } + } + + @Platforms(Platform.HOSTED_ONLY.class) + public static EnumSet getSelectedFeatures() { + String value = NativeImageOptions.MicroArchitecture.getValue(); + if (value == null) { + if (NATIVE.getFeatures().containsAll(X86_64_V3.getFeatures())) { + value = X86_64_V3.getName(); + } else { + System.out.printf("Warning: The host machine does not support all features of '%s'. Falling back to '%s' for best compatibility.%n", + X86_64_V3.getName(), SubstrateOptionsParser.commandArgument(NativeImageOptions.MicroArchitecture, COMPATIBILITY.getName())); + value = COMPATIBILITY.getName(); + } + } + return getCPUFeaturesForArch(value); + } + + public static EnumSet getCPUFeaturesForArch(String marchValue) { + CPUTypeAMD64 value = typeOf(marchValue); + if (value == null) { + throw UserError.abort("Unsupported architecture '%s'. Please adjust '%s'. On AMD64, only %s are available.", + marchValue, + SubstrateOptionsParser.commandArgument(NativeImageOptions.MicroArchitecture, marchValue), + List.of(values()).stream().map(v -> v.name).collect(Collectors.joining(", "))); + } + return value.getFeatures(); + } + + private static CPUTypeAMD64 typeOf(String marchValue) { + for (CPUTypeAMD64 value : values()) { + if (value.name.equals(marchValue)) { + return value; + } + } + return null; + } +} From 2f728fc527d4911dcd11ef90f866f9eedb7792d8 Mon Sep 17 00:00:00 2001 From: Fabio Niephaus Date: Fri, 24 Feb 2023 14:51:30 +0100 Subject: [PATCH 2/2] Compile GraalVM images with `-march=compatibility`. --- sdk/mx.sdk/mx_sdk_vm_impl.py | 1 + 1 file changed, 1 insertion(+) diff --git a/sdk/mx.sdk/mx_sdk_vm_impl.py b/sdk/mx.sdk/mx_sdk_vm_impl.py index c59c031c3a4b..fe71617a707b 100644 --- a/sdk/mx.sdk/mx_sdk_vm_impl.py +++ b/sdk/mx.sdk/mx_sdk_vm_impl.py @@ -1255,6 +1255,7 @@ def contents(self): image_config = self.subject.image_config build_args = [ '--no-fallback', + '-march=compatibility', # Target maximum portability of all GraalVM images. '-H:+AssertInitializationSpecifiedForAllClasses', '-H:+EnforceMaxRuntimeCompileMethods', '-Dorg.graalvm.version={}'.format(_suite.release_version()),