[pypy-commit] pypy better-jit-hooks: improve the hooks to be called before and after optimization

fijal noreply at buildbot.pypy.org
Sun Jan 8 19:03:28 CET 2012


Author: Maciej Fijalkowski <fijall at gmail.com>
Branch: better-jit-hooks
Changeset: r51136:6521c5b63450
Date: 2012-01-08 20:02 +0200
http://bitbucket.org/pypy/pypy/changeset/6521c5b63450/

Log:	improve the hooks to be called before and after optimization

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
@@ -305,6 +305,13 @@
         show_procedures(metainterp_sd, loop)
         loop.check_consistency()
 
+    if metainterp_sd.warmrunnerdesc is not None:
+        portal = metainterp_sd.warmrunnerdesc.portal
+        portal.before_compile(jitdriver_sd.jitdriver, metainterp_sd.logger_ops,
+                              original_jitcell_token, loop.operations, type,
+                              greenkey)
+    else:
+        portal = None
     operations = get_deep_immutable_oplist(loop.operations)
     metainterp_sd.profiler.start_backend()
     debug_start("jit-backend")
@@ -316,11 +323,10 @@
     finally:
         debug_stop("jit-backend")
     metainterp_sd.profiler.end_backend()
-    if metainterp_sd.warmrunnerdesc is not None:
-        portal = metainterp_sd.warmrunnerdesc.portal
-        portal.on_compile(jitdriver_sd.jitdriver, metainterp_sd.logger_ops,
-                          original_jitcell_token, loop.operations, type,
-                          greenkey, ops_offset, asmstart, asmlen)
+    if portal is not None:
+        portal.after_compile(jitdriver_sd.jitdriver, metainterp_sd.logger_ops,
+                             original_jitcell_token, loop.operations, type,
+                             greenkey, ops_offset, asmstart, asmlen)
     metainterp_sd.stats.add_new_loop(loop)
     if not we_are_translated():
         metainterp_sd.stats.compiled()
@@ -341,8 +347,15 @@
         show_procedures(metainterp_sd)
         seen = dict.fromkeys(inputargs)
         TreeLoop.check_consistency_of_branch(operations, seen)
+    if metainterp_sd.warmrunnerdesc is not None:
+        portal = metainterp_sd.warmrunnerdesc.portal
+        portal.before_compile_bridge(jitdriver_sd.jitdriver,
+                                     metainterp_sd.logger_ops,
+                                     original_loop_token, operations, n)
+    else:
+        portal = None
+    operations = get_deep_immutable_oplist(operations)
     metainterp_sd.profiler.start_backend()
-    operations = get_deep_immutable_oplist(operations)
     debug_start("jit-backend")
     try:
         tp = metainterp_sd.cpu.compile_bridge(faildescr, inputargs, operations,
@@ -351,12 +364,12 @@
     finally:
         debug_stop("jit-backend")
     metainterp_sd.profiler.end_backend()
-    if metainterp_sd.warmrunnerdesc is not None:
-        portal = metainterp_sd.warmrunnerdesc.portal
-        portal.on_compile_bridge(jitdriver_sd.jitdriver,
-                                 metainterp_sd.logger_ops,
-                                 original_loop_token, operations, n, ops_offset,
-                                 asmstart, asmlen)
+    if portal is not None:
+        portal.after_compile_bridge(jitdriver_sd.jitdriver,
+                                    metainterp_sd.logger_ops,
+                                    original_loop_token, operations, n,
+                                    ops_offset,
+                                    asmstart, asmlen)
     if not we_are_translated():
         metainterp_sd.stats.compiled()
     metainterp_sd.log("compiled new bridge")
diff --git a/pypy/jit/metainterp/test/test_jitportal.py b/pypy/jit/metainterp/test/test_jitportal.py
--- a/pypy/jit/metainterp/test/test_jitportal.py
+++ b/pypy/jit/metainterp/test/test_jitportal.py
@@ -41,14 +41,25 @@
         assert reasons == [ABORT_FORCE_QUASIIMMUT] * 2
 
     def test_on_compile(self):
-        called = {}
+        called = []
         
         class MyJitPortal(JitPortal):
-            def on_compile(self, jitdriver, logger, looptoken, operations,
-                           type, greenkey, ops_offset, asmaddr, asmlen):
+            def after_compile(self, jitdriver, logger, looptoken, operations,
+                              type, greenkey, ops_offset, asmaddr, asmlen):
                 assert asmaddr == 0
                 assert asmlen == 0
-                called[(greenkey[1].getint(), greenkey[0].getint(), type)] = looptoken
+                called.append(("compile", greenkey[1].getint(),
+                               greenkey[0].getint(), type))
+
+            def before_compile(self, jitdriver, logger, looptoken, oeprations,
+                               type, greenkey):
+                called.append(("optimize", greenkey[1].getint(),
+                               greenkey[0].getint(), type))
+
+            def before_optimize(self, jitdriver, logger, looptoken, oeprations,
+                               type, greenkey):
+                called.append(("trace", greenkey[1].getint(),
+                               greenkey[0].getint(), type))
 
         portal = MyJitPortal()
 
@@ -62,26 +73,35 @@
                 i += 1
 
         self.meta_interp(loop, [1, 4], policy=JitPolicy(portal))
-        assert sorted(called.keys()) == [(4, 1, "loop")]
+        assert called == [#("trace", 4, 1, "loop"),
+                          ("optimize", 4, 1, "loop"),
+                          ("compile", 4, 1, "loop")]
         self.meta_interp(loop, [2, 4], policy=JitPolicy(portal))
-        assert sorted(called.keys()) == [(4, 1, "loop"),
-                                         (4, 2, "loop")]
+        assert called == [#("trace", 4, 1, "loop"),
+                          ("optimize", 4, 1, "loop"),
+                          ("compile", 4, 1, "loop"),
+                          #("trace", 4, 2, "loop"),
+                          ("optimize", 4, 2, "loop"),
+                          ("compile", 4, 2, "loop")]
 
     def test_on_compile_bridge(self):
-        called = {}
+        called = []
         
         class MyJitPortal(JitPortal):
-            def on_compile(self, jitdriver, logger, looptoken, operations,
+            def after_compile(self, jitdriver, logger, looptoken, operations,
                            type, greenkey, ops_offset, asmaddr, asmlen):
                 assert asmaddr == 0
                 assert asmlen == 0
-                called[(greenkey[1].getint(), greenkey[0].getint(), type)] = looptoken
+                called.append("compile")
 
-            def on_compile_bridge(self, jitdriver, logger, orig_token,
-                                  operations, n, ops_offset, asmstart, asmlen):
-                assert 'bridge' not in called
-                called['bridge'] = orig_token
+            def after_compile_bridge(self, jitdriver, logger, orig_token,
+                                     operations, n, ops_offset, asmstart, asmlen):
+                called.append("compile_bridge")
 
+            def before_compile_bridge(self, jitdriver, logger, orig_token,
+                                     operations, n):
+                called.append("before_compile_bridge")
+            
         driver = JitDriver(greens = ['n', 'm'], reds = ['i'])
 
         def loop(n, m):
@@ -94,7 +114,7 @@
                 i += 1
 
         self.meta_interp(loop, [1, 10], policy=JitPolicy(MyJitPortal()))
-        assert sorted(called.keys()) == ['bridge', (10, 1, "loop")]
+        assert called == ["compile", "before_compile_bridge", "compile_bridge"]
 
     def test_resop_interface(self):
         driver = JitDriver(greens = [], reds = ['i'])
diff --git a/pypy/rlib/jit.py b/pypy/rlib/jit.py
--- a/pypy/rlib/jit.py
+++ b/pypy/rlib/jit.py
@@ -731,29 +731,62 @@
     like JIT loops compiled, aborts etc.
     An instance of this class might be returned by the policy.get_jit_portal
     method in order to function.
+
+    each hook will accept some of the following args:
+
+    
+    greenkey - a list of green boxes
+    jitdriver - an instance of jitdriver where tracing started
+    logger - an instance of jit.metainterp.logger.LogOperations
+    ops_offset
+    asmaddr - (int) raw address of assembler block
+    asmlen - assembler block length
+    type - either 'loop' or 'entry bridge'    
     """
     def on_abort(self, reason, jitdriver, greenkey):
         """ A hook called each time a loop is aborted with jitdriver and
         greenkey where it started, reason is a string why it got aborted
         """
 
-    def on_compile(self, jitdriver, logger, looptoken, operations, type,
-                   greenkey, ops_offset, asmaddr, asmlen):
-        """ A hook called when loop is compiled. Overwrite
-        for your own jitdriver if you want to do something special, like
-        call applevel code.
+    #def before_optimize(self, jitdriver, logger, looptoken, operations,
+    #                    type, greenkey):
+    #    """ A hook called before optimizer is run, args described in class
+    #    docstring. Overwrite for custom behavior
+    #    """
+    # DISABLED
 
-        jitdriver - an instance of jitdriver where tracing started
-        logger - an instance of jit.metainterp.logger.LogOperations
-        asmaddr - (int) raw address of assembler block
-        asmlen - assembler block length
-        type - either 'loop' or 'entry bridge'
+    def before_compile(self, jitdriver, logger, looptoken, operations, type,
+                       greenkey):
+        """ A hook called after a loop is optimized, before compiling assembler,
+        args described ni class docstring. Overwrite for custom behavior
         """
 
-    def on_compile_bridge(self, jitdriver, logger, orig_looptoken, operations,
-                          fail_descr_no, ops_offset, asmaddr, asmlen):
-        """ A hook called when a bridge is compiled. Overwrite
-        for your own jitdriver if you want to do something special
+    def after_compile(self, jitdriver, logger, looptoken, operations, type,
+                      greenkey, ops_offset, asmaddr, asmlen):
+        """ A hook called after a loop has compiled assembler,
+        args described in class docstring. Overwrite for custom behavior
+        """
+
+    #def before_optimize_bridge(self, jitdriver, logger, orig_looptoken,
+    #                           operations, fail_descr_no):
+    #    """ A hook called before a bridge is optimized.
+    #    Args described in class docstring, Overwrite for
+    #    custom behavior
+    #    """
+    # DISABLED
+
+    def before_compile_bridge(self, jitdriver, logger, orig_looptoken,
+                              operations, fail_descr_no):
+        """ A hook called before a bridge is compiled, but after optimizations
+        are performed. Args described in class docstring, Overwrite for
+        custom behavior
+        """
+
+    def after_compile_bridge(self, jitdriver, logger, orig_looptoken,
+                             operations, fail_descr_no, ops_offset, asmaddr,
+                             asmlen):
+        """ A hook called after a bridge is compiled, args described in class
+        docstring, Overwrite for custom behavior
         """
 
     def get_stats(self):


More information about the pypy-commit mailing list