Beispiel #1
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
Beispiel #2
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)
Beispiel #3
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)
Beispiel #4
0
 def test_exceptions(self):
     f = mod.get_function('raise')
     try:
         result = interp.run(f)
     except interp.UncaughtException, e:
         exc, = e.args
         assert isinstance(exc, TypeError), exc
Beispiel #5
0
 def test_exceptions(self):
     f = mod.get_function('raise')
     try:
         result = interp.run(f)
     except interp.UncaughtException, e:
         exc, = e.args
         assert isinstance(exc, TypeError), exc
Beispiel #6
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)
Beispiel #7
0
 def test_basic_builder(self):
     v = self.b.alloca(types.Pointer(types.Float32), [])
     result = self.b.mul(types.Int32, [self.a, self.a], result='r')
     c = self.b.convert(types.Float32, [result])
     self.b.store(c, v)
     val = self.b.load(types.Float32, [v])
     self.b.ret(val)
     # print(string(self.f))
     assert interp.run(self.f, args=[10]) == 100
Beispiel #8
0
 def test_basic_builder(self):
     v = self.b.alloca(types.Pointer(types.Float32))
     result = self.b.mul(self.a, self.a, result='r')
     c = self.b.convert(types.Float32, result)
     self.b.store(c, v)
     val = self.b.load(v)
     self.b.ret(val)
     # print(string(self.f))
     assert interp.run(self.f, args=[10]) == 100
Beispiel #9
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)
Beispiel #10
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)
Beispiel #11
0
    def test_loop_builder(self):
        square = self.b.mul(types.Int32, [self.a, self.a])
        c = self.b.convert(types.Float32, [square])
        self.b.position_after(square)
        _, block = self.b.splitblock('start', terminate=True)
        self.b.position_at_end(block)

        const = partial(Const, type=types.Int32)
        cond, body, exit = self.b.gen_loop(const(5), const(10), const(2))
        with self.b.at_front(body):
            self.b.print(c)
        with self.b.at_end(exit):
            self.b.ret(c)

        self.assertEqual(interp.run(self.f, args=[10]), 100.0)
Beispiel #12
0
    def test_loop_builder(self):
        square = self.b.mul(self.a, self.a)
        c = self.b.convert(types.Float32, square)
        self.b.position_after(square)
        _, block = self.b.splitblock('start', terminate=True)
        self.b.position_at_end(block)

        const = partial(Const, type=types.Int32)
        cond, body, exit = self.b.gen_loop(const(5), const(10), const(2))
        with self.b.at_front(body):
            self.b.print(c)
        with self.b.at_end(exit):
            self.b.ret(c)

        self.assertEqual(interp.run(self.f, args=[10]), 100.0)
Beispiel #13
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']
Beispiel #14
0
def interpret(nb_func, run_phase, args, debug=False, tracer=None):
    """Interpret and return result"""
    # Translate flypy function
    argtypes = [typeof(arg) for arg in args]
    env = environment.fresh_env(nb_func, argtypes, target="cpu")
    f, env = run_phase(nb_func, env)

    if debug:
        header(f)

    if tracer is None:
        # Set up tracer to trace interpretation
        if debug:
            tracer = tracing.Tracer()
        else:
            tracer = tracing.DummyTracer()

    newargs = [conversion.fromobject(arg, typeof(arg)) for arg in args]

    # Interpret function
    env.setdefault('interp.handlers', handlers(run_phase, env))
    return interp.run(f, env, args=newargs, tracer=tracer)
Beispiel #15
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']
Beispiel #16
0
def interpret(nb_func, run_phase, args, debug=False, tracer=None):
    """Interpret and return result"""
    # Translate flypy function
    argtypes = [typeof(arg) for arg in args]
    env = environment.fresh_env(nb_func, argtypes, target="cpu")
    f, env = run_phase(nb_func, env)

    if debug:
        header(f)

    if tracer is None:
        # Set up tracer to trace interpretation
        if debug:
            tracer = tracing.Tracer()
        else:
            tracer = tracing.DummyTracer()

    newargs = [conversion.fromobject(arg, typeof(arg)) for arg in args]

    # Interpret function
    env.setdefault('interp.handlers', handlers(run_phase, env))
    return interp.run(f, env, args=newargs, tracer=tracer)
Beispiel #17
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'])
Beispiel #18
0
def interpret(func, env, args, persist=False, **kwds):
    # TODO: allow mixing scidb and non-scidb data...

    dshape = func.type.restype
    descs = [arg._data for arg in args]
    inputs = [desc.query for desc in descs]
    conns = [desc.conn for desc in descs]

    if len(set(conns)) > 1:
        raise InterfaceError(
            "Can only perform query over one scidb interface, got multiple")

    # Assemble query
    env = {'interp.handlers' : handlers}
    query = interp.run(func, env, None, args=inputs)
    [conn] = set(conns)

    code = []
    cleanup = []
    query.generate_code(code, cleanup, set())
    expr = query.result()

    result = _execute(conn, code, cleanup, expr, persist)
    return blaze.array(SciDBDataDescriptor(dshape, result, conn))
Beispiel #19
0
def interpret(func, env, args, persist=False, **kwds):
    # TODO: allow mixing scidb and non-scidb data...

    dshape = func.type.restype
    descs = [arg._data for arg in args]
    inputs = [desc.query for desc in descs]
    conns = [desc.conn for desc in descs]

    if len(set(conns)) > 1:
        raise InterfaceError(
            "Can only perform query over one scidb interface, got multiple")

    # Assemble query
    env = {'interp.handlers': handlers}
    query = interp.run(func, env, None, args=inputs)
    [conn] = set(conns)

    code = []
    cleanup = []
    query.generate_code(code, cleanup, set())
    expr = query.result()

    result = _execute(conn, code, cleanup, expr, persist)
    return blaze.array(SciDBDataDescriptor(dshape, result, conn))
Beispiel #20
0
 def test_parse(self):
     mod = cirparser.from_c(source)
     verify(mod)
     func = mod.get_function('myfunc')
     result = interp.run(func, args=[10.0])
     self.assertEqual(result, 12)
Beispiel #21
0
 def test_simple(self):
     f = mod.get_function('simple')
     result = interp.run(f, args=[10.0])
     assert result == 100.0, result
Beispiel #22
0
def run(func, env=None, exc_model=None, args=()):
    env = env or {}
    env.setdefault('interp.handlers', {}).update(handlers)
    return interp.run(func, env, exc_model, args=args)
Beispiel #23
0
def interpret(func, env, args, **kwds):
    args = [np.array(arg) for arg in args]
    env = {'interp.handlers' : handlers}
    result = interp.run(func, env, None, args=args)
    return blaze.array(result)
Beispiel #24
0
 def test_loop(self):
     loop = mod.get_function('loop')
     result = interp.run(loop)
     assert result == 45, result
Beispiel #25
0
 def test_simple(self):
     f = mod.get_function('simple')
     result = interp.run(f, args=[10.0])
     assert result == 100.0, result
Beispiel #26
0
 def test_loop(self):
     loop = mod.get_function('loop')
     result = interp.run(loop)
     assert result == 45, result
Beispiel #27
0
 def test_parse(self):
     mod = cirparser.from_c(source)
     verify(mod)
     func = mod.get_function('myfunc')
     result = interp.run(func, args=[10.0])
     self.assertEqual(result, 12)
Beispiel #28
0
 def interp(self, *args):
     from pykit.ir import interp
     return interp.run(self.f, self.env, args=self.convert_args(args))
Beispiel #29
0
def interpret(func, env, args, **kwds):
    args = [np.array(arg) for arg in args]
    env = {'interp.handlers': handlers}
    result = interp.run(func, env, None, args=args)
    return blaze.array(result)