Skip to content

Commit a0e665e

Browse files
committed
[GR-69070] Improve replayability with different compiler options
PullRequest: graal/21959
2 parents f23bb72 + 2c49fe4 commit a0e665e

18 files changed

+379
-267
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ public GlobalMetrics getMetricValues() {
172172
throw new GraalError("No backend available for host architecture \"%s\"", hostArchitecture);
173173
}
174174
if (replayCompilationSupport != null) {
175-
factory = replayCompilationSupport.decorateBackendFactory(factory);
175+
factory = replayCompilationSupport.decorateBackendFactory(factory, jvmciRuntime);
176176
}
177177
hostBackend = registerBackend(factory.createBackend(this, compilerConfiguration, jvmciRuntime, null));
178178
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
import jdk.graal.compiler.graph.NodeMap;
6363
import jdk.graal.compiler.graph.NodeSourcePosition;
6464
import jdk.graal.compiler.hotspot.meta.HotSpotProviders;
65+
import jdk.graal.compiler.hotspot.replaycomp.proxy.HotSpotResolvedObjectTypeProxy;
6566
import jdk.graal.compiler.hotspot.stubs.AbstractForeignCallStub;
6667
import jdk.graal.compiler.hotspot.stubs.ForeignCallStub;
6768
import jdk.graal.compiler.hotspot.word.HotSpotWordTypes;
@@ -532,6 +533,8 @@ private synchronized EncodedSnippets encodeSnippets(DebugContext debug, Economic
532533
lookupSnippetType(SnippetTemplate.SnippetInfo.class);
533534
lookupSnippetType(ForeignCallStub.class);
534535
lookupSnippetType(HotSpotSpeculationLog.HotSpotSpeculation.class);
536+
// Needed to pass constant type parameters to snippets when recording/replaying.
537+
lookupSnippetType(HotSpotResolvedObjectTypeProxy.class);
535538

536539
registerAbstractForeignCallStubInfo();
537540

compiler/src/jdk.graal.compiler/src/jdk/graal/compiler/hotspot/replaycomp/CompilationProxies.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,4 +73,11 @@ public interface CompilationProxies {
7373
* @return a debug closeable object representing the debug context
7474
*/
7575
DebugCloseable withDebugContext(DebugContext debugContext);
76+
77+
/**
78+
* Enters the context of a method compilation.
79+
*
80+
* @return a scope for the context
81+
*/
82+
DebugCloseable enterCompilationContext();
7683
}

compiler/src/jdk.graal.compiler/src/jdk/graal/compiler/hotspot/replaycomp/CompilerInterfaceDeclarations.java

Lines changed: 119 additions & 82 deletions
Large diffs are not rendered by default.

compiler/src/jdk.graal.compiler/src/jdk/graal/compiler/hotspot/replaycomp/HotSpotProxyBackendFactory.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import jdk.graal.compiler.hotspot.meta.HotSpotHostForeignCallsProvider;
3333
import jdk.vm.ci.hotspot.HotSpotCodeCacheProvider;
3434
import jdk.vm.ci.hotspot.HotSpotConstantReflectionProvider;
35+
import jdk.vm.ci.hotspot.HotSpotJVMCIRuntime;
3536
import jdk.vm.ci.meta.Constant;
3637
import jdk.vm.ci.meta.JavaConstant;
3738
import jdk.vm.ci.meta.MemoryAccessProvider;
@@ -48,11 +49,12 @@ class HotSpotProxyBackendFactory implements HotSpotBackendFactoryDecorators {
4849

4950
private final ReplayCompilationSupport replayCompilationSupport;
5051

51-
private MetaAccessProvider metaAccessProviderProxy;
52+
private final HotSpotJVMCIRuntime jvmciRuntime;
5253

53-
HotSpotProxyBackendFactory(CompilationProxies proxies, ReplayCompilationSupport replayCompilationSupport) {
54+
HotSpotProxyBackendFactory(CompilationProxies proxies, ReplayCompilationSupport replayCompilationSupport, HotSpotJVMCIRuntime jvmciRuntime) {
5455
this.proxies = proxies;
5556
this.replayCompilationSupport = replayCompilationSupport;
57+
this.jvmciRuntime = jvmciRuntime;
5658
}
5759

5860
@Override
@@ -62,14 +64,13 @@ public void afterJVMCIProvidersCreated() {
6264
* mirrors. We must identify the local mirrors before initialization continues to avoid
6365
* creating duplicate proxies for equivalent local mirrors.
6466
*/
65-
replayCompilationSupport.findLocalMirrors();
67+
replayCompilationSupport.findLocalMirrors(jvmciRuntime);
6668
}
6769

6870
@Override
6971
public MetaAccessProvider decorateMetaAccessProvider(MetaAccessProvider metaAccess) {
7072
// Do not record snippet types in libgraal - decorate the JVMCI meta access only.
71-
metaAccessProviderProxy = (MetaAccessProvider) proxies.proxify(metaAccess);
72-
return new HotSpotSnippetMetaAccessProvider(metaAccessProviderProxy);
73+
return new HotSpotSnippetMetaAccessProvider((MetaAccessProvider) proxies.proxify(metaAccess));
7374
}
7475

7576
@Override
@@ -79,7 +80,7 @@ public HotSpotConstantReflectionProvider decorateConstantReflectionProvider(HotS
7980
return new DecoratedConstantReflectionProvider(delegate);
8081
}
8182

82-
private final class DecoratedConstantReflectionProvider extends HotSpotConstantReflectionProvider {
83+
private static final class DecoratedConstantReflectionProvider extends HotSpotConstantReflectionProvider {
8384
private final HotSpotConstantReflectionProvider delegate;
8485

8586
private DecoratedConstantReflectionProvider(HotSpotConstantReflectionProvider delegate) {
@@ -142,7 +143,7 @@ public ResolvedJavaType asJavaType(Constant constant) {
142143
* Avoid recording an operation with the snippet constant, which is not
143144
* serializable.
144145
*/
145-
return metaAccessProviderProxy.lookupJavaType(objectConstant.asObject(Object.class).getClass());
146+
return objectConstant.asObject(ResolvedJavaType.class);
146147
} else {
147148
return delegate.asJavaType(constant);
148149
}

compiler/src/jdk.graal.compiler/src/jdk/graal/compiler/hotspot/replaycomp/RecordedOperationPersistence.java

Lines changed: 69 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,20 @@ private interface RecursiveDeserializer {
144144
Object deserialize(Object json, ProxyFactory proxyFactory) throws DeserializationException;
145145

146146
Object deserialize(Object json, ProxyFactory proxyFactory, String tag) throws DeserializationException;
147+
148+
/**
149+
* Sets the {@link Architecture} parsed by this deserializer.
150+
*
151+
* @param arch the architecture
152+
*/
153+
void setArchitecture(Architecture arch);
154+
155+
/**
156+
* Gets the {@link Architecture} parsed by this deserializer.
157+
*
158+
* @return the architecture
159+
*/
160+
Architecture getArchitecture();
147161
}
148162

149163
private sealed interface ObjectSerializer {
@@ -805,7 +819,6 @@ public Object deserialize(EconomicMap<String, Object> json, RecursiveDeserialize
805819
}
806820

807821
private static final class RegisterSerializer implements ObjectSerializer {
808-
809822
@Override
810823
public Class<?> clazz() {
811824
return Register.class;
@@ -820,20 +833,17 @@ public String tag() {
820833
public void serialize(Object instance, JsonBuilder.ObjectBuilder objectBuilder, RecursiveSerializer serializer) throws IOException {
821834
Register register = (Register) instance;
822835
objectBuilder.append("number", register.number);
823-
objectBuilder.append("name", register.name);
824-
objectBuilder.append("encoding", register.encoding);
825-
objectBuilder.append("catName", register.getRegisterCategory().toString());
826-
objectBuilder.append("containsRef", register.mayContainReference());
827836
}
828837

829838
@Override
830-
public Object deserialize(EconomicMap<String, Object> json, RecursiveDeserializer deserializer, ProxyFactory proxyFactory) {
839+
public Object deserialize(EconomicMap<String, Object> json, RecursiveDeserializer deserializer, ProxyFactory proxyFactory) throws DeserializationException {
831840
int number = (int) json.get("number");
832-
String name = (String) json.get("name");
833-
int encoding = (int) json.get("encoding");
834-
String catName = (String) json.get("catName");
835-
boolean containsRef = (boolean) json.get("containsRef");
836-
return new Register(number, encoding, name, new Register.RegisterCategory(catName, containsRef));
841+
for (Register register : deserializer.getArchitecture().getRegisters()) {
842+
if (register.number == number) {
843+
return register;
844+
}
845+
}
846+
throw new DeserializationException(this, json, "Register not found");
837847
}
838848
}
839849

@@ -1687,12 +1697,14 @@ public void serialize(Object instance, JsonBuilder.ObjectBuilder objectBuilder,
16871697
public Object deserialize(EconomicMap<String, Object> json, RecursiveDeserializer deserializer, ProxyFactory proxyFactory) throws DeserializationException {
16881698
String name = (String) json.get("name");
16891699
EnumSet<?> features = (EnumSet<?>) deserializer.deserialize(json.get("features"), proxyFactory);
1690-
return switch (name) {
1700+
Architecture architecture = switch (name) {
16911701
case "AMD64" -> new AMD64((EnumSet<AMD64.CPUFeature>) features);
16921702
case "riscv64" -> new RISCV64((EnumSet<RISCV64.CPUFeature>) features);
16931703
case "aarch64" -> new AArch64((EnumSet<AArch64.CPUFeature>) features);
16941704
default -> throw new IllegalStateException("Unexpected value: " + name);
16951705
};
1706+
deserializer.setArchitecture(architecture);
1707+
return architecture;
16961708
}
16971709
}
16981710

@@ -1932,41 +1944,55 @@ public void dump(RecordedCompilationUnit compilationUnit, JsonWriter writer) thr
19321944
recursiveSerializer.serialize(compilationUnit, writer.valueBuilder(), RecordedCompilationUnitSerializer.TAG);
19331945
}
19341946

1935-
private final RecursiveDeserializer recursiveDeserializer = new RecursiveDeserializer() {
1936-
@Override
1937-
@SuppressWarnings("unchecked")
1938-
public Object deserialize(Object json, ProxyFactory proxyFactory) throws DeserializationException {
1939-
if (json instanceof EconomicMap<?, ?>) {
1940-
EconomicMap<String, Object> map = (EconomicMap<String, Object>) json;
1941-
String tag = (String) map.get("tag");
1942-
if (tag == null) {
1943-
throw new IllegalArgumentException("The JSON map does not contain a tag: " + map);
1944-
}
1945-
ObjectSerializer deserializer = tagSerializers.get(tag);
1946-
if (deserializer == null) {
1947-
throw new IllegalArgumentException("No deserializer registered for tag " + tag);
1947+
private RecursiveDeserializer createRecursiveDeserializer() {
1948+
return new RecursiveDeserializer() {
1949+
@Override
1950+
@SuppressWarnings("unchecked")
1951+
public Object deserialize(Object json, ProxyFactory proxyFactory) throws DeserializationException {
1952+
if (json instanceof EconomicMap<?, ?>) {
1953+
EconomicMap<String, Object> map = (EconomicMap<String, Object>) json;
1954+
String tag = (String) map.get("tag");
1955+
if (tag == null) {
1956+
throw new IllegalArgumentException("The JSON map does not contain a tag: " + map);
1957+
}
1958+
ObjectSerializer deserializer = tagSerializers.get(tag);
1959+
if (deserializer == null) {
1960+
throw new IllegalArgumentException("No deserializer registered for tag " + tag);
1961+
}
1962+
return deserializer.deserialize(map, this, proxyFactory);
1963+
} else {
1964+
return json;
19481965
}
1949-
return deserializer.deserialize(map, this, proxyFactory);
1950-
} else {
1951-
return json;
19521966
}
1953-
}
19541967

1955-
@Override
1956-
@SuppressWarnings("unchecked")
1957-
public Object deserialize(Object json, ProxyFactory proxyFactory, String tag) throws DeserializationException {
1958-
if (json instanceof EconomicMap<?, ?>) {
1959-
EconomicMap<String, Object> map = (EconomicMap<String, Object>) json;
1960-
ObjectSerializer deserializer = tagSerializers.get(tag);
1961-
if (deserializer == null) {
1962-
throw new IllegalArgumentException("No deserializer registered for tag " + tag);
1968+
@Override
1969+
@SuppressWarnings("unchecked")
1970+
public Object deserialize(Object json, ProxyFactory proxyFactory, String tag) throws DeserializationException {
1971+
if (json instanceof EconomicMap<?, ?>) {
1972+
EconomicMap<String, Object> map = (EconomicMap<String, Object>) json;
1973+
ObjectSerializer deserializer = tagSerializers.get(tag);
1974+
if (deserializer == null) {
1975+
throw new IllegalArgumentException("No deserializer registered for tag " + tag);
1976+
}
1977+
return deserializer.deserialize(map, this, proxyFactory);
1978+
} else {
1979+
throw new IllegalArgumentException("Expected a map.");
19631980
}
1964-
return deserializer.deserialize(map, this, proxyFactory);
1965-
} else {
1966-
throw new IllegalArgumentException("Expected a map.");
19671981
}
1968-
}
1969-
};
1982+
1983+
private Architecture architecture;
1984+
1985+
@Override
1986+
public void setArchitecture(Architecture arch) {
1987+
architecture = arch;
1988+
}
1989+
1990+
@Override
1991+
public Architecture getArchitecture() {
1992+
return architecture;
1993+
}
1994+
};
1995+
}
19701996

19711997
/**
19721998
* Loads a recorded compilation unit from the given reader.
@@ -1979,6 +2005,6 @@ public Object deserialize(Object json, ProxyFactory proxyFactory, String tag) th
19792005
*/
19802006
public RecordedCompilationUnit load(Reader source, ProxyFactory proxyFactory) throws IOException, DeserializationException {
19812007
JsonParser parser = new JsonParser(source);
1982-
return (RecordedCompilationUnit) recursiveDeserializer.deserialize(parser.parse(), proxyFactory, RecordedCompilationUnitSerializer.TAG);
2008+
return (RecordedCompilationUnit) createRecursiveDeserializer().deserialize(parser.parse(), proxyFactory, RecordedCompilationUnitSerializer.TAG);
19832009
}
19842010
}

compiler/src/jdk.graal.compiler/src/jdk/graal/compiler/hotspot/replaycomp/RecordingCompilationProxies.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -139,11 +139,7 @@ public List<OperationRecorder.RecordedOperation> collectOperationsForSerializati
139139
return recorder.getCurrentRecordedOperations();
140140
}
141141

142-
/**
143-
* Enters the context of a method compilation for the current compilation thread.
144-
*
145-
* @return a scope for the context
146-
*/
142+
@Override
147143
public DebugCloseable enterCompilationContext() {
148144
return recorder.enterCompilationContext();
149145
}

0 commit comments

Comments
 (0)