Skip to content

Commit de29af8

Browse files
committed
[GR-68037] Use encoded snippets in jargraal.
PullRequest: graal/21717
2 parents e482f98 + f339af6 commit de29af8

30 files changed

+508
-471
lines changed

compiler/docs/ReplayCompilation.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,7 @@ Any `-ea`, `-esa`, and `-X` arguments from the command line are passed to the JV
6363

6464
## Jargraal vs. Libgraal
6565

66-
Both jargraal and libgraal compilations can be replayed on jargraal. When jargraal replays a libgraal compilation,
67-
it uses encoded snippets to match the behavior and compilations results of libgraal. It is also possible to replay
68-
libgraal compilations on libgraal. Replaying jargraal compilations on libgraal is not supported.
66+
Jargraal can replay both jargraal and libgraal compilations. Libgraal can replay only libgraal compilations.
6967

7068
It is necessary to explicitly enable the replay launcher entry point when building libgraal using the VM argument
7169
`-Ddebug.jdk.graal.enableReplayLauncher=true`.

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

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,12 @@
3131
import org.junit.Test;
3232

3333
import jdk.graal.compiler.api.directives.GraalDirectives;
34+
import jdk.graal.compiler.api.test.Graal;
3435
import jdk.graal.compiler.core.common.type.Stamp;
3536
import jdk.graal.compiler.core.common.type.StampFactory;
3637
import jdk.graal.compiler.core.phases.HighTier;
38+
import jdk.graal.compiler.debug.DebugCloseable;
39+
import jdk.graal.compiler.hotspot.HotSpotReplacementsImpl;
3740
import jdk.graal.compiler.nodes.GraphState.GuardsStage;
3841
import jdk.graal.compiler.nodes.ValueNode;
3942
import jdk.graal.compiler.nodes.graphbuilderconf.GraphBuilderConfiguration.Plugins;
@@ -48,35 +51,30 @@
4851
import jdk.graal.compiler.replacements.TestSnippets;
4952
import jdk.graal.compiler.replacements.TestSnippets.TransplantTestSnippets;
5053
import jdk.graal.compiler.replacements.nodes.LateLoweredNode;
54+
import jdk.graal.compiler.runtime.RuntimeProvider;
5155
import jdk.vm.ci.code.InstalledCode;
5256
import jdk.vm.ci.code.InvalidInstalledCodeException;
5357
import jdk.vm.ci.meta.JavaKind;
5458
import jdk.vm.ci.meta.ResolvedJavaMethod;
5559

5660
public class TransplantLowLevelGraphTest extends GraalCompilerTest {
61+
private static DebugCloseable snippetScope;
62+
63+
private static TestSnippets.TransplantTestSnippets.Templates transplantTestSnippets;
5764

5865
@BeforeClass
5966
public static void setup() {
60-
/**
61-
* Ensure snippets can be registered in a test setup running jargraal only.
62-
*/
63-
System.setProperty("GraalUnitTest", "true");
67+
Providers providers = Graal.getRequiredCapability(RuntimeProvider.class).getHostBackend().getProviders();
68+
OptionValues options = getInitialOptions();
69+
HotSpotReplacementsImpl replacements = (HotSpotReplacementsImpl) providers.getReplacements();
70+
snippetScope = replacements.suppressEncodedSnippets();
71+
transplantTestSnippets = new TestSnippets.TransplantTestSnippets.Templates(options, providers);
72+
replacements.encode(options);
6473
}
6574

6675
@AfterClass
6776
public static void teardown() {
68-
System.clearProperty("GraalUnitTest");
69-
}
70-
71-
TestSnippets.TransplantTestSnippets.Templates transplantTestSnippets;
72-
73-
@SuppressWarnings("this-escape")
74-
public TransplantLowLevelGraphTest() {
75-
Providers p = getProviders();
76-
OptionValues opt = getInitialOptions();
77-
78-
// ensure that the snippets are registered
79-
transplantTestSnippets = new TestSnippets.TransplantTestSnippets.Templates(opt, p);
77+
snippetScope.close();
8078
}
8179

8280
@Override

compiler/src/jdk.graal.compiler.test/src/jdk/graal/compiler/hotspot/amd64/test/StubAVXTest.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,11 @@
4444
import jdk.graal.compiler.core.common.spi.ForeignCallSignature;
4545
import jdk.graal.compiler.core.common.type.DataPointerConstant;
4646
import jdk.graal.compiler.core.test.GraalCompilerTest;
47+
import jdk.graal.compiler.debug.DebugCloseable;
4748
import jdk.graal.compiler.debug.DebugContext;
4849
import jdk.graal.compiler.hotspot.HotSpotBackend;
4950
import jdk.graal.compiler.hotspot.HotSpotForeignCallLinkage;
51+
import jdk.graal.compiler.hotspot.HotSpotReplacementsImpl;
5052
import jdk.graal.compiler.hotspot.meta.HotSpotForeignCallDescriptor;
5153
import jdk.graal.compiler.hotspot.meta.HotSpotForeignCallsProviderImpl;
5254
import jdk.graal.compiler.hotspot.meta.HotSpotProviders;
@@ -240,8 +242,11 @@ public static int testStub() {
240242
public void test() {
241243
HotSpotProviders providers = (HotSpotProviders) getProviders();
242244
HotSpotForeignCallsProviderImpl foreignCalls = providers.getForeignCalls();
243-
HotSpotForeignCallLinkage linkage = foreignCalls.registerStubCall(TEST_STUB, HotSpotForeignCallDescriptor.Transition.LEAF_NO_VZERO, NO_SIDE_EFFECT, COMPUTES_REGISTERS_KILLED);
244-
linkage.setCompiledStub(new TestStub(GraalCompilerTest.getInitialOptions(), providers, linkage));
245-
runTest("testStub");
245+
OptionValues options = GraalCompilerTest.getInitialOptions();
246+
try (DebugCloseable _ = ((HotSpotReplacementsImpl) providers.getReplacements()).suppressEncodedSnippets()) {
247+
HotSpotForeignCallLinkage linkage = foreignCalls.registerStubCall(TEST_STUB, HotSpotForeignCallDescriptor.Transition.LEAF_NO_VZERO, NO_SIDE_EFFECT, COMPUTES_REGISTERS_KILLED);
248+
linkage.setCompiledStub(new TestStub(options, providers, linkage));
249+
runTest("testStub");
250+
}
246251
}
247252
}

compiler/src/jdk.graal.compiler.test/src/jdk/graal/compiler/hotspot/replaycomp/test/ReplayCompilationTest.java

Lines changed: 1 addition & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
package jdk.graal.compiler.hotspot.replaycomp.test;
2626

2727
import java.io.ByteArrayOutputStream;
28-
import java.io.FileReader;
2928
import java.io.IOException;
3029
import java.nio.charset.Charset;
3130
import java.nio.file.Files;
@@ -55,15 +54,10 @@
5554
import jdk.graal.compiler.hotspot.CompilerConfigurationFactory;
5655
import jdk.graal.compiler.hotspot.HotSpotGraalCompiler;
5756
import jdk.graal.compiler.hotspot.HotSpotGraalCompilerFactory;
58-
import jdk.graal.compiler.hotspot.HotSpotReplacementsImpl;
5957
import jdk.graal.compiler.hotspot.replaycomp.CompilerInterfaceDeclarations;
60-
import jdk.graal.compiler.hotspot.replaycomp.RecordedOperationPersistence;
6158
import jdk.graal.compiler.hotspot.replaycomp.ReplayCompilationRunner;
62-
import jdk.graal.compiler.hotspot.replaycomp.ReplayCompilationSupport;
6359
import jdk.graal.compiler.options.OptionValues;
6460
import jdk.graal.compiler.runtime.RuntimeProvider;
65-
import jdk.graal.compiler.util.json.JsonParser;
66-
import jdk.graal.compiler.util.json.JsonWriter;
6761
import jdk.vm.ci.hotspot.HotSpotCompilationRequest;
6862
import jdk.vm.ci.hotspot.HotSpotCompilationRequestResult;
6963
import jdk.vm.ci.hotspot.HotSpotJVMCIRuntime;
@@ -124,36 +118,17 @@ public void recordAndExecuteReplayRunner() throws Throwable {
124118
HotSpotCompilationRequestResult regularResult = runRegularCompilation(method, recordOptions);
125119
assertTrue(regularResult.getFailure() == null);
126120
Path replayFile = findReplayCompFile(temp.path);
127-
Path replayFileLibgraal = Path.of(temp.path.toString(), "libgraal.json");
128-
copyReplayFileAsLibgraalCompilation(replayFile, replayFileLibgraal);
129121
String[][] argumentLists = new String[][]{
130122
new String[]{"--compare-graphs=true", replayFile.toString()},
131-
new String[]{"--compare-graphs=false", "--benchmark=true", "--iterations=1", replayFileLibgraal.toString()}
123+
new String[]{"--compare-graphs=false", "--benchmark=true", "--iterations=1", temp.path.toString()}
132124
};
133125
for (String[] arguments : argumentLists) {
134126
ReplayCompilationRunner.ExitStatus status = ReplayCompilationRunner.run(arguments, TTY.out().out());
135127
assertTrue(status == ReplayCompilationRunner.ExitStatus.Success);
136128
}
137-
assertTrue(HotSpotReplacementsImpl.snippetsAreEncoded());
138129
});
139130
}
140131

141-
/**
142-
* Creates a copy of the source replay file that acts as if it was compiled with libgraal. This
143-
* way, we test replaying libgraal compilations on jargraal (without access to libgraal).
144-
*
145-
* @param replayFile the source replay file
146-
* @param destFile the created replay file that acts as a libgraal compilation
147-
* ({@link jdk.graal.compiler.hotspot.replaycomp.RecordedOperationPersistence.RecordedCompilationUnit#isLibgraal()})
148-
*/
149-
private static void copyReplayFileAsLibgraalCompilation(Path replayFile, Path destFile) throws Throwable {
150-
try (FileReader reader = new FileReader(replayFile.toFile()); JsonWriter writer = new JsonWriter(destFile)) {
151-
EconomicMap<String, Object> json = JsonParser.parseDict(reader);
152-
json.put(RecordedOperationPersistence.RecordedCompilationUnitSerializer.IS_LIBGRAAL_PROPERTY, true);
153-
writer.print(json);
154-
}
155-
}
156-
157132
@Test
158133
public void unparsableReplayFileSucceeds() throws Throwable {
159134
runTest((temp) -> {
@@ -190,10 +165,6 @@ private static void runTest(TestRunner test) throws Throwable {
190165
System.err.println(outputStream.toString(Charset.defaultCharset()));
191166
throw throwable;
192167
}
193-
} finally {
194-
HotSpotReplacementsImpl.setEncodedSnippets(null);
195-
HotSpotReplacementsImpl.clearSnippetEncoder();
196-
ReplayCompilationSupport.setReplayingLibgraalInJargraal(false);
197168
}
198169
}
199170

compiler/src/jdk.graal.compiler.test/src/jdk/graal/compiler/hotspot/test/TestNewInstanceWithException.java

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -569,6 +569,11 @@ public void runSubprocessTest(Runnable r, String... args) throws IOException, In
569569
}
570570
}
571571

572+
/**
573+
* The argument setting the maximum heap space of the subprocess.
574+
*/
575+
public static final String MAX_HEAP_SPACE_ARG = "-Xmx64m";
576+
572577
public static class TestNewInstanceWithException1 extends TestNewInstanceWithException {
573578

574579
@Test
@@ -579,7 +584,7 @@ public void testNewArrayWithException() throws IOException, InterruptedException
579584
} catch (InvalidInstalledCodeException e) {
580585
throw GraalError.shouldNotReachHere(e);
581586
}
582-
}, "-Xmx32m");
587+
}, MAX_HEAP_SPACE_ARG);
583588
}
584589
}
585590

@@ -592,7 +597,7 @@ public void testNewInstanceWithException() throws IOException, InterruptedExcept
592597
} catch (InvalidInstalledCodeException e) {
593598
throw GraalError.shouldNotReachHere(e);
594599
}
595-
}, "-Xmx32m");
600+
}, MAX_HEAP_SPACE_ARG);
596601
}
597602
}
598603

@@ -605,7 +610,7 @@ public void testDynamicNewInstanceWithException() throws Throwable {
605610
} catch (Throwable e) {
606611
throw GraalError.shouldNotReachHere(e);
607612
}
608-
}, "-Xmx32m");
613+
}, MAX_HEAP_SPACE_ARG);
609614
}
610615
}
611616

@@ -618,7 +623,7 @@ public void testDynamicNewInstanceWithExceptionCanonToInstanceWithException() th
618623
} catch (Throwable e) {
619624
throw GraalError.shouldNotReachHere(e);
620625
}
621-
}, "-Xmx32m");
626+
}, MAX_HEAP_SPACE_ARG);
622627
}
623628
}
624629

@@ -631,7 +636,7 @@ public void testDynamicNewArrayWithException() throws Throwable {
631636
} catch (Throwable e) {
632637
throw GraalError.shouldNotReachHere(e);
633638
}
634-
}, "-Xmx32m");
639+
}, MAX_HEAP_SPACE_ARG);
635640
}
636641
}
637642

@@ -644,7 +649,7 @@ public void testDynamicNewArrayWithExceptionCanonToArrayWithException() throws T
644649
} catch (Throwable e) {
645650
throw GraalError.shouldNotReachHere(e);
646651
}
647-
}, "-Xmx32m");
652+
}, MAX_HEAP_SPACE_ARG);
648653
}
649654
}
650655

@@ -657,7 +662,7 @@ public void testNewMultiArrayWithException() throws Throwable {
657662
} catch (Throwable e) {
658663
throw GraalError.shouldNotReachHere(e);
659664
}
660-
}, "-Xmx32m");
665+
}, MAX_HEAP_SPACE_ARG);
661666
}
662667
}
663668
}

compiler/src/jdk.graal.compiler.test/src/jdk/graal/compiler/hotspot/test/TestNewInstanceWithExceptionRegression.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
*/
2525
package jdk.graal.compiler.hotspot.test;
2626

27+
import static jdk.graal.compiler.hotspot.test.TestNewInstanceWithException.MAX_HEAP_SPACE_ARG;
28+
2729
import java.io.IOException;
2830
import java.util.ArrayList;
2931
import java.util.Collections;
@@ -70,7 +72,7 @@ public void test01() throws IOException, InterruptedException {
7072
} catch (Throwable e) {
7173
throw GraalError.shouldNotReachHere(e);
7274
}
73-
}, "-Xmx32m");
75+
}, MAX_HEAP_SPACE_ARG);
7476
}
7577

7678
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/*
2+
* Copyright (c) 2025, 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.replacements.test;
26+
27+
import org.junit.Assert;
28+
import org.junit.Test;
29+
30+
import jdk.graal.compiler.core.common.GraalOptions;
31+
import jdk.graal.compiler.core.test.GraalCompilerTest;
32+
import jdk.graal.compiler.debug.DebugCloseable;
33+
import jdk.graal.compiler.hotspot.HotSpotReplacementsImpl;
34+
import jdk.graal.compiler.nodes.graphbuilderconf.GraphBuilderConfiguration;
35+
import jdk.graal.compiler.nodes.graphbuilderconf.InvocationPlugins;
36+
import jdk.graal.compiler.options.OptionValues;
37+
import jdk.graal.compiler.replacements.SnippetCounter;
38+
import jdk.graal.compiler.replacements.SnippetSubstitutionInvocationPlugin;
39+
import jdk.graal.compiler.replacements.SnippetTemplate;
40+
import jdk.graal.compiler.replacements.TestSnippets;
41+
import jdk.vm.ci.meta.ResolvedJavaMethod;
42+
43+
/**
44+
* Tests snippet counters with encoded snippets on jargraal.
45+
*/
46+
public class SnippetCountersTest extends GraalCompilerTest {
47+
public static void testedMethod() {
48+
substitutedInvoke();
49+
}
50+
51+
public static void substitutedInvoke() {
52+
}
53+
54+
private TestSnippets.CounterTestSnippets.TestSnippetCounters counters;
55+
56+
@Test
57+
public void test() {
58+
HotSpotReplacementsImpl replacements = (HotSpotReplacementsImpl) getReplacements();
59+
try (DebugCloseable _ = replacements.suppressEncodedSnippets()) {
60+
OptionValues options = new OptionValues(getInitialOptions(), GraalOptions.SnippetCounters, true);
61+
replacements.registerSnippetTemplateCache(new TestSnippets.CounterTestSnippets.Templates(options, getProviders()));
62+
replacements.encode(options);
63+
counters = new TestSnippets.CounterTestSnippets.TestSnippetCounters(SnippetCounter.Group::new);
64+
executeActual(getResolvedJavaMethod("testedMethod"), null);
65+
Assert.assertEquals(1L, counters.increments.value());
66+
Assert.assertEquals(2L, counters.doubleIncrements.value());
67+
}
68+
}
69+
70+
@Override
71+
protected GraphBuilderConfiguration.Plugins getDefaultGraphBuilderPlugins() {
72+
GraphBuilderConfiguration.Plugins p = super.getDefaultGraphBuilderPlugins();
73+
InvocationPlugins.Registration r = new InvocationPlugins.Registration(p.getInvocationPlugins(), SnippetCountersTest.class);
74+
r.register(new SnippetSubstitutionInvocationPlugin<>(TestSnippets.CounterTestSnippets.Templates.class, "substitutedInvoke") {
75+
@Override
76+
public SnippetTemplate.SnippetInfo getSnippet(TestSnippets.CounterTestSnippets.Templates templates) {
77+
return templates.increase;
78+
}
79+
80+
@Override
81+
protected Object[] getConstantArguments(ResolvedJavaMethod targetMethod) {
82+
return new Object[]{counters};
83+
}
84+
});
85+
return p;
86+
}
87+
}

compiler/src/jdk.graal.compiler/src/jdk/graal/compiler/core/common/GraalOptions.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -288,9 +288,6 @@ public final class GraalOptions {
288288
@Option(help = "Enable counters for various paths in snippets.", type = OptionType.Debug)
289289
public static final OptionKey<Boolean> SnippetCounters = new OptionKey<>(false);
290290

291-
@Option(help = "Eagerly construct extra snippet info.", type = OptionType.Debug)
292-
public static final OptionKey<Boolean> EagerSnippets = new OptionKey<>(false);
293-
294291
@Option(help = "Use a cache for snippet graphs.", type = OptionType.Debug)
295292
public static final OptionKey<Boolean> UseSnippetGraphCache = new OptionKey<>(true);
296293

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@
3838
import org.graalvm.collections.MapCursor;
3939

4040
import jdk.graal.compiler.core.common.Fields;
41-
import jdk.graal.compiler.core.common.spi.ForeignCallSignature;
4241
import jdk.graal.compiler.core.common.LibGraalSupport;
42+
import jdk.graal.compiler.core.common.spi.ForeignCallSignature;
4343
import jdk.graal.compiler.core.target.Backend;
4444
import jdk.graal.compiler.debug.GraalError;
4545
import jdk.graal.compiler.graph.NodeClass;
@@ -104,6 +104,7 @@ protected ClassInfo makeClassInfo(Class<?> declaringClass) {
104104
}
105105

106106
private static EncodedSnippets getEncodedSnippets(HotSpotReplacementsImpl replacements, OptionValues options) {
107+
GraalError.guarantee(!HotSpotReplacementsImpl.snippetsAreEncoded(), "snippets should not be encoded");
107108
SymbolicSnippetEncoder snippetEncoder = replacements.maybeInitializeEncoder();
108109
return snippetEncoder.encodeSnippets(options);
109110
}
@@ -130,7 +131,11 @@ private static EconomicMap<ForeignCallSignature, HotSpotForeignCallLinkage> coll
130131
HotSpotProviders providers = replacements.getProviders();
131132
collectForeignCalls(providers.getForeignCalls(), allForeignCalls);
132133

133-
// Instantiate the Truffle compiler to collect its foreign calls as well
134+
/*
135+
* Instantiate the Truffle compiler to collect its foreign calls as well. Ensure the
136+
* snippets are registered using this encoder.
137+
*/
138+
replacements.shareSnippetEncoder();
134139
for (Backend truffleBackend : HotSpotTruffleCompilerImpl.ensureBackendsInitialized(options)) {
135140
HotSpotProviders truffleProviders = (HotSpotProviders) truffleBackend.getProviders();
136141
collectForeignCalls(truffleProviders.getForeignCalls(), allForeignCalls);

0 commit comments

Comments
 (0)