[pypy-svn] r63263 - in pypy/branch/pyjitpl5-simplify/pypy/jit: backend/llgraph metainterp metainterp/test

arigo at codespeak.net arigo at codespeak.net
Mon Mar 23 22:35:37 CET 2009


Author: arigo
Date: Mon Mar 23 22:35:36 2009
New Revision: 63263

Modified:
   pypy/branch/pyjitpl5-simplify/pypy/jit/backend/llgraph/llimpl.py
   pypy/branch/pyjitpl5-simplify/pypy/jit/metainterp/compile.py
   pypy/branch/pyjitpl5-simplify/pypy/jit/metainterp/history.py
   pypy/branch/pyjitpl5-simplify/pypy/jit/metainterp/optimize.py
   pypy/branch/pyjitpl5-simplify/pypy/jit/metainterp/pyjitpl.py
   pypy/branch/pyjitpl5-simplify/pypy/jit/metainterp/test/test_optimize.py
Log:
Intermediate check-in.
Busy trying to have optimize.py work on bridges.


Modified: pypy/branch/pyjitpl5-simplify/pypy/jit/backend/llgraph/llimpl.py
==============================================================================
--- pypy/branch/pyjitpl5-simplify/pypy/jit/backend/llgraph/llimpl.py	(original)
+++ pypy/branch/pyjitpl5-simplify/pypy/jit/backend/llgraph/llimpl.py	Mon Mar 23 22:35:36 2009
@@ -319,6 +319,7 @@
     op = loop.operations[-1]
     op.jump_target = loop_target
     assert op.opnum == rop.JUMP
+    assert len(op.args) == len(loop_target.inputargs)
     if loop_target == loop:
         log.info("compiling new loop")
     else:

Modified: pypy/branch/pyjitpl5-simplify/pypy/jit/metainterp/compile.py
==============================================================================
--- pypy/branch/pyjitpl5-simplify/pypy/jit/metainterp/compile.py	(original)
+++ pypy/branch/pyjitpl5-simplify/pypy/jit/metainterp/compile.py	Mon Mar 23 22:35:36 2009
@@ -9,65 +9,60 @@
 from pypy.jit.metainterp import optimize
 
 
-def compile_new_loop(metainterp, old_loops, endliveboxes):
+def compile_new_loop(metainterp, old_loops):
     """Try to compile a new loop by closing the current history back
     to the first operation.
     """
-    loop = create_empty_loop(metainterp)
     if we_are_translated():
         try:
-            loop = compile_fresh_loop(metainterp, loop, old_loops,
-                                      endliveboxes)
+            loop = compile_fresh_loop(metainterp, old_loops)
             return loop
         except optimize.CancelInefficientLoop:
             return None
     else:
-        return _compile_new_loop_1(metainterp, loop, old_loops, endliveboxes)
+        return _compile_new_loop_1(metainterp, old_loops)
 
-def compile_new_bridge(metainterp, old_loops, endliveboxes, resumekey):
+def compile_new_bridge(metainterp, old_loops, resumekey):
     """Try to compile a new bridge leading from the beginning of the history
     to some existing place.
     """
     if we_are_translated():
         try:
             target_loop = compile_fresh_bridge(metainterp, old_loops,
-                                               endliveboxes, resumekey)
+                                               resumekey)
             return target_loop
         except optimize.CancelInefficientLoop:
             return None
     else:
-        return _compile_new_bridge_1(metainterp, old_loops,
-                                     endliveboxes, resumekey)
+        return _compile_new_bridge_1(metainterp, old_loops, resumekey)
 
 class BridgeInProgress(Exception):
     pass
 
 
 # the following is not translatable
-def _compile_new_loop_1(metainterp, loop, old_loops, endliveboxes):
-    orgloop = loop
+def _compile_new_loop_1(metainterp, old_loops):
     try:
         try:
-            loop = compile_fresh_loop(metainterp, loop, old_loops,
-                                      endliveboxes)
+            loop = compile_fresh_loop(metainterp, old_loops)
         except Exception, exc:
-            show_loop(metainterp, loop, error=exc)
+            show_loop(metainterp, error=exc)
             raise
         else:
-            if loop == orgloop:
-                show_loop(metainterp, loop)
-            else:
+            if loop in old_loops:
                 log.info("reusing loop at %r" % (loop,))
+            else:
+                show_loop(metainterp, loop)
     except optimize.CancelInefficientLoop:
         return None
     loop.check_consistency()
     return loop
 
-def _compile_new_bridge_1(metainterp, old_loops, endliveboxes, resumekey):
+def _compile_new_bridge_1(metainterp, old_loops, resumekey):
     try:
         try:
             target_loop = compile_fresh_bridge(metainterp, old_loops,
-                                               endliveboxes, resumekey)
+                                               resumekey)
         except Exception, exc:
             show_loop(metainterp, error=exc)
             raise
@@ -105,19 +100,17 @@
 
 # ____________________________________________________________
 
-def compile_fresh_loop(metainterp, loop, old_loops, endliveboxes):
-    # ---<temporary>---
-    if old_loops:
-        return old_loops[0]
-    # ---</temporary>---
+def compile_fresh_loop(metainterp, old_loops):
     history = metainterp.history
-    loop.inputargs = history.inputargs
-    loop.operations = history.operations
-    close_loop(loop, endliveboxes)
-    old_loop = optimize.optimize_loop(metainterp.options, old_loops, loop,
-                                      metainterp.cpu)
+    old_loop = optimize.optimize_loop(metainterp.options, old_loops,
+                                      history, metainterp.cpu)
     if old_loop is not None:
         return old_loop
+    loop = create_empty_loop(metainterp)
+    loop.inputargs = history.inputargs
+    loop.specnodes = history.specnodes
+    loop.operations = history.operations
+    loop.operations[-1].jump_target = loop
     mark_keys_in_loop(loop, loop.operations)
     send_loop_to_backend(metainterp, loop)
     metainterp.stats.loops.append(loop)
@@ -125,11 +118,6 @@
     old_loops.append(loop)
     return loop
 
-def close_loop(loop, endliveboxes):
-    op = ResOperation(rop.JUMP, endliveboxes, None)
-    op.jump_target = loop
-    loop.operations.append(op)
-
 def mark_keys_in_loop(loop, operations):
     for op in operations:
         if op.is_guard():
@@ -143,26 +131,16 @@
 
 # ____________________________________________________________
 
-def matching_merge_point(metainterp, targetmp, endliveboxes):
-    return True
-
-def compile_fresh_bridge(metainterp, old_loops, endliveboxes, resumekey):
-    history = metainterp.history
-    #
-    op = ResOperation(rop.JUMP, endliveboxes, None)
-    history.operations.append(op)
-    #
-    #old_loop = optimize.optimize_bridge(metainterp.options, old_loops, bridge,
-    #                                    metainterp.cpu)
-    #if old_loop is None:
-    #    return None
-    # ---<temporary>---
+def compile_fresh_bridge(metainterp, old_loops, resumekey):
+    old_loop = optimize.optimize_bridge(metainterp.options, old_loops,
+                                        metainterp.history, metainterp.cpu)
+    if old_loop is None:
+        return None
     target_loop = old_loops[0]
     op.jump_target = target_loop
-    # ---</temporary>---
     source_loop = resumekey.loop
     guard_op = resumekey.guard_op
-    guard_op.suboperations = history.operations
+    guard_op.suboperations = self.history.operations
     mark_keys_in_loop(source_loop, guard_op.suboperations)
     send_loop_to_backend(metainterp, source_loop)
     metainterp.stats.compiled_count += 1

Modified: pypy/branch/pyjitpl5-simplify/pypy/jit/metainterp/history.py
==============================================================================
--- pypy/branch/pyjitpl5-simplify/pypy/jit/metainterp/history.py	(original)
+++ pypy/branch/pyjitpl5-simplify/pypy/jit/metainterp/history.py	Mon Mar 23 22:35:36 2009
@@ -407,6 +407,7 @@
 
 
 class RunningMatcher(object):
+    specnodes = None
     def __init__(self, cpu):
         self.cpu = cpu
         self.inputargs = None

Modified: pypy/branch/pyjitpl5-simplify/pypy/jit/metainterp/optimize.py
==============================================================================
--- pypy/branch/pyjitpl5-simplify/pypy/jit/metainterp/optimize.py	(original)
+++ pypy/branch/pyjitpl5-simplify/pypy/jit/metainterp/optimize.py	Mon Mar 23 22:35:36 2009
@@ -189,7 +189,7 @@
         if self.virtualized:       flags += 'V'
         return "<InstanceNode %s (%s)>" % (self.source, flags)
 
-def optimize_loop(options, old_loops, loop, cpu=None):
+def optimize_loop(options, old_loops, history, cpu=None):
     if not options.specialize:         # for tests only
         if old_loops:
             return old_loops[0]
@@ -197,7 +197,7 @@
             return None
 
     # This does "Perfect specialization" as per doc/jitpl5.txt.
-    perfect_specializer = PerfectSpecializer(loop, options, cpu)
+    perfect_specializer = PerfectSpecializer(history, options, cpu)
     perfect_specializer.find_nodes()
     perfect_specializer.intersect_input_and_output()
     for old_loop in old_loops:
@@ -206,11 +206,11 @@
     perfect_specializer.optimize_loop()
     return None
 
-def optimize_bridge(options, old_loops, bridge, cpu=None):
+def optimize_bridge(options, old_loops, history, cpu=None):
     if not options.specialize:         # for tests only
         return old_loops[0]
 
-    perfect_specializer = PerfectSpecializer(bridge, options, cpu)
+    perfect_specializer = PerfectSpecializer(history, options, cpu)
     perfect_specializer.find_nodes()
     for old_loop in old_loops:
         if perfect_specializer.match(old_loop.operations):
@@ -221,8 +221,8 @@
 
 class PerfectSpecializer(object):
 
-    def __init__(self, loop, options=Options(), cpu=None):
-        self.loop = loop
+    def __init__(self, history, options=Options(), cpu=None):
+        self.history = history
         self.options = options
         self.cpu = cpu
         self.nodes = {}
@@ -279,9 +279,9 @@
     def find_nodes(self):
         # Steps (1) and (2)
         self.first_escaping_op = True
-        for box in self.loop.inputargs:
+        for box in self.history.inputargs:
             self.nodes[box] = InstanceNode(box, escaped=False, startbox=True)
-        for op in self.loop.operations:
+        for op in self.history.operations:
             #print '| ' + op.repr()
             opnum = op.opnum
             if opnum == rop.JUMP:
@@ -388,15 +388,15 @@
                 self.nodes[box] = InstanceNode(box, escaped=True)
 
     def recursively_find_escaping_values(self):
-        end_args = self.loop.operations[-1].args
-        assert len(self.loop.inputargs) == len(end_args)
+        end_args = self.history.operations[-1].args
+        assert len(self.history.inputargs) == len(end_args)
         memo = {}
         for i in range(len(end_args)):
             end_box = end_args[i]
             if isinstance(end_box, Box):
                 self.nodes[end_box].escape_if_startbox(memo)
         for i in range(len(end_args)):
-            box = self.loop.inputargs[i]
+            box = self.history.inputargs[i]
             other_box = end_args[i]
             if isinstance(other_box, Box):
                 self.nodes[box].add_to_dependency_graph(self.nodes[other_box],
@@ -414,11 +414,11 @@
     def intersect_input_and_output(self):
         # Step (3)
         self.recursively_find_escaping_values()
-        jump = self.loop.operations[-1]
+        jump = self.history.operations[-1]
         assert jump.opnum == rop.JUMP
         specnodes = []
-        for i in range(len(self.loop.inputargs)):
-            enternode = self.nodes[self.loop.inputargs[i]]
+        for i in range(len(self.history.inputargs)):
+            enternode = self.nodes[self.history.inputargs[i]]
             leavenode = self.getnode(jump.args[i])
             specnodes.append(enternode.intersect(leavenode, self.nodes))
         self.specnodes = specnodes
@@ -581,18 +581,18 @@
     def optimize_loop(self):
         newoperations = []
         exception_might_have_happened = False
-        assert len(self.loop.inputargs) == len(self.specnodes)
+        assert len(self.history.inputargs) == len(self.specnodes)
         for i in range(len(self.specnodes)):
-            box = self.loop.inputargs[i]
+            box = self.history.inputargs[i]
             self.specnodes[i].mutate_nodes(self.nodes[box])
-        newinputargs = self.expanded_version_of(self.loop.inputargs, None)
+        newinputargs = self.expanded_version_of(self.history.inputargs, None)
 
 ##        assert mp.opnum == rop.CATCH
 ##        for box in mp.args:
 ##            self.nodes[box].cls = None
 ##            assert not self.nodes[box].virtual
 
-        for op in self.loop.operations:
+        for op in self.history.operations:
             opnum = op.opnum
             if opnum == rop.JUMP:
                 args = self.expanded_version_of(op.args, newoperations)
@@ -602,7 +602,7 @@
                 self.cleanup_field_caches(newoperations)
                 op.args = args
                 newoperations.append(op)
-                continue
+                break
             elif opnum == rop.GUARD_NO_EXCEPTION:
                 if not exception_might_have_happened:
                     continue
@@ -750,9 +750,9 @@
                 self.nodes[box] = instnode
             newoperations.append(op)
 
-        self.loop.specnodes = self.specnodes
-        self.loop.inputargs = newinputargs
-        self.loop.operations = newoperations
+        self.history.specnodes = self.specnodes
+        self.history.inputargs = newinputargs
+        self.history.operations = newoperations
 
     def cleanup_field_caches(self, newoperations):
         # we need to invalidate everything
@@ -776,17 +776,16 @@
             node.cleanfields = r_dict(av_eq, av_hash)
 
     def match_exactly(self, old_loop):
-        old_operations = old_loop.operations
-        old_mp = old_operations[0]
-        assert len(old_mp.specnodes) == len(self.specnodes)
+        assert len(old_loop.specnodes) == len(self.specnodes)
         for i in range(len(self.specnodes)):
-            old_specnode = old_mp.specnodes[i]
+            old_specnode = old_loop.specnodes[i]
             new_specnode = self.specnodes[i]
             if not old_specnode.equals(new_specnode):
                 return False
         return True
 
     def match(self, old_operations):
+        xxx
         old_mp = old_operations[0]
         jump_op = self.loop.operations[-1]
         assert old_op.opnum == rop.MERGE_POINT

Modified: pypy/branch/pyjitpl5-simplify/pypy/jit/metainterp/pyjitpl.py
==============================================================================
--- pypy/branch/pyjitpl5-simplify/pypy/jit/metainterp/pyjitpl.py	(original)
+++ pypy/branch/pyjitpl5-simplify/pypy/jit/metainterp/pyjitpl.py	Mon Mar 23 22:35:36 2009
@@ -857,8 +857,8 @@
         self.history.inputargs = original_boxes[num_green_args:]
         greenkey = original_boxes[:num_green_args]
         old_loops = self.compiled_merge_points.setdefault(greenkey, [])
-        loop = compile_new_loop(self, old_loops,
-                                live_arg_boxes[num_green_args:])
+        self.history.record(rop.JUMP, live_arg_boxes[num_green_args:], None)
+        loop = compile_new_loop(self, old_loops)
         if not loop:
             raise self.ContinueRunningNormally(live_arg_boxes)
         if not we_are_translated():
@@ -874,9 +874,9 @@
         except KeyError:
             target_loop = None
         else:
-            target_loop = compile_new_bridge(self, old_loops,
-                                             live_arg_boxes[num_green_args:],
-                                             key)
+            self.history.record(rop.JUMP, live_arg_boxes[num_green_args:],
+                                None)
+            target_loop = compile_new_bridge(self, old_loops, key)
         if target_loop is None:
             raise self.ContinueRunningNormally(live_arg_boxes)
         if not we_are_translated():
@@ -931,8 +931,13 @@
         # guard failure: rebuild a complete MIFrame stack
         if self.state.must_compile_from_failure(guard_failure.key):
             self.history = history.History(self.cpu)
+            suboperations = guard_failure.key.guard_op.suboperations
+            for i in range(len(suboperations)-1):
+                self.history.operations.append(suboperations[i])
         else:
             self.history = history.BlackHole(self.cpu)
+            # the BlackHole is invalid because it doesn't start with
+            # guard_failure.key.guard_op.suboperations, but that's fine
         self.rebuild_state_after_failure(guard_failure.key.resume_info,
                                          guard_failure.args)
 

Modified: pypy/branch/pyjitpl5-simplify/pypy/jit/metainterp/test/test_optimize.py
==============================================================================
--- pypy/branch/pyjitpl5-simplify/pypy/jit/metainterp/test/test_optimize.py	(original)
+++ pypy/branch/pyjitpl5-simplify/pypy/jit/metainterp/test/test_optimize.py	Mon Mar 23 22:35:36 2009
@@ -34,10 +34,11 @@
 
 # ____________________________________________________________
 
-class Loop(object):
-    def __init__(self, inputargs, operations):
-        self.inputargs = inputargs[:]
-        self.operations = [op.clone() for op in operations]
+def Loop(inputargs, operations):
+    history = History(cpu)
+    history.inputargs = inputargs[:]
+    history.operations = [op.clone() for op in operations]
+    return history
 
 class Any(object):
     def __eq__(self, other):



More information about the Pypy-commit mailing list