Exemple #1
0
def test_find_all_graphs_loops():
    def g(x):
        i = 0
        while i < x:
            i += 1
        return i
    @jit.unroll_safe
    def h(x):
        i = 0
        while i < x:
            i += 1
        return i

    def f(x):
        i = 0
        while i < x*x:
            i += g(x) + h(x)
        return i

    rtyper = support.annotate(f, [7])
    cw = CodeWriter(rtyper)
    jitpolicy = JitPolicy()
    translator = rtyper.annotator.translator
    res = cw.find_all_graphs(translator.graphs[0], None, jitpolicy,
                             supports_floats=True)
    funcs = set([graph.func for graph in res])
    assert funcs == set([f, h])
Exemple #2
0
def test_find_all_graphs_loops():
    def g(x):
        i = 0
        while i < x:
            i += 1
        return i

    @jit.unroll_safe
    def h(x):
        i = 0
        while i < x:
            i += 1
        return i

    def f(x):
        i = 0
        while i < x * x:
            i += g(x) + h(x)
        return i

    rtyper = support.annotate(f, [7])
    cw = CodeWriter(rtyper)
    jitpolicy = JitPolicy()
    translator = rtyper.annotator.translator
    res = cw.find_all_graphs(translator.graphs[0],
                             None,
                             jitpolicy,
                             supports_floats=True)
    funcs = set([graph.func for graph in res])
    assert funcs == set([f, h])
Exemple #3
0
def test_find_all_graphs():
    def f(x):
        if x < 0:
            return f(-x)
        return x + 1

    @jit.purefunction
    def g(x):
        return x + 2

    @jit.dont_look_inside
    def h(x):
        return x + 3

    def i(x):
        return f(x) * g(x) * h(x)

    rtyper = support.annotate(i, [7])
    cw = CodeWriter(rtyper)
    jitpolicy = JitPolicy()
    res = cw.find_all_graphs(rtyper.annotator.translator.graphs[0], None, jitpolicy, True)
    translator = rtyper.annotator.translator

    funcs = set([graph.func for graph in res])
    assert funcs == set([i, f])
Exemple #4
0
def test_find_all_graphs_without_floats():
    def g(x):
        return int(x * 12.5)

    def f(x):
        return g(x) + 1

    rtyper = support.annotate(f, [7])
    cw = CodeWriter(rtyper)
    jitpolicy = JitPolicy()
    translator = rtyper.annotator.translator
    res = cw.find_all_graphs(translator.graphs[0],
                             None,
                             jitpolicy,
                             supports_floats=True)
    funcs = set([graph.func for graph in res])
    assert funcs == set([f, g])

    cw = CodeWriter(rtyper)
    res = cw.find_all_graphs(translator.graphs[0],
                             None,
                             jitpolicy,
                             supports_floats=False)
    funcs = [graph.func for graph in res]
    assert funcs == [f]
Exemple #5
0
def test_find_all_graphs():
    def f(x):
        if x < 0:
            return f(-x)
        return x + 1

    @jit.purefunction
    def g(x):
        return x + 2

    @jit.dont_look_inside
    def h(x):
        return x + 3

    def i(x):
        return f(x) * g(x) * h(x)

    rtyper = support.annotate(i, [7])
    cw = CodeWriter(rtyper)
    jitpolicy = JitPolicy()
    res = cw.find_all_graphs(rtyper.annotator.translator.graphs[0], None,
                             jitpolicy, True)
    translator = rtyper.annotator.translator

    funcs = set([graph.func for graph in res])
    assert funcs == set([i, f])
Exemple #6
0
    def make_graphs(self, func, values, type_system="lltype"):
        class FakeMetaInterpSd:
            virtualizable_info = None

            class options:
                listops = True

            def find_opcode(self, name):
                default = len(self.opname_to_index)
                return self.opname_to_index.setdefault(name, default)

            def _register_indirect_call_target(self, fnaddress, jitcode):
                self.indirectcalls.append((fnaddress, jitcode))

        class FakeMethDescr:
            def __init__(self1, CLASS, methname):
                self.methdescrs.append(self1)
                self1.CLASS = CLASS
                self1.methname = methname
                self1.jitcodes = None

            def setup(self1, jitcodes):
                self1.jitcodes = jitcodes

        self.methdescrs = []

        class FakeCPU:
            supports_floats = False

            def fielddescrof(self, STRUCT, fieldname):
                return ("fielddescr", STRUCT, fieldname)

            def calldescrof(self, FUNC, NON_VOID_ARGS, RESULT, effectinfo=None):
                return ("calldescr", FUNC, NON_VOID_ARGS, RESULT, effectinfo)

            def typedescrof(self, CLASS):
                return ("typedescr", CLASS)

            def methdescrof(self, CLASS, methname):
                return FakeMethDescr(CLASS, methname)

            def sizeof(self, STRUCT):
                return ("sizeof", STRUCT)

        if type_system == "lltype":
            FakeCPU.ts = typesystem.llhelper
        else:
            FakeCPU.ts = typesystem.oohelper

        self.metainterp_sd = FakeMetaInterpSd()
        self.metainterp_sd.opcode_implementations = None
        self.metainterp_sd.opname_to_index = {}
        self.metainterp_sd.indirectcalls = []
        self.metainterp_sd.cpu = FakeCPU()

        self.rtyper = support.annotate(func, values, type_system=type_system)
        self.metainterp_sd.cpu.rtyper = self.rtyper
        return self.rtyper.annotator.translator.graphs
Exemple #7
0
def test_find_all_graphs_str_join():
    def i(x, y):
        return "hello".join([str(x), str(y), "bye"])

    rtyper = support.annotate(i, [7, 100])
    cw = CodeWriter(rtyper)
    jitpolicy = JitPolicy()
    translator = rtyper.annotator.translator
    # does not explode
    cw.find_all_graphs(translator.graphs[0], None, jitpolicy, True)
Exemple #8
0
def test_find_all_graphs_str_join():
    def i(x, y):
        return "hello".join([str(x), str(y), "bye"])

    rtyper = support.annotate(i, [7, 100])
    cw = CodeWriter(rtyper)
    jitpolicy = JitPolicy()
    translator = rtyper.annotator.translator
    # does not explode
    cw.find_all_graphs(translator.graphs[0], None, jitpolicy, True)
Exemple #9
0
    def make_graphs(self, func, values, type_system='lltype'):
        class FakeMetaInterpSd:
            virtualizable_info = None

            def find_opcode(self, name):
                default = len(self.opname_to_index)
                return self.opname_to_index.setdefault(name, default)

            def _register_indirect_call_target(self, fnaddress, jitcode):
                self.indirectcalls.append((fnaddress, jitcode))

        class FakeMethDescr:
            def __init__(self1, CLASS, methname):
                self.methdescrs.append(self1)
                self1.CLASS = CLASS
                self1.methname = methname
                self1.jitcodes = None

            def setup(self1, jitcodes):
                self1.jitcodes = jitcodes

        self.methdescrs = []

        class FakeCPU:
            supports_floats = False

            def fielddescrof(self, STRUCT, fieldname):
                return ('fielddescr', STRUCT, fieldname)

            def calldescrof(self, FUNC, NON_VOID_ARGS, RESULT):
                return ('calldescr', FUNC, NON_VOID_ARGS, RESULT)

            def typedescrof(self, CLASS):
                return ('typedescr', CLASS)

            def methdescrof(self, CLASS, methname):
                return FakeMethDescr(CLASS, methname)

        if type_system == 'lltype':
            FakeCPU.ts = typesystem.llhelper
        else:
            FakeCPU.ts = typesystem.oohelper

        self.metainterp_sd = FakeMetaInterpSd()
        self.metainterp_sd.opcode_implementations = None
        self.metainterp_sd.opname_to_index = {}
        self.metainterp_sd.indirectcalls = []
        self.metainterp_sd.cpu = FakeCPU()

        self.rtyper = support.annotate(func, values, type_system=type_system)
        self.metainterp_sd.cpu.rtyper = self.rtyper
        return self.rtyper.annotator.translator.graphs
Exemple #10
0
def _get_bare_metainterp(func, values, CPUClass, type_system, listops=False):
    from pypy.annotation.policy import AnnotatorPolicy
    from pypy.annotation.model import lltype_to_annotation
    from pypy.rpython.test.test_llinterp import gengraph

    rtyper = support.annotate(func, values, type_system=type_system)

    stats = history.Stats()
    cpu = CPUClass(rtyper, stats, None, False)
    graphs = rtyper.annotator.translator.graphs
    opt = history.Options(listops=listops)
    metainterp_sd = pyjitpl.MetaInterpStaticData(graphs[0], cpu, stats, opt)
    metainterp_sd.finish_setup(optimizer="bogus")
    metainterp = pyjitpl.MetaInterp(metainterp_sd)
    return metainterp, rtyper
Exemple #11
0
def _get_bare_metainterp(func, values, CPUClass, type_system,
                         listops=False):
    from pypy.annotation.policy import AnnotatorPolicy
    from pypy.annotation.model import lltype_to_annotation
    from pypy.rpython.test.test_llinterp import gengraph

    rtyper = support.annotate(func, values, type_system=type_system)

    stats = history.Stats()
    cpu = CPUClass(rtyper, stats, False)
    graphs = rtyper.annotator.translator.graphs
    opt = history.Options(listops=listops)
    metainterp_sd = pyjitpl.MetaInterpStaticData(graphs[0], cpu, stats, opt)
    metainterp_sd.finish_setup(optimizer="bogus")
    metainterp = pyjitpl.MetaInterp(metainterp_sd)
    return metainterp, rtyper
Exemple #12
0
def test_find_all_graphs_without_floats():
    def g(x):
        return int(x * 12.5)
    def f(x):
        return g(x) + 1
    rtyper = support.annotate(f, [7])
    cw = CodeWriter(rtyper)
    jitpolicy = JitPolicy()
    translator = rtyper.annotator.translator
    res = cw.find_all_graphs(translator.graphs[0], None, jitpolicy,
                             supports_floats=True)
    funcs = set([graph.func for graph in res])
    assert funcs == set([f, g])

    cw = CodeWriter(rtyper)        
    res = cw.find_all_graphs(translator.graphs[0], None, jitpolicy,
                             supports_floats=False)
    funcs = [graph.func for graph in res]
    assert funcs == [f]
Exemple #13
0
def test_unroll_safe_and_inline():
    @jit.unroll_safe
    def h(x):
        i = 0
        while i < x:
            i += 1
        return i

    h._always_inline_ = True

    def g(x):
        return h(x)

    rtyper = support.annotate(g, [7])
    cw = CodeWriter(rtyper)
    jitpolicy = JitPolicy()
    translator = rtyper.annotator.translator
    res = cw.find_all_graphs(translator.graphs[0], None, jitpolicy, supports_floats=True)
    funcs = set([graph.func for graph in res])
    assert funcs == set([g, h])
Exemple #14
0
def test_unroll_safe_and_inline():
    @jit.unroll_safe
    def h(x):
        i = 0
        while i < x:
            i += 1
        return i

    h._always_inline_ = True

    def g(x):
        return h(x)

    rtyper = support.annotate(g, [7])
    cw = CodeWriter(rtyper)
    jitpolicy = JitPolicy()
    translator = rtyper.annotator.translator
    res = cw.find_all_graphs(translator.graphs[0],
                             None,
                             jitpolicy,
                             supports_floats=True)
    funcs = set([graph.func for graph in res])
    assert funcs == set([g, h])
Exemple #15
0
    def setup_class(cls):
        from pypy.jit.metainterp.typesystem import llhelper
        from pypy.jit.metainterp.support import annotate
        from pypy.jit.metainterp.warmspot import WarmRunnerDesc
        from pypy.rpython.lltypesystem.rclass import OBJECT, OBJECT_VTABLE
        from pypy.rpython.lltypesystem import lltype, llmemory
        exc_vtable = lltype.malloc(OBJECT_VTABLE, immortal=True)
        cls.exc_vtable = exc_vtable

        class FakeFailDescr(object):
            def __init__(self, no):
                self.no = no

            def handle_fail(self, metainterp_sd):
                if self.no == 0:
                    raise metainterp_sd.warmrunnerdesc.DoneWithThisFrameInt(3)
                if self.no == 1:
                    raise metainterp_sd.warmrunnerdesc.ContinueRunningNormally(
                        [BoxInt(0), BoxInt(1)])
                if self.no == 3:
                    exc = lltype.malloc(OBJECT)
                    exc.typeptr = exc_vtable
                    raise metainterp_sd.warmrunnerdesc.ExitFrameWithExceptionRef(
                        metainterp_sd.cpu,
                        lltype.cast_opaque_ptr(llmemory.GCREF, exc))
                return self.no

        class FakeCPU(object):
            supports_floats = False
            ts = llhelper
            translate_support_code = False

            def get_fail_descr_number(self, d):
                return -1

            def __init__(self, *args, **kwds):
                pass

            def nodescr(self, *args, **kwds):
                pass

            fielddescrof = nodescr
            calldescrof = nodescr
            sizeof = nodescr

            def get_fail_descr_from_number(self, no):
                return FakeFailDescr(no)

            def execute_token(self, token):
                assert token == 2
                return FakeFailDescr(1)

        driver = JitDriver(reds=['red'], greens=['green'])

        def f(green):
            red = 0
            while red < 10:
                driver.can_enter_jit(red=red, green=green)
                driver.jit_merge_point(red=red, green=green)
                red += 1
            return red

        rtyper = annotate(f, [0])
        translator = rtyper.annotator.translator
        translator.config.translation.gc = 'hybrid'
        cls.desc = WarmRunnerDesc(translator, CPUClass=FakeCPU)
Exemple #16
0
    def setup_class(cls):
        from pypy.jit.metainterp.typesystem import llhelper
        from pypy.jit.metainterp.support import annotate
        from pypy.jit.metainterp.warmspot import WarmRunnerDesc
        from pypy.rpython.lltypesystem.rclass import OBJECT, OBJECT_VTABLE
        from pypy.rpython.lltypesystem import lltype, llmemory
        exc_vtable = lltype.malloc(OBJECT_VTABLE, immortal=True)
        cls.exc_vtable = exc_vtable

        class FakeFailDescr(object):
            def __init__(self, no):
                self.no = no
            
            def handle_fail(self, metainterp_sd):
                if self.no == 0:
                    raise metainterp_sd.warmrunnerdesc.DoneWithThisFrameInt(3)
                if self.no == 1:
                    raise metainterp_sd.warmrunnerdesc.ContinueRunningNormally(
                        [BoxInt(0), BoxInt(1)])
                if self.no == 3:
                    exc = lltype.malloc(OBJECT)
                    exc.typeptr = exc_vtable
                    raise metainterp_sd.warmrunnerdesc.ExitFrameWithExceptionRef(
                        metainterp_sd.cpu,
                        lltype.cast_opaque_ptr(llmemory.GCREF, exc))
                return self.no

        class FakeCPU(object):
            supports_floats = False
            ts = llhelper
            translate_support_code = False
            
            def get_fail_descr_number(self, d):
                return -1

            def __init__(self, *args, **kwds):
                pass

            def nodescr(self, *args, **kwds):
                pass
            fielddescrof = nodescr
            calldescrof  = nodescr
            sizeof       = nodescr

            def get_fail_descr_from_number(self, no):
                return FakeFailDescr(no)

            def execute_token(self, token):
                assert token == 2
                return FakeFailDescr(1)

        driver = JitDriver(reds = ['red'], greens = ['green'])
        
        def f(green):
            red = 0
            while red < 10:
                driver.can_enter_jit(red=red, green=green)
                driver.jit_merge_point(red=red, green=green)
                red += 1
            return red

        rtyper = annotate(f, [0])
        translator = rtyper.annotator.translator
        translator.config.translation.gc = 'hybrid'
        cls.desc = WarmRunnerDesc(translator, CPUClass=FakeCPU)