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;

compiler/src/jdk.graal.compiler/src/jdk/graal/compiler/hotspot/CompilationTask.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
import static jdk.graal.compiler.core.GraalCompilerOptions.CompilationFailureAction;
3131
import static jdk.graal.compiler.core.phases.HighTier.Options.Inline;
3232
import static jdk.graal.compiler.java.BytecodeParserOptions.InlineDuringParsing;
33-
import static jdk.vm.ci.services.Services.IS_IN_NATIVE_IMAGE;
33+
import static org.graalvm.nativeimage.ImageInfo.inImageRuntimeCode;
3434

3535
import java.io.PrintStream;
3636

@@ -220,7 +220,7 @@ protected ExceptionAction lookupAction(OptionValues values, Throwable cause) {
220220
*/
221221
private static boolean shouldExitVM(Throwable throwable) {
222222
// If not in libgraal, don't exit
223-
if (!IS_IN_NATIVE_IMAGE) {
223+
if (!inImageRuntimeCode()) {
224224
return false;
225225
}
226226
// If assertions are not enabled, don't exit.

compiler/src/jdk.graal.compiler/src/jdk/graal/compiler/hotspot/CompilerConfigurationFactory.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
import jdk.vm.ci.code.Architecture;
5353
import jdk.vm.ci.common.InitTimer;
5454
import jdk.vm.ci.hotspot.HotSpotJVMCIRuntime;
55-
import jdk.vm.ci.services.Services;
55+
import org.graalvm.nativeimage.ImageInfo;
5656

5757
/**
5858
* A factory that creates the {@link CompilerConfiguration} the compiler will use. Each factory must
@@ -274,7 +274,7 @@ public static CompilerConfigurationFactory selectFactory(String name, OptionValu
274274
* loaded from.
275275
*/
276276
private Object getLoadedFromLocation(boolean verbose) {
277-
if (Services.IS_IN_NATIVE_IMAGE) {
277+
if (ImageInfo.inImageRuntimeCode()) {
278278
if (nativeImageLocationQualifier != null) {
279279
return "a " + nativeImageLocationQualifier + " Native Image shared library";
280280
}

compiler/src/jdk.graal.compiler/src/jdk/graal/compiler/hotspot/EncodedSnippets.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626

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

3131
import java.lang.reflect.Executable;
3232
import java.lang.reflect.Field;
@@ -204,7 +204,7 @@ StructuredGraph getEncodedSnippet(ResolvedJavaMethod method, ResolvedJavaMethod
204204
data = graphDatas.get(methodKey(method));
205205
}
206206
if (data == null) {
207-
if (IS_IN_NATIVE_IMAGE) {
207+
if (inImageRuntimeCode()) {
208208
throw GraalError.shouldNotReachHere("snippet not found: " + method.format("%H.%n(%p)")); // ExcludeFromJacocoGeneratedReport
209209
} else {
210210
return null;
@@ -222,7 +222,7 @@ StructuredGraph getEncodedSnippet(ResolvedJavaMethod method, ResolvedJavaMethod
222222
declaringClass = replacements.getProviders().getMetaAccess().lookupJavaType(Object.class);
223223
}
224224
SymbolicEncodedGraph encodedGraph = new SymbolicEncodedGraph(snippetEncoding, startOffset, snippetObjects, snippetNodeClasses, data.originalMethod, declaringClass);
225-
return decodeSnippetGraph(encodedGraph, method, original, replacements, args, allowAssumptions, options, IS_IN_NATIVE_IMAGE);
225+
return decodeSnippetGraph(encodedGraph, method, original, replacements, args, allowAssumptions, options, inImageRuntimeCode());
226226
}
227227

228228
public SnippetParameterInfo getSnippetParameterInfo(ResolvedJavaMethod method) {
@@ -298,7 +298,7 @@ static StructuredGraph decodeSnippetGraph(SymbolicEncodedGraph encodedGraph, Res
298298
if (args != null) {
299299
MetaAccessProvider meta = HotSpotReplacementsImpl.noticeTypes(providers.getMetaAccess());
300300
SnippetReflectionProvider snippetReflection = replacements.getProviders().getSnippetReflection();
301-
if (IS_IN_NATIVE_IMAGE) {
301+
if (inImageRuntimeCode()) {
302302
snippetReflection = new LibGraalSnippetReflectionProvider(snippetReflection);
303303
}
304304
parameterPlugin = new ConstantBindingParameterPlugin(args, meta, snippetReflection);

compiler/src/jdk.graal.compiler/src/jdk/graal/compiler/hotspot/GraalHotSpotVMConfig.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,17 @@ public class GraalHotSpotVMConfig extends GraalHotSpotVMConfigAccess {
5151
* {@link GraalHotSpotVMConfig} parameter to a {@linkplain Fold foldable} method.
5252
*/
5353
public static final GraalHotSpotVMConfig INJECTED_VMCONFIG = null;
54+
55+
/**
56+
* Sentinel value to use for an {@linkplain InjectedParameter injected}
57+
* {@link MetaAccessProvider} parameter to a {@linkplain Fold foldable} method.
58+
*/
5459
public static final MetaAccessProvider INJECTED_METAACCESS = null;
60+
61+
/**
62+
* Sentinel value to use for an {@linkplain InjectedParameter injected} {@link OptionValues}
63+
* parameter to a {@linkplain Fold foldable} method.
64+
*/
5565
public static final OptionValues INJECTED_OPTIONVALUES = null;
5666

5767
GraalHotSpotVMConfig(HotSpotVMConfigStore store) {

compiler/src/jdk.graal.compiler/src/jdk/graal/compiler/hotspot/HotSpotBackendFactory.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@
2525
package jdk.graal.compiler.hotspot;
2626

2727
import static jdk.vm.ci.common.InitTimer.timer;
28-
import static jdk.vm.ci.services.Services.IS_BUILDING_NATIVE_IMAGE;
29-
import static jdk.vm.ci.services.Services.IS_IN_NATIVE_IMAGE;
28+
import static org.graalvm.nativeimage.ImageInfo.inImageCode;
3029

3130
import jdk.graal.compiler.bytecode.BytecodeProvider;
3231
import jdk.graal.compiler.core.ArchitectureSpecific;
@@ -135,7 +134,7 @@ public final HotSpotBackend createBackend(HotSpotGraalRuntimeProvider graalRunti
135134
OptionValues options = graalRuntime.getOptions();
136135
JVMCIBackend jvmci = jvmciRuntime.getHostJVMCIBackend();
137136
GraalHotSpotVMConfig config = graalRuntime.getVMConfig();
138-
if (IS_BUILDING_NATIVE_IMAGE || IS_IN_NATIVE_IMAGE) {
137+
if (inImageCode()) {
139138
SnippetSignature.initPrimitiveKindCache(jvmci.getMetaAccess());
140139
}
141140
HotSpotCodeCacheProvider codeCache = (HotSpotCodeCacheProvider) jvmci.getCodeCache();

compiler/src/jdk.graal.compiler/src/jdk/graal/compiler/hotspot/HotSpotForeignCallLinkage.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
import java.util.BitSet;
2828
import java.util.List;
2929

30-
import jdk.vm.ci.services.Services;
30+
import org.graalvm.nativeimage.ImageInfo;
3131
import org.graalvm.collections.EconomicMap;
3232
import org.graalvm.collections.EconomicSet;
3333

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

366366
private static GlobalAtomicLong getStubData(ForeignCallSignature sig) {
367367
GlobalAtomicLong data;
368-
if (Services.IS_IN_NATIVE_IMAGE) {
368+
if (ImageInfo.inImageRuntimeCode()) {
369369
data = STUBS.get(sig);
370370
GraalError.guarantee(data != null, "missing global data for %s", sig);
371371
} else {

0 commit comments

Comments
 (0)