[pypy-commit] pypy kill-ootype: Fix rpython/translator/backendopt
rlamy
noreply at buildbot.pypy.org
Sun Jul 7 14:22:28 CEST 2013
Author: Ronan Lamy <ronan.lamy at gmail.com>
Branch: kill-ootype
Changeset: r65254:8fb078df2c3d
Date: 2013-07-07 14:19 +0200
http://bitbucket.org/pypy/pypy/changeset/8fb078df2c3d/
Log: Fix rpython/translator/backendopt
diff --git a/rpython/translator/backendopt/malloc.py b/rpython/translator/backendopt/malloc.py
--- a/rpython/translator/backendopt/malloc.py
+++ b/rpython/translator/backendopt/malloc.py
@@ -537,85 +537,6 @@
raise AssertionError(op.opname)
-class OOTypeMallocRemover(BaseMallocRemover):
-
- IDENTITY_OPS = ('same_as', 'ooupcast', 'oodowncast')
- SUBSTRUCT_OPS = ()
- MALLOC_OP = 'new'
- FIELD_ACCESS = dict.fromkeys(["oogetfield",
- "oosetfield",
- "oononnull",
- "ooisnull",
- #"oois", # ???
- #"instanceof", # ???
- ])
- SUBSTRUCT_ACCESS = {}
- CHECK_ARRAY_INDEX = {}
-
- def get_STRUCT(self, TYPE):
- return TYPE
-
- def union_wrapper(self, S):
- return False
-
- def RTTI_dtor(self, STRUCT):
- return False
-
- def inline_type(self, TYPE):
- return isinstance(TYPE, (ootype.Record, ootype.Instance))
-
- def _get_fields(self, TYPE):
- if isinstance(TYPE, ootype.Record):
- return TYPE._fields
- elif isinstance(TYPE, ootype.Instance):
- return TYPE._allfields()
- else:
- assert False
-
- def flatten(self, TYPE):
- for name, (FIELDTYPE, default) in self._get_fields(TYPE).iteritems():
- key = self.key_for_field_access(TYPE, name)
- constant = Constant(default)
- constant.concretetype = FIELDTYPE
- self.flatconstants[key] = constant
- self.flatnames.append(key)
- self.newvarstype[key] = FIELDTYPE
-
- def key_for_field_access(self, S, fldname):
- CLS, TYPE = S._lookup_field(fldname)
- return CLS, fldname
-
- def flowin_op(self, op, vars, newvarsmap):
- if op.opname == "oogetfield":
- S = op.args[0].concretetype
- fldname = op.args[1].value
- key = self.key_for_field_access(S, fldname)
- newop = SpaceOperation("same_as",
- [newvarsmap[key]],
- op.result)
- self.newops.append(newop)
- elif op.opname == "oosetfield":
- S = op.args[0].concretetype
- fldname = op.args[1].value
- key = self.key_for_field_access(S, fldname)
- assert key in newvarsmap
- newvarsmap[key] = op.args[2]
- elif op.opname in ("same_as", "oodowncast", "ooupcast"):
- vars[op.result] = True
- # Consider the two pointers (input and result) as
- # equivalent. We can, and indeed must, use the same
- # flattened list of variables for both, as a "setfield"
- # via one pointer must be reflected in the other.
- elif op.opname in ("ooisnull", "oononnull"):
- # we know the pointer is not NULL if it comes from
- # a successful malloc
- c = Constant(op.opname == "oononnull", lltype.Bool)
- newop = SpaceOperation('same_as', [c], op.result)
- self.newops.append(newop)
- else:
- raise AssertionError(op.opname)
-
-
def remove_simple_mallocs(graph, type_system='lltypesystem', verbose=True):
if type_system == 'lltypesystem':
remover = LLTypeMallocRemover(verbose)
diff --git a/rpython/translator/backendopt/test/test_all.py b/rpython/translator/backendopt/test/test_all.py
--- a/rpython/translator/backendopt/test/test_all.py
+++ b/rpython/translator/backendopt/test/test_all.py
@@ -2,11 +2,9 @@
from rpython.translator.backendopt.all import backend_optimizations
from rpython.translator.backendopt.all import INLINE_THRESHOLD_FOR_TEST
from rpython.translator.backendopt.support import md5digest
-from rpython.translator.backendopt.test.test_malloc import TestLLTypeMallocRemoval as LLTypeMallocRemovalTest
-from rpython.translator.backendopt.test.test_malloc import TestOOTypeMallocRemoval as OOTypeMallocRemovalTest
+from rpython.translator.backendopt.test.test_malloc import TestMallocRemoval as MallocRemovalTest
from rpython.translator.translator import TranslationContext, graphof
from rpython.flowspace.model import Constant, summary
-from rpython.annotator import model as annmodel
from rpython.rtyper.llinterp import LLInterpreter
from rpython.rlib.rarithmetic import intmask
from rpython.conftest import option
@@ -43,8 +41,9 @@
LARGE_THRESHOLD = 10*INLINE_THRESHOLD_FOR_TEST
HUGE_THRESHOLD = 100*INLINE_THRESHOLD_FOR_TEST
-class BaseTester(object):
- type_system = None
+class TestLLType(object):
+ type_system = 'lltype'
+ check_malloc_removed = MallocRemovalTest.check_malloc_removed
def translateopt(self, func, sig, **optflags):
t = TranslationContext()
@@ -61,7 +60,7 @@
assert big() == 83
t = self.translateopt(big, [], inline_threshold=HUGE_THRESHOLD,
- mallocs=True)
+ mallocs=True)
big_graph = graphof(t, big)
self.check_malloc_removed(big_graph)
@@ -128,7 +127,7 @@
return res
def g(x):
- return s(100) + s(1) + x
+ return s(100) + s(1) + x
def idempotent(n1, n2):
c = [i for i in range(n2)]
@@ -232,10 +231,6 @@
graph = graphof(t, fn)
assert "direct_call" not in summary(graph)
-class TestLLType(BaseTester):
- type_system = 'lltype'
- check_malloc_removed = LLTypeMallocRemovalTest.check_malloc_removed
-
def test_list_comp(self):
def f(n1, n2):
c = [i for i in range(n2)]
@@ -296,9 +291,3 @@
llinterp = LLInterpreter(t.rtyper)
res = llinterp.eval_graph(later_graph, [10])
assert res == 1
-
-
-class TestOOType(BaseTester):
- type_system = 'ootype'
- check_malloc_removed = OOTypeMallocRemovalTest.check_malloc_removed
-
diff --git a/rpython/translator/backendopt/test/test_canraise.py b/rpython/translator/backendopt/test/test_canraise.py
--- a/rpython/translator/backendopt/test/test_canraise.py
+++ b/rpython/translator/backendopt/test/test_canraise.py
@@ -3,16 +3,13 @@
from rpython.translator.simplify import get_funcobj
from rpython.translator.backendopt.canraise import RaiseAnalyzer
from rpython.translator.backendopt.all import backend_optimizations
-from rpython.rtyper.test.tool import LLRtypeMixin, OORtypeMixin
from rpython.conftest import option
-class BaseTestCanRaise(object):
- type_system = None
-
+class TestCanRaise(object):
def translate(self, func, sig):
t = TranslationContext()
t.buildannotator().build_types(func, sig)
- t.buildrtyper(type_system=self.type_system).specialize()
+ t.buildrtyper(type_system='lltype').specialize()
if option.view:
t.view()
return t, RaiseAnalyzer(t)
@@ -136,7 +133,7 @@
obj = B()
f(obj)
m(obj)
-
+
t, ra = self.translate(h, [int])
hgraph = graphof(t, h)
# fiiiish :-(
@@ -179,7 +176,7 @@
# an indirect call without a list of graphs
from rpython.rlib.objectmodel import instantiate
class A:
- pass
+ pass
class B(A):
pass
def g(x):
@@ -195,7 +192,6 @@
result = ra.can_raise(fgraph.startblock.operations[0])
assert result
-class TestLLType(LLRtypeMixin, BaseTestCanRaise):
def test_llexternal(self):
from rpython.rtyper.lltypesystem.rffi import llexternal
from rpython.rtyper.lltypesystem import lltype
@@ -231,8 +227,3 @@
fgraph = graphof(t, f)
result = ra.can_raise(fgraph.startblock.operations[0])
assert not result
-
-
-class TestOOType(OORtypeMixin, BaseTestCanRaise):
- def test_can_raise_recursive(self):
- py.test.skip("ootype: no explicit stack checks raising RuntimeError")
diff --git a/rpython/translator/backendopt/test/test_finalizer.py b/rpython/translator/backendopt/test/test_finalizer.py
--- a/rpython/translator/backendopt/test/test_finalizer.py
+++ b/rpython/translator/backendopt/test/test_finalizer.py
@@ -10,18 +10,16 @@
from rpython.rlib import rgc
-class BaseFinalizerAnalyzerTests(object):
+class TestFinalizerAnalyzer(object):
""" Below are typical destructors that we encounter in pypy
"""
- type_system = None
-
def analyze(self, func, sig, func_to_analyze=None, backendopt=False):
if func_to_analyze is None:
func_to_analyze = func
t = TranslationContext()
t.buildannotator().build_types(func, sig)
- t.buildrtyper(type_system=self.type_system).specialize()
+ t.buildrtyper(type_system='lltype').specialize()
if backendopt:
backend_optimizations(t)
if option.view:
@@ -61,14 +59,10 @@
v3], None))
assert not f.analyze(SpaceOperation('bare_setfield', [v1, Constant('z'),
v4], None))
-
-
-class TestLLType(BaseFinalizerAnalyzerTests):
- type_system = 'lltype'
def test_malloc(self):
S = lltype.GcStruct('S')
-
+
def f():
return lltype.malloc(S)
@@ -77,7 +71,7 @@
def test_raw_free_getfield(self):
S = lltype.Struct('S')
-
+
class A(object):
def __init__(self):
self.x = lltype.malloc(S, flavor='raw')
@@ -100,7 +94,7 @@
def g():
p = lltype.malloc(C, 3, flavor='raw')
f(p)
-
+
def f(p):
c(rffi.ptradd(p, 0))
lltype.free(p, flavor='raw')
@@ -112,7 +106,7 @@
class B(object):
def __init__(self):
self.counter = 1
-
+
class A(object):
def __init__(self):
self.x = B()
@@ -137,6 +131,3 @@
pass
self.analyze(g, []) # did not explode
py.test.raises(FinalizerError, self.analyze, f, [])
-
-class TestOOType(BaseFinalizerAnalyzerTests):
- type_system = 'ootype'
diff --git a/rpython/translator/backendopt/test/test_inline.py b/rpython/translator/backendopt/test/test_inline.py
--- a/rpython/translator/backendopt/test/test_inline.py
+++ b/rpython/translator/backendopt/test/test_inline.py
@@ -13,7 +13,7 @@
from rpython.translator.backendopt.checkvirtual import check_virtual_methods
from rpython.translator.translator import TranslationContext, graphof
from rpython.rtyper.llinterp import LLInterpreter
-from rpython.rtyper.test.tool import LLRtypeMixin, OORtypeMixin
+from rpython.rtyper.test.tool import BaseRtypingTest
from rpython.rlib.rarithmetic import ovfcheck
from rpython.translator.test.snippet import is_perfect_number
from rpython.translator.backendopt.all import INLINE_THRESHOLD_FOR_TEST
@@ -51,12 +51,8 @@
def __init__(self):
self.data2 = 456
-class BaseTestInline:
- type_system = None
-
- def _skip_oo(self, reason):
- if self.type_system == 'ootype':
- py.test.skip("ootypesystem doesn't support %s, yet" % reason)
+class TestInline(BaseRtypingTest):
+ type_system = 'lltype'
def translate(self, func, argtypes):
t = TranslationContext()
@@ -119,7 +115,7 @@
if remove_same_as:
for graph in t.graphs:
removenoops.remove_same_as(graph)
-
+
if heuristic is not None:
kwargs = {"heuristic": heuristic}
else:
@@ -192,7 +188,7 @@
result = eval_func([1])
assert result == 1
result = eval_func([2])
- assert result == 2
+ assert result == 2
def test_inline_several_times(self):
def f(x):
@@ -587,9 +583,6 @@
eval_func([-66])
eval_func([282])
-
-class TestInlineLLType(LLRtypeMixin, BaseTestInline):
-
def test_correct_keepalive_placement(self):
def h(x):
if not x:
@@ -638,158 +631,3 @@
assert len(collect_called_graphs(f_graph, t)) == 1
auto_inline_graphs(t, [f_graph], 32, inline_graph_from_anywhere=True)
assert len(collect_called_graphs(f_graph, t)) == 0
-
-
-class TestInlineOOType(OORtypeMixin, BaseTestInline):
-
- def test_rtype_r_dict_exceptions(self):
- from rpython.rlib.objectmodel import r_dict
- def raising_hash(obj):
- if obj.startswith("bla"):
- raise TypeError
- return 1
- def eq(obj1, obj2):
- return obj1 is obj2
- def f():
- d1 = r_dict(eq, raising_hash)
- d1['xxx'] = 1
- try:
- x = d1["blabla"]
- except Exception:
- return 42
- return x
-
- eval_func, t = self.check_auto_inlining(f, [])
- res = eval_func([])
- assert res == 42
-
- def test_float(self):
- ex = ['', ' ']
- def fn(i):
- s = ex[i]
- try:
- return float(s)
- except ValueError:
- return -999.0
-
- eval_func, t = self.check_auto_inlining(fn, [int])
- expected = fn(0)
- res = eval_func([0])
- assert res == expected
-
- def test_oosend(self):
- class A:
- def foo(self, x):
- return x
- def fn(x):
- a = A()
- return a.foo(x)
-
- eval_func, t = self.check_auto_inlining(fn, [int], checkvirtual=True)
- expected = fn(42)
- res = eval_func([42])
- assert res == expected
-
- def test_not_inline_oosend(self):
- class A:
- def foo(self, x):
- return x
- class B(A):
- def foo(self, x):
- return x+1
-
- def fn(flag, x):
- if flag:
- obj = A()
- else:
- obj = B()
- return obj.foo(x)
-
- eval_func, t = self.check_auto_inlining(fn, [bool, int], checkvirtual=True)
- expected = fn(True, 42)
- res = eval_func([True, 42])
- assert res == expected
-
- def test_oosend_inherited(self):
- class BaseStringFormatter:
- def __init__(self):
- self.fmtpos = 0
- def forward(self):
- self.fmtpos += 1
-
- class StringFormatter(BaseStringFormatter):
- def __init__(self, fmt):
- BaseStringFormatter.__init__(self)
- self.fmt = fmt
- def peekchr(self):
- return self.fmt[self.fmtpos]
- def peel_num(self):
- while True:
- self.forward()
- c = self.peekchr()
- if self.fmtpos == 2: break
- return 0
-
- class UnicodeStringFormatter(BaseStringFormatter):
- pass
-
- def fn(x):
- if x:
- fmt = StringFormatter('foo')
- return fmt.peel_num()
- else:
- dummy = UnicodeStringFormatter()
- dummy.forward()
- return 0
-
- eval_func, t = self.check_auto_inlining(fn, [int], checkvirtual=True,
- remove_same_as=True)
- expected = fn(1)
- res = eval_func([1])
- assert res == expected
-
- def test_classattr(self):
- class A:
- attr = 666
- class B(A):
- attr = 42
- def fn5():
- b = B()
- return b.attr
-
- eval_func, t = self.check_auto_inlining(fn5, [], checkvirtual=True)
- res = eval_func([])
- assert res == 42
-
- def test_indirect_call_becomes_direct(self):
- def h1(n):
- return n+1
- def h2(n):
- return n+2
- def g(myfunc, n):
- return myfunc(n*5)
- def f(x, y):
- return g(h1, x) + g(h2, y)
- eval_func = self.check_inline(g, f, [int, int])
- res = eval_func([10, 173])
- assert res == f(10, 173)
-
- def test_cannot_inline_1(self):
- from rpython.rtyper.lltypesystem import lltype, rffi
- for attr in [None, 'try', True]:
- def h1(n):
- return lltype.malloc(rffi.INTP.TO, 1, flavor='raw')
- if attr is not None:
- h1._always_inline_ = attr
- def f(x):
- try:
- return h1(x)
- except Exception:
- return lltype.nullptr(rffi.INTP.TO)
- #
- def compile():
- self.check_auto_inlining(f, [int])
- if attr is True:
- py.test.raises(CannotInline, compile)
- else:
- compile() # assert does not raise
diff --git a/rpython/translator/backendopt/test/test_malloc.py b/rpython/translator/backendopt/test/test_malloc.py
--- a/rpython/translator/backendopt/test/test_malloc.py
+++ b/rpython/translator/backendopt/test/test_malloc.py
@@ -1,22 +1,17 @@
import py
-from rpython.translator.backendopt.malloc import LLTypeMallocRemover, OOTypeMallocRemover
+from rpython.translator.backendopt.malloc import LLTypeMallocRemover
from rpython.translator.backendopt.all import backend_optimizations
from rpython.translator.translator import TranslationContext, graphof
from rpython.translator import simplify
from rpython.flowspace.model import checkgraph, Block, mkentrymap
from rpython.rtyper.llinterp import LLInterpreter
from rpython.rtyper.lltypesystem import lltype, llmemory
-from rpython.rtyper.ootypesystem import ootype
from rpython.rlib import objectmodel
from rpython.conftest import option
-class BaseMallocRemovalTest(object):
- type_system = None
- MallocRemover = None
-
- def _skip_oo(self, msg):
- if self.type_system == 'ootype':
- py.test.skip(msg)
+class TestMallocRemoval(object):
+ type_system = 'lltype'
+ MallocRemover = LLTypeMallocRemover
def check_malloc_removed(cls, graph):
remover = cls.MallocRemover()
@@ -39,7 +34,7 @@
remover = self.MallocRemover()
t = TranslationContext()
t.buildannotator().build_types(fn, signature)
- t.buildrtyper(type_system=self.type_system).specialize()
+ t.buildrtyper(type_system='lltype').specialize()
graph = graphof(t, fn)
if inline is not None:
from rpython.translator.backendopt.inline import auto_inline_graphs
@@ -154,10 +149,6 @@
-class TestLLTypeMallocRemoval(BaseMallocRemovalTest):
- type_system = 'lltype'
- MallocRemover = LLTypeMallocRemover
-
def test_dont_remove_with__del__(self):
import os
delcalls = [0]
@@ -349,35 +340,3 @@
u[0].s.x = x
return u[0].s.x
graph = self.check(f, [int], [42], 42)
-
-
-class TestOOTypeMallocRemoval(BaseMallocRemovalTest):
- type_system = 'ootype'
- MallocRemover = OOTypeMallocRemover
-
- def test_oononnull(self):
- FOO = ootype.Instance('Foo', ootype.ROOT)
- def fn():
- s = ootype.new(FOO)
- return bool(s)
- self.check(fn, [], [], True)
-
- def test_classattr_as_defaults(self):
- class Bar:
- foo = 41
-
- def fn():
- x = Bar()
- x.foo += 1
- return x.foo
- self.check(fn, [], [], 42)
-
- def test_fn5(self):
- # don't test this in ootype because the class attribute access
- # is turned into an oosend which prevents malloc removal to
- # work unless we inline first. See test_classattr in
- # test_inline.py
- py.test.skip("oosend prevents malloc removal")
-
- def test_bogus_cast_pointer(self):
- py.test.skip("oosend prevents malloc removal")
diff --git a/rpython/translator/backendopt/test/test_writeanalyze.py b/rpython/translator/backendopt/test/test_writeanalyze.py
--- a/rpython/translator/backendopt/test/test_writeanalyze.py
+++ b/rpython/translator/backendopt/test/test_writeanalyze.py
@@ -1,6 +1,5 @@
import py
from rpython.rtyper.lltypesystem import lltype
-from rpython.rtyper.ootypesystem import ootype
from rpython.translator.translator import TranslationContext, graphof
from rpython.translator.simplify import get_funcobj
from rpython.translator.backendopt.writeanalyze import WriteAnalyzer, top_set
@@ -11,9 +10,9 @@
class BaseTest(object):
- type_system = None
+ type_system = 'lltype'
Analyzer = WriteAnalyzer
-
+
def translate(self, func, sig):
t = TranslationContext()
t.buildannotator().build_types(func, sig)
@@ -23,7 +22,7 @@
return t, self.Analyzer(t)
-class BaseTestWriteAnalyze(BaseTest):
+class TestWriteAnalyze(BaseTest):
def test_writes_simple(self):
def g(x):
@@ -149,7 +148,7 @@
obj = B()
f(obj)
m(obj)
-
+
t, wa = self.translate(h, [int])
hgraph = graphof(t, h)
# fiiiish :-(
@@ -176,7 +175,7 @@
# an indirect call without a list of graphs
from rpython.rlib.objectmodel import instantiate
class A:
- pass
+ pass
class B(A):
pass
def g(x):
@@ -190,10 +189,7 @@
t, wa = self.translate(f, [int])
fgraph = graphof(t, f)
result = wa.analyze(fgraph.startblock.operations[0])
- if self.type_system == 'lltype':
- assert result is top_set
- else:
- assert not result # ootype is more precise in this case
+ assert result is top_set
def test_llexternal(self):
from rpython.rtyper.lltypesystem.rffi import llexternal
@@ -224,10 +220,6 @@
result = wa.analyze(ggraph.startblock.operations[-1])
assert not result
-
-class TestLLtype(BaseTestWriteAnalyze):
- type_system = 'lltype'
-
def test_list(self):
def g(x, y, z):
return f(x, y, z)
@@ -284,46 +276,8 @@
assert name.endswith("foobar")
-class TestOOtype(BaseTestWriteAnalyze):
- type_system = 'ootype'
-
- def test_array(self):
- def g(x, y, z):
- return f(x, y, z)
- def f(x, y, z):
- l = [0] * x
- l[1] = 42
- return len(l) + z
-
- t, wa = self.translate(g, [int, int, int])
- ggraph = graphof(t, g)
- assert ggraph.startblock.operations[0].opname == 'direct_call'
-
- result = sorted(wa.analyze(ggraph.startblock.operations[0]))
- assert len(result) == 1
- array, A = result[0]
- assert array == 'array'
- assert A.ITEM is ootype.Signed
-
- def test_list(self):
- def g(x, y, z):
- return f(x, y, z)
- def f(x, y, z):
- l = [0] * x
- l.append(z)
- return len(l) + z
-
- t, wa = self.translate(g, [int, int, int])
- ggraph = graphof(t, g)
- assert ggraph.startblock.operations[0].opname == 'direct_call'
-
- result = wa.analyze(ggraph.startblock.operations[0])
- assert result is top_set
-
-
class TestLLtypeReadWriteAnalyze(BaseTest):
Analyzer = ReadWriteAnalyzer
- type_system = 'lltype'
def test_read_simple(self):
def g(x):
@@ -346,7 +300,7 @@
def h(flag):
obj = A(flag)
return obj.f()
-
+
t, wa = self.translate(h, [int])
hgraph = graphof(t, h)
op_call_f = hgraph.startblock.operations[-1]
More information about the pypy-commit
mailing list