From 0b263f550d5e54d050f58f6b315b092944c9eff9 Mon Sep 17 00:00:00 2001 From: Cliff Click Date: Mon, 10 Aug 2026 11:11:18 -0700 Subject: Chapter 32: Compiler Internal Speedups Co-authored-by: Hsiang-Ying Fu <66062782+fuhsnn@users.noreply.github.com> Co-authored-by: fuhsnn <66062782+fuhsnn@users.noreply.github.com> Co-authored-by: Symious <14933944+symious@users.noreply.github.com> Co-authored-by: Jun Kawasaki Co-authored-by: Claude Opus 5 --- .../java/com/seaofnodes/simple/SchedulingTest.java | 39 ++++++++++++++++++++++ 1 file changed, 39 insertions(+) (limited to 'src/test/java/com/seaofnodes/simple/SchedulingTest.java') diff --git a/src/test/java/com/seaofnodes/simple/SchedulingTest.java b/src/test/java/com/seaofnodes/simple/SchedulingTest.java index 16e12b0f..28b84ee1 100644 --- a/src/test/java/com/seaofnodes/simple/SchedulingTest.java +++ b/src/test/java/com/seaofnodes/simple/SchedulingTest.java @@ -1,8 +1,47 @@ package com.seaofnodes.simple; import org.junit.Test; +import com.seaofnodes.simple.codegen.CodeGen; +import com.seaofnodes.simple.codegen.IterPeeps; +import com.seaofnodes.simple.node.*; +import com.seaofnodes.simple.type.*; +import static org.junit.Assert.*; public class SchedulingTest { + @Test public void verifierAllowsLoadAfterMemoryMerge() { + CodeGen code = loadAfterMemoryMerge(); + assertTrue(IterPeeps.schedulableUses(code)); + } + + @Test public void verifierRejectsLoadUsedBeforeMemoryMerge() { + CodeGen code = loadAfterMemoryMerge(); + ReturnNode ret = (ReturnNode)code._stop.walk(n -> n instanceof ReturnNode ? n : null); + LoadNode load = (LoadNode)ret.expr(); + // The load may move below its check, but cannot serve a use above its memory. + ret.setDef(0,load.in(0)); + try { + IterPeeps.schedulableUses(code); + fail("A load cannot execute before its memory is available"); + } catch( AssertionError error ) { + assertTrue(error.getMessage(),error.getMessage().contains("Unschedulable data node LoadNode")); + } + } + + private static CodeGen loadAfterMemoryMerge() { + CodeGen code = new CodeGen("return arg;").parse(); + ReturnNode ret = (ReturnNode)code._stop.walk(n -> n instanceof ReturnNode ? n : null); + Node ctrl = ret.ctrl(); + IfNode iff = new IfNode(ctrl,ret.expr()).init(); + Node yes = new CProjNode(iff,0,"True").init(); + Node no = new CProjNode(iff,1,"False").init(); + RegionNode region = new RegionNode(null,null,yes,no).init(); + Node phi = new BulkMemPhiNode("mem",region,ret.mem(),ret.mem()).init(); + Node mem = new MemMergeNode(null,phi).init(); + Node load = new LoadNode(null,"[]",2,ctrl,mem,code.con(TypeMemPtr.BOT),code.con(TypeInteger.ZERO)).init(); + ret.setDef(0,region); + ret.setDef(2,load); + return code; + } @Test public void nativeFoldedLoadsWithLaterOperands() throws Exception { TestC.runSYS(""" val compare_noInline={ i64[] a, int flag -> -- cgit v1.2.3