[pypy-svn] r22852 - in pypy/dist/pypy/jit: . test

arigo at codespeak.net arigo at codespeak.net
Sun Jan 29 18:45:58 CET 2006


Author: arigo
Date: Sun Jan 29 18:45:55 2006
New Revision: 22852

Modified:
   pypy/dist/pypy/jit/hintannotator.py
   pypy/dist/pypy/jit/hintbookkeeper.py
   pypy/dist/pypy/jit/hintmodel.py
   pypy/dist/pypy/jit/test/test_hint_annotation.py
Log:
(arre, pedronis, arigo)

Started on specialization of graphs in the hint annotator.


Modified: pypy/dist/pypy/jit/hintannotator.py
==============================================================================
--- pypy/dist/pypy/jit/hintannotator.py	(original)
+++ pypy/dist/pypy/jit/hintannotator.py	Sun Jan 29 18:45:55 2006
@@ -5,10 +5,15 @@
 
 class HintAnnotator(RPythonAnnotator):
 
-    def __init__(self, translator, policy=None):
+    def __init__(self, translator=None, policy=None):
         RPythonAnnotator.__init__(self, translator, policy=policy)
         self.bookkeeper = HintBookkeeper(self) # XXX
 
+    def build_types(self, origgraph, input_args_hs):
+        desc = self.bookkeeper.getdesc(origgraph)
+        flowgraph = desc.specialize(input_args_hs)
+        return self.build_graph_types(flowgraph, input_args_hs)
+
     def consider_op_malloc(self, hs_TYPE):
         TYPE = hs_TYPE.const
         vstructdef = self.bookkeeper.getvirtualcontainerdef(TYPE)

Modified: pypy/dist/pypy/jit/hintbookkeeper.py
==============================================================================
--- pypy/dist/pypy/jit/hintbookkeeper.py	(original)
+++ pypy/dist/pypy/jit/hintbookkeeper.py	Sun Jan 29 18:45:55 2006
@@ -1,25 +1,74 @@
 from pypy.tool.tls import tlsobject
+from pypy.objspace.flow.model import copygraph
 
 TLS = tlsobject()
 
+
+class GraphDesc(object):
+
+    def __init__(self, bookkeeper, origgraph):
+        self.bookkeeper = bookkeeper
+        self.origgraph = origgraph
+        self._cache = {}
+
+    def specialize(self, input_args_hs):
+        from pypy.jit import hintmodel
+        # get the specialized graph -- for now, no specialization
+        graph = self.cachedgraph(None)
+
+        # modify input_args_hs in-place to change their origin
+        for i in range(len(input_args_hs)):
+            old = self.bookkeeper.enter((graph, i))
+            try:
+                input_args_hs[i] = hintmodel.reorigin(input_args_hs[i])
+            finally:
+                self.bookkeeper.leave(old)
+
+        return graph
+
+    def cachedgraph(self, key, alt_name=None):
+        try:
+            return self._cache[key]
+        except KeyError:
+            graph = copygraph(self.origgraph)
+            if alt_name is not None:
+                graph.name = alt_name
+            self._cache[key] = graph
+            self.bookkeeper.annotator.translator.graphs.append(graph)
+            return graph
+
+
 class HintBookkeeper(object):
 
     def __init__(self, hannotator):
         self.pending_specializations = []
         self.originflags = {}
         self.virtual_containers = {}
+        self.descs = {}
         self.annotator = hannotator
 
+    def getdesc(self, graph):
+        try:
+            return self.descs[graph]
+        except KeyError:
+            self.descs[graph] = desc = GraphDesc(self, graph)
+            return desc
+
     def enter(self, position_key):
         """Start of an operation.
         The operation is uniquely identified by the given key."""
+        res = getattr(self, 'position_key', None)
         self.position_key = position_key
         TLS.bookkeeper = self
+        return res
 
-    def leave(self):
+    def leave(self, old=None):
         """End of an operation."""
-        del TLS.bookkeeper
-        del self.position_key
+        if old is None:
+            del TLS.bookkeeper
+            del self.position_key
+        else:
+            self.position_key = old
 
     def myorigin(self):
         try:

Modified: pypy/dist/pypy/jit/hintmodel.py
==============================================================================
--- pypy/dist/pypy/jit/hintmodel.py	(original)
+++ pypy/dist/pypy/jit/hintmodel.py	Sun Jan 29 18:45:55 2006
@@ -164,11 +164,23 @@
         # normal call
         if not hasattr(fnobj, 'graph'):
             raise NotImplementedError("XXX call to externals or primitives")
-        hs_res = bookkeeper.annotator.recursivecall(fnobj.graph,
+        desc = bookkeeper.getdesc(fnobj.graph)
+        input_args_hs = list(args_hs)
+        graph = desc.specialize(input_args_hs)
+        hs_res = bookkeeper.annotator.recursivecall(graph,
                                                     bookkeeper.position_key,
-                                                    args_hs)
-        # for now, keep the origins of 'hs_res' in the new result:
-        return reorigin(hs_res, hs_res)
+                                                    input_args_hs)
+        # look on which input args the hs_res result depends on
+        if isinstance(hs_res, SomeLLAbstractConstant):
+            deps_hs = []
+            for hs_inputarg, hs_arg in zip(input_args_hs, args_hs):
+                if isinstance(hs_inputarg, SomeLLAbstractConstant):
+                    assert len(hs_inputarg.origins) == 1
+                    [o] = hs_inputarg.origins.keys()
+                    if o in hs_res.origins:
+                        deps_hs.append(hs_arg)
+            hs_res = reorigin(hs_res, *deps_hs)
+        return hs_res
 
     def unary_char(hs_c1):
         d = setadd(hs_c1.origins, getbookkeeper().myorigin())

Modified: pypy/dist/pypy/jit/test/test_hint_annotation.py
==============================================================================
--- pypy/dist/pypy/jit/test/test_hint_annotation.py	(original)
+++ pypy/dist/pypy/jit/test/test_hint_annotation.py	Sun Jan 29 18:45:55 2006
@@ -20,11 +20,11 @@
     rtyper.specialize()
     graph1 = graphof(t, func)
     # build hint annotator types
-    hannotator = HintAnnotator(t, policy=policy)
-    t.annotator = hannotator   # XXX?
-    hs = hannotator.build_graph_types(graph1, [SomeLLAbstractConstant(v.concretetype,
-                                                                      {OriginFlags(): True})
-                                               for v in graph1.getargs()])
+    hannotator = HintAnnotator(policy=policy)
+    hs = hannotator.build_types(graph1, [SomeLLAbstractConstant(v.concretetype,
+                                                                {OriginFlags(): True})
+                                         for v in graph1.getargs()])
+    t = hannotator.translator
     #t.view()
     if annotator:
         return hs, hannotator
@@ -209,14 +209,14 @@
     assert len(hs.origins) == 3
 
 def test_simple_call():
-    def ll2(x, y):
+    def ll2(x, y, z):
         return x + (y + 42)
     def ll1(x, y, z):
-        return ll2(x, y - z)
+        return ll2(x, y - z, x + y + z)
     hs = hannotate(ll1, [int, int, int])
     assert isinstance(hs, SomeLLAbstractConstant)
     assert hs.concretetype == lltype.Signed
-    assert len(hs.origins) == 7
+    assert len(hs.origins) == 5
 
 def test_simple_list_operations():
     def ll_function(x, y, index):



More information about the Pypy-commit mailing list