Skip to content

Commit

Permalink
[GR-51303] Rework VectorSupport substitutions
Browse files Browse the repository at this point in the history
PullRequest: graal/18815
  • Loading branch information
gergo- committed Sep 27, 2024
2 parents fe71ff7 + 8c91838 commit e9e3413
Show file tree
Hide file tree
Showing 6 changed files with 278 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021, 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2021, 2024, 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
Expand Down Expand Up @@ -114,6 +114,7 @@
import jdk.graal.compiler.phases.common.CanonicalizerPhase.CustomSimplification;
import jdk.graal.compiler.phases.common.inlining.InliningUtil;
import jdk.graal.compiler.printer.GraalDebugHandlersFactory;
import jdk.graal.compiler.replacements.nodes.MacroNode;
import jdk.vm.ci.meta.Constant;
import jdk.vm.ci.meta.JavaConstant;
import jdk.vm.ci.meta.JavaKind;
Expand Down Expand Up @@ -390,10 +391,11 @@ private TypeFlow<?> getNodeFlow(Node node) {

@Override
public void simplify(Node n, SimplifierTool tool) {
if (n instanceof ValueNode && !(n instanceof LimitedValueProxy) && !(n instanceof PhiNode)) {
if (n instanceof ValueNode && !(n instanceof LimitedValueProxy) && !(n instanceof PhiNode) && !(n instanceof MacroNode)) {
/*
* The stamp of proxy nodes and phi nodes is inferred automatically, so we do not
* need to improve them.
* need to improve them. Macro nodes prohibit changing their stamp because it is
* derived from the macro's fallback invoke.
*/
ValueNode node = (ValueNode) n;
/*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2013, 2023, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2013, 2024, 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
Expand Down Expand Up @@ -1280,6 +1280,9 @@ public static boolean includeAll() {
@Option(help = "Support for calls via the Java Foreign Function and Memory API", type = Expert) //
public static final HostedOptionKey<Boolean> ForeignAPISupport = new HostedOptionKey<>(false);

@Option(help = "Support for intrinsics from the Java Vector API", type = Expert) //
public static final HostedOptionKey<Boolean> VectorAPISupport = new HostedOptionKey<>(false);

@Option(help = "Assume new types cannot be added after analysis", type = OptionType.Expert) //
public static final HostedOptionKey<Boolean> ClosedTypeWorld = new HostedOptionKey<>(true) {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,112 @@
*/
package com.oracle.svm.core.jdk;

import com.oracle.svm.core.AlwaysInline;
import com.oracle.svm.core.annotate.Alias;
import com.oracle.svm.core.annotate.AnnotateOriginal;
import com.oracle.svm.core.annotate.Delete;
import com.oracle.svm.core.annotate.RecomputeFieldValue;
import com.oracle.svm.core.annotate.Substitute;
import com.oracle.svm.core.annotate.TargetClass;
import com.oracle.svm.core.annotate.TargetElement;

@TargetClass(className = "jdk.internal.vm.vector.VectorSupport")
public final class Target_jdk_internal_vm_vector_VectorSupport {
final class Target_jdk_internal_vm_vector_VectorSupport {
@Delete
private static native int registerNatives();

@SuppressWarnings("unused")
@Substitute
public static int getMaxLaneCount(Class<?> etype) {
private static int getMaxLaneCount(Class<?> etype) {
return VectorAPISupport.singleton().getMaxLaneCount(etype);
}
}

@TargetClass(className = "jdk.incubator.vector.LaneType", onlyWith = VectorAPIEnabled.class)
final class Target_jdk_incubator_vector_LaneType {

}

@TargetClass(className = "jdk.incubator.vector.VectorOperators", onlyWith = VectorAPIEnabled.class)
final class Target_jdk_incubator_vector_VectorOperators {
@TargetClass(className = "jdk.incubator.vector.VectorOperators", innerClass = "ConversionImpl", onlyWith = VectorAPIEnabled.class)
private static final class Target_jdk_incubator_vector_VectorOperators_ConversionImpl<E, F> {
/*
* GR-51303: The Vector API is not yet optimized by the Graal compiler. But instead of
* letting applications fail, always return a max lane count of zero for now.
* The following methods are not annotated @ForceInline in the JDK although the Vector API
* implementation clearly expects them to be inlined. They are on the hot path, and both
* their hot path callers and their callees are all @ForceInline.
*/
return 0;

@AnnotateOriginal
@AlwaysInline("Vector API performance")
private static native Target_jdk_incubator_vector_VectorOperators_ConversionImpl<?, ?> ofCopy(Target_jdk_incubator_vector_LaneType dom);

@AnnotateOriginal
@AlwaysInline("Vector API performance")
private static native Target_jdk_incubator_vector_VectorOperators_ConversionImpl<?, ?> ofCast(Target_jdk_incubator_vector_LaneType dom, Target_jdk_incubator_vector_LaneType ran);

@AnnotateOriginal
@AlwaysInline("Vector API performance")
private static native Target_jdk_incubator_vector_VectorOperators_ConversionImpl<?, ?> ofReinterpret(Target_jdk_incubator_vector_LaneType dom, Target_jdk_incubator_vector_LaneType ran);
}
}

@TargetClass(className = "jdk.incubator.vector.ByteVector", onlyWith = VectorAPIEnabled.class)
final class Target_jdk_incubator_vector_ByteVector {
@Alias @RecomputeFieldValue(kind = RecomputeFieldValue.Kind.ArrayIndexShift, declClass = byte[].class, isFinal = true) //
@TargetElement(name = "ARRAY_SHIFT") //
private static int arrayShift;
@Alias @RecomputeFieldValue(kind = RecomputeFieldValue.Kind.ArrayBaseOffset, declClass = byte[].class, isFinal = true) //
@TargetElement(name = "ARRAY_BASE") //
private static long arrayBase;
}

@TargetClass(className = "jdk.incubator.vector.ShortVector", onlyWith = VectorAPIEnabled.class)
final class Target_jdk_incubator_vector_ShortVector {
@Alias @RecomputeFieldValue(kind = RecomputeFieldValue.Kind.ArrayIndexShift, declClass = short[].class, isFinal = true) //
@TargetElement(name = "ARRAY_SHIFT") //
private static int arrayShift;
@Alias @RecomputeFieldValue(kind = RecomputeFieldValue.Kind.ArrayBaseOffset, declClass = short[].class, isFinal = true) //
@TargetElement(name = "ARRAY_BASE") //
private static long arrayBase;
}

@TargetClass(className = "jdk.incubator.vector.IntVector", onlyWith = VectorAPIEnabled.class)
final class Target_jdk_incubator_vector_IntVector {
@Alias @RecomputeFieldValue(kind = RecomputeFieldValue.Kind.ArrayIndexShift, declClass = int[].class, isFinal = true) //
@TargetElement(name = "ARRAY_SHIFT") //
private static int arrayShift;
@Alias @RecomputeFieldValue(kind = RecomputeFieldValue.Kind.ArrayBaseOffset, declClass = int[].class, isFinal = true) //
@TargetElement(name = "ARRAY_BASE") //
private static long arrayBase;
}

@TargetClass(className = "jdk.incubator.vector.LongVector", onlyWith = VectorAPIEnabled.class)
final class Target_jdk_incubator_vector_LongVector {
@Alias @RecomputeFieldValue(kind = RecomputeFieldValue.Kind.ArrayIndexShift, declClass = long[].class, isFinal = true) //
@TargetElement(name = "ARRAY_SHIFT") //
private static int arrayShift;
@Alias @RecomputeFieldValue(kind = RecomputeFieldValue.Kind.ArrayBaseOffset, declClass = long[].class, isFinal = true) //
@TargetElement(name = "ARRAY_BASE") //
private static long arrayBase;
}

@TargetClass(className = "jdk.incubator.vector.FloatVector", onlyWith = VectorAPIEnabled.class)
final class Target_jdk_incubator_vector_FloatVector {
@Alias @RecomputeFieldValue(kind = RecomputeFieldValue.Kind.ArrayIndexShift, declClass = float[].class, isFinal = true) //
@TargetElement(name = "ARRAY_SHIFT") //
private static int arrayShift;
@Alias @RecomputeFieldValue(kind = RecomputeFieldValue.Kind.ArrayBaseOffset, declClass = float[].class, isFinal = true) //
@TargetElement(name = "ARRAY_BASE") //
private static long arrayBase;
}

@TargetClass(className = "jdk.incubator.vector.DoubleVector", onlyWith = VectorAPIEnabled.class)
final class Target_jdk_incubator_vector_DoubleVector {
@Alias @RecomputeFieldValue(kind = RecomputeFieldValue.Kind.ArrayIndexShift, declClass = double[].class, isFinal = true) //
@TargetElement(name = "ARRAY_SHIFT") //
private static int arrayShift;
@Alias @RecomputeFieldValue(kind = RecomputeFieldValue.Kind.ArrayBaseOffset, declClass = double[].class, isFinal = true) //
@TargetElement(name = "ARRAY_BASE") //
private static long arrayBase;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright (c) 2024, 2024, 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.core.jdk;

import java.util.function.BooleanSupplier;

import com.oracle.svm.core.SubstrateOptions;

public class VectorAPIEnabled implements BooleanSupplier {
@Override
public boolean getAsBoolean() {
return SubstrateOptions.VectorAPISupport.getValue();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
/*
* Copyright (c) 2024, 2024, 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.core.jdk;

import java.util.EnumSet;

import org.graalvm.nativeimage.ImageSingletons;
import org.graalvm.nativeimage.Platform;
import org.graalvm.nativeimage.Platforms;

import com.oracle.svm.core.SubstrateTargetDescription;
import com.oracle.svm.core.feature.AutomaticallyRegisteredImageSingleton;
import com.oracle.svm.core.util.VMError;

import jdk.graal.compiler.asm.amd64.AMD64BaseAssembler;
import jdk.graal.compiler.asm.amd64.AVXKind;
import jdk.vm.ci.aarch64.AArch64;
import jdk.vm.ci.aarch64.AArch64Kind;
import jdk.vm.ci.amd64.AMD64;
import jdk.vm.ci.code.CPUFeatureName;

/**
* Provides access to a computation of the maximum Vector API vector size for the target platform.
*/
@AutomaticallyRegisteredImageSingleton
public final class VectorAPISupport {

/**
* The size in bytes of the target's largest vector type. This value is -1 if the target does
* not support vectors.
*/
private final int maxVectorBytes;

@Platforms(Platform.HOSTED_ONLY.class) //
protected VectorAPISupport() {
SubstrateTargetDescription targetDescription = ImageSingletons.lookup(SubstrateTargetDescription.class);
EnumSet<? extends CPUFeatureName> features = null;
if (targetDescription.arch instanceof AMD64 amd64) {
features = amd64.getFeatures();
} else if (targetDescription.arch instanceof AArch64 aarch64) {
features = aarch64.getFeatures();
}
this.maxVectorBytes = computeMaxVectorBytes(features);
}

public static VectorAPISupport singleton() {
return ImageSingletons.lookup(VectorAPISupport.class);
}

@Platforms(Platform.HOSTED_ONLY.class) //
@SuppressWarnings("unchecked")
private static int computeMaxVectorBytes(EnumSet<? extends CPUFeatureName> features) {
if (features == null) {
return -1;
} else if (features.contains(AArch64.CPUFeature.ASIMD)) {
return AArch64Kind.V128_BYTE.getSizeInBytes();
} else if (features.contains(AMD64.CPUFeature.AVX)) {
if (features.contains(AMD64.CPUFeature.AVX512F) && AMD64BaseAssembler.supportsFullAVX512((EnumSet<AMD64.CPUFeature>) features)) {
return AVXKind.AVXSize.ZMM.getBytes();
} else if (features.contains(AMD64.CPUFeature.AVX2)) {
return AVXKind.AVXSize.YMM.getBytes();
} else if (features.contains(AMD64.CPUFeature.AVX)) {
return AVXKind.AVXSize.XMM.getBytes();
} else {
return -1;
}
}
return -1;
}

/**
* Returns the maximum number of bytes in a vector on the target platform, or -1 if no vectors
* are supported. Must only be called after all image singletons are registered.
*/
@Platforms(Platform.HOSTED_ONLY.class)
public int getMaxVectorBytes() {
return maxVectorBytes;
}

/**
* Returns the maximum number of elements (lanes) in a vector with the given element type. If no
* vectors are supported, returns the same result as if the maximum vector size were 64 bits.
* The {@code etype} must be a compile-time constant specifying one of the element types
* supported by Vector API vectors. Must only be called after all image singletons are
* registered.
*/
public int getMaxLaneCount(Class<?> etype) {
/*
* Like the JDK, use a minimum of 64 bits even if the target vector size computation returns
* -1 to signal that no vectors are available.
*/
int maxVectorBits = Math.max(maxVectorBytes * Byte.SIZE, 64);
int elementBytes;
if (etype == byte.class) {
elementBytes = 1;
} else if (etype == short.class) {
elementBytes = 2;
} else if (etype == int.class || etype == float.class) {
elementBytes = 4;
} else if (etype == long.class || etype == double.class) {
elementBytes = 8;
} else {
throw VMError.shouldNotReachHereUnexpectedInput(etype);
}
return maxVectorBits / (elementBytes * Byte.SIZE);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2023, 2023, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2023, 2024, 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
Expand Down Expand Up @@ -68,11 +68,15 @@ public void duringAnalysis(DuringAnalysisAccess access) {
public void afterAnalysis(AfterAnalysisAccess access) {
var vectorSpeciesClass = ReflectionUtil.lookupClass(true, "jdk.incubator.vector.VectorSpecies");
if (vectorSpeciesClass != null && access.isReachable(vectorSpeciesClass)) {
LogUtils.warning(
"This application uses a preview of the Vector API, which is functional but slow on Native Image because it is not yet optimized by the Graal compiler. Please keep this in mind when evaluating performance.");
warnAboutVectorAPI();
}
}

protected void warnAboutVectorAPI() {
LogUtils.warning(
"This application uses a preview of the Vector API, which is functional but slow on Native Image because it is not yet optimized by the Graal compiler. Please keep this in mind when evaluating performance.");
}

@Override
public void afterCompilation(AfterCompilationAccess access) {
if (SubstrateOptions.BuildOutputBreakdowns.getValue()) {
Expand Down

0 comments on commit e9e3413

Please sign in to comment.