Skip to content

Commit

Permalink
[GR-51267] Invocation profiles are not needed for JIT compilation.
Browse files Browse the repository at this point in the history
PullRequest: graal/16574
  • Loading branch information
Christian Wimmer committed Jan 20, 2024
2 parents 2822209 + 7052ef5 commit b6c73ec
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,25 @@
*/
package com.oracle.svm.core.heap;

import java.util.ArrayList;
import java.util.List;

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

import com.oracle.svm.core.BuildPhaseProvider.ReadyForCompilation;
import com.oracle.svm.core.Uninterruptible;
import com.oracle.svm.core.feature.AutomaticallyRegisteredFeature;
import com.oracle.svm.core.feature.AutomaticallyRegisteredImageSingleton;
import com.oracle.svm.core.feature.InternalFeature;
import com.oracle.svm.core.util.DuplicatedInNativeCode;
import com.oracle.svm.core.util.ImageHeapList;
import com.oracle.svm.core.util.VMError;

/**
* This class holds garbage collection causes that are common and therefore shared between different
* garbage collector implementations.
*/
public class GCCause {
@Platforms(Platform.HOSTED_ONLY.class) private static final ArrayList<GCCause> HostedGCCauseList = new ArrayList<>();

@DuplicatedInNativeCode public static final GCCause JavaLangSystemGC = new GCCause("java.lang.System.gc()", 0);
@DuplicatedInNativeCode public static final GCCause UnitTest = new GCCause("Forced GC in unit test", 1);
Expand All @@ -50,28 +51,13 @@ public class GCCause {
@DuplicatedInNativeCode public static final GCCause JvmtiForceGC = new GCCause("JvmtiEnv ForceGarbageCollection", 4);
@DuplicatedInNativeCode public static final GCCause HeapDump = new GCCause("Heap Dump Initiated GC ", 5);

@UnknownObjectField(availability = ReadyForCompilation.class) protected static GCCause[] GCCauses;

private final int id;
private final String name;

@Platforms(Platform.HOSTED_ONLY.class)
@SuppressWarnings("this-escape")
protected GCCause(String name, int id) {
this.id = id;
this.name = name;
addGCCauseMapping();
}

@Platforms(Platform.HOSTED_ONLY.class)
private void addGCCauseMapping() {
synchronized (HostedGCCauseList) {
while (HostedGCCauseList.size() <= id) {
HostedGCCauseList.add(null);
}
VMError.guarantee(HostedGCCauseList.get(id) == null, "%s and another GCCause have the same id.", name);
HostedGCCauseList.set(id, this);
}
}

public String getName() {
Expand All @@ -84,24 +70,40 @@ public int getId() {
}

public static GCCause fromId(int causeId) {
return GCCauses[causeId];
return getGCCauses().get(causeId);
}

public static GCCause[] getGCCauses() {
return GCCauses;
public static List<GCCause> getGCCauses() {
return ImageSingletons.lookup(GCCauseSupport.class).gcCauses;
}
}

@AutomaticallyRegisteredImageSingleton
class GCCauseSupport {
final List<GCCause> gcCauses = ImageHeapList.create(GCCause.class, null);

@Platforms(Platform.HOSTED_ONLY.class)
public static void cacheReverseMapping() {
GCCauses = HostedGCCauseList.toArray(new GCCause[HostedGCCauseList.size()]);
Object collectGCCauses(Object obj) {
if (obj instanceof GCCause gcCause) {
synchronized (gcCauses) {
int id = gcCause.getId();
while (gcCauses.size() <= id) {
gcCauses.add(null);
}
var existing = gcCauses.set(id, gcCause);
if (existing != null && existing != gcCause) {
throw VMError.shouldNotReachHere("Two GCCause objects have the same id " + id + ": " + gcCause.getName() + ", " + existing.getName());
}
}
}
return obj;
}
}

@AutomaticallyRegisteredFeature
class GCCauseFeature implements InternalFeature {
@Override
public void beforeCompilation(BeforeCompilationAccess access) {
GCCause.cacheReverseMapping();
access.registerAsImmutable(GCCause.GCCauses);
public void duringSetup(DuringSetupAccess access) {
access.registerObjectReplacer(ImageSingletons.lookup(GCCauseSupport.class)::collectGCCauses);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
*/
package com.oracle.svm.core.jfr;

import java.util.List;

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

Expand All @@ -37,10 +39,10 @@ public JfrGCCauseSerializer() {
@Override
public void write(JfrChunkWriter writer) {
// GCCauses has null entries
GCCause[] causes = GCCause.getGCCauses();
List<GCCause> causes = GCCause.getGCCauses();
int nonNullItems = 0;
for (int index = 0; index < causes.length; index++) {
if (causes[index] != null) {
for (GCCause cause : causes) {
if (cause != null) {
nonNullItems++;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,11 @@ public synchronized E get(int index) {

@Override
public synchronized E set(int index, E element) {
modified = true;
return hostedList.set(index, element);
E result = hostedList.set(index, element);
if (result != element) {
modified = true;
}
return result;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public static CallTreeInfo create(AnalysisUniverse aUniverse, Map<AnalysisMethod
Map<AnalysisMethod, RuntimeCompiledMethod> runtimeCompilations = new HashMap<>();
for (var method : aUniverse.getMethods()) {
var rMethod = method.getMultiMethod(RUNTIME_COMPILED_METHOD);
if (rMethod != null && rMethod.isReachable() && !invalidForRuntimeCompilation.containsKey(rMethod)) {
if (rMethod != null && rMethod.isReachable() && !invalidForRuntimeCompilation.containsKey(rMethod) && rMethod.getAnalyzedGraph() != null) {
var origInlinedMethods = rMethod.getAnalyzedGraph().getInlinedMethods().stream().map(inlinedMethod -> {
AnalysisMethod orig = ((AnalysisMethod) inlinedMethod).getMultiMethod(ORIGINAL_METHOD);
assert orig != null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import com.oracle.graal.pointsto.infrastructure.Universe;
import com.oracle.graal.pointsto.meta.AnalysisType;
import com.oracle.graal.pointsto.results.StrengthenGraphs;
import com.oracle.svm.common.meta.MultiMethod;
import com.oracle.svm.core.SubstrateUtil;
import com.oracle.svm.core.Uninterruptible;
import com.oracle.svm.core.graal.nodes.InlinedInvokeArgumentsNode;
Expand Down Expand Up @@ -114,11 +115,20 @@ protected FixedNode createUnreachable(StructuredGraph graph, CoreProviders provi

@Override
protected void setInvokeProfiles(Invoke invoke, JavaTypeProfile typeProfile, JavaMethodProfile methodProfile) {
((SubstrateMethodCallTargetNode) invoke.callTarget()).setProfiles(typeProfile, methodProfile);
if (needsProfiles(invoke)) {
((SubstrateMethodCallTargetNode) invoke.callTarget()).setProfiles(typeProfile, methodProfile);
}
}

protected void setInvokeProfiles(Invoke invoke, JavaTypeProfile typeProfile, JavaMethodProfile methodProfile, JavaTypeProfile staticTypeProfile) {
((SubstrateMethodCallTargetNode) invoke.callTarget()).setProfiles(typeProfile, methodProfile, staticTypeProfile);
if (needsProfiles(invoke)) {
((SubstrateMethodCallTargetNode) invoke.callTarget()).setProfiles(typeProfile, methodProfile, staticTypeProfile);
}
}

private static boolean needsProfiles(Invoke invoke) {
/* We do not need any profiles in methods for JIT compilation at image run time. */
return ((MultiMethod) invoke.asNode().graph().method()).getMultiMethodKey() != SubstrateCompilationDirectives.RUNTIME_COMPILED_METHOD;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,15 @@ private void addObjectToImageHeap(final JavaConstant constant, boolean immutable
boolean references = false;
boolean relocatable = false; /* always false when !spawnIsolates() */

if (!type.isInstantiated()) {
StringBuilder msg = new StringBuilder();
msg.append("Image heap writing found an object whose type was not marked as instantiated by the static analysis: ");
msg.append(type.toJavaName(true)).append(" (").append(type).append(")");
msg.append(System.lineSeparator()).append(" reachable through:").append(System.lineSeparator());
fillReasonStack(msg, reason);
VMError.shouldNotReachHere(msg.toString());
}

if (type.isInstanceClass()) {
final HostedInstanceClass clazz = (HostedInstanceClass) type;
// If the type has a monitor field, it has a reference field that is written.
Expand Down

0 comments on commit b6c73ec

Please sign in to comment.