示例#1
0
    def test_still_live(self):
        p = Program(
            Element("MyFork", [Port("in", [])], [Port("out1", []), Port("out2", [])], r'''
                    output { out1(); out2() }'''),
            Element("Def", [Port("in", [])], [Port("out", [])], r'''state.a = 99; output { out(); }'''),
            Element("Join", [Port("in1", []), Port("in2", [])], [], r'''printf("%d\n", state.a);'''),
            Element("Use", [Port("in", [])], [], r'''printf("%d\n", state.a);'''),
            ElementInstance("MyFork", "fork1"),
            ElementInstance("MyFork", "fork2"),
            ElementInstance("Def", "def"),
            ElementInstance("Use", "use"),
            ElementInstance("Join", "join"),
            Connect("fork1", "def", "out1"),
            Connect("fork1", "fork2", "out2"),
            Connect("def", "join", "out", "in1"),
            Connect("fork2", "join", "out1", "in2"),
            Connect("fork2", "use", "out2"),
            State("mystate", "int a;"),
            PipelineState("fork1", "mystate"),
        )

        g = program_to_graph_pass(p)
        pipeline_state_pass(g, liveness_check=False)

        self.check_live_all(g, [("fork1", ["a"]), ("fork2", ["a"]), ("join", ["a"]), ("use", ["a"]), ("def", [])])
        self.check_uses_all(g, [("fork1", ["a"]), ("fork2", ["a"]), ("join", ["a"]), ("use", ["a"]), ("def", ["a"])])
示例#2
0
    def test_either(self):
        p = Program(
            Element("Choice", [Port("in", ["int"])], [Port("out1", []), Port("out2", [])], r'''
                    int c = in();
                    output switch { case c: out1(); else: out2(); }'''),
            Element("Def", [Port("in", [])], [Port("out", [])], r'''state.a = 99; output { out(); }'''),
            Element("Nop", [Port("in", [])], [Port("out", [])], r'''output { out(); }'''),
            Element("Use", [Port("in", [])], [], r'''printf("%d\n", state.a);'''),
            ElementInstance("Choice", "choice"),
            ElementInstance("Def", "def"),
            ElementInstance("Use", "use"),
            ElementInstance("Nop", "nop1"),
            ElementInstance("Nop", "nop2"),
            Connect("choice", "def", "out1"),
            Connect("choice", "nop1", "out2"),
            Connect("def", "nop2"),
            Connect("nop1", "nop2"),
            Connect("nop2", "use"),
            State("mystate", "int a;"),
            PipelineState("choice", "mystate"),
        )

        g = program_to_graph_pass(p)
        pipeline_state_pass(g, liveness_check=False)

        self.check_live_all(g, [("choice", ["a"]), ("nop1", ["a"]), ("nop2", ["a"]), ("use", ["a"]), ("def", [])])
        self.check_uses_all(g, [("choice", ["a"]), ("nop1", ["a"]), ("nop2", ["a"]), ("use", ["a"]), ("def", ["a"])])
示例#3
0
    def test_queue_release(self):
        n_cases = 1
        queue = graph_ir.Queue("smart_queue", entry_size=4, size=16, insts=4, channels=n_cases)
        Enq_ele = Element("smart_enq_ele", [Port("inp" + str(i), []) for i in range(n_cases)], [Port("out", [])],
                          "output { out(); }")
        Deq_ele = Element("smart_deq_ele", [Port("in_core", ["int"]), Port("in", [])],
                          [Port("out" + str(i), []) for i in range(n_cases)], "output { out0(); out1(); }")
        Enq_ele.special = queue
        Deq_ele.special = queue
        enq = ElementInstance("smart_enq_ele", "smart_enq")
        deq = ElementInstance("smart_deq_ele", "smart_deq")
        queue.enq = [enq]
        queue.deq = deq
        p = Program(
            State("mystate", "int a;"),
            Element("Save", [Port("in", ["int"])], [Port("out", [])], r'''
                    state.a = in(); output { out(); }'''),
            Enq_ele,
            Deq_ele,
            enq,
            deq,
            Element("Fork", [Port("in", [])], [Port("out0", []), Port("out1", [])], r'''output { out0(); out1(); }'''),
            Element("Choose", [Port("in", [])], [Port("out0", []), Port("out1", [])],
                    r'''output switch { case (state.a % 2 == 0): out0(); else: out1(); }'''),
            Element("Print", [Port("in", [])], [], r'''printf("%d\n", state.a);'''),
            Element("Hello", [Port("in", [])], [], r'''printf("hello\n");'''),
            ElementInstance("Save", "save"),
            ElementInstance("Fork", "myfork"),
            ElementInstance("Choose", "choose"),
            ElementInstance("Hello", "hello"),
            ElementInstance("Print", "p2"),
            ElementInstance("Print", "p3"),
            PipelineState("save", "mystate"),
            Connect("save", "smart_enq"),
            Connect("smart_enq", "smart_deq", "out", "in"),
            Connect("smart_deq", "myfork"),
            Connect("myfork", "choose", "out0"),
            Connect("myfork", "p3", "out1"),
            Connect("choose", "hello", "out0"),
            Connect("choose", "p2", "out1"),
            APIFunction("tin", ["int"], None),
            APIFunction("tout", ["int"], None),
            ResourceMap("tin", "save"),
            ResourceMap("tin", "smart_enq"),
            ResourceMap("tout", "smart_deq"),
            ResourceMap("tout", "myfork"),
            ResourceMap("tout", "choose"),
            ResourceMap("tout", "hello"),
            ResourceMap("tout", "p2"),
            ResourceMap("tout", "p3"),
        )

        g = program_to_graph_pass(p)
        g.states['mystate'].mapping = {'a': ('int', None, None, None)}
        pipeline_state_pass(g, liveness_check=False)

        deq_release = g.instances["smart_deq_release"]
        prevs = set([name for name, port in deq_release.input2ele["inp"]])
        self.assertEqual(prevs, set(["smart_deq_classify_inst", 'smart_queue_save0_inst']))
示例#4
0
    def test_complicated2(self):
        p = Program(
            Element("Choice", [Port("in", ["int"])], [Port("out1", []), Port("out2", [])], r'''
                    int c = in();
                    output switch { case c: out1(); else: out2(); }'''),
            Element("ForkB1", [Port("in", [])], [Port("out1", []), Port("out2", [])], r'''
                    state.b = 99;
                    output  { out1(); out2(); }'''),
            Element("ForkB2", [Port("in", [])], [Port("out1", []), Port("out2", [])], r'''
                    output  { out1(); out2(); }'''),
            Element("C1", [Port("in", [])], [Port("out", [])], r'''
                    state.a = 99;
                    output { out(); }'''),
            Element("C2", [Port("in", [])], [Port("out", []), Port("out1", [])], r'''
                    output { out(); out1(); }'''),
            Element("JoinUse", [Port("in1", []), Port("in2", [])], [], r'''
                    printf("%d\n", state.a + state.b);'''),
            Element("Print", [Port("in", [])], [], r'''
                    printf("%d\n", state.c);'''),
            ElementInstance("Choice", "a"),
            ElementInstance("ForkB1", "b1"),
            ElementInstance("ForkB2", "b2"),
            ElementInstance("C1", "c1"),
            ElementInstance("C2", "c2"),
            ElementInstance("JoinUse", "use"),
            ElementInstance("Print", "print"),
            Connect("a", "b1", "out1"),
            Connect("a", "b2", "out2"),
            Connect("b1", "c1", "out1"),
            Connect("b1", "c2", "out2"),
            Connect("b2", "c1", "out1"),
            Connect("b2", "c2", "out2"),
            Connect("c1", "use", "out", "in1"),
            Connect("c2", "use", "out", "in2"),
            Connect("c2", "print", "out1"),
            State("mystate", "int a; int b; int c;"),
            PipelineState("a", "mystate"),
        )

        g = program_to_graph_pass(p)
        pipeline_state_pass(g, liveness_check=False)

        self.check_live_all(g, [("a", ["b", "c"]),
                                ("b1", ["c"]), ("b2", ["b", "c"]),
                                ("c1", []), ("c2", ["c"]), # passing nodes
                                ("use", ["a", "b"]), ("print", ["c"])])
        self.check_uses_all(g, [("a", ["a", "b", "c"]),
                                ("b1", ["a", "b", "c"]), ("b2", ["a", "b", "c"]),
                                ("c1", ["a", "b"]), ("c2", ["a", "b", "c"]),
                                ("use", ["a", "b"]), ("print", ["c"])])
示例#5
0
    def test_simple_fail(self):
        p = Program(
            Element("E1", [Port("in", ["int"])], [Port("out", [])], r'''state.a = in() output { out(); }'''),
            Element("E2", [Port("in", [])], [], r'''printf("%d %d\n", state.a, state.b);'''),
            ElementInstance("E1", "e1"),
            ElementInstance("E2", "e2"),
            Connect("e1", "e2"),
            State("mystate", "int a; int b;"),
            PipelineState("e1", "mystate"),
        )

        g = program_to_graph_pass(p)
        pipeline_state_pass(g, liveness_check=False)

        self.check_live_all(g, [("e1", ["b"]), ("e2", ["a", "b"])])
        self.check_uses_all(g, [("e1", ["a", "b"]), ("e2", ["a", "b"])])
示例#6
0
    def test_queue_release2(self):
        n_cases = 1
        queue = graph_ir.Queue("smart_queue", entry_size=4, size=16, insts=4, channels=n_cases)
        Enq_ele = Element("smart_enq_ele", [Port("inp" + str(i), []) for i in range(n_cases)], [Port("out", [])],
                          "output { out(); }")
        Deq_ele = Element("smart_deq_ele", [Port("in_core", ["int"]), Port("in", [])],
                          [Port("out" + str(i), []) for i in range(n_cases)], "output { out0(); out1(); }")
        Enq_ele.special = queue
        Deq_ele.special = queue
        enq = ElementInstance("smart_enq_ele", "smart_enq")
        deq = ElementInstance("smart_deq_ele", "smart_deq")
        queue.enq = [enq]
        queue.deq = deq
        p = Program(
            State("mystate", "int a;"),
            Element("Save", [Port("in", ["int"])], [Port("out", [])], r'''
                    state.a = in(); output { out(); }'''),
            Enq_ele,
            Deq_ele,
            enq,
            deq,
            Element("Choose", [Port("in", [])], [Port("out0", []), Port("out1", [])],
                    r'''output switch { case (state.a % 2 == 0): out0(); case (state.a > 0): out1(); }'''),
            Element("Print", [Port("in", [])], [], r'''printf("%d\n", state.a);'''),
            ElementInstance("Save", "save"),
            ElementInstance("Choose", "choose"),
            ElementInstance("Print", "p0"),
            ElementInstance("Print", "p1"),
            PipelineState("save", "mystate"),
            Connect("save", "smart_enq"),
            Connect("smart_enq", "smart_deq", "out", "in"),
            Connect("smart_deq", "choose"),
            Connect("choose", "p0", "out0"),
            Connect("choose", "p1", "out1"),
            APIFunction("tin", ["int"], None),
            APIFunction("tout", ["int"], None),
            ResourceMap("tin", "save"),
            ResourceMap("tin", "smart_enq"),
            ResourceMap("tout", "smart_deq"),
            ResourceMap("tout", "choose"),
            ResourceMap("tout", "p0"),
            ResourceMap("tout", "p1"),
        )

        g = program_to_graph_pass(p)
        g.states['mystate'].mapping = {'a': ('int', None, None, None)}
        pipeline_state_pass(g, liveness_check=False)