| 1 |
import sys, py |
| 2 |
|
| 3 |
from rpython.jit.metainterp.test.support import LLJitMixin |
| 4 |
from rpython.rlib import jit |
| 5 |
from rpython.rlib.rarithmetic import ovfcheck |
| 6 |
from rpython.rlib.rstring import StringBuilder |
| 7 |
|
| 8 |
|
| 9 |
class TestLLtype(LLJitMixin): |
| 10 |
def test_dont_record_repeated_guard_class(self): |
| 11 |
class A: |
| 12 |
pass |
| 13 |
class B(A): |
| 14 |
pass |
| 15 |
@jit.dont_look_inside |
| 16 |
def extern(n): |
| 17 |
if n == -7: |
| 18 |
return None |
| 19 |
elif n: |
| 20 |
return A() |
| 21 |
else: |
| 22 |
return B() |
| 23 |
def fn(n): |
| 24 |
obj = extern(n) |
| 25 |
return isinstance(obj, B) + isinstance(obj, B) + isinstance(obj, B) + isinstance(obj, B) |
| 26 |
res = self.interp_operations(fn, [0]) |
| 27 |
assert res == 4 |
| 28 |
self.check_operations_history(guard_class=1, guard_nonnull=1) |
| 29 |
res = self.interp_operations(fn, [1]) |
| 30 |
assert not res |
| 31 |
|
| 32 |
def test_dont_record_guard_class_after_new(self): |
| 33 |
class A: |
| 34 |
pass |
| 35 |
class B(A): |
| 36 |
pass |
| 37 |
def fn(n): |
| 38 |
if n == -7: |
| 39 |
obj = None |
| 40 |
elif n: |
| 41 |
obj = A() |
| 42 |
else: |
| 43 |
obj = B() |
| 44 |
return isinstance(obj, B) + isinstance(obj, B) + isinstance(obj, B) + isinstance(obj, B) |
| 45 |
res = self.interp_operations(fn, [0]) |
| 46 |
assert res == 4 |
| 47 |
self.check_operations_history(guard_class=0, guard_nonnull=0) |
| 48 |
res = self.interp_operations(fn, [1]) |
| 49 |
assert not res |
| 50 |
|
| 51 |
def test_guard_isnull_nullifies(self): |
| 52 |
class A: |
| 53 |
pass |
| 54 |
a = A() |
| 55 |
a.x = None |
| 56 |
def fn(n): |
| 57 |
if n == -7: |
| 58 |
a.x = "" |
| 59 |
obj = a.x |
| 60 |
res = 0 |
| 61 |
if not obj: |
| 62 |
res += 1 |
| 63 |
if obj: |
| 64 |
res += 1 |
| 65 |
if obj is None: |
| 66 |
res += 1 |
| 67 |
if obj is not None: |
| 68 |
res += 1 |
| 69 |
return res |
| 70 |
res = self.interp_operations(fn, [0]) |
| 71 |
assert res == 2 |
| 72 |
self.check_operations_history(guard_isnull=1) |
| 73 |
|
| 74 |
def test_heap_caching_while_tracing(self): |
| 75 |
class A: |
| 76 |
pass |
| 77 |
|
| 78 |
@jit.dont_look_inside |
| 79 |
def get(): |
| 80 |
return A() |
| 81 |
|
| 82 |
def fn(n): |
| 83 |
a1 = get() |
| 84 |
a2 = get() |
| 85 |
if n > 0: |
| 86 |
a = a1 |
| 87 |
else: |
| 88 |
a = a2 |
| 89 |
a.x = n |
| 90 |
return a.x |
| 91 |
res = self.interp_operations(fn, [7]) |
| 92 |
assert res == 7 |
| 93 |
self.check_operations_history(getfield_gc_i=0) |
| 94 |
res = self.interp_operations(fn, [-7]) |
| 95 |
assert res == -7 |
| 96 |
self.check_operations_history(getfield_gc_i=0) |
| 97 |
|
| 98 |
def fn(n, ca, cb): |
| 99 |
a1 = get() |
| 100 |
a2 = get() |
| 101 |
a1.x = n |
| 102 |
a2.x = n |
| 103 |
a = a1 |
| 104 |
if ca: |
| 105 |
a = a2 |
| 106 |
b = a1 |
| 107 |
if cb: |
| 108 |
b = a |
| 109 |
return a.x + b.x |
| 110 |
res = self.interp_operations(fn, [7, 0, 1]) |
| 111 |
assert res == 7 * 2 |
| 112 |
self.check_operations_history(getfield_gc_i=1) |
| 113 |
res = self.interp_operations(fn, [-7, 1, 1]) |
| 114 |
assert res == -7 * 2 |
| 115 |
self.check_operations_history(getfield_gc_i=0) |
| 116 |
|
| 117 |
def test_heap_caching_nonnull(self): |
| 118 |
class A: |
| 119 |
def __init__(self, x=None): |
| 120 |
self.next = x |
| 121 |
a0 = A() |
| 122 |
a1 = A() |
| 123 |
a2 = A(a1) |
| 124 |
def fn(n): |
| 125 |
if n > 0: |
| 126 |
a = a1 |
| 127 |
else: |
| 128 |
a = a2 |
| 129 |
if a.next: |
| 130 |
a = A(a.next) |
| 131 |
result = a.next is not None |
| 132 |
a0.next = a |
| 133 |
return result |
| 134 |
return False |
| 135 |
res = self.interp_operations(fn, [-7]) |
| 136 |
assert res == True |
| 137 |
self.check_operations_history(guard_nonnull=1) |
| 138 |
|
| 139 |
def test_heap_caching_while_tracing_invalidation(self): |
| 140 |
class A: |
| 141 |
pass |
| 142 |
@jit.dont_look_inside |
| 143 |
def f(a): |
| 144 |
a.x = 5 |
| 145 |
@jit.dont_look_inside |
| 146 |
def get(): |
| 147 |
return A() |
| 148 |
l = [1] |
| 149 |
def fn(n): |
| 150 |
if n > 0: |
| 151 |
a = get() |
| 152 |
else: |
| 153 |
a = get() |
| 154 |
a.x = n |
| 155 |
x1 = a.x |
| 156 |
f(a) |
| 157 |
x2 = a.x |
| 158 |
l[0] = x2 |
| 159 |
return a.x + x1 + x2 |
| 160 |
res = self.interp_operations(fn, [7]) |
| 161 |
assert res == 5 * 2 + 7 |
| 162 |
self.check_operations_history(getfield_gc_i=1) |
| 163 |
|
| 164 |
def test_heap_caching_dont_store_same(self): |
| 165 |
class A: |
| 166 |
pass |
| 167 |
@jit.dont_look_inside |
| 168 |
def get(): |
| 169 |
return A() |
| 170 |
def fn(n): |
| 171 |
if n > 0: |
| 172 |
a = get() |
| 173 |
else: |
| 174 |
a = get() |
| 175 |
a.x = n |
| 176 |
a.x = n |
| 177 |
return a.x |
| 178 |
res = self.interp_operations(fn, [7]) |
| 179 |
assert res == 7 |
| 180 |
self.check_operations_history(getfield_gc_i=0, setfield_gc=1) |
| 181 |
res = self.interp_operations(fn, [-7]) |
| 182 |
assert res == -7 |
| 183 |
self.check_operations_history(getfield_gc_i=0) |
| 184 |
|
| 185 |
def test_array_caching(self): |
| 186 |
a1 = [0, 0] |
| 187 |
a2 = [0, 0] |
| 188 |
def fn(n): |
| 189 |
if n > 0: |
| 190 |
a = a1 |
| 191 |
else: |
| 192 |
a = a2 |
| 193 |
a[0] = n |
| 194 |
x1 = a[0] |
| 195 |
a[n - n] = n + 1 |
| 196 |
return a[0] + x1 |
| 197 |
res = self.interp_operations(fn, [7]) |
| 198 |
assert res == 7 + 7 + 1 |
| 199 |
self.check_operations_history(getarrayitem_gc_i=1) |
| 200 |
res = self.interp_operations(fn, [-7]) |
| 201 |
assert res == -7 - 7 + 1 |
| 202 |
self.check_operations_history(getarrayitem_gc_i=1) |
| 203 |
|
| 204 |
def fn(n, ca, cb): |
| 205 |
a1[0] = n |
| 206 |
a2[0] = n |
| 207 |
a = a1 |
| 208 |
if ca: |
| 209 |
a = a2 |
| 210 |
b = a1 |
| 211 |
if cb: |
| 212 |
b = a |
| 213 |
return a[0] + b[0] |
| 214 |
res = self.interp_operations(fn, [7, 0, 1]) |
| 215 |
assert res == 7 * 2 |
| 216 |
self.check_operations_history(getarrayitem_gc_i=1) |
| 217 |
res = self.interp_operations(fn, [-7, 1, 1]) |
| 218 |
assert res == -7 * 2 |
| 219 |
self.check_operations_history(getarrayitem_gc_i=0) |
| 220 |
|
| 221 |
def test_array_caching_float(self): |
| 222 |
a1 = [0.0, 0.0] |
| 223 |
a2 = [0.0, 0.0] |
| 224 |
def fn(n): |
| 225 |
if n > 0: |
| 226 |
a = a1 |
| 227 |
else: |
| 228 |
a = a2 |
| 229 |
a[0] = n + 0.01 |
| 230 |
x1 = a[0] |
| 231 |
a[n - n] = n + 0.1 |
| 232 |
return a[0] + x1 |
| 233 |
res = self.interp_operations(fn, [7]) |
| 234 |
assert res == 7 + 7 + 0.01 + 0.1 |
| 235 |
self.check_operations_history(getarrayitem_gc_f=1) |
| 236 |
res = self.interp_operations(fn, [-7]) |
| 237 |
assert res == -7 - 7 + 0.01 + 0.1 |
| 238 |
self.check_operations_history(getarrayitem_gc_f=1) |
| 239 |
|
| 240 |
def fn(n, ca, cb): |
| 241 |
a1[0] = n + 0.01 |
| 242 |
a2[0] = n + 0.01 |
| 243 |
a = a1 |
| 244 |
if ca: |
| 245 |
a = a2 |
| 246 |
b = a1 |
| 247 |
if cb: |
| 248 |
b = a |
| 249 |
return a[0] + b[0] |
| 250 |
res = self.interp_operations(fn, [7, 0, 1]) |
| 251 |
assert res == (7 + 0.01) * 2 |
| 252 |
self.check_operations_history(getarrayitem_gc_f=1) |
| 253 |
res = self.interp_operations(fn, [-7, 1, 1]) |
| 254 |
assert res == (-7 + 0.01) * 2 |
| 255 |
self.check_operations_history(getarrayitem_gc_f=0) |
| 256 |
|
| 257 |
def test_array_caching_while_tracing_invalidation(self): |
| 258 |
a1 = [0, 0] |
| 259 |
a2 = [0, 0] |
| 260 |
@jit.dont_look_inside |
| 261 |
def f(a): |
| 262 |
a[0] = 5 |
| 263 |
class A: pass |
| 264 |
l = A() |
| 265 |
def fn(n): |
| 266 |
if n > 0: |
| 267 |
a = a1 |
| 268 |
else: |
| 269 |
a = a2 |
| 270 |
a[0] = n |
| 271 |
x1 = a[0] |
| 272 |
f(a) |
| 273 |
x2 = a[0] |
| 274 |
l.x = x2 |
| 275 |
return a[0] + x1 + x2 |
| 276 |
res = self.interp_operations(fn, [7]) |
| 277 |
assert res == 5 * 2 + 7 |
| 278 |
self.check_operations_history(getarrayitem_gc_i=1) |
| 279 |
|
| 280 |
def test_array_and_getfield_interaction(self): |
| 281 |
class A: pass |
| 282 |
@jit.dont_look_inside |
| 283 |
def get(): |
| 284 |
a = A() |
| 285 |
a.l = [0, 0] |
| 286 |
return a |
| 287 |
def fn(n): |
| 288 |
if n > 0: |
| 289 |
a = get() |
| 290 |
else: |
| 291 |
a = get() |
| 292 |
a.l = [0, 0] |
| 293 |
a.x = 0 |
| 294 |
a.l[a.x] = n |
| 295 |
a.x += 1 |
| 296 |
a.l[a.x] = n + 1 |
| 297 |
x1 = a.l[a.x] |
| 298 |
a.x -= 1 |
| 299 |
x2 = a.l[a.x] |
| 300 |
return x1 + x2 |
| 301 |
res = self.interp_operations(fn, [7]) |
| 302 |
assert res == 7 * 2 + 1 |
| 303 |
self.check_operations_history(setarrayitem_gc=2, setfield_gc=3, |
| 304 |
getarrayitem_gc_i=0, getfield_gc_r=1) |
| 305 |
|
| 306 |
def test_promote_changes_heap_cache(self): |
| 307 |
class A: pass |
| 308 |
@jit.dont_look_inside |
| 309 |
def get(): |
| 310 |
a = A() |
| 311 |
a.l = [0, 0] |
| 312 |
a.x = 0 |
| 313 |
return a |
| 314 |
def fn(n): |
| 315 |
if n > 0: |
| 316 |
a = get() |
| 317 |
else: |
| 318 |
a = get() |
| 319 |
a.l = [0, 0] |
| 320 |
jit.promote(a.x) |
| 321 |
a.l[a.x] = n |
| 322 |
a.x += 1 |
| 323 |
a.l[a.x] = n + 1 |
| 324 |
x1 = a.l[a.x] |
| 325 |
a.x -= 1 |
| 326 |
x2 = a.l[a.x] |
| 327 |
return x1 + x2 |
| 328 |
res = self.interp_operations(fn, [7]) |
| 329 |
assert res == 7 * 2 + 1 |
| 330 |
self.check_operations_history(setarrayitem_gc=2, setfield_gc=2, |
| 331 |
getarrayitem_gc_i=0, getfield_gc_i=1, |
| 332 |
getfield_gc_r=1) |
| 333 |
|
| 334 |
def test_promote_changes_array_cache(self): |
| 335 |
@jit.dont_look_inside |
| 336 |
def get(): |
| 337 |
return [0, 0] |
| 338 |
def fn(n): |
| 339 |
a = get() |
| 340 |
a[0] = n |
| 341 |
jit.hint(n, promote=True) |
| 342 |
x1 = a[0] |
| 343 |
jit.hint(x1, promote=True) |
| 344 |
a[n - n] = n + 1 |
| 345 |
return a[0] + x1 |
| 346 |
res = self.interp_operations(fn, [7]) |
| 347 |
assert res == 7 + 7 + 1 |
| 348 |
self.check_operations_history(getarrayitem_gc_i=0, guard_value=1) |
| 349 |
res = self.interp_operations(fn, [-7]) |
| 350 |
assert res == -7 - 7 + 1 |
| 351 |
self.check_operations_history(getarrayitem_gc_i=0, guard_value=1) |
| 352 |
|
| 353 |
|
| 354 |
def test_list_caching(self): |
| 355 |
@jit.dont_look_inside |
| 356 |
def get(): |
| 357 |
return [0, 0] |
| 358 |
def fn(n): |
| 359 |
a = get() |
| 360 |
if not n > 0: |
| 361 |
if n < -1000: |
| 362 |
a.append(5) |
| 363 |
a[0] = n |
| 364 |
x1 = a[0] |
| 365 |
a[n - n] = n + 1 |
| 366 |
return a[0] + x1 |
| 367 |
res = self.interp_operations(fn, [7]) |
| 368 |
assert res == 7 + 7 + 1 |
| 369 |
self.check_operations_history(getarrayitem_gc_i=1, |
| 370 |
getfield_gc_r=1) |
| 371 |
res = self.interp_operations(fn, [-7]) |
| 372 |
assert res == -7 - 7 + 1 |
| 373 |
self.check_operations_history(getarrayitem_gc_i=1, |
| 374 |
getfield_gc_r=1) |
| 375 |
|
| 376 |
def fn(n, ca, cb): |
| 377 |
a1 = get() |
| 378 |
a2 = get() |
| 379 |
a1[0] = n |
| 380 |
a2[0] = n |
| 381 |
a = a1 |
| 382 |
if ca: |
| 383 |
a = a2 |
| 384 |
if n < -100: |
| 385 |
a.append(5) |
| 386 |
b = a1 |
| 387 |
if cb: |
| 388 |
b = a |
| 389 |
return a[0] + b[0] |
| 390 |
res = self.interp_operations(fn, [7, 0, 1]) |
| 391 |
assert res == 7 * 2 |
| 392 |
self.check_operations_history(getarrayitem_gc_i=1, |
| 393 |
getfield_gc_r=2) |
| 394 |
res = self.interp_operations(fn, [-7, 1, 1]) |
| 395 |
assert res == -7 * 2 |
| 396 |
self.check_operations_history(getarrayitem_gc_i=0, |
| 397 |
getfield_gc_r=2) |
| 398 |
|
| 399 |
def test_list_caching_negative(self): |
| 400 |
def fn(n): |
| 401 |
a = [0] * n |
| 402 |
if n > 1000: |
| 403 |
a.append(0) |
| 404 |
a[-1] = n |
| 405 |
x1 = a[-1] |
| 406 |
a[n - n - 1] = n + 1 |
| 407 |
return a[-1] + x1 + 1000 * a[2] |
| 408 |
res = self.interp_operations(fn, [7]) |
| 409 |
assert res == 7 + 7 + 1 |
| 410 |
self.check_operations_history(setarrayitem_gc=2, |
| 411 |
setfield_gc=2, call_n=0, call_i=0, call_r=0) |
| 412 |
|
| 413 |
def test_list_caching_negative_nonzero_init(self): |
| 414 |
def fn(n): |
| 415 |
a = [42] * n |
| 416 |
if n > 1000: |
| 417 |
a.append(0) |
| 418 |
a[-1] = n |
| 419 |
x1 = a[-1] |
| 420 |
a[n - n - 1] = n + 1 |
| 421 |
return a[-1] + x1 + 1000 * a[2] |
| 422 |
res = self.interp_operations(fn, [7]) |
| 423 |
assert res == 7 + 7 + 1 + 42000 |
| 424 |
self.check_operations_history(setarrayitem_gc=2, |
| 425 |
setfield_gc=0, call_r=1) |
| 426 |
|
| 427 |
def test_virtualizable_with_array_heap_cache(self): |
| 428 |
myjitdriver = jit.JitDriver(greens = [], reds = ['n', 'x', 'i', 'frame'], |
| 429 |
virtualizables = ['frame']) |
| 430 |
|
| 431 |
class Frame(object): |
| 432 |
_virtualizable_ = ['l[*]', 's'] |
| 433 |
|
| 434 |
def __init__(self, a, s): |
| 435 |
self = jit.hint(self, access_directly=True, fresh_virtualizable=True) |
| 436 |
self.l = [0] * (4 + a) |
| 437 |
self.s = s |
| 438 |
|
| 439 |
def f(n, a, i): |
| 440 |
frame = Frame(a, 0) |
| 441 |
frame.l[0] = a |
| 442 |
frame.l[1] = a + 1 |
| 443 |
frame.l[2] = a + 2 |
| 444 |
frame.l[3] = a + 3 |
| 445 |
if not i: |
| 446 |
return frame.l[0] + len(frame.l) |
| 447 |
x = 0 |
| 448 |
while n > 0: |
| 449 |
myjitdriver.can_enter_jit(frame=frame, n=n, x=x, i=i) |
| 450 |
myjitdriver.jit_merge_point(frame=frame, n=n, x=x, i=i) |
| 451 |
frame.s = jit.promote(frame.s) |
| 452 |
n -= 1 |
| 453 |
s = frame.s |
| 454 |
assert s >= 0 |
| 455 |
x += frame.l[s] |
| 456 |
frame.s += 1 |
| 457 |
s = frame.s |
| 458 |
assert s >= 0 |
| 459 |
x += frame.l[s] |
| 460 |
x += len(frame.l) |
| 461 |
x += f(n, n, 0) |
| 462 |
frame.s -= 1 |
| 463 |
return x |
| 464 |
|
| 465 |
res = self.meta_interp(f, [10, 1, 1], listops=True) |
| 466 |
assert res == f(10, 1, 1) |
| 467 |
self.check_history(getarrayitem_gc_i=0, getfield_gc_i=0, |
| 468 |
getfield_gc_r=0) |
| 469 |
|
| 470 |
|
| 471 |
def test_nonstandard_virtualizable(self): |
| 472 |
myjitdriver = jit.JitDriver(greens = [], reds = ['n', 'x', 'i', 'frame'], |
| 473 |
virtualizables = ['frame']) |
| 474 |
|
| 475 |
class Frame(object): |
| 476 |
_virtualizable_ = ['s'] |
| 477 |
|
| 478 |
def __init__(self, s): |
| 479 |
self.s = s |
| 480 |
self.next = None |
| 481 |
|
| 482 |
def f(n, a, i): |
| 483 |
frame = Frame(5) |
| 484 |
x = 0 |
| 485 |
while n > 0: |
| 486 |
myjitdriver.can_enter_jit(frame=frame, n=n, x=x, i=i) |
| 487 |
myjitdriver.jit_merge_point(frame=frame, n=n, x=x, i=i) |
| 488 |
n -= 1 |
| 489 |
s = frame.s |
| 490 |
assert s >= 0 |
| 491 |
frame.s += 1 |
| 492 |
|
| 493 |
f = Frame(7) |
| 494 |
frame.next = f |
| 495 |
x += f.s |
| 496 |
frame.s -= 1 |
| 497 |
frame.next = None |
| 498 |
return x |
| 499 |
|
| 500 |
res = self.meta_interp(f, [10, 1, 1], listops=True) |
| 501 |
assert res == f(10, 1, 1) |
| 502 |
|
| 503 |
|
| 504 |
self.check_history(ptr_eq=0) |
| 505 |
|
| 506 |
|
| 507 |
def test_heap_caching_array_pure(self): |
| 508 |
class A(object): |
| 509 |
pass |
| 510 |
p1 = A() |
| 511 |
p2 = A() |
| 512 |
def fn(n): |
| 513 |
if n >= 0: |
| 514 |
a = (n, n + 1) |
| 515 |
p = p1 |
| 516 |
else: |
| 517 |
a = (n + 1, n) |
| 518 |
p = p2 |
| 519 |
p.x = a |
| 520 |
|
| 521 |
return p.x[0] + p.x[1] |
| 522 |
res = self.interp_operations(fn, [7]) |
| 523 |
assert res == 7 + 7 + 1 |
| 524 |
self.check_operations_history(getfield_gc_r=0) |
| 525 |
res = self.interp_operations(fn, [-7]) |
| 526 |
assert res == -7 - 7 + 1 |
| 527 |
self.check_operations_history(getfield_gc_r=0) |
| 528 |
|
| 529 |
def test_heap_caching_and_elidable_function(self): |
| 530 |
class A: |
| 531 |
pass |
| 532 |
@jit.dont_look_inside |
| 533 |
def get(): |
| 534 |
return A() |
| 535 |
@jit.elidable |
| 536 |
def f(b): |
| 537 |
return b + 1 |
| 538 |
def fn(n): |
| 539 |
if n > 0: |
| 540 |
a = get() |
| 541 |
else: |
| 542 |
a = A() |
| 543 |
a.x = n |
| 544 |
z = f(6) |
| 545 |
return z + a.x |
| 546 |
res = self.interp_operations(fn, [7]) |
| 547 |
assert res == 7 + 7 |
| 548 |
self.check_operations_history(getfield_gc_i=0) |
| 549 |
res = self.interp_operations(fn, [-7]) |
| 550 |
assert res == -7 + 7 |
| 551 |
self.check_operations_history(getfield_gc_i=0) |
| 552 |
|
| 553 |
def test_heap_caching_multiple_objects(self): |
| 554 |
class Gbl(object): |
| 555 |
pass |
| 556 |
g = Gbl() |
| 557 |
class A(object): |
| 558 |
pass |
| 559 |
a1 = A() |
| 560 |
g.a1 = a1 |
| 561 |
a1.x = 7 |
| 562 |
a2 = A() |
| 563 |
g.a2 = a2 |
| 564 |
a2.x = 7 |
| 565 |
def gn(a1, a2): |
| 566 |
return a1.x + a2.x |
| 567 |
def fn(n): |
| 568 |
if n < 0: |
| 569 |
a1 = A() |
| 570 |
g.a1 = a1 |
| 571 |
a1.x = n |
| 572 |
a2 = A() |
| 573 |
g.a2 = a2 |
| 574 |
a2.x = n - 1 |
| 575 |
else: |
| 576 |
a1 = g.a1 |
| 577 |
a2 = g.a2 |
| 578 |
return a1.x + a2.x + gn(a1, a2) |
| 579 |
res = self.interp_operations(fn, [-7]) |
| 580 |
assert res == 2 * -7 + 2 * -8 |
| 581 |
self.check_operations_history(setfield_gc=4, getfield_gc_i=0, |
| 582 |
getfield_gc_r=0) |
| 583 |
res = self.interp_operations(fn, [7]) |
| 584 |
assert res == 4 * 7 |
| 585 |
self.check_operations_history(getfield_gc_i=2, getfield_gc_r=2) |
| 586 |
|
| 587 |
def test_heap_caching_quasi_immutable(self): |
| 588 |
class A: |
| 589 |
_immutable_fields_ = ['x?'] |
| 590 |
a1 = A() |
| 591 |
a1.x = 5 |
| 592 |
a2 = A() |
| 593 |
a2.x = 7 |
| 594 |
|
| 595 |
@jit.elidable |
| 596 |
def get(n): |
| 597 |
if n > 0: |
| 598 |
return a1 |
| 599 |
return a2 |
| 600 |
|
| 601 |
def g(a): |
| 602 |
return a.x |
| 603 |
|
| 604 |
def fn(n): |
| 605 |
jit.promote(n) |
| 606 |
a = get(n) |
| 607 |
return g(a) + a.x |
| 608 |
res = self.interp_operations(fn, [7]) |
| 609 |
assert res == 10 |
| 610 |
self.check_operations_history(quasiimmut_field=1) |
| 611 |
|
| 612 |
def test_heap_caching_quasi_immutable_2(self): |
| 613 |
class A: |
| 614 |
_immutable_fields_ = ['x?'] |
| 615 |
a1 = A() |
| 616 |
a1.x = 5 |
| 617 |
a2 = A() |
| 618 |
a2.x = 7 |
| 619 |
|
| 620 |
@jit.elidable |
| 621 |
def get(n): |
| 622 |
if n > 0: |
| 623 |
return a1 |
| 624 |
return a2 |
| 625 |
|
| 626 |
def g(a): |
| 627 |
return a.x |
| 628 |
|
| 629 |
def fn(n): |
| 630 |
jit.promote(n) |
| 631 |
return get(n).x + get(n).x |
| 632 |
res = self.interp_operations(fn, [7]) |
| 633 |
assert res == 10 |
| 634 |
self.check_operations_history(quasiimmut_field=1) |
| 635 |
|
| 636 |
|
| 637 |
|
| 638 |
def test_heap_caching_multiple_tuples(self): |
| 639 |
class Gbl(object): |
| 640 |
pass |
| 641 |
g = Gbl() |
| 642 |
def gn(a1, a2): |
| 643 |
return a1[0] + a2[0] |
| 644 |
def fn(n): |
| 645 |
a1 = (n, ) |
| 646 |
g.a = a1 |
| 647 |
a2 = (n - 1, ) |
| 648 |
g.a = a2 |
| 649 |
jit.promote(n) |
| 650 |
return a1[0] + a2[0] + gn(a1, a2) |
| 651 |
res = self.interp_operations(fn, [7]) |
| 652 |
assert res == 2 * 7 + 2 * 6 |
| 653 |
self.check_operations_history(getfield_gc_i=0, |
| 654 |
getfield_gc_r=0) |
| 655 |
res = self.interp_operations(fn, [-7]) |
| 656 |
assert res == 2 * -7 + 2 * -8 |
| 657 |
self.check_operations_history(getfield_gc_i=0, |
| 658 |
getfield_gc_r=0) |
| 659 |
|
| 660 |
def test_heap_caching_multiple_arrays(self): |
| 661 |
class Gbl(object): |
| 662 |
pass |
| 663 |
g = Gbl() |
| 664 |
def fn(n): |
| 665 |
a1 = [n, n, n] |
| 666 |
g.a = a1 |
| 667 |
a1[0] = n |
| 668 |
a2 = [n, n, n] |
| 669 |
g.a = a2 |
| 670 |
a2[0] = n - 1 |
| 671 |
return a1[0] + a2[0] + a1[0] + a2[0] |
| 672 |
res = self.interp_operations(fn, [7]) |
| 673 |
assert res == 2 * 7 + 2 * 6 |
| 674 |
self.check_operations_history(getarrayitem_gc_i=0) |
| 675 |
res = self.interp_operations(fn, [-7]) |
| 676 |
assert res == 2 * -7 + 2 * -8 |
| 677 |
self.check_operations_history(getarrayitem_gc_i=0) |
| 678 |
|
| 679 |
def test_heap_caching_multiple_arrays_getarrayitem(self): |
| 680 |
class Gbl(object): |
| 681 |
pass |
| 682 |
g = Gbl() |
| 683 |
g.a1 = [7, 8, 9] |
| 684 |
g.a2 = [8, 9, 10, 11] |
| 685 |
|
| 686 |
def fn(i): |
| 687 |
if i < 0: |
| 688 |
g.a1 = [7, 8, 9] |
| 689 |
g.a2 = [7, 8, 9, 10] |
| 690 |
jit.promote(i) |
| 691 |
a1 = g.a1 |
| 692 |
a1[i + 1] = 15 |
| 693 |
a2 = g.a2 |
| 694 |
a2[i + 1] = 19 |
| 695 |
return a1[i] + a2[i] + a1[i] + a2[i] |
| 696 |
res = self.interp_operations(fn, [0]) |
| 697 |
assert res == 2 * 7 + 2 * 8 |
| 698 |
self.check_operations_history(getarrayitem_gc_i=2) |
| 699 |
|
| 700 |
|
| 701 |
def test_heap_caching_multiple_lists(self): |
| 702 |
class Gbl(object): |
| 703 |
pass |
| 704 |
g = Gbl() |
| 705 |
g.l = [] |
| 706 |
def fn(n): |
| 707 |
if n < -100: |
| 708 |
g.l.append(1) |
| 709 |
a1 = [n, n, n] |
| 710 |
g.l = a1 |
| 711 |
a1[0] = n |
| 712 |
a2 = [n, n, n] |
| 713 |
g.l = a2 |
| 714 |
a2[0] = n - 1 |
| 715 |
return a1[0] + a2[0] + a1[0] + a2[0] |
| 716 |
res = self.interp_operations(fn, [7]) |
| 717 |
assert res == 2 * 7 + 2 * 6 |
| 718 |
self.check_operations_history(getarrayitem_gc_i=0, getfield_gc_i=0, |
| 719 |
getfield_gc_r=0) |
| 720 |
res = self.interp_operations(fn, [-7]) |
| 721 |
assert res == 2 * -7 + 2 * -8 |
| 722 |
self.check_operations_history(getarrayitem_gc_i=0, getfield_gc_i=0, |
| 723 |
getfield_gc_r=0) |
| 724 |
|
| 725 |
def test_length_caching(self): |
| 726 |
class Gbl(object): |
| 727 |
pass |
| 728 |
g = Gbl() |
| 729 |
g.a = [0] * 7 |
| 730 |
def fn(n): |
| 731 |
a = g.a |
| 732 |
res = len(a) + len(a) |
| 733 |
a1 = [0] * n |
| 734 |
g.a = a1 |
| 735 |
return len(a1) + res |
| 736 |
res = self.interp_operations(fn, [7], backendopt=True) |
| 737 |
assert res == 7 * 3 |
| 738 |
self.check_operations_history(arraylen_gc=1) |
| 739 |
|
| 740 |
def test_arraycopy(self): |
| 741 |
class Gbl(object): |
| 742 |
pass |
| 743 |
g = Gbl() |
| 744 |
g.a = [0] * 7 |
| 745 |
def fn(n): |
| 746 |
assert n >= 0 |
| 747 |
a = g.a |
| 748 |
x = [0] * n |
| 749 |
x[2] = 21 |
| 750 |
return len(a[:n]) + x[2] |
| 751 |
res = self.interp_operations(fn, [3], backendopt=True) |
| 752 |
assert res == 24 |
| 753 |
self.check_operations_history(getarrayitem_gc_i=0) |
| 754 |
|
| 755 |
def test_fold_int_add_ovf(self): |
| 756 |
def fn(n): |
| 757 |
jit.promote(n) |
| 758 |
try: |
| 759 |
n = ovfcheck(n + 1) |
| 760 |
except OverflowError: |
| 761 |
return 12 |
| 762 |
else: |
| 763 |
return n |
| 764 |
res = self.interp_operations(fn, [3]) |
| 765 |
assert res == 4 |
| 766 |
self.check_operations_history(int_add_ovf=0) |
| 767 |
res = self.interp_operations(fn, [sys.maxint]) |
| 768 |
assert res == 12 |
| 769 |
|
| 770 |
def test_opaque_list(self): |
| 771 |
from rpython.rlib.rerased import new_erasing_pair |
| 772 |
erase, unerase = new_erasing_pair("test_opaque_list") |
| 773 |
def fn(n, ca, cb): |
| 774 |
l1 = [n] |
| 775 |
l2 = [n] |
| 776 |
a1 = erase(l1) |
| 777 |
a2 = erase(l1) |
| 778 |
a = a1 |
| 779 |
if ca: |
| 780 |
a = a2 |
| 781 |
if n < -100: |
| 782 |
unerase(a).append(5) |
| 783 |
b = a1 |
| 784 |
if cb: |
| 785 |
b = a |
| 786 |
return unerase(a)[0] + unerase(b)[0] |
| 787 |
res = self.interp_operations(fn, [7, 0, 1]) |
| 788 |
assert res == 7 * 2 |
| 789 |
self.check_operations_history(getarrayitem_gc_i=0, |
| 790 |
getfield_gc_i=0, getfield_gc_r=0) |
| 791 |
res = self.interp_operations(fn, [-7, 1, 1]) |
| 792 |
assert res == -7 * 2 |
| 793 |
self.check_operations_history(getarrayitem_gc_i=0, |
| 794 |
getfield_gc_i=0, getfield_gc_r=0) |
| 795 |
|
| 796 |
def test_copy_str_content(self): |
| 797 |
def fn(n): |
| 798 |
a = StringBuilder() |
| 799 |
x = [1] |
| 800 |
a.append("hello world") |
| 801 |
return x[0] |
| 802 |
res = self.interp_operations(fn, [0]) |
| 803 |
assert res == 1 |
| 804 |
self.check_operations_history(getarrayitem_gc_i=0, |
| 805 |
getarrayitem_gc_pure_i=0) |
| 806 |
|
| 807 |
def test_raise_known_class_no_guard_class(self): |
| 808 |
def raise_exc(cls): |
| 809 |
raise cls |
| 810 |
|
| 811 |
def fn(n): |
| 812 |
if n: |
| 813 |
cls = ValueError |
| 814 |
else: |
| 815 |
cls = TypeError |
| 816 |
try: |
| 817 |
raise_exc(cls) |
| 818 |
except ValueError: |
| 819 |
return -1 |
| 820 |
return n |
| 821 |
|
| 822 |
res = self.interp_operations(fn, [1]) |
| 823 |
assert res == -1 |
| 824 |
self.check_operations_history(guard_class=0) |
| 825 |
|
| 826 |
def test_dont_record_setfield_gc_zeros(self): |
| 827 |
py.test.skip("see test_unescaped_write_zero in test_ajit") |
| 828 |
class A(object): |
| 829 |
pass |
| 830 |
|
| 831 |
def make_a(): |
| 832 |
return A() |
| 833 |
make_a._dont_inline_ = True |
| 834 |
|
| 835 |
def fn(n): |
| 836 |
a = make_a() |
| 837 |
a.x = jit.promote(n) |
| 838 |
return a.x |
| 839 |
|
| 840 |
res = self.interp_operations(fn, [0]) |
| 841 |
assert res == 0 |
| 842 |
self.check_operations_history(setfield_gc=0) |
| 843 |
|
| 844 |
def test_record_known_class_does_not_invalidate(self): |
| 845 |
class A: |
| 846 |
pass |
| 847 |
class B(A): |
| 848 |
pass |
| 849 |
class C(object): |
| 850 |
_immutable_fields_ = ['x?'] |
| 851 |
c = C() |
| 852 |
c.x = 5 |
| 853 |
c.b = A() |
| 854 |
c.b.x = 14 |
| 855 |
def fn(n): |
| 856 |
if n == 99: |
| 857 |
c.x = 12 |
| 858 |
c.b = B() |
| 859 |
c.b.x = 12 |
| 860 |
return 15 |
| 861 |
b = c.b |
| 862 |
x = b.x |
| 863 |
jit.record_exact_class(c.b, A) |
| 864 |
y = b.x |
| 865 |
return x + y |
| 866 |
res = self.interp_operations(fn, [1]) |
| 867 |
assert res == 2 * 14 |
| 868 |
self.check_operations_history(getfield_gc_i=1) |
| 869 |
|
| 870 |
def test_loop_invariant1(self): |
| 871 |
class A(object): |
| 872 |
pass |
| 873 |
a = A() |
| 874 |
a.current_a = A() |
| 875 |
a.current_a.x = 1 |
| 876 |
@jit.loop_invariant |
| 877 |
def f(): |
| 878 |
return a.current_a |
| 879 |
|
| 880 |
@jit.loop_invariant |
| 881 |
def f1(): |
| 882 |
return a.current_a |
| 883 |
|
| 884 |
def g(x): |
| 885 |
res = 0 |
| 886 |
res += f().x |
| 887 |
res += f().x |
| 888 |
res += f().x |
| 889 |
res += f1().x |
| 890 |
res += f1().x |
| 891 |
if x > 1000: |
| 892 |
a.current_a = A() |
| 893 |
a.current_a.x = 2 |
| 894 |
return res |
| 895 |
res = self.interp_operations(g, [21]) |
| 896 |
assert res == g(21) |
| 897 |
self.check_operations_history(call_loopinvariant_r=2) |
| 898 |
|
| 899 |
def test_heapcache_interiorfields(self): |
| 900 |
def fn(n): |
| 901 |
d = {1: n, 2: n} |
| 902 |
d[4] = n + 1 |
| 903 |
return d[4] |
| 904 |
res = self.interp_operations(fn, [0]) |
| 905 |
assert res == 1 |
| 906 |
self.check_operations_history(getinteriorfield_gc_i=0) |
| 907 |
|
| 908 |
def test_heapcache_from_constant(self): |
| 909 |
class A(object): |
| 910 |
_immutable_fields_ = ['next'] |
| 911 |
pass |
| 912 |
g = A() |
| 913 |
g.x = 2 |
| 914 |
g.next = A() |
| 915 |
g.next.x = 1 |
| 916 |
g.next.next = g |
| 917 |
def fn(n): |
| 918 |
lg = g |
| 919 |
if n: |
| 920 |
lg = g.next |
| 921 |
res = lg.next.x |
| 922 |
if n: |
| 923 |
lg.next.x = 2 |
| 924 |
res += lg.next.x |
| 925 |
return res |
| 926 |
res = self.interp_operations(fn, [0]) |
| 927 |
assert res == 2 |
| 928 |
self.check_operations_history(getfield_gc_r=0) |
| 929 |
|
| 930 |
def test_constify_bools(self): |
| 931 |
class A(object): |
| 932 |
pass |
| 933 |
a = A() |
| 934 |
a.x = True |
| 935 |
a.y = False |
| 936 |
def fn(n): |
| 937 |
if n == 123141: |
| 938 |
a.x = False |
| 939 |
a.y = True |
| 940 |
if a.x: |
| 941 |
res = 0 |
| 942 |
else: |
| 943 |
res = 1 |
| 944 |
if a.x: |
| 945 |
res += 2 |
| 946 |
else: |
| 947 |
res += 4 |
| 948 |
|
| 949 |
if a.y: |
| 950 |
res += 12 |
| 951 |
else: |
| 952 |
res += 20 |
| 953 |
if a.y: |
| 954 |
res += 2 |
| 955 |
else: |
| 956 |
res += 4 |
| 957 |
return res |
| 958 |
res = self.interp_operations(fn, [0]) |
| 959 |
assert res == 26 |
| 960 |
self.check_operations_history(guard_true=1, guard_false=2) |
| 961 |
|