1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
|
import pytest
from rpython.jit.codewriter.effectinfo import (effectinfo_from_writeanalyze,
EffectInfo, VirtualizableAnalyzer)
from rpython.rlib import jit
from rpython.rtyper.lltypesystem import lltype
from rpython.rtyper.lltypesystem.rclass import OBJECT
from rpython.translator.translator import TranslationContext, graphof
class FakeCPU(object):
def fielddescrof(self, T, fieldname):
return ('fielddescr', T, fieldname)
def arraydescrof(self, A):
return ('arraydescr', A)
def test_no_oopspec_duplicate():
# check that all the various EffectInfo.OS_* have unique values
oopspecs = set()
for name, value in EffectInfo.__dict__.iteritems():
if name.startswith('OS_'):
assert value not in oopspecs
oopspecs.add(value)
def test_include_read_field():
S = lltype.GcStruct("S", ("a", lltype.Signed))
effects = frozenset([("readstruct", lltype.Ptr(S), "a")])
effectinfo = effectinfo_from_writeanalyze(effects, FakeCPU())
assert list(effectinfo.readonly_descrs_fields) == [('fielddescr', S, "a")]
assert not effectinfo.write_descrs_fields
assert not effectinfo.write_descrs_arrays
def test_include_write_field():
S = lltype.GcStruct("S", ("a", lltype.Signed))
effects = frozenset([("struct", lltype.Ptr(S), "a")])
effectinfo = effectinfo_from_writeanalyze(effects, FakeCPU())
assert list(effectinfo.write_descrs_fields) == [('fielddescr', S, "a")]
assert not effectinfo.readonly_descrs_fields
assert not effectinfo.write_descrs_arrays
def test_include_read_array():
A = lltype.GcArray(lltype.Signed)
effects = frozenset([("readarray", lltype.Ptr(A))])
effectinfo = effectinfo_from_writeanalyze(effects, FakeCPU())
assert not effectinfo.readonly_descrs_fields
assert list(effectinfo.readonly_descrs_arrays) == [('arraydescr', A)]
assert not effectinfo.write_descrs_fields
assert not effectinfo.write_descrs_arrays
def test_include_write_array():
A = lltype.GcArray(lltype.Signed)
effects = frozenset([("array", lltype.Ptr(A))])
effectinfo = effectinfo_from_writeanalyze(effects, FakeCPU())
assert not effectinfo.readonly_descrs_fields
assert not effectinfo.write_descrs_fields
assert list(effectinfo.write_descrs_arrays) == [('arraydescr', A)]
def test_dont_include_read_and_write_field():
S = lltype.GcStruct("S", ("a", lltype.Signed))
effects = frozenset([("readstruct", lltype.Ptr(S), "a"),
("struct", lltype.Ptr(S), "a")])
effectinfo = effectinfo_from_writeanalyze(effects, FakeCPU())
assert not effectinfo.readonly_descrs_fields
assert list(effectinfo.write_descrs_fields) == [('fielddescr', S, "a")]
assert not effectinfo.write_descrs_arrays
def test_dont_include_read_and_write_array():
A = lltype.GcArray(lltype.Signed)
effects = frozenset([("readarray", lltype.Ptr(A)),
("array", lltype.Ptr(A))])
effectinfo = effectinfo_from_writeanalyze(effects, FakeCPU())
assert not effectinfo.readonly_descrs_fields
assert not effectinfo.readonly_descrs_arrays
assert not effectinfo.write_descrs_fields
assert list(effectinfo.write_descrs_arrays) == [('arraydescr', A)]
def test_filter_out_typeptr():
effects = frozenset([("struct", lltype.Ptr(OBJECT), "typeptr")])
effectinfo = effectinfo_from_writeanalyze(effects, None)
assert not effectinfo.readonly_descrs_fields
assert not effectinfo.write_descrs_fields
assert not effectinfo.write_descrs_arrays
def test_filter_out_array_of_void():
effects = frozenset([("array", lltype.Ptr(lltype.GcArray(lltype.Void)))])
effectinfo = effectinfo_from_writeanalyze(effects, None)
assert not effectinfo.readonly_descrs_fields
assert not effectinfo.write_descrs_fields
assert not effectinfo.write_descrs_arrays
def test_filter_out_struct_with_void():
effects = frozenset([("struct", lltype.Ptr(lltype.GcStruct("x", ("a", lltype.Void))), "a")])
effectinfo = effectinfo_from_writeanalyze(effects, None)
assert not effectinfo.readonly_descrs_fields
assert not effectinfo.write_descrs_fields
assert not effectinfo.write_descrs_arrays
class TestVirtualizableAnalyzer(object):
def analyze(self, func, sig):
t = TranslationContext()
t.buildannotator().build_types(func, sig)
t.buildrtyper().specialize()
fgraph = graphof(t, func)
return VirtualizableAnalyzer(t).analyze(fgraph.startblock.operations[0])
def test_constructor(self):
class A(object):
x = 1
class B(A):
x = 2
@jit.elidable
def g(cls):
return cls()
def f(x):
if x:
cls = A
else:
cls = B
return g(cls).x
def entry(x):
return f(x)
res = self.analyze(entry, [int])
assert not res
|