| 1 |
import pytest |
| 2 |
import random |
| 3 |
import string |
| 4 |
|
| 5 |
from rpython.rtyper.lltypesystem import lltype, llmemory, rffi |
| 6 |
from rpython.rtyper import rclass |
| 7 |
from rpython.rtyper.rclass import ( |
| 8 |
OBJECT, OBJECT_VTABLE, FieldListAccessor, IR_QUASIIMMUTABLE) |
| 9 |
from rpython.rlib.rjitlog import rjitlog as jl |
| 10 |
|
| 11 |
from rpython.jit.backend.llgraph import runner |
| 12 |
from rpython.jit.metainterp.history import ( |
| 13 |
TreeLoop, AbstractDescr, JitCellToken) |
| 14 |
from rpython.jit.metainterp.history import IntFrontendOp, RefFrontendOp |
| 15 |
from rpython.jit.codewriter.effectinfo import EffectInfo, compute_bitstrings |
| 16 |
from rpython.jit.codewriter import heaptracker |
| 17 |
from rpython.jit.tool.oparser import ( |
| 18 |
OpParser, pure_parse, convert_loop_to_trace) |
| 19 |
from rpython.jit.metainterp.quasiimmut import QuasiImmutDescr |
| 20 |
from rpython.jit.metainterp import compile |
| 21 |
from rpython.jit.metainterp.jitprof import EmptyProfiler |
| 22 |
from rpython.jit.metainterp.counter import DeterministicJitCounter |
| 23 |
from rpython.config.translationoption import get_combined_translation_config |
| 24 |
from rpython.jit.metainterp.resoperation import ( |
| 25 |
rop, ResOperation, InputArgRef, AbstractValue) |
| 26 |
from rpython.jit.metainterp.virtualref import VirtualRefInfo |
| 27 |
from rpython.jit.metainterp.optimizeopt.util import ( |
| 28 |
sort_descrs, equaloplists, args_dict) |
| 29 |
|
| 30 |
|
| 31 |
def test_sort_descrs(): |
| 32 |
class PseudoDescr(AbstractDescr): |
| 33 |
def __init__(self, n): |
| 34 |
self.n = n |
| 35 |
def sort_key(self): |
| 36 |
return self.n |
| 37 |
for i in range(17): |
| 38 |
lst = [PseudoDescr(j) for j in range(i)] |
| 39 |
lst2 = lst[:] |
| 40 |
random.shuffle(lst2) |
| 41 |
sort_descrs(lst2) |
| 42 |
assert lst2 == lst |
| 43 |
|
| 44 |
def make_remap(inp1, inp2): |
| 45 |
remap = {} |
| 46 |
for a, b in zip(inp1, inp2): |
| 47 |
remap[b] = a |
| 48 |
return remap |
| 49 |
|
| 50 |
def test_equaloplists(): |
| 51 |
ops = """ |
| 52 |
[i0] |
| 53 |
i1 = int_add(i0, 1) |
| 54 |
i2 = int_add(i1, 1) |
| 55 |
guard_true(i1) [i2] |
| 56 |
jump(i1) |
| 57 |
""" |
| 58 |
namespace = {} |
| 59 |
loop1 = pure_parse(ops, namespace=namespace) |
| 60 |
loop2 = pure_parse(ops, namespace=namespace) |
| 61 |
loop3 = pure_parse(ops.replace("i2 = int_add", "i2 = int_sub"), |
| 62 |
namespace=namespace) |
| 63 |
assert equaloplists(loop1.operations, loop2.operations, |
| 64 |
remap=make_remap(loop1.inputargs, |
| 65 |
loop2.inputargs)) |
| 66 |
with pytest.raises(AssertionError): |
| 67 |
equaloplists( |
| 68 |
loop1.operations, loop3.operations, |
| 69 |
remap=make_remap(loop1.inputargs, loop3.inputargs)) |
| 70 |
|
| 71 |
def test_equaloplists_fail_args(): |
| 72 |
ops = """ |
| 73 |
[i0] |
| 74 |
i1 = int_add(i0, 1) |
| 75 |
i2 = int_add(i1, 1) |
| 76 |
guard_true(i1) [i2, i1] |
| 77 |
jump(i1) |
| 78 |
""" |
| 79 |
namespace = {} |
| 80 |
loop1 = pure_parse(ops, namespace=namespace) |
| 81 |
loop2 = pure_parse(ops.replace("[i2, i1]", "[i1, i2]"), |
| 82 |
namespace=namespace) |
| 83 |
with pytest.raises(AssertionError): |
| 84 |
equaloplists( |
| 85 |
loop1.operations, loop2.operations, |
| 86 |
remap=make_remap(loop1.inputargs, loop2.inputargs)) |
| 87 |
assert equaloplists(loop1.operations, loop2.operations, |
| 88 |
remap=make_remap(loop1.inputargs, loop2.inputargs), |
| 89 |
strict_fail_args=False) |
| 90 |
loop3 = pure_parse(ops.replace("[i2, i1]", "[i2, i0]"), |
| 91 |
namespace=namespace) |
| 92 |
with pytest.raises(AssertionError): |
| 93 |
equaloplists( |
| 94 |
loop1.operations, loop3.operations, |
| 95 |
remap=make_remap(loop1.inputargs, loop3.inputargs)) |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
class LLtypeMixin(object): |
| 100 |
node_vtable = lltype.malloc(OBJECT_VTABLE, immortal=True) |
| 101 |
node_vtable.name = rclass.alloc_array_name('node') |
| 102 |
node_vtable.subclassrange_min = 0 |
| 103 |
node_vtable.subclassrange_max = 2 |
| 104 |
node_vtable2 = lltype.malloc(OBJECT_VTABLE, immortal=True) |
| 105 |
node_vtable2.name = rclass.alloc_array_name('node2') |
| 106 |
node_vtable2.subclassrange_min = 1 |
| 107 |
node_vtable2.subclassrange_max = 1 |
| 108 |
node_vtable3 = lltype.malloc(OBJECT_VTABLE, immortal=True) |
| 109 |
node_vtable3.name = rclass.alloc_array_name('node3') |
| 110 |
node_vtable3.subclassrange_min = 3 |
| 111 |
node_vtable3.subclassrange_max = 3 |
| 112 |
cpu = runner.LLGraphCPU(None) |
| 113 |
|
| 114 |
NODE = lltype.GcForwardReference() |
| 115 |
S = lltype.GcForwardReference() |
| 116 |
NODE.become(lltype.GcStruct('NODE', ('parent', OBJECT), |
| 117 |
('value', lltype.Signed), |
| 118 |
('floatval', lltype.Float), |
| 119 |
('charval', lltype.Char), |
| 120 |
('nexttuple', lltype.Ptr(S)), |
| 121 |
('next', lltype.Ptr(NODE)))) |
| 122 |
S.become(lltype.GcStruct('TUPLE', ('a', lltype.Signed), ('abis', lltype.Signed), |
| 123 |
('b', lltype.Ptr(NODE)))) |
| 124 |
heaptracker.set_testing_vtable_for_gcstruct(NODE, node_vtable, "NODE") |
| 125 |
NODE2 = lltype.GcStruct('NODE2', ('parent', NODE), |
| 126 |
('other', lltype.Ptr(NODE))) |
| 127 |
heaptracker.set_testing_vtable_for_gcstruct(NODE2, node_vtable2, "NODE2") |
| 128 |
|
| 129 |
NODE3 = lltype.GcForwardReference() |
| 130 |
NODE3.become(lltype.GcStruct('NODE3', ('parent', OBJECT), |
| 131 |
('value', lltype.Signed), |
| 132 |
('next', lltype.Ptr(NODE3)), |
| 133 |
hints={'immutable': True})) |
| 134 |
heaptracker.set_testing_vtable_for_gcstruct(NODE3, node_vtable3, "NODE3") |
| 135 |
|
| 136 |
big_fields = [('big' + i, lltype.Signed) for i in string.ascii_lowercase] |
| 137 |
BIG = lltype.GcForwardReference() |
| 138 |
BIG.become(lltype.GcStruct('BIG', *big_fields, hints={'immutable': True})) |
| 139 |
|
| 140 |
for field, _ in big_fields: |
| 141 |
locals()[field + 'descr'] = cpu.fielddescrof(BIG, field) |
| 142 |
|
| 143 |
node = lltype.malloc(NODE) |
| 144 |
node.value = 5 |
| 145 |
node.next = node |
| 146 |
node.parent.typeptr = node_vtable |
| 147 |
nodeaddr = lltype.cast_opaque_ptr(llmemory.GCREF, node) |
| 148 |
|
| 149 |
node2 = lltype.malloc(NODE2) |
| 150 |
node2.parent.parent.typeptr = node_vtable2 |
| 151 |
node2addr = lltype.cast_opaque_ptr(llmemory.GCREF, node2) |
| 152 |
myptr = lltype.cast_opaque_ptr(llmemory.GCREF, node) |
| 153 |
mynodeb = lltype.malloc(NODE) |
| 154 |
myarray = lltype.cast_opaque_ptr(llmemory.GCREF, |
| 155 |
lltype.malloc(lltype.GcArray(lltype.Signed), 13, zero=True)) |
| 156 |
mynodeb.parent.typeptr = node_vtable |
| 157 |
myptrb = lltype.cast_opaque_ptr(llmemory.GCREF, mynodeb) |
| 158 |
myptr2 = lltype.malloc(NODE2) |
| 159 |
myptr2.parent.parent.typeptr = node_vtable2 |
| 160 |
myptr2 = lltype.cast_opaque_ptr(llmemory.GCREF, myptr2) |
| 161 |
nullptr = lltype.nullptr(llmemory.GCREF.TO) |
| 162 |
|
| 163 |
mynode3 = lltype.malloc(NODE3) |
| 164 |
mynode3.parent.typeptr = node_vtable3 |
| 165 |
mynode3.value = 7 |
| 166 |
mynode3.next = mynode3 |
| 167 |
myptr3 = lltype.cast_opaque_ptr(llmemory.GCREF, mynode3) |
| 168 |
mynode4 = lltype.malloc(NODE3) |
| 169 |
mynode4.parent.typeptr = node_vtable3 |
| 170 |
myptr4 = lltype.cast_opaque_ptr(llmemory.GCREF, mynode4) |
| 171 |
|
| 172 |
nodesize = cpu.sizeof(NODE, node_vtable) |
| 173 |
node_tid = nodesize.get_type_id() |
| 174 |
nodesize2 = cpu.sizeof(NODE2, node_vtable2) |
| 175 |
nodesize3 = cpu.sizeof(NODE3, node_vtable3) |
| 176 |
valuedescr = cpu.fielddescrof(NODE, 'value') |
| 177 |
floatdescr = cpu.fielddescrof(NODE, 'floatval') |
| 178 |
chardescr = cpu.fielddescrof(NODE, 'charval') |
| 179 |
nextdescr = cpu.fielddescrof(NODE, 'next') |
| 180 |
nexttupledescr = cpu.fielddescrof(NODE, 'nexttuple') |
| 181 |
otherdescr = cpu.fielddescrof(NODE2, 'other') |
| 182 |
valuedescr3 = cpu.fielddescrof(NODE3, 'value') |
| 183 |
nextdescr3 = cpu.fielddescrof(NODE3, 'next') |
| 184 |
assert valuedescr3.is_always_pure() |
| 185 |
assert nextdescr3.is_always_pure() |
| 186 |
|
| 187 |
accessor = FieldListAccessor() |
| 188 |
accessor.initialize(None, {'inst_field': IR_QUASIIMMUTABLE}) |
| 189 |
QUASI = lltype.GcStruct('QUASIIMMUT', ('inst_field', lltype.Signed), |
| 190 |
('mutate_field', rclass.OBJECTPTR), |
| 191 |
hints={'immutable_fields': accessor}) |
| 192 |
quasisize = cpu.sizeof(QUASI, None) |
| 193 |
quasi = lltype.malloc(QUASI, immortal=True) |
| 194 |
quasi.inst_field = -4247 |
| 195 |
quasifielddescr = cpu.fielddescrof(QUASI, 'inst_field') |
| 196 |
quasiptr = lltype.cast_opaque_ptr(llmemory.GCREF, quasi) |
| 197 |
quasiimmutdescr = QuasiImmutDescr(cpu, quasiptr, quasifielddescr, |
| 198 |
cpu.fielddescrof(QUASI, 'mutate_field')) |
| 199 |
|
| 200 |
NODEOBJ = lltype.GcStruct('NODEOBJ', ('parent', OBJECT), |
| 201 |
('ref', lltype.Ptr(OBJECT))) |
| 202 |
nodeobj = lltype.malloc(NODEOBJ) |
| 203 |
nodeobjvalue = lltype.cast_opaque_ptr(llmemory.GCREF, nodeobj) |
| 204 |
refdescr = cpu.fielddescrof(NODEOBJ, 'ref') |
| 205 |
|
| 206 |
INTOBJ_NOIMMUT = lltype.GcStruct('INTOBJ_NOIMMUT', ('parent', OBJECT), |
| 207 |
('intval', lltype.Signed)) |
| 208 |
INTOBJ_IMMUT = lltype.GcStruct('INTOBJ_IMMUT', ('parent', OBJECT), |
| 209 |
('intval', lltype.Signed), |
| 210 |
hints={'immutable': True}) |
| 211 |
intobj_noimmut_vtable = lltype.malloc(OBJECT_VTABLE, immortal=True) |
| 212 |
intobj_immut_vtable = lltype.malloc(OBJECT_VTABLE, immortal=True) |
| 213 |
noimmut_intval = cpu.fielddescrof(INTOBJ_NOIMMUT, 'intval') |
| 214 |
immut_intval = cpu.fielddescrof(INTOBJ_IMMUT, 'intval') |
| 215 |
immut = lltype.malloc(INTOBJ_IMMUT, zero=True) |
| 216 |
immutaddr = lltype.cast_opaque_ptr(llmemory.GCREF, immut) |
| 217 |
noimmut_descr = cpu.sizeof(INTOBJ_NOIMMUT, intobj_noimmut_vtable) |
| 218 |
immut_descr = cpu.sizeof(INTOBJ_IMMUT, intobj_immut_vtable) |
| 219 |
|
| 220 |
PTROBJ_IMMUT = lltype.GcStruct('PTROBJ_IMMUT', ('parent', OBJECT), |
| 221 |
('ptrval', lltype.Ptr(OBJECT)), |
| 222 |
hints={'immutable': True}) |
| 223 |
ptrobj_immut_vtable = lltype.malloc(OBJECT_VTABLE, immortal=True) |
| 224 |
ptrobj_immut_descr = cpu.sizeof(PTROBJ_IMMUT, ptrobj_immut_vtable) |
| 225 |
immut_ptrval = cpu.fielddescrof(PTROBJ_IMMUT, 'ptrval') |
| 226 |
|
| 227 |
arraydescr = cpu.arraydescrof(lltype.GcArray(lltype.Signed)) |
| 228 |
int32arraydescr = cpu.arraydescrof(lltype.GcArray(rffi.INT)) |
| 229 |
int16arraydescr = cpu.arraydescrof(lltype.GcArray(rffi.SHORT)) |
| 230 |
float32arraydescr = cpu.arraydescrof(lltype.GcArray(lltype.SingleFloat)) |
| 231 |
arraydescr_tid = arraydescr.get_type_id() |
| 232 |
array = lltype.malloc(lltype.GcArray(lltype.Signed), 15, zero=True) |
| 233 |
arrayref = lltype.cast_opaque_ptr(llmemory.GCREF, array) |
| 234 |
array2 = lltype.malloc(lltype.GcArray(lltype.Ptr(S)), 15, zero=True) |
| 235 |
array2ref = lltype.cast_opaque_ptr(llmemory.GCREF, array2) |
| 236 |
gcarraydescr = cpu.arraydescrof(lltype.GcArray(llmemory.GCREF)) |
| 237 |
gcarraydescr_tid = gcarraydescr.get_type_id() |
| 238 |
floatarraydescr = cpu.arraydescrof(lltype.GcArray(lltype.Float)) |
| 239 |
|
| 240 |
arrayimmutdescr = cpu.arraydescrof(lltype.GcArray(lltype.Signed, hints={"immutable": True})) |
| 241 |
immutarray = lltype.cast_opaque_ptr(llmemory.GCREF, lltype.malloc(arrayimmutdescr.A, 13, zero=True)) |
| 242 |
gcarrayimmutdescr = cpu.arraydescrof(lltype.GcArray(llmemory.GCREF, hints={"immutable": True})) |
| 243 |
floatarrayimmutdescr = cpu.arraydescrof(lltype.GcArray(lltype.Float, hints={"immutable": True})) |
| 244 |
|
| 245 |
|
| 246 |
tpl = lltype.malloc(S, zero=True) |
| 247 |
tupleaddr = lltype.cast_opaque_ptr(llmemory.GCREF, tpl) |
| 248 |
nodefull2 = lltype.malloc(NODE, zero=True) |
| 249 |
nodefull2addr = lltype.cast_opaque_ptr(llmemory.GCREF, nodefull2) |
| 250 |
ssize = cpu.sizeof(S, None) |
| 251 |
adescr = cpu.fielddescrof(S, 'a') |
| 252 |
abisdescr = cpu.fielddescrof(S, 'abis') |
| 253 |
bdescr = cpu.fielddescrof(S, 'b') |
| 254 |
|
| 255 |
arraydescr2 = cpu.arraydescrof(lltype.GcArray(lltype.Ptr(S))) |
| 256 |
|
| 257 |
T = lltype.GcStruct('TUPLE', |
| 258 |
('c', lltype.Signed), |
| 259 |
('d', lltype.Ptr(lltype.GcArray(lltype.Ptr(NODE))))) |
| 260 |
|
| 261 |
W_ROOT = lltype.GcStruct('W_ROOT', ('parent', OBJECT), |
| 262 |
('inst_w_seq', llmemory.GCREF), ('inst_index', lltype.Signed), |
| 263 |
('inst_w_list', llmemory.GCREF), ('inst_length', lltype.Signed), |
| 264 |
('inst_start', lltype.Signed), ('inst_step', lltype.Signed)) |
| 265 |
inst_w_seq = cpu.fielddescrof(W_ROOT, 'inst_w_seq') |
| 266 |
inst_index = cpu.fielddescrof(W_ROOT, 'inst_index') |
| 267 |
inst_length = cpu.fielddescrof(W_ROOT, 'inst_length') |
| 268 |
inst_start = cpu.fielddescrof(W_ROOT, 'inst_start') |
| 269 |
inst_step = cpu.fielddescrof(W_ROOT, 'inst_step') |
| 270 |
inst_w_list = cpu.fielddescrof(W_ROOT, 'inst_w_list') |
| 271 |
w_root_vtable = lltype.malloc(OBJECT_VTABLE, immortal=True) |
| 272 |
|
| 273 |
tsize = cpu.sizeof(T, None) |
| 274 |
cdescr = cpu.fielddescrof(T, 'c') |
| 275 |
ddescr = cpu.fielddescrof(T, 'd') |
| 276 |
arraydescr3 = cpu.arraydescrof(lltype.GcArray(lltype.Ptr(NODE3))) |
| 277 |
|
| 278 |
U = lltype.GcStruct('U', |
| 279 |
('parent', OBJECT), |
| 280 |
('one', lltype.Ptr(lltype.GcArray(lltype.Ptr(NODE))))) |
| 281 |
u_vtable = lltype.malloc(OBJECT_VTABLE, immortal=True) |
| 282 |
SIMPLE = lltype.GcStruct('simple', |
| 283 |
('parent', OBJECT), |
| 284 |
('value', lltype.Signed)) |
| 285 |
simplevalue = cpu.fielddescrof(SIMPLE, 'value') |
| 286 |
simple_vtable = lltype.malloc(OBJECT_VTABLE, immortal=True) |
| 287 |
simpledescr = cpu.sizeof(SIMPLE, simple_vtable) |
| 288 |
simple = lltype.malloc(SIMPLE, zero=True) |
| 289 |
simpleaddr = lltype.cast_opaque_ptr(llmemory.GCREF, simple) |
| 290 |
|
| 291 |
onedescr = cpu.fielddescrof(U, 'one') |
| 292 |
|
| 293 |
FUNC = lltype.FuncType([lltype.Signed], lltype.Signed) |
| 294 |
plaincalldescr = cpu.calldescrof(FUNC, FUNC.ARGS, FUNC.RESULT, |
| 295 |
EffectInfo.MOST_GENERAL) |
| 296 |
elidablecalldescr = cpu.calldescrof(FUNC, FUNC.ARGS, FUNC.RESULT, |
| 297 |
EffectInfo([valuedescr], [], [], |
| 298 |
[valuedescr], [], [], |
| 299 |
EffectInfo.EF_ELIDABLE_CANNOT_RAISE)) |
| 300 |
elidable2calldescr = cpu.calldescrof(FUNC, FUNC.ARGS, FUNC.RESULT, |
| 301 |
EffectInfo([valuedescr], [], [], |
| 302 |
[valuedescr], [], [], |
| 303 |
EffectInfo.EF_ELIDABLE_OR_MEMORYERROR)) |
| 304 |
elidable3calldescr = cpu.calldescrof(FUNC, FUNC.ARGS, FUNC.RESULT, |
| 305 |
EffectInfo([valuedescr], [], [], |
| 306 |
[valuedescr], [], [], |
| 307 |
EffectInfo.EF_ELIDABLE_CAN_RAISE)) |
| 308 |
nonwritedescr = cpu.calldescrof(FUNC, FUNC.ARGS, FUNC.RESULT, |
| 309 |
EffectInfo([], [], [], [], [], [])) |
| 310 |
writeadescr = cpu.calldescrof(FUNC, FUNC.ARGS, FUNC.RESULT, |
| 311 |
EffectInfo([], [], [], [adescr], [], [])) |
| 312 |
writearraydescr = cpu.calldescrof(FUNC, FUNC.ARGS, FUNC.RESULT, |
| 313 |
EffectInfo([], [], [], [adescr], [arraydescr], |
| 314 |
[])) |
| 315 |
writevalue3descr = cpu.calldescrof(FUNC, FUNC.ARGS, FUNC.RESULT, |
| 316 |
EffectInfo([], [], [], [valuedescr3], [], [])) |
| 317 |
readadescr = cpu.calldescrof(FUNC, FUNC.ARGS, FUNC.RESULT, |
| 318 |
EffectInfo([adescr], [], [], [], [], [])) |
| 319 |
mayforcevirtdescr = cpu.calldescrof(FUNC, FUNC.ARGS, FUNC.RESULT, |
| 320 |
EffectInfo([nextdescr], [], [], [], [], [], |
| 321 |
EffectInfo.EF_FORCES_VIRTUAL_OR_VIRTUALIZABLE, |
| 322 |
can_invalidate=True)) |
| 323 |
arraycopydescr = cpu.calldescrof(FUNC, FUNC.ARGS, FUNC.RESULT, |
| 324 |
EffectInfo([], [arraydescr], [], [], [arraydescr], [], |
| 325 |
EffectInfo.EF_CANNOT_RAISE, |
| 326 |
oopspecindex=EffectInfo.OS_ARRAYCOPY)) |
| 327 |
arraymovedescr = cpu.calldescrof(FUNC, FUNC.ARGS, FUNC.RESULT, |
| 328 |
EffectInfo([], [arraydescr], [], [], [arraydescr], [], |
| 329 |
EffectInfo.EF_CANNOT_RAISE, |
| 330 |
oopspecindex=EffectInfo.OS_ARRAYMOVE)) |
| 331 |
|
| 332 |
raw_malloc_descr = cpu.calldescrof(FUNC, FUNC.ARGS, FUNC.RESULT, |
| 333 |
EffectInfo([], [], [], [], [], [], |
| 334 |
EffectInfo.EF_CAN_RAISE, |
| 335 |
oopspecindex=EffectInfo.OS_RAW_MALLOC_VARSIZE_CHAR)) |
| 336 |
raw_free_descr = cpu.calldescrof(FUNC, FUNC.ARGS, FUNC.RESULT, |
| 337 |
EffectInfo([], [], [], [], [], [], |
| 338 |
EffectInfo.EF_CANNOT_RAISE, |
| 339 |
oopspecindex=EffectInfo.OS_RAW_FREE)) |
| 340 |
|
| 341 |
chararray = lltype.GcArray(lltype.Char) |
| 342 |
chararraydescr = cpu.arraydescrof(chararray) |
| 343 |
u2array = lltype.GcArray(rffi.USHORT) |
| 344 |
u2arraydescr = cpu.arraydescrof(u2array) |
| 345 |
|
| 346 |
nodefull = lltype.malloc(NODE2, zero=True) |
| 347 |
nodefull.parent.next = lltype.cast_pointer(lltype.Ptr(NODE), nodefull) |
| 348 |
nodefull.parent.nexttuple = tpl |
| 349 |
nodefulladdr = lltype.cast_opaque_ptr(llmemory.GCREF, nodefull) |
| 350 |
|
| 351 |
|
| 352 |
complexarray = lltype.GcArray( |
| 353 |
lltype.Struct("complex", |
| 354 |
("real", lltype.Float), |
| 355 |
("imag", lltype.Float), |
| 356 |
) |
| 357 |
) |
| 358 |
complexarraydescr = cpu.arraydescrof(complexarray) |
| 359 |
complexrealdescr = cpu.interiorfielddescrof(complexarray, "real") |
| 360 |
compleximagdescr = cpu.interiorfielddescrof(complexarray, "imag") |
| 361 |
complexarraycopydescr = cpu.calldescrof(FUNC, FUNC.ARGS, FUNC.RESULT, |
| 362 |
EffectInfo([], [complexarraydescr], [], [], [complexarraydescr], [], |
| 363 |
EffectInfo.EF_CANNOT_RAISE, |
| 364 |
oopspecindex=EffectInfo.OS_ARRAYCOPY)) |
| 365 |
|
| 366 |
rawarraydescr = cpu.arraydescrof(lltype.Array(lltype.Signed, |
| 367 |
hints={'nolength': True})) |
| 368 |
rawarraydescr_char = cpu.arraydescrof(lltype.Array(lltype.Char, |
| 369 |
hints={'nolength': True})) |
| 370 |
rawarraydescr_float = cpu.arraydescrof(lltype.Array(lltype.Float, |
| 371 |
hints={'nolength': True})) |
| 372 |
|
| 373 |
fc_array = lltype.GcArray( |
| 374 |
lltype.Struct( |
| 375 |
"floatchar", ("float", lltype.Float), ("char", lltype.Char))) |
| 376 |
fc_array_descr = cpu.arraydescrof(fc_array) |
| 377 |
fc_array_floatdescr = cpu.interiorfielddescrof(fc_array, "float") |
| 378 |
fc_array_chardescr = cpu.interiorfielddescrof(fc_array, "char") |
| 379 |
|
| 380 |
for _name, _os in [ |
| 381 |
('strconcatdescr', 'OS_STR_CONCAT'), |
| 382 |
('strslicedescr', 'OS_STR_SLICE'), |
| 383 |
('strequaldescr', 'OS_STR_EQUAL'), |
| 384 |
('streq_slice_checknull_descr', 'OS_STREQ_SLICE_CHECKNULL'), |
| 385 |
('streq_slice_nonnull_descr', 'OS_STREQ_SLICE_NONNULL'), |
| 386 |
('streq_slice_char_descr', 'OS_STREQ_SLICE_CHAR'), |
| 387 |
('streq_nonnull_descr', 'OS_STREQ_NONNULL'), |
| 388 |
('streq_nonnull_char_descr', 'OS_STREQ_NONNULL_CHAR'), |
| 389 |
('streq_checknull_char_descr', 'OS_STREQ_CHECKNULL_CHAR'), |
| 390 |
('streq_lengthok_descr', 'OS_STREQ_LENGTHOK'), |
| 391 |
]: |
| 392 |
if _name in ('strconcatdescr', 'strslicedescr'): |
| 393 |
_extra = EffectInfo.EF_ELIDABLE_OR_MEMORYERROR |
| 394 |
else: |
| 395 |
_extra = EffectInfo.EF_ELIDABLE_CANNOT_RAISE |
| 396 |
_oopspecindex = getattr(EffectInfo, _os) |
| 397 |
locals()[_name] = \ |
| 398 |
cpu.calldescrof(FUNC, FUNC.ARGS, FUNC.RESULT, |
| 399 |
EffectInfo([], [], [], [], [], [], _extra, |
| 400 |
oopspecindex=_oopspecindex)) |
| 401 |
|
| 402 |
_oopspecindex = getattr(EffectInfo, _os.replace('STR', 'UNI')) |
| 403 |
locals()[_name.replace('str', 'unicode')] = \ |
| 404 |
cpu.calldescrof(FUNC, FUNC.ARGS, FUNC.RESULT, |
| 405 |
EffectInfo([], [], [], [], [], [], _extra, |
| 406 |
oopspecindex=_oopspecindex)) |
| 407 |
|
| 408 |
s2u_descr = cpu.calldescrof(FUNC, FUNC.ARGS, FUNC.RESULT, |
| 409 |
EffectInfo([], [], [], [], [], [], EffectInfo.EF_ELIDABLE_CAN_RAISE, |
| 410 |
oopspecindex=EffectInfo.OS_STR2UNICODE)) |
| 411 |
|
| 412 |
|
| 413 |
class LoopToken(AbstractDescr): |
| 414 |
pass |
| 415 |
asmdescr = LoopToken() |
| 416 |
|
| 417 |
|
| 418 |
class FakeWarmRunnerDesc: |
| 419 |
pass |
| 420 |
FakeWarmRunnerDesc.cpu = cpu |
| 421 |
vrefinfo = VirtualRefInfo(FakeWarmRunnerDesc) |
| 422 |
virtualtokendescr = vrefinfo.descr_virtual_token |
| 423 |
virtualforceddescr = vrefinfo.descr_forced |
| 424 |
FUNC = lltype.FuncType([], lltype.Void) |
| 425 |
ei = EffectInfo([], [], [], [], [], [], EffectInfo.EF_CANNOT_RAISE, |
| 426 |
can_invalidate=False, |
| 427 |
oopspecindex=EffectInfo.OS_JIT_FORCE_VIRTUALIZABLE) |
| 428 |
clear_vable = cpu.calldescrof(FUNC, FUNC.ARGS, FUNC.RESULT, ei) |
| 429 |
|
| 430 |
jit_virtual_ref_vtable = vrefinfo.jit_virtual_ref_vtable |
| 431 |
vref_descr = cpu.sizeof(vrefinfo.JIT_VIRTUAL_REF, jit_virtual_ref_vtable) |
| 432 |
|
| 433 |
FUNC = lltype.FuncType([lltype.Signed, lltype.Signed], lltype.Signed) |
| 434 |
ei = EffectInfo([], [], [], [], [], [], EffectInfo.EF_ELIDABLE_CANNOT_RAISE, |
| 435 |
can_invalidate=False, |
| 436 |
oopspecindex=EffectInfo.OS_INT_PY_DIV) |
| 437 |
int_py_div_descr = cpu.calldescrof(FUNC, FUNC.ARGS, FUNC.RESULT, ei) |
| 438 |
ei = EffectInfo([], [], [], [], [], [], EffectInfo.EF_ELIDABLE_CANNOT_RAISE, |
| 439 |
can_invalidate=False, |
| 440 |
oopspecindex=EffectInfo.OS_INT_UDIV) |
| 441 |
int_udiv_descr = cpu.calldescrof(FUNC, FUNC.ARGS, FUNC.RESULT, ei) |
| 442 |
ei = EffectInfo([], [], [], [], [], [], EffectInfo.EF_ELIDABLE_CANNOT_RAISE, |
| 443 |
can_invalidate=False, |
| 444 |
oopspecindex=EffectInfo.OS_INT_PY_MOD) |
| 445 |
int_py_mod_descr = cpu.calldescrof(FUNC, FUNC.ARGS, FUNC.RESULT, ei) |
| 446 |
|
| 447 |
FUNC = lltype.FuncType([], llmemory.GCREF) |
| 448 |
ei = EffectInfo([], [], [], [], [], [], EffectInfo.EF_ELIDABLE_CAN_RAISE) |
| 449 |
plain_r_calldescr = cpu.calldescrof(FUNC, FUNC.ARGS, FUNC.RESULT, ei) |
| 450 |
|
| 451 |
namespace = locals() |
| 452 |
|
| 453 |
|
| 454 |
class FakeCallInfoCollection: |
| 455 |
def callinfo_for_oopspec(self, oopspecindex): |
| 456 |
calldescrtype = type(LLtypeMixin.strequaldescr) |
| 457 |
effectinfotype = type(LLtypeMixin.strequaldescr.get_extra_info()) |
| 458 |
for value in LLtypeMixin.__dict__.values(): |
| 459 |
if isinstance(value, calldescrtype): |
| 460 |
extra = value.get_extra_info() |
| 461 |
if (extra and isinstance(extra, effectinfotype) and |
| 462 |
extra.oopspecindex == oopspecindex): |
| 463 |
|
| 464 |
return value, 0 |
| 465 |
raise AssertionError("not found: oopspecindex=%d" % |
| 466 |
oopspecindex) |
| 467 |
|
| 468 |
calldescr_udiv = LLtypeMixin.int_udiv_descr |
| 469 |
|
| 470 |
|
| 471 |
class Fake(object): |
| 472 |
failargs_limit = 1000 |
| 473 |
storedebug = None |
| 474 |
|
| 475 |
class FakeWarmState(object): |
| 476 |
vec = True |
| 477 |
vec_all = False |
| 478 |
vec_cost = 0 |
| 479 |
def __init__(self, enable_opts): |
| 480 |
self.enable_opts = enable_opts |
| 481 |
|
| 482 |
class FakeJitDriverStaticData(object): |
| 483 |
vec = False |
| 484 |
class warmstate: |
| 485 |
pureop_historylength = 16 |
| 486 |
|
| 487 |
virtualizable_info = None |
| 488 |
|
| 489 |
class FakeMetaInterpStaticData(object): |
| 490 |
all_descrs = [] |
| 491 |
|
| 492 |
def __init__(self, cpu): |
| 493 |
self.cpu = cpu |
| 494 |
self.profiler = EmptyProfiler() |
| 495 |
self.options = Fake() |
| 496 |
self.globaldata = Fake() |
| 497 |
self.config = get_combined_translation_config(translating=True) |
| 498 |
self.jitlog = jl.JitLogger() |
| 499 |
self.callinfocollection = FakeCallInfoCollection() |
| 500 |
|
| 501 |
class logger_noopt: |
| 502 |
@classmethod |
| 503 |
def log_loop(*args, **kwds): |
| 504 |
pass |
| 505 |
|
| 506 |
@classmethod |
| 507 |
def log_loop_from_trace(*args, **kwds): |
| 508 |
pass |
| 509 |
|
| 510 |
class logger_ops: |
| 511 |
repr_of_resop = repr |
| 512 |
|
| 513 |
class warmrunnerdesc: |
| 514 |
class memory_manager: |
| 515 |
retrace_limit = 5 |
| 516 |
max_retrace_guards = 15 |
| 517 |
jitcounter = DeterministicJitCounter() |
| 518 |
|
| 519 |
def get_name_from_address(self, addr): |
| 520 |
|
| 521 |
try: |
| 522 |
return "".join(addr.ptr.name.chars) |
| 523 |
except AttributeError: |
| 524 |
return "" |
| 525 |
|
| 526 |
class jitdrivers_sd(object): |
| 527 |
def __getitem__(self, index): |
| 528 |
return self |
| 529 |
|
| 530 |
class warmstate(object): |
| 531 |
@staticmethod |
| 532 |
def get_location_str(*args): |
| 533 |
return "fake location str!" |
| 534 |
jitdrivers_sd = jitdrivers_sd() |
| 535 |
|
| 536 |
def _try_find_typeid_for_vtable(self, known_class): |
| 537 |
ptr = known_class.value.adr.ptr |
| 538 |
if ptr == LLtypeMixin.node_vtable: |
| 539 |
return LLtypeMixin.nodesize.get_type_id() |
| 540 |
if ptr == LLtypeMixin.node_vtable2: |
| 541 |
return LLtypeMixin.nodesize2.get_type_id() |
| 542 |
if ptr == LLtypeMixin.node_vtable3: |
| 543 |
return LLtypeMixin.nodesize3.get_type_id() |
| 544 |
raise KeyError |
| 545 |
|
| 546 |
|
| 547 |
class Info(object): |
| 548 |
def __init__(self, preamble, short_preamble=None, virtual_state=None): |
| 549 |
self.preamble = preamble |
| 550 |
self.short_preamble = short_preamble |
| 551 |
self.virtual_state = virtual_state |
| 552 |
|
| 553 |
|
| 554 |
class BaseTest(LLtypeMixin): |
| 555 |
@pytest.fixture(autouse=True) |
| 556 |
def cls_attributes(self): |
| 557 |
metainterp_sd = FakeMetaInterpStaticData(self.cpu) |
| 558 |
metainterp_sd.virtualref_info = self.vrefinfo |
| 559 |
compute_bitstrings(self.cpu.fetch_all_descrs()) |
| 560 |
self.metainterp_sd = metainterp_sd |
| 561 |
self.jitdriver_sd = FakeJitDriverStaticData() |
| 562 |
|
| 563 |
def parse(self, s, boxkinds=None, want_fail_descr=True, postprocess=None): |
| 564 |
AbstractValue._repr_memo.counter = 0 |
| 565 |
self.oparse = OpParser(s, self.cpu, self.namespace, boxkinds, |
| 566 |
None, False, postprocess) |
| 567 |
return self.oparse.parse() |
| 568 |
|
| 569 |
def add_guard_future_condition(self, res): |
| 570 |
|
| 571 |
if res.operations[-1].getopnum() == rop.JUMP: |
| 572 |
guard = ResOperation(rop.GUARD_FUTURE_CONDITION, []) |
| 573 |
res.operations.insert(-1, guard) |
| 574 |
|
| 575 |
@staticmethod |
| 576 |
def assert_equal(optimized, expected, text_right=None): |
| 577 |
assert len(optimized.inputargs) == len(expected.inputargs) |
| 578 |
remap = {} |
| 579 |
for box1, box2 in zip(optimized.inputargs, expected.inputargs): |
| 580 |
assert box1.type == box2.type |
| 581 |
remap[box2] = box1 |
| 582 |
assert equaloplists(optimized.operations, |
| 583 |
expected.operations, False, remap, text_right) |
| 584 |
|
| 585 |
def _convert_call_pure_results(self, d): |
| 586 |
if d is None: |
| 587 |
return args_dict() |
| 588 |
call_pure_results = args_dict() |
| 589 |
for k, v in d.items(): |
| 590 |
call_pure_results[list(k)] = v |
| 591 |
return call_pure_results |
| 592 |
|
| 593 |
def convert_values(self, inpargs, values): |
| 594 |
if values: |
| 595 |
r = [] |
| 596 |
for arg, v in zip(inpargs, values): |
| 597 |
if arg.type == 'i': |
| 598 |
n = IntFrontendOp(0, v) |
| 599 |
else: |
| 600 |
n = RefFrontendOp(0, v) |
| 601 |
assert arg.type == 'r' |
| 602 |
r.append(n) |
| 603 |
return r |
| 604 |
return inpargs |
| 605 |
|
| 606 |
def unroll_and_optimize(self, loop, call_pure_results=None, |
| 607 |
jump_values=None): |
| 608 |
self.add_guard_future_condition(loop) |
| 609 |
jump_op = loop.operations[-1] |
| 610 |
assert jump_op.getopnum() == rop.JUMP |
| 611 |
celltoken = JitCellToken() |
| 612 |
runtime_boxes = self.pack_into_boxes(jump_op, jump_values) |
| 613 |
jump_op.setdescr(celltoken) |
| 614 |
call_pure_results = self._convert_call_pure_results(call_pure_results) |
| 615 |
t = convert_loop_to_trace(loop, self.metainterp_sd) |
| 616 |
preamble_data = compile.PreambleCompileData( |
| 617 |
t, runtime_boxes, call_pure_results, enable_opts=self.enable_opts) |
| 618 |
start_state, preamble_ops = preamble_data.optimize_trace( |
| 619 |
self.metainterp_sd, self.jitdriver_sd, {}) |
| 620 |
preamble_data.forget_optimization_info() |
| 621 |
loop_data = compile.UnrolledLoopData( |
| 622 |
preamble_data.trace, celltoken, start_state, call_pure_results, |
| 623 |
enable_opts=self.enable_opts) |
| 624 |
loop_info, ops = loop_data.optimize_trace(self.metainterp_sd, self.jitdriver_sd, {}) |
| 625 |
preamble = TreeLoop('preamble') |
| 626 |
preamble.inputargs = start_state.renamed_inputargs |
| 627 |
start_label = ResOperation(rop.LABEL, start_state.renamed_inputargs) |
| 628 |
preamble.operations = ([start_label] + preamble_ops + |
| 629 |
loop_info.extra_same_as + [loop_info.label_op]) |
| 630 |
loop.inputargs = loop_info.label_op.getarglist()[:] |
| 631 |
loop.operations = [loop_info.label_op] + ops |
| 632 |
return Info(preamble, loop_info.target_token.short_preamble, |
| 633 |
start_state.virtual_state) |
| 634 |
|
| 635 |
def pack_into_boxes(self, jump_op, jump_values): |
| 636 |
assert jump_op.getopnum() == rop.JUMP |
| 637 |
r = [] |
| 638 |
if jump_values is not None: |
| 639 |
assert len(jump_values) == len(jump_op.getarglist()) |
| 640 |
for i, v in enumerate(jump_values): |
| 641 |
if v is not None: |
| 642 |
r.append(InputArgRef(v)) |
| 643 |
else: |
| 644 |
r.append(None) |
| 645 |
else: |
| 646 |
for i, box in enumerate(jump_op.getarglist()): |
| 647 |
if box.type == 'r' and not box.is_constant(): |
| 648 |
|
| 649 |
|
| 650 |
|
| 651 |
|
| 652 |
r.append(InputArgRef(self.nodefulladdr)) |
| 653 |
else: |
| 654 |
r.append(None) |
| 655 |
return r |
| 656 |
|
| 657 |
class FakeDescr(compile.ResumeGuardDescr): |
| 658 |
def clone_if_mutable(self): |
| 659 |
return FakeDescr() |
| 660 |
def __eq__(self, other): |
| 661 |
return isinstance(other, FakeDescr) |
| 662 |
|
| 663 |
def convert_old_style_to_targets(loop, jump): |
| 664 |
newloop = TreeLoop(loop.name) |
| 665 |
newloop.inputargs = loop.inputargs |
| 666 |
newloop.operations = [ResOperation(rop.LABEL, loop.inputargs, descr=FakeDescr())] + \ |
| 667 |
loop.operations |
| 668 |
if not jump: |
| 669 |
assert newloop.operations[-1].getopnum() == rop.JUMP |
| 670 |
newloop.operations[-1] = newloop.operations[-1].copy_and_change( |
| 671 |
rop.LABEL) |
| 672 |
return newloop |
| 673 |
|
| 674 |
|