[pypy-svn] r65592 - in pypy/branch/pyjitpl5-experiments/pypy/jit/metainterp: . test

antocuni at codespeak.net antocuni at codespeak.net
Thu Jun 4 22:18:24 CEST 2009


Author: antocuni
Date: Thu Jun  4 22:18:23 2009
New Revision: 65592

Modified:
   pypy/branch/pyjitpl5-experiments/pypy/jit/metainterp/optimize3.py
   pypy/branch/pyjitpl5-experiments/pypy/jit/metainterp/test/test_optimize3.py
Log:
start porting perfect specialization from optimize.py to optimize3.py: so far
I ported just the bits needed to pass test_virtualizable_simple_find_nodes
(which has been ported from test_optimize.test_A_find_nodes, but it seems the
coverage is bad :-/



Modified: pypy/branch/pyjitpl5-experiments/pypy/jit/metainterp/optimize3.py
==============================================================================
--- pypy/branch/pyjitpl5-experiments/pypy/jit/metainterp/optimize3.py	(original)
+++ pypy/branch/pyjitpl5-experiments/pypy/jit/metainterp/optimize3.py	Thu Jun  4 22:18:23 2009
@@ -1,12 +1,13 @@
 from pypy.jit.metainterp.resoperation import rop, ResOperation
-from pypy.jit.metainterp.history import Const, Box
+from pypy.jit.metainterp.history import Const, Box, AbstractValue
 
 class InstanceNode(object):
-    def __init__(self, source, const=False):
+    def __init__(self, source, const=False, escaped=True):
         self.source = source
         if const:
             assert isinstance(source, Const)
         self.const = const
+        self.escaped = escaped
         self.cls = None
 
     def __repr__(self):
@@ -38,6 +39,10 @@
             return node
 
     def find_nodes(self):
+        for box in self.loop.inputargs:
+            self.nodes[box] = InstanceNode(box, escaped=False,)
+                                           #startbox=True)
+
         for op in self.loop.operations:
             self._find_nodes_in_guard_maybe(op)
             if self._is_pure_and_constfoldable(op):
@@ -136,11 +141,14 @@
         op.suboperations = [op_fail]
         return op
 
-    def optimize_loop(self, loop):
+    def _init(self, loop):
         self.nodes = {}
         self.field_caches = {}
         self.fixedops = {}
         self.loop = loop
+
+    def optimize_loop(self, loop):
+        self._init(loop)
         self.find_nodes()
         self.optimize_operations()
 
@@ -211,6 +219,47 @@
 
 
 
+class OptimizeVirtuals(AbstractOptimization):
+
+    def find_nodes_guard_class(self, spec, op):
+        # XXX: how does this relate to OptimizeGuards.guard_class?
+        instnode = spec.getnode(op.args[0])
+        if instnode.cls is None:
+            instnode.cls = InstanceNode(op.args[1], const=True)
+
+    def find_nodes_new_with_vtable(self, spec, op):
+        box = op.result
+        instnode = InstanceNode(box, escaped=False)
+        instnode.cls = InstanceNode(op.args[0], const=True)
+        spec.nodes[box] = instnode
+
+##     def find_nodes_setfield_gc(self, spec, op):
+##         instnode = spec.getnode(op.args[0])
+##         fielddescr = op.descr
+##         fieldnode = self.getnode(op.args[1])
+##         assert isinstance(fielddescr, AbstractValue)
+##         instnode.curfields[fielddescr] = fieldnode
+##         ##self.dependency_graph.append((instnode, fieldnode))
+
+##     def find_nodes_getfield_gc(self, spec, op):
+##         import pdb;pdb.set_trace()
+##         instnode = spec.getnode(op.args[0])
+##         fielddescr = op.descr
+##         resbox = op.result
+##         assert isinstance(fielddescr, AbstractValue)
+##         if fielddescr in instnode.curfields:
+##             fieldnode = instnode.curfields[fielddescr]
+##         elif fielddescr in instnode.origfields:
+##             fieldnode = instnode.origfields[fielddescr]
+##         else:
+##             fieldnode = InstanceNode(resbox, escaped=False)
+##             if instnode.startbox:
+##                 fieldnode.startbox = True
+##             ##self.dependency_graph.append((instnode, fieldnode))
+##             instnode.origfields[fielddescr] = fieldnode
+##         self.nodes[resbox] = fieldnode
+
+
 # -------------------------------------------------------------------
 
 OPTLIST = [

Modified: pypy/branch/pyjitpl5-experiments/pypy/jit/metainterp/test/test_optimize3.py
==============================================================================
--- pypy/branch/pyjitpl5-experiments/pypy/jit/metainterp/test/test_optimize3.py	(original)
+++ pypy/branch/pyjitpl5-experiments/pypy/jit/metainterp/test/test_optimize3.py	Thu Jun  4 22:18:23 2009
@@ -10,7 +10,7 @@
 
 from pypy.jit.metainterp.optimize3 import AbstractOptimization
 from pypy.jit.metainterp.optimize3 import optimize_loop, Specializer,\
-     OptimizeGuards
+     OptimizeGuards, OptimizeVirtuals
 from pypy.jit.metainterp.test.test_optimize import equaloplists, ANY
 from pypy.jit.metainterp.test.oparser import parse
 
@@ -58,7 +58,9 @@
                                         ('next', lltype.Ptr(NODE))))
     node = lltype.malloc(NODE)
     nodebox = BoxPtr(lltype.cast_opaque_ptr(llmemory.GCREF, node))
-    nodedescr = cpu.fielddescrof(NODE, 'value')
+    nodebox2 = BoxPtr(lltype.cast_opaque_ptr(llmemory.GCREF, node))
+    nodesize = cpu.sizeof(NODE)
+    valuedescr = cpu.fielddescrof(NODE, 'value')
     namespace = locals()
 
 class OOtypeMixin(object):
@@ -71,10 +73,13 @@
                       'next': NODE})
 
     node_vtable = ootype.runtimeClass(NODE)
+    node_vtable_adr = ootype.cast_to_object(node_vtable)
 
     node = ootype.new(NODE)
     nodebox = BoxObj(ootype.cast_to_object(node))
-    nodedescr = cpu.fielddescrof(NODE, 'value')
+    nodebox2 = BoxObj(ootype.cast_to_object(node))
+    valuedescr = cpu.fielddescrof(NODE, 'value')
+    nodesize = cpu.typedescrof(NODE)
 
     namespace = locals()
 
@@ -172,6 +177,47 @@
         loop = self.optimize(loop, [OptimizeGuards()])
         self.assert_equal(loop, expected)
 
+    def _get_virtual_simple_loop(self):
+        ops = """
+        [i0, p0]
+        guard_class(p0, ConstClass(node_vtable))
+          fail()
+        i1 = getfield_gc(p0, descr=valuedescr)
+        i2 = int_sub(i1, 1)
+        i3 = int_add(i0, i1)
+        p1 = new_with_vtable(ConstClass(node_vtable), descr=nodesize)
+        setfield_gc(p1, i2, descr=valuedescr)
+        jump(i3, p1)
+        """
+        loop = self.parse(ops)
+        # cheat
+        loop.inputargs[1].value = self.nodebox.value # p0
+        loop.operations[1].result.value = 20         # i1
+        loop.operations[2].result.value = 19         # i2
+        loop.operations[3].result.value = 20         # i3
+        loop.operations[4].result = self.nodebox2    # p1
+        loop.operations[5].args[0] = self.nodebox2   # p1
+        loop.operations[6].args[1] = self.nodebox2   # p1
+        loop.operations[6].jump_target = loop
+        return loop
+
+    def test_virtual_simple_find_nodes(self):
+        loop = self._get_virtual_simple_loop()
+        spec = Specializer([OptimizeVirtuals()])
+        spec._init(loop)
+        spec.find_nodes()
+
+        i0, p0 = loop.inputargs
+        i3 = loop.operations[3].result
+        p1 = loop.operations[4].result
+        assert spec.nodes[i0] is not spec.nodes[i3]
+        assert spec.nodes[p0] is not spec.nodes[p1]
+        assert spec.nodes[p0].cls.source.value == self.node_vtable_adr
+        assert not spec.nodes[p0].escaped
+        assert spec.nodes[p1].cls.source.value == self.node_vtable_adr
+        assert not spec.nodes[p1].escaped
+
+
 
 class TestLLtype(LLtypeMixin, BaseTestOptimize3):
     pass



More information about the Pypy-commit mailing list