| 1 |
import pytest, sys |
| 2 |
from pypy.module.pypyjit.test_pypy_c.test_00_model import BaseTestPyPyC |
| 3 |
|
| 4 |
|
| 5 |
class TestMisc(BaseTestPyPyC): |
| 6 |
def test_f1(self): |
| 7 |
def f1(n): |
| 8 |
"Arbitrary test function." |
| 9 |
i = 0 |
| 10 |
x = 1 |
| 11 |
while i<n: |
| 12 |
j = 0 |
| 13 |
while j<=i: |
| 14 |
j = j + 1 |
| 15 |
x = x + (i&j) |
| 16 |
i = i + 1 |
| 17 |
return x |
| 18 |
log = self.run(f1, [2117]) |
| 19 |
assert log.result == 1083876708 |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
loop0, loop1 = log.loops_by_filename(self.filepath) |
| 25 |
expected = """ |
| 26 |
i9 = int_le(i7, i8) |
| 27 |
guard_true(i9, descr=...) |
| 28 |
i11 = int_add_ovf(i7, 1) |
| 29 |
guard_no_overflow(descr=...) |
| 30 |
i12 = int_and(i8, i11) |
| 31 |
i13 = int_add_ovf(i6, i12) |
| 32 |
guard_no_overflow(descr=...) |
| 33 |
--TICK-- |
| 34 |
jump(..., descr=...) |
| 35 |
""" |
| 36 |
assert loop0.match(expected) |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
def test_factorial(self): |
| 43 |
def fact(n): |
| 44 |
r = 1 |
| 45 |
while n > 1: |
| 46 |
r *= n |
| 47 |
n -= 1 |
| 48 |
return r |
| 49 |
log = self.run(fact, [7], threshold=4) |
| 50 |
assert log.result == 5040 |
| 51 |
loop, = log.loops_by_filename(self.filepath) |
| 52 |
assert loop.match(""" |
| 53 |
i7 = int_gt(i4, 1) |
| 54 |
guard_true(i7, descr=...) |
| 55 |
i8 = int_mul_ovf(i5, i4) |
| 56 |
guard_no_overflow(descr=...) |
| 57 |
i10 = int_sub(i4, 1) |
| 58 |
--TICK-- |
| 59 |
jump(..., descr=...) |
| 60 |
""") |
| 61 |
|
| 62 |
log = self.run(fact, [25], threshold=20) |
| 63 |
assert log.result == 15511210043330985984000000L |
| 64 |
loop, = log.loops_by_filename(self.filepath) |
| 65 |
assert loop.match(""" |
| 66 |
i7 = int_gt(i4, 1) |
| 67 |
guard_true(i7, descr=...) |
| 68 |
p11 = call_r(ConstClass(rbigint.int_mul), p5, i4, descr=...) |
| 69 |
guard_no_exception(descr=...) |
| 70 |
i13 = int_sub(i4, 1) |
| 71 |
--TICK-- |
| 72 |
jump(..., descr=...) |
| 73 |
""") |
| 74 |
|
| 75 |
def test_mixed_type_loop(self): |
| 76 |
def main(n): |
| 77 |
i = 0.0 |
| 78 |
j = 2 |
| 79 |
while i < n: |
| 80 |
i = j + i |
| 81 |
return i |
| 82 |
|
| 83 |
log = self.run(main, [1000]) |
| 84 |
assert log.result == 1000.0 |
| 85 |
loop, = log.loops_by_filename(self.filepath) |
| 86 |
assert loop.match(""" |
| 87 |
i9 = float_lt(f5, f7) |
| 88 |
guard_true(i9, descr=...) |
| 89 |
f10 = float_add(f8, f5) |
| 90 |
--TICK-- |
| 91 |
jump(..., descr=...) |
| 92 |
""") |
| 93 |
|
| 94 |
def test_cached_pure_func_of_equal_fields(self): |
| 95 |
def main(n): |
| 96 |
class A(object): |
| 97 |
def __init__(self, val): |
| 98 |
self.val1 = self.val2 = val |
| 99 |
A("x") |
| 100 |
a = A(1) |
| 101 |
b = A(1) |
| 102 |
sa = 0 |
| 103 |
while n: |
| 104 |
sa += 2*a.val1 |
| 105 |
sa += 2*b.val2 |
| 106 |
b.val2 = a.val1 |
| 107 |
n -= 1 |
| 108 |
return sa |
| 109 |
|
| 110 |
log = self.run(main, [1000]) |
| 111 |
assert log.result == 4000 |
| 112 |
loop, = log.loops_by_filename(self.filepath) |
| 113 |
assert loop.match(""" |
| 114 |
i12 = int_is_true(i4) |
| 115 |
guard_true(i12, descr=...) |
| 116 |
guard_not_invalidated(descr=...) |
| 117 |
guard_nonnull_class(p10, ConstClass(W_IntObject), descr=...) |
| 118 |
i10p = getfield_gc_i(p10, descr=...) |
| 119 |
i10 = int_mul_ovf(2, i10p) |
| 120 |
guard_no_overflow(descr=...) |
| 121 |
i14 = int_add_ovf(i13, i10) |
| 122 |
guard_no_overflow(descr=...) |
| 123 |
i13 = int_add_ovf(i14, i9) |
| 124 |
guard_no_overflow(descr=...) |
| 125 |
setfield_gc(p17, p10, descr=...) |
| 126 |
i17 = int_sub_ovf(i4, 1) |
| 127 |
guard_no_overflow(descr=...) |
| 128 |
--TICK-- |
| 129 |
jump(..., descr=...) |
| 130 |
""") |
| 131 |
|
| 132 |
def test_range_iter_simple(self): |
| 133 |
def main(n): |
| 134 |
def g(n): |
| 135 |
return range(n) |
| 136 |
s = 0 |
| 137 |
for i in range(n): |
| 138 |
tmp = g(n) |
| 139 |
s += tmp[i] |
| 140 |
a = 0 |
| 141 |
return s |
| 142 |
|
| 143 |
log = self.run(main, [1000]) |
| 144 |
assert log.result == 1000 * 999 / 2 |
| 145 |
loop, = log.loops_by_filename(self.filepath) |
| 146 |
assert loop.match(self.RANGE_ITER_STEP_1) |
| 147 |
|
| 148 |
RANGE_ITER_STEP_1 = """ |
| 149 |
guard_not_invalidated? |
| 150 |
# W_IntRangeStepOneIterator.next() |
| 151 |
i16 = int_lt(i11, i12) |
| 152 |
guard_true(i16, descr=...) |
| 153 |
i20 = int_add(i11, 1) |
| 154 |
setfield_gc(p4, i20, descr=<.* .*W_IntRangeIterator.inst_current .*>) |
| 155 |
guard_not_invalidated? |
| 156 |
i21 = force_token() |
| 157 |
i89 = int_lt(0, i9) |
| 158 |
guard_true(i89, descr=...) |
| 159 |
i88 = int_sub(i9, 1) |
| 160 |
|
| 161 |
# Compared with pypy2, we get these two operations extra. |
| 162 |
# I think the reason is that W_IntRangeStepOneIterator is used |
| 163 |
# for any 'start' value, which might be negative. |
| 164 |
i89 = int_lt(i11, 0) |
| 165 |
guard_false(i89, descr=...) |
| 166 |
|
| 167 |
i25 = int_ge(i11, i9) |
| 168 |
guard_false(i25, descr=...) |
| 169 |
i27 = int_add_ovf(i7, i11) |
| 170 |
guard_no_overflow(descr=...) |
| 171 |
--TICK-- |
| 172 |
jump(..., descr=...) |
| 173 |
""" |
| 174 |
|
| 175 |
def test_range_iter_normal(self): |
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
def main(n): |
| 180 |
def g(n): |
| 181 |
return range(n) |
| 182 |
s = 0 |
| 183 |
for i in range(1, n): |
| 184 |
tmp = g(n) |
| 185 |
s += tmp[i] |
| 186 |
a = 0 |
| 187 |
return s |
| 188 |
|
| 189 |
log = self.run(main, [1000]) |
| 190 |
assert log.result == 1000 * 999 / 2 |
| 191 |
loop, = log.loops_by_filename(self.filepath) |
| 192 |
assert loop.match(self.RANGE_ITER_STEP_1) |
| 193 |
|
| 194 |
def test_chain_of_guards(self): |
| 195 |
src = """ |
| 196 |
class A(object): |
| 197 |
def method_x(self): |
| 198 |
return 3 |
| 199 |
|
| 200 |
l = ["x", "y"] |
| 201 |
|
| 202 |
def main(arg): |
| 203 |
sum = 0 |
| 204 |
a = A() |
| 205 |
i = 0 |
| 206 |
while i < 500: |
| 207 |
name = l[arg] |
| 208 |
sum += getattr(a, 'method_' + name)() |
| 209 |
i += 1 |
| 210 |
return sum |
| 211 |
""" |
| 212 |
log = self.run(src, [0]) |
| 213 |
assert log.result == 500*3 |
| 214 |
loops = log.loops_by_filename(self.filepath) |
| 215 |
assert len(loops) == 1 |
| 216 |
|
| 217 |
def test_unpack_iterable_non_list_tuple(self): |
| 218 |
def main(n): |
| 219 |
import array |
| 220 |
|
| 221 |
items = [array.array("i", [1])] * n |
| 222 |
total = 0 |
| 223 |
for a, in items: |
| 224 |
total += a |
| 225 |
return total |
| 226 |
|
| 227 |
log = self.run(main, [1000000]) |
| 228 |
assert log.result == 1000000 |
| 229 |
loop, = log.loops_by_filename(self.filepath) |
| 230 |
assert loop.match(""" |
| 231 |
guard_not_invalidated? |
| 232 |
i16 = uint_ge(i12, i14) |
| 233 |
guard_false(i16, descr=...) |
| 234 |
p17 = getarrayitem_gc_r(p16, i12, descr=<ArrayP .>) |
| 235 |
i19 = int_add(i12, 1) |
| 236 |
setfield_gc(p9, i19, descr=<FieldS .*W_AbstractSeqIterObject.inst_index .*>) |
| 237 |
guard_nonnull_class(p17, ..., descr=...) |
| 238 |
guard_not_invalidated? |
| 239 |
i21 = getfield_gc_i(p17, descr=<FieldS .*W_Array.*.inst_len .*>) |
| 240 |
i22 = int_lt(i21, 0) |
| 241 |
guard_false(i22, descr=...) |
| 242 |
i23 = int_lt(0, i21) |
| 243 |
guard_true(i23, descr=...) |
| 244 |
i24 = getfield_gc_i(p17, descr=<FieldU .*W_ArrayBase.inst__buffer .*>) |
| 245 |
i25 = getarrayitem_raw_i(i24, 0, descr=<.*>) |
| 246 |
i27 = int_lt(1, i21) |
| 247 |
guard_false(i27, descr=...) |
| 248 |
i28 = int_add_ovf(i10, i25) |
| 249 |
guard_no_overflow(descr=...) |
| 250 |
--TICK-- |
| 251 |
if00 = arraylen_gc(p16, descr=...) |
| 252 |
jump(..., descr=...) |
| 253 |
""") |
| 254 |
|
| 255 |
def test_dont_trace_every_iteration(self): |
| 256 |
def reference(a, b): |
| 257 |
i = sa = 0 |
| 258 |
while i < 300: |
| 259 |
sa += a % b |
| 260 |
i += 1 |
| 261 |
return sa |
| 262 |
|
| 263 |
def main(a, b): |
| 264 |
i = sa = 0 |
| 265 |
while i < 300: |
| 266 |
if a > 0: |
| 267 |
pass |
| 268 |
if 1 < b < 2: |
| 269 |
pass |
| 270 |
sa += a % b |
| 271 |
i += 1 |
| 272 |
return sa |
| 273 |
|
| 274 |
log_ref = self.run(reference, [10, 20]) |
| 275 |
assert log_ref.result == 300 * (10 % 20) |
| 276 |
|
| 277 |
log = self.run(main, [10, 20]) |
| 278 |
assert log.result == 300 * (10 % 20) |
| 279 |
assert log.jit_summary.tracing_no == log_ref.jit_summary.tracing_no |
| 280 |
loop, = log.loops_by_filename(self.filepath) |
| 281 |
assert loop.match(""" |
| 282 |
i11 = int_lt(i7, 300) |
| 283 |
guard_true(i11, descr=...) |
| 284 |
i12 = int_add_ovf(i8, i9) |
| 285 |
guard_no_overflow(descr=...) |
| 286 |
i14 = int_add(i7, 1) |
| 287 |
--TICK-- |
| 288 |
jump(..., descr=...) |
| 289 |
""") |
| 290 |
|
| 291 |
log = self.run(main, [-10, -20]) |
| 292 |
assert log.result == 300 * (-10 % -20) |
| 293 |
assert log.jit_summary.tracing_no == log_ref.jit_summary.tracing_no |
| 294 |
|
| 295 |
def test_overflow_checking(self): |
| 296 |
""" |
| 297 |
This test only checks that we get the expected result, not that any |
| 298 |
optimization has been applied. |
| 299 |
""" |
| 300 |
def main(): |
| 301 |
import sys |
| 302 |
def f(a,b): |
| 303 |
if a < 0: |
| 304 |
return -1 |
| 305 |
return a-b |
| 306 |
|
| 307 |
total = sys.maxsize - 2147483647 |
| 308 |
for i in range(100000): |
| 309 |
total += f(i, 5) |
| 310 |
|
| 311 |
return total |
| 312 |
|
| 313 |
self.run_and_check(main, []) |
| 314 |
|
| 315 |
def test_global(self): |
| 316 |
|
| 317 |
log = self.run(""" |
| 318 |
i = 0 |
| 319 |
globalinc = 1 |
| 320 |
def main(n): |
| 321 |
global i |
| 322 |
while i < n: |
| 323 |
l = globalinc # ID: globalread |
| 324 |
i += l |
| 325 |
""", [1000]) |
| 326 |
|
| 327 |
loop, = log.loops_by_id("globalread", is_entry_bridge=True) |
| 328 |
assert loop.match_by_id("globalread", """ |
| 329 |
# only a dead read |
| 330 |
dummy_get_utf8? |
| 331 |
""") |
| 332 |
|
| 333 |
def test_eval(self): |
| 334 |
def main(): |
| 335 |
i = 1 |
| 336 |
a = compile('x+x+x+x+x+x', 'eval', 'eval') |
| 337 |
b = {'x': 7} |
| 338 |
while i < 1000: |
| 339 |
y = eval(a, b, b) |
| 340 |
i += 1 |
| 341 |
return y |
| 342 |
|
| 343 |
log = self.run(main) |
| 344 |
assert log.result == 42 |
| 345 |
|
| 346 |
|
| 347 |
assert len(log.loops_by_id("eval")) == 1 |
| 348 |
|
| 349 |
def test_sys_exc_info(self): |
| 350 |
def main(): |
| 351 |
i = 1 |
| 352 |
lst = [i] |
| 353 |
while i < 1000: |
| 354 |
try: |
| 355 |
return lst[i] |
| 356 |
except: |
| 357 |
e = sys.exc_info()[1] |
| 358 |
if not isinstance(e, IndexError): |
| 359 |
raise |
| 360 |
i += 1 |
| 361 |
return 42 |
| 362 |
|
| 363 |
log = self.run(main) |
| 364 |
assert log.result == 42 |
| 365 |
|
| 366 |
|
| 367 |
assert len(log.loops_by_id("exc_info")) == 1 |
| 368 |
|
| 369 |
def test_long_comparison(self): |
| 370 |
def main(n): |
| 371 |
while n: |
| 372 |
x = 12345678901234567890123456 |
| 373 |
x > 1231231231231231231231231 |
| 374 |
n -= 1 |
| 375 |
|
| 376 |
log = self.run(main, [300]) |
| 377 |
loop, = log.loops_by_id("long_op") |
| 378 |
assert len(loop.ops_by_id("long_op")) == 0 |
| 379 |
|
| 380 |
def test_settrace(self): |
| 381 |
def main(n): |
| 382 |
import sys |
| 383 |
sys.settrace(lambda *args, **kwargs: None) |
| 384 |
|
| 385 |
def f(): |
| 386 |
return 1 |
| 387 |
|
| 388 |
while n: |
| 389 |
n -= f() |
| 390 |
|
| 391 |
log = self.run(main, [300]) |
| 392 |
loops = log.loops_by_filename(self.filepath) |
| 393 |
|
| 394 |
|
| 395 |
assert len(loops) == 1 |
| 396 |
|
| 397 |
def test_forward_jump_absolute_doesnt_start_loops(self): |
| 398 |
def main(): |
| 399 |
def f(x): |
| 400 |
for i in range(x): |
| 401 |
break |
| 402 |
|
| 403 |
for i in range(1000000): |
| 404 |
f(i) |
| 405 |
|
| 406 |
log = self.run(main, []) |
| 407 |
loops = log.loops_by_filename(self.filepath) |
| 408 |
assert len(loops) == 1 |
| 409 |
|
| 410 |
def test_stat_result_virtual(self): |
| 411 |
def main(n): |
| 412 |
import os |
| 413 |
res = 0 |
| 414 |
for i in range(n): |
| 415 |
res += os.path.islink(__file__) |
| 416 |
return res |
| 417 |
log = self.run(main, [3000]) |
| 418 |
loop, = log.loops_by_id("islink") |
| 419 |
opnames = log.opnames(loop.allops()) |
| 420 |
|
| 421 |
assert opnames.count('new_with_vtable') == 1 |
| 422 |
assert opnames.count('new') == 0 |
| 423 |
assert opnames.count('new_array_clear') == 0 |
| 424 |
|
| 425 |
@pytest.mark.skipif("sys.maxint == 2 ** 31 - 1") |
| 426 |
def test_locals(self): |
| 427 |
def main(n): |
| 428 |
res = 0 |
| 429 |
for i in range(n): |
| 430 |
locals()["abc"] = 1 |
| 431 |
res += locals()["abc"] + locals()["i"] |
| 432 |
return res |
| 433 |
log = self.run(main, [3000]) |
| 434 |
loop, = log.loops_by_filename(self.filepath) |
| 435 |
assert loop.match(""" |
| 436 |
... |
| 437 |
i82 = int_add(i76, 1) |
| 438 |
setfield_gc(p16, i82, descr=...) |
| 439 |
guard_not_invalidated(descr=...) |
| 440 |
setarrayitem_gc(p63, 1, i71, descr=...) |
| 441 |
setarrayitem_gc(p63, 2, i76, descr=...) |
| 442 |
setarrayitem_gc(p63, 0, i77, descr=...) |
| 443 |
i87 = int_add_ovf(i71, i82) |
| 444 |
guard_no_overflow(descr=...) |
| 445 |
|
| 446 |
--TICK-- |
| 447 |
|
| 448 |
i92 = arraylen_gc(p61, descr=...) |
| 449 |
i93 = arraylen_gc(p63, descr=...) |
| 450 |
jump(..., descr=...) |
| 451 |
""") |
| 452 |
|
| 453 |
def test_locals_in_inlined_function(self): |
| 454 |
def main(n): |
| 455 |
def g(): |
| 456 |
locals()["ABC"] = True |
| 457 |
return 1 |
| 458 |
res = 0 |
| 459 |
for i in range(n): |
| 460 |
res += g() |
| 461 |
return res |
| 462 |
log = self.run(main, [3000]) |
| 463 |
loop, = log.loops_by_filename(self.filepath) |
| 464 |
opnames = log.opnames(loop.allops()) |
| 465 |
assert "new" not in opnames |
| 466 |
|
| 467 |
def test_sys_flags_access(self): |
| 468 |
def main(n): |
| 469 |
import sys |
| 470 |
x = 0 |
| 471 |
for i in range(n): |
| 472 |
import sys |
| 473 |
flags = sys.flags |
| 474 |
x += flags.debug |
| 475 |
log = self.run(main, [3000]) |
| 476 |
loop, = log.loops_by_id("flags", is_entry_bridge=True) |
| 477 |
ops = loop.ops_by_id("flags") |
| 478 |
assert ops == [] |
| 479 |
|
| 480 |
def test_tuple_slice(self): |
| 481 |
def main(n): |
| 482 |
t = (1, 2, 3, 4, 5, n) |
| 483 |
res = 0 |
| 484 |
for i in range(n): |
| 485 |
res += len(t[0:5:2]) |
| 486 |
log = self.run(main, [3000]) |
| 487 |
loop, = log.loops_by_id("getslice") |
| 488 |
ops = loop.ops_by_id("getslice") |
| 489 |
opnames = log.opnames(ops) |
| 490 |
assert "new_with_vtables" not in opnames |
| 491 |
assert "call_may_force_r" not in opnames |
| 492 |
assert "call_r" in opnames |
| 493 |
|
| 494 |
def test_tuple_slice_virtual(self): |
| 495 |
def main(n): |
| 496 |
t = (1, 2, 3, 4, 5, n) |
| 497 |
res = 0 |
| 498 |
for i in range(n): |
| 499 |
t = (1, 2, 3, 4, 5, n) |
| 500 |
res += len(t[slice(0, 5)]) |
| 501 |
log = self.run(main, [3000]) |
| 502 |
loop, = log.loops_by_id("getslice") |
| 503 |
ops = loop.ops_by_id("getslice") |
| 504 |
opnames = log.opnames(ops) |
| 505 |
assert "new_with_vtables" not in opnames |
| 506 |
assert "call_may_force_r" not in opnames |
| 507 |
assert "new_array_clear" not in opnames |
| 508 |
|
| 509 |
def test_id_no_rbigint(self): |
| 510 |
def main(n): |
| 511 |
l = [object() for i in range(n)] |
| 512 |
res = 0 |
| 513 |
for obj in l: |
| 514 |
res ^= id(obj) |
| 515 |
log = self.run(main, [3000]) |
| 516 |
loop, = log.loops_by_id("id") |
| 517 |
ops = loop.ops_by_id("id") |
| 518 |
opnames = log.opnames(ops) |
| 519 |
if sys.maxsize > 2**32: |
| 520 |
|
| 521 |
|
| 522 |
assert "call_r" not in opnames |
| 523 |
assert opnames.count('call_i') == 1 |