Search in sources :

Example 6 with SchedulePhase

use of org.graalvm.compiler.phases.schedule.SchedulePhase in project graal by oracle.

the class GraphOrder method assertSchedulableGraph.

/**
 * This method schedules the graph and makes sure that, for every node, all inputs are available
 * at the position where it is scheduled. This is a very expensive assertion.
 */
public static boolean assertSchedulableGraph(final StructuredGraph graph) {
    assert graph.getGuardsStage() != GuardsStage.AFTER_FSA : "Cannot use the BlockIteratorClosure after FrameState Assignment, HIR Loop Data Structures are no longer valid.";
    try {
        final SchedulePhase schedulePhase = new SchedulePhase(SchedulingStrategy.LATEST_OUT_OF_LOOPS, true);
        final EconomicMap<LoopBeginNode, NodeBitMap> loopEntryStates = EconomicMap.create(Equivalence.IDENTITY);
        schedulePhase.apply(graph, false);
        final ScheduleResult schedule = graph.getLastSchedule();
        BlockIteratorClosure<NodeBitMap> closure = new BlockIteratorClosure<NodeBitMap>() {

            @Override
            protected List<NodeBitMap> processLoop(Loop<Block> loop, NodeBitMap initialState) {
                return ReentrantBlockIterator.processLoop(this, loop, initialState).exitStates;
            }

            @Override
            protected NodeBitMap processBlock(final Block block, final NodeBitMap currentState) {
                final List<Node> list = graph.getLastSchedule().getBlockToNodesMap().get(block);
                /*
                     * A stateAfter is not valid directly after its associated state split, but
                     * right before the next fixed node. Therefore a pending stateAfter is kept that
                     * will be checked at the correct position.
                     */
                FrameState pendingStateAfter = null;
                for (final Node node : list) {
                    if (node instanceof ValueNode) {
                        FrameState stateAfter = node instanceof StateSplit ? ((StateSplit) node).stateAfter() : null;
                        if (node instanceof FullInfopointNode) {
                            stateAfter = ((FullInfopointNode) node).getState();
                        }
                        if (pendingStateAfter != null && node instanceof FixedNode) {
                            pendingStateAfter.applyToNonVirtual(new NodeClosure<Node>() {

                                @Override
                                public void apply(Node usage, Node nonVirtualNode) {
                                    assert currentState.isMarked(nonVirtualNode) || nonVirtualNode instanceof VirtualObjectNode || nonVirtualNode instanceof ConstantNode : nonVirtualNode + " not available at virtualstate " + usage + " before " + node + " in block " + block + " \n" + list;
                                }
                            });
                            pendingStateAfter = null;
                        }
                        if (node instanceof AbstractMergeNode) {
                            // phis aren't scheduled, so they need to be added explicitly
                            currentState.markAll(((AbstractMergeNode) node).phis());
                            if (node instanceof LoopBeginNode) {
                                // remember the state at the loop entry, it's restored at exits
                                loopEntryStates.put((LoopBeginNode) node, currentState.copy());
                            }
                        } else if (node instanceof ProxyNode) {
                            assert false : "proxy nodes should not be in the schedule";
                        } else if (node instanceof LoopExitNode) {
                            if (graph.hasValueProxies()) {
                                for (ProxyNode proxy : ((LoopExitNode) node).proxies()) {
                                    for (Node input : proxy.inputs()) {
                                        if (input != proxy.proxyPoint()) {
                                            assert currentState.isMarked(input) : input + " not available at " + proxy + " in block " + block + "\n" + list;
                                        }
                                    }
                                }
                                // loop contents are only accessible via proxies at the exit
                                currentState.clearAll();
                                currentState.markAll(loopEntryStates.get(((LoopExitNode) node).loopBegin()));
                            }
                            // Loop proxies aren't scheduled, so they need to be added
                            // explicitly
                            currentState.markAll(((LoopExitNode) node).proxies());
                        } else {
                            for (Node input : node.inputs()) {
                                if (input != stateAfter) {
                                    if (input instanceof FrameState) {
                                        ((FrameState) input).applyToNonVirtual(new VirtualState.NodeClosure<Node>() {

                                            @Override
                                            public void apply(Node usage, Node nonVirtual) {
                                                assert currentState.isMarked(nonVirtual) : nonVirtual + " not available at " + node + " in block " + block + "\n" + list;
                                            }
                                        });
                                    } else {
                                        assert currentState.isMarked(input) || input instanceof VirtualObjectNode || input instanceof ConstantNode : input + " not available at " + node + " in block " + block + "\n" + list;
                                    }
                                }
                            }
                        }
                        if (node instanceof AbstractEndNode) {
                            AbstractMergeNode merge = ((AbstractEndNode) node).merge();
                            for (PhiNode phi : merge.phis()) {
                                ValueNode phiValue = phi.valueAt((AbstractEndNode) node);
                                assert phiValue == null || currentState.isMarked(phiValue) || phiValue instanceof ConstantNode : phiValue + " not available at phi " + phi + " / end " + node + " in block " + block;
                            }
                        }
                        if (stateAfter != null) {
                            assert pendingStateAfter == null;
                            pendingStateAfter = stateAfter;
                        }
                        currentState.mark(node);
                    }
                }
                if (pendingStateAfter != null) {
                    pendingStateAfter.applyToNonVirtual(new NodeClosure<Node>() {

                        @Override
                        public void apply(Node usage, Node nonVirtualNode) {
                            assert currentState.isMarked(nonVirtualNode) || nonVirtualNode instanceof VirtualObjectNode || nonVirtualNode instanceof ConstantNode : nonVirtualNode + " not available at virtualstate " + usage + " at end of block " + block + " \n" + list;
                        }
                    });
                }
                return currentState;
            }

            @Override
            protected NodeBitMap merge(Block merge, List<NodeBitMap> states) {
                NodeBitMap result = states.get(0);
                for (int i = 1; i < states.size(); i++) {
                    result.intersect(states.get(i));
                }
                return result;
            }

            @Override
            protected NodeBitMap getInitialState() {
                NodeBitMap ret = graph.createNodeBitMap();
                ret.markAll(graph.getNodes().filter(ConstantNode.class));
                return ret;
            }

            @Override
            protected NodeBitMap cloneState(NodeBitMap oldState) {
                return oldState.copy();
            }
        };
        ReentrantBlockIterator.apply(closure, schedule.getCFG().getStartBlock());
    } catch (Throwable t) {
        graph.getDebug().handle(t);
    }
    return true;
}
Also used : VirtualObjectNode(org.graalvm.compiler.nodes.virtual.VirtualObjectNode) SchedulePhase(org.graalvm.compiler.phases.schedule.SchedulePhase) ScheduleResult(org.graalvm.compiler.nodes.StructuredGraph.ScheduleResult) ConstantNode(org.graalvm.compiler.nodes.ConstantNode) AbstractMergeNode(org.graalvm.compiler.nodes.AbstractMergeNode) LoopBeginNode(org.graalvm.compiler.nodes.LoopBeginNode) FixedNode(org.graalvm.compiler.nodes.FixedNode) VirtualObjectNode(org.graalvm.compiler.nodes.virtual.VirtualObjectNode) AbstractEndNode(org.graalvm.compiler.nodes.AbstractEndNode) ValueNode(org.graalvm.compiler.nodes.ValueNode) LoopExitNode(org.graalvm.compiler.nodes.LoopExitNode) Node(org.graalvm.compiler.graph.Node) EndNode(org.graalvm.compiler.nodes.EndNode) FullInfopointNode(org.graalvm.compiler.nodes.FullInfopointNode) PhiNode(org.graalvm.compiler.nodes.PhiNode) ProxyNode(org.graalvm.compiler.nodes.ProxyNode) FixedNode(org.graalvm.compiler.nodes.FixedNode) FrameState(org.graalvm.compiler.nodes.FrameState) VirtualState(org.graalvm.compiler.nodes.VirtualState) LoopBeginNode(org.graalvm.compiler.nodes.LoopBeginNode) ConstantNode(org.graalvm.compiler.nodes.ConstantNode) AbstractEndNode(org.graalvm.compiler.nodes.AbstractEndNode) ArrayList(java.util.ArrayList) List(java.util.List) Loop(org.graalvm.compiler.core.common.cfg.Loop) ProxyNode(org.graalvm.compiler.nodes.ProxyNode) BlockIteratorClosure(org.graalvm.compiler.phases.graph.ReentrantBlockIterator.BlockIteratorClosure) LoopExitNode(org.graalvm.compiler.nodes.LoopExitNode) PhiNode(org.graalvm.compiler.nodes.PhiNode) NodeBitMap(org.graalvm.compiler.graph.NodeBitMap) AbstractMergeNode(org.graalvm.compiler.nodes.AbstractMergeNode) FullInfopointNode(org.graalvm.compiler.nodes.FullInfopointNode) ValueNode(org.graalvm.compiler.nodes.ValueNode) Block(org.graalvm.compiler.nodes.cfg.Block) StateSplit(org.graalvm.compiler.nodes.StateSplit)

Example 7 with SchedulePhase

use of org.graalvm.compiler.phases.schedule.SchedulePhase in project graal by oracle.

the class NodeCostUtil method computeGraphCycles.

@SuppressWarnings("try")
public static double computeGraphCycles(StructuredGraph graph, boolean fullSchedule) {
    Function<Block, Iterable<? extends Node>> blockToNodes;
    ControlFlowGraph cfg;
    if (fullSchedule) {
        SchedulePhase schedule = new SchedulePhase(SchedulePhase.SchedulingStrategy.LATEST_OUT_OF_LOOPS, true);
        schedule.apply(graph);
        cfg = graph.getLastSchedule().getCFG();
        blockToNodes = b -> graph.getLastSchedule().getBlockToNodesMap().get(b);
    } else {
        cfg = ControlFlowGraph.compute(graph, true, true, false, false);
        BlockMap<List<FixedNode>> nodes = new BlockMap<>(cfg);
        for (Block b : cfg.getBlocks()) {
            ArrayList<FixedNode> curNodes = new ArrayList<>();
            for (FixedNode node : b.getNodes()) {
                curNodes.add(node);
            }
            nodes.put(b, curNodes);
        }
        blockToNodes = b -> nodes.get(b);
    }
    double weightedCycles = 0D;
    DebugContext debug = graph.getDebug();
    try (DebugContext.Scope s = debug.scope("NodeCostSummary")) {
        for (Block block : cfg.getBlocks()) {
            for (Node n : blockToNodes.apply(block)) {
                double probWeighted = n.estimatedNodeCycles().value * block.probability();
                assert Double.isFinite(probWeighted);
                weightedCycles += probWeighted;
                if (debug.isLogEnabled()) {
                    debug.log("Node %s contributes cycles:%f size:%d to graph %s [block prob:%f]", n, n.estimatedNodeCycles().value * block.probability(), n.estimatedNodeSize().value, graph, block.probability());
                }
            }
        }
    }
    assert weightedCycles >= 0D;
    assert Double.isFinite(weightedCycles);
    return weightedCycles;
}
Also used : SchedulePhase(org.graalvm.compiler.phases.schedule.SchedulePhase) FixedNode(org.graalvm.compiler.nodes.FixedNode) Node(org.graalvm.compiler.graph.Node) ArrayList(java.util.ArrayList) FixedNode(org.graalvm.compiler.nodes.FixedNode) DebugContext(org.graalvm.compiler.debug.DebugContext) BlockMap(org.graalvm.compiler.core.common.cfg.BlockMap) ControlFlowGraph(org.graalvm.compiler.nodes.cfg.ControlFlowGraph) Block(org.graalvm.compiler.nodes.cfg.Block) ArrayList(java.util.ArrayList) List(java.util.List)

Example 8 with SchedulePhase

use of org.graalvm.compiler.phases.schedule.SchedulePhase in project graal by oracle.

the class GuardPrioritiesTest method unknownTest.

@Test
public void unknownTest() {
    assumeTrue("GuardPriorities must be turned one", GraalOptions.GuardPriorities.getValue(getInitialOptions()));
    StructuredGraph graph = prepareGraph("unknownCondition");
    new SchedulePhase(SchedulePhase.SchedulingStrategy.EARLIEST_WITH_GUARD_ORDER).apply(graph);
    for (GuardNode g1 : graph.getNodes(GuardNode.TYPE)) {
        for (GuardNode g2 : graph.getNodes(GuardNode.TYPE)) {
            if (g1.getSpeculation().isNull() ^ g2.getSpeculation().isNull()) {
                GuardNode withSpeculation = g1.getSpeculation().isNull() ? g2 : g1;
                GuardNode withoutSpeculation = g1.getSpeculation().isNull() ? g1 : g2;
                if (withoutSpeculation.isNegated() && withoutSpeculation.getCondition() instanceof IsNullNode) {
                    IsNullNode isNullNode = (IsNullNode) withoutSpeculation.getCondition();
                    if (isNullNode.getValue() instanceof ParameterNode && ((ParameterNode) isNullNode.getValue()).index() == 1) {
                        // this is the null check before the speculative guard, it's the only
                        // one that should be above
                        assertOrderedAfterLastSchedule(graph, withoutSpeculation, withSpeculation);
                        continue;
                    }
                }
                assertOrderedAfterLastSchedule(graph, withSpeculation, withoutSpeculation);
            }
        }
    }
}
Also used : SchedulePhase(org.graalvm.compiler.phases.schedule.SchedulePhase) StructuredGraph(org.graalvm.compiler.nodes.StructuredGraph) ParameterNode(org.graalvm.compiler.nodes.ParameterNode) IsNullNode(org.graalvm.compiler.nodes.calc.IsNullNode) GuardNode(org.graalvm.compiler.nodes.GuardNode) Test(org.junit.Test)

Example 9 with SchedulePhase

use of org.graalvm.compiler.phases.schedule.SchedulePhase in project graal by oracle.

the class ScheduleState method beforeInvocation.

@Override
public void beforeInvocation() {
    schedule = new SchedulePhase(selectedStrategy);
    super.beforeInvocation();
}
Also used : SchedulePhase(org.graalvm.compiler.phases.schedule.SchedulePhase)

Example 10 with SchedulePhase

use of org.graalvm.compiler.phases.schedule.SchedulePhase in project graal by oracle.

the class EffectsPhase method runAnalysis.

@SuppressWarnings("try")
public boolean runAnalysis(StructuredGraph graph, PhaseContextT context) {
    boolean changed = false;
    CompilationAlarm compilationAlarm = CompilationAlarm.current();
    DebugContext debug = graph.getDebug();
    for (int iteration = 0; iteration < maxIterations && !compilationAlarm.hasExpired(); iteration++) {
        try (DebugContext.Scope s = debug.scope(debug.areScopesEnabled() ? "iteration " + iteration : null)) {
            ScheduleResult schedule;
            ControlFlowGraph cfg;
            if (unscheduled) {
                schedule = null;
                cfg = ControlFlowGraph.compute(graph, true, true, false, false);
            } else {
                new SchedulePhase(SchedulePhase.SchedulingStrategy.EARLIEST).apply(graph, false);
                schedule = graph.getLastSchedule();
                cfg = schedule.getCFG();
            }
            try (DebugContext.Scope scheduleScope = debug.scope("EffectsPhaseWithSchedule", schedule)) {
                Closure<?> closure = createEffectsClosure(context, schedule, cfg);
                ReentrantBlockIterator.apply(closure, cfg.getStartBlock());
                if (closure.needsApplyEffects()) {
                    // apply the effects collected during this iteration
                    HashSetNodeEventListener listener = new HashSetNodeEventListener();
                    try (NodeEventScope nes = graph.trackNodeEvents(listener)) {
                        closure.applyEffects();
                    }
                    if (debug.isDumpEnabled(DebugContext.VERBOSE_LEVEL)) {
                        debug.dump(DebugContext.VERBOSE_LEVEL, graph, "%s iteration", getName());
                    }
                    new DeadCodeEliminationPhase(Required).apply(graph);
                    EconomicSet<Node> changedNodes = listener.getNodes();
                    for (Node node : graph.getNodes()) {
                        if (node instanceof Simplifiable) {
                            changedNodes.add(node);
                        }
                    }
                    postIteration(graph, context, changedNodes);
                }
                if (closure.hasChanged()) {
                    changed = true;
                } else {
                    break;
                }
            } catch (Throwable t) {
                throw debug.handle(t);
            }
        }
    }
    return changed;
}
Also used : ScheduleResult(org.graalvm.compiler.nodes.StructuredGraph.ScheduleResult) SchedulePhase(org.graalvm.compiler.phases.schedule.SchedulePhase) CompilationAlarm(org.graalvm.compiler.core.common.util.CompilationAlarm) Node(org.graalvm.compiler.graph.Node) DebugContext(org.graalvm.compiler.debug.DebugContext) HashSetNodeEventListener(org.graalvm.compiler.phases.common.util.HashSetNodeEventListener) NodeEventScope(org.graalvm.compiler.graph.Graph.NodeEventScope) ControlFlowGraph(org.graalvm.compiler.nodes.cfg.ControlFlowGraph) DeadCodeEliminationPhase(org.graalvm.compiler.phases.common.DeadCodeEliminationPhase) Simplifiable(org.graalvm.compiler.graph.spi.Simplifiable)

Aggregations

SchedulePhase (org.graalvm.compiler.phases.schedule.SchedulePhase)20 Block (org.graalvm.compiler.nodes.cfg.Block)10 Node (org.graalvm.compiler.graph.Node)9 StructuredGraph (org.graalvm.compiler.nodes.StructuredGraph)8 ScheduleResult (org.graalvm.compiler.nodes.StructuredGraph.ScheduleResult)8 DebugContext (org.graalvm.compiler.debug.DebugContext)5 ReturnNode (org.graalvm.compiler.nodes.ReturnNode)5 ArrayList (java.util.ArrayList)4 List (java.util.List)4 ConstantNode (org.graalvm.compiler.nodes.ConstantNode)4 ParameterNode (org.graalvm.compiler.nodes.ParameterNode)4 ProxyNode (org.graalvm.compiler.nodes.ProxyNode)4 ValueNode (org.graalvm.compiler.nodes.ValueNode)4 ControlFlowGraph (org.graalvm.compiler.nodes.cfg.ControlFlowGraph)4 VirtualObjectNode (org.graalvm.compiler.nodes.virtual.VirtualObjectNode)4 AbstractMergeNode (org.graalvm.compiler.nodes.AbstractMergeNode)3 FixedNode (org.graalvm.compiler.nodes.FixedNode)3 FixedWithNextNode (org.graalvm.compiler.nodes.FixedWithNextNode)3 FrameState (org.graalvm.compiler.nodes.FrameState)3 FullInfopointNode (org.graalvm.compiler.nodes.FullInfopointNode)3