summaryrefslogtreecommitdiff
path: root/chapter32/src/test/java/com/seaofnodes/simple/SchedulingTest.java
blob: 28b84ee1a98dc7987788daa63459ec0d17f8c1e1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
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 ->
                i64 x=a[0];
                i64 y=flag ? a[1] : a[2];
                return x==y;
            };
            val add_noInline={ i64[] a, int flag ->
                i64 x=a[0];
                i64 y=flag ? a[1] : a[2];
                return x+y;
            };
            val main={ ->
                val a=new i64[3]; a[0]=17; a[1]=17; a[2]=23;
                if(!compare_noInline(a,1) || compare_noInline(a,0)) return 1;
                if(add_noInline(a,1)!=34 || add_noInline(a,0)!=40) return 2;
                return 0;
            };
            ""","scheduleLaterOperands","",-1);
    }

    @Test public void nativeLoadsAcrossWrites() throws Exception {
        TestC.runSYS("""
            val change_noInline={ i64[] a -> a[0]=a[1]; return a[0]; };
            val compare_noInline={ i64[] a ->
                i64 x=a[0];
                return x==change_noInline(a);
            };
            val add_noInline={ i64[] a ->
                i64 x=a[0];
                return x+change_noInline(a);
            };
            val main={ ->
                val a=new i64[2]; a[0]=17; a[1]=23;
                if(compare_noInline(a)) return 1;
                a[0]=17; a[1]=29;
                if(add_noInline(a)!=46) return 2;
                return 0;
            };
            ""","scheduleAcrossWrites","",-1);
    }
}