Skip to content

Commit

Permalink
Clean pass normal tests
Browse files Browse the repository at this point in the history
New fcn print upgrades several golden rule tests.
Move the unlink-all from the walk::err call (which IS used during IterPeeps) to GCM.
CallEnd upgrades return type based on constant call.
Fix unlink to also unlink CallEnd
Constant TFP print checks constant
Tested an untested Load
  • Loading branch information
cliffclick committed Feb 16, 2025
1 parent c46cd15 commit bb4bc9a
Show file tree
Hide file tree
Showing 10 changed files with 36 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public static void buildCFG( CodeGen code ) {

// Post-Order of CFG
private static void _rpo_cfg(CFGNode def, Node use, BitSet visit, Ary<CFGNode> rpo) {
if( def instanceof CallNode call ) call.unlink_all();
if( !(use instanceof CFGNode cfg) || visit.get(cfg._nid) )
return; // Been there, done that
if( def instanceof CallNode && cfg instanceof FunNode )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,17 @@ StringBuilder _print1(StringBuilder sb, BitSet visited) {
public Type compute() {
if( !(in(0) instanceof CallNode call) )
return TypeTuple.RET.dual();
Type ret = call.fptr().addDep(this)._type instanceof TypeFunPtr tfp ? tfp.ret() : Type.BOTTOM;
Type ret = Type.BOTTOM;
TypeMem mem = TypeMem.BOT;
if( call.fptr().addDep(this)._type instanceof TypeFunPtr tfp ) {
ret = tfp.ret();
// Here, if I can figure out I've found *all* callers, then I can meet
// across the linked returns and join with the function return type.
if( tfp.isConstant() && nIns()>1 ) {
assert nIns()==2; // Linked exactly once for a constant
ret = ((TypeTuple)in(1)._type).ret(); // Return type
}
}
return TypeTuple.make(call._type,TypeMem.BOT,ret);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ private void link( FunNode fun ) {
}

// Unlink all linked functions
void unlink_all() {
public void unlink_all() {
for( int i=0; i<_outputs._len; i++ )
if( out(i) instanceof FunNode fun ) {
assert linked(fun);
Expand All @@ -137,6 +137,7 @@ void unlink_all() {
if( use instanceof ParmNode parm )
use.delDef(idx);
fun.delDef(idx);
cend().delDef(cend()._inputs.find(fun.ret()));
assert !linked(fun);
i--;
}
Expand All @@ -156,7 +157,6 @@ public Parser.ParseException err() {
if( !arg(i+2)._type.isa(tfp.arg(i)) )
return Parser.error( "Argument #"+i+" isa "+arg(i+2)._type+", but must be a "+tfp.arg(i), _loc);

unlink_all();
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public static Node make( Type type ) {

@Override
StringBuilder _print1(StringBuilder sb, BitSet visited) {
if( _con instanceof TypeFunPtr tfp ) {
if( _con instanceof TypeFunPtr tfp && tfp.isConstant() ) {
FunNode fun = CodeGen.CODE.link(tfp);
if( fun._name != null )
return sb.append("{ ").append(fun._name).append("}");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import com.seaofnodes.simple.*;
import com.seaofnodes.simple.type.Type;
import com.seaofnodes.simple.type.TypeFunPtr;
import com.seaofnodes.simple.type.TypeTuple;

import java.util.BitSet;
import static com.seaofnodes.simple.codegen.CodeGen.CODE;

Expand Down Expand Up @@ -72,6 +74,10 @@ public Node idealize() {
return progress;
}

// Upgrade inferred or user-written return type to actual
if( _ret!=null && _ret._type instanceof TypeTuple tt && tt.ret() != _sig.ret() )
throw Utils.TODO();

// When can we assume no callers? Or no other callers (except main)?
// In a partial compilation, we assume Start gets access to any/all
// top-level public structures and recursively what they point to.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ private Node extend(Node val) {
Node shf = con(shift);
if( shf._type==TypeInteger.ZERO )
return val;
//return peep(new SarNode(null,peep(new ShlNode(null,val,shf.keep())),shf.unkeep()));
throw Utils.TODO();
Node shl = new ShlNode(null,val,shf.keep()).peephole();
return new SarNode(null,shl,shf.unkeep());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ public Type glb() {
return make(ts);
}

public Type ret() { assert _types.length==3; return _types[2]; }

@Override public String str() { return print(new SB()).toString(); }

@Override public SB print(SB sb) {
Expand Down
10 changes: 5 additions & 5 deletions chapter20/src/test/java/com/seaofnodes/simple/Chapter18Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public void testFcn1() {
return sq(arg)+sq(3);
""");
code.parse().opto().typeCheck().GCM().localSched();
assertEquals("Stop[ return (sq( 3)+sq( arg)); return (Parm_x(sq,int,3,arg)*x); ]", code._stop.toString());
assertEquals("Stop[ return (#2+#2); return (Parm_x(sq,int)*x); ]", code._stop.toString());
assertEquals("13", Eval2.eval(code, 2));
}

Expand Down Expand Up @@ -116,7 +116,7 @@ public void testFcn4() {
return fcn(3);
""");
code.parse().opto();
assertEquals("Stop[ return Phi(Region,{ int -> int #1},{ int -> int #2})( 3); return (Parm_x($fun,int,3)*x); return (Parm_x($fun,int,3)<<1); ]", code._stop.toString());
assertEquals("Stop[ return #2; return (Parm_x($fun1,int,3)*x); return (Parm_x($fun2,int,3)<<1); ]", code._stop.toString());
assertEquals("6", Eval2.eval(code, 0));
assertEquals("9", Eval2.eval(code, 1));
}
Expand All @@ -126,7 +126,7 @@ public void testFcn4() {
public void testFcn5() {
CodeGen code = new CodeGen("val fact = { int x -> x <= 1 ? 1 : x*fact(x-1); }; return fact(arg);");
code.parse().opto().typeCheck();
assertEquals("Stop[ return fact( arg); return Phi(Region,1,(Parm_x(fact,int,arg,(x-1))*fact( Sub))); ]", code._stop.toString());
assertEquals("Stop[ return #2; return Phi(Region,1,(Parm_x(fact,int,arg,(x-1))*#2)); ]", code._stop.toString());
assertEquals( "1", Eval2.eval(code, 0));
assertEquals( "1", Eval2.eval(code, 1));
assertEquals( "2", Eval2.eval(code, 2));
Expand Down Expand Up @@ -176,7 +176,7 @@ public void testFcn8() {
}
""");
code.parse().opto().typeCheck().GCM().localSched();
assertEquals("Stop[ return x( Phi(Loop,arg,x( 3))); return Parm_i(x,int,3,Phi_arg); ]", code._stop.toString());
assertEquals("Stop[ return #2; return Parm_i(x,int); ]", code._stop.toString());
assertEquals("3", Eval2.eval(code, 0));
}

Expand Down Expand Up @@ -347,7 +347,7 @@ public void testInline() {
return f2f(o)(1);
""");
code.parse().opto().typeCheck().GCM().localSched();
assertEquals("Stop[ return Phi(Region,o( arg),o( 1)); return Parm_i(o,int,arg,1); ]", code._stop.toString());
assertEquals("Stop[ return Phi(Region,#2,#2); return Parm_i(o,int); ]", code._stop.toString());
assertEquals("1", Eval2.eval(code, 2));
}

Expand Down
10 changes: 5 additions & 5 deletions chapter20/src/test/java/com/seaofnodes/simple/Chapter19Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@ public void testString() {
String !s = new String { cs = new u8[17]; };
s.cs[0] = 67; // C
s.cs[1] = 108; // l
hashCode(s);""");
hashCode(s)+hashCode(s);""");
code.parse().opto().typeCheck().GCM().localSched();
assertEquals("Stop[ return Phi(Region,123456789,Phi(Loop,0,(.[]+((Phi_hash<<5)-Phi_hash)))); return Phi(Region,1,0,0,1); ]", code._stop.toString());
assertEquals("-2449306563677080489", Eval2.eval(code, 2));
assertEquals("Stop[ return (#2+#2); return Phi(Region,1,0,0,1); return Phi(Region,._hashCode,Phi(Region,123456789,Phi(Loop,0,(.[]+((Phi_hash<<5)-Phi_hash))))); ]", code._stop.toString());
assertEquals("-4898613127354160978", Eval2.eval(code, 2));
}

@Test
Expand Down Expand Up @@ -383,7 +383,7 @@ public void testFcn1() {
return fcn(2)*10 + fcn(3);
""");
code.parse().opto().typeCheck().instSelect("x86_64_v2", "SystemV").GCM().localSched();
assertEquals("Stop[ return (add,Phi(Region,{ int -> int #1},{ int -> int #2})( 3),(muli,Phi_( 2))); return (mul,Parm_x($fun,int,3,2),x); return (shli,Parm_x($fun,int,3,2)); ]", code.print());
assertEquals("Stop[ return (add,#2,(muli,#2)); return (mul,Parm_x($fun1,int),x); return (shli,Parm_x($fun2,int)); ]", code.print());
}

@Test
Expand All @@ -394,6 +394,6 @@ public void testFcn2() {
return sq(arg) + sq(3);
""");
code.parse().opto().typeCheck().instSelect("x86_64_v2", "SystemV").GCM().localSched();
assertEquals("Stop[ return (add,sq(),sq()); return (mul,Parm_x(sq,int,3,arg),x); ]", code.print());
assertEquals("Stop[ return (add,#2,#2); return (mul,Parm_x(sq,int),x); ]", code.print());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public void testNewton() {
return sqrt(farg) + sqrt(farg+2.0);
""");
code.parse().opto().typeCheck().instSelect("x86_64_v2", "SystemV").GCM().localSched().regAlloc();
assertEquals("return (mov,Phi(Loop,(mov,(i2f8,arg)),(mulf,(addf,(mov,(divf,i2f8,mov)),mov),0.5f)));", code.print());
assertEquals("Stop[ return (addf,#2,(mov,#2)); return (mov,Phi(Loop,(mov,(mov,Parm_x(sqrt,flt))),(mulf,(addf,(divf,(mov,mov),mov),mov),0.5f))); ]", code.print());
};

@Test
Expand Down

0 comments on commit bb4bc9a

Please sign in to comment.