From c5ef4e52fd7d183861b33476db999fa04f777607 Mon Sep 17 00:00:00 2001 From: Cliff Click Date: Sun, 27 Sep 2026 08:52:12 -0700 Subject: Remove -disablePeephole --- .../main/java/com/seaofnodes/simple/CodeGen.java | 7 +--- .../java/com/seaofnodes/simple/node/IfNode.java | 1 - .../main/java/com/seaofnodes/simple/node/Node.java | 20 +-------- .../java/com/seaofnodes/simple/node/PhiNode.java | 3 -- .../com/seaofnodes/simple/node/RegionNode.java | 3 +- .../java/com/seaofnodes/simple/Chapter02Test.java | 4 +- .../java/com/seaofnodes/simple/Chapter03Test.java | 7 ---- .../java/com/seaofnodes/simple/Chapter07Test.java | 12 ------ .../java/com/seaofnodes/simple/Chapter11Test.java | 14 ------- .../java/com/seaofnodes/simple/Chapter18Test.java | 18 -------- .../java/com/seaofnodes/simple/FuzzerWrap.java | 1 - .../java/com/seaofnodes/simple/SchedulerTest.java | 1 - .../java/com/seaofnodes/simple/fuzzer/Fuzzer.java | 10 ++--- .../com/seaofnodes/simple/fuzzer/FuzzerUtils.java | 10 +++-- .../seaofnodes/simple/node/PrintTestSupport.java | 48 ---------------------- 15 files changed, 19 insertions(+), 140 deletions(-) delete mode 100644 chapter18/src/test/java/com/seaofnodes/simple/node/PrintTestSupport.java (limited to 'chapter18/src') diff --git a/chapter18/src/main/java/com/seaofnodes/simple/CodeGen.java b/chapter18/src/main/java/com/seaofnodes/simple/CodeGen.java index 65d820f5..a9dfe476 100644 --- a/chapter18/src/main/java/com/seaofnodes/simple/CodeGen.java +++ b/chapter18/src/main/java/com/seaofnodes/simple/CodeGen.java @@ -45,14 +45,12 @@ public class CodeGen { CodeGen( String src, TypeInteger arg ) { _phase = null; _src = src; _arg=arg; CODE=this; } public CodeGen( String src ) { this(src,TypeInteger.BOT); } - public CodeGen parse() { return parse(false); } - public CodeGen parse(boolean disable) { + public CodeGen parse() { assert _phase == null; _phase = Phase.Parse; _linker.clear(); P = new Parser(_src,_arg); - Node._disablePeephole = disable; _stop = P.parse(); _start = Parser.START; // Capture global start if( _obs != null ) _obs.phase("Parse"); @@ -75,8 +73,7 @@ public class CodeGen { public CodeGen typeCheck() { // Demand phase Opto for cleaning up dead control flow at least, - // required for the following GCM. Note that peeps can be disabled, - // but still the dead CFG will get cleaned. + // required for the following GCM. assert _phase == Phase.Opto; _phase = Phase.TypeCheck; diff --git a/chapter18/src/main/java/com/seaofnodes/simple/node/IfNode.java b/chapter18/src/main/java/com/seaofnodes/simple/node/IfNode.java index eafcdf5b..c49ee662 100644 --- a/chapter18/src/main/java/com/seaofnodes/simple/node/IfNode.java +++ b/chapter18/src/main/java/com/seaofnodes/simple/node/IfNode.java @@ -34,7 +34,6 @@ public class IfNode extends CFGNode implements MultiNode { // If the If node is not reachable then neither is any following Proj if (ctrl()._type != Type.CONTROL && ctrl()._type != Type.BOTTOM ) return TypeTuple.IF_NEITHER; - if( _disablePeephole ) return TypeTuple.IF_BOTH; Node pred = pred(); Type t = pred._type; // High types mean NEITHER side is reachable. diff --git a/chapter18/src/main/java/com/seaofnodes/simple/node/Node.java b/chapter18/src/main/java/com/seaofnodes/simple/node/Node.java index 637f4b14..0016c711 100644 --- a/chapter18/src/main/java/com/seaofnodes/simple/node/Node.java +++ b/chapter18/src/main/java/com/seaofnodes/simple/node/Node.java @@ -284,12 +284,6 @@ public abstract class Node implements Cloneable { // ------------------------------------------------------------------------ // Graph-based optimizations - /** - * We allow disabling peephole opt so that we can observe the - * full graph, vs the optimized graph. - */ - public static boolean _disablePeephole = false; - /** * Try to peephole at this node and return a better replacement Node. * Always returns some not-null Node (often this). @@ -326,17 +320,8 @@ public abstract class Node implements Cloneable { // Compute initial or improved Type Type old = setType(compute()); - // Peepholes can be turned off - except for cleaning up dead CFG paths. - // These need to clean up so the following code motion algorithms don't - // get confused by dead or infinite paths. - if( _disablePeephole && !(this instanceof RegionNode) && !(this instanceof PhiNode) ) - return old==_type ? null : this; // Peephole optimizations turned off - - // Replace constant computations from non-constants with a constant - // node. If peeps are disabled, still allow high Phis to collapse; - // they typically come from dead Regions, and we want the Region to - // collapse, which requires the Phis to die first. - if( _type.isHighOrConst() && !isConst() && (!_disablePeephole || _type.isHigh()) ) + // Replace constant computations from non-constants with a constant node. + if( _type.isHighOrConst() && !isConst() ) return ConstantNode.make(_type).peepholeOpt(); // Global Value Numbering @@ -633,7 +618,6 @@ public abstract class Node implements Cloneable { */ public static void reset() { UNIQUE_ID = 1; - _disablePeephole=false; GVN.clear(); ITER_CNT = ITER_NOP_CNT = 0; } diff --git a/chapter18/src/main/java/com/seaofnodes/simple/node/PhiNode.java b/chapter18/src/main/java/com/seaofnodes/simple/node/PhiNode.java index b3e36d41..1f3062e8 100644 --- a/chapter18/src/main/java/com/seaofnodes/simple/node/PhiNode.java +++ b/chapter18/src/main/java/com/seaofnodes/simple/node/PhiNode.java @@ -70,9 +70,6 @@ public class PhiNode extends Node { Node live = singleUniqueInput(); if (live != null) return live; - // No more fancy peeps - if( _disablePeephole ) - return null; // No bother if region is going to fold dead paths soon for( int i=1; i { - String first = stop.p(99); - assertEquals(first,stop.p(99)); - stop.toString(); - }); - } - // A placeholder test used to rapidly rotate through fuzzer produced issues @Test diff --git a/chapter18/src/test/java/com/seaofnodes/simple/Chapter18Test.java b/chapter18/src/test/java/com/seaofnodes/simple/Chapter18Test.java index 0ea32a20..9582ecce 100644 --- a/chapter18/src/test/java/com/seaofnodes/simple/Chapter18Test.java +++ b/chapter18/src/test/java/com/seaofnodes/simple/Chapter18Test.java @@ -85,24 +85,6 @@ public class Chapter18Test { org.junit.Assert.assertSame(declared,field.get(v)); } - @Test public void testPrintingLazyMemory() throws Exception { - var code = new CodeGen("return 0;").parse(); - var base = new ConstantNode(com.seaofnodes.simple.type.TypeMem.BOT); - var outer = new ScopeNode(); - var inner = new ScopeNode(); - var loop = new LoopNode(null,code._start); - loop._type = com.seaofnodes.simple.type.Type.CONTROL; - var outerMem = new MemMergeNode(true); - outerMem.addDef(null); outerMem.addDef(base); outerMem.addDef(base); - outer.addDef(loop); outer.addDef(outerMem); - var innerMem = new MemMergeNode(true); - innerMem.addDef(null); innerMem.addDef(outer); innerMem.addDef(outer); - inner.addDef(loop); inner.addDef(innerMem); - var mem = new MemMergeNode(true); - mem.addDef(null); mem.addDef(base); mem.addDef(inner); - PrintTestSupport.unchanged(mem, () -> org.junit.Assert.assertTrue(mem.toString().contains("Lazy_"))); - } - @Test public void testJig() { diff --git a/chapter18/src/test/java/com/seaofnodes/simple/FuzzerWrap.java b/chapter18/src/test/java/com/seaofnodes/simple/FuzzerWrap.java index 1427c99d..143828e6 100644 --- a/chapter18/src/test/java/com/seaofnodes/simple/FuzzerWrap.java +++ b/chapter18/src/test/java/com/seaofnodes/simple/FuzzerWrap.java @@ -18,7 +18,6 @@ public class FuzzerWrap { }; private static final long[] OPEN_FAILING_SEEDS = { - 973358943756616234L, // Parser failure on nullable field access after dead code }; @Test public void fuzzPeepsRegression () { fuzzPeepsSeeds(REGRESSION_SEEDS); } diff --git a/chapter18/src/test/java/com/seaofnodes/simple/SchedulerTest.java b/chapter18/src/test/java/com/seaofnodes/simple/SchedulerTest.java index e869e133..d50c302a 100644 --- a/chapter18/src/test/java/com/seaofnodes/simple/SchedulerTest.java +++ b/chapter18/src/test/java/com/seaofnodes/simple/SchedulerTest.java @@ -30,7 +30,6 @@ int i=v.f; if (arg) v.f=1; return i; """); - StopNode._disablePeephole = true; code.parse();//.opto(); var eval = new Evaluator(code._stop); assertEquals(2L, eval.evaluate(1, 10)); diff --git a/chapter18/src/test/java/com/seaofnodes/simple/fuzzer/Fuzzer.java b/chapter18/src/test/java/com/seaofnodes/simple/fuzzer/Fuzzer.java index 9f2ffa4d..a5634e82 100644 --- a/chapter18/src/test/java/com/seaofnodes/simple/fuzzer/Fuzzer.java +++ b/chapter18/src/test/java/com/seaofnodes/simple/fuzzer/Fuzzer.java @@ -16,7 +16,7 @@ import java.util.function.Consumer; * randomly choose a variation the parser would parse and generate it. * These scripts are the parsed by the parser and all exceptions are caught and filtered * to only show one occurrence of one problem. This includes script which generate different - * results when the compiled graph with and without peeps is executed. + * results when graphs optimized with different worklist seeds are executed. * To aid debugging scripts that cause errors are then reduced by applying rules and checking * that the same issue persists. */ @@ -73,7 +73,7 @@ public class Fuzzer { } /** - * Run checks for script. Compile the script with peeps enabled and disabled. + * Run checks for script. Compile the script with two different optimizer worklist seeds. * Check that exceptions raised in the parser by both methods are the same and only happens if the script may be invalid. * If the script was successfully parsed check that both version behave the same. * @param script The script to test @@ -82,10 +82,10 @@ public class Fuzzer { private static void runCheck(String script, boolean valid) { CodeGen code1; try { - code1 = FuzzerUtils.parse(script, false); + code1 = FuzzerUtils.parse(script, 123); } catch (RuntimeException e1) { try { - FuzzerUtils.parse(script, true); + FuzzerUtils.parse(script, 456); } catch (RuntimeException e2) { if (FuzzerUtils.isExceptionFromSameCause(e1, e2)) { if (!valid || e1.getClass() == Parser.ParseException.class) return; @@ -95,7 +95,7 @@ public class Fuzzer { } throw e1; } - CodeGen code2 = FuzzerUtils.parse(script, true); + CodeGen code2 = FuzzerUtils.parse(script, 456); checkGraphs(code1, code2, 0); checkGraphs(code1, code2, 1); checkGraphs(code1, code2, 10); diff --git a/chapter18/src/test/java/com/seaofnodes/simple/fuzzer/FuzzerUtils.java b/chapter18/src/test/java/com/seaofnodes/simple/fuzzer/FuzzerUtils.java index 62a149a8..4509c6fc 100644 --- a/chapter18/src/test/java/com/seaofnodes/simple/fuzzer/FuzzerUtils.java +++ b/chapter18/src/test/java/com/seaofnodes/simple/fuzzer/FuzzerUtils.java @@ -51,6 +51,7 @@ class FuzzerUtils { * Used to clear as exceptions might happen in the walk and left this bitset not cleared */ private static final BitSet NodeWalkVisit; + private static final java.util.Random WorkRandom; /** * Write access to Iterate.MID_ASSERT @@ -66,6 +67,7 @@ class FuzzerUtils { static { try { NodeWalkVisit = getFieldValue(Node.class, "WVISIT"); + WorkRandom = getFieldValue(getFieldValue(IterPeeps.class, "WORK"), "_R"); set_MID_ASSERT = MethodHandles.lookup().unreflectSetter(getField( IterPeeps.class, "MID_ASSERT")); } catch (Exception e) { throw new ExceptionInInitializerError(e); @@ -112,9 +114,9 @@ class FuzzerUtils { } /** - * Parse script with peepholes enabled or disabled + * Parse and optimize using the given worklist seed */ - public static CodeGen parse(String script, boolean runPeeps) { + public static CodeGen parse(String script, long workListSeed) { var err = System.err; var out = System.out; try { @@ -126,7 +128,9 @@ class FuzzerUtils { throw rethrow(e); } var code = new CodeGen(script); - return code.parse(!runPeeps).opto().typeCheck().GCM().localSched(); + code.parse(); + WorkRandom.setSeed(workListSeed); + return code.opto().typeCheck().GCM().localSched(); } finally { NodeWalkVisit.clear(); System.setErr(err); diff --git a/chapter18/src/test/java/com/seaofnodes/simple/node/PrintTestSupport.java b/chapter18/src/test/java/com/seaofnodes/simple/node/PrintTestSupport.java deleted file mode 100644 index ceea1de5..00000000 --- a/chapter18/src/test/java/com/seaofnodes/simple/node/PrintTestSupport.java +++ /dev/null @@ -1,48 +0,0 @@ -package com.seaofnodes.simple.node; -import java.util.*; -import static org.junit.Assert.*; - -// Inspect raw fields: the regression must not itself use lazy accessors. -public class PrintTestSupport { - public static void unchanged(Node root, Runnable print) throws Exception { - var hash = Node.class.getDeclaredField("_hash"); - hash.setAccessible(true); - var nodes = new ArrayList(); - var seen = new IdentityHashMap(); - nodes.add(root); - seen.put(root,true); - for( int i=0; i(); - var outs = new IdentityHashMap(); - var hashes = new IdentityHashMap(); - var versions = new IdentityHashMap(); - var depths = new IdentityHashMap(); - for( Node n : nodes ) { - ins.put(n,n._inputs.toArray(new Node[0])); - outs.put(n,n._outputs.toArray(new Node[0])); - hashes.put(n,hash.getInt(n)); - if( n instanceof CFGNode cfg ) { - depths.put(n,(int)cfg._idepth); - versions.put(n,(int)cfg._idepthVersion); - } - } - print.run(); - for( Node n : nodes ) { - assertEquals("hash of node "+n._nid,(int)hashes.get(n),hash.getInt(n)); - if( n instanceof CFGNode cfg ) { - assertEquals("depth of node "+n._nid,(int)depths.get(n),cfg._idepth); - assertEquals("depth version of node "+n._nid,(int)versions.get(n),cfg._idepthVersion); - } - assertEquals(ins.get(n).length,n.nIns()); - assertEquals(outs.get(n).length,n.nOuts()); - for( int i=0; i