[pypy-svn] r53655 - in pypy/branch/jit-hotpath/pypy: rpython/lltypesystem rpython/ootypesystem rpython/test translator/backendopt/test
antocuni at codespeak.net
antocuni at codespeak.net
Thu Apr 10 15:03:23 CEST 2008
Author: antocuni
Date: Thu Apr 10 15:03:22 2008
New Revision: 53655
Modified:
pypy/branch/jit-hotpath/pypy/rpython/lltypesystem/lloperation.py
pypy/branch/jit-hotpath/pypy/rpython/ootypesystem/ooopimpl.py
pypy/branch/jit-hotpath/pypy/rpython/ootypesystem/rclass.py
pypy/branch/jit-hotpath/pypy/rpython/test/test_rclass.py
pypy/branch/jit-hotpath/pypy/translator/backendopt/test/test_constfold.py
Log:
teach backendopt how to constfold oogetfield
Modified: pypy/branch/jit-hotpath/pypy/rpython/lltypesystem/lloperation.py
==============================================================================
--- pypy/branch/jit-hotpath/pypy/rpython/lltypesystem/lloperation.py (original)
+++ pypy/branch/jit-hotpath/pypy/rpython/lltypesystem/lloperation.py Thu Apr 10 15:03:22 2008
@@ -459,7 +459,7 @@
'runtimenew': LLOp(oo=True, canraise=(MemoryError,)),
'oonewcustomdict': LLOp(oo=True, canraise=(MemoryError,)),
'oosetfield': LLOp(oo=True),
- 'oogetfield': LLOp(oo=True, sideeffects=False),
+ 'oogetfield': LLOp(oo=True, sideeffects=False, canrun=True),
'oosend': LLOp(oo=True, canraise=(Exception,)),
'ooupcast': LLOp(oo=True, canfold=True),
'oodowncast': LLOp(oo=True, canfold=True),
Modified: pypy/branch/jit-hotpath/pypy/rpython/ootypesystem/ooopimpl.py
==============================================================================
--- pypy/branch/jit-hotpath/pypy/rpython/ootypesystem/ooopimpl.py (original)
+++ pypy/branch/jit-hotpath/pypy/rpython/ootypesystem/ooopimpl.py Thu Apr 10 15:03:22 2008
@@ -49,6 +49,11 @@
def op_subclassof(class1, class2):
return ootype.subclassof(class1, class2)
+def op_oogetfield(inst, name):
+ checkinst(inst)
+ if not ootype.typeOf(inst)._hints.get('immutable'):
+ raise TypeError("cannot fold oogetfield on mutable struct")
+ return getattr(inst, name)
def is_inst(inst):
return isinstance(ootype.typeOf(inst), (ootype.Instance, ootype.BuiltinType, ootype.StaticMethod))
Modified: pypy/branch/jit-hotpath/pypy/rpython/ootypesystem/rclass.py
==============================================================================
--- pypy/branch/jit-hotpath/pypy/rpython/ootypesystem/rclass.py (original)
+++ pypy/branch/jit-hotpath/pypy/rpython/ootypesystem/rclass.py Thu Apr 10 15:03:22 2008
@@ -174,6 +174,9 @@
hints = classdef.classdesc.pyobj._rpython_hints
else:
hints = {}
+ if '_immutable_' in self.classdef.classdesc.classdict:
+ hints = hints.copy()
+ hints['immutable'] = True
self.lowleveltype = ootype.Instance(classdef.name, b, {}, {}, _hints = hints)
self.prebuiltinstances = {} # { id(x): (x, _ptr) }
self.object_type = self.lowleveltype
Modified: pypy/branch/jit-hotpath/pypy/rpython/test/test_rclass.py
==============================================================================
--- pypy/branch/jit-hotpath/pypy/rpython/test/test_rclass.py (original)
+++ pypy/branch/jit-hotpath/pypy/rpython/test/test_rclass.py Thu Apr 10 15:03:22 2008
@@ -632,6 +632,41 @@
return a.revealconst(1) + b.revealconst(2) + a.revealconst(3)
assert self.interpret(fn, []) == 3 + 8 + 9
+ def test_immutable(self):
+ class I(object):
+ _immutable_ = True
+
+ def __init__(self, v):
+ self.v = v
+
+ i = I(3)
+ def f():
+ return i.v
+
+ t, typer, graph = self.gengraph(f, [], backendopt=True)
+ assert summary(graph) == {}
+
+ def test_immutable_inheritance(self):
+ class I(object):
+ def __init__(self, v):
+ self.v = v
+
+ class J(I):
+ _immutable_ = True
+ def __init__(self, v, w):
+ self.w = w
+ I.__init__(self, v)
+
+ j = J(3, 4)
+ def f():
+ j.v = j.v * 1 # make the annotator think it is mutated
+ j.w = j.w * 1 # make the annotator think it is mutated
+ return j.v + j.w
+
+ t, typer, graph = self.gengraph(f, [], backendopt=True)
+ summ = summary(graph)
+ assert summ == {"setfield": 2} or summ == {"oosetfield": 2}
+
class TestLltype(BaseTestRclass, LLRtypeMixin):
@@ -699,40 +734,6 @@
assert destrptra is not None
assert destrptrb is not None
- def test_immutable(self):
- class I(object):
- _immutable_ = True
-
- def __init__(self, v):
- self.v = v
-
- i = I(3)
- def f():
- return i.v
-
- t, typer, graph = self.gengraph(f, [], backendopt=True)
- assert summary(graph) == {}
-
- def test_immutable_inheritance(self):
- class I(object):
- def __init__(self, v):
- self.v = v
-
- class J(I):
- _immutable_ = True
- def __init__(self, v, w):
- self.w = w
- I.__init__(self, v)
-
- j = J(3, 4)
- def f():
- j.v = j.v * 1 # make the annotator think it is mutated
- j.w = j.w * 1 # make the annotator think it is mutated
- return j.v + j.w
-
- t, typer, graph = self.gengraph(f, [], backendopt=True)
- assert summary(graph) == {"setfield": 2}
-
def test_instance_repr(self):
from pypy.rlib.objectmodel import current_object_addr_as_int
class FooBar(object):
Modified: pypy/branch/jit-hotpath/pypy/translator/backendopt/test/test_constfold.py
==============================================================================
--- pypy/branch/jit-hotpath/pypy/translator/backendopt/test/test_constfold.py (original)
+++ pypy/branch/jit-hotpath/pypy/translator/backendopt/test/test_constfold.py Thu Apr 10 15:03:22 2008
@@ -3,6 +3,7 @@
from pypy.translator.translator import TranslationContext, graphof
from pypy.rpython.llinterp import LLInterpreter
from pypy.rpython.lltypesystem import lltype
+from pypy.rpython.ootypesystem import ootype
from pypy.rpython.lltypesystem.lloperation import llop
from pypy.rlib import objectmodel
from pypy.translator.backendopt.constfold import constant_fold_graph
@@ -41,6 +42,21 @@
assert summary(graph) == {'direct_call': 1}
check_graph(graph, [], 124, t)
+def test_simple_ootype():
+ S1 = ootype.Instance('S1', ootype.ROOT, {'x': lltype.Signed}, _hints={'immutable': True})
+ s1 = ootype.new(S1)
+ s1.x = 123
+ def g(y):
+ return y + 1
+ def fn():
+ return g(s1.x)
+
+ graph, t = get_graph(fn, [])
+ assert summary(graph) == {'oogetfield': 1, 'direct_call': 1}
+ constant_fold_graph(graph)
+ assert summary(graph) == {'direct_call': 1}
+ check_graph(graph, [], 124, t)
+
def test_along_link():
S1 = lltype.GcStruct('S1', ('x', lltype.Signed), hints={'immutable': True})
More information about the pypy-svn
mailing list