From 05769745f8050155332597c701455be0fac59e18 Mon Sep 17 00:00:00 2001 From: Cliff Click Date: Fri, 4 Dec 2026 15:08:30 -0800 Subject: Refresh singleton escapes after type closure and preserve saved callees Recompute final singleton-slot escapes after recursive types are interned. Preserve aggregate, mutable-slot, and opaque bounds. Sharper escapes allow unused imported helpers to die: restore their summaries when later direct calls appear, and retain native entries referenced by serialized IR even when lowering removes the last local caller. Seed those entries with analyzed ranges, and interpret only callback default memory as an outside interface. Add recursive-type and native interpolation regressions; full make -k -j8 tests passes (844 executions). --- .../java/com/seaofnodes/simple/codegen/CodeGen.java | 17 ++++++++++++----- .../com/seaofnodes/simple/codegen/FunctionSummary.java | 2 +- .../java/com/seaofnodes/simple/codegen/Serialize.java | 6 ++++++ .../java/com/seaofnodes/simple/codegen/Unlink.java | 18 ++++++++++++++++++ .../main/java/com/seaofnodes/simple/node/CallNode.java | 9 ++++++--- .../main/java/com/seaofnodes/simple/node/FunNode.java | 4 +++- .../main/java/com/seaofnodes/simple/node/ParmNode.java | 2 +- .../main/java/com/seaofnodes/simple/type/TypeMem.java | 13 +++++++++++++ 8 files changed, 60 insertions(+), 11 deletions(-) (limited to 'chapter33/src/main/java/com/seaofnodes/simple') 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)+".") ) { - if( _stop._inputs.find(cu._stop)==-1 ) _stop.addDef(cu._stop); + if( fidx==0 ? summary.name().equals(TypeStruct.addClzPrefix(cu._cname)+".") + : 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 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()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 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) { -- cgit v1.2.3