Skip to content

Commit

Permalink
[GR-49653] Separate RuntimeCompilationSupport from Generic Compilatio…
Browse files Browse the repository at this point in the history
…nSupport.

PullRequest: graal/15872
  • Loading branch information
teshull committed Nov 15, 2023
2 parents 0245980 + bb49fba commit 38d4ba4
Show file tree
Hide file tree
Showing 19 changed files with 728 additions and 606 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,47 +53,6 @@
import java.util.stream.Collectors;

import org.graalvm.collections.EconomicMap;
import jdk.graal.compiler.code.DisassemblerProvider;
import jdk.graal.compiler.core.GraalServiceThread;
import jdk.graal.compiler.core.common.spi.ForeignCallSignature;
import jdk.graal.compiler.core.target.Backend;
import jdk.graal.compiler.debug.DebugContext;
import jdk.graal.compiler.debug.GraalError;
import jdk.graal.compiler.graph.Node;
import jdk.graal.compiler.graph.NodeClass;
import jdk.graal.compiler.hotspot.EncodedSnippets;
import jdk.graal.compiler.hotspot.HotSpotBackend;
import jdk.graal.compiler.hotspot.HotSpotCodeCacheListener;
import jdk.graal.compiler.hotspot.HotSpotForeignCallLinkageImpl;
import jdk.graal.compiler.hotspot.HotSpotForeignCallLinkageImpl.CodeInfo;
import jdk.graal.compiler.hotspot.HotSpotGraalCompiler;
import jdk.graal.compiler.hotspot.HotSpotGraalOptionValues;
import jdk.graal.compiler.hotspot.HotSpotGraalRuntime;
import jdk.graal.compiler.hotspot.HotSpotReplacementsImpl;
import jdk.graal.compiler.hotspot.SnippetObjectConstant;
import jdk.graal.compiler.hotspot.meta.HotSpotHostForeignCallsProvider;
import jdk.graal.compiler.hotspot.meta.HotSpotInvocationPluginProvider;
import jdk.graal.compiler.hotspot.meta.HotSpotProviders;
import jdk.graal.compiler.hotspot.stubs.Stub;
import jdk.graal.compiler.nodes.graphbuilderconf.GeneratedPluginFactory;
import jdk.graal.compiler.nodes.spi.SnippetParameterInfo;
import jdk.graal.compiler.options.Option;
import jdk.graal.compiler.options.OptionDescriptor;
import jdk.graal.compiler.options.OptionDescriptors;
import jdk.graal.compiler.options.OptionDescriptorsMap;
import jdk.graal.compiler.options.OptionKey;
import jdk.graal.compiler.options.OptionValues;
import jdk.graal.compiler.options.OptionsParser;
import jdk.graal.compiler.phases.util.Providers;
import jdk.graal.compiler.serviceprovider.GlobalAtomicLong;
import jdk.graal.compiler.serviceprovider.GraalServices;
import jdk.graal.compiler.serviceprovider.SpeculationReasonGroup;
import jdk.graal.compiler.truffle.PartialEvaluatorConfiguration;
import jdk.graal.compiler.truffle.host.TruffleHostEnvironment;
import jdk.graal.compiler.truffle.hotspot.HotSpotTruffleCompilerImpl;
import jdk.graal.compiler.truffle.hotspot.TruffleCallBoundaryInstrumentationFactory;
import jdk.graal.compiler.truffle.substitutions.GraphBuilderInvocationPluginProvider;
import jdk.graal.compiler.truffle.substitutions.GraphDecoderInvocationPluginProvider;
import org.graalvm.jniutils.JNI;
import org.graalvm.jniutils.JNIExceptionWrapper;
import org.graalvm.jniutils.JNIMethodScope;
Expand Down Expand Up @@ -133,7 +92,7 @@
import com.oracle.svm.core.util.UserError;
import com.oracle.svm.core.util.UserError.UserException;
import com.oracle.svm.core.util.VMError;
import com.oracle.svm.graal.hosted.RuntimeCompilationFeature;
import com.oracle.svm.graal.hosted.GraalCompilerFeature;
import com.oracle.svm.graal.hotspot.libgraal.LibGraalEntryPoints.RuntimeStubInfo;
import com.oracle.svm.hosted.FeatureImpl;
import com.oracle.svm.hosted.FeatureImpl.DuringSetupAccessImpl;
Expand All @@ -144,6 +103,47 @@
import com.oracle.svm.util.ModuleSupport;
import com.oracle.svm.util.ReflectionUtil;

import jdk.graal.compiler.code.DisassemblerProvider;
import jdk.graal.compiler.core.GraalServiceThread;
import jdk.graal.compiler.core.common.spi.ForeignCallSignature;
import jdk.graal.compiler.core.target.Backend;
import jdk.graal.compiler.debug.DebugContext;
import jdk.graal.compiler.debug.GraalError;
import jdk.graal.compiler.graph.Node;
import jdk.graal.compiler.graph.NodeClass;
import jdk.graal.compiler.hotspot.EncodedSnippets;
import jdk.graal.compiler.hotspot.HotSpotBackend;
import jdk.graal.compiler.hotspot.HotSpotCodeCacheListener;
import jdk.graal.compiler.hotspot.HotSpotForeignCallLinkageImpl;
import jdk.graal.compiler.hotspot.HotSpotForeignCallLinkageImpl.CodeInfo;
import jdk.graal.compiler.hotspot.HotSpotGraalCompiler;
import jdk.graal.compiler.hotspot.HotSpotGraalOptionValues;
import jdk.graal.compiler.hotspot.HotSpotGraalRuntime;
import jdk.graal.compiler.hotspot.HotSpotReplacementsImpl;
import jdk.graal.compiler.hotspot.SnippetObjectConstant;
import jdk.graal.compiler.hotspot.meta.HotSpotHostForeignCallsProvider;
import jdk.graal.compiler.hotspot.meta.HotSpotInvocationPluginProvider;
import jdk.graal.compiler.hotspot.meta.HotSpotProviders;
import jdk.graal.compiler.hotspot.stubs.Stub;
import jdk.graal.compiler.nodes.graphbuilderconf.GeneratedPluginFactory;
import jdk.graal.compiler.nodes.spi.SnippetParameterInfo;
import jdk.graal.compiler.options.Option;
import jdk.graal.compiler.options.OptionDescriptor;
import jdk.graal.compiler.options.OptionDescriptors;
import jdk.graal.compiler.options.OptionDescriptorsMap;
import jdk.graal.compiler.options.OptionKey;
import jdk.graal.compiler.options.OptionValues;
import jdk.graal.compiler.options.OptionsParser;
import jdk.graal.compiler.phases.util.Providers;
import jdk.graal.compiler.serviceprovider.GlobalAtomicLong;
import jdk.graal.compiler.serviceprovider.GraalServices;
import jdk.graal.compiler.serviceprovider.SpeculationReasonGroup;
import jdk.graal.compiler.truffle.PartialEvaluatorConfiguration;
import jdk.graal.compiler.truffle.host.TruffleHostEnvironment;
import jdk.graal.compiler.truffle.hotspot.HotSpotTruffleCompilerImpl;
import jdk.graal.compiler.truffle.hotspot.TruffleCallBoundaryInstrumentationFactory;
import jdk.graal.compiler.truffle.substitutions.GraphBuilderInvocationPluginProvider;
import jdk.graal.compiler.truffle.substitutions.GraphDecoderInvocationPluginProvider;
import jdk.vm.ci.code.CompilationRequest;
import jdk.vm.ci.code.CompilationRequestResult;
import jdk.vm.ci.hotspot.HotSpotConstantReflectionProvider;
Expand Down Expand Up @@ -200,7 +200,7 @@ public boolean isInConfiguration(IsInConfigurationAccess access) {

@Override
public List<Class<? extends Feature>> getRequiredFeatures() {
return List.of(JNIFeature.class, RuntimeCompilationFeature.getRuntimeCompilationFeature(), ReflectionFeature.class);
return List.of(JNIFeature.class, GraalCompilerFeature.class, ReflectionFeature.class);
}

public static final class IsEnabled implements BooleanSupplier {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
/*
* Copyright (c) 2015, 2017, 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.graal;

import static org.graalvm.word.LocationIdentity.ANY_LOCATION;

import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.graalvm.collections.EconomicMap;
import org.graalvm.nativeimage.ImageSingletons;
import org.graalvm.nativeimage.Platform;
import org.graalvm.nativeimage.Platforms;
import org.graalvm.nativeimage.hosted.Feature.DuringAnalysisAccess;
import org.graalvm.word.Pointer;
import org.graalvm.word.WordFactory;

import com.oracle.svm.core.c.CGlobalData;
import com.oracle.svm.core.c.CGlobalDataFactory;
import com.oracle.svm.core.util.ImageHeapMap;
import com.oracle.svm.hosted.FeatureImpl.DuringAnalysisAccessImpl;

import jdk.graal.compiler.core.gen.NodeMatchRules;
import jdk.graal.compiler.core.match.MatchStatement;
import jdk.graal.compiler.debug.DebugHandlersFactory;
import jdk.graal.compiler.graph.Node;
import jdk.graal.compiler.graph.NodeClass;
import jdk.graal.compiler.lir.CompositeValueClass;
import jdk.graal.compiler.lir.LIRInstructionClass;
import jdk.graal.compiler.lir.phases.LIRPhase;
import jdk.graal.compiler.phases.BasePhase;
import jdk.graal.compiler.serviceprovider.GraalServices;

/**
* Holds data that is pre-computed during native image generation and accessed at run time during a
* Graal compilation.
*/
public class GraalCompilerSupport {

public final EconomicMap<Class<?>, NodeClass<?>> nodeClasses = ImageHeapMap.create();
public final EconomicMap<Class<?>, LIRInstructionClass<?>> instructionClasses = ImageHeapMap.create();
public final EconomicMap<Class<?>, CompositeValueClass<?>> compositeValueClasses = ImageHeapMap.create();
public HashMap<Class<? extends NodeMatchRules>, EconomicMap<Class<? extends Node>, List<MatchStatement>>> matchRuleRegistry;

protected EconomicMap<Class<?>, BasePhase.BasePhaseStatistics> basePhaseStatistics;
protected EconomicMap<Class<?>, LIRPhase.LIRPhaseStatistics> lirPhaseStatistics;

protected final List<DebugHandlersFactory> debugHandlersFactories = new ArrayList<>();

private static final CGlobalData<Pointer> nextIsolateId = CGlobalDataFactory.createWord((Pointer) WordFactory.unsigned(1L));

private volatile long isolateId = 0;

/**
* Gets an identifier for the current isolate that is guaranteed to be unique for the first
* {@code 2^64 - 1} isolates in the process.
*
* @return a non-zero value
*/
public long getIsolateId() {
if (isolateId == 0) {
synchronized (this) {
if (isolateId == 0) {
Pointer p = nextIsolateId.get();
long value;
long nextValue;
do {
value = p.readLong(0);
nextValue = value + 1;
if (nextValue == 0) {
// Avoid setting id to reserved 0 value after long integer overflow
nextValue = 1;
}
} while (p.compareAndSwapLong(0, value, nextValue, ANY_LOCATION) != value);
isolateId = value;
}
}
}
return isolateId;
}

@Platforms(Platform.HOSTED_ONLY.class)
public GraalCompilerSupport() {
for (DebugHandlersFactory c : GraalServices.load(DebugHandlersFactory.class)) {
debugHandlersFactories.add(c);
}
}

public HashMap<Class<? extends NodeMatchRules>, EconomicMap<Class<? extends Node>, List<MatchStatement>>> getMatchRuleRegistry() {
return matchRuleRegistry;
}

public void setMatchRuleRegistry(HashMap<Class<? extends NodeMatchRules>, EconomicMap<Class<? extends Node>, List<MatchStatement>>> matchRuleRegistry) {
this.matchRuleRegistry = matchRuleRegistry;
}

@Platforms(Platform.HOSTED_ONLY.class)
public static void allocatePhaseStatisticsCache() {
GraalCompilerSupport.get().basePhaseStatistics = ImageHeapMap.create();
GraalCompilerSupport.get().lirPhaseStatistics = ImageHeapMap.create();
}

/* Invoked once for every class that is reachable in the native image. */
@Platforms(Platform.HOSTED_ONLY.class)
public static void registerPhaseStatistics(DuringAnalysisAccess a, Class<?> newlyReachableClass) {
DuringAnalysisAccessImpl access = (DuringAnalysisAccessImpl) a;

if (!Modifier.isAbstract(newlyReachableClass.getModifiers())) {
if (BasePhase.class.isAssignableFrom(newlyReachableClass)) {
registerStatistics(newlyReachableClass, GraalCompilerSupport.get().basePhaseStatistics, new BasePhase.BasePhaseStatistics(newlyReachableClass), access);

} else if (LIRPhase.class.isAssignableFrom(newlyReachableClass)) {
registerStatistics(newlyReachableClass, GraalCompilerSupport.get().lirPhaseStatistics, new LIRPhase.LIRPhaseStatistics(newlyReachableClass), access);

}
}
}

private static <S> void registerStatistics(Class<?> phaseSubClass, EconomicMap<Class<?>, S> cache, S newStatistics, DuringAnalysisAccessImpl access) {
assert !cache.containsKey(phaseSubClass);

cache.put(phaseSubClass, newStatistics);
access.requireAnalysisIteration();
}

public static GraalCompilerSupport get() {
return ImageSingletons.lookup(GraalCompilerSupport.class);
}

public EconomicMap<Class<?>, BasePhase.BasePhaseStatistics> getBasePhaseStatistics() {
return basePhaseStatistics;
}

public EconomicMap<Class<?>, LIRPhase.LIRPhaseStatistics> getLirPhaseStatistics() {
return lirPhaseStatistics;
}

public List<DebugHandlersFactory> getDebugHandlersFactories() {
return debugHandlersFactories;
}
}
Loading

0 comments on commit 38d4ba4

Please sign in to comment.