diff options
Diffstat (limited to 'chapter33/src/main/java/com')
8 files changed, 60 insertions, 11 deletions
diff --git a/chapter33/src/main/java/com/seaofnodes/simple/codegen/CodeGen.java b/chapter33/src/main/java/com/seaofnodes/simple/codegen/CodeGen.java index 4b0e7de4..6bded23f 100644 --- a/chapter33/src/main/java/com/seaofnodes/simple/codegen/CodeGen.java +++ b/chapter33/src/main/java/com/seaofnodes/simple/codegen/CodeGen.java @@ -362,16 +362,23 @@ public class CodeGen { return null; } - // An inline import can reveal a call to a private initializer whose + // An inline import can reveal a call to a private function whose // unused summary was deleted after SCCP. Recreate the wrapper from its // saved metadata; this is a direct call, not a function-pointer escape. public FunNode restoreClinit(CompUnit cu) { - if( cu==null || cu._obj==null ) return null; + return restoreSummary(cu,0); + } + public FunNode restoreFunction(int fidx) { + return restoreSummary(StaticData.owner(this,_fidxs.owner(fidx)),fidx); + } + private FunNode restoreSummary(CompUnit cu, int fidx) { + if( _phase.ordinal()<=Phase.Opto.ordinal() || cu==null || cu._src!=null || cu._obj==null ) return null; ElfReader elf = getElf(cu._obj); - if( elf._nodes!=null ) return null; + if( elf._nodes!=null || elf._functions==null ) return null; for( FunctionSummary summary : elf._functions ) - if( summary.name().equals(TypeStruct.addClzPrefix(cu._cname)+".<clinit>") ) { - if( _stop._inputs.find(cu._stop)==-1 ) _stop.addDef(cu._stop); + if( fidx==0 ? summary.name().equals(TypeStruct.addClzPrefix(cu._cname)+".<clinit>") + : summary.sig().fidx()==fidx ) { + if( _stop._inputs.find(cu._stop)==-1 ) add(_stop).addDef(cu._stop); return summary.install(this,cu); } return null; diff --git a/chapter33/src/main/java/com/seaofnodes/simple/codegen/FunctionSummary.java b/chapter33/src/main/java/com/seaofnodes/simple/codegen/FunctionSummary.java index 37baf926..2e9be867 100644 --- a/chapter33/src/main/java/com/seaofnodes/simple/codegen/FunctionSummary.java +++ b/chapter33/src/main/java/com/seaofnodes/simple/codegen/FunctionSummary.java @@ -22,7 +22,7 @@ record FunctionSummary(TypeFunPtr sig, TypeTuple state, String name, int size, i Node value = new ProjNode(body,2,"value").init(); ReturnNode ret = new ReturnNode(ctrl,memory,value,rpc,fun).init(); fun.setRet(ret); - cu._stop.addDef(ret); + code.add(cu._stop).addDef(ret); code.link(fun); return fun; } diff --git a/chapter33/src/main/java/com/seaofnodes/simple/codegen/Serialize.java b/chapter33/src/main/java/com/seaofnodes/simple/codegen/Serialize.java index ec135149..35c2eac0 100644 --- a/chapter33/src/main/java/com/seaofnodes/simple/codegen/Serialize.java +++ b/chapter33/src/main/java/com/seaofnodes/simple/codegen/Serialize.java @@ -52,6 +52,12 @@ public class Serialize { code._serials.put(cu,serial); if( cu._fname.equals(code._srcName==null ? "Test" : code._srcName) ) code._serial=serial; } + // Saved bodies can still call these functions even if lowering later + // inlines their last local caller. Keep native entries for that saved + // interface, using the already analyzed parameter bounds. + for( FunNode fun : code._linker ) + if( fun!=null && !fun.isDead() && fun.isFromSource() ) + Unlink.retainEntry(code,fun); } private BAOS serialize(CodeGen code, CompUnit top) { diff --git a/chapter33/src/main/java/com/seaofnodes/simple/codegen/Unlink.java b/chapter33/src/main/java/com/seaofnodes/simple/codegen/Unlink.java index bb9a6196..3b03e941 100644 --- a/chapter33/src/main/java/com/seaofnodes/simple/codegen/Unlink.java +++ b/chapter33/src/main/java/com/seaofnodes/simple/codegen/Unlink.java @@ -1,11 +1,29 @@ package com.seaofnodes.simple.codegen; import com.seaofnodes.simple.node.*; +import com.seaofnodes.simple.type.*; import com.seaofnodes.simple.util.Ary; // Discard call-graph edges after LoopTree. Machine selection keeps only this // object's bodies; ideal evaluation keeps imports so their calls can execute. abstract class Unlink { + // A code-generation entry does not publish a function pointer or widen + // any parameter. It preserves an already checked callable implementation. + static void retainEntry(CodeGen code, FunNode fun) { + StartNode start = fun._compunit._start; + if( fun.nIns()>1 && fun.in(1)==start ) return; + code.add(fun).insertDef(1,start); + for( Node use : fun._outputs ) + if( use instanceof ParmNode parm ) { + // Preserve the range, not a recursive widening iteration. + Type seed = parm._type instanceof TypeInteger ti ? ti.makeWide((byte)0) : parm._type; + code.add(parm).insertDef(1,ConstantNode.seed(seed).init()); + code.addAll(parm._outputs); + parm.moveDepsToWorklist(); + } + code.keepInCompUnit(fun); + } + // Disconnect compiler-only interprocedural edges before scheduling. Keep // signatures in the linker; runtime calls resolve FIDXs, not graph edges. static Ary<FunNode> forEval(CodeGen code, StartNode start, StopNode stop) { diff --git a/chapter33/src/main/java/com/seaofnodes/simple/node/CallNode.java b/chapter33/src/main/java/com/seaofnodes/simple/node/CallNode.java index a48f9115..c91d9a16 100644 --- a/chapter33/src/main/java/com/seaofnodes/simple/node/CallNode.java +++ b/chapter33/src/main/java/com/seaofnodes/simple/node/CallNode.java @@ -161,9 +161,11 @@ public class CallNode extends CFGNode { // Walk the bits and link for( int fidx = fidxs.next(0); fidx > 0; fidx = fidxs.next(fidx) ) { FunNode fun = CodeGen.CODE.link(fidx); + boolean restored = fun==null; + if( fun==null && !CodeGen.CODE._midAssert ) fun = CodeGen.CODE.restoreFunction(fidx); if( fun==null ) addDepForwards(CodeGen.CODE._start); if( fun!=null && !addDep(fun)._folding && !linked(fun) ) - progress = link(fun); + progress = link(fun,restored); } } } @@ -180,7 +182,8 @@ public class CallNode extends CFGNode { } // Link so this calls fun - public Node link( FunNode fun ) { + public Node link( FunNode fun ) { return link(fun,false); } + private Node link( FunNode fun, boolean restored ) { assert !linked(fun); CodeGen.CODE.add(fun).addDef(this); for( Node use : fun._outputs ) @@ -207,7 +210,7 @@ public class CallNode extends CFGNode { CodeGen code = CodeGen.CODE; if( code._phase.ordinal()>CodeGen.Phase.Opto.ordinal() && code._phase.ordinal()<CodeGen.Phase.Lower.ordinal() ) { - boolean repair = !fun.compute().isa(fun._type) || !cend().compute().isa(cend()._type); + boolean repair = restored || !fun.compute().isa(fun._type) || !cend().compute().isa(cend()._type); for( Node use : fun._outputs ) if( use instanceof ParmNode && !use.compute().isa(use._type) ) repair = true; if( repair ) Iter.repair(code); diff --git a/chapter33/src/main/java/com/seaofnodes/simple/node/FunNode.java b/chapter33/src/main/java/com/seaofnodes/simple/node/FunNode.java index 896dff05..772de7fe 100644 --- a/chapter33/src/main/java/com/seaofnodes/simple/node/FunNode.java +++ b/chapter33/src/main/java/com/seaofnodes/simple/node/FunNode.java @@ -199,7 +199,9 @@ public class FunNode extends RegionNode { // - FIDX is not in the escape set // Concrete callers keep the body alive; FinalizeIR retains surviving // functions for emission without giving them unknown callers. - if( !isRoot() && in(1) instanceof StartCUNode start ) { + // After serialization this can be a native entry for saved callers. + if( !isRoot() && in(1) instanceof StartCUNode start && + (CODE._phase==null || CODE._phase.ordinal()<CodeGen.Phase.Serialize.ordinal()) ) { boolean preOpto = CodeGen.CODE._phase == null || CodeGen.CODE._phase.ordinal() < CodeGen.Phase.Opto.ordinal(); if( !preOpto && !start.escapedFIDX(_sig.fidx()) && _ret!=null ) { diff --git a/chapter33/src/main/java/com/seaofnodes/simple/node/ParmNode.java b/chapter33/src/main/java/com/seaofnodes/simple/node/ParmNode.java index aea402f6..65807b12 100644 --- a/chapter33/src/main/java/com/seaofnodes/simple/node/ParmNode.java +++ b/chapter33/src/main/java/com/seaofnodes/simple/node/ParmNode.java @@ -87,7 +87,7 @@ public class ParmNode extends PhiNode { Type arg = in(i)._type; // The unknown-caller callback input is Start's memory // projection, interpreted using this parameter's signature. - if( i==1 && r.in(i) instanceof StartNode && _idx>1 && arg instanceof TypeMem outside ) + if( i==1 && r.in(i) instanceof StartNode && _declaredType instanceof TypeFunPtr && arg instanceof TypeMem outside ) arg = outside.outside(_declaredType); t = t.meet(arg); } diff --git a/chapter33/src/main/java/com/seaofnodes/simple/type/TypeMem.java b/chapter33/src/main/java/com/seaofnodes/simple/type/TypeMem.java index a56aee69..011cbb37 100644 --- a/chapter33/src/main/java/com/seaofnodes/simple/type/TypeMem.java +++ b/chapter33/src/main/java/com/seaofnodes/simple/type/TypeMem.java @@ -185,6 +185,19 @@ public class TypeMem extends Type { return make(_alias,_t._upgradeType(TYPES),_one,_clz,_final,_escFs,_escAs); } + @Override public TypeMem upgradeType(HashMap<String,Type> TYPES) { + TypeMem mem = (TypeMem)super.upgradeType(TYPES); + Type t = mem._t; + // A final singleton slot may have been seeded while its referent was + // still open. Recompute its escapes after closure, when recursive + // types are interned and their cached alias/FIDX summaries are valid. + // Aggregate memory and mutable slots can also hold earlier escapes. + if( mem._one && mem._clz && mem._final && mem._alias!=1 && t.isConstant() && !opaque(t) ) + return make(mem._alias,t,mem._one,mem._clz,mem._final, + escapeFIDX(TypeSet.EMPTY,t),escapeAlias(TypeSet.EMPTY,t)); + return mem; + } + @Override int hash() { return 9876543 + _alias + _t.hashCode() + (_one ? 8196 : 0) + (_clz ? 4098 : 0) + (_final ? 16392 : 0) + _escFs.hashCode() + _escAs.hashCode(); } @Override boolean eq(Type t) { |
