From 3c8e0e0d39f6ce5b0cd1458a8ac78d6897f82848 Mon Sep 17 00:00:00 2001 From: Tomas Zezula Date: Mon, 26 Feb 2024 13:19:21 +0100 Subject: [PATCH] [GR-52324] Add InsertGuardFencesPhase into runtime compilation phases. --- .../compiler/phases/common/InsertGuardFencesPhase.java | 7 ++++++- .../src/com/oracle/svm/truffle/TruffleFeature.java | 10 ++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/compiler/src/jdk.graal.compiler/src/jdk/graal/compiler/phases/common/InsertGuardFencesPhase.java b/compiler/src/jdk.graal.compiler/src/jdk/graal/compiler/phases/common/InsertGuardFencesPhase.java index 0983eaf4ddd1..01175be01f6a 100644 --- a/compiler/src/jdk.graal.compiler/src/jdk/graal/compiler/phases/common/InsertGuardFencesPhase.java +++ b/compiler/src/jdk.graal.compiler/src/jdk/graal/compiler/phases/common/InsertGuardFencesPhase.java @@ -32,6 +32,7 @@ import java.util.List; import java.util.Optional; +import jdk.graal.compiler.core.common.SpectrePHTMitigations; import jdk.graal.compiler.core.common.type.IntegerStamp; import jdk.graal.compiler.debug.Assertions; import jdk.graal.compiler.debug.DebugContext; @@ -68,6 +69,10 @@ public Optional notApplicableTo(GraphState graphState) { @Override protected void run(StructuredGraph graph) { + SpectrePHTMitigations mitigations = SpectrePHTBarriers.getValue(graph.getOptions()); + if (mitigations == SpectrePHTMitigations.None || mitigations == SpectrePHTMitigations.AllTargets) { + return; + } ControlFlowGraph cfg = ControlFlowGraph.newBuilder(graph).connectBlocks(true).computeFrequency(true).build(); for (AbstractBeginNode beginNode : graph.getNodes(AbstractBeginNode.TYPE)) { if (hasPotentialUnsafeAccess(cfg, beginNode)) { @@ -76,7 +81,7 @@ protected void run(StructuredGraph graph) { continue; } if (hasGuardUsages(beginNode)) { - if (SpectrePHTBarriers.getValue(graph.getOptions()) == NonDeoptGuardTargets) { + if (mitigations == NonDeoptGuardTargets) { if (isDeoptGuard(beginNode)) { graph.getDebug().log(DebugContext.VERBOSE_LEVEL, "Skipping deoptimizing guard speculation fence at %s", beginNode); continue; diff --git a/substratevm/src/com.oracle.svm.truffle/src/com/oracle/svm/truffle/TruffleFeature.java b/substratevm/src/com.oracle.svm.truffle/src/com/oracle/svm/truffle/TruffleFeature.java index 8519f01bcbe4..93d380abcc11 100644 --- a/substratevm/src/com.oracle.svm.truffle/src/com/oracle/svm/truffle/TruffleFeature.java +++ b/substratevm/src/com.oracle.svm.truffle/src/com/oracle/svm/truffle/TruffleFeature.java @@ -100,6 +100,7 @@ import java.util.function.ToLongFunction; import java.util.function.UnaryOperator; +import jdk.graal.compiler.phases.common.InsertGuardFencesPhase; import org.graalvm.collections.Pair; import org.graalvm.nativeimage.AnnotationAccess; import org.graalvm.nativeimage.ImageSingletons; @@ -970,6 +971,15 @@ public void registerGraalPhases(Providers providers, Suites suites, boolean host if (hosted && HostInliningPhase.Options.TruffleHostInlining.getValue(HostedOptionValues.singleton()) && suites.getHighTier() instanceof HighTier) { suites.getHighTier().prependPhase(new SubstrateHostInliningPhase(CanonicalizerPhase.create())); } + /* + * On HotSpot, the InsertGuardFencesPhase is inserted into the mid-tier depending on the + * runtime option SpectrePHTBarriers. However, TruffleFeature registers phases during + * image-build time. Therefore, for Truffle compilations, we need to register the phase + * eagerly because the SpectrePHTBarriers options is set only at image-execution time. + */ + if (!hosted && suites.getMidTier().findPhase(InsertGuardFencesPhase.class, true) == null) { + suites.getMidTier().appendPhase(new InsertGuardFencesPhase()); + } } }