Skip to content

Commit

Permalink
[GR-32314] Formalize compiler phase ordering constraints.
Browse files Browse the repository at this point in the history
  • Loading branch information
GiuntaJ committed Aug 10, 2022
1 parent 5c7166e commit f188332
Show file tree
Hide file tree
Showing 166 changed files with 2,587 additions and 724 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,4 @@ bench-results.json
jmh_result.json
/vm/src/installer/dist/
/tools/generators/node_modules/
/tools/generators/out/
/tools/generators/out/
5 changes: 3 additions & 2 deletions compiler/mx.compiler/mx_compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,7 @@ def compiler_gate_runner(suites, unit_test_runs, bootstrap_tests, tasks, extraVM
# Run ctw against rt.jar on hosted
ctw_flags = [
'-DCompileTheWorld.Config=Inline=false CompilationFailureAction=ExitVM CompilationBailoutAsFailure=false', '-esa', '-XX:-UseJVMCICompiler', '-XX:+EnableJVMCI',
'-DCompileTheWorld.MultiThreaded=true', '-Dgraal.InlineDuringParsing=false', '-Dgraal.TrackNodeSourcePosition=true',
'-DCompileTheWorld.MultiThreaded=true', '-Dgraal.InlineDuringParsing=false', '-Dgraal.TrackNodeSourcePosition=true', '-Dgraal.VerifyPhasePlan=true',
'-DCompileTheWorld.Verbose=false', '-XX:ReservedCodeCacheSize=300m',
]
with Task('CTW:hosted', tasks, tags=GraalTags.ctw, report=True) as t:
Expand Down Expand Up @@ -608,7 +608,7 @@ def compiler_gate_benchmark_runner(tasks, extraVMarguments=None, prefix=''):
if mx.get_arch() not in _registers:
mx.warn('No registers for register pressure tests are defined for architecture ' + mx.get_arch())

_defaultFlags = ['-Dgraal.CompilationWatchDogStartDelay=60.0D']
_defaultFlags = ['-Dgraal.CompilationWatchDogStartDelay=60.0D', '-Dgraal.VerifyPhasePlan=true']
_assertionFlags = ['-esa', '-Dgraal.DetailedAsserts=true']
_graalErrorFlags = _compiler_error_options()
_graalEconomyFlags = ['-Dgraal.CompilerConfiguration=economy']
Expand Down Expand Up @@ -703,6 +703,7 @@ def _unittest_config_participant(config):
vmArgs.append('--add-modules=' + jmd.name)

vmArgs.append('-Dgraal.TrackNodeSourcePosition=true')
vmArgs.append('-Dgraal.VerifyPhasePlan=true')
vmArgs.append('-esa')

# Always run unit tests without UseJVMCICompiler unless explicitly requested
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2015, 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2015, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -37,6 +37,8 @@
import org.graalvm.compiler.phases.tiers.LowTierContext;
import org.graalvm.compiler.phases.tiers.Suites;

import jdk.vm.ci.code.Architecture;

public class AMD64SuitesCreator extends DefaultSuitesCreator {

public AMD64SuitesCreator(CompilerConfiguration compilerConfiguration, Plugins plugins) {
Expand All @@ -48,8 +50,8 @@ public AMD64SuitesCreator(CompilerConfiguration compilerConfiguration) {
}

@Override
public Suites createSuites(OptionValues options) {
Suites suites = super.createSuites(options);
public Suites createSuites(OptionValues options, Architecture arch) {
Suites suites = super.createSuites(options, arch);
ListIterator<BasePhase<? super LowTierContext>> position = suites.getLowTier().findPhase(UseTrappingNullChecksPhase.class);
if (position != null) {
position.previous();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
*/
package org.graalvm.compiler.core.amd64;

import java.util.Optional;

import org.graalvm.collections.EconomicMap;
import org.graalvm.compiler.core.common.GraalOptions;
import org.graalvm.compiler.core.common.cfg.AbstractControlFlowGraph;
Expand All @@ -34,6 +36,7 @@
import org.graalvm.compiler.nodes.DeoptimizeNode;
import org.graalvm.compiler.nodes.DeoptimizingFixedWithNextNode;
import org.graalvm.compiler.nodes.DynamicDeoptimizeNode;
import org.graalvm.compiler.nodes.GraphState;
import org.graalvm.compiler.nodes.IfNode;
import org.graalvm.compiler.nodes.LogicNode;
import org.graalvm.compiler.nodes.PhiNode;
Expand Down Expand Up @@ -76,6 +79,11 @@ private static boolean conditionIsZeroCheck(LogicNode condition, ValueNode divis
return false;
}

@Override
public Optional<NotApplicable> canApply(GraphState graphState) {
return ALWAYS_APPLICABLE;
}

@Override
protected void run(StructuredGraph graph, LowTierContext context) {
if (!GraalOptions.FloatingDivNodes.getValue(graph.getOptions())) {
Expand Down Expand Up @@ -198,6 +206,11 @@ public void actionBeforeGuardRewrite(DeoptimizingFixedWithNextNode trappingVersi

}

@Override
public Optional<NotApplicable> canApply(GraphState graphState) {
return ALWAYS_APPLICABLE;
}

@Override
protected void run(StructuredGraph graph, LowTierContext context) {
MetaAccessProvider metaAccessProvider = context.getMetaAccess();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,9 @@ public final class GraalOptions {
@Option(help = "", type = OptionType.Debug)
public static final OptionKey<Boolean> VerifyPhases = new OptionKey<>(false);

@Option(help = "Verifies that the phase plan respects the phase ordering constraints.", type = OptionType.Debug)
public static final OptionKey<Boolean> VerifyPhasePlan = new OptionKey<>(false);

// Debug settings:
@Option(help = "Start tracing compiled GC barriers after N garbage collections (disabled if N <= 0).", type = OptionType.Debug)
public static final OptionKey<Integer> GCDebugStartCycle = new OptionKey<>(-1);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2021, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -51,6 +51,26 @@ default String getPhaseName(T phase) {
return phase.getClass().getName();
}

/**
* Retrieves the changes to the state of the graph caused by the phase at {@code position} in
* the phase plan.
*
* @param position the position of the phase in the plan
*/
default String getGraphStateDiff(int position) {
return null;
}

/**
* Gets the index of the phase that caused a failure during the execution of this phase plan
* (assertion error, compilation bailout, or any other Throwable).
*
* @return the index of the phase that caused the failure, {@code -1} means no failure occurred.
*/
default int getFailureIndex() {
return -1;
}

/**
* Utility for formatting a plan as a string.
*/
Expand Down Expand Up @@ -80,16 +100,31 @@ public <T> String toString(PhasePlan<T> plan) {

@SuppressWarnings("unchecked")
private <T> Formatter printPlan(String indent, String indentLast, PhasePlan<T> plan, Formatter buf) {
int index = 0;
for (Iterator<T> iter = plan.getPhases().iterator(); iter.hasNext();) {
T phase = iter.next();
String className = plan.getPhaseName(phase);
boolean hasNext = iter.hasNext();
buf.format("%s%s%n", hasNext ? indent : indentLast, abbreviate(className));
buf.format("%s%s", hasNext ? indent : indentLast, abbreviate(className));

String graphState = plan.getGraphStateDiff(index);
if (graphState != null) {
buf.format(" %s", graphState);
}

if (plan.getFailureIndex() == index) {
buf.format(" /* THE FAILURE OCCURRED DURING THIS PHASE */");
}

buf.format("%n");

if (phase instanceof PhasePlan) {
PhasePlan<T> subPlan = (PhasePlan<T>) phase;
String prefix = hasNext ? CONNECTING_INDENT : EMPTY_INDENT;
printPlan(prefix + indent, prefix + indentLast, subPlan, buf);
}

index++;
}
return buf;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2011, 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2011, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -28,7 +28,6 @@

import org.graalvm.compiler.debug.DebugContext;
import org.graalvm.compiler.nodes.StructuredGraph;
import org.graalvm.compiler.phases.BasePhase;
import org.graalvm.compiler.printer.BinaryGraphPrinter;
import org.junit.Before;
import org.junit.Test;
Expand All @@ -53,7 +52,7 @@ public void phaseNameIsRecognizedAsType() {
assertEquals(MyPhase.class.getName(), res);
}

private static final class MyPhase extends BasePhase<Void> {
private static final class MyPhase extends TestBasePhase<Void> {
@Override
protected void run(StructuredGraph graph, Void context) {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ public void test14() {
protected void prepareGraph(StructuredGraph graph, CanonicalizerPhase canonicalizer, CoreProviders context, boolean applyLowering) {
super.prepareGraph(graph, canonicalizer, context, applyLowering);
graph.clearAllStateAfterForTestingOnly();
graph.setGuardsStage(StructuredGraph.GuardsStage.AFTER_FSA);
graph.getGraphState().setAfterFSA();
DebugContext debug = graph.getDebug();
debug.dump(DebugContext.BASIC_LEVEL, graph, "After preparation");
canonicalizer.apply(graph, context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,10 @@ static NodeCount count(NodeClass<?> nodeClass, int count) {

private void checkHighTierGraph(String snippet, NodeCount... counts) {
StructuredGraph graph = parseEager(snippet, AllowAssumptions.YES);
Suites suites = getBackend().getSuites().getDefaultSuites(new OptionValues(getInitialOptions(), GraalOptions.LoopPeeling, false));
Suites suites = super.createSuites(new OptionValues(getInitialOptions(), GraalOptions.LoopPeeling, false));
PhaseSuite<HighTierContext> ht = suites.getHighTier().copy();
ListIterator<BasePhase<? super HighTierContext>> position = ht.findPhase(LoopFullUnrollPhase.class);
position.add(new BasePhase<HighTierContext>() {
position.add(new TestBasePhase<HighTierContext>() {

@Override
protected void run(@SuppressWarnings("hiding") StructuredGraph graph, HighTierContext context) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2016, 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2016, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -28,7 +28,6 @@
import org.graalvm.compiler.nodes.extended.IntegerSwitchNode;
import org.graalvm.compiler.nodes.java.LoadIndexedNode;
import org.graalvm.compiler.options.OptionValues;
import org.graalvm.compiler.phases.Phase;
import org.graalvm.compiler.phases.tiers.Suites;
import org.junit.Assume;
import org.junit.Test;
Expand Down Expand Up @@ -141,7 +140,7 @@ public void test2() {
@Override
protected Suites createSuites(OptionValues options) {
Suites ret = super.createSuites(options);
ret.getHighTier().prependPhase(new Phase() {
ret.getHighTier().prependPhase(new TestPhase() {
@Override
protected void run(StructuredGraph graph) {
Assume.assumeTrue(graph.getNodes().filter(LoadIndexedNode.class).first().array().isConstant());
Expand All @@ -156,7 +155,7 @@ public CharSequence getName() {
return "CheckGraphPhase";
}
});
ret.getHighTier().addBeforeLast(new Phase() {
ret.getHighTier().addBeforeLast(new TestPhase() {
@Override
protected void run(StructuredGraph graph) {
/* Re-writing of the switch cases eliminates the array load. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@ public class FloatingDivTest extends GraalCompilerTest {

private void checkHighTierGraph(String snippet, int fixedDivsBeforeLowering, int floatingDivsBeforeLowering, int fixedDivAfterLowering, int floatingDivAfterLowering) {
StructuredGraph graph = parseEager(snippet, AllowAssumptions.YES);
Suites suites = getBackend().getSuites().getDefaultSuites(new OptionValues(getInitialOptions(), GraalOptions.LoopPeeling, false, GraalOptions.EarlyGVN, false));
Suites suites = super.createSuites(new OptionValues(getInitialOptions(), GraalOptions.LoopPeeling, false, GraalOptions.EarlyGVN, false));
PhaseSuite<HighTierContext> ht = suites.getHighTier().copy();
ListIterator<BasePhase<? super HighTierContext>> position = ht.findPhase(LoweringPhase.class);
position.previous();
position.add(new BasePhase<HighTierContext>() {
position.add(new TestBasePhase<HighTierContext>() {

@Override
protected void run(@SuppressWarnings("hiding") StructuredGraph graph, HighTierContext context) {
Expand All @@ -74,7 +74,7 @@ private void checkFinalGraph(String snippet, int fixedDivs, int floatingDivs, in
return;
}
StructuredGraph graph = parseEager(snippet, AllowAssumptions.YES);
Suites suites = getBackend().getSuites().getDefaultSuites(new OptionValues(getInitialOptions(), GraalOptions.LoopPeeling, false, GraalOptions.EarlyGVN, false));
Suites suites = super.createSuites(new OptionValues(getInitialOptions(), GraalOptions.LoopPeeling, false, GraalOptions.EarlyGVN, false));

suites.getHighTier().apply(graph, getDefaultHighTierContext());
suites.getMidTier().apply(graph, getDefaultMidTierContext());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,8 @@
import org.graalvm.compiler.phases.BasePhase;
import org.graalvm.compiler.phases.OptimisticOptimizations;
import org.graalvm.compiler.phases.OptimisticOptimizations.Optimization;
import org.graalvm.compiler.phases.Phase;
import org.graalvm.compiler.phases.PhaseSuite;
import org.graalvm.compiler.phases.Speculative;
import org.graalvm.compiler.phases.common.CanonicalizerPhase;
import org.graalvm.compiler.phases.common.inlining.InliningPhase;
import org.graalvm.compiler.phases.common.inlining.info.InlineInfo;
Expand Down Expand Up @@ -267,7 +267,7 @@ protected static void safepoint() {
}

protected Suites createSuites(OptionValues opts) {
Suites ret = backend.getSuites().getDefaultSuites(opts).copy();
Suites ret = backend.getSuites().getDefaultSuites(opts, getTarget().arch).copy();

String phasePlanFile = System.getProperty("test.graal.phaseplan.file");
if (phasePlanFile != null) {
Expand All @@ -276,6 +276,12 @@ protected Suites createSuites(OptionValues opts) {
testPhasePlanSerialization(ret, opts);
}

if (getSpeculationLog() == null) {
ret.getHighTier().removeSpeculativePhases();
ret.getMidTier().removeSpeculativePhases();
ret.getLowTier().removeSpeculativePhases();
}

ListIterator<BasePhase<? super HighTierContext>> iter = ret.getHighTier().findPhase(ConvertDeoptimizeToGuardPhase.class, true);
if (iter == null) {
/*
Expand All @@ -284,8 +290,7 @@ protected Suites createSuites(OptionValues opts) {
*/
iter = ret.getHighTier().findPhase(CanonicalizerPhase.class);
}
ret.getHighTier().appendPhase(new Phase() {

ret.getHighTier().appendPhase(new TestPhase() {
@Override
protected void run(StructuredGraph graph) {
checkHighTierGraph(graph);
Expand All @@ -301,8 +306,7 @@ public CharSequence getName() {
return "CheckGraphPhase";
}
});
ret.getMidTier().appendPhase(new Phase() {

ret.getMidTier().appendPhase(new TestPhase() {
@Override
protected void run(StructuredGraph graph) {
checkMidTierGraph(graph);
Expand All @@ -318,8 +322,7 @@ public CharSequence getName() {
return "CheckGraphPhase";
}
});
ret.getLowTier().appendPhase(new Phase() {

ret.getLowTier().appendPhase(new TestPhase() {
@Override
protected void run(StructuredGraph graph) {
checkLowTierGraph(graph);
Expand Down Expand Up @@ -431,7 +434,7 @@ private void testPhasePlanSerialization(Suites originalSuites, OptionValues opts
savePhasePlan(dos, originalSuites);
}
try (DataInputStream in = new DataInputStream(new ByteArrayInputStream(baos.toByteArray()))) {
newSuites = loadPhasePlan(in, backend.getSuites().getDefaultSuites(opts).copy());
newSuites = loadPhasePlan(in, backend.getSuites().getDefaultSuites(opts, getTarget().arch).copy());
}
} catch (IOException e) {
throw new GraalError(e, "Error in phase plan serialization");
Expand Down Expand Up @@ -1304,6 +1307,10 @@ protected void applyFrontEnd(StructuredGraph graph) {

protected StructuredGraph lastCompiledGraph;

/**
* This method needs to be overwritten by tests that want to use {@link Speculative} phases. It
* may be called multiple times before a compilation is started.
*/
protected SpeculationLog getSpeculationLog() {
return null;
}
Expand Down Expand Up @@ -1469,7 +1476,8 @@ protected final Builder builder(ResolvedJavaMethod method, AllowAssumptions allo
}

protected final Builder builder(ResolvedJavaMethod method, AllowAssumptions allowAssumptions, CompilationIdentifier compilationId, OptionValues options) {
return new Builder(options, getDebugContext(options, compilationId.toString(CompilationIdentifier.Verbosity.ID), method), allowAssumptions).method(method).compilationId(compilationId);
return new Builder(options, getDebugContext(options, compilationId.toString(CompilationIdentifier.Verbosity.ID), method), allowAssumptions).method(method).compilationId(
compilationId);
}

protected final Builder builder(ResolvedJavaMethod method, AllowAssumptions allowAssumptions, OptionValues options) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2017, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2017, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand All @@ -25,11 +25,9 @@

package org.graalvm.compiler.core.test;

import org.graalvm.compiler.api.test.Graal;
import org.graalvm.compiler.nodes.StructuredGraph;
import org.graalvm.compiler.nodes.memory.WriteNode;
import org.graalvm.compiler.phases.tiers.Suites;
import org.graalvm.compiler.runtime.RuntimeProvider;
import org.junit.Test;

public class MemoryGraphCanonicalizeTest extends GraalCompilerTest {
Expand Down Expand Up @@ -72,7 +70,7 @@ public void testComplexElimination() {

public void testGraph(String name, int expectedWrites) {
StructuredGraph graph = parseEager(name, StructuredGraph.AllowAssumptions.YES);
Suites s = Graal.getRequiredCapability(RuntimeProvider.class).getHostBackend().getSuites().getDefaultSuites(getInitialOptions());
Suites s = super.createSuites(getInitialOptions());
s.getHighTier().apply(graph, getDefaultHighTierContext());
s.getMidTier().apply(graph, getDefaultMidTierContext());

Expand Down
Loading

0 comments on commit f188332

Please sign in to comment.