Skip to content

Commit

Permalink
[GR-53919] Remove descriptions of allocation-related runtime routines…
Browse files Browse the repository at this point in the history
… that throw exceptions on allocation failure.

PullRequest: graal/17668
  • Loading branch information
mur47x111 committed May 9, 2024
2 parents 10e0538 + e75b0c4 commit 033c93a
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 177 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -516,12 +516,6 @@ public int threadTlabTopOffset() {
public final long unsafeArraycopy = getFieldValue("StubRoutines::_unsafe_arraycopy", Long.class, "address");
public final long genericArraycopy = getFieldValue("StubRoutines::_generic_arraycopy", Long.class, "address");

// Allocation stubs that throw an exception when allocation fails
public final long newInstanceAddress = getAddress("JVMCIRuntime::new_instance");
public final long newArrayAddress = getAddress("JVMCIRuntime::new_array");
public final long newMultiArrayAddress = getAddress("JVMCIRuntime::new_multi_array");
public final long dynamicNewInstanceAddress = getAddress("JVMCIRuntime::dynamic_new_instance");

// Allocation stubs that return null when allocation fails
public final long newInstanceOrNullAddress = getAddress("JVMCIRuntime::new_instance_or_null");
public final long newArrayOrNullAddress = getAddress("JVMCIRuntime::new_array_or_null");
Expand All @@ -530,23 +524,14 @@ public int threadTlabTopOffset() {

public final long invokeJavaMethodAddress = getAddress("JVMCIRuntime::invoke_static_method_one_arg");

public boolean areNullAllocationStubsAvailable() {
return newInstanceOrNullAddress != 0L;
}

/**
* Checks that HotSpot implements all or none of the allocate-or-null stubs.
*/
private boolean checkNullAllocationStubs() {
if (newInstanceOrNullAddress == 0L) {
assert newArrayOrNullAddress == 0L : newArrayOrNullAddress;
assert newMultiArrayOrNullAddress == 0L : newMultiArrayOrNullAddress;
assert dynamicNewInstanceOrNullAddress == 0L : dynamicNewInstanceAddress;
} else {
assert newArrayOrNullAddress != 0L;
assert newMultiArrayOrNullAddress != 0L;
assert dynamicNewInstanceOrNullAddress != 0L;
}
assert newInstanceOrNullAddress != 0L;
assert newArrayOrNullAddress != 0L;
assert newMultiArrayOrNullAddress != 0L;
assert dynamicNewInstanceOrNullAddress != 0L;
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@
import org.graalvm.collections.EconomicSet;
import org.graalvm.collections.Equivalence;
import org.graalvm.collections.MapCursor;
import org.graalvm.word.LocationIdentity;
import org.graalvm.word.WordBase;

import jdk.graal.compiler.code.CompilationResult;
import jdk.graal.compiler.core.common.CompilationIdentifier;
import jdk.graal.compiler.core.common.alloc.RegisterAllocationConfig;
Expand Down Expand Up @@ -77,9 +80,6 @@
import jdk.graal.compiler.options.OptionValues;
import jdk.graal.compiler.phases.tiers.SuitesProvider;
import jdk.graal.compiler.word.Word;
import org.graalvm.word.LocationIdentity;
import org.graalvm.word.WordBase;

import jdk.vm.ci.code.CallingConvention;
import jdk.vm.ci.code.CompilationRequest;
import jdk.vm.ci.code.CompiledCode;
Expand Down Expand Up @@ -269,41 +269,29 @@ public static void unsafeArraycopy(Word srcAddr, Word dstAddr, Word size) {

private static final LocationIdentity[] TLAB_LOCATIONS = new LocationIdentity[]{TLAB_TOP_LOCATION, TLAB_END_LOCATION};

/**
* New multi array stub that throws an {@link OutOfMemoryError} on allocation failure.
*/
public static final HotSpotForeignCallDescriptor NEW_MULTI_ARRAY = new HotSpotForeignCallDescriptor(SAFEPOINT, NO_SIDE_EFFECT, TLAB_LOCATIONS, "new_multi_array", Object.class, KlassPointer.class,
int.class, Word.class);

/**
* New multi array stub that will return null on allocation failure.
*/
public static final HotSpotForeignCallDescriptor NEW_MULTI_ARRAY_OR_NULL = new HotSpotForeignCallDescriptor(SAFEPOINT, NO_SIDE_EFFECT, TLAB_LOCATIONS, "new_multi_array_or_null", Object.class,
KlassPointer.class, int.class, Word.class);

/**
* New array stub that throws an {@link OutOfMemoryError} on allocation failure.
*/
public static final HotSpotForeignCallDescriptor NEW_ARRAY = new HotSpotForeignCallDescriptor(SAFEPOINT, NO_SIDE_EFFECT, TLAB_LOCATIONS, "new_array", Object.class, KlassPointer.class, int.class);

/**
* New array stub that will return null on allocation failure.
*/
public static final HotSpotForeignCallDescriptor NEW_ARRAY_OR_NULL = new HotSpotForeignCallDescriptor(SAFEPOINT, NO_SIDE_EFFECT, TLAB_LOCATIONS, "new_array_or_null", Object.class,
KlassPointer.class,
int.class);

/**
* New instance stub that throws an {@link OutOfMemoryError} on allocation failure.
*/
public static final HotSpotForeignCallDescriptor NEW_INSTANCE = new HotSpotForeignCallDescriptor(SAFEPOINT, NO_SIDE_EFFECT, TLAB_LOCATIONS, "new_instance", Object.class, KlassPointer.class);

/**
* New instance stub that will return null on allocation failure.
*/
public static final HotSpotForeignCallDescriptor NEW_INSTANCE_OR_NULL = new HotSpotForeignCallDescriptor(SAFEPOINT, NO_SIDE_EFFECT, TLAB_LOCATIONS, "new_instance_or_null", Object.class,
KlassPointer.class);

/** New dynamic array stub that returns null on allocation failure. */
public static final HotSpotForeignCallDescriptor DYNAMIC_NEW_INSTANCE_OR_NULL = new HotSpotForeignCallDescriptor(SAFEPOINT, NO_SIDE_EFFECT, NO_LOCATIONS, "dynamic_new_instance_or_null",
Object.class, Class.class);

public HotSpotBackend(HotSpotGraalRuntimeProvider runtime, HotSpotProviders providers) {
super(providers);
this.runtime = runtime;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import static jdk.graal.compiler.hotspot.HotSpotBackend.BIGINTEGER_LEFT_SHIFT_WORKER;
import static jdk.graal.compiler.hotspot.HotSpotBackend.BIGINTEGER_RIGHT_SHIFT_WORKER;
import static jdk.graal.compiler.hotspot.HotSpotBackend.CHACHA20Block;
import static jdk.graal.compiler.hotspot.HotSpotBackend.DYNAMIC_NEW_INSTANCE_OR_NULL;
import static jdk.graal.compiler.hotspot.HotSpotBackend.ELECTRONIC_CODEBOOK_DECRYPT_AESCRYPT;
import static jdk.graal.compiler.hotspot.HotSpotBackend.ELECTRONIC_CODEBOOK_ENCRYPT_AESCRYPT;
import static jdk.graal.compiler.hotspot.HotSpotBackend.EXCEPTION_HANDLER;
Expand All @@ -41,11 +42,8 @@
import static jdk.graal.compiler.hotspot.HotSpotBackend.MD5_IMPL_COMPRESS_MB;
import static jdk.graal.compiler.hotspot.HotSpotBackend.MONTGOMERY_MULTIPLY;
import static jdk.graal.compiler.hotspot.HotSpotBackend.MONTGOMERY_SQUARE;
import static jdk.graal.compiler.hotspot.HotSpotBackend.NEW_ARRAY;
import static jdk.graal.compiler.hotspot.HotSpotBackend.NEW_ARRAY_OR_NULL;
import static jdk.graal.compiler.hotspot.HotSpotBackend.NEW_INSTANCE;
import static jdk.graal.compiler.hotspot.HotSpotBackend.NEW_INSTANCE_OR_NULL;
import static jdk.graal.compiler.hotspot.HotSpotBackend.NEW_MULTI_ARRAY;
import static jdk.graal.compiler.hotspot.HotSpotBackend.NEW_MULTI_ARRAY_OR_NULL;
import static jdk.graal.compiler.hotspot.HotSpotBackend.POLY1305_PROCESSBLOCKS;
import static jdk.graal.compiler.hotspot.HotSpotBackend.SHA2_IMPL_COMPRESS_MB;
Expand All @@ -72,8 +70,6 @@
import static jdk.graal.compiler.hotspot.meta.HotSpotForeignCallDescriptor.Transition.LEAF;
import static jdk.graal.compiler.hotspot.meta.HotSpotForeignCallDescriptor.Transition.LEAF_NO_VZERO;
import static jdk.graal.compiler.hotspot.meta.HotSpotForeignCallDescriptor.Transition.SAFEPOINT;
import static jdk.graal.compiler.hotspot.replacements.HotSpotAllocationSnippets.DYNAMIC_NEW_INSTANCE;
import static jdk.graal.compiler.hotspot.replacements.HotSpotAllocationSnippets.DYNAMIC_NEW_INSTANCE_OR_NULL;
import static jdk.graal.compiler.hotspot.replacements.HotSpotG1WriteBarrierSnippets.G1WBPOSTCALL;
import static jdk.graal.compiler.hotspot.replacements.HotSpotG1WriteBarrierSnippets.G1WBPRECALL;
import static jdk.graal.compiler.hotspot.replacements.HotSpotG1WriteBarrierSnippets.VALIDATE_OBJECT;
Expand Down Expand Up @@ -474,17 +470,10 @@ public void initialize(HotSpotProviders providers, OptionValues options) {

registerForeignCall(VM_MESSAGE_C, c.vmMessageAddress, NativeCall);

linkForeignCall(options, providers, NEW_INSTANCE, c.newInstanceAddress, PREPEND_THREAD);
linkForeignCall(options, providers, NEW_ARRAY, c.newArrayAddress, PREPEND_THREAD);
linkForeignCall(options, providers, NEW_MULTI_ARRAY, c.newMultiArrayAddress, PREPEND_THREAD);
linkForeignCall(options, providers, DYNAMIC_NEW_INSTANCE, c.dynamicNewInstanceAddress, PREPEND_THREAD);

if (c.areNullAllocationStubsAvailable()) {
linkForeignCall(options, providers, NEW_INSTANCE_OR_NULL, c.newInstanceOrNullAddress, PREPEND_THREAD);
linkForeignCall(options, providers, NEW_ARRAY_OR_NULL, c.newArrayOrNullAddress, PREPEND_THREAD);
linkForeignCall(options, providers, NEW_MULTI_ARRAY_OR_NULL, c.newMultiArrayOrNullAddress, PREPEND_THREAD);
linkForeignCall(options, providers, DYNAMIC_NEW_INSTANCE_OR_NULL, c.dynamicNewInstanceOrNullAddress, PREPEND_THREAD);
}
linkForeignCall(options, providers, NEW_INSTANCE_OR_NULL, c.newInstanceOrNullAddress, PREPEND_THREAD);
linkForeignCall(options, providers, NEW_ARRAY_OR_NULL, c.newArrayOrNullAddress, PREPEND_THREAD);
linkForeignCall(options, providers, NEW_MULTI_ARRAY_OR_NULL, c.newMultiArrayOrNullAddress, PREPEND_THREAD);
linkForeignCall(options, providers, DYNAMIC_NEW_INSTANCE_OR_NULL, c.dynamicNewInstanceOrNullAddress, PREPEND_THREAD);

if (c.supportJVMTIVThreadNotification()) {
linkForeignCall(options, providers, SHAREDRUNTIME_NOTIFY_JVMTI_VTHREAD_START, c.jvmtiVThreadStart, DONT_PREPEND_THREAD);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,12 @@
package jdk.graal.compiler.hotspot.replacements;

import static jdk.graal.compiler.core.common.GraalOptions.MinimalBulkZeroingSize;
import static jdk.graal.compiler.core.common.spi.ForeignCallDescriptor.CallSideEffect.NO_SIDE_EFFECT;
import static jdk.graal.compiler.hotspot.GraalHotSpotVMConfig.INJECTED_OPTIONVALUES;
import static jdk.graal.compiler.hotspot.GraalHotSpotVMConfig.INJECTED_VMCONFIG;
import static jdk.graal.compiler.hotspot.HotSpotBackend.NEW_ARRAY;
import static jdk.graal.compiler.hotspot.HotSpotBackend.DYNAMIC_NEW_INSTANCE_OR_NULL;
import static jdk.graal.compiler.hotspot.HotSpotBackend.NEW_ARRAY_OR_NULL;
import static jdk.graal.compiler.hotspot.HotSpotBackend.NEW_INSTANCE;
import static jdk.graal.compiler.hotspot.HotSpotBackend.NEW_INSTANCE_OR_NULL;
import static jdk.graal.compiler.hotspot.HotSpotBackend.NEW_MULTI_ARRAY;
import static jdk.graal.compiler.hotspot.HotSpotBackend.NEW_MULTI_ARRAY_OR_NULL;
import static jdk.graal.compiler.hotspot.meta.HotSpotForeignCallDescriptor.Transition.SAFEPOINT;
import static jdk.graal.compiler.hotspot.meta.HotSpotForeignCallsProviderImpl.NO_LOCATIONS;
import static jdk.graal.compiler.hotspot.replacements.HotSpotReplacementsUtil.CLASS_ARRAY_KLASS_LOCATION;
import static jdk.graal.compiler.hotspot.replacements.HotSpotReplacementsUtil.CLASS_INIT_STATE_LOCATION;
import static jdk.graal.compiler.hotspot.replacements.HotSpotReplacementsUtil.CLASS_INIT_THREAD_LOCATION;
Expand Down Expand Up @@ -89,7 +84,6 @@
import jdk.graal.compiler.graph.Node.ConstantNodeParameter;
import jdk.graal.compiler.graph.Node.NodeIntrinsic;
import jdk.graal.compiler.hotspot.GraalHotSpotVMConfig;
import jdk.graal.compiler.hotspot.meta.HotSpotForeignCallDescriptor;
import jdk.graal.compiler.hotspot.meta.HotSpotProviders;
import jdk.graal.compiler.hotspot.meta.HotSpotRegistersProvider;
import jdk.graal.compiler.hotspot.nodes.KlassBeingInitializedCheckNode;
Expand Down Expand Up @@ -135,12 +129,6 @@
import jdk.vm.ci.meta.ResolvedJavaType;

public class HotSpotAllocationSnippets extends AllocationSnippets {
/** New dynamic array stub that throws an {@link OutOfMemoryError} on allocation failure. */
public static final HotSpotForeignCallDescriptor DYNAMIC_NEW_INSTANCE = new HotSpotForeignCallDescriptor(SAFEPOINT, NO_SIDE_EFFECT, NO_LOCATIONS, "dynamic_new_instance", Object.class,
Class.class);
/** New dynamic array stub that returns null on allocation failure. */
public static final HotSpotForeignCallDescriptor DYNAMIC_NEW_INSTANCE_OR_NULL = new HotSpotForeignCallDescriptor(SAFEPOINT, NO_SIDE_EFFECT, NO_LOCATIONS, "dynamic_new_instance_or_null",
Object.class, Class.class);

private final GraalHotSpotVMConfig config;
private final Register threadRegister;
Expand Down Expand Up @@ -211,11 +199,7 @@ public Object allocateInstanceDynamic(@NonNullParameter Class<?> type,
DeoptimizeNode.deopt(None, RuntimeConstraint);
}
}
if (withException) {
return PiNode.piCastToSnippetReplaceeStamp(dynamicNewInstanceWithExceptionStub(type));
} else {
return PiNode.piCastToSnippetReplaceeStamp(dynamicNewInstanceStub(type));
}
return PiNode.piCastToSnippetReplaceeStamp(dynamicNewInstanceStub(type, withException));
}

@Snippet
Expand Down Expand Up @@ -350,62 +334,35 @@ protected final int getPrefetchDistance() {
}

@Override
protected final Object callNewInstanceStub(Word hub) {
protected final Object callNewInstanceStub(Word hub, boolean withException) {
KlassPointer klassPtr = KlassPointer.fromWord(hub);
if (useNullAllocationStubs(INJECTED_VMCONFIG)) {
if (!withException) {
return nonNullOrDeopt(newInstanceOrNull(NEW_INSTANCE_OR_NULL, klassPtr));
} else {
return newInstance(NEW_INSTANCE, klassPtr);
return nonNullOrDeopt(newInstanceWithException(NEW_INSTANCE_OR_NULL, klassPtr));
}
}

@Override
protected Object callNewInstanceWithExceptionStub(Word hub) {
KlassPointer klassPtr = KlassPointer.fromWord(hub);
return newInstanceWithException(NEW_INSTANCE, klassPtr);
}

@NodeIntrinsic(value = ForeignCallNode.class, injectedStampIsNonNull = true)
private static native Object newInstance(@ConstantNodeParameter ForeignCallDescriptor descriptor, KlassPointer hub);

@NodeIntrinsic(value = ForeignCallWithExceptionNode.class, injectedStampIsNonNull = true)
@NodeIntrinsic(value = ForeignCallWithExceptionNode.class)
private static native Object newInstanceWithException(@ConstantNodeParameter ForeignCallDescriptor descriptor, KlassPointer hub);

@NodeIntrinsic(value = ForeignCallNode.class, injectedStampIsNonNull = false)
@NodeIntrinsic(value = ForeignCallNode.class)
private static native Object newInstanceOrNull(@ConstantNodeParameter ForeignCallDescriptor descriptor, KlassPointer hub);

/**
* When allocating on the slow path, determines whether to use a version of the runtime call
* that returns {@code null} on a failed allocation instead of raising an OutOfMemoryError.
*/
@Fold
static boolean useNullAllocationStubs(@InjectedParameter GraalHotSpotVMConfig config) {
return config.areNullAllocationStubsAvailable();
}

@Override
protected final Object callNewArrayStub(Word hub, int length) {
protected final Object callNewArrayStub(Word hub, int length, boolean withException) {
KlassPointer klassPtr = KlassPointer.fromWord(hub);
if (useNullAllocationStubs(INJECTED_VMCONFIG)) {
if (!withException) {
return nonNullOrDeopt(newArrayOrNull(NEW_ARRAY_OR_NULL, klassPtr, length));
} else {
return newArray(NEW_ARRAY, klassPtr, length);
return nonNullOrDeopt(newArrayWithException(NEW_ARRAY_OR_NULL, klassPtr, length));
}
}

@Override
protected Object callNewArrayWithExceptionStub(Word hub, int length) {
KlassPointer klassPtr = KlassPointer.fromWord(hub);
return newArrayWithException(NEW_ARRAY, klassPtr, length);
}

@NodeIntrinsic(value = ForeignCallNode.class, injectedStampIsNonNull = true)
private static native Object newArray(@ConstantNodeParameter ForeignCallDescriptor descriptor, KlassPointer hub, int length);

@NodeIntrinsic(value = ForeignCallWithExceptionNode.class, injectedStampIsNonNull = true)
@NodeIntrinsic(value = ForeignCallWithExceptionNode.class)
private static native Object newArrayWithException(@ConstantNodeParameter ForeignCallDescriptor descriptor, KlassPointer hub, int length);

@NodeIntrinsic(value = ForeignCallNode.class, injectedStampIsNonNull = false)
@NodeIntrinsic(value = ForeignCallNode.class)
private static native Object newArrayOrNull(@ConstantNodeParameter ForeignCallDescriptor descriptor, KlassPointer hub, int length);

/**
Expand All @@ -418,50 +375,34 @@ private static Object nonNullOrDeopt(Object obj) {
return obj;
}

public static Object dynamicNewInstanceStub(Class<?> elementType) {
if (useNullAllocationStubs(INJECTED_VMCONFIG)) {
public static Object dynamicNewInstanceStub(Class<?> elementType, boolean withException) {
if (!withException) {
return nonNullOrDeopt(dynamicNewInstanceOrNull(DYNAMIC_NEW_INSTANCE_OR_NULL, elementType));
} else {
return dynamicNewInstance(DYNAMIC_NEW_INSTANCE, elementType);
return nonNullOrDeopt(dynamicNewInstanceWithException(DYNAMIC_NEW_INSTANCE_OR_NULL, elementType));
}
}

public static Object dynamicNewInstanceWithExceptionStub(Class<?> elementType) {
return dynamicNewInstanceWithException(DYNAMIC_NEW_INSTANCE, elementType);
}

@NodeIntrinsic(value = ForeignCallNode.class, injectedStampIsNonNull = true)
public static native Object dynamicNewInstance(@ConstantNodeParameter ForeignCallDescriptor descriptor, Class<?> elementType);

@NodeIntrinsic(value = ForeignCallWithExceptionNode.class, injectedStampIsNonNull = true)
@NodeIntrinsic(value = ForeignCallWithExceptionNode.class)
public static native Object dynamicNewInstanceWithException(@ConstantNodeParameter ForeignCallDescriptor descriptor, Class<?> elementType);

@NodeIntrinsic(value = ForeignCallNode.class, injectedStampIsNonNull = false)
@NodeIntrinsic(value = ForeignCallNode.class)
public static native Object dynamicNewInstanceOrNull(@ConstantNodeParameter ForeignCallDescriptor descriptor, Class<?> elementType);

@Override
protected final Object callNewMultiArrayStub(Word hub, int rank, Word dims) {
protected final Object callNewMultiArrayStub(Word hub, int rank, Word dims, boolean withException) {
KlassPointer klassPointer = KlassPointer.fromWord(hub);
if (useNullAllocationStubs(INJECTED_VMCONFIG)) {
if (!withException) {
return nonNullOrDeopt(newMultiArrayOrNull(NEW_MULTI_ARRAY_OR_NULL, klassPointer, rank, dims));
} else {
return newMultiArray(NEW_MULTI_ARRAY, klassPointer, rank, dims);
return nonNullOrDeopt(newMultiArrayWithException(NEW_MULTI_ARRAY_OR_NULL, klassPointer, rank, dims));
}
}

@Override
protected Object callNewMultiArrayStubWithException(Word hub, int rank, Word dims) {
KlassPointer klassPointer = KlassPointer.fromWord(hub);
return newMultiArrayWithException(NEW_MULTI_ARRAY, klassPointer, rank, dims);
}

@NodeIntrinsic(value = ForeignCallNode.class, injectedStampIsNonNull = true)
private static native Object newMultiArray(@ConstantNodeParameter ForeignCallDescriptor descriptor, KlassPointer hub, int rank, Word dims);

@NodeIntrinsic(value = ForeignCallWithExceptionNode.class, injectedStampIsNonNull = true)
@NodeIntrinsic(value = ForeignCallWithExceptionNode.class)
private static native Object newMultiArrayWithException(@ConstantNodeParameter ForeignCallDescriptor descriptor, KlassPointer hub, int rank, Word dims);

@NodeIntrinsic(value = ForeignCallNode.class, injectedStampIsNonNull = false)
@NodeIntrinsic(value = ForeignCallNode.class)
private static native Object newMultiArrayOrNull(@ConstantNodeParameter ForeignCallDescriptor descriptor, KlassPointer hub, int rank, Word dims);

@Fold
Expand Down

0 comments on commit 033c93a

Please sign in to comment.