Skip to content

Commit d15b684

Browse files
committed
[GR-57999] Replace JVMCI API with Native Image API.
PullRequest: graal/18863
2 parents 4e459bf + 17f3837 commit d15b684

File tree

70 files changed

+386
-273
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

70 files changed

+386
-273
lines changed

compiler/mx.compiler/suite.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@
164164
"dependencies" : [
165165
"sdk:WORD",
166166
"sdk:COLLECTIONS",
167+
"sdk:NATIVEIMAGE",
167168
"truffle:TRUFFLE_COMPILER",
168169
],
169170
"requires" : [
@@ -582,6 +583,7 @@
582583
"distDependencies" : [
583584
"sdk:COLLECTIONS",
584585
"sdk:WORD",
586+
"sdk:NATIVEIMAGE",
585587
"truffle:TRUFFLE_COMPILER",
586588
],
587589
"allowsJavadocWarnings": True,

compiler/src/jdk.graal.compiler.processor/src/jdk/graal/compiler/replacements/processor/GeneratedFoldPlugin.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ protected void createExecute(AbstractProcessor processor, PrintWriter out, Injec
176176
protected void createHelpers(AbstractProcessor processor, PrintWriter out, InjectedDependencies deps) {
177177
out.printf("\n");
178178
out.printf(" @Override\n");
179-
out.printf(" public boolean replace(GraphBuilderContext b, GeneratedPluginInjectionProvider injection, Stamp stamp, NodeInputList<ValueNode> args) {\n");
179+
out.printf(" public boolean replace(GraphBuilderContext b, Replacements injection, Stamp stamp, NodeInputList<ValueNode> args) {\n");
180180

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

compiler/src/jdk.graal.compiler.processor/src/jdk/graal/compiler/replacements/processor/GeneratedNodeIntrinsicPlugin.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ protected void createHelpers(AbstractProcessor processor, PrintWriter out, Injec
247247
}
248248
out.printf("\n");
249249
out.printf(" @Override\n");
250-
out.printf(" public boolean replace(GraphBuilderContext b, GeneratedPluginInjectionProvider injection, Stamp stamp, NodeInputList<ValueNode> args) {\n");
250+
out.printf(" public boolean replace(GraphBuilderContext b, Replacements injection, Stamp stamp, NodeInputList<ValueNode> args) {\n");
251251

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

compiler/src/jdk.graal.compiler.processor/src/jdk/graal/compiler/replacements/processor/PluginGenerator.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@ protected static void createImports(PrintWriter out, AbstractProcessor processor
177177
if (plugin.needsReplacement(processor)) {
178178
extra.add("jdk.graal.compiler.options.ExcludeFromJacocoGeneratedReport");
179179
extra.add("jdk.graal.compiler.graph.NodeInputList");
180+
extra.add("jdk.graal.compiler.nodes.spi.Replacements");
180181
if (plugin.isWithExceptionReplacement(processor)) {
181182
extra.add("jdk.graal.compiler.nodes.PluginReplacementWithExceptionNode");
182183
} else {

compiler/src/jdk.graal.compiler.test/src/jdk/graal/compiler/core/test/CheckGraalInvariants.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,7 @@ public static void runTest(InvariantsTool tool) {
325325
verifiers.add(new VerifyDebugUsage());
326326
verifiers.add(new VerifyVirtualizableUsage());
327327
verifiers.add(new VerifyUpdateUsages());
328+
verifiers.add(new VerifyLibGraalContextChecks());
328329
verifiers.add(new VerifyBailoutUsage());
329330
verifiers.add(new VerifySystemPropertyUsage());
330331
verifiers.add(new VerifyInstanceOfUsage());
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/*
2+
* Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation. Oracle designates this
8+
* particular file as subject to the "Classpath" exception as provided
9+
* by Oracle in the LICENSE file that accompanied this code.
10+
*
11+
* This code is distributed in the hope that it will be useful, but WITHOUT
12+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14+
* version 2 for more details (a copy is included in the LICENSE file that
15+
* accompanied this code).
16+
*
17+
* You should have received a copy of the GNU General Public License version
18+
* 2 along with this work; if not, write to the Free Software Foundation,
19+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20+
*
21+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22+
* or visit www.oracle.com if you need additional information or have any
23+
* questions.
24+
*/
25+
package jdk.graal.compiler.core.test;
26+
27+
import java.util.List;
28+
29+
import jdk.graal.compiler.hotspot.HotSpotGraalCompilerFactory;
30+
import jdk.graal.compiler.nodes.StructuredGraph;
31+
import jdk.graal.compiler.nodes.java.LoadFieldNode;
32+
import jdk.graal.compiler.nodes.spi.CoreProviders;
33+
import jdk.graal.compiler.phases.VerifyPhase;
34+
import jdk.graal.compiler.serviceprovider.GraalServices;
35+
import jdk.vm.ci.meta.ResolvedJavaField;
36+
import jdk.vm.ci.meta.ResolvedJavaMethod;
37+
import jdk.vm.ci.meta.ResolvedJavaType;
38+
import jdk.vm.ci.services.Services;
39+
import org.graalvm.nativeimage.ImageInfo;
40+
41+
/**
42+
* Ensures that the only code directly accessing
43+
* {@link jdk.vm.ci.services.Services#IS_IN_NATIVE_IMAGE} and
44+
* {@link jdk.vm.ci.services.Services#IS_BUILDING_NATIVE_IMAGE} is in
45+
* {@link jdk.graal.compiler.serviceprovider.GraalServices}. All other code must use one of the
46+
* following methods:
47+
* <ul>
48+
* <li>{@link GraalServices#isBuildingLibgraal()}</li>
49+
* <li>{@link GraalServices#isInLibgraal()}</li>
50+
* <li>{@link ImageInfo#inImageCode()}</li>
51+
* <li>{@link ImageInfo#inImageBuildtimeCode()}</li>
52+
* <li>{@link ImageInfo#inImageRuntimeCode()}</li>
53+
* </ul>
54+
*/
55+
public class VerifyLibGraalContextChecks extends VerifyPhase<CoreProviders> {
56+
57+
@Override
58+
public boolean checkContract() {
59+
return false;
60+
}
61+
62+
static boolean isAllowedToAccess(ResolvedJavaMethod method) {
63+
if (method.getDeclaringClass().toJavaName().equals(GraalServices.class.getName())) {
64+
return method.getName().equals("isBuildingLibgraal") || method.getName().equals("isInLibgraal");
65+
}
66+
if (method.getDeclaringClass().toJavaName().equals(HotSpotGraalCompilerFactory.class.getName())) {
67+
return method.getName().equals("createCompiler");
68+
}
69+
return false;
70+
}
71+
72+
@Override
73+
protected void verify(StructuredGraph graph, CoreProviders context) {
74+
75+
final ResolvedJavaType servicesType = context.getMetaAccess().lookupJavaType(Services.class);
76+
servicesType.getStaticFields();
77+
78+
List<LoadFieldNode> loads = graph.getNodes().filter(LoadFieldNode.class).snapshot();
79+
for (LoadFieldNode load : loads) {
80+
ResolvedJavaField field = load.field();
81+
if (field.getDeclaringClass().toJavaName().equals(Services.class.getName())) {
82+
if (field.getName().equals("IS_BUILDING_NATIVE_IMAGE") || field.getName().equals("IS_IN_NATIVE_IMAGE")) {
83+
if (!isAllowedToAccess(graph.method())) {
84+
String recommendation = field.getName().equals("IS_BUILDING_NATIVE_IMAGE") ? "isBuildingLibgraal" : "isInLibgraal";
85+
throw new VerificationError("reading %s in %s is prohibited - use %s.%s() instead",
86+
field.format("%H.%n"),
87+
graph.method().format("%H.%n(%p)"),
88+
GraalServices.class.getName(),
89+
recommendation);
90+
91+
}
92+
}
93+
}
94+
}
95+
}
96+
}

compiler/src/jdk.graal.compiler.test/src/jdk/graal/compiler/core/test/VerifySnippetProbabilities.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@
5252
import jdk.vm.ci.meta.ResolvedJavaMethod;
5353
import jdk.vm.ci.meta.ResolvedJavaType;
5454

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

5761
private static final Object[] KNOWN_PROFILE_INTRINSICS = {
@@ -123,9 +127,9 @@ protected void verify(StructuredGraph graph, CoreProviders context) {
123127
found = true;
124128
break;
125129
} else {
126-
// some snippets have complex semantics separated in different
127-
// method in the same class, allow such patterns they will be
128-
// fully inlined
130+
// Some snippets have complex semantics factored out into other
131+
// methods in the same class. Allow such patterns as they will
132+
// be inlined.
129133
if (targetMethod.getDeclaringClass().equals(method.getDeclaringClass())) {
130134
found = true;
131135
break;
@@ -141,7 +145,7 @@ protected void verify(StructuredGraph graph, CoreProviders context) {
141145
}
142146
} else {
143147
// abstract / interface methods called in a snippet, most likely due
144-
// to overriden snippet logic that folds later, ignore
148+
// to overridden snippet logic that folds later, ignore
145149
found = true;
146150
break;
147151
}
@@ -155,8 +159,8 @@ protected void verify(StructuredGraph graph, CoreProviders context) {
155159
}
156160
}
157161
if (!found) {
158-
throw new VerificationError("Node %s in snippet %s has unknown probability %s (nsp %s) and does not call" +
159-
"BranchProbabilityNode.probabiliy/GraalDirectives.inject<> on any of it's condition inputs.",
162+
throw new VerificationError("Node %s in snippet/substitution %s has unknown probability %s (nsp %s) and does not call" +
163+
"BranchProbabilityNode.probability/GraalDirectives.inject<> on any of its condition inputs.",
160164
ifNode, graph, profile,
161165
ifNode.getNodeSourcePosition());
162166

compiler/src/jdk.graal.compiler/src/jdk/graal/compiler/core/CompilationWatchDog.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
import jdk.graal.compiler.serviceprovider.IsolateUtil;
4343

4444
import jdk.vm.ci.common.NativeImageReinitialize;
45-
import jdk.vm.ci.services.Services;
45+
import org.graalvm.nativeimage.ImageInfo;
4646

4747
/**
4848
* A watch dog for {@linkplain #watch watching} and reporting on long running compilations.
@@ -335,7 +335,7 @@ public Thread newThread(Runnable r) {
335335
*/
336336
public static CompilationWatchDog watch(CompilationIdentifier compilation, OptionValues options, boolean singleShotExecutor, EventHandler eventHandler) {
337337
int delay = Options.CompilationWatchDogStartDelay.getValue(options);
338-
if (Services.IS_BUILDING_NATIVE_IMAGE && !Options.CompilationWatchDogStartDelay.hasBeenSet(options)) {
338+
if (ImageInfo.inImageBuildtimeCode() && !Options.CompilationWatchDogStartDelay.hasBeenSet(options)) {
339339
// Disable watch dog by default when building a native image
340340
delay = 0;
341341
}

compiler/src/jdk.graal.compiler/src/jdk/graal/compiler/core/common/util/CompilationAlarm.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
import jdk.graal.compiler.options.OptionValues;
3838
import jdk.graal.compiler.phases.BasePhase;
3939
import jdk.graal.compiler.serviceprovider.GraalServices;
40-
import jdk.vm.ci.services.Services;
40+
import org.graalvm.nativeimage.ImageInfo;
4141

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

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

6767
/**

compiler/src/jdk.graal.compiler/src/jdk/graal/compiler/debug/IgvDumpChannel.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444

4545
import jdk.graal.compiler.serviceprovider.GraalServices;
4646
import jdk.vm.ci.common.NativeImageReinitialize;
47-
import jdk.vm.ci.services.Services;
47+
import org.graalvm.nativeimage.ImageInfo;
4848

4949
final class IgvDumpChannel implements WritableByteChannel {
5050

@@ -104,7 +104,7 @@ WritableByteChannel channel() throws IOException {
104104
if (target == PrintGraphTarget.File) {
105105
sharedChannel = createFileChannel(pathProvider, null);
106106
} else if (target == PrintGraphTarget.Network) {
107-
if (Services.IS_IN_NATIVE_IMAGE && !ENABLE_NETWORK_DUMPING) {
107+
if (ImageInfo.inImageRuntimeCode() && !ENABLE_NETWORK_DUMPING) {
108108
if (!networkDumpingUnsupportedWarned) {
109109
// Ignore races or multiple isolates - an extra warning is ok
110110
networkDumpingUnsupportedWarned = true;

0 commit comments

Comments
 (0)