Skip to content

Commit

Permalink
[GR-57999] Replace JVMCI API with Native Image API.
Browse files Browse the repository at this point in the history
PullRequest: graal/18863
  • Loading branch information
dougxc committed Nov 2, 2024
2 parents 4e459bf + 17f3837 commit d15b684
Show file tree
Hide file tree
Showing 70 changed files with 386 additions and 273 deletions.
2 changes: 2 additions & 0 deletions compiler/mx.compiler/suite.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@
"dependencies" : [
"sdk:WORD",
"sdk:COLLECTIONS",
"sdk:NATIVEIMAGE",
"truffle:TRUFFLE_COMPILER",
],
"requires" : [
Expand Down Expand Up @@ -582,6 +583,7 @@
"distDependencies" : [
"sdk:COLLECTIONS",
"sdk:WORD",
"sdk:NATIVEIMAGE",
"truffle:TRUFFLE_COMPILER",
],
"allowsJavadocWarnings": True,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ protected void createExecute(AbstractProcessor processor, PrintWriter out, Injec
protected void createHelpers(AbstractProcessor processor, PrintWriter out, InjectedDependencies deps) {
out.printf("\n");
out.printf(" @Override\n");
out.printf(" public boolean replace(GraphBuilderContext b, GeneratedPluginInjectionProvider injection, Stamp stamp, NodeInputList<ValueNode> args) {\n");
out.printf(" public boolean replace(GraphBuilderContext b, Replacements injection, Stamp stamp, NodeInputList<ValueNode> args) {\n");

List<? extends VariableElement> params = intrinsicMethod.getParameters();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ protected void createHelpers(AbstractProcessor processor, PrintWriter out, Injec
}
out.printf("\n");
out.printf(" @Override\n");
out.printf(" public boolean replace(GraphBuilderContext b, GeneratedPluginInjectionProvider injection, Stamp stamp, NodeInputList<ValueNode> args) {\n");
out.printf(" public boolean replace(GraphBuilderContext b, Replacements injection, Stamp stamp, NodeInputList<ValueNode> args) {\n");

List<? extends VariableElement> params = getParameters();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ protected static void createImports(PrintWriter out, AbstractProcessor processor
if (plugin.needsReplacement(processor)) {
extra.add("jdk.graal.compiler.options.ExcludeFromJacocoGeneratedReport");
extra.add("jdk.graal.compiler.graph.NodeInputList");
extra.add("jdk.graal.compiler.nodes.spi.Replacements");
if (plugin.isWithExceptionReplacement(processor)) {
extra.add("jdk.graal.compiler.nodes.PluginReplacementWithExceptionNode");
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,7 @@ public static void runTest(InvariantsTool tool) {
verifiers.add(new VerifyDebugUsage());
verifiers.add(new VerifyVirtualizableUsage());
verifiers.add(new VerifyUpdateUsages());
verifiers.add(new VerifyLibGraalContextChecks());
verifiers.add(new VerifyBailoutUsage());
verifiers.add(new VerifySystemPropertyUsage());
verifiers.add(new VerifyInstanceOfUsage());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/*
* Copyright (c) 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 jdk.graal.compiler.core.test;

import java.util.List;

import jdk.graal.compiler.hotspot.HotSpotGraalCompilerFactory;
import jdk.graal.compiler.nodes.StructuredGraph;
import jdk.graal.compiler.nodes.java.LoadFieldNode;
import jdk.graal.compiler.nodes.spi.CoreProviders;
import jdk.graal.compiler.phases.VerifyPhase;
import jdk.graal.compiler.serviceprovider.GraalServices;
import jdk.vm.ci.meta.ResolvedJavaField;
import jdk.vm.ci.meta.ResolvedJavaMethod;
import jdk.vm.ci.meta.ResolvedJavaType;
import jdk.vm.ci.services.Services;
import org.graalvm.nativeimage.ImageInfo;

/**
* Ensures that the only code directly accessing
* {@link jdk.vm.ci.services.Services#IS_IN_NATIVE_IMAGE} and
* {@link jdk.vm.ci.services.Services#IS_BUILDING_NATIVE_IMAGE} is in
* {@link jdk.graal.compiler.serviceprovider.GraalServices}. All other code must use one of the
* following methods:
* <ul>
* <li>{@link GraalServices#isBuildingLibgraal()}</li>
* <li>{@link GraalServices#isInLibgraal()}</li>
* <li>{@link ImageInfo#inImageCode()}</li>
* <li>{@link ImageInfo#inImageBuildtimeCode()}</li>
* <li>{@link ImageInfo#inImageRuntimeCode()}</li>
* </ul>
*/
public class VerifyLibGraalContextChecks extends VerifyPhase<CoreProviders> {

@Override
public boolean checkContract() {
return false;
}

static boolean isAllowedToAccess(ResolvedJavaMethod method) {
if (method.getDeclaringClass().toJavaName().equals(GraalServices.class.getName())) {
return method.getName().equals("isBuildingLibgraal") || method.getName().equals("isInLibgraal");
}
if (method.getDeclaringClass().toJavaName().equals(HotSpotGraalCompilerFactory.class.getName())) {
return method.getName().equals("createCompiler");
}
return false;
}

@Override
protected void verify(StructuredGraph graph, CoreProviders context) {

final ResolvedJavaType servicesType = context.getMetaAccess().lookupJavaType(Services.class);
servicesType.getStaticFields();

List<LoadFieldNode> loads = graph.getNodes().filter(LoadFieldNode.class).snapshot();
for (LoadFieldNode load : loads) {
ResolvedJavaField field = load.field();
if (field.getDeclaringClass().toJavaName().equals(Services.class.getName())) {
if (field.getName().equals("IS_BUILDING_NATIVE_IMAGE") || field.getName().equals("IS_IN_NATIVE_IMAGE")) {
if (!isAllowedToAccess(graph.method())) {
String recommendation = field.getName().equals("IS_BUILDING_NATIVE_IMAGE") ? "isBuildingLibgraal" : "isInLibgraal";
throw new VerificationError("reading %s in %s is prohibited - use %s.%s() instead",
field.format("%H.%n"),
graph.method().format("%H.%n(%p)"),
GraalServices.class.getName(),
recommendation);

}
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@
import jdk.vm.ci.meta.ResolvedJavaMethod;
import jdk.vm.ci.meta.ResolvedJavaType;

/**
* Checks that every {@link IfNode} in a {@linkplain StructuredGraph#isSubstitution() snippet or
* substitution} has a known probability.
*/
public class VerifySnippetProbabilities extends VerifyPhase<CoreProviders> {

private static final Object[] KNOWN_PROFILE_INTRINSICS = {
Expand Down Expand Up @@ -123,9 +127,9 @@ protected void verify(StructuredGraph graph, CoreProviders context) {
found = true;
break;
} else {
// some snippets have complex semantics separated in different
// method in the same class, allow such patterns they will be
// fully inlined
// Some snippets have complex semantics factored out into other
// methods in the same class. Allow such patterns as they will
// be inlined.
if (targetMethod.getDeclaringClass().equals(method.getDeclaringClass())) {
found = true;
break;
Expand All @@ -141,7 +145,7 @@ protected void verify(StructuredGraph graph, CoreProviders context) {
}
} else {
// abstract / interface methods called in a snippet, most likely due
// to overriden snippet logic that folds later, ignore
// to overridden snippet logic that folds later, ignore
found = true;
break;
}
Expand All @@ -155,8 +159,8 @@ protected void verify(StructuredGraph graph, CoreProviders context) {
}
}
if (!found) {
throw new VerificationError("Node %s in snippet %s has unknown probability %s (nsp %s) and does not call" +
"BranchProbabilityNode.probabiliy/GraalDirectives.inject<> on any of it's condition inputs.",
throw new VerificationError("Node %s in snippet/substitution %s has unknown probability %s (nsp %s) and does not call" +
"BranchProbabilityNode.probability/GraalDirectives.inject<> on any of its condition inputs.",
ifNode, graph, profile,
ifNode.getNodeSourcePosition());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
import jdk.graal.compiler.serviceprovider.IsolateUtil;

import jdk.vm.ci.common.NativeImageReinitialize;
import jdk.vm.ci.services.Services;
import org.graalvm.nativeimage.ImageInfo;

/**
* A watch dog for {@linkplain #watch watching} and reporting on long running compilations.
Expand Down Expand Up @@ -335,7 +335,7 @@ public Thread newThread(Runnable r) {
*/
public static CompilationWatchDog watch(CompilationIdentifier compilation, OptionValues options, boolean singleShotExecutor, EventHandler eventHandler) {
int delay = Options.CompilationWatchDogStartDelay.getValue(options);
if (Services.IS_BUILDING_NATIVE_IMAGE && !Options.CompilationWatchDogStartDelay.hasBeenSet(options)) {
if (ImageInfo.inImageBuildtimeCode() && !Options.CompilationWatchDogStartDelay.hasBeenSet(options)) {
// Disable watch dog by default when building a native image
delay = 0;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
import jdk.graal.compiler.options.OptionValues;
import jdk.graal.compiler.phases.BasePhase;
import jdk.graal.compiler.serviceprovider.GraalServices;
import jdk.vm.ci.services.Services;
import org.graalvm.nativeimage.ImageInfo;

/**
* Utility class that allows the compiler to monitor compilations that take a very long time.
Expand All @@ -61,7 +61,7 @@ public static class Options {
// @formatter:on
}

public static final boolean LOG_PROGRESS_DETECTION = !Services.IS_IN_NATIVE_IMAGE &&
public static final boolean LOG_PROGRESS_DETECTION = !ImageInfo.inImageRuntimeCode() &&
Boolean.parseBoolean(GraalServices.getSavedProperty("debug." + CompilationAlarm.class.getName() + ".logProgressDetection"));

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@

import jdk.graal.compiler.serviceprovider.GraalServices;
import jdk.vm.ci.common.NativeImageReinitialize;
import jdk.vm.ci.services.Services;
import org.graalvm.nativeimage.ImageInfo;

final class IgvDumpChannel implements WritableByteChannel {

Expand Down Expand Up @@ -104,7 +104,7 @@ WritableByteChannel channel() throws IOException {
if (target == PrintGraphTarget.File) {
sharedChannel = createFileChannel(pathProvider, null);
} else if (target == PrintGraphTarget.Network) {
if (Services.IS_IN_NATIVE_IMAGE && !ENABLE_NETWORK_DUMPING) {
if (ImageInfo.inImageRuntimeCode() && !ENABLE_NETWORK_DUMPING) {
if (!networkDumpingUnsupportedWarned) {
// Ignore races or multiple isolates - an extra warning is ok
networkDumpingUnsupportedWarned = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
import static jdk.graal.compiler.core.GraalCompilerOptions.CompilationFailureAction;
import static jdk.graal.compiler.core.phases.HighTier.Options.Inline;
import static jdk.graal.compiler.java.BytecodeParserOptions.InlineDuringParsing;
import static jdk.vm.ci.services.Services.IS_IN_NATIVE_IMAGE;
import static org.graalvm.nativeimage.ImageInfo.inImageRuntimeCode;

import java.io.PrintStream;

Expand Down Expand Up @@ -220,7 +220,7 @@ protected ExceptionAction lookupAction(OptionValues values, Throwable cause) {
*/
private static boolean shouldExitVM(Throwable throwable) {
// If not in libgraal, don't exit
if (!IS_IN_NATIVE_IMAGE) {
if (!inImageRuntimeCode()) {
return false;
}
// If assertions are not enabled, don't exit.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
import jdk.vm.ci.code.Architecture;
import jdk.vm.ci.common.InitTimer;
import jdk.vm.ci.hotspot.HotSpotJVMCIRuntime;
import jdk.vm.ci.services.Services;
import org.graalvm.nativeimage.ImageInfo;

/**
* A factory that creates the {@link CompilerConfiguration} the compiler will use. Each factory must
Expand Down Expand Up @@ -274,7 +274,7 @@ public static CompilerConfigurationFactory selectFactory(String name, OptionValu
* loaded from.
*/
private Object getLoadedFromLocation(boolean verbose) {
if (Services.IS_IN_NATIVE_IMAGE) {
if (ImageInfo.inImageRuntimeCode()) {
if (nativeImageLocationQualifier != null) {
return "a " + nativeImageLocationQualifier + " Native Image shared library";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

import static jdk.graal.compiler.hotspot.HotSpotReplacementsImpl.isGraalClass;
import static jdk.graal.compiler.nodes.graphbuilderconf.IntrinsicContext.CompilationContext.INLINE_AFTER_PARSING;
import static jdk.vm.ci.services.Services.IS_IN_NATIVE_IMAGE;
import static org.graalvm.nativeimage.ImageInfo.inImageRuntimeCode;

import java.lang.reflect.Executable;
import java.lang.reflect.Field;
Expand Down Expand Up @@ -204,7 +204,7 @@ StructuredGraph getEncodedSnippet(ResolvedJavaMethod method, ResolvedJavaMethod
data = graphDatas.get(methodKey(method));
}
if (data == null) {
if (IS_IN_NATIVE_IMAGE) {
if (inImageRuntimeCode()) {
throw GraalError.shouldNotReachHere("snippet not found: " + method.format("%H.%n(%p)")); // ExcludeFromJacocoGeneratedReport
} else {
return null;
Expand All @@ -222,7 +222,7 @@ StructuredGraph getEncodedSnippet(ResolvedJavaMethod method, ResolvedJavaMethod
declaringClass = replacements.getProviders().getMetaAccess().lookupJavaType(Object.class);
}
SymbolicEncodedGraph encodedGraph = new SymbolicEncodedGraph(snippetEncoding, startOffset, snippetObjects, snippetNodeClasses, data.originalMethod, declaringClass);
return decodeSnippetGraph(encodedGraph, method, original, replacements, args, allowAssumptions, options, IS_IN_NATIVE_IMAGE);
return decodeSnippetGraph(encodedGraph, method, original, replacements, args, allowAssumptions, options, inImageRuntimeCode());
}

public SnippetParameterInfo getSnippetParameterInfo(ResolvedJavaMethod method) {
Expand Down Expand Up @@ -298,7 +298,7 @@ static StructuredGraph decodeSnippetGraph(SymbolicEncodedGraph encodedGraph, Res
if (args != null) {
MetaAccessProvider meta = HotSpotReplacementsImpl.noticeTypes(providers.getMetaAccess());
SnippetReflectionProvider snippetReflection = replacements.getProviders().getSnippetReflection();
if (IS_IN_NATIVE_IMAGE) {
if (inImageRuntimeCode()) {
snippetReflection = new LibGraalSnippetReflectionProvider(snippetReflection);
}
parameterPlugin = new ConstantBindingParameterPlugin(args, meta, snippetReflection);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,17 @@ public class GraalHotSpotVMConfig extends GraalHotSpotVMConfigAccess {
* {@link GraalHotSpotVMConfig} parameter to a {@linkplain Fold foldable} method.
*/
public static final GraalHotSpotVMConfig INJECTED_VMCONFIG = null;

/**
* Sentinel value to use for an {@linkplain InjectedParameter injected}
* {@link MetaAccessProvider} parameter to a {@linkplain Fold foldable} method.
*/
public static final MetaAccessProvider INJECTED_METAACCESS = null;

/**
* Sentinel value to use for an {@linkplain InjectedParameter injected} {@link OptionValues}
* parameter to a {@linkplain Fold foldable} method.
*/
public static final OptionValues INJECTED_OPTIONVALUES = null;

GraalHotSpotVMConfig(HotSpotVMConfigStore store) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@
package jdk.graal.compiler.hotspot;

import static jdk.vm.ci.common.InitTimer.timer;
import static jdk.vm.ci.services.Services.IS_BUILDING_NATIVE_IMAGE;
import static jdk.vm.ci.services.Services.IS_IN_NATIVE_IMAGE;
import static org.graalvm.nativeimage.ImageInfo.inImageCode;

import jdk.graal.compiler.bytecode.BytecodeProvider;
import jdk.graal.compiler.core.ArchitectureSpecific;
Expand Down Expand Up @@ -135,7 +134,7 @@ public final HotSpotBackend createBackend(HotSpotGraalRuntimeProvider graalRunti
OptionValues options = graalRuntime.getOptions();
JVMCIBackend jvmci = jvmciRuntime.getHostJVMCIBackend();
GraalHotSpotVMConfig config = graalRuntime.getVMConfig();
if (IS_BUILDING_NATIVE_IMAGE || IS_IN_NATIVE_IMAGE) {
if (inImageCode()) {
SnippetSignature.initPrimitiveKindCache(jvmci.getMetaAccess());
}
HotSpotCodeCacheProvider codeCache = (HotSpotCodeCacheProvider) jvmci.getCodeCache();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import java.util.BitSet;
import java.util.List;

import jdk.vm.ci.services.Services;
import org.graalvm.nativeimage.ImageInfo;
import org.graalvm.collections.EconomicMap;
import org.graalvm.collections.EconomicSet;

Expand Down Expand Up @@ -365,7 +365,7 @@ static HotSpotForeignCallLinkageImpl.CodeInfo getCodeInfo(Stub stub, Backend bac

private static GlobalAtomicLong getStubData(ForeignCallSignature sig) {
GlobalAtomicLong data;
if (Services.IS_IN_NATIVE_IMAGE) {
if (ImageInfo.inImageRuntimeCode()) {
data = STUBS.get(sig);
GraalError.guarantee(data != null, "missing global data for %s", sig);
} else {
Expand Down
Loading

0 comments on commit d15b684

Please sign in to comment.