diff options
| author | Cliff Click <cliffc@acm.org> | 2026-12-04 15:08:30 -0800 |
|---|---|---|
| committer | Cliff Click <cliffc@acm.org> | 2026-12-04 15:08:30 -0800 |
| commit | 05769745f8050155332597c701455be0fac59e18 (patch) | |
| tree | 868b808d95cb2ca28a3d60d6a7820005144b4945 /chapter33/src | |
| parent | 58ede6e92922602a4b4f1b46cdcb8b6082e931e7 (diff) | |
| download | Simple-ch33_try2.tar.gz Simple-ch33_try2.zip | |
Refresh singleton escapes after type closure and preserve saved calleesch33_try2
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).
Diffstat (limited to 'chapter33/src')
11 files changed, 104 insertions, 12 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) { diff --git a/chapter33/src/test/java/com/seaofnodes/simple/ClassInitDependenciesTest.java b/chapter33/src/test/java/com/seaofnodes/simple/ClassInitDependenciesTest.java index 4cacd0e6..999e518d 100644 --- a/chapter33/src/test/java/com/seaofnodes/simple/ClassInitDependenciesTest.java +++ b/chapter33/src/test/java/com/seaofnodes/simple/ClassInitDependenciesTest.java @@ -429,7 +429,11 @@ public class ClassInitDependenciesTest { if( n instanceof CLInitNode init && init.fun()==fun && init.cu()==b ) { touches[0]++; TypeTuple state=(TypeTuple)init._type; - assertSame(TypeMem.BOT,state._types[1]); + // Initialization merges whole memory; its escape sets + // can be sharper than the all-escaped MemBot bound. + TypeMem memory=(TypeMem)state._types[1]; + assertEquals(1,memory._alias); + assertSame(Type.BOTTOM,memory._t); TypeMem flag=(TypeMem)state._types[2]; assertEquals(b._clz.field("$init")._alias,flag._alias); assertSame(TypeInteger.TRUE,flag._t); diff --git a/chapter33/src/test/java/com/seaofnodes/simple/StringFormatTest.java b/chapter33/src/test/java/com/seaofnodes/simple/StringFormatTest.java index d6783380..0163de05 100644 --- a/chapter33/src/test/java/com/seaofnodes/simple/StringFormatTest.java +++ b/chapter33/src/test/java/com/seaofnodes/simple/StringFormatTest.java @@ -11,6 +11,10 @@ import org.junit.Test; import static org.junit.Assert.*; public class StringFormatTest extends TestC { + @Test public void testSingleIntegerInterpolation() throws Exception { + runSYS("val main={ -> sys.io.p(\"%(7)\"); return 0; };","singleIntegerInterpolation","7",-1); + } + // Share a few native programs, retaining a separately reported assertion // for each case. Blocks isolate locals; the delimiter preserves embedded // NULs and newlines in the expected output. diff --git a/chapter33/src/test/java/com/seaofnodes/simple/TypeTest.java b/chapter33/src/test/java/com/seaofnodes/simple/TypeTest.java index 28b6b0fb..fb1834fa 100644 --- a/chapter33/src/test/java/com/seaofnodes/simple/TypeTest.java +++ b/chapter33/src/test/java/com/seaofnodes/simple/TypeTest.java @@ -1,12 +1,47 @@ package com.seaofnodes.simple; import com.seaofnodes.simple.type.*; +import java.util.HashMap; import org.junit.Assert; import org.junit.Test; import static org.junit.Assert.assertSame; public class TypeTest { + @Test public void testSingletonMemoryUpgradeEscapes() { + TypeStruct open = TypeStruct.open("class:UpgradeChild"); + TypeMemPtr ptr = TypeMemPtr.make((byte)2,open,true); + TypeFunPtr callback = TypeFunPtr.make1((byte)2,true,TypeFunPtr.TEMPTY,TypeInteger.BOT,11); + TypeStruct child = open.add(Field.make("self",ptr,7,true)) + .add(Field.make("callback",callback,8,true)) + .add(Field.make("value",TypeInteger.BOT,9,false)).close(); + HashMap<String,Type> types = new HashMap<>(); + types.put(child._name,child); + TypeMem initial = TypeMem.make(6,ptr,true,true,true,TypeSet.FULL,TypeSet.FULL); + TypeMem upgraded = (TypeMem)initial.upgradeType(types); + assertSame(child,((TypeMemPtr)upgraded._t)._obj); + assertSame(TypeSet.of(11),upgraded._escFs); + assertSame(TypeSet.of(7).with(8).with(9),upgraded._escAs); + assertSame(upgraded,upgraded.upgradeType(types)); + + // Only a final singleton class slot can replace its old escape bound. + // Aggregate memory and mutable/non-class slots retain prior escapes. + for( TypeMem other : new TypeMem[]{ + TypeMem.make(6,ptr,true,true,false,TypeSet.FULL,TypeSet.FULL), + TypeMem.make(6,ptr,true,false,true,TypeSet.FULL,TypeSet.FULL), + TypeMem.make(6,ptr,false,true,true,TypeSet.FULL,TypeSet.FULL), + TypeMem.make(1,ptr,true,true,true,TypeSet.FULL,TypeSet.FULL)} ) { + TypeMem kept = (TypeMem)other.upgradeType(types); + assertSame(TypeSet.FULL,kept._escFs); + assertSame(TypeSet.FULL,kept._escAs); + } + TypeStruct opaque = TypeStruct.forward("ExternalUpgradeChild"); + types.put(opaque._name,opaque); + TypeMem external = TypeMem.make(6,TypeMemPtr.make((byte)2,opaque,true), + true,true,true,TypeSet.FULL,TypeSet.FULL); + assertSame(external,external.upgradeType(types)); + } + @Test public void testFinalPointerCycles() { TypeStruct cycle = TypeStruct.open("FinalPointerCycle"); Assert.assertFalse(cycle.isFinal()); |
