Author: Armin Rigo <[email protected]>
Branch: 
Changeset: r44333:4d4b01d56455
Date: 2011-05-20 15:40 +0200
http://bitbucket.org/pypy/pypy/changeset/4d4b01d56455/

Log:    hg merge partial-virtualizable:

        A hack to speed up CALL_ASSEMBLER: don't fill all fields of the
        frame that we just forced. The fields that are virtualizable can
        remain uninitialized, because they are (generally) not read out of
        the frame; they are directly passed as arguments.

        The only exception to that (I think) is compile_tmp_callback(). Fix
        and tests: now the code starts by copying the values from the
        arguments into the frame, thus completing its initialization.

        "pypy lib-python" tests pass, which may (or may not) show that it
        works...

diff --git a/pypy/jit/backend/llgraph/llimpl.py 
b/pypy/jit/backend/llgraph/llimpl.py
--- a/pypy/jit/backend/llgraph/llimpl.py
+++ b/pypy/jit/backend/llgraph/llimpl.py
@@ -1564,6 +1564,9 @@
     assert list(argsiter_f) == []
     return args
 
+def do_partial_virtualizable(virtualizable):
+    pass
+
 
 # for ootype meth and staticmeth
 def call_maybe_on_top_of_llinterp(meth, args):
diff --git a/pypy/jit/metainterp/compile.py b/pypy/jit/metainterp/compile.py
--- a/pypy/jit/metainterp/compile.py
+++ b/pypy/jit/metainterp/compile.py
@@ -650,6 +650,7 @@
         raise metainterp_sd.ExitFrameWithExceptionRef(cpu, exception)
 
 propagate_exception_descr = PropagateExceptionDescr()
+_compile_bogus_code = False     # for test_compile_tmp_callback_and_using_it
 
 def compile_tmp_callback(cpu, jitdriver_sd, greenboxes, redboxes,
                          memory_manager=None):
@@ -683,6 +684,38 @@
     else:
         finishargs = []
     #
+    # must cancel the optimization done by optimize_PARTIAL_VIRTUALIZABLE
+    # in optimizeopt/virtualize.py
+    setoperations = []
+    src_index = nb_red_args
+    vinfo = jitdriver_sd.virtualizable_info
+    if vinfo is not None:
+        vablebox = inputargs[jitdriver_sd.index_of_virtualizable]
+        for descr in vinfo.static_field_descrs:
+            valuebox = inputargs[src_index]
+            src_index += 1
+            setoperations.append(
+                ResOperation(rop.SETFIELD_GC, [vablebox, valuebox], None,
+                             descr=descr))
+        for arrayindex in range(len(vinfo.array_field_descrs)):
+            # xxx fish the virtualizable from the box
+            length = vinfo.get_array_length(vablebox.getref_base(), arrayindex)
+            assert src_index + length <= len(inputargs)
+            arraybox = BoxPtr()
+            arrayfielddescr = vinfo.array_field_descrs[arrayindex]
+            arraydescr = vinfo.array_descrs[arrayindex]
+            setoperations.append(
+                ResOperation(rop.GETFIELD_GC, [vablebox], arraybox,
+                             descr=arrayfielddescr))
+            for i in range(length):
+                valuebox = inputargs[src_index]
+                src_index += 1
+                setoperations.append(
+                    ResOperation(rop.SETARRAYITEM_GC,
+                                 [arraybox, history.ConstInt(i), valuebox],
+                                 None, descr=arraydescr))
+    assert src_index == len(inputargs)
+    #
     jd = jitdriver_sd
     faildescr = propagate_exception_descr
     operations = [
@@ -691,6 +724,12 @@
         ResOperation(rop.FINISH, finishargs, None, descr=jd.portal_finishtoken)
         ]
     operations[1].setfailargs([])
+    if _compile_bogus_code:      # testing only
+        operations.insert(0, ResOperation(rop.INT_FLOORDIV,
+                                          [history.ConstInt(42),
+                                           history.ConstInt(0)],
+                                          history.BoxInt()))
+    operations = setoperations + operations
     operations = get_deep_immutable_oplist(operations)
     cpu.compile_loop(inputargs, operations, loop_token, log=False)
     if memory_manager is not None:    # for tests
diff --git a/pypy/jit/metainterp/executor.py b/pypy/jit/metainterp/executor.py
--- a/pypy/jit/metainterp/executor.py
+++ b/pypy/jit/metainterp/executor.py
@@ -323,6 +323,7 @@
                          rop.JIT_DEBUG,
                          rop.SETARRAYITEM_RAW,
                          rop.QUASIIMMUT_FIELD,
+                         rop.PARTIAL_VIRTUALIZABLE,
                          ):      # list of opcodes never executed by pyjitpl
                 continue
             raise AssertionError("missing %r" % (key,))
diff --git a/pypy/jit/metainterp/history.py b/pypy/jit/metainterp/history.py
--- a/pypy/jit/metainterp/history.py
+++ b/pypy/jit/metainterp/history.py
@@ -211,6 +211,10 @@
     def get_jitcode_for_class(self, oocls):
         return self.jitcodes[oocls]
 
+class JitDriverDescr(AbstractDescr):
+    def __init__(self, jitdriver_sd):
+        self.jitdriver_sd = jitdriver_sd
+
 
 class Const(AbstractValue):
     __slots__ = ()
diff --git a/pypy/jit/metainterp/optimizeopt/simplify.py 
b/pypy/jit/metainterp/optimizeopt/simplify.py
--- a/pypy/jit/metainterp/optimizeopt/simplify.py
+++ b/pypy/jit/metainterp/optimizeopt/simplify.py
@@ -25,6 +25,9 @@
         #     but it's a bit hard to implement robustly if heap.py is also run
         pass
 
+    def optimize_PARTIAL_VIRTUALIZABLE(self, op):
+        pass
+
     def propagate_forward(self, op):
         opnum = op.getopnum()
         for value, func in optimize_ops:
diff --git a/pypy/jit/metainterp/optimizeopt/virtualize.py 
b/pypy/jit/metainterp/optimizeopt/virtualize.py
--- a/pypy/jit/metainterp/optimizeopt/virtualize.py
+++ b/pypy/jit/metainterp/optimizeopt/virtualize.py
@@ -1,3 +1,4 @@
+from pypy.jit.metainterp import history
 from pypy.jit.metainterp.history import Const, ConstInt, BoxInt
 from pypy.jit.metainterp.resoperation import rop, ResOperation
 from pypy.jit.metainterp.optimizeutil import _findall, sort_descrs
@@ -435,6 +436,32 @@
         ###self.heap_op_optimizer.optimize_SETARRAYITEM_GC(op, value, 
fieldvalue)
         self.emit_operation(op)
 
+    def optimize_PARTIAL_VIRTUALIZABLE(self, op):
+        value = self.getvalue(op.getarg(0))
+        if value.is_virtual():
+            jddescr = op.getdescr()
+            assert isinstance(jddescr, history.JitDriverDescr)
+            vinfo = jddescr.jitdriver_sd.virtualizable_info
+            # XXX giant hack.  The idea is that this virtual will be passed
+            # as the virtualizable to the CALL_ASSEMBLER operation that
+            # follows, so it will be forced.  But we will also pass the
+            # virtualizable fields explicitly as arguments to CALL_ASSEMBLER,
+            # which have already been read out of the virtual just before.
+            # The following hackery resets the fields to their NULL/0 value,
+            # which has (only) the effect that forcing the virtual will not
+            # write anything in these fields.
+            assert isinstance(value, AbstractVirtualStructValue)
+            for descr in vinfo.static_field_descrs:
+                try:
+                    del value._fields[descr]
+                except KeyError:
+                    pass
+            for descr in vinfo.array_field_descrs:
+                avalue = value.getfield(descr, None)
+                if isinstance(avalue, VArrayValue):
+                    for i in range(len(avalue._items)):
+                        avalue._items[i] = avalue.constvalue
+
     def propagate_forward(self, op):
         opnum = op.getopnum()
         for value, func in optimize_ops:
diff --git a/pypy/jit/metainterp/pyjitpl.py b/pypy/jit/metainterp/pyjitpl.py
--- a/pypy/jit/metainterp/pyjitpl.py
+++ b/pypy/jit/metainterp/pyjitpl.py
@@ -2340,6 +2340,9 @@
             vbox = args[index]
             args = args + self.gen_load_from_other_virtualizable(vinfo, vbox)
             # ^^^ and not "+=", which makes 'args' a resizable list
+            jddescr = history.JitDriverDescr(targetjitdriver_sd)
+            self.history.record(rop.PARTIAL_VIRTUALIZABLE, [vbox], None,
+                                descr=jddescr)
         warmrunnerstate = targetjitdriver_sd.warmstate
         token = warmrunnerstate.get_assembler_token(greenargs, args)
         op = op.copy_and_change(rop.CALL_ASSEMBLER, args=args, descr=token)
diff --git a/pypy/jit/metainterp/resoperation.py 
b/pypy/jit/metainterp/resoperation.py
--- a/pypy/jit/metainterp/resoperation.py
+++ b/pypy/jit/metainterp/resoperation.py
@@ -478,6 +478,7 @@
     'COPYSTRCONTENT/5',       # src, dst, srcstart, dststart, length
     'COPYUNICODECONTENT/5',
     'QUASIIMMUT_FIELD/1d',    # [objptr], descr=SlowMutateDescr
+    'PARTIAL_VIRTUALIZABLE/1d',  # removed before it's passed to the backend
 
     '_CANRAISE_FIRST', # ----- start of can_raise operations -----
     '_CALL_FIRST',
diff --git a/pypy/jit/metainterp/test/test_recursive.py 
b/pypy/jit/metainterp/test/test_recursive.py
--- a/pypy/jit/metainterp/test/test_recursive.py
+++ b/pypy/jit/metainterp/test/test_recursive.py
@@ -655,8 +655,9 @@
             i = 1
             while 1:
                 driver.jit_merge_point(codeno=codeno, i=i, frame=frame)
+                assert frame.j >= 100
                 if (i >> 1) == 1:
-                    if frame.j == 0:
+                    if frame.j == 100:
                         return
                     portal(2, Frame(frame.j - 1))
                 elif i == 5:
@@ -665,7 +666,7 @@
                 driver.can_enter_jit(codeno=codeno, i=i, frame=frame)
 
         def main(codeno, j):
-            portal(codeno, Frame(j))
+            portal(codeno, Frame(j + 100))
 
         main(2, 5)
 
@@ -695,6 +696,85 @@
             print redirected
             assert redirected.keys() == trace
 
+    def test_compile_tmp_callback_and_using_it(self):
+        # unlike the previous tests, this test calls compile_tmp_callback()
+        # and actually invokes the compiled temporary callback
+        driver = JitDriver(greens = ['codeno'], reds = ['i'])
+
+        def main(codeno):
+            i = 1
+            while i < 7:
+                driver.jit_merge_point(codeno=codeno, i=i)
+                if codeno == 1:
+                    return 42
+                if i >= 3:
+                    main(1)
+                i += 1
+            return i
+
+        from pypy.rpython.llinterp import LLException
+        from pypy.jit.metainterp import compile
+        compile._compile_bogus_code = True
+        try:
+            e = py.test.raises(LLException, self.meta_interp,
+                               main, [2], inline=True)
+        finally:
+            compile._compile_bogus_code = False
+        assert e.value.args[2][0] is ZeroDivisionError
+
+        res = self.meta_interp(main, [2], inline=True)
+        assert res == 7
+
+    def test_compile_tmp_callback_and_using_it_with_virtualizable(self):
+        # same as the previous test, but with a virtualizable
+        class Frame(object):
+            _virtualizable2_ = ['j']
+            def __init__(self, j):
+                self.j = j
+
+        driver = JitDriver(greens = ['codeno'], reds = ['i', 'frame'],
+                           virtualizables = ['frame'])
+
+        def main(codeno):
+            frame = Frame(codeno+100)
+            i = 1
+            while i < 7:
+                driver.jit_merge_point(codeno=codeno, i=i, frame=frame)
+                assert frame.j == codeno+100
+                if codeno == 1:
+                    return
+                if i >= 3:
+                    main(1)
+                i += 1
+
+        self.meta_interp(main, [2], inline=True)
+
+    def test_compile_tmp_callback_and_using_it_with_virtualizable_array(self):
+        # same as the previous test, but with a virtualizable with an array
+        class Frame(object):
+            _virtualizable2_ = ['lst[*]']
+            def __init__(self, lst):
+                self = hint(self, fresh_virtualizable=True,
+                                  access_directly=True)
+                self.lst = lst
+
+        driver = JitDriver(greens = ['codeno'], reds = ['i', 'frame'],
+                           virtualizables = ['frame'])
+
+        def main(codeno):
+            frame = Frame([codeno+100])
+            i = 1
+            while i < 7:
+                driver.jit_merge_point(codeno=codeno, i=i, frame=frame)
+                assert frame.lst[0] == codeno+100
+                if codeno == 1:
+                    return
+                if i >= 3:
+                    main(1)
+                i += 1
+
+        self.meta_interp(main, [2], inline=True)
+
     def test_directly_call_assembler_return(self):
         driver = JitDriver(greens = ['codeno'], reds = ['i', 'k'],
                            get_printable_location = lambda codeno : 
str(codeno))
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to