Skip to content

Commit 6e8dca1

Browse files
Merge pull request #65 from CompilerProgramming/float
Remove the dependency based IterPeeps and replace it with simple iterative version. Also bug fixes
2 parents 3d15519 + e6b71d3 commit 6e8dca1

22 files changed

Lines changed: 1515 additions & 249 deletions

File tree

optvm/src/test/java/com/compilerprogramming/ezlang/interpreter/TestInterpreter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1008,7 +1008,7 @@ func quicksort(arr: [Float], low: Int, high: Int) {
10081008
}
10091009
}
10101010
1011-
func eq(a: [Int], b: [Int], n: Int)->Int
1011+
func eq(a: [Float], b: [Float], n: Int)->Int
10121012
{
10131013
var result = 1
10141014
var i = 0

seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/Compiler.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -364,9 +364,13 @@ private ReturnNode generateFunctionBody(Symbol.FunctionTypeSymbol functionTypeSy
364364
// Parse the body
365365
Node last = compileStatement(funcDecl.block);
366366

367-
// Last expression is the return
368-
if( ctrl()._type== Type.CONTROL )
369-
fun.addReturn(ctrl(), _scope.mem().merge(), last);
367+
// Add an implicit return only for reachable fall-through. Void functions
368+
// use an integer sentinel in the backend signature.
369+
if( ctrl()._type== Type.CONTROL ) {
370+
EZType.EZTypeFunction functionType = (EZType.EZTypeFunction) functionTypeSymbol.type;
371+
Node result = functionType.returnType instanceof EZType.EZTypeVoid ? ZERO : last;
372+
fun.addReturn(ctrl(), _scope.mem().merge(), result);
373+
}
370374

371375
// Pop off the inProgress node on the multi-exit Region merge
372376
assert r.inProgress();

seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/IterPeeps.java

Lines changed: 0 additions & 201 deletions
This file was deleted.
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package com.compilerprogramming.ezlang.compiler;
2+
3+
import com.compilerprogramming.ezlang.compiler.codegen.CodeGen;
4+
import com.compilerprogramming.ezlang.compiler.node.Node;
5+
import com.compilerprogramming.ezlang.compiler.node.StopNode;
6+
7+
import java.util.ArrayList;
8+
import java.util.BitSet;
9+
import java.util.List;
10+
import java.util.function.Consumer;
11+
12+
public class IterPeeps2 {
13+
14+
static void dfs(Node root, Consumer<Node> consumer, BitSet visited) {
15+
visited.set(root._nid);
16+
/* For each successor node */
17+
for (int i = 0; i < root.nOuts(); i++) {
18+
Node S = root.out(i);
19+
if (S == null)
20+
continue;
21+
if (!visited.get(S._nid))
22+
dfs(S, consumer, visited);
23+
}
24+
consumer.accept(root);
25+
}
26+
27+
public static List<Node> rpo(Node root) {
28+
List<Node> nodes = new ArrayList<>();
29+
// note add below prepends
30+
dfs(root, (n)->nodes.add(0,n), new BitSet());
31+
return nodes;
32+
}
33+
34+
public void iterate(CodeGen code) {
35+
final Node startNode = code._start; // We need the start node to get to the full graph
36+
int iter = 1;
37+
int count = 0; // Count of nodes we peepholed
38+
for (;;iter++) {
39+
List<Node> rpos = rpo(startNode);
40+
count += rpos.size();
41+
boolean progress = false;
42+
for (int i = 0; i < rpos.size(); i++) {
43+
Node n = rpos.get(i);
44+
if (n.isDead()) continue;
45+
Node x = n.peepholeOpt();
46+
if (x != null) {
47+
progress = true;
48+
if (x.isDead()) continue;
49+
// peepholeOpt can return brand-new nodes, needing an initial type set
50+
if( x._type==null ) x.setType(x.compute());
51+
if (x != n) n.subsume(x);
52+
}
53+
if( n.isUnused() && !(n instanceof StopNode) )
54+
n.kill(); // Just plain dead
55+
}
56+
if (!progress) break;
57+
}
58+
System.out.println("Completed in " + iter + " iterations; peepholed " + count + " nodes");
59+
// if( show )
60+
// System.out.println(new GraphVisualizer().generateDotOutput(stopNode,null,null));
61+
}
62+
}

seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/codegen/CodeGen.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public CodeGen( String src, TypeInteger arg, long workListSeed ) {
5151
_stop = new StopNode(src);
5252
_src = src;
5353
_arg = arg;
54-
_iter = new IterPeeps(workListSeed);
54+
_iter = new IterPeeps2();
5555
_main = makeFun(TypeTuple.MAIN, Type.BOTTOM);
5656
P = new Compiler(this,arg);
5757
}
@@ -180,7 +180,7 @@ public CodeGen parse() {
180180

181181
// ---------------------------
182182
// Iterator peepholes.
183-
public final IterPeeps _iter;
183+
public final IterPeeps2 _iter;
184184
// Stop tracking deps while assertin
185185
public boolean _midAssert;
186186
// Statistics on peepholes
@@ -206,8 +206,6 @@ public CodeGen opto() {
206206
// loop unroll, peel, RCE, etc
207207
return this;
208208
}
209-
public <N extends Node> N add( N n ) { return _iter.add(n); }
210-
public void addAll( Ary<Node> ary ) { _iter.addAll(ary); }
211209

212210

213211
// ---------------------------

seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/codegen/GlobalCodeMotion.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.compilerprogramming.ezlang.compiler.codegen;
22

33
import com.compilerprogramming.ezlang.compiler.util.Ary;
4-
import com.compilerprogramming.ezlang.compiler.IterPeeps.WorkList;
4+
import com.compilerprogramming.ezlang.compiler.util.WorkList;
55
import com.compilerprogramming.ezlang.compiler.util.Utils;
66
import com.compilerprogramming.ezlang.compiler.node.*;
77
import com.compilerprogramming.ezlang.compiler.type.*;

seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/codegen/RegAlloc.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,7 @@ boolean splitByLoop( byte round, LRG lrg ) {
397397
// If the minLoopDepth is less than the maxLoopDepth: for-all defs and
398398
// uses, if at minLoopDepth or lower, split after def and before use.
399399
for( Node n : _ns ) {
400-
if( n instanceof SplitNode && min!=max ) continue; // Ignoring splits; since spilling need to split in a deeper loop
400+
if( n instanceof SplitNode ) continue; // Ignoring splits; since spilling need to split in a deeper loop
401401
if( n.isDead() ) continue; // Some Cloneable went dead by other spill changes
402402
// If this is a 2-address commutable op (e.g. AddX86, MulX86) and the rhs has only a single user,
403403
// commute the inputs... which chops the LHS live ranges' upper bound to just the RHS.

seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/node/CallEndNode.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,6 @@ public Node idealize() {
8989
fun.setDef(1, Compiler.XCTRL); // No default/unknown StartNode caller
9090
fun.setDef(2,call.ctrl()); // Bypass the Call;
9191
fun.ret().setDef(3,null); // Return is folding also
92-
CodeGen.CODE.addAll(fun._outputs);
93-
// Repeat defs 1 layer down, for users of Parm (Phis)
94-
for( Node parm : fun._outputs )
95-
if( parm instanceof ParmNode )
96-
CodeGen.CODE.addAll(parm._outputs);
9792

9893
// Inlining immediately blows all cache idepth fields past the inline point.
9994
// Bump the global version number invalidating them en-masse.

seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/node/CallNode.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ private Node link( FunNode fun ) {
129129
if( use instanceof ParmNode parm )
130130
parm.addDef(parm._idx==0 ? new ConstantNode(cend()._rpc).peephole() : arg(parm._idx));
131131
// Call end points to function return
132-
CodeGen.CODE.add(cend()).addDef(fun.ret());
132+
cend().addDef(fun.ret());
133133
return this;
134134
}
135135

seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/node/FunNode.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ public ParmNode rpc() {
6565
public void setSig( TypeFunPtr sig ) {
6666
assert sig.isa(_sig);
6767
if( _sig != sig ) {
68-
CODE.add(this);
6968
_sig = sig;
7069
}
7170
}
@@ -89,8 +88,6 @@ public Node idealize() {
8988
// Some linked path dies
9089
Node progress = deadPath();
9190
if( progress!=null ) {
92-
if( nIns()==3 && in(2) instanceof CallNode call )
93-
CODE.add(call.cend()); // If Start and one call, check for inline
9491
return progress;
9592
}
9693

@@ -114,8 +111,6 @@ public Node idealize() {
114111

115112
// If down to a single input, become that input
116113
if( nIns()==2 && !hasPhi() ) {
117-
CODE.add( CODE._stop ); // Stop will remove dead path
118-
CODE.add( _ret ); // Return will compute to TOP control
119114
return in(1); // Collapse if no Phis; 1-input Phis will collapse on their own
120115
}
121116

0 commit comments

Comments
 (0)