summaryrefslogtreecommitdiff
path: root/chapter32/src/test/java/com/seaofnodes/simple/EscapeFlowTest.java
diff options
context:
space:
mode:
authorCliff Click <cliffc@acm.org>2026-11-15 16:11:39 -0800
committerCliff Click <cliffc@acm.org>2026-11-15 16:11:39 -0800
commit9fe7c6f9697b5bbf3041261fdc60040e75cc674f (patch)
treed92a8ff20ed984938d90ad8917b4d67fba9fb745 /chapter32/src/test/java/com/seaofnodes/simple/EscapeFlowTest.java
parentf0a6c6757a795b8e186de0076836a87acef59fa7 (diff)
downloadSimple-ch32.tar.gz
Simple-ch32.zip
Split off chapter 31ch32
Ch 32 will be focused on compiler performance issues.
Diffstat (limited to 'chapter32/src/test/java/com/seaofnodes/simple/EscapeFlowTest.java')
-rw-r--r--chapter32/src/test/java/com/seaofnodes/simple/EscapeFlowTest.java174
1 files changed, 174 insertions, 0 deletions
diff --git a/chapter32/src/test/java/com/seaofnodes/simple/EscapeFlowTest.java b/chapter32/src/test/java/com/seaofnodes/simple/EscapeFlowTest.java
new file mode 100644
index 00000000..ee7406c6
--- /dev/null
+++ b/chapter32/src/test/java/com/seaofnodes/simple/EscapeFlowTest.java
@@ -0,0 +1,174 @@
+package com.seaofnodes.simple;
+
+import com.seaofnodes.simple.codegen.CodeGen;
+import com.seaofnodes.simple.node.*;
+import com.seaofnodes.simple.type.*;
+import org.junit.Test;
+import static org.junit.Assert.*;
+
+public class EscapeFlowTest {
+ @Test public void testArrayGeneratorDoesNotPublishUnrelatedFunctions() {
+ for( long seed : new long[]{1,123,987654321} )
+ for( String access : new String[]{"","~"} ) {
+ CodeGen code=new CodeGen("""
+ val _hidden_noInline={i64 x -> x+1;};
+ val data=new i32[%s(arg&7)]{int i -> i*i;};
+ return data#;
+ """.formatted(access),seed,true).parse();
+ int hidden=named(code,"_hidden_noInline").sig().fidx();
+ code.driver(CodeGen.Phase.TypeCheck);
+ TypeMem outside=((TypeTuple)code._start._type).mem();
+ assertFalse(XInt.bit(outside._escFs,hidden));
+ assertEquals("0",Eval2.eval(code,0));
+ assertEquals("4",Eval2.eval(code,4));
+ }
+ }
+
+ @Test public void testArrayGeneratorPublishesItsCallbacks() {
+ CodeGen code=new CodeGen("""
+ val _callback_noInline={i64 x -> x+1;};
+ val callbacks=new {i64 -> i64}[~3]{int i -> _callback_noInline;};
+ return callbacks;
+ """).parse();
+ FunNode callback=named(code,"_callback_noInline");
+ int fidx=callback.sig().fidx();
+ code.driver(CodeGen.Phase.TypeCheck);
+ assertTrue(XInt.bit(((TypeTuple)code._start._type).mem()._escFs,fidx));
+ assertFalse(callback.isDead());
+ assertTrue(callback.unknownCallers());
+ }
+
+ // A cofinite outside set must not revive private allocation helpers.
+ @Test public void testOpaqueRoundTrip() {
+ for( long seed : new long[]{1,123,987654321} ) {
+ CodeGen code = new CodeGen("""
+ struct _A { B? !b; new _A = { B? p -> b=p; }; };
+ val wrap = { B? b -> return new _A(b); };
+ return wrap;
+ """,seed,true).driver(CodeGen.Phase.TypeCheck);
+ TypeMem outside = ((TypeTuple)code._start._type).mem();
+ assertTrue(XInt.isHigh(outside._escFs));
+ assertNotSame(XInt.FULL,outside._escFs);
+ for( FunNode fun : code._linker )
+ if( fun!=null && fun._name!=null && fun._name.contains("_A.") )
+ assertFalse(fun._name,fun.unknownCallers());
+ }
+ }
+
+ // Foreign callbacks remain possible. The default callback argument reads
+ // Start memory rather than a frozen declaration constant.
+ @Test public void testUnknownCallerCallback() {
+ CodeGen code = new CodeGen("""
+ val _hidden_noInline = { int n -> int result=n+2; return result; };
+ val apply_noInline = { {int->int} f,int n -> return f(n); };
+ return apply_noInline;
+ """).parse();
+ TypeMemPtr self = (TypeMemPtr)named(code,"apply_noInline").parm(2).in(1)._type;
+ assertTrue(self._one);
+ assertEquals("class:Test",self._obj._name);
+ code.driver(CodeGen.Phase.TypeCheck);
+ FunNode apply = named(code,"apply_noInline");
+ assertTrue(apply.unknownCallers());
+ ParmNode callback = apply.parm(3);
+ assertTrue(callback.in(1) instanceof ProjNode);
+ TypeFunPtr type = (TypeFunPtr)callback._type;
+ assertTrue(XInt.isHigh(type.fidxs()));
+ assertSame(TypeInteger.I32,apply.ret().expr()._type);
+ // Only <clinit> and apply escape; forwarding #self must not expose _hidden.
+ assertEquals("Stop[ return { apply_noInline}; return #2; ]",code.print());
+ }
+
+ @Test public void testNativeCallbackRoundTrip() {
+ CodeGen code = new CodeGen("""
+ val _callback_noInline = { int n -> int result=n+1; return result; };
+ { {int->int} -> {int->int} } bounce = "C";
+ return bounce(_callback_noInline)(((arg<<32)>>32));
+ """).driver(CodeGen.Phase.TypeCheck);
+ FunNode callback = named(code,"_callback_noInline");
+ assertTrue(callback.unknownCallers());
+ assertTrue(XInt.bit(((TypeTuple)code._start._type).mem()._escFs,callback.sig().fidx()));
+ }
+
+ // Call remains CONTROL. Changing only the outside set must wake CallEnd
+ // and extend the possible callback returned by the native factory.
+ @Test public void testOutsideReturnDependency() {
+ CodeGen code = new CodeGen("return 0;").parse();
+ code._phase = CodeGen.Phase.Opto;
+ code.externFunc(100,"factory");
+ TypeFunPtr callback = TypeFunPtr.make(false,TypeFunPtr.TEMPTY,TypeInteger.BOT);
+ TypeFunPtr factory = TypeFunPtr.make1((byte)2,true,TypeFunPtr.TEMPTY,callback,100);
+ CallNode call = new CallNode(null,ConstantNode.raw(Type.CONTROL),
+ ConstantNode.raw(TypeMem.START),ConstantNode.raw(factory)).init();
+ CallEndNode end = new CallEndNode(call,TypeRPC.BOT);
+ int[] foreign = XInt.dual(XInt.make(7));
+ code._start._type = TypeTuple.make(Type.CONTROL,TypeMem.START.makeFrom(foreign,XInt.EMPTY),TypeInteger.BOT);
+ TypeTuple before = (TypeTuple)end.compute();
+ assertFalse(XInt.bit(((TypeFunPtr)before.ret()).fidxs(),7));
+ boolean depends = false;
+ for( int i = 0; i<code._start.nDeps(); i++ ) depends |= code._start.dep(i)==end;
+ assertTrue(depends);
+ code._start._type = TypeTuple.make(Type.CONTROL,TypeMem.START.makeFrom(XInt.FULL,XInt.EMPTY),TypeInteger.BOT);
+ TypeTuple after = (TypeTuple)end.compute();
+ assertTrue(before.isa(after));
+ assertTrue(XInt.bit(((TypeFunPtr)after.ret()).fidxs(),7));
+ }
+
+ // Ordinary arguments do not read Start, but a later opaque argument must
+ // subscribe both the summary and consumers of its exposed escapes.
+ @Test public void testSummaryOutsideDependency() {
+ CodeGen code = new CodeGen("return 0;").parse();
+ code._phase = CodeGen.Phase.Opto;
+ TypeTuple state = TypeTuple.make(Type.CONTROL,TypeMem.START,TypeInteger.BOT);
+ code._start._type = TypeTuple.make(Type.CONTROL,TypeMem.BOT,TypeInteger.BOT);
+ Node ctrl = ConstantNode.raw(Type.CONTROL), mem = ConstantNode.raw(TypeMem.START);
+ Node arg = ConstantNode.raw(Type.BOTTOM);
+ SummaryNode summary = new SummaryNode(state,new int[0],ctrl,mem,arg).init();
+ SummaryNode observer = new SummaryNode(state,new int[0],ctrl,mem).init();
+ summary.exposed(observer);
+ code._iter._work.clear();
+ code._start.setType(TypeTuple.make(Type.CONTROL,TypeMem.START.makeFrom(XInt.make(XInt.make(7),8),XInt.EMPTY),TypeInteger.BOT));
+ assertFalse(code._iter._work.on(summary));
+ assertFalse(code._iter._work.on(observer));
+
+ arg.setType(TypeMemPtr.makeNullable(TypeStruct.forward("OpaqueSummary")));
+ assertTrue(code._iter._work.on(summary));
+ assertTrue(code._iter._work.on(observer));
+ assertTrue(XInt.bit(((TypeTuple)summary.compute()).mem()._escFs,7));
+ assertTrue(XInt.bit(summary.exposed(observer)._escFs,7));
+ code._iter._work.clear();
+ code._start.setType(TypeTuple.make(Type.CONTROL,TypeMem.START.makeFrom(XInt.make(8),XInt.EMPTY),TypeInteger.BOT));
+ assertTrue(code._iter._work.on(summary));
+ assertTrue(code._iter._work.on(observer));
+ assertTrue(XInt.bit(((TypeTuple)summary.compute()).mem()._escFs,8));
+ assertTrue(XInt.bit(summary.exposed(observer)._escFs,8));
+ assertFalse(XInt.bit(summary.exposed(observer)._escFs,7));
+ }
+
+ // Public construction exposes the allocator; private-memory helpers
+ // remain internal and can inline into it.
+ @Test public void testPublicAllocator() {
+ CodeGen code = new CodeGen("""
+ struct Box { int n; new Box = { int x -> n=x; }; };
+ return new Box(((arg<<32)>>32)).n;
+ """).parse();
+ FunNode allocator = named(code,"Test.Box.<new>");
+ int[] helpers = XInt.EMPTY;
+ for( FunNode fun : code._linker )
+ if( fun!=null && fun.sig().ret() instanceof TypeMem )
+ helpers = XInt.make(helpers,fun.sig().fidx());
+ assertNotSame(XInt.EMPTY,helpers);
+ code.driver(CodeGen.Phase.TypeCheck);
+ TypeMem outside = ((TypeTuple)code._start._type).mem();
+ assertTrue(allocator.unknownCallers());
+ assertTrue(XInt.bit(outside._escFs,allocator.sig().fidx()));
+ for( int fidx = XInt.next(helpers,0); fidx>=0; fidx = XInt.next(helpers,fidx) )
+ assertFalse(XInt.bit(outside._escFs,fidx));
+ assertEquals("42",Eval2.eval(code,42));
+ }
+
+ private static FunNode named(CodeGen code, String name) {
+ for( FunNode fun : code._linker )
+ if( fun!=null && name.equals(fun._name) ) return fun;
+ throw new AssertionError("Missing function "+name);
+ }
+}