Exemple #1
0
    def test_bridge_from_interpreter_4(self):
        jitdriver = JitDriver(reds = ['n', 'k'], greens = [])
        
        def f(n, k):
            while n > 0:
                jitdriver.can_enter_jit(n=n, k=k)
                jitdriver.jit_merge_point(n=n, k=k)
                if k:
                    n -= 2
                else:
                    n -= 1
            return n + k

        from pypy.rpython.test.test_llinterp import get_interpreter, clear_tcache
        from pypy.jit.metainterp.warmspot import WarmRunnerDesc
        
        interp, graph = get_interpreter(f, [0, 0], backendopt=False,
                                        inline_threshold=0, type_system=self.type_system)
        clear_tcache()
        translator = interp.typer.annotator.translator
        translator.config.translation.gc = "boehm"
        warmrunnerdesc = WarmRunnerDesc(translator,
                                        CPUClass=self.CPUClass)
        warmrunnerdesc.state.set_param_threshold(3)          # for tests
        warmrunnerdesc.state.set_param_trace_eagerness(0)    # for tests
        warmrunnerdesc.finish()
        for n, k in [(20, 0), (20, 1)]:
            interp.eval_graph(graph, [n, k])
Exemple #2
0
def interpret(func, values, view=False, viewbefore=False, policy=None,
              someobjects=False):
    interp, graph = get_interpreter(func, values, view, viewbefore, policy,
                             someobjects, type_system='ootype')
    for g in interp.typer.annotator.translator.graphs:
        check_only_ootype(g)
    return interp.eval_graph(graph, values)
Exemple #3
0
def test_constfold():
    layoutbuilder = TypeLayoutBuilder(FakeGC)
    tid1 = layoutbuilder.get_type_id(GC_A)
    tid2 = layoutbuilder.get_type_id(GC_S3)
    class MockGC:
        def set_query_functions(self, is_varsize,
                                has_gcptr_in_varsize,
                                is_gcarrayofgcptr,
                                *rest):
            self.is_varsize = is_varsize
            self.has_gcptr_in_varsize = has_gcptr_in_varsize
            self.is_gcarrayofgcptr = is_gcarrayofgcptr
    gc = MockGC()
    layoutbuilder.initialize_gc_query_function(gc)
    #
    def f():
        return (1 * gc.is_varsize(tid1) +
               10 * gc.has_gcptr_in_varsize(tid1) +
              100 * gc.is_gcarrayofgcptr(tid1) +
             1000 * gc.is_varsize(tid2) +
            10000 * gc.has_gcptr_in_varsize(tid2) +
           100000 * gc.is_gcarrayofgcptr(tid2))
    interp, graph = get_interpreter(f, [], backendopt=True)
    assert interp.eval_graph(graph, []) == 11001
    assert graph.startblock.exits[0].args == [Constant(11001, lltype.Signed)]
Exemple #4
0
def test_constfold():
    layoutbuilder = TypeLayoutBuilder(FakeGC, {})
    tid1 = layoutbuilder.get_type_id(GC_A)
    tid2 = layoutbuilder.get_type_id(GC_S3)

    class MockGC:
        def set_query_functions(self, is_varsize, has_gcptr_in_varsize,
                                is_gcarrayofgcptr, *rest):
            self.is_varsize = is_varsize
            self.has_gcptr_in_varsize = has_gcptr_in_varsize
            self.is_gcarrayofgcptr = is_gcarrayofgcptr

    gc = MockGC()
    layoutbuilder.initialize_gc_query_function(gc)

    #
    def f():
        return (1 * gc.is_varsize(tid1) + 10 * gc.has_gcptr_in_varsize(tid1) +
                100 * gc.is_gcarrayofgcptr(tid1) + 1000 * gc.is_varsize(tid2) +
                10000 * gc.has_gcptr_in_varsize(tid2) +
                100000 * gc.is_gcarrayofgcptr(tid2))

    interp, graph = get_interpreter(f, [], backendopt=True)
    assert interp.eval_graph(graph, []) == 11001
    assert graph.startblock.exits[0].args == [Constant(11001, lltype.Signed)]
Exemple #5
0
def test_optimize_method():
    def fn(n):
        if n > 0:
            x = B(n)
        else:
            x = C(n)
        return x.meth(100)

    interp, graph = get_interpreter(fn, [-1000])

    t = interp.typer.annotator.translator
    t.config.translation.backendopt.constfold = True
    backend_optimizations(t)
    if conftest.option.view:
        t.view()

    LLFrame = interp.frame_class

    class MyFrame(LLFrame):
        def op_indirect_call(self, f, *args):
            raise AssertionError("this call should be optimized away")

    interp.frame_class = MyFrame
    res = interp.eval_graph(graph, [-1000])
    assert res == -897
Exemple #6
0
    def test_bridge_from_interpreter_4(self):
        jitdriver = JitDriver(reds=['n', 'k'], greens=[])

        def f(n, k):
            while n > 0:
                jitdriver.can_enter_jit(n=n, k=k)
                jitdriver.jit_merge_point(n=n, k=k)
                if k:
                    n -= 2
                else:
                    n -= 1
            return n + k

        from pypy.rpython.test.test_llinterp import get_interpreter, clear_tcache
        from pypy.jit.metainterp.warmspot import WarmRunnerDesc

        interp, graph = get_interpreter(f, [0, 0],
                                        backendopt=False,
                                        inline_threshold=0,
                                        type_system=self.type_system)
        clear_tcache()
        translator = interp.typer.annotator.translator
        translator.config.translation.gc = "boehm"
        warmrunnerdesc = WarmRunnerDesc(translator, CPUClass=self.CPUClass)
        warmrunnerdesc.state.set_param_threshold(3)  # for tests
        warmrunnerdesc.state.set_param_trace_eagerness(0)  # for tests
        warmrunnerdesc.finish()
        for n, k in [(20, 0), (20, 1)]:
            interp.eval_graph(graph, [n, k])
def test_access_directly_escape():
    class Global:
        pass

    glob = Global()

    def g(b):
        glob.b = b

    def h(b):
        return b.v0

    def f(n):
        b = B(n)
        g(b)
        g(hint(b, access_directly=True))
        return h(glob.b)

    interp, graph = get_interpreter(f, [23])
    desc = interp.typer.annotator.bookkeeper.getdesc(g)
    g_graphs = desc._cache.values()
    assert len(g_graphs) == 2
    summaries = map(summary, g_graphs)
    summaries.sort()
    assert summaries == [{'setfield': 1}, {'setfield': 1}]
    h_graph = interp.typer.annotator.translator._graphof(h)
    assert summary(h_graph) == {'direct_call': 1}

    res = interp.eval_graph(graph, [23])
    assert res == 23
def test_access_directly_escape():
    class Global:
        pass
    glob = Global()

    def g(b):
        glob.b = b

    def h(b):
        return b.v0

    def f(n):
        b = B(n)
        g(b)
        g(hint(b, access_directly=True))
        return h(glob.b)

    interp, graph = get_interpreter(f, [23])
    desc = interp.typer.annotator.bookkeeper.getdesc(g)
    g_graphs = desc._cache.values()
    assert len(g_graphs) == 2
    summaries = map(summary, g_graphs)
    summaries.sort()
    assert summaries == [{'setfield': 1},
                         {'setfield': 1}]
    h_graph = interp.typer.annotator.translator._graphof(h)
    assert summary(h_graph) == {'direct_call': 1}

    res = interp.eval_graph(graph, [23])
    assert res == 23
def interpret(func, values):
    interp, graph = get_interpreter(func, values)
    t = interp.typer.annotator.translator
    if t not in _already_transformed:
        etrafo = exceptiontransform.ExceptionTransformer(t)
        etrafo.transform_completely()
        _already_transformed[t] = True
    return interp.eval_graph(graph, values)
Exemple #10
0
def interpret(func, values):
    interp, graph = get_interpreter(func, values)
    t = interp.typer.annotator.translator
    if t not in _already_transformed:
        etrafo = exceptiontransform.ExceptionTransformer(t)
        etrafo.transform_completely()
        _already_transformed[t] = True
    return interp.eval_graph(graph, values)
Exemple #11
0
 def interpret(self, fn, args):
     interp, graph = get_interpreter(fn, args, view=False, viewbefore=False, type_system=self.type_system)
     if option.view:
         interp.typer.annotator.translator.view()
     build_trees(graph)
     if option.view:
         interp.typer.annotator.translator.view()
     return interp.eval_graph(graph, args)
Exemple #12
0
def ll_meta_interp(function, args, backendopt=False, type_system='lltype',
                   listcomp=False, **kwds):
    if listcomp:
        extraconfigopts = {'translation.list_comprehension_operations': True}
    else:
        extraconfigopts = {}
    interp, graph = get_interpreter(function, args,
                                    backendopt=False,  # will be done below
                                    type_system=type_system,
                                    **extraconfigopts)
    clear_tcache()
    return jittify_and_run(interp, graph, args, backendopt=backendopt, **kwds)
Exemple #13
0
 def interpret(self, fn, args):
     interp, graph = get_interpreter(fn,
                                     args,
                                     view=False,
                                     viewbefore=False,
                                     type_system=self.type_system)
     if option.view:
         interp.typer.annotator.translator.view()
     build_trees(graph)
     if option.view:
         interp.typer.annotator.translator.view()
     return interp.eval_graph(graph, args)
Exemple #14
0
def test_run_translation():
    from pypy.translator.goal.ann_override import PyPyAnnotatorPolicy
    from pypy.rpython.test.test_llinterp import get_interpreter

    # first annotate, rtype, and backendoptimize PyPy
    try:
        interp, graph = get_interpreter(entry_point, [], backendopt=True,
                                        config=config,
                                        type_system=config.translation.type_system,
                                        policy=PyPyAnnotatorPolicy(space))
    except Exception, e:
        print '%s: %s' % (e.__class__, e)
        pdb.post_mortem(sys.exc_info()[2])
        raise
def get_runner(f, exceptedop, types):
    values = [t() for t in types]
    interp, graph = get_interpreter(f, values)
    for op in support.graph_operations(graph):
        if op.opname == exceptedop:
            break
    else:
        assert False, "op %r not found!"%(exceptedop,)
    t = interp.typer.annotator.translator # FIIISH!
    raisingop2direct_call.raisingop2direct_call(t, [graph])
    def ret(*args):
        assert map(type, args) == types
        return interp.eval_graph(graph, args)
    return ret
def test_access_directly():
    def g(b):
        return b.v0

    def f(n):
        b = B(n)
        b = hint(b, access_directly=True)
        return g(b)

    interp, graph = get_interpreter(f, [23])
    g_graph = interp.typer.annotator.translator._graphof(g)
    assert summary(g_graph) == {'getfield': 1}

    res = interp.eval_graph(graph, [23])
    assert res == 23
Exemple #17
0
def test_access_directly():
    def g(b):
        return b.v0

    def f(n):
        b = B(n)
        b = hint(b, access_directly=True)
        return g(b)

    interp, graph = get_interpreter(f, [23])
    g_graph = interp.typer.annotator.translator._graphof(g)
    assert summary(g_graph) == {'getfield': 1}

    res = interp.eval_graph(graph, [23])
    assert res == 23
Exemple #18
0
def test_run_translation():
    from pypy.translator.goal.ann_override import PyPyAnnotatorPolicy
    from pypy.rpython.test.test_llinterp import get_interpreter

    # first annotate and rtype
    try:
        interp, graph = get_interpreter(
            entry_point, [],
            backendopt=False,
            config=config,
            type_system=config.translation.type_system,
            policy=PyPyAnnotatorPolicy(space))
    except Exception, e:
        print '%s: %s' % (e.__class__, e)
        pdb.post_mortem(sys.exc_info()[2])
        raise
def get_runner(f, exceptedop, types):
    values = [t() for t in types]
    interp, graph = get_interpreter(f, values)
    for op in support.graph_operations(graph):
        if op.opname == exceptedop:
            break
    else:
        assert False, "op %r not found!" % (exceptedop, )
    t = interp.typer.annotator.translator  # FIIISH!
    raisingop2direct_call.raisingop2direct_call(t, [graph])

    def ret(*args):
        assert map(type, args) == types
        return interp.eval_graph(graph, args)

    return ret
Exemple #20
0
def interpret(func,
              values,
              view=False,
              viewbefore=False,
              policy=None,
              someobjects=False):
    interp, graph = get_interpreter(func,
                                    values,
                                    view,
                                    viewbefore,
                                    policy,
                                    someobjects,
                                    type_system='ootype')
    for g in interp.typer.annotator.translator.graphs:
        check_only_ootype(g)
    return interp.eval_graph(graph, values)
    def test_simple_struct(self):
        S0 = RStruct('S0', [('x', rc_int)])
        def func():
            s = S0.allocate()
            s.ref_x().set_value(12)
            return s.ref_x().get_value()

        interp, graph = get_interpreter(func, [], policy=POLICY,
                                        backendopt=True)
        res = interp.eval_graph(graph, [])
        assert res == 12
        # after inlining the get_value() call, there is a getarrayitem
        # at the end of the main graph.  However, the memory it accesses
        # must be protected by a following keepalive...
        entrymap = mkentrymap(graph)
        [link] = entrymap[graph.returnblock]
        assert link.prevblock.operations[-1].opname == 'keepalive'
Exemple #22
0
def test_access_directly_method():
    class A(B):
        def meth1(self, x):
            return self.g(x + 1)

        def g(self, y):
            return self.v0 * y

    def f(n):
        a = A(n)
        a = hint(a, access_directly=True)
        return a.meth1(100)

    interp, graph = get_interpreter(f, [23])
    g_graph = interp.typer.annotator.translator._graphof(A.g.im_func)
    assert summary(g_graph) == {'getfield': 1, 'int_mul': 1}

    res = interp.eval_graph(graph, [23])
    assert res == 2323
def test_access_directly_method():
    class A(B):
        def meth1(self, x):
            return self.g(x+1)

        def g(self, y):
            return self.v0 * y

    def f(n):
        a = A(n)
        a = hint(a, access_directly=True)
        return a.meth1(100)

    interp, graph = get_interpreter(f, [23])
    g_graph = interp.typer.annotator.translator._graphof(A.g.im_func)
    assert summary(g_graph) == {'getfield': 1, 'int_mul': 1}

    res = interp.eval_graph(graph, [23])
    assert res == 2323
Exemple #24
0
def test_access_directly_specialized():
    def g(b):
        return b.v0

    def f(n):
        b = B(n)
        x = g(b)
        y = g(hint(b, access_directly=True))
        return x + y

    interp, graph = get_interpreter(f, [23])
    desc = interp.typer.annotator.bookkeeper.getdesc(g)
    g_graphs = desc._cache.values()
    assert len(g_graphs) == 2
    summaries = map(summary, g_graphs)
    summaries.sort()
    assert summaries == [{'direct_call': 1}, {'getfield': 1}]

    res = interp.eval_graph(graph, [23])
    assert res == 46
Exemple #25
0
def test_untagged_subclasses():
    def g(x):
        return x.attrvalue   # should not produce a call to ll_unboxed_getclass
    def fn(n):
        y = C(12)
        if n > 0:
            x = B(5)
        else:
            x = D(5)
        return g(x)

    interp, graph = get_interpreter(fn, [-1000], taggedpointers=True)

    t = interp.typer.annotator.translator
    ggraph = graphof(t, g)
    assert summary(ggraph) == {'cast_pointer': 2, 'getfield': 2}

    res = interp.eval_graph(graph, [-1000])
    assert res == 68
    res = interp.eval_graph(graph, [3])
    assert res == 66
def test_access_directly_specialized():
    def g(b):
        return b.v0

    def f(n):
        b = B(n)
        x = g(b)
        y = g(hint(b, access_directly=True))
        return x + y

    interp, graph = get_interpreter(f, [23])
    desc = interp.typer.annotator.bookkeeper.getdesc(g)
    g_graphs = desc._cache.values()
    assert len(g_graphs) == 2
    summaries = map(summary, g_graphs)
    summaries.sort()
    assert summaries == [{'direct_call': 1},
                         {'getfield': 1}]

    res = interp.eval_graph(graph, [23])
    assert res == 46
Exemple #27
0
def test_optimize_method():
    def fn(n):
        if n > 0:
            x = B(n)
        else:
            x = C(n)
        return x.meth(100)
    interp, graph = get_interpreter(fn, [-1000], taggedpointers=True)

    t = interp.typer.annotator.translator
    t.config.translation.backendopt.constfold = True
    backend_optimizations(t)
    if conftest.option.view:
        t.view()

    LLFrame = interp.frame_class
    class MyFrame(LLFrame):
        def op_indirect_call(self, f, *args):
            raise AssertionError("this call should be optimized away")
    interp.frame_class = MyFrame
    res = interp.eval_graph(graph, [-1000])
    assert res == -897
Exemple #28
0
 def interpret(self, func, values, **kwds):
     interp, graph = get_interpreter(func, values, **kwds)
     gcwrapper.prepare_graphs_and_create_gc(interp, self.GCClass,
                                            self.GC_PARAMS)
     return interp.eval_graph(graph, values)
Exemple #29
0
 def interpret(self, func, values, **kwds):
     interp, graph = get_interpreter(func, values, **kwds)
     gcwrapper.prepare_graphs_and_create_gc(interp, self.GCClass,
                                            self.GC_PARAMS)
     return interp.eval_graph(graph, values)