[pypy-svn] r40619 - in pypy/branch/jit-virtual-world/pypy/jit/hintannotator: . test

arigo at codespeak.net arigo at codespeak.net
Fri Mar 16 21:26:11 CET 2007


Author: arigo
Date: Fri Mar 16 21:26:09 2007
New Revision: 40619

Modified:
   pypy/branch/jit-virtual-world/pypy/jit/hintannotator/model.py
   pypy/branch/jit-virtual-world/pypy/jit/hintannotator/test/test_annotator.py
Log:
Constant-folding superpowers for the hintannotator.
Used to fold we_are_jitted() paths.


Modified: pypy/branch/jit-virtual-world/pypy/jit/hintannotator/model.py
==============================================================================
--- pypy/branch/jit-virtual-world/pypy/jit/hintannotator/model.py	(original)
+++ pypy/branch/jit-virtual-world/pypy/jit/hintannotator/model.py	Fri Mar 16 21:26:09 2007
@@ -714,26 +714,38 @@
     RESTYPE = getbookkeeper().current_op_concretetype()
     return SomeLLAbstractVariable(RESTYPE)
 
-def const_unary(hs_c1):
+def const_unary(llop, hs_c1):
     #XXX unsure hacks
     bk = getbookkeeper()
     origin = bk.myorigin()
     d = setadd(hs_c1.origins, origin)
     RESTYPE = bk.current_op_concretetype()
-    return SomeLLAbstractConstant(RESTYPE, d,
-                                  eager_concrete = hs_c1.eager_concrete,
-                                  myorigin = origin)
+    hs_res = SomeLLAbstractConstant(RESTYPE, d,
+                                    eager_concrete = hs_c1.eager_concrete,
+                                    myorigin = origin)
+    if hs_c1.is_constant():
+        try:
+            hs_res.const = llop(RESTYPE, hs_c1.const)
+        except Exception:   # XXX not too nice
+            pass
+    return hs_res
 
-def const_binary((hs_c1, hs_c2)):
+def const_binary(llop, (hs_c1, hs_c2)):
     #XXX unsure hacks
     bk = getbookkeeper()
     origin = bk.myorigin()
     d = newset(hs_c1.origins, hs_c2.origins, {origin: True})
     RESTYPE = bk.current_op_concretetype()
-    return SomeLLAbstractConstant(RESTYPE, d,
-                                  eager_concrete = hs_c1.eager_concrete or
-                                                   hs_c2.eager_concrete,
-                                  myorigin = origin)
+    hs_res = SomeLLAbstractConstant(RESTYPE, d,
+                                    eager_concrete = hs_c1.eager_concrete or
+                                                     hs_c2.eager_concrete,
+                                    myorigin = origin)
+    if hs_c1.is_constant() and hs_c2.is_constant():
+        try:
+            hs_res.const = llop(RESTYPE, hs_c1.const, hs_c2.const)
+        except Exception:   # XXX not too nice
+            pass
+    return hs_res
 
 def setup(oplist, ValueCls, var_fn, ConstantCls, const_fn):
     for name in oplist:
@@ -743,7 +755,8 @@
                 setattr(ValueCls, name, var_fn)
             if llop.canfold or llop.tryfold:
                 if name not in ConstantCls.__dict__:
-                    setattr(ConstantCls, name, const_fn)
+                    setattr(ConstantCls, name,
+                            lambda s, llop=llop: const_fn(llop, s))
 setup(UNARY_OPERATIONS,
       SomeLLAbstractValue, var_unary,
       SomeLLAbstractConstant, const_unary)

Modified: pypy/branch/jit-virtual-world/pypy/jit/hintannotator/test/test_annotator.py
==============================================================================
--- pypy/branch/jit-virtual-world/pypy/jit/hintannotator/test/test_annotator.py	(original)
+++ pypy/branch/jit-virtual-world/pypy/jit/hintannotator/test/test_annotator.py	Fri Mar 16 21:26:09 2007
@@ -5,7 +5,7 @@
 from pypy.jit.hintannotator.bookkeeper import HintBookkeeper
 from pypy.jit.hintannotator.model import *
 from pypy.rpython.lltypesystem import lltype
-from pypy.rlib.objectmodel import hint
+from pypy.rlib.objectmodel import hint, we_are_jitted
 from pypy.annotation import model as annmodel
 from pypy.objspace.flow import model as flowmodel
 from pypy.translator.backendopt.inline import auto_inlining
@@ -856,3 +856,20 @@
     assert hs.is_green()
     hs = hannotator.binding(tsgraph.getargs()[1])
     assert hs.is_green()
+
+def test_ignore_nonjitted_path():
+    def f(n):
+        if we_are_jitted():
+            return 5
+        else:
+            return n
+    hs = hannotate(f, [int])
+    assert hs.is_green()
+
+    def g(n):
+        if not we_are_jitted():
+            return n
+        else:
+            return 5
+    hs = hannotate(g, [int])
+    assert hs.is_green()



More information about the Pypy-commit mailing list