[pypy-commit] pypy even-more-jit-hooks: hopefully improve the interface, now how do I update the docs?

fijal noreply at buildbot.pypy.org
Fri Jul 6 23:50:48 CEST 2012


Author: Maciej Fijalkowski <fijall at gmail.com>
Branch: even-more-jit-hooks
Changeset: r55948:2902abfe3b06
Date: 2012-07-06 23:50 +0200
http://bitbucket.org/pypy/pypy/changeset/2902abfe3b06/

Log:	hopefully improve the interface, now how do I update the docs?

diff --git a/pypy/module/pypyjit/interp_resop.py b/pypy/module/pypyjit/interp_resop.py
--- a/pypy/module/pypyjit/interp_resop.py
+++ b/pypy/module/pypyjit/interp_resop.py
@@ -15,17 +15,12 @@
 
 class Cache(object):
     in_recursion = False
-    no = 0
 
     def __init__(self, space):
         self.w_compile_hook = space.w_None
         self.w_abort_hook = space.w_None
         self.w_optimize_hook = space.w_None
 
-    def getno(self):
-        self.no += 1
-        return self.no - 1
-
 def wrap_greenkey(space, jitdriver, greenkey, greenkey_repr):
     if greenkey is None:
         return space.w_None
@@ -220,6 +215,10 @@
         jit_hooks.resop_setresult(self.op, box.llbox)
 
 class DebugMergePoint(WrappedOp):
+    """ A class representing Debug Merge Point - the entry point
+    to a jitted loop.
+    """
+    
     def __init__(self, space, op, repr_of_resop, jd_name, call_depth, call_id,
         w_greenkey):
 
@@ -259,13 +258,78 @@
 DebugMergePoint.typedef = TypeDef(
     'DebugMergePoint', WrappedOp.typedef,
     __new__ = interp2app(descr_new_dmp),
-    greenkey = interp_attrproperty_w("w_greenkey", cls=DebugMergePoint),
+    __doc__ = DebugMergePoint.__doc__,
+    greenkey = interp_attrproperty_w("w_greenkey", cls=DebugMergePoint,
+               doc="Representation of place where the loop was compiled. "
+                    "In the case of the main interpreter loop, it's a triplet "
+                    "(code, ofs, is_profiled)"),
     pycode = GetSetProperty(DebugMergePoint.get_pycode),
-    bytecode_no = GetSetProperty(DebugMergePoint.get_bytecode_no),
-    call_depth = interp_attrproperty("call_depth", cls=DebugMergePoint),
-    call_id = interp_attrproperty("call_id", cls=DebugMergePoint),
-    jitdriver_name = GetSetProperty(DebugMergePoint.get_jitdriver_name),
+    bytecode_no = GetSetProperty(DebugMergePoint.get_bytecode_no,
+                                 doc="offset in the bytecode"),
+    call_depth = interp_attrproperty("call_depth", cls=DebugMergePoint,
+                                     doc="Depth of calls within this loop"),
+    call_id = interp_attrproperty("call_id", cls=DebugMergePoint,
+                     doc="Number of applevel function traced in this loop"),
+    jitdriver_name = GetSetProperty(DebugMergePoint.get_jitdriver_name,
+                     doc="Name of the jitdriver 'pypyjit' in the case "
+                                    "of the main interpreter loop"),
 )
 DebugMergePoint.acceptable_as_base_class = False
 
+class W_JitLoopInfo(Wrappable):
+    """ Loop debug information
+    """
+    
+    w_green_key = None
+    bridge_no   = 0
+    asmaddr     = 0
+    asmlen      = 0
+    
+    def __init__(self, space, debug_info, is_bridge=False):
+        logops = debug_info.logger._make_log_operations()
+        if debug_info.asminfo is not None:
+            ofs = debug_info.asminfo.ops_offset
+        else:
+            ofs = {}
+        self.w_ops = space.newlist(
+            wrap_oplist(space, logops, debug_info.operations, ofs))
+        
+        self.jd_name = debug_info.get_jitdriver().name
+        self.type = debug_info.type
+        if is_bridge:
+            self.bridge_no = debug_info.fail_descr_no
+            self.w_green_key = space.w_None
+        else:
+            self.w_green_key = wrap_greenkey(space,
+                                             debug_info.get_jitdriver(),
+                                             debug_info.greenkey,
+                                             debug_info.get_greenkey_repr())
+        self.loop_no = debug_info.looptoken.number
+        asminfo = debug_info.asminfo
+        if asminfo is not None:
+            self.asmaddr = asminfo.asmaddr
+            self.asmlen = asminfo.asmlen
 
+    def descr_repr(self, space):
+        lgt = space.int_w(space.len(self.w_ops))
+        if self.type == "bridge":
+            code_repr = 'bridge no %d' % self.bridge_no
+        else:
+            code_repr = space.str_w(space.repr(self.w_green_key))
+        return space.wrap('<JitLoopInfo %s, %d operations, starting at <%s>>' %
+                          (self.jd_name, lgt, code_repr))
+
+W_JitLoopInfo.typedef = TypeDef(
+    'JitLoopInfo',
+    __doc__ = W_JitLoopInfo.__doc__,
+    jitdriver_name = interp_attrproperty('jd_name', cls=W_JitLoopInfo,
+                       doc="Name of the JitDriver, pypyjit for the main one"),
+    greenkey       = interp_attrproperty_w('w_green_key', cls=W_JitLoopInfo,
+               doc="Representation of place where the loop was compiled. "
+                    "In the case of the main interpreter loop, it's a triplet "
+                    "(code, ofs, is_profiled)"),
+    operations     = interp_attrproperty_w('w_ops', cls=W_JitLoopInfo, doc=
+                    "List of operations in this loop."),
+    __repr__ = interp2app(W_JitLoopInfo.descr_repr),
+)
+W_JitLoopInfo.acceptable_as_base_class = False
diff --git a/pypy/module/pypyjit/policy.py b/pypy/module/pypyjit/policy.py
--- a/pypy/module/pypyjit/policy.py
+++ b/pypy/module/pypyjit/policy.py
@@ -2,8 +2,8 @@
 from pypy.rlib.jit import JitHookInterface, Counters
 from pypy.rlib import jit_hooks
 from pypy.interpreter.error import OperationError
-from pypy.module.pypyjit.interp_resop import wrap_oplist, Cache, wrap_greenkey,\
-     WrappedOp
+from pypy.module.pypyjit.interp_resop import Cache, wrap_greenkey,\
+     WrappedOp, W_JitLoopInfo
 
 class PyPyJitIface(JitHookInterface):
     def on_abort(self, reason, jitdriver, greenkey, greenkey_repr):
@@ -27,76 +27,46 @@
                 cache.in_recursion = False
 
     def after_compile(self, debug_info):
-        w_greenkey = wrap_greenkey(self.space, debug_info.get_jitdriver(),
-                                   debug_info.greenkey,
-                                   debug_info.get_greenkey_repr())
-        self._compile_hook(debug_info, w_greenkey)
+        self._compile_hook(debug_info, is_bridge=False)
 
     def after_compile_bridge(self, debug_info):
-        self._compile_hook(debug_info,
-                           self.space.wrap(debug_info.fail_descr_no))
+        self._compile_hook(debug_info, is_bridge=True)
 
     def before_compile(self, debug_info):
-        w_greenkey = wrap_greenkey(self.space, debug_info.get_jitdriver(),
-                                   debug_info.greenkey,
-                                   debug_info.get_greenkey_repr())
-        self._optimize_hook(debug_info, w_greenkey)
+        self._optimize_hook(debug_info, is_bridge=False)
 
     def before_compile_bridge(self, debug_info):
-        self._optimize_hook(debug_info,
-                            self.space.wrap(debug_info.fail_descr_no))
+        self._optimize_hook(debug_info, is_bridge=True)
 
-    def _compile_hook(self, debug_info, w_arg):
+    def _compile_hook(self, debug_info, is_bridge):
         space = self.space
         cache = space.fromcache(Cache)
-        # note that we *have to* get a number here always, even if we're in
-        # recursion
-        no = cache.getno()
         if cache.in_recursion:
             return
         if space.is_true(cache.w_compile_hook):
-            logops = debug_info.logger._make_log_operations()
-            list_w = wrap_oplist(space, logops, debug_info.operations,
-                                 debug_info.asminfo.ops_offset)
+            w_debug_info = W_JitLoopInfo(space, debug_info, is_bridge)
             cache.in_recursion = True
             try:
                 try:
-                    jd_name = debug_info.get_jitdriver().name
-                    asminfo = debug_info.asminfo
                     space.call_function(cache.w_compile_hook,
-                                        space.wrap(jd_name),
-                                        space.wrap(debug_info.type),
-                                        w_arg,
-                                        space.newlist(list_w),
-                                        space.wrap(no),
-                                        space.wrap(asminfo.asmaddr),
-                                        space.wrap(asminfo.asmlen))
+                                        space.wrap(w_debug_info))
                 except OperationError, e:
                     e.write_unraisable(space, "jit hook ", cache.w_compile_hook)
             finally:
                 cache.in_recursion = False
 
-    def _optimize_hook(self, debug_info, w_arg):
+    def _optimize_hook(self, debug_info, is_bridge=False):
         space = self.space
         cache = space.fromcache(Cache)
-        # note that we *have to* get a number here always, even if we're in
-        # recursion
-        no = cache.getno()
         if cache.in_recursion:
             return
         if space.is_true(cache.w_optimize_hook):
-            logops = debug_info.logger._make_log_operations()
-            list_w = wrap_oplist(space, logops, debug_info.operations)
+            w_debug_info = W_JitLoopInfo(space, debug_info, is_bridge)
             cache.in_recursion = True
             try:
                 try:
-                    jd_name = debug_info.get_jitdriver().name
                     w_res = space.call_function(cache.w_optimize_hook,
-                                                space.wrap(jd_name),
-                                                space.wrap(debug_info.type),
-                                                w_arg,
-                                                space.newlist(list_w),
-                                                space.wrap(no))
+                                                space.wrap(w_debug_info))
                     if space.is_w(w_res, space.w_None):
                         return
                     l = []
diff --git a/pypy/module/pypyjit/test/test_jit_hook.py b/pypy/module/pypyjit/test/test_jit_hook.py
--- a/pypy/module/pypyjit/test/test_jit_hook.py
+++ b/pypy/module/pypyjit/test/test_jit_hook.py
@@ -102,23 +102,22 @@
         import pypyjit
         all = []
 
-        def hook(name, looptype, tuple_or_guard_no, ops, loopno, asmstart,
-                 asmlen):
-            all.append((name, looptype, tuple_or_guard_no, ops, loopno))
+        def hook(info):
+            all.append(info)
 
         self.on_compile()
         pypyjit.set_compile_hook(hook)
         assert not all
         self.on_compile()
         assert len(all) == 1
-        elem = all[0]
-        assert elem[0] == 'pypyjit'
-        assert elem[2][0].co_name == 'function'
-        assert elem[2][1] == 0
-        assert elem[2][2] == False
-        assert len(elem[3]) == 4
-        int_add = elem[3][0]
-        dmp = elem[3][1]
+        info = all[0]
+        assert info.jitdriver_name == 'pypyjit'
+        assert info.greenkey[0].co_name == 'function'
+        assert info.greenkey[1] == 0
+        assert info.greenkey[2] == False
+        assert len(info.operations) == 4
+        int_add = info.operations[0]
+        dmp = info.operations[1]
         assert isinstance(dmp, pypyjit.DebugMergePoint)
         assert dmp.pycode is self.f.func_code
         assert dmp.greenkey == (self.f.func_code, 0, False)
@@ -127,6 +126,8 @@
         assert int_add.name == 'int_add'
         assert int_add.num == self.int_add_num
         self.on_compile_bridge()
+        code_repr = "(<code object function, file '?', line 2>, 0, False)"
+        assert repr(all[0]) == '<JitLoopInfo pypyjit, 4 operations, starting at <%s>>' % code_repr
         assert len(all) == 2
         pypyjit.set_compile_hook(None)
         self.on_compile()
@@ -168,12 +169,12 @@
         import pypyjit
         l = []
 
-        def hook(*args):
-            l.append(args)
+        def hook(info):
+            l.append(info)
 
         pypyjit.set_compile_hook(hook)
         self.on_compile()
-        op = l[0][3][1]
+        op = l[0].operations[1]
         assert isinstance(op, pypyjit.ResOperation)
         assert 'function' in repr(op)
 
@@ -192,17 +193,17 @@
         import pypyjit
         l = []
 
-        def hook(name, looptype, tuple_or_guard_no, ops, *args):
-            l.append(ops)
+        def hook(info):
+            l.append(info.jitdriver_name)
 
-        def optimize_hook(name, looptype, tuple_or_guard_no, ops, loopno):
+        def optimize_hook(info):
             return []
 
         pypyjit.set_compile_hook(hook)
         pypyjit.set_optimize_hook(optimize_hook)
         self.on_optimize()
         self.on_compile()
-        assert l == [[]]
+        assert l == ['pypyjit']
 
     def test_creation(self):
         from pypyjit import Box, ResOperation


More information about the pypy-commit mailing list