Пример #1
0
    def test_sccp_dead_loop(self):
        simple = textwrap.dedent("""
        #include <pykit_ir.h>

        Int32 f(Int32 i) {
            Int32 x, y, z;

            x = 2;
            y = 3;
            z = 4;

            while (y < x) {
                if (y < x) {
                    x = 1;
                    x = x + 1;
                    y = x - z;
                }
            }

            return x + z;
        }
        """)
        mod = from_c(simple)
        f = mod.get_function("f")
        cfa.run(f)
        sccp.run(f)
        verify(f)

        verify(f)
        ops = list(f.ops)
        assert len(list(f.ops)) == 1
        [op] = ops
        assert op.opcode == 'ret'
        assert isinstance(op.args[0], Const)
        self.assertEqual(op.args[0].const, 6)
Пример #2
0
    def test_sccp(self):
        simple = textwrap.dedent("""
        #include <pykit_ir.h>

        Int32 f(Int32 i) {
            Int32 x, y, z;

            x = 2;
            y = 3;
            z = 4;

            if (x < y)
                x = y;
            else
                x = i;

            return x + z;
        }
        """)
        mod = from_c(simple)
        f = mod.get_function("f")
        cfa.run(f)
        sccp.run(f)
        verify(f)

        ops = list(f.ops)
        assert len(list(f.ops)) == 1
        [op] = ops
        assert op.opcode == 'ret'
        assert isinstance(op.args[0], Const)
        self.assertEqual(op.args[0].const, 7)
Пример #3
0
    def test_sccp_endless_loop(self):
        simple = textwrap.dedent("""
        #include <pykit_ir.h>

        Int32 f(Int32 i) {
            Int32 x, y, z;

            x = 2;
            y = 3;
            z = 4;

            while (x < y) {
                if (x < y) {
                    x = 2;
                }
            }

            return x + z;
        }
        """)
        mod = from_c(simple)
        f = mod.get_function("f")
        cfa.run(f)
        sccp.run(f)
        verify(f)

        assert len(f.blocks) == 2
        start, loop = f.blocks
        assert start.terminator.opcode == 'jump'
        assert start.terminator.args[0] == loop
        assert loop.terminator.opcode == 'jump'
        assert loop.terminator.args[0] == loop
Пример #4
0
 def test_ssa2(self):
     mod = from_c(source)
     f = mod.get_function('func')
     cfa.run(f)
     verify(f)
     codes = opcodes(f)
     self.assertEqual(codes.count('phi'), 3)
Пример #5
0
 def test_ssa2(self):
     mod = from_c(source)
     f = mod.get_function('func')
     cfa.run(f)
     verify(f)
     codes = opcodes(f)
     self.assertEqual(codes.count('phi'), 3)
Пример #6
0
    def test_inline2(self):
        harder = textwrap.dedent("""
        int callee(int i) {
            (void) print(i);
            while (i < 10) {
                i = i + 1;
                return i * 2;
            }
            return i;
        }

        int caller() {
            int x = 4;
            while (x < 10) {
                (void) print(x);
                x = call(callee, list(x));
            }
            return x;
        }
        """)
        mod = from_c(harder)
        func = mod.get_function("caller")
        verify(func)
        result = interp.run(func)

        [callsite] = findallops(func, 'call')
        inline.inline(func, callsite)
        cfa.run(func)
        verify(func)
Пример #7
0
    def test_inline2(self):
        harder = textwrap.dedent("""
        int callee(int i) {
            (void) print(i);
            while (i < 10) {
                i = i + 1;
                return i * 2;
            }
            return i;
        }

        int caller() {
            int x = 4;
            while (x < 10) {
                (void) print(x);
                x = call(callee, list(x));
            }
            return x;
        }
        """)
        mod = from_c(harder)
        func = mod.get_function("caller")
        verify(func)
        result = interp.run(func)

        [callsite] = findallops(func, 'call')
        inline.inline(func, callsite)
        cfa.run(func)
        verify(func)

        # TODO: update phi when splitting blocks
        result2 = interp.run(func)
        assert result == result2
Пример #8
0
    def test_swap(self):
        simple = textwrap.dedent("""
        #include <pykit_ir.h>

        int f(int i) {
            int x = 1;
            int y = 2;
            int tmp;
            while (i > 0) {
                tmp = x;
                x = y;
                y = tmp;
                i = i - 1;
            }
            return x;
        }
        """)
        mod = from_c(simple)
        func = mod.get_function("f")
        cfa.run(func)

        verify(func)
        ssa_result1 = interp.run(func, args=[1])
        ssa_result2 = interp.run(func, args=[2])

        reg2mem.reg2mem(func)
        verify(func)

        stack_result1 = interp.run(func, args=[1])
        stack_result2 = interp.run(func, args=[2])

        self.assertEqual(ssa_result1, stack_result1)
        self.assertEqual(ssa_result2, stack_result2)
Пример #9
0
    def test_swap(self):
        simple = textwrap.dedent("""
        #include <pykit_ir.h>

        int f(int i) {
            int x = 1;
            int y = 2;
            int tmp;
            while (i > 0) {
                tmp = x;
                x = y;
                y = tmp;
                i = i - 1;
            }
            return x;
        }
        """)
        mod = from_c(simple)
        func = mod.get_function("f")
        cfa.run(func)

        verify(func)
        ssa_result1 = interp.run(func, args=[1])
        ssa_result2 = interp.run(func, args=[2])

        reg2mem.reg2mem(func)
        verify(func)

        stack_result1 = interp.run(func, args=[1])
        stack_result2 = interp.run(func, args=[2])

        self.assertEqual(ssa_result1, stack_result1)
        self.assertEqual(ssa_result2, stack_result2)
Пример #10
0
    def test_sccp_dead_loop(self):
        simple = textwrap.dedent("""
        #include <pykit_ir.h>

        Int32 f(Int32 i) {
            Int32 x, y, z;

            x = 2;
            y = 3;
            z = 4;

            while (y < x) {
                if (y < x) {
                    x = 1;
                    x = x + 1;
                    y = x - z;
                }
            }

            return x + z;
        }
        """)
        mod = from_c(simple)
        f = mod.get_function("f")
        cfa.run(f)
        sccp.run(f)
        verify(f)

        verify(f)
        ops = list(f.ops)
        assert len(list(f.ops)) == 1
        [op] = ops
        assert op.opcode == 'ret'
        assert isinstance(op.args[0], Const)
        self.assertEqual(op.args[0].const, 6)
Пример #11
0
    def test_sccp_endless_loop(self):
        simple = textwrap.dedent("""
        #include <pykit_ir.h>

        Int32 f(Int32 i) {
            Int32 x, y, z;

            x = 2;
            y = 3;
            z = 4;

            while (x < y) {
                if (x < y) {
                    x = 2;
                }
            }

            return x + z;
        }
        """)
        mod = from_c(simple)
        f = mod.get_function("f")
        cfa.run(f)
        sccp.run(f)
        verify(f)

        assert len(f.blocks) == 2
        start, loop = f.blocks
        assert start.terminator.opcode == 'jump'
        assert start.terminator.args[0] == loop
        assert loop.terminator.opcode == 'jump'
        assert loop.terminator.args[0] == loop
Пример #12
0
    def test_sccp(self):
        simple = textwrap.dedent("""
        #include <pykit_ir.h>

        Int32 f(Int32 i) {
            Int32 x, y, z;

            x = 2;
            y = 3;
            z = 4;

            if (x < y)
                x = y;
            else
                x = i;

            return x + z;
        }
        """)
        mod = from_c(simple)
        f = mod.get_function("f")
        cfa.run(f)
        sccp.run(f)
        verify(f)

        ops = list(f.ops)
        assert len(list(f.ops)) == 1
        [op] = ops
        assert op.opcode == 'ret'
        assert isinstance(op.args[0], Const)
        self.assertEqual(op.args[0].const, 7)
Пример #13
0
    def test_inline2(self):
        harder = """
        int callee(int i) {
            (void) print(i);
            while (i < 10) {
                i = i + 1;
                return i * 2;
            }
            return i;
        }

        int caller() {
            int x = 4;
            while (x < 10) {
                (void) print(x);
                x = call(callee, list(x));
            }
            return x;
        }
        """
        mod = from_c(harder)
        func = mod.get_function("caller")
        verify(func)
        result = interp.run(func)

        [callsite] = findallops(func, 'call')
        inline.inline(func, callsite)
        cfa.run(func)
        verify(func)
Пример #14
0
    def test_cfg(self):
        mod = from_c(source)
        f = mod.get_function('func_simple')
        verify(f)
        flow = cfa.cfg(f)

        cond_block = findop(f, 'cbranch').block
        self.assertEqual(len(flow[cond_block]), 2)
Пример #15
0
    def test_cfg(self):
        mod = from_c(source)
        f = mod.get_function('func_simple')
        verify(f)
        flow = cfa.cfg(f)

        cond_block = findop(f, 'cbranch').block
        self.assertEqual(len(flow[cond_block]), 2)
Пример #16
0
    def test_swap_loop(self):
        simple = textwrap.dedent("""
        #include <pykit_ir.h>

        int f(int i, int j) {
            int x = 1;
            int y = 2;
            int tmp;
            while (i > 0) {
                while (j > 0) {
                    tmp = x;
                    x = y;
                    y = tmp;
                    j = j - 1;
                }
                i = i - 1;
            }
            return x;
        }
        """)
        mod = from_c(simple)
        func = mod.get_function("f")
        cfa.run(func)

        verify(func)

        ssa_results = []
        for i in range(10):
            for j in range(10):
                ssa_result = interp.run(func, args=[i, j])
                ssa_results.append(ssa_result)

        #print(func)
        reg2mem.reg2mem(func)
        verify(func)
        #print(func)

        stack_results = []
        for i in range(10):
            for j in range(10):
                stack_result = interp.run(func, args=[i, j])
                stack_results.append(stack_result)
Пример #17
0
    def test_swap_loop(self):
        simple = textwrap.dedent("""
        #include <pykit_ir.h>

        int f(int i, int j) {
            int x = 1;
            int y = 2;
            int tmp;
            while (i > 0) {
                while (j > 0) {
                    tmp = x;
                    x = y;
                    y = tmp;
                    j = j - 1;
                }
                i = i - 1;
            }
            return x;
        }
        """)
        mod = from_c(simple)
        func = mod.get_function("f")
        cfa.run(func)

        verify(func)

        ssa_results = []
        for i in range(10):
            for j in range(10):
                ssa_result = interp.run(func, args=[i, j])
                ssa_results.append(ssa_result)

        #print(func)
        reg2mem.reg2mem(func)
        verify(func)
        #print(func)

        stack_results = []
        for i in range(10):
            for j in range(10):
                stack_result = interp.run(func, args=[i, j])
                stack_results.append(stack_result)
Пример #18
0
def pykitcompile(source, funcname=None, cfanalyze=True):
    m = from_c(source)
    verify(m)
    if cfanalyze:
        for function in m.functions.values():
            cfa.run(function)
            verify(function)
        verify(m)

    if len(m.functions) == 1:
        funcname, = m.functions

    if funcname:
        f = m.get_function(funcname)
        b = Builder(f)
        entry = f.startblock
    else:
        f, b, entry = None, None, None

    env = environment.fresh_env()
    return State(m, f, b, entry, env)
Пример #19
0
    def test_ops(self):
        source = textwrap.dedent("""
        #include <pykit_ir.h>

        Int32 f(Int32 i) {
            Int32 x;

            x = 2;

            if (x < 3)
                x = x + 1;
            if (x <= 3)
                x = x + 1;
            if (x >= 3)
                x = x + 1;
            if (x >= 3)
                x = x + 1;
            if (x > 3)
                x = x + 1;

            return x;
        }
        """)

        mod = from_c(source)
        f = mod.get_function("f")
        remove_convert(f)
        cfa.run(f)
        sccp.run(f)
        verify(f)

        # print(f)

        ops = list(f.ops)

        assert len(list(f.ops)) == 1
        [op] = ops
        assert op.opcode == 'ret'
        assert isinstance(op.args[0], Const)
        self.assertEqual(op.args[0].const, 7)
Пример #20
0
    def test_ops(self):
        source = textwrap.dedent("""
        #include <pykit_ir.h>

        Int32 f(Int32 i) {
            Int32 x;

            x = 2;

            if (x < 3)
                x = x + 1;
            if (x <= 3)
                x = x + 1;
            if (x >= 3)
                x = x + 1;
            if (x >= 3)
                x = x + 1;
            if (x > 3)
                x = x + 1;

            return x;
        }
        """)

        mod = from_c(source)
        f = mod.get_function("f")
        remove_convert(f)
        cfa.run(f)
        sccp.run(f)
        verify(f)

        # print(f)

        ops = list(f.ops)

        assert len(list(f.ops)) == 1
        [op] = ops
        assert op.opcode == 'ret'
        assert isinstance(op.args[0], Const)
        self.assertEqual(op.args[0].const, 7)
Пример #21
0
    def test_inline(self):
        simple = """
        #include <pykit_ir.h>

        int callee(int i) {
            return i * i;
        }

        int caller(int i) {
            int x = call(callee, list(i));
            return x;
        }
        """
        mod = from_c(simple)
        func = mod.get_function("caller")
        [callsite] = findallops(func, 'call')
        inline.inline(func, callsite)
        cfa.run(func)
        verify(func)
        assert interp.run(func, args=[10]) == 100
        assert len(list(func.blocks)) == 1
        assert opcodes(func) == ['mul', 'ret']
Пример #22
0
    def test_inline(self):
        simple = textwrap.dedent("""
        #include <pykit_ir.h>

        int callee(int i) {
            return i * i;
        }

        int caller(int i) {
            int x = call(callee, list(i));
            return x;
        }
        """)
        mod = from_c(simple)
        func = mod.get_function("caller")
        [callsite] = findallops(func, 'call')
        inline.inline(func, callsite)
        cfa.run(func)
        verify(func)
        assert interp.run(func, args=[10]) == 100
        assert len(list(func.blocks)) == 1
        assert opcodes(func) == ['mul', 'ret']
Пример #23
0
    def test_ssa(self):
        mod = from_c(source)
        f = mod.get_function('func_simple')
        verify(f)
        self.assertEqual(opcodes(f.startblock),
                         ['alloca', 'store', 'load', 'gt', 'cbranch'])

        # SSA
        CFG = cfa.cfg(f)
        cfa.ssa(f, CFG)

        assert len(f.blocks) == 4
        blocks = list(f.blocks)
        self.assertEqual(opcodes(blocks[0]), ['gt', 'cbranch'])
        self.assertEqual(opcodes(blocks[1]), ['jump'])
        self.assertEqual(opcodes(blocks[2]), ['jump'])
        self.assertEqual(opcodes(blocks[3]), ['phi', 'convert', 'ret'])

        phi = findop(f, 'phi')
        iblocks, ivals = phi.args
        self.assertEqual(sorted(iblocks), sorted([blocks[1], blocks[2]]))
        self.assertEqual(len(ivals), 2)
Пример #24
0
    def test_ssa(self):
        mod = from_c(source)
        f = mod.get_function('func_simple')
        verify(f)
        self.assertEqual(opcodes(f.startblock),
                         ['alloca', 'store', 'load', 'gt', 'cbranch'])

        # SSA
        CFG = cfa.cfg(f)
        cfa.ssa(f, CFG)

        assert len(f.blocks) == 4
        blocks = list(f.blocks)
        self.assertEqual(opcodes(blocks[0]), ['gt', 'cbranch'])
        self.assertEqual(opcodes(blocks[1]), ['jump'])
        self.assertEqual(opcodes(blocks[2]), ['jump'])
        self.assertEqual(opcodes(blocks[3]), ['phi', 'ret'])

        phi = findop(f, 'phi')
        iblocks, ivals = phi.args
        self.assertEqual(sorted(iblocks), sorted([blocks[1], blocks[2]]))
        self.assertEqual(len(ivals), 2)
Пример #25
0
    def test_inline(self):
        simple = textwrap.dedent("""
        #include <pykit_ir.h>

        int callee(int i) {
            return i * i;
        }

        int caller(int i) {
            int x = call(callee, list(i));
            return x;
        }
        """)
        mod = from_c(simple)
        func = mod.get_function("caller")
        [callsite] = findallops(func, 'call')
        inline.inline(func, callsite)
        cfa.run(func)
        verify(func)
        assert interp.run(func, args=[10]) == 100
        assert len(list(func.blocks)) == 1
        self.assertEqual([o for o in opcodes(func) if o != 'convert'],
                         ['mul', 'ret'])
Пример #26
0
thread_module_source = """
typedef struct Thread;
typedef struct ThreadPool;

extern Thread *thread_start(void *function, void *args); /*:
    { "exc.badval": 0, "exc.raise": "RuntimeError",
      "exc.msg": "Unable to initialize thread" } :*/
extern void thread_join(Thread *thread);

extern ThreadPool *threadpool_start(Int32 nthreads); /*:
    { "exc.badval": 0, "exc.raise": "RuntimeError",
      "exc.msg": "Unable to initialize threadpool" } :*/
extern void threadpool_submit(ThreadPool *threadpool, void *func, void *args);
extern void threadpool_join(ThreadPool *threadpool);
extern void threadpool_close(ThreadPool *threadpool);
"""

thread_module = from_c(thread_module_source, __file__)


def resolve_threading_rt(env, threadmod):
    threadlib = ctypes.CDLL(env["library.threads"])
    libraries.resolve_symbols(threadmod, threadlib)


def run(func, env, threadmod=thread_module):
    """Generate runtime calls into thread library"""
    resolve_threading_rt(env, threadmod)
    func.parent.link(threadmod)
    RuntimeLowering(func).lower_ops_into_runtime(func, ops.oplist("thread_*"))
Пример #27
0
 def test_normalize(self):
     mod = from_c(testfunc)
     func = mod.get_function("func")
     ret.run(func, fresh_env())
     ops = opcodes(func)
     self.assertEqual(ops.count("ret"), 1, ops)
Пример #28
0
 def setUp(self):
     self.m = from_c(source)
     self.f = self.m.get_function('testfunc')
     self.b = Builder(self.f)