From 8416d9b9384badd29856a0431c68fd5cfb96f964 Mon Sep 17 00:00:00 2001 From: Sacha Coppey Date: Tue, 12 Nov 2024 12:44:19 +0100 Subject: [PATCH] Persist all strengthened graphs from tracked methods --- .../graal/pointsto/heap/ImageLayerLoader.java | 40 ++++++++----------- .../pointsto/results/StrengthenGraphs.java | 7 ++-- .../src/com/oracle/svm/hosted/SVMHost.java | 11 +++-- .../svm/hosted/SubstrateStrengthenGraphs.java | 17 +------- .../svm/hosted/heap/SVMImageLayerLoader.java | 21 ++-------- 5 files changed, 32 insertions(+), 64 deletions(-) diff --git a/substratevm/src/com.oracle.graal.pointsto/src/com/oracle/graal/pointsto/heap/ImageLayerLoader.java b/substratevm/src/com.oracle.graal.pointsto/src/com/oracle/graal/pointsto/heap/ImageLayerLoader.java index 47ee090ab228..d89d6b431fed 100644 --- a/substratevm/src/com.oracle.graal.pointsto/src/com/oracle/graal/pointsto/heap/ImageLayerLoader.java +++ b/substratevm/src/com.oracle.graal.pointsto/src/com/oracle/graal/pointsto/heap/ImageLayerLoader.java @@ -578,10 +578,7 @@ public void addBaseLayerMethod(AnalysisMethod analysisMethod) { } public void initializeBaseLayerMethod(AnalysisMethod analysisMethod) { - initializeBaseLayerMethod(analysisMethod, getMethodData(analysisMethod)); - } - - protected void initializeBaseLayerMethod(AnalysisMethod analysisMethod, PersistedAnalysisMethod.Reader md) { + PersistedAnalysisMethod.Reader md = getMethodData(analysisMethod); registerFlag(md.getIsVirtualRootMethod(), true, () -> analysisMethod.registerAsVirtualRootMethod(PERSISTED)); registerFlag(md.getIsDirectRootMethod(), true, () -> analysisMethod.registerAsDirectRootMethod(PERSISTED)); registerFlag(md.getIsInvoked(), true, () -> analysisMethod.registerAsInvoked(PERSISTED)); @@ -600,16 +597,28 @@ public boolean hasAnalysisParsedGraph(AnalysisMethod analysisMethod) { public AnalysisParsedGraph getAnalysisParsedGraph(AnalysisMethod analysisMethod) { PersistedAnalysisMethod.Reader methodData = getMethodData(analysisMethod); - byte[] encodedAnalyzedGraph = readEncodedGraph(methodData.getAnalysisGraphLocation().toString()); boolean intrinsic = methodData.getAnalysisGraphIsIntrinsic(); - EncodedGraph analyzedGraph = (EncodedGraph) ObjectCopier.decode(imageLayerSnapshotUtil.getGraphDecoder(this, analysisMethod, universe.getSnippetReflection()), encodedAnalyzedGraph); + EncodedGraph analyzedGraph = getEncodedGraph(analysisMethod, methodData.getAnalysisGraphLocation()); if (hasStrengthenedGraph(analysisMethod)) { throw AnalysisError.shouldNotReachHere("Strengthened graphs are not supported until late loading is implemented."); } - afterGraphDecodeHook(analyzedGraph); return new AnalysisParsedGraph(analyzedGraph, intrinsic); } + public boolean hasStrengthenedGraph(AnalysisMethod analysisMethod) { + return getMethodData(analysisMethod).hasStrengthenedGraphLocation(); + } + + public EncodedGraph getStrengthenedGraph(AnalysisMethod analysisMethod) { + PersistedAnalysisMethod.Reader methodData = getMethodData(analysisMethod); + return getEncodedGraph(analysisMethod, methodData.getStrengthenedGraphLocation()); + } + + protected EncodedGraph getEncodedGraph(AnalysisMethod analysisMethod, Text.Reader location) { + byte[] encodedAnalyzedGraph = readEncodedGraph(location.toString()); + return (EncodedGraph) ObjectCopier.decode(imageLayerSnapshotUtil.getGraphDecoder(this, analysisMethod, universe.getSnippetReflection()), encodedAnalyzedGraph); + } + private byte[] readEncodedGraph(String location) { int closingBracketAt = location.length() - 1; AnalysisError.guarantee(location.charAt(0) == '@' && location.charAt(closingBracketAt) == ']', "Location must start with '@' and end with ']': %s", location); @@ -632,23 +641,6 @@ private byte[] readEncodedGraph(String location) { return bb.array(); } - public boolean hasStrengthenedGraph(AnalysisMethod analysisMethod) { - return getMethodData(analysisMethod).hasStrengthenedGraphLocation(); - } - - public void setStrengthenedGraph(AnalysisMethod analysisMethod) { - PersistedAnalysisMethod.Reader methodData = getMethodData(analysisMethod); - byte[] encodedAnalyzedGraph = readEncodedGraph(methodData.getStrengthenedGraphLocation().toString()); - EncodedGraph analyzedGraph = (EncodedGraph) ObjectCopier.decode(imageLayerSnapshotUtil.getGraphDecoder(this, analysisMethod, universe.getSnippetReflection()), encodedAnalyzedGraph); - afterGraphDecodeHook(analyzedGraph); - analysisMethod.setAnalyzedGraph(analyzedGraph); - } - - @SuppressWarnings("unused") - protected void afterGraphDecodeHook(EncodedGraph encodedGraph) { - - } - protected static int getId(String line) { return Integer.parseInt(line.split(" = ")[1]); } diff --git a/substratevm/src/com.oracle.graal.pointsto/src/com/oracle/graal/pointsto/results/StrengthenGraphs.java b/substratevm/src/com.oracle.graal.pointsto/src/com/oracle/graal/pointsto/results/StrengthenGraphs.java index b5834093e2a9..0e5d143918a2 100644 --- a/substratevm/src/com.oracle.graal.pointsto/src/com/oracle/graal/pointsto/results/StrengthenGraphs.java +++ b/substratevm/src/com.oracle.graal.pointsto/src/com/oracle/graal/pointsto/results/StrengthenGraphs.java @@ -240,7 +240,10 @@ public final void applyResults(AnalysisMethod method) { } if (method.analyzedInPriorLayer()) { - useSharedLayerGraph(method); + /* + * The method was already strengthened in a prior layer, so there is no need to + * strengthen it in this layer. + */ return; } @@ -273,8 +276,6 @@ public final void applyResults(AnalysisMethod method) { protected abstract void postStrengthenGraphs(StructuredGraph graph, AnalysisMethod method); - protected abstract void useSharedLayerGraph(AnalysisMethod method); - protected abstract void persistStrengthenGraph(AnalysisMethod method); /* diff --git a/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/SVMHost.java b/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/SVMHost.java index 6d9c11ff30c9..6371b13d7b79 100644 --- a/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/SVMHost.java +++ b/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/SVMHost.java @@ -258,11 +258,14 @@ public boolean useBaseLayer() { /** * If we are skipping the analysis of a prior layer method, we must ensure analysis was - * performed in the prior layer and the analysis results have been serialized. Currently this + * performed in the prior layer and the analysis results have been serialized. Currently, this * approximates to either: *
    - *
  1. We have an analyzed graph available. See {@link ImageLayerLoader#hasAnalysisParsedGraph} - * for which analysis graphs are persisted.
  2. + *
  3. We have a strengthened graph available. See {@link ImageLayerLoader#hasStrengthenedGraph} + * for which strengthened graphs are persisted. Having an analysis parsed graph (see + * {@link ImageLayerLoader#hasAnalysisParsedGraph}) is not enough because methods with only an + * analysis parsed graph are inlined before analysis, but not analyzed. Additionally, having a + * strengthened graph implies also having an analysis parsed graph.
  4. *
  5. A compile target exists this layer can call.
  6. *
* @@ -271,7 +274,7 @@ public boolean useBaseLayer() { @Override public boolean analyzedInPriorLayer(AnalysisMethod method) { ImageLayerLoader imageLayerLoader = HostedImageLayerBuildingSupport.singleton().getLoader(); - return imageLayerLoader.hasAnalysisParsedGraph(method) || HostedDynamicLayerInfo.singleton().compiledInPriorLayer(method); + return imageLayerLoader.hasStrengthenedGraph(method) || HostedDynamicLayerInfo.singleton().compiledInPriorLayer(method); } protected InlineBeforeAnalysisPolicyUtils getInlineBeforeAnalysisPolicyUtils() { diff --git a/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/SubstrateStrengthenGraphs.java b/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/SubstrateStrengthenGraphs.java index 711810d23e09..8f33fd0b94eb 100644 --- a/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/SubstrateStrengthenGraphs.java +++ b/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/SubstrateStrengthenGraphs.java @@ -26,7 +26,6 @@ import java.util.function.Supplier; -import com.oracle.graal.pointsto.heap.ImageLayerLoader; import com.oracle.graal.pointsto.infrastructure.Universe; import com.oracle.graal.pointsto.meta.AnalysisMethod; import com.oracle.graal.pointsto.meta.AnalysisType; @@ -44,8 +43,8 @@ import com.oracle.svm.hosted.code.SubstrateCompilationDirectives; import com.oracle.svm.hosted.imagelayer.HostedImageLayerBuildingSupport; import com.oracle.svm.hosted.meta.HostedType; - import com.oracle.svm.hosted.phases.AnalyzeJavaHomeAccessPhase; + import jdk.graal.compiler.graph.Node; import jdk.graal.compiler.nodes.ConstantNode; import jdk.graal.compiler.nodes.DeoptimizeNode; @@ -78,21 +77,9 @@ protected void postStrengthenGraphs(StructuredGraph graph, AnalysisMethod method } } - @Override - protected void useSharedLayerGraph(AnalysisMethod method) { - ImageLayerLoader imageLayerLoader = HostedImageLayerBuildingSupport.singleton().getLoader(); - /* - * GR-55294: When the analysis elements from the base layer will be able to be materialized - * after the analysis, fewer graphs will have to be loaded here as well. - */ - if (imageLayerLoader.hasStrengthenedGraph(method)) { - imageLayerLoader.setStrengthenedGraph(method); - } - } - @Override protected void persistStrengthenGraph(AnalysisMethod method) { - if (HostedImageLayerBuildingSupport.buildingSharedLayer() && method.isReachable()) { + if (HostedImageLayerBuildingSupport.buildingSharedLayer() && method.isTrackedAcrossLayers()) { HostedImageLayerBuildingSupport.singleton().getWriter().persistMethodStrengthenedGraph(method); } } diff --git a/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/heap/SVMImageLayerLoader.java b/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/heap/SVMImageLayerLoader.java index 8e697fb9d7f3..2e2dde1425d0 100644 --- a/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/heap/SVMImageLayerLoader.java +++ b/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/heap/SVMImageLayerLoader.java @@ -44,7 +44,6 @@ import org.graalvm.collections.UnmodifiableEconomicMap; import org.graalvm.nativeimage.impl.CEntryPointLiteralCodePointer; -import com.oracle.graal.pointsto.flow.AnalysisParsedGraph; import com.oracle.graal.pointsto.heap.ImageHeapConstant; import com.oracle.graal.pointsto.heap.ImageHeapInstance; import com.oracle.graal.pointsto.heap.ImageLayerLoader; @@ -55,7 +54,6 @@ import com.oracle.graal.pointsto.heap.SharedLayerSnapshotCapnProtoSchemaHolder.ImageSingletonObject; import com.oracle.graal.pointsto.heap.SharedLayerSnapshotCapnProtoSchemaHolder.KeyStoreEntry; import com.oracle.graal.pointsto.heap.SharedLayerSnapshotCapnProtoSchemaHolder.PersistedAnalysisField; -import com.oracle.graal.pointsto.heap.SharedLayerSnapshotCapnProtoSchemaHolder.PersistedAnalysisMethod; import com.oracle.graal.pointsto.heap.SharedLayerSnapshotCapnProtoSchemaHolder.PersistedAnalysisType; import com.oracle.graal.pointsto.heap.SharedLayerSnapshotCapnProtoSchemaHolder.PersistedConstant; import com.oracle.graal.pointsto.meta.AnalysisField; @@ -76,7 +74,6 @@ import com.oracle.svm.hosted.SVMHost; import com.oracle.svm.hosted.c.CGlobalDataFeature; import com.oracle.svm.hosted.fieldfolding.StaticFinalFieldFoldingFeature; -import com.oracle.svm.hosted.imagelayer.HostedDynamicLayerInfo; import com.oracle.svm.hosted.meta.HostedUniverse; import com.oracle.svm.hosted.meta.RelocatableConstant; import com.oracle.svm.hosted.util.IdentityHashCodeUtil; @@ -170,26 +167,14 @@ protected Annotation[] getAnnotations(StructList.Reader