Skip to content

Commit

Permalink
[NTI] Build the workset iteratively instead of recursively to avoid b…
Browse files Browse the repository at this point in the history
…lowing the stack in very large programs.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=160458087
  • Loading branch information
dimvar authored and brad4d committed Jun 29, 2017
1 parent e03439b commit e625c6a
Show file tree
Hide file tree
Showing 3 changed files with 226 additions and 124 deletions.
190 changes: 190 additions & 0 deletions src/com/google/javascript/jscomp/NTIWorkset.java
@@ -0,0 +1,190 @@
/*
* Copyright 2017 The Closure Compiler Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.javascript.jscomp;

import com.google.common.base.Preconditions;
import com.google.javascript.jscomp.ControlFlowGraph.Branch;
import com.google.javascript.jscomp.graph.DiGraph.DiGraphEdge;
import com.google.javascript.jscomp.graph.DiGraph.DiGraphNode;
import com.google.javascript.rhino.Node;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

/**
* Represents the workset used by the flow-sensitive analysis in NTI.
* The workset is computed happens iteratively, otherwise large programs can cause stack overflow.
*/
public class NTIWorkset {
private final ControlFlowGraph<Node> cfg;
// What this class computes. Represents the workset used by the flow-sensitive analysis in NTI.
private final List<DiGraphNode<Node, ControlFlowGraph.Branch>> ntiWorkset;
// The algorithm that computes the NTI workset itself uses a workset.
private Deque<DiGraphNode<Node, ControlFlowGraph.Branch>> workset;
// If a node is in this set, don't revisit it.
private Set<DiGraphNode<Node, ControlFlowGraph.Branch>> seen;

NTIWorkset(ControlFlowGraph<Node> cfg) {
this.cfg = cfg;
this.ntiWorkset = new ArrayList<>();
this.workset = new ArrayDeque<>();
this.seen = new LinkedHashSet<>();
buildWorkset();
}

private void buildWorkset() {
Preconditions.checkState(ntiWorkset.isEmpty());
workset.push(cfg.getEntry());
while (!workset.isEmpty()) {
processGraphNode();
}
workset = null;
seen = null;
}

Iterable<DiGraphNode<Node, ControlFlowGraph.Branch>> forward() {
Preconditions.checkState(!ntiWorkset.isEmpty());
return ntiWorkset;
}

/**
* The backwards analysis in NTI traverses the workset in the reverse direction.
*/
private class BackwardIterator implements Iterator<DiGraphNode<Node, ControlFlowGraph.Branch>> {
int i = ntiWorkset.size() - 1;

@Override
public boolean hasNext() {
return i >= 0;
}

@Override
public DiGraphNode<Node, Branch> next() {
return ntiWorkset.get(i--);
}

@Override
public void remove() {
throw new UnsupportedOperationException();
}
}

Iterable<DiGraphNode<Node, ControlFlowGraph.Branch>> backward() {
Preconditions.checkState(!ntiWorkset.isEmpty());
return new Iterable<DiGraphNode<Node, ControlFlowGraph.Branch>>() {
@Override
public Iterator<DiGraphNode<Node, Branch>> iterator() {
return new BackwardIterator();
}
};
}

private void processGraphNode() {
DiGraphNode<Node, ControlFlowGraph.Branch> dn = workset.pop();
if (seen.contains(dn) || dn == cfg.getImplicitReturn()) {
return;
}
switch (dn.getValue().getToken()) {
case DO:
case WHILE:
case FOR:
case FOR_IN:
case FOR_OF: {
List<DiGraphEdge<Node, ControlFlowGraph.Branch>> outEdges = dn.getOutEdges();
// The workset is a stack. If we want to analyze nodeA after nodeB, we need to push nodeA
// before nodeB. For this reason, we push the code after a loop before the loop body.
for (DiGraphEdge<Node, ControlFlowGraph.Branch> outEdge : outEdges) {
if (outEdge.getValue() == ControlFlowGraph.Branch.ON_FALSE) {
workset.push(outEdge.getDestination());
}
}
for (DiGraphEdge<Node, ControlFlowGraph.Branch> outEdge : outEdges) {
if (outEdge.getValue() == ControlFlowGraph.Branch.ON_TRUE) {
workset.push(outEdge.getDestination());
}
}
// The loop condition must be analyzed first, so it's pushed last.
seen.add(dn);
ntiWorkset.add(dn);
return;
}
default: {
for (DiGraphEdge<Node, ControlFlowGraph.Branch> inEdge : dn.getInEdges()) {
DiGraphNode<Node, ControlFlowGraph.Branch> source = inEdge.getSource();
Node sourceNode = source.getValue();
// Wait for all other incoming edges at join nodes.
if (!seen.contains(inEdge.getSource()) && !sourceNode.isDo()) {
return;
}
// The loop header has already been added, and will be analyzed before the loop body.
// Here, we want to add it again, so that we analyze the header after the loop body,
// and before the code following the loop.
if (NodeUtil.isLoopStructure(sourceNode) && !sourceNode.isDo()
&& inEdge.getValue() == ControlFlowGraph.Branch.ON_FALSE) {
ntiWorkset.add(source);
}
}
seen.add(dn);
if (cfg.getEntry() != dn) {
ntiWorkset.add(dn);
}
Node n = dn.getValue();
List<DiGraphNode<Node, ControlFlowGraph.Branch>> succs = cfg.getDirectedSuccNodes(dn);
// Currently, the ELSE branch of an IF is analyzed before the THEN branch.
// To do it the other way around, the ELSE branch has to be pushed to the workset
// *before* the THEN branch, so we need to reverse succs. But the order doesn't impact
// correctness, so we don't do the reversal.
for (DiGraphNode<Node, ControlFlowGraph.Branch> succ : succs) {
workset.push(succ);
if (succ == cfg.getImplicitReturn()) {
if (n.getNext() != null) {
processDeadNode(n.getNext());
}
}
}
if (n.isTry()) {
processDeadNode(n.getSecondChild());
} else if (n.isBreak() || n.isContinue() || n.isThrow()) {
processDeadNode(n.getNext());
}
}
}
}

/**
* Analyze dead code, such as a catch that is never executed or a statement following a
* return/break/continue. This code can be a predecessor of live code in the cfg. We wait
* on incoming edges before adding nodes to the workset, and don't want dead code to block
* live code from being analyzed.
*/
private void processDeadNode(Node maybeDeadNode) {
if (maybeDeadNode == null) {
return;
}
DiGraphNode<Node, ControlFlowGraph.Branch> cfgNode = cfg.getDirectedGraphNode(maybeDeadNode);
if (cfgNode == null) {
return;
}
if (cfg.getDirectedPredNodes(cfgNode).isEmpty()) {
workset.push(cfgNode);
}
}
}
129 changes: 6 additions & 123 deletions src/com/google/javascript/jscomp/NewTypeInference.java
Expand Up @@ -49,10 +49,8 @@
import com.google.javascript.rhino.TypeI; import com.google.javascript.rhino.TypeI;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.LinkedHashSet; import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Objects; import java.util.Objects;
Expand Down Expand Up @@ -747,115 +745,6 @@ private JSType getSummaryOfLocalFunDef(String name) {
return changeTypeIfFunctionNamespace(fnScope, fnType); return changeTypeIfFunctionNamespace(fnScope, fnType);
} }


private void buildWorkset(
DiGraphNode<Node, ControlFlowGraph.Branch> dn,
List<DiGraphNode<Node, ControlFlowGraph.Branch>> workset) {
buildWorksetHelper(dn, workset,
new LinkedHashSet<DiGraphNode<Node, ControlFlowGraph.Branch>>());
}

private void buildWorksetHelper(
DiGraphNode<Node, ControlFlowGraph.Branch> dn,
List<DiGraphNode<Node, ControlFlowGraph.Branch>> workset,
Set<DiGraphNode<Node, ControlFlowGraph.Branch>> seen) {
if (seen.contains(dn) || dn == this.cfg.getImplicitReturn()) {
return;
}
switch (dn.getValue().getToken()) {
case DO:
case WHILE:
case FOR:
case FOR_IN:
case FOR_OF:
// Do the loop body first, then the loop follow.
// For DO loops, we do BODY-CONDT-CONDF-FOLLOW
// Since CONDT is currently unused, this could be optimized.
List<DiGraphEdge<Node, ControlFlowGraph.Branch>> outEdges = dn.getOutEdges();
seen.add(dn);
workset.add(dn);
for (DiGraphEdge<Node, ControlFlowGraph.Branch> outEdge : outEdges) {
if (outEdge.getValue() == ControlFlowGraph.Branch.ON_TRUE) {
buildWorksetHelper(outEdge.getDestination(), workset, seen);
}
}
workset.add(dn);
for (DiGraphEdge<Node, ControlFlowGraph.Branch> outEdge : outEdges) {
if (outEdge.getValue() == ControlFlowGraph.Branch.ON_FALSE) {
buildWorksetHelper(outEdge.getDestination(), workset, seen);
}
}
break;
default: {
// Wait for all other incoming edges at join nodes.
for (DiGraphEdge<Node, ControlFlowGraph.Branch> inEdge :
dn.getInEdges()) {
if (!seen.contains(inEdge.getSource())
&& !inEdge.getSource().getValue().isDo()) {
return;
}
}
seen.add(dn);
if (this.cfg.getEntry() != dn) {
workset.add(dn);
}
// Don't recur for straight-line code
while (true) {
Node n = dn.getValue();
if (n.isTry()) {
maybeAddDeadCode(workset, seen, n.getSecondChild());
} else if (n.isBreak() || n.isContinue() || n.isThrow()) {
maybeAddDeadCode(workset, seen, n.getNext());
}
List<DiGraphNode<Node, ControlFlowGraph.Branch>> succs =
this.cfg.getDirectedSuccNodes(dn);
if (succs.size() != 1) {
break;
}
DiGraphNode<Node, ControlFlowGraph.Branch> succ = succs.get(0);
if (succ == this.cfg.getImplicitReturn()) {
if (n.getNext() != null) {
maybeAddDeadCode(workset, seen, n.getNext());
}
return;
}
// Make sure that succ isn't a join node
if (this.cfg.getDirectedPredNodes(succ).size() > 1) {
break;
}
workset.add(succ);
seen.add(succ);
dn = succ;
}
for (DiGraphNode<Node, ControlFlowGraph.Branch> succ :
this.cfg.getDirectedSuccNodes(dn)) {
buildWorksetHelper(succ, workset, seen);
}
break;
}
}
}

// Analyze dead code, such as a catch that is never executed or a statement
// following a return/break/continue. This code can be a predecessor of live
// code in the cfg. We wait on incoming edges before adding nodes to the
// workset, and don't want dead code to block live code from being analyzed.
private void maybeAddDeadCode(
List<DiGraphNode<Node, ControlFlowGraph.Branch>> workset,
Set<DiGraphNode<Node, ControlFlowGraph.Branch>> seen,
Node maybeDeadNode) {
if (maybeDeadNode == null) {
return;
}
DiGraphNode<Node, ControlFlowGraph.Branch> cfgNode =
this.cfg.getDirectedGraphNode(maybeDeadNode);
if (cfgNode == null) {
return;
}
if (this.cfg.getDirectedPredNodes(cfgNode).isEmpty()) {
buildWorksetHelper(cfgNode, workset, seen);
}
}

private void analyzeFunction(NTIScope scope) { private void analyzeFunction(NTIScope scope) {
println("=== Analyzing function: ", scope.getReadableName(), " ==="); println("=== Analyzing function: ", scope.getReadableName(), " ===");
currentScope = scope; currentScope = scope;
Expand All @@ -865,13 +754,10 @@ private void analyzeFunction(NTIScope scope) {
println(this.cfg); println(this.cfg);
// The size is > 1 when multiple files are compiled // The size is > 1 when multiple files are compiled
// Preconditions.checkState(cfg.getEntry().getOutEdges().size() == 1); // Preconditions.checkState(cfg.getEntry().getOutEdges().size() == 1);
List<DiGraphNode<Node, ControlFlowGraph.Branch>> workset = NTIWorkset workset = new NTIWorkset(this.cfg);
new LinkedList<>();
buildWorkset(this.cfg.getEntry(), workset);
/* println("Workset: ", workset); */ /* println("Workset: ", workset); */
this.typeEnvFromDeclaredTypes = getTypeEnvFromDeclaredTypes(); this.typeEnvFromDeclaredTypes = getTypeEnvFromDeclaredTypes();
if (scope.isFunction() && scope.hasUndeclaredFormalsOrOuters()) { if (scope.isFunction() && scope.hasUndeclaredFormalsOrOuters()) {
Collections.reverse(workset);
// Ideally, we would like to only set the in-edges of the implicit return // Ideally, we would like to only set the in-edges of the implicit return
// rather than all edges. However, we cannot do that because of a bug in // rather than all edges. However, we cannot do that because of a bug in
// workset construction. (The test testBadWorksetConstruction would fail.) // workset construction. (The test testBadWorksetConstruction would fail.)
Expand All @@ -884,7 +770,6 @@ private void analyzeFunction(NTIScope scope) {
envs.put(e, this.typeEnvFromDeclaredTypes); envs.put(e, this.typeEnvFromDeclaredTypes);
} }
analyzeFunctionBwd(workset); analyzeFunctionBwd(workset);
Collections.reverse(workset);
// TODO(dimvar): Revisit what we throw away after the bwd analysis // TODO(dimvar): Revisit what we throw away after the bwd analysis
TypeEnv entryEnv = getEntryTypeEnv(); TypeEnv entryEnv = getEntryTypeEnv();
initEdgeEnvsFwd(entryEnv); initEdgeEnvsFwd(entryEnv);
Expand All @@ -905,9 +790,8 @@ private void analyzeFunction(NTIScope scope) {
} }
} }


private void analyzeFunctionBwd( private void analyzeFunctionBwd(NTIWorkset workset) {
List<DiGraphNode<Node, ControlFlowGraph.Branch>> workset) { for (DiGraphNode<Node, ControlFlowGraph.Branch> dn : workset.backward()) {
for (DiGraphNode<Node, ControlFlowGraph.Branch> dn : workset) {
Node n = dn.getValue(); Node n = dn.getValue();
TypeEnv outEnv = checkNotNull(getOutEnv(dn)); TypeEnv outEnv = checkNotNull(getOutEnv(dn));
TypeEnv inEnv; TypeEnv inEnv;
Expand Down Expand Up @@ -999,9 +883,8 @@ private void analyzeFunctionBwd(
} }
} }


private void analyzeFunctionFwd( private void analyzeFunctionFwd(NTIWorkset workset) {
List<DiGraphNode<Node, ControlFlowGraph.Branch>> workset) { for (DiGraphNode<Node, ControlFlowGraph.Branch> dn : workset.forward()) {
for (DiGraphNode<Node, ControlFlowGraph.Branch> dn : workset) {
Node n = dn.getValue(); Node n = dn.getValue();
Node parent = n.getParent(); Node parent = n.getParent();
checkState(n != null, "Implicit return should not be in workset."); checkState(n != null, "Implicit return should not be in workset.");
Expand Down Expand Up @@ -4213,7 +4096,7 @@ static EnvTypePair join(EnvTypePair p1, EnvTypePair p2) {
} }


private static JSType envGetType(TypeEnv env, String pname) { private static JSType envGetType(TypeEnv env, String pname) {
checkArgument(!pname.contains(".")); checkArgument(!pname.contains("."), pname);
return env.getType(pname); return env.getType(pname);
} }


Expand Down

0 comments on commit e625c6a

Please sign in to comment.