| 1 |
import math |
| 2 |
import sys |
| 3 |
|
| 4 |
import py |
| 5 |
import pytest |
| 6 |
|
| 7 |
from rpython.rlib import rgc |
| 8 |
from rpython.jit.codewriter.policy import StopAtXPolicy |
| 9 |
from rpython.jit.metainterp import history |
| 10 |
from rpython.jit.metainterp.test.support import LLJitMixin, noConst |
| 11 |
from rpython.jit.metainterp.warmspot import get_stats |
| 12 |
from rpython.jit.metainterp.pyjitpl import MetaInterp |
| 13 |
from rpython.rlib import rerased |
| 14 |
from rpython.rlib.jit import (JitDriver, we_are_jitted, hint, dont_look_inside, |
| 15 |
loop_invariant, elidable, promote, jit_debug, assert_green, |
| 16 |
AssertGreenFailed, unroll_safe, current_trace_length, look_inside_iff, |
| 17 |
isconstant, isvirtual, set_param, record_exact_class, record_known_result, |
| 18 |
record_exact_value, loop_unrolling_heuristic) |
| 19 |
from rpython.rlib.longlong2float import float2longlong, longlong2float |
| 20 |
from rpython.rlib.rarithmetic import ovfcheck, is_valid_int, int_force_ge_zero |
| 21 |
from rpython.rtyper.lltypesystem import lltype, rffi |
| 22 |
|
| 23 |
|
| 24 |
class BasicTests: |
| 25 |
def test_basic(self): |
| 26 |
def f(x, y): |
| 27 |
return x + y |
| 28 |
res = self.interp_operations(f, [40, 2]) |
| 29 |
assert res == 42 |
| 30 |
|
| 31 |
def test_basic_inst(self): |
| 32 |
class A: |
| 33 |
pass |
| 34 |
def f(n): |
| 35 |
a = A() |
| 36 |
a.x = n |
| 37 |
return a.x |
| 38 |
res = self.interp_operations(f, [42]) |
| 39 |
assert res == 42 |
| 40 |
|
| 41 |
def test_uint_floordiv(self): |
| 42 |
from rpython.rlib.rarithmetic import r_uint |
| 43 |
|
| 44 |
def f(a, b): |
| 45 |
a = r_uint(a) |
| 46 |
b = r_uint(b) |
| 47 |
return a/b |
| 48 |
|
| 49 |
res = self.interp_operations(f, [-4, 3]) |
| 50 |
assert res == long(r_uint(-4)) // 3 |
| 51 |
|
| 52 |
def test_direct_call(self): |
| 53 |
def g(n): |
| 54 |
return n + 2 |
| 55 |
def f(a, b): |
| 56 |
return g(a) + g(b) |
| 57 |
res = self.interp_operations(f, [8, 98]) |
| 58 |
assert res == 110 |
| 59 |
|
| 60 |
def test_direct_call_with_guard(self): |
| 61 |
def g(n): |
| 62 |
if n < 0: |
| 63 |
return 0 |
| 64 |
return n + 2 |
| 65 |
def f(a, b): |
| 66 |
return g(a) + g(b) |
| 67 |
res = self.interp_operations(f, [8, 98]) |
| 68 |
assert res == 110 |
| 69 |
|
| 70 |
def test_loop_1(self): |
| 71 |
myjitdriver = JitDriver(greens = [], reds = ['x', 'y', 'res']) |
| 72 |
def f(x, y): |
| 73 |
res = 0 |
| 74 |
while y > 0: |
| 75 |
myjitdriver.can_enter_jit(x=x, y=y, res=res) |
| 76 |
myjitdriver.jit_merge_point(x=x, y=y, res=res) |
| 77 |
res += x |
| 78 |
y -= 1 |
| 79 |
return res |
| 80 |
res = self.meta_interp(f, [6, 7]) |
| 81 |
assert res == 42 |
| 82 |
self.check_trace_count(1) |
| 83 |
self.check_resops({'jump': 1, 'int_gt': 2, 'int_add': 2, |
| 84 |
'guard_true': 2, 'int_sub': 2}) |
| 85 |
|
| 86 |
if self.basic: |
| 87 |
found = 0 |
| 88 |
for op in get_stats().get_all_loops()[0]._all_operations(): |
| 89 |
if op.getopname() == 'guard_true': |
| 90 |
liveboxes = op.getfailargs() |
| 91 |
assert len(liveboxes) == 3 |
| 92 |
for box in liveboxes: |
| 93 |
assert box.type == 'i' |
| 94 |
found += 1 |
| 95 |
assert found == 2 |
| 96 |
|
| 97 |
def test_loop_variant_mul1(self): |
| 98 |
myjitdriver = JitDriver(greens = [], reds = ['y', 'res', 'x']) |
| 99 |
def f(x, y): |
| 100 |
res = 0 |
| 101 |
while y > 0: |
| 102 |
myjitdriver.can_enter_jit(x=x, y=y, res=res) |
| 103 |
myjitdriver.jit_merge_point(x=x, y=y, res=res) |
| 104 |
res += x * x |
| 105 |
x += 1 |
| 106 |
res += x * x |
| 107 |
y -= 1 |
| 108 |
return res |
| 109 |
res = self.meta_interp(f, [6, 7]) |
| 110 |
assert res == 1323 |
| 111 |
self.check_trace_count(1) |
| 112 |
self.check_simple_loop(int_mul=1) |
| 113 |
|
| 114 |
def test_rutf8(self): |
| 115 |
from rpython.rlib import rutf8, jit |
| 116 |
class U(object): |
| 117 |
def __init__(self, u, l): |
| 118 |
self.u = u |
| 119 |
self.l = l |
| 120 |
self._index_storage = rutf8.null_storage() |
| 121 |
|
| 122 |
def _get_index_storage(self): |
| 123 |
return jit.conditional_call_elidable(self._index_storage, |
| 124 |
U._compute_index_storage, self) |
| 125 |
|
| 126 |
def _compute_index_storage(self): |
| 127 |
storage = rutf8.create_utf8_index_storage(self.u, self.l) |
| 128 |
self._index_storage = storage |
| 129 |
return storage |
| 130 |
|
| 131 |
def m(a): |
| 132 |
return f(a) |
| 133 |
def f(a): |
| 134 |
x = str(a) |
| 135 |
u = U(x, len(x)) |
| 136 |
st = u._get_index_storage() |
| 137 |
return rutf8.codepoint_index_at_byte_position( |
| 138 |
u.u, st, 1, len(x)) |
| 139 |
|
| 140 |
self.interp_operations(m, [123232]) |
| 141 |
|
| 142 |
|
| 143 |
def test_loop_variant_mul_ovf(self): |
| 144 |
myjitdriver = JitDriver(greens = [], reds = ['y', 'res', 'x']) |
| 145 |
def f(x, y): |
| 146 |
res = 0 |
| 147 |
while y > 0: |
| 148 |
myjitdriver.can_enter_jit(x=x, y=y, res=res) |
| 149 |
myjitdriver.jit_merge_point(x=x, y=y, res=res) |
| 150 |
try: |
| 151 |
res += ovfcheck(x * x) |
| 152 |
x += 1 |
| 153 |
res += ovfcheck(x * x) |
| 154 |
y -= 1 |
| 155 |
except OverflowError: |
| 156 |
assert 0 |
| 157 |
return res |
| 158 |
res = self.meta_interp(f, [6, 7]) |
| 159 |
assert res == 1323 |
| 160 |
self.check_trace_count(1) |
| 161 |
self.check_simple_loop(int_mul_ovf=1) |
| 162 |
|
| 163 |
def test_loop_invariant_mul1(self): |
| 164 |
myjitdriver = JitDriver(greens = [], reds = ['y', 'res', 'x']) |
| 165 |
def f(x, y): |
| 166 |
res = 0 |
| 167 |
while y > 0: |
| 168 |
myjitdriver.can_enter_jit(x=x, y=y, res=res) |
| 169 |
myjitdriver.jit_merge_point(x=x, y=y, res=res) |
| 170 |
res += x * x |
| 171 |
y -= 1 |
| 172 |
return res |
| 173 |
res = self.meta_interp(f, [6, 7]) |
| 174 |
assert res == 252 |
| 175 |
self.check_trace_count(1) |
| 176 |
self.check_simple_loop(int_mul=0) |
| 177 |
self.check_resops({'jump': 1, 'int_gt': 2, 'int_add': 2, |
| 178 |
'int_mul': 1, 'guard_true': 2, 'int_sub': 2}) |
| 179 |
|
| 180 |
|
| 181 |
def test_loop_invariant_mul_ovf1(self): |
| 182 |
myjitdriver = JitDriver(greens = [], reds = ['y', 'res', 'x']) |
| 183 |
def f(x, y): |
| 184 |
res = 0 |
| 185 |
while y > 0: |
| 186 |
myjitdriver.can_enter_jit(x=x, y=y, res=res) |
| 187 |
myjitdriver.jit_merge_point(x=x, y=y, res=res) |
| 188 |
b = y * 2 |
| 189 |
try: |
| 190 |
res += ovfcheck(x * x) + b |
| 191 |
except OverflowError: |
| 192 |
assert 0 |
| 193 |
y -= 1 |
| 194 |
return res |
| 195 |
res = self.meta_interp(f, [6, 7]) |
| 196 |
assert res == 308 |
| 197 |
self.check_trace_count(1) |
| 198 |
self.check_simple_loop(int_mul_ovf=0) |
| 199 |
self.check_resops({'jump': 1, 'int_lshift': 2, 'int_gt': 2, |
| 200 |
'int_mul_ovf': 1, 'int_add': 4, |
| 201 |
'guard_true': 2, 'guard_no_overflow': 1, |
| 202 |
'int_sub': 2}) |
| 203 |
|
| 204 |
def test_loop_invariant_mul_bridge1(self): |
| 205 |
myjitdriver = JitDriver(greens = [], reds = ['y', 'res', 'x', 'n']) |
| 206 |
def f(x, y, n): |
| 207 |
res = 0 |
| 208 |
while y > 0: |
| 209 |
myjitdriver.can_enter_jit(x=x, y=y, n=n, res=res) |
| 210 |
myjitdriver.jit_merge_point(x=x, y=y, n=n, res=res) |
| 211 |
res += x * x |
| 212 |
if y<n: |
| 213 |
x += 1 |
| 214 |
y -= 1 |
| 215 |
return res |
| 216 |
res = self.meta_interp(f, [6, 32, 16]) |
| 217 |
assert res == 3427 |
| 218 |
self.check_trace_count(3) |
| 219 |
|
| 220 |
def test_loop_invariant_mul_bridge_maintaining1(self): |
| 221 |
myjitdriver = JitDriver(greens = [], reds = ['y', 'res', 'x', 'n']) |
| 222 |
def f(x, y, n): |
| 223 |
res = 0 |
| 224 |
while y > 0: |
| 225 |
myjitdriver.can_enter_jit(x=x, y=y, res=res, n=n) |
| 226 |
myjitdriver.jit_merge_point(x=x, y=y, res=res, n=n) |
| 227 |
res += x * x |
| 228 |
if y<n: |
| 229 |
res += 1 |
| 230 |
y -= 1 |
| 231 |
return res |
| 232 |
res = self.meta_interp(f, [6, 32, 16]) |
| 233 |
assert res == 1167 |
| 234 |
self.check_trace_count(3) |
| 235 |
self.check_resops(int_mul=3) |
| 236 |
|
| 237 |
def test_loop_invariant_mul_bridge_maintaining2(self): |
| 238 |
myjitdriver = JitDriver(greens = [], reds = ['y', 'res', 'x', 'n']) |
| 239 |
def f(x, y, n): |
| 240 |
res = 0 |
| 241 |
while y > 0: |
| 242 |
myjitdriver.can_enter_jit(x=x, y=y, res=res, n=n) |
| 243 |
myjitdriver.jit_merge_point(x=x, y=y, res=res, n=n) |
| 244 |
z = x * x |
| 245 |
res += z |
| 246 |
if y<n: |
| 247 |
res += z |
| 248 |
y -= 1 |
| 249 |
return res |
| 250 |
res = self.meta_interp(f, [6, 32, 16]) |
| 251 |
assert res == 1692 |
| 252 |
self.check_trace_count(3) |
| 253 |
self.check_resops(int_mul=3) |
| 254 |
|
| 255 |
def test_loop_invariant_mul_bridge_maintaining3(self): |
| 256 |
myjitdriver = JitDriver(greens = [], reds = ['y', 'res', 'x', 'm']) |
| 257 |
def f(x, y, m): |
| 258 |
res = 0 |
| 259 |
while y > 0: |
| 260 |
myjitdriver.can_enter_jit(x=x, y=y, res=res, m=m) |
| 261 |
myjitdriver.jit_merge_point(x=x, y=y, res=res, m=m) |
| 262 |
z = x * x |
| 263 |
res += z |
| 264 |
if y<m: |
| 265 |
res += z |
| 266 |
y -= 1 |
| 267 |
return res |
| 268 |
res = self.meta_interp(f, [6, 32, 16]) |
| 269 |
assert res == 1692 |
| 270 |
self.check_trace_count(3) |
| 271 |
self.check_resops({'int_lt': 4, 'int_gt': 4, 'guard_false': 2, |
| 272 |
'guard_true': 6, 'int_sub': 4, 'jump': 3, |
| 273 |
'int_mul': 3, 'int_add': 4}) |
| 274 |
|
| 275 |
def test_loop_invariant_mul_ovf2(self): |
| 276 |
myjitdriver = JitDriver(greens = [], reds = ['y', 'res', 'x']) |
| 277 |
def f(x, y): |
| 278 |
res = 0 |
| 279 |
while y > 0: |
| 280 |
myjitdriver.can_enter_jit(x=x, y=y, res=res) |
| 281 |
myjitdriver.jit_merge_point(x=x, y=y, res=res) |
| 282 |
b = y * 2 |
| 283 |
try: |
| 284 |
res += ovfcheck(x * x) + b |
| 285 |
except OverflowError: |
| 286 |
res += 1 |
| 287 |
y -= 1 |
| 288 |
return res |
| 289 |
res = self.meta_interp(f, [sys.maxint, 7]) |
| 290 |
assert res == f(sys.maxint, 7) |
| 291 |
self.check_trace_count(1) |
| 292 |
res = self.meta_interp(f, [6, 7]) |
| 293 |
assert res == 308 |
| 294 |
|
| 295 |
def test_loop_invariant_mul_bridge_ovf1(self): |
| 296 |
myjitdriver = JitDriver(greens = [], reds = ['y', 'res', 'x1', 'x2']) |
| 297 |
def f(x1, x2, y): |
| 298 |
res = 0 |
| 299 |
while y > 0: |
| 300 |
myjitdriver.can_enter_jit(x1=x1, x2=x2, y=y, res=res) |
| 301 |
myjitdriver.jit_merge_point(x1=x1, x2=x2, y=y, res=res) |
| 302 |
try: |
| 303 |
res += ovfcheck(x1 * x1) |
| 304 |
except OverflowError: |
| 305 |
res += 1 |
| 306 |
if y<32 and (y>>2)&1==0: |
| 307 |
x1, x2 = x2, x1 |
| 308 |
y -= 1 |
| 309 |
return res |
| 310 |
res = self.meta_interp(f, [6, sys.maxint, 48]) |
| 311 |
self.check_trace_count(6) |
| 312 |
assert res == f(6, sys.maxint, 48) |
| 313 |
|
| 314 |
def test_loop_invariant_mul_bridge_ovf2(self): |
| 315 |
myjitdriver = JitDriver(greens = [], reds = ['y', 'res', 'x1', 'x2', 'n']) |
| 316 |
def f(x1, x2, n, y): |
| 317 |
res = 0 |
| 318 |
while y > 0: |
| 319 |
myjitdriver.can_enter_jit(x1=x1, x2=x2, y=y, res=res, n=n) |
| 320 |
myjitdriver.jit_merge_point(x1=x1, x2=x2, y=y, res=res, n=n) |
| 321 |
try: |
| 322 |
res += ovfcheck(x1 * x1) |
| 323 |
except OverflowError: |
| 324 |
res += 1 |
| 325 |
y -= 1 |
| 326 |
if y&4 == 0: |
| 327 |
x1, x2 = x2, x1 |
| 328 |
return res |
| 329 |
res = self.meta_interp(f, [6, sys.maxint, 32, 48]) |
| 330 |
assert res == f(6, sys.maxint, 32, 48) |
| 331 |
res = self.meta_interp(f, [sys.maxint, 6, 32, 48]) |
| 332 |
assert res == f(sys.maxint, 6, 32, 48) |
| 333 |
|
| 334 |
|
| 335 |
def test_loop_invariant_intbox(self): |
| 336 |
myjitdriver = JitDriver(greens = [], reds = ['y', 'res', 'x']) |
| 337 |
class I: |
| 338 |
__slots__ = 'intval' |
| 339 |
_immutable_ = True |
| 340 |
def __init__(self, intval): |
| 341 |
self.intval = intval |
| 342 |
def f(i, y): |
| 343 |
res = 0 |
| 344 |
x = I(i) |
| 345 |
while y > 0: |
| 346 |
myjitdriver.can_enter_jit(x=x, y=y, res=res) |
| 347 |
myjitdriver.jit_merge_point(x=x, y=y, res=res) |
| 348 |
res += x.intval * x.intval |
| 349 |
y -= 1 |
| 350 |
return res |
| 351 |
res = self.meta_interp(f, [6, 7]) |
| 352 |
assert res == 252 |
| 353 |
self.check_trace_count(1) |
| 354 |
self.check_resops({'jump': 1, 'int_gt': 2, 'int_add': 2, |
| 355 |
'getfield_gc_i': 1, 'int_mul': 1, |
| 356 |
'guard_true': 2, 'int_sub': 2}) |
| 357 |
|
| 358 |
def test_loops_are_transient(self): |
| 359 |
import gc, weakref |
| 360 |
myjitdriver = JitDriver(greens = [], reds = ['x', 'y', 'res']) |
| 361 |
def f(x, y): |
| 362 |
res = 0 |
| 363 |
while y > 0: |
| 364 |
myjitdriver.can_enter_jit(x=x, y=y, res=res) |
| 365 |
myjitdriver.jit_merge_point(x=x, y=y, res=res) |
| 366 |
res += x |
| 367 |
if y%2: |
| 368 |
res *= 2 |
| 369 |
y -= 1 |
| 370 |
return res |
| 371 |
wr_loops = [] |
| 372 |
old_init = history.TreeLoop.__init__.im_func |
| 373 |
try: |
| 374 |
def track_init(self, name): |
| 375 |
old_init(self, name) |
| 376 |
wr_loops.append(weakref.ref(self)) |
| 377 |
history.TreeLoop.__init__ = track_init |
| 378 |
res = self.meta_interp(f, [6, 15], no_stats=True) |
| 379 |
finally: |
| 380 |
history.TreeLoop.__init__ = old_init |
| 381 |
|
| 382 |
assert res == f(6, 15) |
| 383 |
gc.collect() |
| 384 |
|
| 385 |
assert not [wr for wr in wr_loops if wr()] |
| 386 |
|
| 387 |
def test_string(self): |
| 388 |
def f(n): |
| 389 |
bytecode = 'adlfkj' + chr(n) |
| 390 |
if n < len(bytecode): |
| 391 |
return bytecode[n] |
| 392 |
else: |
| 393 |
return "?" |
| 394 |
res = self.interp_operations(f, [1]) |
| 395 |
assert res == ord("d") |
| 396 |
res = self.interp_operations(f, [6]) |
| 397 |
assert res == 6 |
| 398 |
res = self.interp_operations(f, [42]) |
| 399 |
assert res == ord("?") |
| 400 |
|
| 401 |
def test_chr2str(self): |
| 402 |
def f(n): |
| 403 |
s = chr(n) |
| 404 |
return s[0] |
| 405 |
res = self.interp_operations(f, [3]) |
| 406 |
assert res == 3 |
| 407 |
|
| 408 |
def test_unicode(self): |
| 409 |
def f(n): |
| 410 |
bytecode = u'adlfkj' + unichr(n) |
| 411 |
if n < len(bytecode): |
| 412 |
return bytecode[n] |
| 413 |
else: |
| 414 |
return u"?" |
| 415 |
res = self.interp_operations(f, [1]) |
| 416 |
assert res == ord(u"d") |
| 417 |
res = self.interp_operations(f, [6]) |
| 418 |
assert res == 6 |
| 419 |
res = self.interp_operations(f, [42]) |
| 420 |
assert res == ord(u"?") |
| 421 |
|
| 422 |
def test_char_in_constant_string(self): |
| 423 |
def g(string): |
| 424 |
return '\x00' in string |
| 425 |
def f(): |
| 426 |
if g('abcdef'): return -60 |
| 427 |
if not g('abc\x00ef'): return -61 |
| 428 |
return 42 |
| 429 |
res = self.interp_operations(f, []) |
| 430 |
assert res == 42 |
| 431 |
self.check_operations_history({'finish': 1}) |
| 432 |
|
| 433 |
def test_residual_call(self): |
| 434 |
@dont_look_inside |
| 435 |
def externfn(x, y): |
| 436 |
return x * y |
| 437 |
def f(n): |
| 438 |
return externfn(n, n+1) |
| 439 |
res = self.interp_operations(f, [6]) |
| 440 |
assert res == 42 |
| 441 |
self.check_operations_history(int_add=1, int_mul=0, call_i=1, guard_no_exception=0) |
| 442 |
|
| 443 |
def test_residual_call_elidable(self): |
| 444 |
def externfn(x, y): |
| 445 |
return x * y |
| 446 |
externfn._elidable_function_ = True |
| 447 |
def f(n): |
| 448 |
promote(n) |
| 449 |
return externfn(n, n+1) |
| 450 |
res = self.interp_operations(f, [6]) |
| 451 |
assert res == 42 |
| 452 |
|
| 453 |
self.check_operations_history(int_add=0, int_mul=0, |
| 454 |
call_i=0, call_pure_i=0) |
| 455 |
|
| 456 |
def test_residual_call_elidable_1(self): |
| 457 |
@elidable |
| 458 |
def externfn(x, y): |
| 459 |
return x * y |
| 460 |
def f(n): |
| 461 |
return externfn(n, n+1) |
| 462 |
res = self.interp_operations(f, [6]) |
| 463 |
assert res == 42 |
| 464 |
|
| 465 |
self.check_operations_history(int_add=1, int_mul=0, |
| 466 |
call_i=0, call_pure_i=1) |
| 467 |
|
| 468 |
def test_residual_call_elidable_2(self): |
| 469 |
myjitdriver = JitDriver(greens = [], reds = ['n']) |
| 470 |
@elidable |
| 471 |
def externfn(x): |
| 472 |
return x - 1 |
| 473 |
def f(n): |
| 474 |
while n > 0: |
| 475 |
myjitdriver.can_enter_jit(n=n) |
| 476 |
myjitdriver.jit_merge_point(n=n) |
| 477 |
n = externfn(n) |
| 478 |
return n |
| 479 |
res = self.meta_interp(f, [7]) |
| 480 |
assert res == 0 |
| 481 |
|
| 482 |
|
| 483 |
self.check_resops(call_pure_i=0, call_i=2, int_sub=0) |
| 484 |
|
| 485 |
def test_constfold_call_elidable(self): |
| 486 |
myjitdriver = JitDriver(greens = ['m'], reds = ['n']) |
| 487 |
@elidable |
| 488 |
def externfn(x): |
| 489 |
return x - 3 |
| 490 |
def f(n, m): |
| 491 |
while n > 0: |
| 492 |
myjitdriver.can_enter_jit(n=n, m=m) |
| 493 |
myjitdriver.jit_merge_point(n=n, m=m) |
| 494 |
n -= externfn(m) |
| 495 |
return n |
| 496 |
res = self.meta_interp(f, [21, 5]) |
| 497 |
assert res == -1 |
| 498 |
|
| 499 |
self.check_resops(call_pure_i=0, call_i=0, int_sub=2) |
| 500 |
|
| 501 |
def test_constfold_call_elidable_2(self): |
| 502 |
myjitdriver = JitDriver(greens = ['m'], reds = ['n']) |
| 503 |
@elidable |
| 504 |
def externfn(x): |
| 505 |
return x - 3 |
| 506 |
class V: |
| 507 |
def __init__(self, value): |
| 508 |
self.value = value |
| 509 |
def f(n, m): |
| 510 |
while n > 0: |
| 511 |
myjitdriver.can_enter_jit(n=n, m=m) |
| 512 |
myjitdriver.jit_merge_point(n=n, m=m) |
| 513 |
v = V(m) |
| 514 |
n -= externfn(v.value) |
| 515 |
return n |
| 516 |
res = self.meta_interp(f, [21, 5]) |
| 517 |
assert res == -1 |
| 518 |
|
| 519 |
self.check_resops(call_pure_i=0, call_i=0, int_sub=2) |
| 520 |
|
| 521 |
def test_elidable_function_returning_object(self): |
| 522 |
myjitdriver = JitDriver(greens = ['m'], reds = ['n']) |
| 523 |
class V: |
| 524 |
def __init__(self, x): |
| 525 |
self.x = x |
| 526 |
v1 = V(1) |
| 527 |
v2 = V(2) |
| 528 |
@elidable |
| 529 |
def externfn(x): |
| 530 |
if x: |
| 531 |
return v1 |
| 532 |
else: |
| 533 |
return v2 |
| 534 |
def f(n, m): |
| 535 |
while n > 0: |
| 536 |
myjitdriver.can_enter_jit(n=n, m=m) |
| 537 |
myjitdriver.jit_merge_point(n=n, m=m) |
| 538 |
m = V(m).x |
| 539 |
n -= externfn(m).x + externfn(m + m - m).x |
| 540 |
return n |
| 541 |
res = self.meta_interp(f, [21, 5]) |
| 542 |
assert res == -1 |
| 543 |
|
| 544 |
self.check_resops(call_pure_r=0, call_r=0, getfield_gc_i=1, int_sub=2, |
| 545 |
call_pure_i=0, call_i=0) |
| 546 |
|
| 547 |
def test_elidable_raising(self): |
| 548 |
myjitdriver = JitDriver(greens = ['m'], reds = ['n']) |
| 549 |
@elidable |
| 550 |
def externfn(x): |
| 551 |
if x <= 0: |
| 552 |
raise ValueError |
| 553 |
return x - 1 |
| 554 |
def f(n, m): |
| 555 |
while n > 0: |
| 556 |
myjitdriver.can_enter_jit(n=n, m=m) |
| 557 |
myjitdriver.jit_merge_point(n=n, m=m) |
| 558 |
try: |
| 559 |
n -= externfn(m) |
| 560 |
except ValueError: |
| 561 |
n -= 1 |
| 562 |
return n |
| 563 |
res = self.meta_interp(f, [22, 6]) |
| 564 |
assert res == -3 |
| 565 |
|
| 566 |
self.check_resops(call_pure_i=0, call_i=0, int_sub=2) |
| 567 |
|
| 568 |
res = self.meta_interp(f, [22, -5]) |
| 569 |
assert res == 0 |
| 570 |
|
| 571 |
self.check_resops(call_pure_i=0, call_i=2, int_sub=2) |
| 572 |
|
| 573 |
def test_elidable_raising_2(self): |
| 574 |
myjitdriver = JitDriver(greens = ['m'], reds = ['n']) |
| 575 |
@elidable |
| 576 |
def externfn(x): |
| 577 |
if x <= 0: |
| 578 |
raise ValueError |
| 579 |
return x - 1 |
| 580 |
def f(n, m): |
| 581 |
while n > 0: |
| 582 |
myjitdriver.can_enter_jit(n=n, m=m) |
| 583 |
myjitdriver.jit_merge_point(n=n, m=m) |
| 584 |
try: |
| 585 |
n -= externfn(noConst(m)) |
| 586 |
except ValueError: |
| 587 |
n -= 1 |
| 588 |
return n |
| 589 |
res = self.meta_interp(f, [22, 6]) |
| 590 |
assert res == -3 |
| 591 |
|
| 592 |
self.check_resops(call_pure_i=0, call_i=0, int_sub=2) |
| 593 |
|
| 594 |
res = self.meta_interp(f, [22, -5]) |
| 595 |
assert res == 0 |
| 596 |
|
| 597 |
self.check_resops(call_pure_i=0, call_i=2, int_sub=2) |
| 598 |
|
| 599 |
def test_constant_across_mp(self): |
| 600 |
myjitdriver = JitDriver(greens = [], reds = ['n']) |
| 601 |
class X(object): |
| 602 |
pass |
| 603 |
def f(n): |
| 604 |
while n > -100: |
| 605 |
myjitdriver.can_enter_jit(n=n) |
| 606 |
myjitdriver.jit_merge_point(n=n) |
| 607 |
x = X() |
| 608 |
x.arg = 5 |
| 609 |
if n <= 0: break |
| 610 |
n -= x.arg |
| 611 |
x.arg = 6 |
| 612 |
return n |
| 613 |
res = self.meta_interp(f, [31]) |
| 614 |
assert res == -4 |
| 615 |
|
| 616 |
def test_stopatxpolicy(self): |
| 617 |
myjitdriver = JitDriver(greens = [], reds = ['y']) |
| 618 |
def internfn(y): |
| 619 |
return y * 3 |
| 620 |
def externfn(y): |
| 621 |
return y ^ 4 |
| 622 |
def f(y): |
| 623 |
while y >= 0: |
| 624 |
myjitdriver.can_enter_jit(y=y) |
| 625 |
myjitdriver.jit_merge_point(y=y) |
| 626 |
if y & 7: |
| 627 |
f = internfn |
| 628 |
else: |
| 629 |
f = externfn |
| 630 |
f(y) |
| 631 |
y -= 1 |
| 632 |
return 42 |
| 633 |
policy = StopAtXPolicy(externfn) |
| 634 |
res = self.meta_interp(f, [31], policy=policy) |
| 635 |
assert res == 42 |
| 636 |
self.check_resops(int_mul=2, int_xor=0) |
| 637 |
|
| 638 |
def test_we_are_jitted(self): |
| 639 |
myjitdriver = JitDriver(greens = [], reds = ['y']) |
| 640 |
def f(y): |
| 641 |
while y >= 0: |
| 642 |
myjitdriver.can_enter_jit(y=y) |
| 643 |
myjitdriver.jit_merge_point(y=y) |
| 644 |
if we_are_jitted(): |
| 645 |
x = 1 |
| 646 |
else: |
| 647 |
x = 10 |
| 648 |
y -= x |
| 649 |
return y |
| 650 |
assert f(55) == -5 |
| 651 |
res = self.meta_interp(f, [55]) |
| 652 |
assert res == -1 |
| 653 |
|
| 654 |
def test_confirm_enter_jit(self): |
| 655 |
def confirm_enter_jit(x, y): |
| 656 |
return x <= 5 |
| 657 |
myjitdriver = JitDriver(greens = ['x'], reds = ['y'], |
| 658 |
confirm_enter_jit = confirm_enter_jit) |
| 659 |
def f(x, y): |
| 660 |
while y >= 0: |
| 661 |
myjitdriver.can_enter_jit(x=x, y=y) |
| 662 |
myjitdriver.jit_merge_point(x=x, y=y) |
| 663 |
y -= x |
| 664 |
return y |
| 665 |
|
| 666 |
res = self.meta_interp(f, [10, 84]) |
| 667 |
assert res == -6 |
| 668 |
self.check_trace_count(0) |
| 669 |
|
| 670 |
res = self.meta_interp(f, [3, 19]) |
| 671 |
assert res == -2 |
| 672 |
self.check_trace_count(1) |
| 673 |
|
| 674 |
def test_can_never_inline(self): |
| 675 |
def can_never_inline(x): |
| 676 |
return x > 50 |
| 677 |
myjitdriver = JitDriver(greens = ['x'], reds = ['y'], |
| 678 |
can_never_inline = can_never_inline) |
| 679 |
@dont_look_inside |
| 680 |
def marker(): |
| 681 |
pass |
| 682 |
def f(x, y): |
| 683 |
while y >= 0: |
| 684 |
myjitdriver.can_enter_jit(x=x, y=y) |
| 685 |
myjitdriver.jit_merge_point(x=x, y=y) |
| 686 |
x += 1 |
| 687 |
if x == 4 or x == 61: |
| 688 |
marker() |
| 689 |
y -= x |
| 690 |
return y |
| 691 |
|
| 692 |
res = self.meta_interp(f, [3, 6], repeat=7, function_threshold=0) |
| 693 |
assert res == 6 - 4 - 5 |
| 694 |
self.check_history(call_n=0) |
| 695 |
|
| 696 |
res = self.meta_interp(f, [60, 84], repeat=7) |
| 697 |
assert res == 84 - 61 - 62 |
| 698 |
self.check_history(call_n=1) |
| 699 |
|
| 700 |
def test_unroll_one_loop_iteration(self): |
| 701 |
def unroll(code): |
| 702 |
return code == 0 |
| 703 |
myjitdriver = JitDriver(greens = ['code'], |
| 704 |
reds = ['loops', 'inner_loops', 's'], |
| 705 |
should_unroll_one_iteration=unroll) |
| 706 |
|
| 707 |
def f(code, loops, inner_loops): |
| 708 |
s = 0 |
| 709 |
while loops > 0: |
| 710 |
myjitdriver.jit_merge_point(code=code, loops=loops, |
| 711 |
inner_loops=inner_loops, s=s) |
| 712 |
if code == 1: |
| 713 |
s += f(0, inner_loops, 0) |
| 714 |
loops -= 1 |
| 715 |
s += 1 |
| 716 |
return s |
| 717 |
|
| 718 |
res = self.meta_interp(f, [1, 4, 1], enable_opts="", inline=True) |
| 719 |
assert res == f(1, 4, 1) |
| 720 |
self.check_history(call_assembler_i=0) |
| 721 |
|
| 722 |
res = self.meta_interp(f, [1, 4, 2], enable_opts="", inline=True) |
| 723 |
assert res == f(1, 4, 2) |
| 724 |
self.check_history(call_assembler_i=1) |
| 725 |
|
| 726 |
def test_format(self): |
| 727 |
def f(n): |
| 728 |
return len("<%d>" % n) |
| 729 |
res = self.interp_operations(f, [421]) |
| 730 |
assert res == 5 |
| 731 |
|
| 732 |
def test_switch(self): |
| 733 |
def f(n): |
| 734 |
if n == -5: return 12 |
| 735 |
elif n == 2: return 51 |
| 736 |
elif n == 7: return 1212 |
| 737 |
else: return 42 |
| 738 |
res = self.interp_operations(f, [7]) |
| 739 |
assert res == 1212 |
| 740 |
res = self.interp_operations(f, [12311]) |
| 741 |
assert res == 42 |
| 742 |
|
| 743 |
def test_switch_bridges(self): |
| 744 |
from rpython.rlib.rarithmetic import intmask |
| 745 |
myjitdriver = JitDriver(greens = [], reds = 'auto') |
| 746 |
lsts = [[-5, 2, 20] * 6, |
| 747 |
[7, 123, 2] * 6, |
| 748 |
[12311, -5, 7] * 6, |
| 749 |
[7, 123, 2] * 4 + [-5, -5, -5] * 2, |
| 750 |
[7, 123, 2] * 4 + [-5, -5, -5] * 2 + [12311, 12311, 12311], |
| 751 |
] |
| 752 |
def f(case): |
| 753 |
x = 0 |
| 754 |
i = 0 |
| 755 |
lst = lsts[case] |
| 756 |
while i < len(lst): |
| 757 |
myjitdriver.jit_merge_point() |
| 758 |
n = lst[i] |
| 759 |
if n == -5: a = 5 |
| 760 |
elif n == 2: a = 4 |
| 761 |
elif n == 7: a = 3 |
| 762 |
else: a = 2 |
| 763 |
x = intmask(x * 10 + a) |
| 764 |
|
| 765 |
i += 1 |
| 766 |
return x |
| 767 |
res = self.meta_interp(f, [0], backendopt=True) |
| 768 |
assert res == intmask(542 * 1001001001001001) |
| 769 |
res = self.meta_interp(f, [1], backendopt=True) |
| 770 |
assert res == intmask(324 * 1001001001001001) |
| 771 |
res = self.meta_interp(f, [2], backendopt=True) |
| 772 |
assert res == intmask(253 * 1001001001001001) |
| 773 |
res = self.meta_interp(f, [3], backendopt=True) |
| 774 |
assert res == intmask(324324324324555555) |
| 775 |
res = self.meta_interp(f, [4], backendopt=True) |
| 776 |
assert res == intmask(324324324324555555222) |
| 777 |
|
| 778 |
def test_r_uint(self): |
| 779 |
from rpython.rlib.rarithmetic import r_uint |
| 780 |
myjitdriver = JitDriver(greens = [], reds = ['y']) |
| 781 |
def f(y): |
| 782 |
y = r_uint(y) |
| 783 |
while y > 0: |
| 784 |
myjitdriver.can_enter_jit(y=y) |
| 785 |
myjitdriver.jit_merge_point(y=y) |
| 786 |
y -= 1 |
| 787 |
return y |
| 788 |
res = self.meta_interp(f, [10]) |
| 789 |
assert res == 0 |
| 790 |
|
| 791 |
def test_uint_operations(self): |
| 792 |
from rpython.rlib.rarithmetic import r_uint |
| 793 |
def f(n): |
| 794 |
return ((r_uint(n) - 123) >> 1) <= r_uint(456) |
| 795 |
res = self.interp_operations(f, [50]) |
| 796 |
assert res == False |
| 797 |
self.check_operations_history(int_rshift=0, uint_rshift=1, |
| 798 |
int_le=0, uint_le=1, |
| 799 |
int_sub=1) |
| 800 |
|
| 801 |
def test_uint_condition(self): |
| 802 |
from rpython.rlib.rarithmetic import r_uint |
| 803 |
def f(n): |
| 804 |
if ((r_uint(n) - 123) >> 1) <= r_uint(456): |
| 805 |
return 24 |
| 806 |
else: |
| 807 |
return 12 |
| 808 |
res = self.interp_operations(f, [50]) |
| 809 |
assert res == 12 |
| 810 |
self.check_operations_history(int_rshift=0, uint_rshift=1, |
| 811 |
int_le=0, uint_le=1, |
| 812 |
int_sub=1) |
| 813 |
|
| 814 |
def test_int_between(self): |
| 815 |
|
| 816 |
def check(arg1, arg2, arg3, expect_result, **expect_operations): |
| 817 |
from rpython.rtyper.lltypesystem import lltype |
| 818 |
from rpython.rtyper.lltypesystem.lloperation import llop |
| 819 |
loc = locals().copy() |
| 820 |
exec(py.code.Source(""" |
| 821 |
def f(n, m, p): |
| 822 |
arg1 = %(arg1)s |
| 823 |
arg2 = %(arg2)s |
| 824 |
arg3 = %(arg3)s |
| 825 |
return llop.int_between(lltype.Bool, arg1, arg2, arg3) |
| 826 |
""" % locals()).compile(), loc) |
| 827 |
res = self.interp_operations(loc['f'], [5, 6, 7]) |
| 828 |
assert res == expect_result |
| 829 |
self.check_operations_history(expect_operations) |
| 830 |
|
| 831 |
check('n', 'm', 'p', True, int_sub=2, uint_lt=1) |
| 832 |
check('n', 'p', 'm', False, int_sub=2, uint_lt=1) |
| 833 |
|
| 834 |
check('n', 'm', 6, False, int_sub=2, uint_lt=1) |
| 835 |
|
| 836 |
check('n', 4, 'p', False, int_sub=2, uint_lt=1) |
| 837 |
check('n', 5, 'p', True, int_sub=2, uint_lt=1) |
| 838 |
check('n', 8, 'p', False, int_sub=2, uint_lt=1) |
| 839 |
|
| 840 |
check('n', 6, 7, True, int_sub=2, uint_lt=1) |
| 841 |
|
| 842 |
check(-2, 'n', 'p', True, int_sub=2, uint_lt=1) |
| 843 |
check(-2, 'm', 'p', True, int_sub=2, uint_lt=1) |
| 844 |
check(-2, 'p', 'm', False, int_sub=2, uint_lt=1) |
| 845 |
|
| 846 |
|
| 847 |
|
| 848 |
|
| 849 |
check(2, 'n', 6, True, int_sub=1, uint_lt=1) |
| 850 |
check(2, 'm', 6, False, int_sub=1, uint_lt=1) |
| 851 |
check(2, 'p', 6, False, int_sub=1, uint_lt=1) |
| 852 |
check(5, 'n', 6, True, int_eq=1) |
| 853 |
check(5, 'm', 6, False, int_eq=1) |
| 854 |
|
| 855 |
check(2, 6, 'm', False, int_sub=1, uint_lt=1) |
| 856 |
check(2, 6, 'p', True, int_sub=1, uint_lt=1) |
| 857 |
|
| 858 |
check(2, 40, 6, False) |
| 859 |
check(2, 40, 60, True) |
| 860 |
|
| 861 |
def test_getfield(self): |
| 862 |
class A: |
| 863 |
pass |
| 864 |
a1 = A() |
| 865 |
a1.foo = 5 |
| 866 |
a2 = A() |
| 867 |
a2.foo = 8 |
| 868 |
def f(x): |
| 869 |
if x > 5: |
| 870 |
a = a1 |
| 871 |
else: |
| 872 |
a = a2 |
| 873 |
return a.foo * x |
| 874 |
res = self.interp_operations(f, [42]) |
| 875 |
assert res == 210 |
| 876 |
self.check_operations_history(getfield_gc_i=1) |
| 877 |
|
| 878 |
def test_getfield_immutable(self): |
| 879 |
class A: |
| 880 |
_immutable_ = True |
| 881 |
a1 = A() |
| 882 |
a1.foo = 5 |
| 883 |
a2 = A() |
| 884 |
a2.foo = 8 |
| 885 |
def f(x): |
| 886 |
if x > 5: |
| 887 |
a = a1 |
| 888 |
else: |
| 889 |
a = a2 |
| 890 |
return a.foo * x |
| 891 |
res = self.interp_operations(f, [42]) |
| 892 |
assert res == 210 |
| 893 |
self.check_operations_history(getfield_gc_i=0) |
| 894 |
|
| 895 |
def test_setfield_bool(self): |
| 896 |
class A: |
| 897 |
def __init__(self): |
| 898 |
self.flag = True |
| 899 |
myjitdriver = JitDriver(greens = [], reds = ['n', 'obj']) |
| 900 |
def f(n): |
| 901 |
obj = A() |
| 902 |
res = False |
| 903 |
while n > 0: |
| 904 |
myjitdriver.can_enter_jit(n=n, obj=obj) |
| 905 |
myjitdriver.jit_merge_point(n=n, obj=obj) |
| 906 |
obj.flag = False |
| 907 |
n -= 1 |
| 908 |
return res |
| 909 |
res = self.meta_interp(f, [7]) |
| 910 |
assert type(res) == bool |
| 911 |
assert not res |
| 912 |
|
| 913 |
def test_int_add_ovf(self): |
| 914 |
def f(x, y): |
| 915 |
try: |
| 916 |
return ovfcheck(x + y) |
| 917 |
except OverflowError: |
| 918 |
return -42 |
| 919 |
res = self.interp_operations(f, [-100, 2]) |
| 920 |
assert res == -98 |
| 921 |
res = self.interp_operations(f, [1, sys.maxint]) |
| 922 |
assert res == -42 |
| 923 |
|
| 924 |
def test_ovf_raise(self): |
| 925 |
def g(x, y): |
| 926 |
try: |
| 927 |
return ovfcheck(x * y) |
| 928 |
except OverflowError: |
| 929 |
raise |
| 930 |
|
| 931 |
def f(x, y): |
| 932 |
try: |
| 933 |
return g(x, y) |
| 934 |
except OverflowError: |
| 935 |
return 3 |
| 936 |
|
| 937 |
res = self.interp_operations(f, [sys.maxint, 2]) |
| 938 |
assert res == 3 |
| 939 |
res = self.interp_operations(f, [3, 2]) |
| 940 |
assert res == 6 |
| 941 |
|
| 942 |
def test_int_sub_ovf(self): |
| 943 |
def f(x, y): |
| 944 |
try: |
| 945 |
return ovfcheck(x - y) |
| 946 |
except OverflowError: |
| 947 |
return -42 |
| 948 |
res = self.interp_operations(f, [-100, 2]) |
| 949 |
assert res == -102 |
| 950 |
res = self.interp_operations(f, [1, -sys.maxint]) |
| 951 |
assert res == -42 |
| 952 |
|
| 953 |
def test_int_mul_ovf(self): |
| 954 |
def f(x, y): |
| 955 |
try: |
| 956 |
return ovfcheck(x * y) |
| 957 |
except OverflowError: |
| 958 |
return -42 |
| 959 |
res = self.interp_operations(f, [-100, 2]) |
| 960 |
assert res == -200 |
| 961 |
res = self.interp_operations(f, [-3, sys.maxint//2]) |
| 962 |
assert res == -42 |
| 963 |
|
| 964 |
def test_mod_ovf(self): |
| 965 |
myjitdriver = JitDriver(greens = [], reds = ['n', 'x', 'y']) |
| 966 |
def f(n, x, y): |
| 967 |
while n > 0: |
| 968 |
myjitdriver.can_enter_jit(x=x, y=y, n=n) |
| 969 |
myjitdriver.jit_merge_point(x=x, y=y, n=n) |
| 970 |
n -= ovfcheck(x % y) |
| 971 |
x += 1 |
| 972 |
return n |
| 973 |
res = self.meta_interp(f, [20, 1, 2]) |
| 974 |
assert res == 0 |
| 975 |
self.check_resops(call_i=2, int_eq=3, int_and=2) |
| 976 |
|
| 977 |
def test_abs(self): |
| 978 |
myjitdriver = JitDriver(greens = [], reds = ['i', 't']) |
| 979 |
def f(i): |
| 980 |
t = 0 |
| 981 |
while i < 10: |
| 982 |
myjitdriver.can_enter_jit(i=i, t=t) |
| 983 |
myjitdriver.jit_merge_point(i=i, t=t) |
| 984 |
t += abs(i) |
| 985 |
i += 1 |
| 986 |
return t |
| 987 |
res = self.meta_interp(f, [-5]) |
| 988 |
assert res == 5+4+3+2+1+0+1+2+3+4+5+6+7+8+9 |
| 989 |
|
| 990 |
def test_int_c_div(self): |
| 991 |
from rpython.rlib.rarithmetic import int_c_div |
| 992 |
myjitdriver = JitDriver(greens = [], reds = ['i', 't']) |
| 993 |
def f(i): |
| 994 |
t = 0 |
| 995 |
while i < 10: |
| 996 |
myjitdriver.can_enter_jit(i=i, t=t) |
| 997 |
myjitdriver.jit_merge_point(i=i, t=t) |
| 998 |
t += int_c_div(-100, i) |
| 999 |
i += 1 |
| 1000 |
return t |
| 1001 |
expected = -sum([100 // n for n in range(1, 10)]) |
| 1002 |
assert f(1) == expected |
| 1003 |
res = self.meta_interp(f, [1]) |
| 1004 |
assert res == expected |
| 1005 |
|
| 1006 |
|
| 1007 |
def test_int_c_mod(self): |
| 1008 |
from rpython.rlib.rarithmetic import int_c_mod |
| 1009 |
myjitdriver = JitDriver(greens = [], reds = ['i', 't']) |
| 1010 |
def f(i): |
| 1011 |
t = 0 |
| 1012 |
while i < 10: |
| 1013 |
myjitdriver.can_enter_jit(i=i, t=t) |
| 1014 |
myjitdriver.jit_merge_point(i=i, t=t) |
| 1015 |
t += int_c_mod(-100, i) |
| 1016 |
i += 1 |
| 1017 |
return t |
| 1018 |
expected = -sum([100 % n for n in range(1, 10)]) |
| 1019 |
assert f(1) == expected |
| 1020 |
res = self.meta_interp(f, [1]) |
| 1021 |
assert res == expected |
| 1022 |
|
| 1023 |
|
| 1024 |
def test_positive_c_div_mod(self): |
| 1025 |
from rpython.rlib.rarithmetic import int_c_div, int_c_mod |
| 1026 |
myjitdriver = JitDriver(greens = [], reds = ['i', 't']) |
| 1027 |
def f(i): |
| 1028 |
t = 0 |
| 1029 |
while i < 10: |
| 1030 |
myjitdriver.can_enter_jit(i=i, t=t) |
| 1031 |
myjitdriver.jit_merge_point(i=i, t=t) |
| 1032 |
assert i > 0 |
| 1033 |
t += int_c_div(100, i) - int_c_mod(100, i) |
| 1034 |
i += 1 |
| 1035 |
return t |
| 1036 |
expected = sum([100 // n - 100 % n for n in range(1, 10)]) |
| 1037 |
assert f(1) == expected |
| 1038 |
res = self.meta_interp(f, [1]) |
| 1039 |
assert res == expected |
| 1040 |
|
| 1041 |
|
| 1042 |
def test_int_c_div_by_constant(self): |
| 1043 |
from rpython.rlib.rarithmetic import int_c_div |
| 1044 |
myjitdriver = JitDriver(greens = ['k'], reds = ['i', 't']) |
| 1045 |
def f(i, k): |
| 1046 |
t = 0 |
| 1047 |
while i < 100: |
| 1048 |
myjitdriver.can_enter_jit(i=i, t=t, k=k) |
| 1049 |
myjitdriver.jit_merge_point(i=i, t=t, k=k) |
| 1050 |
t += int_c_div(i, k) |
| 1051 |
i += 1 |
| 1052 |
return t |
| 1053 |
expected = sum([i // 10 for i in range(51, 100)]) |
| 1054 |
assert f(-50, 10) == expected |
| 1055 |
res = self.meta_interp(f, [-50, 10]) |
| 1056 |
assert res == expected |
| 1057 |
self.check_resops(call=0, uint_mul_high=2) |
| 1058 |
|
| 1059 |
def test_float(self): |
| 1060 |
myjitdriver = JitDriver(greens = [], reds = ['x', 'y', 'res']) |
| 1061 |
def f(x, y): |
| 1062 |
x = float(x) |
| 1063 |
y = float(y) |
| 1064 |
res = 0.0 |
| 1065 |
while y > 0.0: |
| 1066 |
myjitdriver.can_enter_jit(x=x, y=y, res=res) |
| 1067 |
myjitdriver.jit_merge_point(x=x, y=y, res=res) |
| 1068 |
res += x |
| 1069 |
y -= 1.0 |
| 1070 |
return res |
| 1071 |
res = self.meta_interp(f, [6, 7]) |
| 1072 |
assert res == 42.0 |
| 1073 |
self.check_trace_count(1) |
| 1074 |
self.check_resops({'jump': 1, 'float_gt': 2, 'float_add': 2, |
| 1075 |
'float_sub': 2, 'guard_true': 2}) |
| 1076 |
|
| 1077 |
def test_print(self): |
| 1078 |
myjitdriver = JitDriver(greens = [], reds = ['n']) |
| 1079 |
def f(n): |
| 1080 |
while n > 0: |
| 1081 |
myjitdriver.can_enter_jit(n=n) |
| 1082 |
myjitdriver.jit_merge_point(n=n) |
| 1083 |
print n |
| 1084 |
n -= 1 |
| 1085 |
return n |
| 1086 |
res = self.meta_interp(f, [7]) |
| 1087 |
assert res == 0 |
| 1088 |
|
| 1089 |
def test_bridge_from_interpreter_1(self): |
| 1090 |
mydriver = JitDriver(reds = ['n'], greens = []) |
| 1091 |
|
| 1092 |
def f(n): |
| 1093 |
while n > 0: |
| 1094 |
mydriver.can_enter_jit(n=n) |
| 1095 |
mydriver.jit_merge_point(n=n) |
| 1096 |
n -= 1 |
| 1097 |
|
| 1098 |
self.meta_interp(f, [20], repeat=7) |
| 1099 |
|
| 1100 |
self.check_jitcell_token_count(1) |
| 1101 |
|
| 1102 |
|
| 1103 |
|
| 1104 |
|
| 1105 |
self.check_enter_count(2) |
| 1106 |
|
| 1107 |
def test_bridge_from_interpreter_2(self): |
| 1108 |
|
| 1109 |
mydriver = JitDriver(reds = ['n'], greens = []) |
| 1110 |
glob = [1] |
| 1111 |
|
| 1112 |
def f(n): |
| 1113 |
while n > 0: |
| 1114 |
mydriver.can_enter_jit(n=n) |
| 1115 |
mydriver.jit_merge_point(n=n) |
| 1116 |
if n == 17 and glob[0]: |
| 1117 |
glob[0] = 0 |
| 1118 |
x = n + 1 |
| 1119 |
y = n + 2 |
| 1120 |
z = n + 3 |
| 1121 |
k = n + 4 |
| 1122 |
n -= 1 |
| 1123 |
n += x + y + z + k |
| 1124 |
n -= x + y + z + k |
| 1125 |
n -= 1 |
| 1126 |
|
| 1127 |
self.meta_interp(f, [20], repeat=7) |
| 1128 |
|
| 1129 |
def test_bridge_from_interpreter_3(self): |
| 1130 |
|
| 1131 |
mydriver = JitDriver(reds = ['n', 'x', 'y', 'z', 'k'], greens = []) |
| 1132 |
class Global: |
| 1133 |
pass |
| 1134 |
glob = Global() |
| 1135 |
|
| 1136 |
def f(n): |
| 1137 |
glob.x = 1 |
| 1138 |
x = 0 |
| 1139 |
y = 0 |
| 1140 |
z = 0 |
| 1141 |
k = 0 |
| 1142 |
while n > 0: |
| 1143 |
mydriver.can_enter_jit(n=n, x=x, y=y, z=z, k=k) |
| 1144 |
mydriver.jit_merge_point(n=n, x=x, y=y, z=z, k=k) |
| 1145 |
x += 10 |
| 1146 |
y += 3 |
| 1147 |
z -= 15 |
| 1148 |
k += 4 |
| 1149 |
if n == 17 and glob.x: |
| 1150 |
glob.x = 0 |
| 1151 |
x += n + 1 |
| 1152 |
y += n + 2 |
| 1153 |
z += n + 3 |
| 1154 |
k += n + 4 |
| 1155 |
n -= 1 |
| 1156 |
n -= 1 |
| 1157 |
return x + 2*y + 3*z + 5*k + 13*n |
| 1158 |
|
| 1159 |
res = self.meta_interp(f, [20], repeat=7) |
| 1160 |
assert res == f(20) |
| 1161 |
|
| 1162 |
def test_bridge_from_interpreter_4(self): |
| 1163 |
jitdriver = JitDriver(reds = ['n', 'k'], greens = []) |
| 1164 |
|
| 1165 |
def f(n, k): |
| 1166 |
while n > 0: |
| 1167 |
jitdriver.can_enter_jit(n=n, k=k) |
| 1168 |
jitdriver.jit_merge_point(n=n, k=k) |
| 1169 |
if k: |
| 1170 |
n -= 2 |
| 1171 |
else: |
| 1172 |
n -= 1 |
| 1173 |
return n + k |
| 1174 |
|
| 1175 |
from rpython.rtyper.test.test_llinterp import get_interpreter, clear_tcache |
| 1176 |
from rpython.jit.metainterp.warmspot import WarmRunnerDesc |
| 1177 |
|
| 1178 |
interp, graph = get_interpreter(f, [0, 0], backendopt=False, |
| 1179 |
inline_threshold=0) |
| 1180 |
clear_tcache() |
| 1181 |
translator = interp.typer.annotator.translator |
| 1182 |
translator.config.translation.gc = "boehm" |
| 1183 |
warmrunnerdesc = WarmRunnerDesc(translator, |
| 1184 |
CPUClass=self.CPUClass) |
| 1185 |
state = warmrunnerdesc.jitdrivers_sd[0].warmstate |
| 1186 |
state.set_param_threshold(3) |
| 1187 |
state.set_param_trace_eagerness(0) |
| 1188 |
warmrunnerdesc.finish() |
| 1189 |
for n, k in [(20, 0), (20, 1)]: |
| 1190 |
interp.eval_graph(graph, [n, k]) |
| 1191 |
|
| 1192 |
def test_bridge_leaving_interpreter_5(self): |
| 1193 |
mydriver = JitDriver(reds = ['n', 'x'], greens = []) |
| 1194 |
class Global: |
| 1195 |
pass |
| 1196 |
glob = Global() |
| 1197 |
|
| 1198 |
def f(n): |
| 1199 |
x = 0 |
| 1200 |
glob.x = 1 |
| 1201 |
while n > 0: |
| 1202 |
mydriver.can_enter_jit(n=n, x=x) |
| 1203 |
mydriver.jit_merge_point(n=n, x=x) |
| 1204 |
glob.x += 1 |
| 1205 |
x += 3 |
| 1206 |
n -= 1 |
| 1207 |
glob.x += 100 |
| 1208 |
return glob.x + x |
| 1209 |
res = self.meta_interp(f, [20], repeat=7) |
| 1210 |
assert res == f(20) |
| 1211 |
|
| 1212 |
def test_instantiate_classes(self): |
| 1213 |
class Base: pass |
| 1214 |
class A(Base): foo = 72 |
| 1215 |
class B(Base): foo = 8 |
| 1216 |
def f(n): |
| 1217 |
if n > 5: |
| 1218 |
cls = A |
| 1219 |
else: |
| 1220 |
cls = B |
| 1221 |
return cls().foo |
| 1222 |
res = self.interp_operations(f, [3]) |
| 1223 |
assert res == 8 |
| 1224 |
res = self.interp_operations(f, [13]) |
| 1225 |
assert res == 72 |
| 1226 |
|
| 1227 |
def test_instantiate_does_not_call(self): |
| 1228 |
mydriver = JitDriver(reds = ['n', 'x'], greens = []) |
| 1229 |
class Base: pass |
| 1230 |
class A(Base): foo = 72 |
| 1231 |
class B(Base): foo = 8 |
| 1232 |
|
| 1233 |
def f(n): |
| 1234 |
x = 0 |
| 1235 |
while n > 0: |
| 1236 |
mydriver.can_enter_jit(n=n, x=x) |
| 1237 |
mydriver.jit_merge_point(n=n, x=x) |
| 1238 |
if n & 1 == 0: |
| 1239 |
cls = A |
| 1240 |
else: |
| 1241 |
cls = B |
| 1242 |
inst = cls() |
| 1243 |
x += inst.foo |
| 1244 |
n -= 1 |
| 1245 |
return x |
| 1246 |
res = self.meta_interp(f, [20], enable_opts='') |
| 1247 |
assert res == f(20) |
| 1248 |
self.check_resops(call_i=0, call_r=0) |
| 1249 |
|
| 1250 |
def test_zerodivisionerror(self): |
| 1251 |
|
| 1252 |
|
| 1253 |
|
| 1254 |
def f(n): |
| 1255 |
assert n >= 0 |
| 1256 |
try: |
| 1257 |
return ovfcheck(5 % n) |
| 1258 |
except ZeroDivisionError: |
| 1259 |
return -666 |
| 1260 |
except OverflowError: |
| 1261 |
return -777 |
| 1262 |
res = self.interp_operations(f, [0]) |
| 1263 |
assert res == -666 |
| 1264 |
|
| 1265 |
def f(n): |
| 1266 |
assert n >= 0 |
| 1267 |
try: |
| 1268 |
return ovfcheck(6 // n) |
| 1269 |
except ZeroDivisionError: |
| 1270 |
return -667 |
| 1271 |
except OverflowError: |
| 1272 |
return -778 |
| 1273 |
res = self.interp_operations(f, [0]) |
| 1274 |
assert res == -667 |
| 1275 |
|
| 1276 |
def test_div_overflow(self): |
| 1277 |
import sys |
| 1278 |
myjitdriver = JitDriver(greens = [], reds = ['x', 'y', 'res']) |
| 1279 |
def f(x, y): |
| 1280 |
res = 0 |
| 1281 |
while y > 0: |
| 1282 |
myjitdriver.can_enter_jit(x=x, y=y, res=res) |
| 1283 |
myjitdriver.jit_merge_point(x=x, y=y, res=res) |
| 1284 |
try: |
| 1285 |
res += ovfcheck((-sys.maxint-1) // x) |
| 1286 |
x += 5 |
| 1287 |
except OverflowError: |
| 1288 |
res += 100 |
| 1289 |
y -= 1 |
| 1290 |
return res |
| 1291 |
expected = ((-sys.maxint-1) // (-41) + |
| 1292 |
(-sys.maxint-1) // (-36) + |
| 1293 |
(-sys.maxint-1) // (-31) + |
| 1294 |
(-sys.maxint-1) // (-26) + |
| 1295 |
(-sys.maxint-1) // (-21) + |
| 1296 |
(-sys.maxint-1) // (-16) + |
| 1297 |
(-sys.maxint-1) // (-11) + |
| 1298 |
(-sys.maxint-1) // (-6) + |
| 1299 |
100 * 8) |
| 1300 |
assert f(-41, 16) == expected |
| 1301 |
res = self.meta_interp(f, [-41, 16]) |
| 1302 |
assert res == expected |
| 1303 |
|
| 1304 |
def test_overflow_fold_if_divisor_constant(self): |
| 1305 |
import sys |
| 1306 |
myjitdriver = JitDriver(greens = [], reds = ['x', 'y', 'res']) |
| 1307 |
def f(x, y): |
| 1308 |
res = 0 |
| 1309 |
while y > 0: |
| 1310 |
myjitdriver.can_enter_jit(x=x, y=y, res=res) |
| 1311 |
myjitdriver.jit_merge_point(x=x, y=y, res=res) |
| 1312 |
try: |
| 1313 |
res += ovfcheck(x // 2) |
| 1314 |
res += ovfcheck(x % 2) |
| 1315 |
x += 5 |
| 1316 |
except OverflowError: |
| 1317 |
res += 100 |
| 1318 |
y -= 1 |
| 1319 |
return res |
| 1320 |
res = self.meta_interp(f, [-41, 8]) |
| 1321 |
|
| 1322 |
|
| 1323 |
|
| 1324 |
self.check_resops(guard_true=2, guard_false=0) |
| 1325 |
|
| 1326 |
def test_isinstance(self): |
| 1327 |
class A: |
| 1328 |
pass |
| 1329 |
class B(A): |
| 1330 |
pass |
| 1331 |
@dont_look_inside |
| 1332 |
def extern(n): |
| 1333 |
if n: |
| 1334 |
return A() |
| 1335 |
else: |
| 1336 |
return B() |
| 1337 |
def fn(n): |
| 1338 |
obj = extern(n) |
| 1339 |
return isinstance(obj, B) |
| 1340 |
res = self.interp_operations(fn, [0]) |
| 1341 |
assert res |
| 1342 |
self.check_operations_history(guard_class=1) |
| 1343 |
res = self.interp_operations(fn, [1]) |
| 1344 |
assert not res |
| 1345 |
|
| 1346 |
def test_isinstance_2(self): |
| 1347 |
driver = JitDriver(greens = [], reds = ['n', 'sum', 'x']) |
| 1348 |
class A: |
| 1349 |
pass |
| 1350 |
class B(A): |
| 1351 |
pass |
| 1352 |
class C(B): |
| 1353 |
pass |
| 1354 |
|
| 1355 |
def main(): |
| 1356 |
return f(5, B()) * 10 + f(5, C()) + f(5, A()) * 100 |
| 1357 |
|
| 1358 |
def f(n, x): |
| 1359 |
sum = 0 |
| 1360 |
while n > 0: |
| 1361 |
driver.can_enter_jit(x=x, n=n, sum=sum) |
| 1362 |
driver.jit_merge_point(x=x, n=n, sum=sum) |
| 1363 |
if isinstance(x, B): |
| 1364 |
sum += 1 |
| 1365 |
n -= 1 |
| 1366 |
return sum |
| 1367 |
|
| 1368 |
res = self.meta_interp(main, []) |
| 1369 |
assert res == 55 |
| 1370 |
|
| 1371 |
|
| 1372 |
def test_assert_isinstance(self): |
| 1373 |
class A: |
| 1374 |
pass |
| 1375 |
class B(A): |
| 1376 |
pass |
| 1377 |
def fn(n): |
| 1378 |
|
| 1379 |
if n: |
| 1380 |
obj = B() |
| 1381 |
obj.a = n |
| 1382 |
else: |
| 1383 |
obj = A() |
| 1384 |
obj.a = 17 |
| 1385 |
assert isinstance(obj, B) |
| 1386 |
return obj.a |
| 1387 |
res = self.interp_operations(fn, [1]) |
| 1388 |
assert res == 1 |
| 1389 |
self.check_operations_history(guard_class=0) |
| 1390 |
|
| 1391 |
def test_r_dict(self): |
| 1392 |
from rpython.rlib.objectmodel import r_dict |
| 1393 |
class FooError(Exception): |
| 1394 |
pass |
| 1395 |
def myeq(n, m): |
| 1396 |
return n == m |
| 1397 |
def myhash(n): |
| 1398 |
if n < 0: |
| 1399 |
raise FooError |
| 1400 |
return -n |
| 1401 |
def f(n): |
| 1402 |
d = r_dict(myeq, myhash) |
| 1403 |
for i in range(10): |
| 1404 |
d[i] = i*i |
| 1405 |
try: |
| 1406 |
return d[n] |
| 1407 |
except FooError: |
| 1408 |
return 99 |
| 1409 |
res = self.interp_operations(f, [5]) |
| 1410 |
assert res == f(5) |
| 1411 |
|
| 1412 |
def test_free_object(self): |
| 1413 |
import weakref |
| 1414 |
myjitdriver = JitDriver(greens = [], reds = ['n', 'x']) |
| 1415 |
class X(object): |
| 1416 |
pass |
| 1417 |
def main(n, x): |
| 1418 |
while n > 0: |
| 1419 |
myjitdriver.can_enter_jit(n=n, x=x) |
| 1420 |
myjitdriver.jit_merge_point(n=n, x=x) |
| 1421 |
n -= x.foo |
| 1422 |
def g(n): |
| 1423 |
x = X() |
| 1424 |
x.foo = 2 |
| 1425 |
main(n, x) |
| 1426 |
x.foo = 5 |
| 1427 |
return weakref.ref(x) |
| 1428 |
def f(n): |
| 1429 |
r = g(n) |
| 1430 |
rgc.collect(); rgc.collect(); rgc.collect() |
| 1431 |
return r() is None |
| 1432 |
|
| 1433 |
assert f(30) == 1 |
| 1434 |
res = self.meta_interp(f, [30], no_stats=True) |
| 1435 |
assert res == 1 |
| 1436 |
|
| 1437 |
def test_pass_around(self): |
| 1438 |
myjitdriver = JitDriver(greens = [], reds = ['n', 'x']) |
| 1439 |
|
| 1440 |
def call(): |
| 1441 |
pass |
| 1442 |
|
| 1443 |
def f(n, x): |
| 1444 |
while n > 0: |
| 1445 |
myjitdriver.can_enter_jit(n=n, x=x) |
| 1446 |
myjitdriver.jit_merge_point(n=n, x=x) |
| 1447 |
if n % 2: |
| 1448 |
call() |
| 1449 |
if n == 8: |
| 1450 |
return x |
| 1451 |
x = 3 |
| 1452 |
else: |
| 1453 |
x = 5 |
| 1454 |
n -= 1 |
| 1455 |
return 0 |
| 1456 |
|
| 1457 |
self.meta_interp(f, [40, 0]) |
| 1458 |
|
| 1459 |
def test_const_inputargs(self): |
| 1460 |
myjitdriver = JitDriver(greens = ['m'], reds = ['n', 'x']) |
| 1461 |
def f(n, x): |
| 1462 |
m = 0x7FFFFFFF |
| 1463 |
while n > 0: |
| 1464 |
myjitdriver.can_enter_jit(m=m, n=n, x=x) |
| 1465 |
myjitdriver.jit_merge_point(m=m, n=n, x=x) |
| 1466 |
x = 42 |
| 1467 |
n -= 1 |
| 1468 |
m = m >> 1 |
| 1469 |
return x |
| 1470 |
|
| 1471 |
res = self.meta_interp(f, [50, 1], enable_opts='') |
| 1472 |
assert res == 42 |
| 1473 |
|
| 1474 |
def test_set_param(self): |
| 1475 |
myjitdriver = JitDriver(greens = [], reds = ['n', 'x']) |
| 1476 |
def g(n): |
| 1477 |
x = 0 |
| 1478 |
while n > 0: |
| 1479 |
myjitdriver.can_enter_jit(n=n, x=x) |
| 1480 |
myjitdriver.jit_merge_point(n=n, x=x) |
| 1481 |
n -= 1 |
| 1482 |
x += n |
| 1483 |
return x |
| 1484 |
def f(n, threshold, arg): |
| 1485 |
if arg: |
| 1486 |
set_param(myjitdriver, 'threshold', threshold) |
| 1487 |
else: |
| 1488 |
set_param(None, 'threshold', threshold) |
| 1489 |
return g(n) |
| 1490 |
|
| 1491 |
res = self.meta_interp(f, [10, 3, 1]) |
| 1492 |
assert res == 9 + 8 + 7 + 6 + 5 + 4 + 3 + 2 + 1 + 0 |
| 1493 |
self.check_jitcell_token_count(1) |
| 1494 |
|
| 1495 |
res = self.meta_interp(f, [10, 13, 0]) |
| 1496 |
assert res == 9 + 8 + 7 + 6 + 5 + 4 + 3 + 2 + 1 + 0 |
| 1497 |
self.check_jitcell_token_count(0) |
| 1498 |
|
| 1499 |
def test_set_param_pureops_historylength(self): |
| 1500 |
myjitdriver = JitDriver(greens=[], reds='auto') |
| 1501 |
def g(i): |
| 1502 |
set_param(myjitdriver, 'pureop_historylength', i) |
| 1503 |
x1 = x2 = x3 = x4 = 0 |
| 1504 |
while x1 < 100: |
| 1505 |
myjitdriver.jit_merge_point() |
| 1506 |
a = x1 + 1 |
| 1507 |
x2 += 1 |
| 1508 |
x3 += 1 |
| 1509 |
x4 += 1 |
| 1510 |
x1 += 1 |
| 1511 |
return a |
| 1512 |
res = self.meta_interp(g, [4]) |
| 1513 |
self.check_resops(int_add=10) |
| 1514 |
res = self.meta_interp(g, [16]) |
| 1515 |
self.check_resops(int_add=8) |
| 1516 |
|
| 1517 |
def test_dont_look_inside(self): |
| 1518 |
@dont_look_inside |
| 1519 |
def g(a, b): |
| 1520 |
return a + b |
| 1521 |
def f(a, b): |
| 1522 |
return g(a, b) |
| 1523 |
res = self.interp_operations(f, [3, 5]) |
| 1524 |
assert res == 8 |
| 1525 |
self.check_operations_history(int_add=0, call_i=1) |
| 1526 |
|
| 1527 |
def test_listcomp(self): |
| 1528 |
myjitdriver = JitDriver(greens = [], reds = ['x', 'y', 'lst']) |
| 1529 |
def f(x, y): |
| 1530 |
lst = [0, 0, 0] |
| 1531 |
while y > 0: |
| 1532 |
myjitdriver.can_enter_jit(x=x, y=y, lst=lst) |
| 1533 |
myjitdriver.jit_merge_point(x=x, y=y, lst=lst) |
| 1534 |
lst = [i+x for i in lst if i >=0] |
| 1535 |
y -= 1 |
| 1536 |
return lst[0] |
| 1537 |
res = self.meta_interp(f, [6, 7], listcomp=True, backendopt=True, listops=True) |
| 1538 |
|
| 1539 |
assert res == 42 |
| 1540 |
|
| 1541 |
def test_tuple_immutable(self): |
| 1542 |
def new(a, b): |
| 1543 |
return a, b |
| 1544 |
def f(a, b): |
| 1545 |
tup = new(a, b) |
| 1546 |
return tup[1] |
| 1547 |
res = self.interp_operations(f, [3, 5]) |
| 1548 |
assert res == 5 |
| 1549 |
self.check_operations_history(setfield_gc=2, getfield_gc_i=0) |
| 1550 |
|
| 1551 |
def test_oosend_look_inside_only_one(self): |
| 1552 |
class A: |
| 1553 |
pass |
| 1554 |
class B(A): |
| 1555 |
def g(self): |
| 1556 |
return 123 |
| 1557 |
class C(A): |
| 1558 |
@dont_look_inside |
| 1559 |
def g(self): |
| 1560 |
return 456 |
| 1561 |
def f(n): |
| 1562 |
if n > 3: |
| 1563 |
x = B() |
| 1564 |
else: |
| 1565 |
x = C() |
| 1566 |
return x.g() + x.g() |
| 1567 |
res = self.interp_operations(f, [10]) |
| 1568 |
assert res == 123 * 2 |
| 1569 |
res = self.interp_operations(f, [-10]) |
| 1570 |
assert res == 456 * 2 |
| 1571 |
|
| 1572 |
def test_residual_external_call(self): |
| 1573 |
import math |
| 1574 |
myjitdriver = JitDriver(greens = [], reds = ['y', 'x', 'res']) |
| 1575 |
|
| 1576 |
|
| 1577 |
|
| 1578 |
|
| 1579 |
@dont_look_inside |
| 1580 |
def modf(x): |
| 1581 |
return math.modf(x) |
| 1582 |
|
| 1583 |
def f(x, y): |
| 1584 |
x = float(x) |
| 1585 |
res = 0.0 |
| 1586 |
while y > 0: |
| 1587 |
myjitdriver.can_enter_jit(x=x, y=y, res=res) |
| 1588 |
myjitdriver.jit_merge_point(x=x, y=y, res=res) |
| 1589 |
|
| 1590 |
rpart, ipart = modf(x) |
| 1591 |
res += ipart |
| 1592 |
y -= 1 |
| 1593 |
return res |
| 1594 |
res = self.meta_interp(f, [6, 7]) |
| 1595 |
assert res == 42 |
| 1596 |
self.check_trace_count(1) |
| 1597 |
self.check_resops(call_r=2) |
| 1598 |
|
| 1599 |
def test_merge_guardclass_guardvalue(self): |
| 1600 |
myjitdriver = JitDriver(greens = [], reds = ['x', 'l']) |
| 1601 |
|
| 1602 |
class A(object): |
| 1603 |
def g(self, x): |
| 1604 |
return x - 5 |
| 1605 |
class B(A): |
| 1606 |
def g(self, y): |
| 1607 |
return y - 3 |
| 1608 |
|
| 1609 |
a1 = A() |
| 1610 |
a2 = A() |
| 1611 |
b = B() |
| 1612 |
def f(x): |
| 1613 |
l = [a1] * 100 + [a2] * 100 + [b] * 100 |
| 1614 |
while x > 0: |
| 1615 |
myjitdriver.can_enter_jit(x=x, l=l) |
| 1616 |
myjitdriver.jit_merge_point(x=x, l=l) |
| 1617 |
a = l[x] |
| 1618 |
x = a.g(x) |
| 1619 |
promote(a) |
| 1620 |
return x |
| 1621 |
res = self.meta_interp(f, [299], listops=True) |
| 1622 |
assert res == f(299) |
| 1623 |
self.check_resops(guard_class=0, guard_value=6) |
| 1624 |
|
| 1625 |
def test_merge_guardnonnull_guardclass(self): |
| 1626 |
myjitdriver = JitDriver(greens = [], reds = ['x', 'l']) |
| 1627 |
|
| 1628 |
class A(object): |
| 1629 |
def g(self, x): |
| 1630 |
return x - 3 |
| 1631 |
class B(A): |
| 1632 |
def g(self, y): |
| 1633 |
return y - 5 |
| 1634 |
|
| 1635 |
a1 = A() |
| 1636 |
b1 = B() |
| 1637 |
def f(x): |
| 1638 |
l = [None] * 100 + [b1] * 100 + [a1] * 100 |
| 1639 |
while x > 0: |
| 1640 |
myjitdriver.can_enter_jit(x=x, l=l) |
| 1641 |
myjitdriver.jit_merge_point(x=x, l=l) |
| 1642 |
a = l[x] |
| 1643 |
if a: |
| 1644 |
x = a.g(x) |
| 1645 |
else: |
| 1646 |
x -= 7 |
| 1647 |
return x |
| 1648 |
res = self.meta_interp(f, [299], listops=True) |
| 1649 |
assert res == f(299) |
| 1650 |
self.check_resops(guard_class=0, guard_nonnull=4, |
| 1651 |
guard_nonnull_class=4, guard_isnull=2) |
| 1652 |
|
| 1653 |
|
| 1654 |
def test_merge_guardnonnull_guardvalue(self): |
| 1655 |
myjitdriver = JitDriver(greens = [], reds = ['x', 'l']) |
| 1656 |
|
| 1657 |
class A(object): |
| 1658 |
pass |
| 1659 |
class B(A): |
| 1660 |
pass |
| 1661 |
|
| 1662 |
a1 = A() |
| 1663 |
b1 = B() |
| 1664 |
def f(x): |
| 1665 |
l = [b1] * 100 + [None] * 100 + [a1] * 100 |
| 1666 |
while x > 0: |
| 1667 |
myjitdriver.can_enter_jit(x=x, l=l) |
| 1668 |
myjitdriver.jit_merge_point(x=x, l=l) |
| 1669 |
a = l[x] |
| 1670 |
if a: |
| 1671 |
x -= 5 |
| 1672 |
else: |
| 1673 |
x -= 7 |
| 1674 |
promote(a) |
| 1675 |
return x |
| 1676 |
res = self.meta_interp(f, [299], listops=True) |
| 1677 |
assert res == f(299) |
| 1678 |
self.check_resops(guard_value=4, guard_class=0, guard_nonnull=4, |
| 1679 |
guard_nonnull_class=0, guard_isnull=2) |
| 1680 |
|
| 1681 |
|
| 1682 |
def test_merge_guardnonnull_guardvalue_2(self): |
| 1683 |
myjitdriver = JitDriver(greens = [], reds = ['x', 'l']) |
| 1684 |
|
| 1685 |
class A(object): |
| 1686 |
pass |
| 1687 |
class B(A): |
| 1688 |
pass |
| 1689 |
|
| 1690 |
a1 = A() |
| 1691 |
b1 = B() |
| 1692 |
def f(x): |
| 1693 |
l = [None] * 100 + [b1] * 100 + [a1] * 100 |
| 1694 |
while x > 0: |
| 1695 |
myjitdriver.can_enter_jit(x=x, l=l) |
| 1696 |
myjitdriver.jit_merge_point(x=x, l=l) |
| 1697 |
a = l[x] |
| 1698 |
if a: |
| 1699 |
x -= 5 |
| 1700 |
else: |
| 1701 |
x -= 7 |
| 1702 |
promote(a) |
| 1703 |
return x |
| 1704 |
res = self.meta_interp(f, [299], listops=True) |
| 1705 |
assert res == f(299) |
| 1706 |
self.check_resops(guard_value=4, guard_class=0, guard_nonnull=4, |
| 1707 |
guard_nonnull_class=0, guard_isnull=2) |
| 1708 |
|
| 1709 |
|
| 1710 |
def test_merge_guardnonnull_guardclass_guardvalue(self): |
| 1711 |
myjitdriver = JitDriver(greens = [], reds = ['x', 'l']) |
| 1712 |
|
| 1713 |
class A(object): |
| 1714 |
def g(self, x): |
| 1715 |
return x - 3 |
| 1716 |
class B(A): |
| 1717 |
def g(self, y): |
| 1718 |
return y - 5 |
| 1719 |
|
| 1720 |
a1 = A() |
| 1721 |
a2 = A() |
| 1722 |
b1 = B() |
| 1723 |
def f(x): |
| 1724 |
l = [a2] * 100 + [None] * 100 + [b1] * 100 + [a1] * 100 |
| 1725 |
while x > 0: |
| 1726 |
myjitdriver.can_enter_jit(x=x, l=l) |
| 1727 |
myjitdriver.jit_merge_point(x=x, l=l) |
| 1728 |
a = l[x] |
| 1729 |
if a: |
| 1730 |
x = a.g(x) |
| 1731 |
else: |
| 1732 |
x -= 7 |
| 1733 |
promote(a) |
| 1734 |
return x |
| 1735 |
res = self.meta_interp(f, [399], listops=True) |
| 1736 |
assert res == f(399) |
| 1737 |
self.check_resops(guard_class=0, guard_nonnull=6, guard_value=6, |
| 1738 |
guard_nonnull_class=0, guard_isnull=2) |
| 1739 |
|
| 1740 |
|
| 1741 |
def test_residual_call_doesnt_lose_info(self): |
| 1742 |
myjitdriver = JitDriver(greens = [], reds = ['x', 'y', 'l']) |
| 1743 |
|
| 1744 |
class A(object): |
| 1745 |
pass |
| 1746 |
|
| 1747 |
globall = [""] |
| 1748 |
@dont_look_inside |
| 1749 |
def g(x): |
| 1750 |
globall[0] = str(x) |
| 1751 |
return x |
| 1752 |
|
| 1753 |
def f(x): |
| 1754 |
y = A() |
| 1755 |
y.v = x |
| 1756 |
l = [0] |
| 1757 |
while y.v > 0: |
| 1758 |
myjitdriver.can_enter_jit(x=x, y=y, l=l) |
| 1759 |
myjitdriver.jit_merge_point(x=x, y=y, l=l) |
| 1760 |
l[0] = y.v |
| 1761 |
lc = l[0] |
| 1762 |
y.v = g(y.v) - y.v/y.v + lc/l[0] - 1 |
| 1763 |
return y.v |
| 1764 |
res = self.meta_interp(f, [20], listops=True) |
| 1765 |
self.check_resops(getarrayitem_gc_i=0, getfield_gc_i=1) |
| 1766 |
|
| 1767 |
def test_guard_isnull_nonnull(self): |
| 1768 |
myjitdriver = JitDriver(greens = [], reds = ['x', 'res']) |
| 1769 |
class A(object): |
| 1770 |
pass |
| 1771 |
|
| 1772 |
@dont_look_inside |
| 1773 |
def create(x): |
| 1774 |
if x >= -40: |
| 1775 |
return A() |
| 1776 |
return None |
| 1777 |
|
| 1778 |
def f(x): |
| 1779 |
res = 0 |
| 1780 |
while x > 0: |
| 1781 |
myjitdriver.can_enter_jit(x=x, res=res) |
| 1782 |
myjitdriver.jit_merge_point(x=x, res=res) |
| 1783 |
obj = create(x-1) |
| 1784 |
if obj is not None: |
| 1785 |
res += 1 |
| 1786 |
obj2 = create(x-1000) |
| 1787 |
if obj2 is None: |
| 1788 |
res += 1 |
| 1789 |
x -= 1 |
| 1790 |
return res |
| 1791 |
res = self.meta_interp(f, [21]) |
| 1792 |
assert res == 42 |
| 1793 |
self.check_resops(guard_nonnull=2, guard_isnull=2) |
| 1794 |
|
| 1795 |
def test_loop_invariant1(self): |
| 1796 |
myjitdriver = JitDriver(greens = [], reds = ['x', 'res']) |
| 1797 |
class A(object): |
| 1798 |
pass |
| 1799 |
a = A() |
| 1800 |
a.current_a = A() |
| 1801 |
a.current_a.x = 1 |
| 1802 |
@loop_invariant |
| 1803 |
def f(): |
| 1804 |
return a.current_a |
| 1805 |
|
| 1806 |
def g(x): |
| 1807 |
res = 0 |
| 1808 |
while x > 0: |
| 1809 |
myjitdriver.can_enter_jit(x=x, res=res) |
| 1810 |
myjitdriver.jit_merge_point(x=x, res=res) |
| 1811 |
res += f().x |
| 1812 |
res += f().x |
| 1813 |
res += f().x |
| 1814 |
x -= 1 |
| 1815 |
a.current_a = A() |
| 1816 |
a.current_a.x = 2 |
| 1817 |
return res |
| 1818 |
res = self.meta_interp(g, [21]) |
| 1819 |
assert res == 3 * 21 |
| 1820 |
self.check_resops(call_r=1) |
| 1821 |
|
| 1822 |
def test_bug_optimizeopt_mutates_ops(self): |
| 1823 |
myjitdriver = JitDriver(greens = [], reds = ['x', 'res', 'const', 'a']) |
| 1824 |
class A(object): |
| 1825 |
pass |
| 1826 |
class B(A): |
| 1827 |
pass |
| 1828 |
|
| 1829 |
glob = A() |
| 1830 |
glob.a = None |
| 1831 |
def f(x): |
| 1832 |
res = 0 |
| 1833 |
a = A() |
| 1834 |
a.x = 0 |
| 1835 |
glob.a = A() |
| 1836 |
const = 2 |
| 1837 |
while x > 0: |
| 1838 |
myjitdriver.can_enter_jit(x=x, res=res, a=a, const=const) |
| 1839 |
myjitdriver.jit_merge_point(x=x, res=res, a=a, const=const) |
| 1840 |
if type(glob.a) is B: |
| 1841 |
res += 1 |
| 1842 |
if a is None: |
| 1843 |
a = A() |
| 1844 |
a.x = x |
| 1845 |
glob.a = B() |
| 1846 |
const = 2 |
| 1847 |
else: |
| 1848 |
promote(const) |
| 1849 |
x -= const |
| 1850 |
res += a.x |
| 1851 |
a = None |
| 1852 |
glob.a = A() |
| 1853 |
const = 1 |
| 1854 |
return res |
| 1855 |
res = self.meta_interp(f, [21]) |
| 1856 |
assert res == f(21) |
| 1857 |
|
| 1858 |
def test_getitem_indexerror(self): |
| 1859 |
lst = [10, 4, 9, 16] |
| 1860 |
def f(n): |
| 1861 |
try: |
| 1862 |
return lst[n] |
| 1863 |
except IndexError: |
| 1864 |
return -2 |
| 1865 |
res = self.interp_operations(f, [2]) |
| 1866 |
assert res == 9 |
| 1867 |
res = self.interp_operations(f, [4]) |
| 1868 |
assert res == -2 |
| 1869 |
res = self.interp_operations(f, [-4]) |
| 1870 |
assert res == 10 |
| 1871 |
res = self.interp_operations(f, [-5]) |
| 1872 |
assert res == -2 |
| 1873 |
|
| 1874 |
def test_guard_always_changing_value(self): |
| 1875 |
myjitdriver = JitDriver(greens = [], reds = ['x', 'a']) |
| 1876 |
def f(x): |
| 1877 |
a = 0 |
| 1878 |
while x > 0: |
| 1879 |
myjitdriver.can_enter_jit(x=x, a=a) |
| 1880 |
myjitdriver.jit_merge_point(x=x, a=a) |
| 1881 |
a += 1 |
| 1882 |
promote(a) |
| 1883 |
x -= 1 |
| 1884 |
self.meta_interp(f, [50]) |
| 1885 |
self.check_trace_count(1) |
| 1886 |
|
| 1887 |
|
| 1888 |
|
| 1889 |
def test_swap_values(self): |
| 1890 |
def f(x, y): |
| 1891 |
if x > 5: |
| 1892 |
x, y = y, x |
| 1893 |
return x - y |
| 1894 |
res = self.interp_operations(f, [10, 2]) |
| 1895 |
assert res == -8 |
| 1896 |
res = self.interp_operations(f, [3, 2]) |
| 1897 |
assert res == 1 |
| 1898 |
|
| 1899 |
def test_raw_malloc_and_access(self): |
| 1900 |
TP = rffi.CArray(lltype.Signed) |
| 1901 |
|
| 1902 |
def f(n): |
| 1903 |
a = lltype.malloc(TP, n, flavor='raw') |
| 1904 |
a[0] = n |
| 1905 |
res = a[0] |
| 1906 |
lltype.free(a, flavor='raw') |
| 1907 |
return res |
| 1908 |
|
| 1909 |
res = self.interp_operations(f, [10]) |
| 1910 |
assert res == 10 |
| 1911 |
|
| 1912 |
def test_raw_malloc_and_access_float(self): |
| 1913 |
TP = rffi.CArray(lltype.Float) |
| 1914 |
|
| 1915 |
def f(n, f): |
| 1916 |
a = lltype.malloc(TP, n, flavor='raw') |
| 1917 |
a[0] = f |
| 1918 |
res = a[0] |
| 1919 |
lltype.free(a, flavor='raw') |
| 1920 |
return res |
| 1921 |
|
| 1922 |
res = self.interp_operations(f, [10, 3.5]) |
| 1923 |
assert res == 3.5 |
| 1924 |
|
| 1925 |
def test_jit_debug(self): |
| 1926 |
myjitdriver = JitDriver(greens = [], reds = ['x']) |
| 1927 |
class A: |
| 1928 |
pass |
| 1929 |
def f(x): |
| 1930 |
while x > 0: |
| 1931 |
myjitdriver.can_enter_jit(x=x) |
| 1932 |
myjitdriver.jit_merge_point(x=x) |
| 1933 |
jit_debug("hi there:", x) |
| 1934 |
jit_debug("foobar") |
| 1935 |
x -= 1 |
| 1936 |
return x |
| 1937 |
res = self.meta_interp(f, [8]) |
| 1938 |
assert res == 0 |
| 1939 |
self.check_resops(jit_debug=4) |
| 1940 |
|
| 1941 |
def test_assert_green(self): |
| 1942 |
def f(x, promote_flag): |
| 1943 |
if promote_flag: |
| 1944 |
promote(x) |
| 1945 |
assert_green(x) |
| 1946 |
return x |
| 1947 |
res = self.interp_operations(f, [8, 1]) |
| 1948 |
assert res == 8 |
| 1949 |
py.test.raises(AssertGreenFailed, self.interp_operations, f, [8, 0]) |
| 1950 |
|
| 1951 |
def test_multiple_specialied_versions1(self): |
| 1952 |
myjitdriver = JitDriver(greens = [], reds = ['y', 'x', 'res']) |
| 1953 |
class Base: |
| 1954 |
def __init__(self, val): |
| 1955 |
self.val = val |
| 1956 |
class A(Base): |
| 1957 |
def binop(self, other): |
| 1958 |
return A(self.val + other.val) |
| 1959 |
class B(Base): |
| 1960 |
def binop(self, other): |
| 1961 |
return B(self.val * other.val) |
| 1962 |
def f(x, y): |
| 1963 |
res = x |
| 1964 |
while y > 0: |
| 1965 |
myjitdriver.can_enter_jit(y=y, x=x, res=res) |
| 1966 |
myjitdriver.jit_merge_point(y=y, x=x, res=res) |
| 1967 |
res = res.binop(x) |
| 1968 |
y -= 1 |
| 1969 |
return res |
| 1970 |
def g(x, y): |
| 1971 |
a1 = f(A(x), y) |
| 1972 |
a2 = f(A(x), y) |
| 1973 |
b1 = f(B(x), y) |
| 1974 |
b2 = f(B(x), y) |
| 1975 |
assert a1.val == a2.val |
| 1976 |
assert b1.val == b2.val |
| 1977 |
return a1.val + b1.val |
| 1978 |
res = self.meta_interp(g, [6, 7]) |
| 1979 |
assert res == 6*8 + 6**8 |
| 1980 |
self.check_trace_count(4) |
| 1981 |
self.check_resops({'guard_class': 2, 'int_gt': 4, |
| 1982 |
'getfield_gc_i': 4, 'guard_true': 4, |
| 1983 |
'int_sub': 4, 'jump': 2, 'int_mul': 2, |
| 1984 |
'int_add': 2}) |
| 1985 |
|
| 1986 |
def test_multiple_specialied_versions_array(self): |
| 1987 |
myjitdriver = JitDriver(greens = [], reds = ['idx', 'y', 'x', 'res', |
| 1988 |
'array']) |
| 1989 |
class Base: |
| 1990 |
def __init__(self, val): |
| 1991 |
self.val = val |
| 1992 |
class A(Base): |
| 1993 |
def binop(self, other): |
| 1994 |
return A(self.val + other.val) |
| 1995 |
class B(Base): |
| 1996 |
def binop(self, other): |
| 1997 |
return B(self.val - other.val) |
| 1998 |
def f(x, y): |
| 1999 |
res = x |
| 2000 |
array = [1, 2, 3] |
| 2001 |
array[1] = 7 |
| 2002 |
idx = 0 |
| 2003 |
while y > 0: |
| 2004 |
myjitdriver.can_enter_jit(idx=idx, y=y, x=x, res=res, |
| 2005 |
array=array) |
| 2006 |
myjitdriver.jit_merge_point(idx=idx, y=y, x=x, res=res, |
| 2007 |
array=array) |
| 2008 |
res = res.binop(x) |
| 2009 |
res.val += array[idx] + array[1] |
| 2010 |
if y < 10: |
| 2011 |
idx = 2 |
| 2012 |
y -= 1 |
| 2013 |
return res |
| 2014 |
def g(x, y): |
| 2015 |
a1 = f(A(x), y) |
| 2016 |
a2 = f(A(x), y) |
| 2017 |
b1 = f(B(x), y) |
| 2018 |
b2 = f(B(x), y) |
| 2019 |
assert a1.val == a2.val |
| 2020 |
assert b1.val == b2.val |
| 2021 |
return a1.val + b1.val |
| 2022 |
res = self.meta_interp(g, [6, 20]) |
| 2023 |
assert res == g(6, 20) |
| 2024 |
self.check_trace_count(8) |
| 2025 |
|
| 2026 |
self.check_resops(getarrayitem_gc_i=10 + 6) |
| 2027 |
|
| 2028 |
def test_multiple_specialied_versions_bridge(self): |
| 2029 |
myjitdriver = JitDriver(greens = [], reds = ['y', 'x', 'z', 'res']) |
| 2030 |
class Base: |
| 2031 |
def __init__(self, val): |
| 2032 |
self.val = val |
| 2033 |
def getval(self): |
| 2034 |
return self.val |
| 2035 |
class A(Base): |
| 2036 |
def binop(self, other): |
| 2037 |
return A(self.getval() + other.getval()) |
| 2038 |
class B(Base): |
| 2039 |
def binop(self, other): |
| 2040 |
return B(self.getval() * other.getval()) |
| 2041 |
def f(x, y, z): |
| 2042 |
res = x |
| 2043 |
while y > 0: |
| 2044 |
myjitdriver.can_enter_jit(y=y, x=x, z=z, res=res) |
| 2045 |
myjitdriver.jit_merge_point(y=y, x=x, z=z, res=res) |
| 2046 |
res = res.binop(x) |
| 2047 |
y -= 1 |
| 2048 |
if y < 7: |
| 2049 |
x = z |
| 2050 |
return res |
| 2051 |
def g(x, y): |
| 2052 |
a1 = f(A(x), y, A(x)) |
| 2053 |
a2 = f(A(x), y, A(x)) |
| 2054 |
assert a1.val == a2.val |
| 2055 |
b1 = f(B(x), y, B(x)) |
| 2056 |
b2 = f(B(x), y, B(x)) |
| 2057 |
assert b1.val == b2.val |
| 2058 |
c1 = f(B(x), y, A(x)) |
| 2059 |
c2 = f(B(x), y, A(x)) |
| 2060 |
assert c1.val == c2.val |
| 2061 |
d1 = f(A(x), y, B(x)) |
| 2062 |
d2 = f(A(x), y, B(x)) |
| 2063 |
assert d1.val == d2.val |
| 2064 |
return a1.val + b1.val + c1.val + d1.val |
| 2065 |
res = self.meta_interp(g, [3, 14]) |
| 2066 |
assert res == g(3, 14) |
| 2067 |
|
| 2068 |
def test_failing_inlined_guard(self): |
| 2069 |
myjitdriver = JitDriver(greens = [], reds = ['y', 'x', 'z', 'res']) |
| 2070 |
class Base: |
| 2071 |
def __init__(self, val): |
| 2072 |
self.val = val |
| 2073 |
def getval(self): |
| 2074 |
return self.val |
| 2075 |
class A(Base): |
| 2076 |
def binop(self, other): |
| 2077 |
return A(self.getval() + other.getval()) |
| 2078 |
class B(Base): |
| 2079 |
def binop(self, other): |
| 2080 |
return B(self.getval() * other.getval()) |
| 2081 |
def f(x, y, z): |
| 2082 |
res = x |
| 2083 |
while y > 0: |
| 2084 |
myjitdriver.can_enter_jit(y=y, x=x, z=z, res=res) |
| 2085 |
myjitdriver.jit_merge_point(y=y, x=x, z=z, res=res) |
| 2086 |
res = res.binop(x) |
| 2087 |
y -= 1 |
| 2088 |
if y < 8: |
| 2089 |
x = z |
| 2090 |
return res |
| 2091 |
def g(x, y): |
| 2092 |
c1 = f(A(x), y, B(x)) |
| 2093 |
c2 = f(A(x), y, B(x)) |
| 2094 |
assert c1.val == c2.val |
| 2095 |
return c1.val |
| 2096 |
res = self.meta_interp(g, [3, 16]) |
| 2097 |
assert res == g(3, 16) |
| 2098 |
|
| 2099 |
def test_inlined_guard_in_short_preamble(self): |
| 2100 |
myjitdriver = JitDriver(greens = [], reds = ['y', 'x', 'z', 'res']) |
| 2101 |
class A: |
| 2102 |
def __init__(self, val): |
| 2103 |
self.val = val |
| 2104 |
def getval(self): |
| 2105 |
return self.val |
| 2106 |
def binop(self, other): |
| 2107 |
return A(self.getval() + other.getval()) |
| 2108 |
def f(x, y, z): |
| 2109 |
res = x |
| 2110 |
while y > 0: |
| 2111 |
myjitdriver.can_enter_jit(y=y, x=x, z=z, res=res) |
| 2112 |
myjitdriver.jit_merge_point(y=y, x=x, z=z, res=res) |
| 2113 |
res = res.binop(x) |
| 2114 |
y -= 1 |
| 2115 |
if y < 7: |
| 2116 |
x = z |
| 2117 |
return res |
| 2118 |
def g(x, y): |
| 2119 |
a1 = f(A(x), y, A(x)) |
| 2120 |
a2 = f(A(x), y, A(x)) |
| 2121 |
assert a1.val == a2.val |
| 2122 |
return a1.val |
| 2123 |
res = self.meta_interp(g, [3, 14]) |
| 2124 |
assert res == g(3, 14) |
| 2125 |
|
| 2126 |
def test_specialized_bridge(self): |
| 2127 |
myjitdriver = JitDriver(greens = [], reds = ['y', 'x', 'res']) |
| 2128 |
class A: |
| 2129 |
def __init__(self, val): |
| 2130 |
self.val = val |
| 2131 |
def binop(self, other): |
| 2132 |
return A(self.val + other.val) |
| 2133 |
def f(x, y): |
| 2134 |
res = A(0) |
| 2135 |
while y > 0: |
| 2136 |
myjitdriver.can_enter_jit(y=y, x=x, res=res) |
| 2137 |
myjitdriver.jit_merge_point(y=y, x=x, res=res) |
| 2138 |
res = res.binop(A(y)) |
| 2139 |
if y<7: |
| 2140 |
res = x |
| 2141 |
y -= 1 |
| 2142 |
return res |
| 2143 |
def g(x, y): |
| 2144 |
a1 = f(A(x), y) |
| 2145 |
a2 = f(A(x), y) |
| 2146 |
assert a1.val == a2.val |
| 2147 |
return a1.val |
| 2148 |
res = self.meta_interp(g, [6, 14]) |
| 2149 |
assert res == g(6, 14) |
| 2150 |
|
| 2151 |
def test_specialied_bridge_const(self): |
| 2152 |
myjitdriver = JitDriver(greens = [], reds = ['y', 'const', 'x', 'res']) |
| 2153 |
class A: |
| 2154 |
def __init__(self, val): |
| 2155 |
self.val = val |
| 2156 |
def binop(self, other): |
| 2157 |
return A(self.val + other.val) |
| 2158 |
def f(x, y): |
| 2159 |
res = A(0) |
| 2160 |
const = 7 |
| 2161 |
while y > 0: |
| 2162 |
myjitdriver.can_enter_jit(y=y, x=x, res=res, const=const) |
| 2163 |
myjitdriver.jit_merge_point(y=y, x=x, res=res, const=const) |
| 2164 |
const = promote(const) |
| 2165 |
res = res.binop(A(const)) |
| 2166 |
if y<7: |
| 2167 |
res = x |
| 2168 |
y -= 1 |
| 2169 |
return res |
| 2170 |
def g(x, y): |
| 2171 |
a1 = f(A(x), y) |
| 2172 |
a2 = f(A(x), y) |
| 2173 |
assert a1.val == a2.val |
| 2174 |
return a1.val |
| 2175 |
res = self.meta_interp(g, [6, 14]) |
| 2176 |
assert res == g(6, 14) |
| 2177 |
|
| 2178 |
def test_multiple_specialied_zigzag(self): |
| 2179 |
myjitdriver = JitDriver(greens = [], reds = ['y', 'x', 'res']) |
| 2180 |
class Base: |
| 2181 |
def __init__(self, val): |
| 2182 |
self.val = val |
| 2183 |
class A(Base): |
| 2184 |
def binop(self, other): |
| 2185 |
return A(self.val + other.val) |
| 2186 |
def switch(self): |
| 2187 |
return B(self.val) |
| 2188 |
class B(Base): |
| 2189 |
def binop(self, other): |
| 2190 |
return B(self.val * other.val) |
| 2191 |
def switch(self): |
| 2192 |
return A(self.val) |
| 2193 |
def f(x, y): |
| 2194 |
res = x |
| 2195 |
while y > 0: |
| 2196 |
myjitdriver.can_enter_jit(y=y, x=x, res=res) |
| 2197 |
myjitdriver.jit_merge_point(y=y, x=x, res=res) |
| 2198 |
if y % 4 == 0: |
| 2199 |
res = res.switch() |
| 2200 |
res = res.binop(x) |
| 2201 |
y -= 1 |
| 2202 |
return res |
| 2203 |
def g(x, y): |
| 2204 |
set_param(myjitdriver, 'max_unroll_loops', 5) |
| 2205 |
a1 = f(A(x), y) |
| 2206 |
a2 = f(A(x), y) |
| 2207 |
b1 = f(B(x), y) |
| 2208 |
b2 = f(B(x), y) |
| 2209 |
assert a1.val == a2.val |
| 2210 |
assert b1.val == b2.val |
| 2211 |
return a1.val + b1.val |
| 2212 |
res = self.meta_interp(g, [3, 23]) |
| 2213 |
assert res == 7068153 |
| 2214 |
self.check_trace_count(6) |
| 2215 |
self.check_resops(guard_true=8, guard_class=2, int_mul=3, |
| 2216 |
int_add=3, guard_false=4) |
| 2217 |
|
| 2218 |
def test_dont_trace_every_iteration(self): |
| 2219 |
myjitdriver = JitDriver(greens = [], reds = ['a', 'b', 'i', 'sa']) |
| 2220 |
|
| 2221 |
def main(a, b): |
| 2222 |
i = sa = 0 |
| 2223 |
|
| 2224 |
while i < 200: |
| 2225 |
myjitdriver.can_enter_jit(a=a, b=b, i=i, sa=sa) |
| 2226 |
myjitdriver.jit_merge_point(a=a, b=b, i=i, sa=sa) |
| 2227 |
if a > 0: pass |
| 2228 |
if b < 2: pass |
| 2229 |
sa += a % b |
| 2230 |
i += 1 |
| 2231 |
return sa |
| 2232 |
def g(): |
| 2233 |
return main(10, 20) + main(-10, -20) |
| 2234 |
res = self.meta_interp(g, []) |
| 2235 |
assert res == g() |
| 2236 |
self.check_enter_count(2) |
| 2237 |
|
| 2238 |
def test_current_trace_length(self): |
| 2239 |
myjitdriver = JitDriver(greens = ['g'], reds = ['x', 'l']) |
| 2240 |
@dont_look_inside |
| 2241 |
def residual(): |
| 2242 |
print "hi there" |
| 2243 |
@unroll_safe |
| 2244 |
def loop(g): |
| 2245 |
y = 0 |
| 2246 |
while y < g: |
| 2247 |
residual() |
| 2248 |
y += 1 |
| 2249 |
def f(x, g): |
| 2250 |
l = [] |
| 2251 |
n = 0 |
| 2252 |
while x > 0: |
| 2253 |
myjitdriver.can_enter_jit(x=x, g=g, l=l) |
| 2254 |
myjitdriver.jit_merge_point(x=x, g=g, l=l) |
| 2255 |
loop(g) |
| 2256 |
x -= 1 |
| 2257 |
l.append(current_trace_length()) |
| 2258 |
return l[-2] |
| 2259 |
res = self.meta_interp(f, [5, 8]) |
| 2260 |
assert 14 < res < 42 |
| 2261 |
res = self.meta_interp(f, [5, 2]) |
| 2262 |
assert 4 < res < 14 |
| 2263 |
|
| 2264 |
def test_compute_identity_hash(self): |
| 2265 |
from rpython.rlib.objectmodel import compute_identity_hash |
| 2266 |
class A(object): |
| 2267 |
pass |
| 2268 |
def f(): |
| 2269 |
a = A() |
| 2270 |
return compute_identity_hash(a) == compute_identity_hash(a) |
| 2271 |
res = self.interp_operations(f, []) |
| 2272 |
assert res |
| 2273 |
|
| 2274 |
|
| 2275 |
def test_compute_unique_id(self): |
| 2276 |
from rpython.rlib.objectmodel import compute_unique_id |
| 2277 |
class A(object): |
| 2278 |
pass |
| 2279 |
def f(): |
| 2280 |
a1 = A() |
| 2281 |
a2 = A() |
| 2282 |
return (compute_unique_id(a1) == compute_unique_id(a1) and |
| 2283 |
compute_unique_id(a1) != compute_unique_id(a2)) |
| 2284 |
res = self.interp_operations(f, []) |
| 2285 |
assert res |
| 2286 |
|
| 2287 |
def test_wrap_around_add(self): |
| 2288 |
myjitdriver = JitDriver(greens = [], reds = ['x', 'n']) |
| 2289 |
class A: |
| 2290 |
pass |
| 2291 |
def f(x): |
| 2292 |
n = 0 |
| 2293 |
while x > 0: |
| 2294 |
myjitdriver.can_enter_jit(x=x, n=n) |
| 2295 |
myjitdriver.jit_merge_point(x=x, n=n) |
| 2296 |
x += 1 |
| 2297 |
n += 1 |
| 2298 |
return n |
| 2299 |
res = self.meta_interp(f, [sys.maxint-10]) |
| 2300 |
assert res == 11 |
| 2301 |
self.check_jitcell_token_count(1) |
| 2302 |
|
| 2303 |
def test_wrap_around_mul(self): |
| 2304 |
myjitdriver = JitDriver(greens = [], reds = ['x', 'n']) |
| 2305 |
class A: |
| 2306 |
pass |
| 2307 |
def f(x): |
| 2308 |
n = 0 |
| 2309 |
while x > 0: |
| 2310 |
myjitdriver.can_enter_jit(x=x, n=n) |
| 2311 |
myjitdriver.jit_merge_point(x=x, n=n) |
| 2312 |
x *= 2 |
| 2313 |
n += 1 |
| 2314 |
return n |
| 2315 |
res = self.meta_interp(f, [sys.maxint>>10]) |
| 2316 |
assert res == 11 |
| 2317 |
self.check_jitcell_token_count(1) |
| 2318 |
|
| 2319 |
def test_wrap_around_sub(self): |
| 2320 |
myjitdriver = JitDriver(greens = [], reds = ['x', 'n']) |
| 2321 |
class A: |
| 2322 |
pass |
| 2323 |
def f(x): |
| 2324 |
n = 0 |
| 2325 |
while x < 0: |
| 2326 |
myjitdriver.can_enter_jit(x=x, n=n) |
| 2327 |
myjitdriver.jit_merge_point(x=x, n=n) |
| 2328 |
x -= 1 |
| 2329 |
n += 1 |
| 2330 |
return n |
| 2331 |
res = self.meta_interp(f, [10-sys.maxint]) |
| 2332 |
assert res == 12 |
| 2333 |
self.check_jitcell_token_count(1) |
| 2334 |
|
| 2335 |
def test_caching_setfield(self): |
| 2336 |
myjitdriver = JitDriver(greens = [], reds = ['sa', 'i', 'n', 'a', 'node']) |
| 2337 |
class A: |
| 2338 |
pass |
| 2339 |
def f(n, a): |
| 2340 |
i = sa = 0 |
| 2341 |
node = A() |
| 2342 |
node.val1 = node.val2 = 0 |
| 2343 |
while i < n: |
| 2344 |
myjitdriver.can_enter_jit(sa=sa, i=i, n=n, a=a, node=node) |
| 2345 |
myjitdriver.jit_merge_point(sa=sa, i=i, n=n, a=a, node=node) |
| 2346 |
sa += node.val1 + node.val2 |
| 2347 |
if i < n/2: |
| 2348 |
node.val1 = a |
| 2349 |
node.val2 = a |
| 2350 |
else: |
| 2351 |
node.val1 = a |
| 2352 |
node.val2 = a + 1 |
| 2353 |
i += 1 |
| 2354 |
return sa |
| 2355 |
res = self.meta_interp(f, [32, 7]) |
| 2356 |
assert res == f(32, 7) |
| 2357 |
|
| 2358 |
def test_caching_setarrayitem_fixed(self): |
| 2359 |
myjitdriver = JitDriver(greens = [], reds = ['sa', 'i', 'n', 'a', 'node']) |
| 2360 |
def f(n, a): |
| 2361 |
i = sa = 0 |
| 2362 |
node = [1, 2, 3] |
| 2363 |
while i < n: |
| 2364 |
myjitdriver.can_enter_jit(sa=sa, i=i, n=n, a=a, node=node) |
| 2365 |
myjitdriver.jit_merge_point(sa=sa, i=i, n=n, a=a, node=node) |
| 2366 |
sa += node[0] + node[1] |
| 2367 |
if i < n/2: |
| 2368 |
node[0] = a |
| 2369 |
node[1] = a |
| 2370 |
else: |
| 2371 |
node[0] = a |
| 2372 |
node[1] = a + 1 |
| 2373 |
i += 1 |
| 2374 |
return sa |
| 2375 |
res = self.meta_interp(f, [32, 7]) |
| 2376 |
assert res == f(32, 7) |
| 2377 |
|
| 2378 |
def test_caching_setarrayitem_var(self): |
| 2379 |
myjitdriver = JitDriver(greens = [], reds = ['sa', 'i', 'n', 'a', 'b', 'node']) |
| 2380 |
def f(n, a, b): |
| 2381 |
i = sa = 0 |
| 2382 |
node = [1, 2, 3] |
| 2383 |
while i < n: |
| 2384 |
myjitdriver.can_enter_jit(sa=sa, i=i, n=n, a=a, b=b, node=node) |
| 2385 |
myjitdriver.jit_merge_point(sa=sa, i=i, n=n, a=a, b=b, node=node) |
| 2386 |
sa += node[0] + node[b] |
| 2387 |
if i < n/2: |
| 2388 |
node[0] = a |
| 2389 |
node[b] = a |
| 2390 |
else: |
| 2391 |
node[0] = a |
| 2392 |
node[b] = a + 1 |
| 2393 |
i += 1 |
| 2394 |
return sa |
| 2395 |
res = self.meta_interp(f, [32, 7, 2]) |
| 2396 |
assert res == f(32, 7, 2) |
| 2397 |
|
| 2398 |
def test_getfield_result_with_intbound(self): |
| 2399 |
myjitdriver = JitDriver(greens = [], reds = ['sa', 'i', 'n', 'a', 'node']) |
| 2400 |
class A: |
| 2401 |
pass |
| 2402 |
def f(n, a): |
| 2403 |
i = sa = 0 |
| 2404 |
node = A() |
| 2405 |
node.val1 = a |
| 2406 |
while i < n: |
| 2407 |
myjitdriver.can_enter_jit(sa=sa, i=i, n=n, a=a, node=node) |
| 2408 |
myjitdriver.jit_merge_point(sa=sa, i=i, n=n, a=a, node=node) |
| 2409 |
if node.val1 > 0: |
| 2410 |
sa += 1 |
| 2411 |
if i > n/2: |
| 2412 |
node.val1 = -a |
| 2413 |
i += 1 |
| 2414 |
return sa |
| 2415 |
res = self.meta_interp(f, [32, 7]) |
| 2416 |
assert res == f(32, 7) |
| 2417 |
|
| 2418 |
def test_getfield_result_constant(self): |
| 2419 |
myjitdriver = JitDriver(greens = [], reds = ['sa', 'i', 'n', 'a', 'node']) |
| 2420 |
class A: |
| 2421 |
pass |
| 2422 |
def f(n, a): |
| 2423 |
i = sa = 0 |
| 2424 |
node = A() |
| 2425 |
node.val1 = 7 |
| 2426 |
while i < n: |
| 2427 |
myjitdriver.can_enter_jit(sa=sa, i=i, n=n, a=a, node=node) |
| 2428 |
myjitdriver.jit_merge_point(sa=sa, i=i, n=n, a=a, node=node) |
| 2429 |
if node.val1 == 7: |
| 2430 |
sa += 1 |
| 2431 |
if i > n/2: |
| 2432 |
node.val1 = -7 |
| 2433 |
i += 1 |
| 2434 |
return sa |
| 2435 |
res = self.meta_interp(f, [32, 7]) |
| 2436 |
assert res == f(32, 7) |
| 2437 |
|
| 2438 |
def test_overflowing_shift_pos(self): |
| 2439 |
myjitdriver = JitDriver(greens = [], reds = ['a', 'b', 'n', 'sa']) |
| 2440 |
def f1(a, b): |
| 2441 |
n = sa = 0 |
| 2442 |
while n < 10: |
| 2443 |
myjitdriver.jit_merge_point(a=a, b=b, n=n, sa=sa) |
| 2444 |
if 0 < a <= 5: pass |
| 2445 |
if 0 < b <= 5: pass |
| 2446 |
sa += (((((a << b) << b) << b) >> b) >> b) >> b |
| 2447 |
n += 1 |
| 2448 |
return sa |
| 2449 |
|
| 2450 |
def f2(a, b): |
| 2451 |
n = sa = 0 |
| 2452 |
while n < 10: |
| 2453 |
myjitdriver.jit_merge_point(a=a, b=b, n=n, sa=sa) |
| 2454 |
if 0 < a < promote(sys.maxint/2): pass |
| 2455 |
if 0 < b < 100: pass |
| 2456 |
sa += (((((a << b) << b) << b) >> b) >> b) >> b |
| 2457 |
n += 1 |
| 2458 |
return sa |
| 2459 |
|
| 2460 |
assert self.meta_interp(f1, [5, 5]) == 50 |
| 2461 |
self.check_resops(int_rshift=0) |
| 2462 |
|
| 2463 |
for f in (f1, f2): |
| 2464 |
assert self.meta_interp(f, [5, 6]) == 50 |
| 2465 |
self.check_resops(int_rshift=3) |
| 2466 |
|
| 2467 |
assert self.meta_interp(f, [10, 5]) == 100 |
| 2468 |
self.check_resops(int_rshift=3) |
| 2469 |
|
| 2470 |
assert self.meta_interp(f, [10, 6]) == 100 |
| 2471 |
self.check_resops(int_rshift=3) |
| 2472 |
|
| 2473 |
assert self.meta_interp(f, [5, 31]) == 0 |
| 2474 |
self.check_resops(int_rshift=3) |
| 2475 |
|
| 2476 |
bigval = 1 |
| 2477 |
while is_valid_int(bigval << 3): |
| 2478 |
bigval = bigval << 1 |
| 2479 |
|
| 2480 |
assert self.meta_interp(f, [bigval, 5]) == 0 |
| 2481 |
self.check_resops(int_rshift=3) |
| 2482 |
|
| 2483 |
def test_overflowing_shift_neg(self): |
| 2484 |
myjitdriver = JitDriver(greens = [], reds = ['a', 'b', 'n', 'sa']) |
| 2485 |
def f1(a, b): |
| 2486 |
n = sa = 0 |
| 2487 |
while n < 10: |
| 2488 |
myjitdriver.jit_merge_point(a=a, b=b, n=n, sa=sa) |
| 2489 |
if -5 <= a < 0: pass |
| 2490 |
if 0 < b <= 5: pass |
| 2491 |
sa += (((((a << b) << b) << b) >> b) >> b) >> b |
| 2492 |
n += 1 |
| 2493 |
return sa |
| 2494 |
|
| 2495 |
def f2(a, b): |
| 2496 |
n = sa = 0 |
| 2497 |
while n < 10: |
| 2498 |
myjitdriver.jit_merge_point(a=a, b=b, n=n, sa=sa) |
| 2499 |
if -promote(sys.maxint/2) < a < 0: pass |
| 2500 |
if 0 < b < 100: pass |
| 2501 |
sa += (((((a << b) << b) << b) >> b) >> b) >> b |
| 2502 |
n += 1 |
| 2503 |
return sa |
| 2504 |
|
| 2505 |
assert self.meta_interp(f1, [-5, 5]) == -50 |
| 2506 |
self.check_resops(int_rshift=0) |
| 2507 |
|
| 2508 |
for f in (f1, f2): |
| 2509 |
assert self.meta_interp(f, [-5, 6]) == -50 |
| 2510 |
self.check_resops(int_rshift=3) |
| 2511 |
|
| 2512 |
assert self.meta_interp(f, [-10, 5]) == -100 |
| 2513 |
self.check_resops(int_rshift=3) |
| 2514 |
|
| 2515 |
assert self.meta_interp(f, [-10, 6]) == -100 |
| 2516 |
self.check_resops(int_rshift=3) |
| 2517 |
|
| 2518 |
assert self.meta_interp(f, [-5, 31]) == 0 |
| 2519 |
self.check_resops(int_rshift=3) |
| 2520 |
|
| 2521 |
bigval = 1 |
| 2522 |
while is_valid_int(bigval << 3): |
| 2523 |
bigval = bigval << 1 |
| 2524 |
|
| 2525 |
assert self.meta_interp(f, [bigval, 5]) == 0 |
| 2526 |
self.check_resops(int_rshift=3) |
| 2527 |
|
| 2528 |
def test_pure_op_not_to_be_propagated(self): |
| 2529 |
myjitdriver = JitDriver(greens = [], reds = ['n', 'sa']) |
| 2530 |
def f(n): |
| 2531 |
sa = 0 |
| 2532 |
while n > 0: |
| 2533 |
myjitdriver.jit_merge_point(n=n, sa=sa) |
| 2534 |
sa += n + 1 |
| 2535 |
n -= 1 |
| 2536 |
return sa |
| 2537 |
assert self.meta_interp(f, [10]) == f(10) |
| 2538 |
|
| 2539 |
def test_inputarg_reset_bug(self): |
| 2540 |
|
| 2541 |
|
| 2542 |
|
| 2543 |
|
| 2544 |
|
| 2545 |
|
| 2546 |
|
| 2547 |
|
| 2548 |
|
| 2549 |
|
| 2550 |
|
| 2551 |
|
| 2552 |
|
| 2553 |
|
| 2554 |
|
| 2555 |
|
| 2556 |
|
| 2557 |
def get_printable_location(i): |
| 2558 |
return str(i) |
| 2559 |
|
| 2560 |
myjitdriver = JitDriver(greens = ['i'], reds = ['j', 'c', 'a'], |
| 2561 |
get_printable_location=get_printable_location) |
| 2562 |
bytecode = "0j10jc20a3" |
| 2563 |
def f(): |
| 2564 |
set_param(myjitdriver, 'threshold', 7) |
| 2565 |
set_param(myjitdriver, 'trace_eagerness', 1) |
| 2566 |
i = j = c = a = 1 |
| 2567 |
while True: |
| 2568 |
myjitdriver.jit_merge_point(i=i, j=j, c=c, a=a) |
| 2569 |
if i >= len(bytecode): |
| 2570 |
break |
| 2571 |
op = bytecode[i] |
| 2572 |
i += 1 |
| 2573 |
if op == 'j': |
| 2574 |
j += 1 |
| 2575 |
elif op == 'c': |
| 2576 |
promote(c) |
| 2577 |
c = 1 - c |
| 2578 |
elif op == '2': |
| 2579 |
if j < 3: |
| 2580 |
i -= 3 |
| 2581 |
myjitdriver.can_enter_jit(i=i, j=j, c=c, a=a) |
| 2582 |
elif op == '1': |
| 2583 |
k = j*a |
| 2584 |
if j < 100: |
| 2585 |
i -= 2 |
| 2586 |
a += k |
| 2587 |
myjitdriver.can_enter_jit(i=i, j=j, c=c, a=a) |
| 2588 |
else: |
| 2589 |
a += k*2 |
| 2590 |
elif op == '0': |
| 2591 |
j = c = a = 0 |
| 2592 |
elif op == 'a': |
| 2593 |
j += 1 |
| 2594 |
a += 1 |
| 2595 |
elif op == '3': |
| 2596 |
if a < 100: |
| 2597 |
i -= 2 |
| 2598 |
myjitdriver.can_enter_jit(i=i, j=j, c=c, a=a) |
| 2599 |
|
| 2600 |
else: |
| 2601 |
return ord(op) |
| 2602 |
return 42 |
| 2603 |
assert f() == 42 |
| 2604 |
def g(): |
| 2605 |
res = 1 |
| 2606 |
for i in range(10): |
| 2607 |
res = f() |
| 2608 |
return res |
| 2609 |
res = self.meta_interp(g, []) |
| 2610 |
assert res == 42 |
| 2611 |
|
| 2612 |
def test_read_timestamp(self): |
| 2613 |
import time |
| 2614 |
from rpython.rlib.rtimer import read_timestamp |
| 2615 |
def busy_loop(): |
| 2616 |
start = time.time() |
| 2617 |
while time.time() - start < 0.1: |
| 2618 |
|
| 2619 |
pass |
| 2620 |
|
| 2621 |
def f(): |
| 2622 |
t1 = read_timestamp() |
| 2623 |
busy_loop() |
| 2624 |
t2 = read_timestamp() |
| 2625 |
return t2 - t1 > 1000 |
| 2626 |
res = self.interp_operations(f, []) |
| 2627 |
assert res |
| 2628 |
|
| 2629 |
def test_get_timestamp_unit(self): |
| 2630 |
import time |
| 2631 |
from rpython.rlib import rtimer |
| 2632 |
def f(): |
| 2633 |
return rtimer.get_timestamp_unit() |
| 2634 |
unit = self.interp_operations(f, []) |
| 2635 |
assert unit == rtimer.UNIT_NS |
| 2636 |
|
| 2637 |
def test_bug688_multiple_immutable_fields(self): |
| 2638 |
myjitdriver = JitDriver(greens=[], reds=['counter','context']) |
| 2639 |
|
| 2640 |
class Tag: |
| 2641 |
pass |
| 2642 |
class InnerContext(): |
| 2643 |
_immutable_fields_ = ['variables','local_names'] |
| 2644 |
def __init__(self, variables): |
| 2645 |
self.variables = variables |
| 2646 |
self.local_names = [0] |
| 2647 |
|
| 2648 |
def store(self): |
| 2649 |
self.local_names[0] = 1 |
| 2650 |
|
| 2651 |
def retrieve(self): |
| 2652 |
variables = self.variables |
| 2653 |
promote(variables) |
| 2654 |
result = self.local_names[0] |
| 2655 |
if result == 0: |
| 2656 |
return -1 |
| 2657 |
else: |
| 2658 |
return -1 |
| 2659 |
def build(): |
| 2660 |
context = InnerContext(Tag()) |
| 2661 |
|
| 2662 |
context.store() |
| 2663 |
|
| 2664 |
counter = 0 |
| 2665 |
while True: |
| 2666 |
myjitdriver.jit_merge_point(context=context, counter = counter) |
| 2667 |
context.retrieve() |
| 2668 |
context.retrieve() |
| 2669 |
|
| 2670 |
counter += 1 |
| 2671 |
if counter > 10: |
| 2672 |
return 7 |
| 2673 |
assert self.meta_interp(build, []) == 7 |
| 2674 |
self.check_resops(getfield_gc_r=2) |
| 2675 |
|
| 2676 |
def test_args_becomming_equal(self): |
| 2677 |
myjitdriver = JitDriver(greens = [], reds = ['n', 'i', 'sa', 'a', 'b']) |
| 2678 |
def f(n, a, b): |
| 2679 |
sa = i = 0 |
| 2680 |
while i < n: |
| 2681 |
myjitdriver.jit_merge_point(n=n, i=i, sa=sa, a=a, b=b) |
| 2682 |
sa += a |
| 2683 |
sa *= b |
| 2684 |
if i > n/2: |
| 2685 |
a = b |
| 2686 |
i += 1 |
| 2687 |
return sa |
| 2688 |
assert self.meta_interp(f, [20, 1, 2]) == f(20, 1, 2) |
| 2689 |
|
| 2690 |
def test_args_becomming_equal_boxed1(self): |
| 2691 |
class A(object): |
| 2692 |
def __init__(self, a, b): |
| 2693 |
self.a = a |
| 2694 |
self.b = b |
| 2695 |
myjitdriver = JitDriver(greens = [], reds = ['n', 'i', 'sa', 'a', 'b', 'node']) |
| 2696 |
|
| 2697 |
def f(n, a, b): |
| 2698 |
sa = i = 0 |
| 2699 |
node = A(a,b) |
| 2700 |
while i < n: |
| 2701 |
myjitdriver.jit_merge_point(n=n, i=i, sa=sa, a=a, b=b, node=node) |
| 2702 |
sa += node.a |
| 2703 |
sa *= node.b |
| 2704 |
if i > n/2: |
| 2705 |
node = A(b, b) |
| 2706 |
else: |
| 2707 |
node = A(a, b) |
| 2708 |
i += 1 |
| 2709 |
return sa |
| 2710 |
assert self.meta_interp(f, [20, 1, 2]) == f(20, 1, 2) |
| 2711 |
|
| 2712 |
def test_args_becomming_not_equal_boxed1(self): |
| 2713 |
class A(object): |
| 2714 |
def __init__(self, a, b): |
| 2715 |
self.a = a |
| 2716 |
self.b = b |
| 2717 |
myjitdriver = JitDriver(greens = [], reds = ['n', 'i', 'sa', 'a', 'b', 'node']) |
| 2718 |
|
| 2719 |
def f(n, a, b): |
| 2720 |
sa = i = 0 |
| 2721 |
node = A(b, b) |
| 2722 |
while i < n: |
| 2723 |
myjitdriver.jit_merge_point(n=n, i=i, sa=sa, a=a, b=b, node=node) |
| 2724 |
sa += node.a |
| 2725 |
sa *= node.b |
| 2726 |
if i > n/2: |
| 2727 |
node = A(a, b) |
| 2728 |
else: |
| 2729 |
node = A(b, b) |
| 2730 |
i += 1 |
| 2731 |
return sa |
| 2732 |
assert self.meta_interp(f, [20, 1, 2]) == f(20, 1, 2) |
| 2733 |
|
| 2734 |
def test_args_becomming_equal_boxed2(self): |
| 2735 |
class A(object): |
| 2736 |
def __init__(self, a, b): |
| 2737 |
self.a = a |
| 2738 |
self.b = b |
| 2739 |
myjitdriver = JitDriver(greens = [], reds = ['n', 'i', 'sa', 'node']) |
| 2740 |
|
| 2741 |
def f(n, a, b): |
| 2742 |
sa = i = 0 |
| 2743 |
node = A(a, b) |
| 2744 |
while i < n: |
| 2745 |
myjitdriver.jit_merge_point(n=n, i=i, sa=sa, node=node) |
| 2746 |
sa += node.a |
| 2747 |
sa *= node.b |
| 2748 |
if i > n/2: |
| 2749 |
node = A(node.b, node.b) |
| 2750 |
else: |
| 2751 |
node = A(node.b, node.a) |
| 2752 |
i += 1 |
| 2753 |
return sa |
| 2754 |
assert self.meta_interp(f, [20, 1, 2]) == f(20, 1, 2) |
| 2755 |
|
| 2756 |
def test_inlined_short_preamble_guard_needed_in_loop1(self): |
| 2757 |
class A(object): |
| 2758 |
def __init__(self, a): |
| 2759 |
self.a = a |
| 2760 |
myjitdriver = JitDriver(greens = [], reds = ['n', 'i', 'sa', |
| 2761 |
'a', 'b']) |
| 2762 |
def f(n, a, b): |
| 2763 |
sa = i = 0 |
| 2764 |
while i < n: |
| 2765 |
myjitdriver.jit_merge_point(n=n, i=i, sa=sa, a=a, b=b) |
| 2766 |
if a.a < 10: |
| 2767 |
sa += a.a |
| 2768 |
b.a = i |
| 2769 |
i += 1 |
| 2770 |
return sa |
| 2771 |
def g(n): |
| 2772 |
return f(n, A(5), A(10)) |
| 2773 |
assert self.meta_interp(g, [20]) == g(20) |
| 2774 |
|
| 2775 |
def test_ovf_guard_in_short_preamble2(self): |
| 2776 |
class A(object): |
| 2777 |
def __init__(self, val): |
| 2778 |
self.val = val |
| 2779 |
myjitdriver = JitDriver(greens = [], reds = ['n', 'i', 'sa', 'a', 'node1', 'node2']) |
| 2780 |
def f(n, a): |
| 2781 |
node1 = node2 = A(0) |
| 2782 |
sa = i = 0 |
| 2783 |
while i < n: |
| 2784 |
myjitdriver.jit_merge_point(n=n, i=i, sa=sa, a=a, node1=node1, node2=node2) |
| 2785 |
node2.val = 7 |
| 2786 |
if a >= 100: |
| 2787 |
sa += 1 |
| 2788 |
try: |
| 2789 |
sa += ovfcheck(i + i) |
| 2790 |
except OverflowError: |
| 2791 |
assert 0 |
| 2792 |
node1 = A(i) |
| 2793 |
i += 1 |
| 2794 |
assert self.meta_interp(f, [20, 7]) == f(20, 7) |
| 2795 |
|
| 2796 |
def test_intbounds_generalized(self): |
| 2797 |
myjitdriver = JitDriver(greens = [], reds = ['n', 'i', 'sa']) |
| 2798 |
|
| 2799 |
def f(n): |
| 2800 |
sa = i = 0 |
| 2801 |
while i < n: |
| 2802 |
myjitdriver.jit_merge_point(n=n, i=i, sa=sa) |
| 2803 |
if i > n/2: |
| 2804 |
sa += 1 |
| 2805 |
else: |
| 2806 |
sa += 2 |
| 2807 |
i += 1 |
| 2808 |
return sa |
| 2809 |
assert self.meta_interp(f, [20]) == f(20) |
| 2810 |
self.check_resops(int_lt=4, int_le=0, int_ge=0, int_gt=4) |
| 2811 |
|
| 2812 |
def test_intbounds_not_generalized1(self): |
| 2813 |
myjitdriver = JitDriver(greens = [], reds = ['n', 'i', 'sa']) |
| 2814 |
|
| 2815 |
def f(n): |
| 2816 |
sa = i = 0 |
| 2817 |
while i < n: |
| 2818 |
myjitdriver.jit_merge_point(n=n, i=i, sa=sa) |
| 2819 |
if i > n/2: |
| 2820 |
sa += 1 |
| 2821 |
else: |
| 2822 |
sa += 2 |
| 2823 |
assert -100 < i < 100 |
| 2824 |
i += 1 |
| 2825 |
return sa |
| 2826 |
assert self.meta_interp(f, [20]) == f(20) |
| 2827 |
self.check_resops(int_lt=6, int_le=2, int_ge=4, int_gt=5) |
| 2828 |
|
| 2829 |
|
| 2830 |
def test_intbounds_not_generalized2(self): |
| 2831 |
myjitdriver = JitDriver(greens = [], reds = ['n', 'i', 'sa', 'node']) |
| 2832 |
class A(object): |
| 2833 |
def __init__(self, val): |
| 2834 |
self.val = val |
| 2835 |
def f(n): |
| 2836 |
sa = i = 0 |
| 2837 |
node = A(n) |
| 2838 |
while i < n: |
| 2839 |
myjitdriver.jit_merge_point(n=n, i=i, sa=sa, node=node) |
| 2840 |
if i > n/2: |
| 2841 |
sa += 1 |
| 2842 |
else: |
| 2843 |
sa += 2 |
| 2844 |
assert -100 <= node.val <= 100 |
| 2845 |
i += 1 |
| 2846 |
return sa |
| 2847 |
assert self.meta_interp(f, [20]) == f(20) |
| 2848 |
self.check_resops(int_lt=4, int_le=3, int_ge=3, int_gt=4) |
| 2849 |
|
| 2850 |
def test_retrace_limit1(self): |
| 2851 |
myjitdriver = JitDriver(greens = [], reds = ['n', 'i', 'sa', 'a']) |
| 2852 |
|
| 2853 |
def f(n, limit): |
| 2854 |
set_param(myjitdriver, 'retrace_limit', limit) |
| 2855 |
sa = i = a = 0 |
| 2856 |
while i < n: |
| 2857 |
myjitdriver.jit_merge_point(n=n, i=i, sa=sa, a=a) |
| 2858 |
a = i/4 |
| 2859 |
a = hint(a, promote=True) |
| 2860 |
sa += a |
| 2861 |
i += 1 |
| 2862 |
return sa |
| 2863 |
assert self.meta_interp(f, [20, 2]) == f(20, 2) |
| 2864 |
self.check_jitcell_token_count(1) |
| 2865 |
self.check_target_token_count(4) |
| 2866 |
assert self.meta_interp(f, [20, 3]) == f(20, 3) |
| 2867 |
self.check_jitcell_token_count(1) |
| 2868 |
self.check_target_token_count(5) |
| 2869 |
|
| 2870 |
def test_max_retrace_guards(self): |
| 2871 |
myjitdriver = JitDriver(greens = [], reds = ['n', 'i', 'sa', 'a']) |
| 2872 |
|
| 2873 |
def f(n, limit): |
| 2874 |
set_param(myjitdriver, 'retrace_limit', 3) |
| 2875 |
set_param(myjitdriver, 'max_retrace_guards', limit) |
| 2876 |
sa = i = a = 0 |
| 2877 |
while i < n: |
| 2878 |
myjitdriver.jit_merge_point(n=n, i=i, sa=sa, a=a) |
| 2879 |
a = i/4 |
| 2880 |
a = hint(a, promote=True) |
| 2881 |
sa += a |
| 2882 |
i += 1 |
| 2883 |
return sa |
| 2884 |
assert self.meta_interp(f, [20, 1]) == f(20, 1) |
| 2885 |
self.check_jitcell_token_count(1) |
| 2886 |
self.check_target_token_count(2) |
| 2887 |
assert self.meta_interp(f, [20, 10]) == f(20, 10) |
| 2888 |
self.check_jitcell_token_count(1) |
| 2889 |
self.check_target_token_count(5) |
| 2890 |
|
| 2891 |
def test_max_unroll_loops(self): |
| 2892 |
from rpython.jit.metainterp import optimizeopt |
| 2893 |
myjitdriver = JitDriver(greens = [], reds = ['n', 'i']) |
| 2894 |
|
| 2895 |
def f(n, limit): |
| 2896 |
set_param(myjitdriver, 'threshold', 5) |
| 2897 |
set_param(myjitdriver, 'max_unroll_loops', limit) |
| 2898 |
i = 0 |
| 2899 |
while i < n: |
| 2900 |
myjitdriver.jit_merge_point(n=n, i=i) |
| 2901 |
print i |
| 2902 |
i += 1 |
| 2903 |
return i |
| 2904 |
|
| 2905 |
def my_compile_loop( |
| 2906 |
self, original_boxes, live_arg_boxes, start, use_unroll): |
| 2907 |
return None |
| 2908 |
old_compile_loop = MetaInterp.compile_loop |
| 2909 |
MetaInterp.compile_loop = my_compile_loop |
| 2910 |
try: |
| 2911 |
res = self.meta_interp(f, [23, 4]) |
| 2912 |
assert res == 23 |
| 2913 |
self.check_trace_count(0) |
| 2914 |
self.check_aborted_count(3) |
| 2915 |
|
| 2916 |
res = self.meta_interp(f, [23, 20]) |
| 2917 |
assert res == 23 |
| 2918 |
self.check_trace_count(0) |
| 2919 |
self.check_aborted_count(2) |
| 2920 |
finally: |
| 2921 |
MetaInterp.compile_loop = old_compile_loop |
| 2922 |
|
| 2923 |
def test_max_unroll_loops_retry_without_unroll(self): |
| 2924 |
if not self.basic: |
| 2925 |
py.test.skip("unrolling") |
| 2926 |
myjitdriver = JitDriver(greens = [], reds = ['n', 'i']) |
| 2927 |
|
| 2928 |
def f(n, limit): |
| 2929 |
set_param(myjitdriver, 'threshold', 5) |
| 2930 |
set_param(myjitdriver, 'max_unroll_loops', limit) |
| 2931 |
i = 0 |
| 2932 |
while i < n: |
| 2933 |
myjitdriver.jit_merge_point(n=n, i=i) |
| 2934 |
print i |
| 2935 |
i += 1 |
| 2936 |
return i |
| 2937 |
|
| 2938 |
seen = [] |
| 2939 |
def my_compile_loop( |
| 2940 |
self, original_boxes, live_arg_boxes, start, use_unroll): |
| 2941 |
seen.append(use_unroll) |
| 2942 |
return None |
| 2943 |
old_compile_loop = MetaInterp.compile_loop |
| 2944 |
MetaInterp.compile_loop = my_compile_loop |
| 2945 |
try: |
| 2946 |
res = self.meta_interp(f, [23, 4]) |
| 2947 |
assert res == 23 |
| 2948 |
assert False in seen |
| 2949 |
assert True in seen |
| 2950 |
finally: |
| 2951 |
MetaInterp.compile_loop = old_compile_loop |
| 2952 |
|
| 2953 |
def test_retrace_limit_with_extra_guards(self): |
| 2954 |
myjitdriver = JitDriver(greens = [], reds = ['n', 'i', 'sa', 'a', |
| 2955 |
'node']) |
| 2956 |
def f(n, limit): |
| 2957 |
set_param(myjitdriver, 'retrace_limit', limit) |
| 2958 |
sa = i = a = 0 |
| 2959 |
node = [1, 2, 3] |
| 2960 |
node[1] = n |
| 2961 |
while i < n: |
| 2962 |
myjitdriver.jit_merge_point(n=n, i=i, sa=sa, a=a, node=node) |
| 2963 |
a = i/4 |
| 2964 |
a = hint(a, promote=True) |
| 2965 |
if i&1 == 0: |
| 2966 |
sa += node[i%3] |
| 2967 |
sa += a |
| 2968 |
i += 1 |
| 2969 |
return sa |
| 2970 |
assert self.meta_interp(f, [20, 2]) == f(20, 2) |
| 2971 |
self.check_jitcell_token_count(1) |
| 2972 |
self.check_target_token_count(4) |
| 2973 |
assert self.meta_interp(f, [20, 3]) == f(20, 3) |
| 2974 |
self.check_jitcell_token_count(1) |
| 2975 |
self.check_target_token_count(5) |
| 2976 |
|
| 2977 |
def test_retrace_ending_up_retracing_another_loop(self): |
| 2978 |
|
| 2979 |
myjitdriver = JitDriver(greens = ['pc'], reds = ['n', 'i', 'sa']) |
| 2980 |
bytecode = "0+sI0+SI" |
| 2981 |
def f(n): |
| 2982 |
set_param(None, 'threshold', 3) |
| 2983 |
set_param(None, 'trace_eagerness', 1) |
| 2984 |
set_param(None, 'retrace_limit', 5) |
| 2985 |
set_param(None, 'function_threshold', -1) |
| 2986 |
pc = sa = i = 0 |
| 2987 |
while pc < len(bytecode): |
| 2988 |
myjitdriver.jit_merge_point(pc=pc, n=n, sa=sa, i=i) |
| 2989 |
n = hint(n, promote=True) |
| 2990 |
op = bytecode[pc] |
| 2991 |
if op == '0': |
| 2992 |
i = 0 |
| 2993 |
elif op == '+': |
| 2994 |
i += 1 |
| 2995 |
elif op == 's': |
| 2996 |
sa += i |
| 2997 |
elif op == 'S': |
| 2998 |
sa += 2 |
| 2999 |
elif op == 'I': |
| 3000 |
if i < n: |
| 3001 |
pc -= 2 |
| 3002 |
myjitdriver.can_enter_jit(pc=pc, n=n, sa=sa, i=i) |
| 3003 |
continue |
| 3004 |
pc += 1 |
| 3005 |
return sa |
| 3006 |
|
| 3007 |
def g(n1, n2): |
| 3008 |
for i in range(10): |
| 3009 |
f(n1) |
| 3010 |
for i in range(10): |
| 3011 |
f(n2) |
| 3012 |
|
| 3013 |
nn = [10, 3] |
| 3014 |
assert self.meta_interp(g, nn) == g(*nn) |
| 3015 |
|
| 3016 |
|
| 3017 |
|
| 3018 |
|
| 3019 |
|
| 3020 |
|
| 3021 |
self.check_trace_count(8) |
| 3022 |
|
| 3023 |
|
| 3024 |
|
| 3025 |
def g(n): |
| 3026 |
for i in range(n): |
| 3027 |
for j in range(10): |
| 3028 |
f(n-i) |
| 3029 |
|
| 3030 |
res = self.meta_interp(g, [10]) |
| 3031 |
assert res == g(10) |
| 3032 |
|
| 3033 |
self.check_jitcell_token_count(2) |
| 3034 |
if 0: |
| 3035 |
for cell in get_stats().get_all_jitcell_tokens(): |
| 3036 |
|
| 3037 |
assert len(cell.target_tokens) <= 7 |
| 3038 |
|
| 3039 |
def test_nested_retrace(self): |
| 3040 |
|
| 3041 |
myjitdriver = JitDriver(greens = ['pc'], reds = ['n', 'a', 'i', 'j', 'sa']) |
| 3042 |
bytecode = "ij+Jj+JI" |
| 3043 |
def f(n, a): |
| 3044 |
set_param(None, 'threshold', 5) |
| 3045 |
set_param(None, 'trace_eagerness', 1) |
| 3046 |
set_param(None, 'retrace_limit', 2) |
| 3047 |
pc = sa = i = j = 0 |
| 3048 |
while pc < len(bytecode): |
| 3049 |
myjitdriver.jit_merge_point(pc=pc, n=n, sa=sa, i=i, j=j, a=a) |
| 3050 |
a = hint(a, promote=True) |
| 3051 |
op = bytecode[pc] |
| 3052 |
if op == 'i': |
| 3053 |
i = 0 |
| 3054 |
elif op == 'j': |
| 3055 |
j = 0 |
| 3056 |
elif op == '+': |
| 3057 |
sa += a |
| 3058 |
elif op == 'J': |
| 3059 |
j += 1 |
| 3060 |
if j < 3: |
| 3061 |
pc -= 1 |
| 3062 |
myjitdriver.can_enter_jit(pc=pc, n=n, sa=sa, i=i, j=j, a=a) |
| 3063 |
continue |
| 3064 |
elif op == 'I': |
| 3065 |
i += 1 |
| 3066 |
if i < n: |
| 3067 |
pc -= 6 |
| 3068 |
myjitdriver.can_enter_jit(pc=pc, n=n, sa=sa, i=i, j=j, a=a) |
| 3069 |
continue |
| 3070 |
pc += 1 |
| 3071 |
return sa |
| 3072 |
|
| 3073 |
res = self.meta_interp(f, [10, 7]) |
| 3074 |
assert res == f(10, 7) |
| 3075 |
self.check_jitcell_token_count(2) |
| 3076 |
if self.basic: |
| 3077 |
for cell in get_stats().get_all_jitcell_tokens(): |
| 3078 |
assert len(cell.target_tokens) == 2 |
| 3079 |
|
| 3080 |
def g(n): |
| 3081 |
return f(n, 2) + f(n, 3) |
| 3082 |
|
| 3083 |
res = self.meta_interp(g, [10]) |
| 3084 |
assert res == g(10) |
| 3085 |
self.check_jitcell_token_count(2) |
| 3086 |
if self.basic: |
| 3087 |
for cell in get_stats().get_all_jitcell_tokens(): |
| 3088 |
assert len(cell.target_tokens) <= 3 |
| 3089 |
|
| 3090 |
def g(n): |
| 3091 |
return f(n, 2) + f(n, 3) + f(n, 4) + f(n, 5) + f(n, 6) + f(n, 7) |
| 3092 |
|
| 3093 |
res = self.meta_interp(g, [10]) |
| 3094 |
assert res == g(10) |
| 3095 |
|
| 3096 |
self.check_jitcell_token_count(3) |
| 3097 |
cnt = 0 |
| 3098 |
if self.basic: |
| 3099 |
for cell in get_stats().get_all_jitcell_tokens(): |
| 3100 |
if cell.target_tokens is None: |
| 3101 |
cnt += 1 |
| 3102 |
else: |
| 3103 |
assert len(cell.target_tokens) <= 4 |
| 3104 |
assert cnt == 1 |
| 3105 |
|
| 3106 |
def test_frame_finished_during_retrace(self): |
| 3107 |
class Base(object): |
| 3108 |
pass |
| 3109 |
class A(Base): |
| 3110 |
def __init__(self, a): |
| 3111 |
self.val = a |
| 3112 |
self.num = 1 |
| 3113 |
def inc(self): |
| 3114 |
return A(self.val + 1) |
| 3115 |
class B(Base): |
| 3116 |
def __init__(self, a): |
| 3117 |
self.val = a |
| 3118 |
self.num = 1000 |
| 3119 |
def inc(self): |
| 3120 |
return B(self.val + 1) |
| 3121 |
myjitdriver = JitDriver(greens = [], reds = ['sa', 'a']) |
| 3122 |
def f(): |
| 3123 |
set_param(None, 'threshold', 3) |
| 3124 |
set_param(None, 'trace_eagerness', 2) |
| 3125 |
a = A(0) |
| 3126 |
sa = 0 |
| 3127 |
while a.val < 8: |
| 3128 |
myjitdriver.jit_merge_point(a=a, sa=sa) |
| 3129 |
a = a.inc() |
| 3130 |
if a.val > 4: |
| 3131 |
a = B(a.val) |
| 3132 |
sa += a.num |
| 3133 |
return sa |
| 3134 |
res = self.meta_interp(f, []) |
| 3135 |
assert res == f() |
| 3136 |
|
| 3137 |
def test_frame_finished_during_continued_retrace(self): |
| 3138 |
class Base(object): |
| 3139 |
pass |
| 3140 |
class A(Base): |
| 3141 |
def __init__(self, a): |
| 3142 |
self.val = a |
| 3143 |
self.num = 100 |
| 3144 |
def inc(self): |
| 3145 |
return A(self.val + 1) |
| 3146 |
class B(Base): |
| 3147 |
def __init__(self, a): |
| 3148 |
self.val = a |
| 3149 |
self.num = 10000 |
| 3150 |
def inc(self): |
| 3151 |
return B(self.val + 1) |
| 3152 |
myjitdriver = JitDriver(greens = [], reds = ['sa', 'b', 'a']) |
| 3153 |
def f(b): |
| 3154 |
set_param(None, 'threshold', 6) |
| 3155 |
set_param(None, 'trace_eagerness', 4) |
| 3156 |
a = A(0) |
| 3157 |
sa = 0 |
| 3158 |
while a.val < 15: |
| 3159 |
myjitdriver.jit_merge_point(a=a, b=b, sa=sa) |
| 3160 |
a = a.inc() |
| 3161 |
if a.val > 8: |
| 3162 |
a = B(a.val) |
| 3163 |
if b == 1: |
| 3164 |
b = 2 |
| 3165 |
else: |
| 3166 |
b = 1 |
| 3167 |
sa += a.num + b |
| 3168 |
return sa |
| 3169 |
res = self.meta_interp(f, [1]) |
| 3170 |
assert res == f(1) |
| 3171 |
|
| 3172 |
def test_remove_array_operations(self): |
| 3173 |
myjitdriver = JitDriver(greens = [], reds = ['a']) |
| 3174 |
class W_Int: |
| 3175 |
def __init__(self, intvalue): |
| 3176 |
self.intvalue = intvalue |
| 3177 |
def f(x): |
| 3178 |
a = [W_Int(x)] |
| 3179 |
while a[0].intvalue > 0: |
| 3180 |
myjitdriver.jit_merge_point(a=a) |
| 3181 |
a[0] = W_Int(a[0].intvalue - 3) |
| 3182 |
return a[0].intvalue |
| 3183 |
res = self.meta_interp(f, [100]) |
| 3184 |
assert res == -2 |
| 3185 |
self.check_resops(setarrayitem_gc=2, getarrayitem_gc_r=1) |
| 3186 |
|
| 3187 |
def test_continue_tracing_with_boxes_in_start_snapshot_replaced_by_optimizer(self): |
| 3188 |
myjitdriver = JitDriver(greens = [], reds = ['sa', 'n', 'a', 'b']) |
| 3189 |
def f(n): |
| 3190 |
sa = a = 0 |
| 3191 |
b = 10 |
| 3192 |
while n: |
| 3193 |
myjitdriver.jit_merge_point(sa=sa, n=n, a=a, b=b) |
| 3194 |
sa += b |
| 3195 |
b += 1 |
| 3196 |
if b > 7: |
| 3197 |
pass |
| 3198 |
if a == 0: |
| 3199 |
a = 1 |
| 3200 |
elif a == 1: |
| 3201 |
a = 2 |
| 3202 |
elif a == 2: |
| 3203 |
a = 0 |
| 3204 |
sa += a |
| 3205 |
sa += 0 |
| 3206 |
n -= 1 |
| 3207 |
return sa |
| 3208 |
res = self.meta_interp(f, [16]) |
| 3209 |
assert res == f(16) |
| 3210 |
|
| 3211 |
def test_loopinvariant_array_shrinking1(self): |
| 3212 |
myjitdriver = JitDriver(greens = [], reds = ['sa', 'n', 'i', 'a']) |
| 3213 |
def f(n): |
| 3214 |
sa = i = 0 |
| 3215 |
a = [0, 1, 2, 3, 4] |
| 3216 |
while i < n: |
| 3217 |
myjitdriver.jit_merge_point(sa=sa, n=n, a=a, i=i) |
| 3218 |
if i < n / 2: |
| 3219 |
sa += a[4] |
| 3220 |
elif i == n / 2: |
| 3221 |
a.pop() |
| 3222 |
i += 1 |
| 3223 |
res = self.meta_interp(f, [32]) |
| 3224 |
assert res == f(32) |
| 3225 |
self.check_resops(arraylen_gc=3) |
| 3226 |
|
| 3227 |
def test_ulonglong_mod(self): |
| 3228 |
myjitdriver = JitDriver(greens = [], reds = ['n', 'a']) |
| 3229 |
class A: |
| 3230 |
pass |
| 3231 |
def f(n): |
| 3232 |
sa = i = rffi.cast(rffi.ULONGLONG, 1) |
| 3233 |
a = A() |
| 3234 |
while i < rffi.cast(rffi.ULONGLONG, n): |
| 3235 |
a.sa = sa |
| 3236 |
a.i = i |
| 3237 |
myjitdriver.jit_merge_point(n=n, a=a) |
| 3238 |
sa = a.sa |
| 3239 |
i = a.i |
| 3240 |
sa += sa % i |
| 3241 |
i += 1 |
| 3242 |
res = self.meta_interp(f, [32]) |
| 3243 |
assert res == f(32) |
| 3244 |
|
| 3245 |
def test_int_signext(self): |
| 3246 |
def f(n): |
| 3247 |
return rffi.cast(rffi.SIGNEDCHAR, n) |
| 3248 |
def f1(n): |
| 3249 |
return rffi.cast(rffi.SIGNEDCHAR, n + 1) |
| 3250 |
res = self.interp_operations(f, [128]) |
| 3251 |
assert res == -128 |
| 3252 |
res = self.interp_operations(f1, [127]) |
| 3253 |
assert res == -128 |
| 3254 |
res = self.interp_operations(f, [-35 + 256 * 29]) |
| 3255 |
assert res == -35 |
| 3256 |
res = self.interp_operations(f, [127 - 256 * 29]) |
| 3257 |
assert res == 127 |
| 3258 |
|
| 3259 |
def test_bug_inline_short_preamble_can_be_inconsistent_in_optimizeopt(self): |
| 3260 |
myjitdriver = JitDriver(greens = [], reds = "auto") |
| 3261 |
class Str(object): |
| 3262 |
_immutable_fields_ = ['s'] |
| 3263 |
def __init__(self, s): |
| 3264 |
self.s = s |
| 3265 |
|
| 3266 |
empty = Str("") |
| 3267 |
space = Str(" ") |
| 3268 |
|
| 3269 |
def f(a, b): |
| 3270 |
line = " " * a + " a" * b |
| 3271 |
token = "" |
| 3272 |
res = [] |
| 3273 |
index = 0 |
| 3274 |
while True: |
| 3275 |
myjitdriver.jit_merge_point() |
| 3276 |
if index >= len(line): |
| 3277 |
break |
| 3278 |
char = line[index] |
| 3279 |
index += 1 |
| 3280 |
if char == space.s: |
| 3281 |
if token != empty.s: |
| 3282 |
res.append(token) |
| 3283 |
token = empty.s |
| 3284 |
else: |
| 3285 |
token += char |
| 3286 |
return len(res) |
| 3287 |
args = [50, 50] |
| 3288 |
res = self.meta_interp(f, args) |
| 3289 |
assert res == f(*args) |
| 3290 |
|
| 3291 |
def test_tail_recursion_elimination_tracing(self): |
| 3292 |
myjitdriver = JitDriver(greens=[], reds='auto') |
| 3293 |
def g6(i): |
| 3294 |
return g5(i) |
| 3295 |
def g5(i): |
| 3296 |
return g4(i) |
| 3297 |
def g4(i): |
| 3298 |
return g3(i) |
| 3299 |
def g3(i): |
| 3300 |
return g2(i) |
| 3301 |
def g2(i): |
| 3302 |
return g1(i) |
| 3303 |
def g1(i): |
| 3304 |
if i & 1: |
| 3305 |
return i + 1 |
| 3306 |
return i + 3 |
| 3307 |
def g(i): |
| 3308 |
x = i |
| 3309 |
while x < 100: |
| 3310 |
myjitdriver.jit_merge_point() |
| 3311 |
x = g6(x) |
| 3312 |
return x |
| 3313 |
res = self.meta_interp(g, [4]) |
| 3314 |
|
| 3315 |
assert len(get_stats().loops[0].operations[3]._descr.rd_numb.code) == 14 |
| 3316 |
|
| 3317 |
def test_tail_recursion_elimination_tracing_void(self): |
| 3318 |
myjitdriver = JitDriver(greens=[], reds='auto') |
| 3319 |
def g6(): |
| 3320 |
return g5() |
| 3321 |
def g5(): |
| 3322 |
return g4() |
| 3323 |
def g4(): |
| 3324 |
return g3() |
| 3325 |
def g3(): |
| 3326 |
return g2() |
| 3327 |
def g2(): |
| 3328 |
return g1() |
| 3329 |
def g1(): |
| 3330 |
if a.x & 1: |
| 3331 |
a.x += 1 |
| 3332 |
else: |
| 3333 |
a.x += 3 |
| 3334 |
class A: |
| 3335 |
pass |
| 3336 |
a = A() |
| 3337 |
a.x = 12 |
| 3338 |
def g(i): |
| 3339 |
a.x = i |
| 3340 |
while a.x < 100: |
| 3341 |
myjitdriver.jit_merge_point() |
| 3342 |
g6() |
| 3343 |
return a.x |
| 3344 |
res = self.meta_interp(g, [4]) |
| 3345 |
assert res == g(4) |
| 3346 |
|
| 3347 |
assert len(get_stats().loops[0].operations[4]._descr.rd_numb.code) == 12 |
| 3348 |
|
| 3349 |
def test_uint_mul_high(self): |
| 3350 |
from rpython.rlib.rarithmetic import uint_mul_high, intmask, r_uint |
| 3351 |
def f(x, y): |
| 3352 |
return intmask(uint_mul_high(r_uint(x), r_uint(y))) |
| 3353 |
res = self.interp_operations(f, [40, 2]) |
| 3354 |
assert res == 0 |
| 3355 |
self.check_operations_history(uint_mul_high=1) |
| 3356 |
|
| 3357 |
|
| 3358 |
class BaseLLtypeTests(BasicTests): |
| 3359 |
|
| 3360 |
def test_identityhash(self): |
| 3361 |
A = lltype.GcStruct("A") |
| 3362 |
def f(): |
| 3363 |
obj1 = lltype.malloc(A) |
| 3364 |
obj2 = lltype.malloc(A) |
| 3365 |
return lltype.identityhash(obj1) == lltype.identityhash(obj2) |
| 3366 |
assert not f() |
| 3367 |
res = self.interp_operations(f, []) |
| 3368 |
assert not res |
| 3369 |
|
| 3370 |
def test_oops_on_nongc(self): |
| 3371 |
from rpython.rtyper.lltypesystem import lltype |
| 3372 |
|
| 3373 |
TP = lltype.Struct('x') |
| 3374 |
def f(i1, i2): |
| 3375 |
p1 = prebuilt[i1] |
| 3376 |
p2 = prebuilt[i2] |
| 3377 |
a = p1 is p2 |
| 3378 |
b = p1 is not p2 |
| 3379 |
c = bool(p1) |
| 3380 |
d = not bool(p2) |
| 3381 |
return 1000*a + 100*b + 10*c + d |
| 3382 |
prebuilt = [lltype.malloc(TP, flavor='raw', immortal=True)] * 2 |
| 3383 |
expected = f(0, 1) |
| 3384 |
assert self.interp_operations(f, [0, 1]) == expected |
| 3385 |
|
| 3386 |
def test_casts(self): |
| 3387 |
py.test.skip("xxx fix or kill") |
| 3388 |
if not self.basic: |
| 3389 |
py.test.skip("test written in a style that " |
| 3390 |
"means it's frontend only") |
| 3391 |
from rpython.rtyper.lltypesystem import lltype, llmemory, rffi |
| 3392 |
|
| 3393 |
TP = lltype.GcStruct('S1') |
| 3394 |
def f(p): |
| 3395 |
n = lltype.cast_ptr_to_int(p) |
| 3396 |
return n |
| 3397 |
x = lltype.malloc(TP) |
| 3398 |
xref = lltype.cast_opaque_ptr(llmemory.GCREF, x) |
| 3399 |
res = self.interp_operations(f, [xref]) |
| 3400 |
y = llmemory.cast_ptr_to_adr(x) |
| 3401 |
y = llmemory.cast_adr_to_int(y) |
| 3402 |
assert rffi.get_real_int(res) == rffi.get_real_int(y) |
| 3403 |
|
| 3404 |
TP = lltype.Struct('S2') |
| 3405 |
prebuilt = [lltype.malloc(TP, immortal=True), |
| 3406 |
lltype.malloc(TP, immortal=True)] |
| 3407 |
def f(x): |
| 3408 |
p = prebuilt[x] |
| 3409 |
n = lltype.cast_ptr_to_int(p) |
| 3410 |
return n |
| 3411 |
res = self.interp_operations(f, [1]) |
| 3412 |
y = llmemory.cast_ptr_to_adr(prebuilt[1]) |
| 3413 |
y = llmemory.cast_adr_to_int(y) |
| 3414 |
assert rffi.get_real_int(res) == rffi.get_real_int(y) |
| 3415 |
|
| 3416 |
def test_collapsing_ptr_eq(self): |
| 3417 |
S = lltype.GcStruct('S') |
| 3418 |
p = lltype.malloc(S) |
| 3419 |
driver = JitDriver(greens = [], reds = ['n', 'x']) |
| 3420 |
|
| 3421 |
def f(n, x): |
| 3422 |
while n > 0: |
| 3423 |
driver.can_enter_jit(n=n, x=x) |
| 3424 |
driver.jit_merge_point(n=n, x=x) |
| 3425 |
if x: |
| 3426 |
n -= 1 |
| 3427 |
n -= 1 |
| 3428 |
|
| 3429 |
def main(): |
| 3430 |
f(10, p) |
| 3431 |
f(10, lltype.nullptr(S)) |
| 3432 |
|
| 3433 |
self.meta_interp(main, []) |
| 3434 |
|
| 3435 |
def test_enable_opts(self): |
| 3436 |
jitdriver = JitDriver(greens = [], reds = ['a']) |
| 3437 |
|
| 3438 |
class A(object): |
| 3439 |
def __init__(self, i): |
| 3440 |
self.i = i |
| 3441 |
|
| 3442 |
def f(): |
| 3443 |
a = A(0) |
| 3444 |
|
| 3445 |
while a.i < 10: |
| 3446 |
jitdriver.jit_merge_point(a=a) |
| 3447 |
jitdriver.can_enter_jit(a=a) |
| 3448 |
a = A(a.i + 1) |
| 3449 |
|
| 3450 |
self.meta_interp(f, []) |
| 3451 |
self.check_resops(new_with_vtable=0) |
| 3452 |
self.meta_interp(f, [], enable_opts='') |
| 3453 |
self.check_resops(new_with_vtable=1) |
| 3454 |
|
| 3455 |
def test_two_loopinvariant_arrays1(self): |
| 3456 |
from rpython.rtyper.lltypesystem import lltype, llmemory, rffi |
| 3457 |
myjitdriver = JitDriver(greens = [], reds = ['sa', 'n', 'i', 'a']) |
| 3458 |
TP = lltype.GcArray(lltype.Signed) |
| 3459 |
def f(n): |
| 3460 |
sa = i = 0 |
| 3461 |
a = lltype.malloc(TP, 5) |
| 3462 |
a[4] = 7 |
| 3463 |
while i < n: |
| 3464 |
myjitdriver.jit_merge_point(sa=sa, n=n, a=a, i=i) |
| 3465 |
if i < n/2: |
| 3466 |
sa += a[4] |
| 3467 |
if i == n/2: |
| 3468 |
a = lltype.malloc(TP, 3) |
| 3469 |
i += 1 |
| 3470 |
return sa |
| 3471 |
res = self.meta_interp(f, [32]) |
| 3472 |
assert res == f(32) |
| 3473 |
self.check_trace_count(2) |
| 3474 |
|
| 3475 |
def test_two_loopinvariant_arrays2(self): |
| 3476 |
from rpython.rtyper.lltypesystem import lltype, llmemory, rffi |
| 3477 |
myjitdriver = JitDriver(greens = [], reds = ['sa', 'n', 'i', 'a']) |
| 3478 |
TP = lltype.GcArray(lltype.Signed) |
| 3479 |
def f(n): |
| 3480 |
sa = i = 0 |
| 3481 |
a = lltype.malloc(TP, 5) |
| 3482 |
a[4] = 7 |
| 3483 |
while i < n: |
| 3484 |
myjitdriver.jit_merge_point(sa=sa, n=n, a=a, i=i) |
| 3485 |
if i < n/2: |
| 3486 |
sa += a[4] |
| 3487 |
elif i > n/2: |
| 3488 |
sa += a[2] |
| 3489 |
if i == n/2: |
| 3490 |
a = lltype.malloc(TP, 3) |
| 3491 |
a[2] = 42 |
| 3492 |
i += 1 |
| 3493 |
return sa |
| 3494 |
res = self.meta_interp(f, [32]) |
| 3495 |
assert res == f(32) |
| 3496 |
self.check_trace_count(2) |
| 3497 |
|
| 3498 |
def test_two_loopinvariant_arrays3(self): |
| 3499 |
from rpython.rtyper.lltypesystem import lltype, llmemory, rffi |
| 3500 |
myjitdriver = JitDriver(greens = [], reds = ['sa', 'n', 'i', 'a']) |
| 3501 |
TP = lltype.GcArray(lltype.Signed) |
| 3502 |
def f(n): |
| 3503 |
sa = i = 0 |
| 3504 |
a = lltype.malloc(TP, 5) |
| 3505 |
a[2] = 7 |
| 3506 |
while i < n: |
| 3507 |
myjitdriver.jit_merge_point(sa=sa, n=n, a=a, i=i) |
| 3508 |
if i < n/2: |
| 3509 |
sa += a[2] |
| 3510 |
elif i > n/2: |
| 3511 |
sa += a[3] |
| 3512 |
if i == n/2: |
| 3513 |
a = lltype.malloc(TP, 7) |
| 3514 |
a[3] = 10 |
| 3515 |
a[2] = 42 |
| 3516 |
i += 1 |
| 3517 |
return sa |
| 3518 |
res = self.meta_interp(f, [32]) |
| 3519 |
assert res == f(32) |
| 3520 |
self.check_trace_count(3) |
| 3521 |
|
| 3522 |
def test_two_loopinvariant_arrays_boxed(self): |
| 3523 |
class A(object): |
| 3524 |
def __init__(self, a): |
| 3525 |
self.a = a |
| 3526 |
from rpython.rtyper.lltypesystem import lltype, llmemory, rffi |
| 3527 |
myjitdriver = JitDriver(greens = [], reds = ['sa', 'n', 'i', 'a']) |
| 3528 |
TP = lltype.GcArray(lltype.Signed) |
| 3529 |
a1 = A(lltype.malloc(TP, 5)) |
| 3530 |
a2 = A(lltype.malloc(TP, 3)) |
| 3531 |
def f(n): |
| 3532 |
sa = i = 0 |
| 3533 |
a = a1 |
| 3534 |
a.a[4] = 7 |
| 3535 |
while i < n: |
| 3536 |
myjitdriver.jit_merge_point(sa=sa, n=n, a=a, i=i) |
| 3537 |
if i < n/2: |
| 3538 |
sa += a.a[4] |
| 3539 |
if i == n/2: |
| 3540 |
a = a2 |
| 3541 |
i += 1 |
| 3542 |
return sa |
| 3543 |
res = self.meta_interp(f, [32]) |
| 3544 |
assert res == f(32) |
| 3545 |
self.check_resops(arraylen_gc=2) |
| 3546 |
|
| 3547 |
def test_release_gil_flush_heap_cache(self): |
| 3548 |
T = rffi.CArrayPtr(rffi.TIME_T) |
| 3549 |
|
| 3550 |
external = rffi.llexternal("time", [T], rffi.TIME_T, releasegil=True) |
| 3551 |
|
| 3552 |
|
| 3553 |
class Lock(object): |
| 3554 |
@dont_look_inside |
| 3555 |
def acquire(self): |
| 3556 |
external(lltype.nullptr(T.TO)) |
| 3557 |
@dont_look_inside |
| 3558 |
def release(self): |
| 3559 |
external(lltype.nullptr(T.TO)) |
| 3560 |
class X(object): |
| 3561 |
def __init__(self, idx): |
| 3562 |
self.field = idx |
| 3563 |
@dont_look_inside |
| 3564 |
def get_obj(z): |
| 3565 |
return X(z) |
| 3566 |
myjitdriver = JitDriver(greens=[], reds=["n", "l", "z", "lock"]) |
| 3567 |
def f(n, z): |
| 3568 |
lock = Lock() |
| 3569 |
l = 0 |
| 3570 |
while n > 0: |
| 3571 |
myjitdriver.jit_merge_point(lock=lock, l=l, n=n, z=z) |
| 3572 |
x = get_obj(z) |
| 3573 |
l += x.field |
| 3574 |
lock.acquire() |
| 3575 |
|
| 3576 |
n -= x.field |
| 3577 |
lock.release() |
| 3578 |
return n |
| 3579 |
res = self.meta_interp(f, [10, 1]) |
| 3580 |
self.check_resops(getfield_gc_i=4) |
| 3581 |
assert res == f(10, 1) |
| 3582 |
|
| 3583 |
def test_jit_merge_point_with_raw_pointer(self): |
| 3584 |
driver = JitDriver(greens = [], reds = ['n', 'x']) |
| 3585 |
|
| 3586 |
TP = lltype.Array(lltype.Signed, hints={'nolength': True}) |
| 3587 |
|
| 3588 |
def f(n): |
| 3589 |
x = lltype.malloc(TP, 10, flavor='raw') |
| 3590 |
x[0] = 1 |
| 3591 |
while n > 0: |
| 3592 |
driver.jit_merge_point(n=n, x=x) |
| 3593 |
n -= x[0] |
| 3594 |
lltype.free(x, flavor='raw') |
| 3595 |
return n |
| 3596 |
|
| 3597 |
self.meta_interp(f, [10], repeat=3) |
| 3598 |
|
| 3599 |
def test_jit_merge_point_with_pbc(self): |
| 3600 |
driver = JitDriver(greens = [], reds = ['x']) |
| 3601 |
|
| 3602 |
class A(object): |
| 3603 |
def __init__(self, x): |
| 3604 |
self.x = x |
| 3605 |
def _freeze_(self): |
| 3606 |
return True |
| 3607 |
pbc = A(1) |
| 3608 |
|
| 3609 |
def main(x): |
| 3610 |
return f(x, pbc) |
| 3611 |
|
| 3612 |
def f(x, pbc): |
| 3613 |
while x > 0: |
| 3614 |
driver.jit_merge_point(x = x) |
| 3615 |
x -= pbc.x |
| 3616 |
return x |
| 3617 |
|
| 3618 |
self.meta_interp(main, [10]) |
| 3619 |
|
| 3620 |
def test_look_inside_iff_const(self): |
| 3621 |
@look_inside_iff(lambda arg: isconstant(arg)) |
| 3622 |
def f(arg): |
| 3623 |
s = 0 |
| 3624 |
while arg > 0: |
| 3625 |
s += arg |
| 3626 |
arg -= 1 |
| 3627 |
return s |
| 3628 |
|
| 3629 |
driver = JitDriver(greens = ['code'], reds = ['n', 'arg', 's']) |
| 3630 |
|
| 3631 |
def main(code, n, arg): |
| 3632 |
s = 0 |
| 3633 |
while n > 0: |
| 3634 |
driver.jit_merge_point(code=code, n=n, arg=arg, s=s) |
| 3635 |
if code == 0: |
| 3636 |
s += f(arg) |
| 3637 |
else: |
| 3638 |
s += f(1) |
| 3639 |
n -= 1 |
| 3640 |
return s |
| 3641 |
|
| 3642 |
res = self.meta_interp(main, [0, 10, 2], enable_opts='') |
| 3643 |
assert res == main(0, 10, 2) |
| 3644 |
self.check_resops(call_i=1) |
| 3645 |
res = self.meta_interp(main, [1, 10, 2], enable_opts='') |
| 3646 |
assert res == main(1, 10, 2) |
| 3647 |
self.check_resops(call_i=0) |
| 3648 |
|
| 3649 |
def test_look_inside_iff_const_float(self): |
| 3650 |
@look_inside_iff(lambda arg: isconstant(arg)) |
| 3651 |
def f(arg): |
| 3652 |
return arg + 0.5 |
| 3653 |
|
| 3654 |
driver = JitDriver(greens = [], reds = ['n', 'total']) |
| 3655 |
|
| 3656 |
def main(n): |
| 3657 |
total = 0.0 |
| 3658 |
while n > 0: |
| 3659 |
driver.jit_merge_point(n=n, total=total) |
| 3660 |
total = f(total) |
| 3661 |
n -= 1 |
| 3662 |
return total |
| 3663 |
|
| 3664 |
res = self.meta_interp(main, [10], enable_opts='') |
| 3665 |
assert res == 5.0 |
| 3666 |
self.check_resops(call_f=1) |
| 3667 |
|
| 3668 |
def test_look_inside_iff_virtual(self): |
| 3669 |
from rpython.rlib.debug import ll_assert_not_none |
| 3670 |
|
| 3671 |
@look_inside_iff(lambda arg, n: isvirtual(arg)) |
| 3672 |
def f(arg, n): |
| 3673 |
if n == 100: |
| 3674 |
for i in xrange(n): |
| 3675 |
n += i |
| 3676 |
return arg.x |
| 3677 |
class A(object): |
| 3678 |
def __init__(self, x): |
| 3679 |
self.x = x |
| 3680 |
driver = JitDriver(greens=['n'], reds=['i', 'a']) |
| 3681 |
def main(n): |
| 3682 |
i = 0 |
| 3683 |
a = A(3) |
| 3684 |
while i < 20: |
| 3685 |
driver.jit_merge_point(i=i, n=n, a=a) |
| 3686 |
if n == 0: |
| 3687 |
i += f(a, n) |
| 3688 |
else: |
| 3689 |
i += f(ll_assert_not_none(A(2)), n) |
| 3690 |
res = self.meta_interp(main, [0], enable_opts='') |
| 3691 |
assert res == main(0) |
| 3692 |
self.check_resops(call_i=1, getfield_gc_i=0) |
| 3693 |
res = self.meta_interp(main, [1], enable_opts='') |
| 3694 |
assert res == main(1) |
| 3695 |
self.check_resops(call_i=0, getfield_gc_i=0) |
| 3696 |
|
| 3697 |
def test_isvirtual_call_assembler(self): |
| 3698 |
driver = JitDriver(greens = ['code'], reds = ['n', 's']) |
| 3699 |
|
| 3700 |
@look_inside_iff(lambda t1, t2: isvirtual(t1)) |
| 3701 |
def g(t1, t2): |
| 3702 |
return t1[0] == t2[0] |
| 3703 |
|
| 3704 |
def create(n): |
| 3705 |
return (1, 2, n) |
| 3706 |
create._dont_inline_ = True |
| 3707 |
|
| 3708 |
def f(code, n): |
| 3709 |
s = 0 |
| 3710 |
while n > 0: |
| 3711 |
driver.can_enter_jit(code=code, n=n, s=s) |
| 3712 |
driver.jit_merge_point(code=code, n=n, s=s) |
| 3713 |
t = create(n) |
| 3714 |
if code: |
| 3715 |
f(0, 3) |
| 3716 |
s += t[2] |
| 3717 |
g(t, (1, 2, n)) |
| 3718 |
n -= 1 |
| 3719 |
return s |
| 3720 |
|
| 3721 |
self.meta_interp(f, [1, 10], inline=True) |
| 3722 |
self.check_resops(call_i=0, call_may_force_i=0, call_assembler_i=2) |
| 3723 |
|
| 3724 |
def test_reuse_elidable_result(self): |
| 3725 |
driver = JitDriver(reds=['n', 's'], greens = []) |
| 3726 |
def main(n): |
| 3727 |
s = 0 |
| 3728 |
while n > 0: |
| 3729 |
driver.jit_merge_point(s=s, n=n) |
| 3730 |
s += len(str(n)) + len(str(n)) |
| 3731 |
n -= 1 |
| 3732 |
return s |
| 3733 |
res = self.meta_interp(main, [10]) |
| 3734 |
assert res == main(10) |
| 3735 |
self.check_resops({'int_gt': 2, 'strlen': 2, 'guard_true': 2, |
| 3736 |
'int_sub': 2, 'jump': 1, 'call_r': 2, |
| 3737 |
'guard_no_exception': 2, 'int_add': 4}) |
| 3738 |
|
| 3739 |
def test_elidable_method(self): |
| 3740 |
py.test.skip("not supported so far: @elidable methods") |
| 3741 |
class A(object): |
| 3742 |
@elidable |
| 3743 |
def meth(self): |
| 3744 |
return 41 |
| 3745 |
class B(A): |
| 3746 |
@elidable |
| 3747 |
def meth(self): |
| 3748 |
return 42 |
| 3749 |
x = B() |
| 3750 |
def callme(x): |
| 3751 |
return x.meth() |
| 3752 |
def f(): |
| 3753 |
callme(A()) |
| 3754 |
return callme(x) |
| 3755 |
res = self.interp_operations(f, []) |
| 3756 |
assert res == 42 |
| 3757 |
self.check_operations_history({'finish': 1}) |
| 3758 |
|
| 3759 |
def test_look_inside_iff_const_getarrayitem_gc_pure(self): |
| 3760 |
driver = JitDriver(greens=['unroll'], reds=['s', 'n']) |
| 3761 |
|
| 3762 |
class A(object): |
| 3763 |
_immutable_fields_ = ["x[*]"] |
| 3764 |
def __init__(self, x): |
| 3765 |
self.x = [x] |
| 3766 |
|
| 3767 |
@look_inside_iff(lambda x: isconstant(x)) |
| 3768 |
def f(x): |
| 3769 |
i = 0 |
| 3770 |
for c in x: |
| 3771 |
i += 1 |
| 3772 |
return i |
| 3773 |
|
| 3774 |
def main(unroll, n): |
| 3775 |
s = 0 |
| 3776 |
while n > 0: |
| 3777 |
driver.jit_merge_point(s=s, n=n, unroll=unroll) |
| 3778 |
if unroll: |
| 3779 |
x = A("xx") |
| 3780 |
else: |
| 3781 |
x = A("x" * n) |
| 3782 |
s += f(x.x[0]) |
| 3783 |
n -= 1 |
| 3784 |
return s |
| 3785 |
|
| 3786 |
res = self.meta_interp(main, [0, 10]) |
| 3787 |
assert res == main(0, 10) |
| 3788 |
|
| 3789 |
self.check_resops(call_i=2, call_r=2) |
| 3790 |
res = self.meta_interp(main, [1, 10]) |
| 3791 |
assert res == main(1, 10) |
| 3792 |
self.check_resops(call_i=0, call_r=0) |
| 3793 |
|
| 3794 |
def test_setarrayitem_followed_by_arraycopy(self): |
| 3795 |
myjitdriver = JitDriver(greens = [], reds = ['n', 'sa', 'x', 'y']) |
| 3796 |
def f(n): |
| 3797 |
sa = 0 |
| 3798 |
x = [1,2,n] |
| 3799 |
y = [1,2,3] |
| 3800 |
while n > 0: |
| 3801 |
myjitdriver.jit_merge_point(sa=sa, n=n, x=x, y=y) |
| 3802 |
y[0] = n |
| 3803 |
x[0:3] = y |
| 3804 |
sa += x[0] |
| 3805 |
n -= 1 |
| 3806 |
return sa |
| 3807 |
res = self.meta_interp(f, [16]) |
| 3808 |
assert res == f(16) |
| 3809 |
|
| 3810 |
def test_ptr_eq(self): |
| 3811 |
myjitdriver = JitDriver(greens = [], reds = ["n", "x"]) |
| 3812 |
class A(object): |
| 3813 |
def __init__(self, v): |
| 3814 |
self.v = v |
| 3815 |
def f(n, x): |
| 3816 |
while n > 0: |
| 3817 |
myjitdriver.jit_merge_point(n=n, x=x) |
| 3818 |
z = 0 / x |
| 3819 |
a1 = A("key") |
| 3820 |
a2 = A("\x00") |
| 3821 |
n -= [a1, a2][z].v is not a2.v |
| 3822 |
return n |
| 3823 |
res = self.meta_interp(f, [10, 1]) |
| 3824 |
assert res == 0 |
| 3825 |
|
| 3826 |
def test_instance_ptr_eq(self): |
| 3827 |
myjitdriver = JitDriver(greens = [], reds = ["n", "i", "a1", "a2"]) |
| 3828 |
class A(object): |
| 3829 |
pass |
| 3830 |
def f(n): |
| 3831 |
a1 = A() |
| 3832 |
a2 = A() |
| 3833 |
i = 0 |
| 3834 |
while n > 0: |
| 3835 |
myjitdriver.jit_merge_point(n=n, i=i, a1=a1, a2=a2) |
| 3836 |
if n % 2: |
| 3837 |
a = a2 |
| 3838 |
else: |
| 3839 |
a = a1 |
| 3840 |
i += a is a1 |
| 3841 |
n -= 1 |
| 3842 |
return i |
| 3843 |
res = self.meta_interp(f, [10]) |
| 3844 |
assert res == f(10) |
| 3845 |
def f(n): |
| 3846 |
a1 = A() |
| 3847 |
a2 = A() |
| 3848 |
i = 0 |
| 3849 |
while n > 0: |
| 3850 |
myjitdriver.jit_merge_point(n=n, i=i, a1=a1, a2=a2) |
| 3851 |
if n % 2: |
| 3852 |
a = a2 |
| 3853 |
else: |
| 3854 |
a = a1 |
| 3855 |
if a is a2: |
| 3856 |
i += 1 |
| 3857 |
n -= 1 |
| 3858 |
return i |
| 3859 |
res = self.meta_interp(f, [10]) |
| 3860 |
assert res == f(10) |
| 3861 |
|
| 3862 |
def test_virtual_array_of_structs(self): |
| 3863 |
myjitdriver = JitDriver(greens = [], reds=["n", "d"]) |
| 3864 |
def f(n): |
| 3865 |
d = None |
| 3866 |
while n > 0: |
| 3867 |
myjitdriver.jit_merge_point(n=n, d=d) |
| 3868 |
d = {"q": 1} |
| 3869 |
if n % 2: |
| 3870 |
d["k"] = n |
| 3871 |
else: |
| 3872 |
d["z"] = n |
| 3873 |
n -= len(d) - d["q"] |
| 3874 |
return n |
| 3875 |
res = self.meta_interp(f, [10]) |
| 3876 |
assert res == 0 |
| 3877 |
|
| 3878 |
def test_virtual_dict_constant_keys(self): |
| 3879 |
myjitdriver = JitDriver(greens = [], reds = ["n"]) |
| 3880 |
def g(d): |
| 3881 |
return d["key"] - 1 |
| 3882 |
|
| 3883 |
def f(n): |
| 3884 |
while n > 0: |
| 3885 |
myjitdriver.jit_merge_point(n=n) |
| 3886 |
x = {"key": n} |
| 3887 |
n = g(x) |
| 3888 |
del x["key"] |
| 3889 |
return n |
| 3890 |
|
| 3891 |
res = self.meta_interp(f, [10]) |
| 3892 |
assert res == 0 |
| 3893 |
self.check_resops({'jump': 1, 'guard_true': 2, 'int_gt': 2, |
| 3894 |
'int_sub': 2}) |
| 3895 |
|
| 3896 |
def test_virtual_opaque_ptr(self): |
| 3897 |
myjitdriver = JitDriver(greens = [], reds = ["n"]) |
| 3898 |
erase, unerase = rerased.new_erasing_pair("x") |
| 3899 |
@look_inside_iff(lambda x: isvirtual(x)) |
| 3900 |
def g(x): |
| 3901 |
return x[0] |
| 3902 |
def f(n): |
| 3903 |
while n > 0: |
| 3904 |
myjitdriver.jit_merge_point(n=n) |
| 3905 |
x = [] |
| 3906 |
y = erase(x) |
| 3907 |
z = unerase(y) |
| 3908 |
z.append(1) |
| 3909 |
n -= g(z) |
| 3910 |
return n |
| 3911 |
res = self.meta_interp(f, [10]) |
| 3912 |
assert res == 0 |
| 3913 |
self.check_resops({'jump': 1, 'guard_true': 2, 'int_gt': 2, |
| 3914 |
'int_sub': 2}) |
| 3915 |
|
| 3916 |
|
| 3917 |
def test_virtual_opaque_dict(self): |
| 3918 |
myjitdriver = JitDriver(greens = [], reds = ["n"]) |
| 3919 |
erase, unerase = rerased.new_erasing_pair("x") |
| 3920 |
@look_inside_iff(lambda x: isvirtual(x)) |
| 3921 |
def g(x): |
| 3922 |
return x[0]["key"] - 1 |
| 3923 |
def f(n): |
| 3924 |
while n > 0: |
| 3925 |
myjitdriver.jit_merge_point(n=n) |
| 3926 |
x = [{}] |
| 3927 |
x[0]["key"] = n |
| 3928 |
x[0]["other key"] = n |
| 3929 |
y = erase(x) |
| 3930 |
z = unerase(y) |
| 3931 |
n = g(x) |
| 3932 |
return n |
| 3933 |
res = self.meta_interp(f, [10]) |
| 3934 |
assert res == 0 |
| 3935 |
self.check_resops({'int_sub': 2, 'int_gt': 2, 'guard_true': 2, |
| 3936 |
'jump': 1}) |
| 3937 |
|
| 3938 |
def test_virtual_after_bridge(self): |
| 3939 |
myjitdriver = JitDriver(greens = [], reds = ["n"]) |
| 3940 |
@look_inside_iff(lambda x: isvirtual(x)) |
| 3941 |
def g(x): |
| 3942 |
return x[0] |
| 3943 |
def f(n): |
| 3944 |
while n > 0: |
| 3945 |
myjitdriver.jit_merge_point(n=n) |
| 3946 |
x = [1] |
| 3947 |
if n & 1: |
| 3948 |
n -= g(x) |
| 3949 |
else: |
| 3950 |
n -= g(x) |
| 3951 |
return n |
| 3952 |
res = self.meta_interp(f, [10]) |
| 3953 |
assert res == 0 |
| 3954 |
self.check_resops(call_i=0, call_may_force_i=0, new_array=0) |
| 3955 |
|
| 3956 |
def test_loop_unrolling_heuristic_needs_constant_size(self): |
| 3957 |
myjitdriver = JitDriver(greens = [], reds = 'auto') |
| 3958 |
@look_inside_iff(lambda x: loop_unrolling_heuristic(x, len(x))) |
| 3959 |
def g(x): |
| 3960 |
return x[0] |
| 3961 |
def f(n): |
| 3962 |
l = [1] * 1000 |
| 3963 |
l.append(1) |
| 3964 |
while n > 0: |
| 3965 |
myjitdriver.jit_merge_point() |
| 3966 |
x = l[:n] |
| 3967 |
n -= g(x) |
| 3968 |
x.append(n) |
| 3969 |
return n |
| 3970 |
res = self.meta_interp(f, [10]) |
| 3971 |
assert res == 0 |
| 3972 |
self.check_resops(call_i=2, new_array=2) |
| 3973 |
|
| 3974 |
def test_convert_from_SmallFunctionSetPBCRepr_to_FunctionsPBCRepr(self): |
| 3975 |
f1 = lambda n: n+1 |
| 3976 |
f2 = lambda n: n+2 |
| 3977 |
f3 = lambda n: n+3 |
| 3978 |
f4 = lambda n: n+4 |
| 3979 |
f5 = lambda n: n+5 |
| 3980 |
f6 = lambda n: n+6 |
| 3981 |
f7 = lambda n: n+7 |
| 3982 |
f8 = lambda n: n+8 |
| 3983 |
def h(n, x): |
| 3984 |
return x(n) |
| 3985 |
h._dont_inline = True |
| 3986 |
def g(n, x): |
| 3987 |
return h(n, x) |
| 3988 |
g._dont_inline = True |
| 3989 |
def f(n): |
| 3990 |
n = g(n, f1) |
| 3991 |
n = g(n, f2) |
| 3992 |
n = h(n, f3) |
| 3993 |
n = h(n, f4) |
| 3994 |
n = h(n, f5) |
| 3995 |
n = h(n, f6) |
| 3996 |
n = h(n, f7) |
| 3997 |
n = h(n, f8) |
| 3998 |
return n |
| 3999 |
assert f(5) == 41 |
| 4000 |
translationoptions = {'withsmallfuncsets': 3} |
| 4001 |
self.interp_operations(f, [5], translationoptions=translationoptions) |
| 4002 |
|
| 4003 |
|
| 4004 |
def test_annotation_gives_class_knowledge_to_tracer(self): |
| 4005 |
py.test.skip("disabled") |
| 4006 |
class Base(object): |
| 4007 |
pass |
| 4008 |
class A(Base): |
| 4009 |
def f(self): |
| 4010 |
return self.a |
| 4011 |
def g(self): |
| 4012 |
return self.a + 1 |
| 4013 |
class B(Base): |
| 4014 |
def f(self): |
| 4015 |
return self.b |
| 4016 |
def g(self): |
| 4017 |
return self.b + 1 |
| 4018 |
class C(B): |
| 4019 |
def f(self): |
| 4020 |
self.c += 1 |
| 4021 |
return self.c |
| 4022 |
def g(self): |
| 4023 |
return self.c + 1 |
| 4024 |
@dont_look_inside |
| 4025 |
def make(x): |
| 4026 |
if x > 0: |
| 4027 |
a = A() |
| 4028 |
a.a = x + 1 |
| 4029 |
elif x < 0: |
| 4030 |
a = B() |
| 4031 |
a.b = -x |
| 4032 |
else: |
| 4033 |
a = C() |
| 4034 |
a.c = 10 |
| 4035 |
return a |
| 4036 |
def f(x): |
| 4037 |
a = make(x) |
| 4038 |
if x > 0: |
| 4039 |
assert isinstance(a, A) |
| 4040 |
z = a.f() |
| 4041 |
elif x < 0: |
| 4042 |
assert isinstance(a, B) |
| 4043 |
z = a.f() |
| 4044 |
else: |
| 4045 |
assert isinstance(a, C) |
| 4046 |
z = a.f() |
| 4047 |
return z + a.g() |
| 4048 |
res1 = f(6) |
| 4049 |
res2 = self.interp_operations(f, [6]) |
| 4050 |
assert res1 == res2 |
| 4051 |
self.check_operations_history(guard_class=0, record_exact_class=1) |
| 4052 |
|
| 4053 |
res1 = f(-6) |
| 4054 |
res2 = self.interp_operations(f, [-6]) |
| 4055 |
assert res1 == res2 |
| 4056 |
|
| 4057 |
self.check_operations_history(guard_class=1) |
| 4058 |
|
| 4059 |
res1 = f(0) |
| 4060 |
res2 = self.interp_operations(f, [0]) |
| 4061 |
assert res1 == res2 |
| 4062 |
|
| 4063 |
self.check_operations_history(guard_class=0, record_exact_class=1) |
| 4064 |
|
| 4065 |
def test_give_class_knowledge_to_tracer_explicitly(self): |
| 4066 |
class Base(object): |
| 4067 |
def f(self): |
| 4068 |
raise NotImplementedError |
| 4069 |
def g(self): |
| 4070 |
raise NotImplementedError |
| 4071 |
class A(Base): |
| 4072 |
def f(self): |
| 4073 |
return self.a |
| 4074 |
def g(self): |
| 4075 |
return self.a + 1 |
| 4076 |
class B(Base): |
| 4077 |
def f(self): |
| 4078 |
return self.b |
| 4079 |
def g(self): |
| 4080 |
return self.b + 1 |
| 4081 |
class C(B): |
| 4082 |
def f(self): |
| 4083 |
self.c += 1 |
| 4084 |
return self.c |
| 4085 |
def g(self): |
| 4086 |
return self.c + 1 |
| 4087 |
@dont_look_inside |
| 4088 |
def make(x): |
| 4089 |
if x > 0: |
| 4090 |
a = A() |
| 4091 |
a.a = x + 1 |
| 4092 |
elif x < 0: |
| 4093 |
a = B() |
| 4094 |
a.b = -x |
| 4095 |
else: |
| 4096 |
a = C() |
| 4097 |
a.c = 10 |
| 4098 |
return a |
| 4099 |
def f(x): |
| 4100 |
a = make(x) |
| 4101 |
if x > 0: |
| 4102 |
record_exact_class(a, A) |
| 4103 |
z = a.f() |
| 4104 |
elif x < 0: |
| 4105 |
record_exact_class(a, B) |
| 4106 |
z = a.f() |
| 4107 |
else: |
| 4108 |
record_exact_class(a, C) |
| 4109 |
z = a.f() |
| 4110 |
return z + a.g() |
| 4111 |
res1 = f(6) |
| 4112 |
res2 = self.interp_operations(f, [6]) |
| 4113 |
assert res1 == res2 |
| 4114 |
self.check_operations_history(guard_class=0, record_exact_class=1) |
| 4115 |
|
| 4116 |
res1 = f(-6) |
| 4117 |
res2 = self.interp_operations(f, [-6]) |
| 4118 |
assert res1 == res2 |
| 4119 |
self.check_operations_history(guard_class=0, record_exact_class=1) |
| 4120 |
|
| 4121 |
res1 = f(0) |
| 4122 |
res2 = self.interp_operations(f, [0]) |
| 4123 |
assert res1 == res2 |
| 4124 |
|
| 4125 |
self.check_operations_history(guard_class=0, record_exact_class=1) |
| 4126 |
|
| 4127 |
def test_record_known_result(self): |
| 4128 |
@elidable |
| 4129 |
def f(x): |
| 4130 |
return x + 1 |
| 4131 |
@elidable |
| 4132 |
def g(x): |
| 4133 |
return x - 1 |
| 4134 |
|
| 4135 |
def call_f(x): |
| 4136 |
y = f(x) |
| 4137 |
record_known_result(x, g, y) |
| 4138 |
a = g(y) |
| 4139 |
return a |
| 4140 |
|
| 4141 |
myjitdriver = JitDriver(greens=[], reds=['x', 'res']) |
| 4142 |
def main(x): |
| 4143 |
res = 0 |
| 4144 |
while x > 0: |
| 4145 |
myjitdriver.jit_merge_point(x=x, res=res) |
| 4146 |
res += x |
| 4147 |
x = call_f(x) - 1 |
| 4148 |
return res |
| 4149 |
res = self.meta_interp(main, [10], backendopt=True) |
| 4150 |
assert res == main(10) |
| 4151 |
self.check_resops(call_i=2) |
| 4152 |
|
| 4153 |
|
| 4154 |
def test_record_exact_value(self): |
| 4155 |
class A(object): |
| 4156 |
_immutable_fields_ = ['x'] |
| 4157 |
|
| 4158 |
a = A() |
| 4159 |
b = A() |
| 4160 |
a.x = 42 |
| 4161 |
b.x = 233 |
| 4162 |
|
| 4163 |
@dont_look_inside |
| 4164 |
def make(x): |
| 4165 |
if x > 0: |
| 4166 |
return a |
| 4167 |
else: |
| 4168 |
return b |
| 4169 |
def f(x): |
| 4170 |
inst = make(x) |
| 4171 |
if x > 0: |
| 4172 |
record_exact_value(inst, a) |
| 4173 |
else: |
| 4174 |
record_exact_value(inst, b) |
| 4175 |
return inst.x |
| 4176 |
res = self.interp_operations(f, [1]) |
| 4177 |
assert res == 42 |
| 4178 |
self.check_operations_history(record_exact_value_r=1) |
| 4179 |
|
| 4180 |
def test_record_exact_value_int_constant(self): |
| 4181 |
class A: |
| 4182 |
pass |
| 4183 |
def f(x): |
| 4184 |
a = A() |
| 4185 |
if x == 1: |
| 4186 |
a.x = 1 |
| 4187 |
else: |
| 4188 |
a.x = x |
| 4189 |
record_exact_value(a.x, 1) |
| 4190 |
return a.x |
| 4191 |
res = self.interp_operations(f, [1]) |
| 4192 |
assert res == 1 |
| 4193 |
|
| 4194 |
self.check_operations_history(record_exact_value_i=0) |
| 4195 |
|
| 4196 |
def test_record_exact_value_int_constant_bogus(self): |
| 4197 |
class A: |
| 4198 |
pass |
| 4199 |
def f(x): |
| 4200 |
a = A() |
| 4201 |
if x == 1: |
| 4202 |
a.x = 1 |
| 4203 |
else: |
| 4204 |
a.x = x |
| 4205 |
record_exact_value(a.x, 12) |
| 4206 |
return a.x |
| 4207 |
|
| 4208 |
|
| 4209 |
with py.test.raises(Exception): |
| 4210 |
self.interp_operations(f, [1]) |
| 4211 |
|
| 4212 |
def test_generator(self): |
| 4213 |
def g(n): |
| 4214 |
yield n+1 |
| 4215 |
yield n+2 |
| 4216 |
yield n+3 |
| 4217 |
def f(n): |
| 4218 |
gen = g(n) |
| 4219 |
return gen.next() * gen.next() * gen.next() |
| 4220 |
res = self.interp_operations(f, [10]) |
| 4221 |
assert res == 11 * 12 * 13 |
| 4222 |
self.check_operations_history(int_add=3, int_mul=2) |
| 4223 |
|
| 4224 |
def test_setinteriorfield(self): |
| 4225 |
A = lltype.GcArray(lltype.Struct('S', ('x', lltype.Signed))) |
| 4226 |
a = lltype.malloc(A, 5, immortal=True) |
| 4227 |
def g(n): |
| 4228 |
a[n].x = n + 2 |
| 4229 |
return a[n].x |
| 4230 |
res = self.interp_operations(g, [1]) |
| 4231 |
assert res == 3 |
| 4232 |
|
| 4233 |
def test_float_bytes(self): |
| 4234 |
def f(n): |
| 4235 |
ll = float2longlong(n) |
| 4236 |
return longlong2float(ll) |
| 4237 |
|
| 4238 |
for x in [2.5, float("nan"), -2.5, float("inf")]: |
| 4239 |
|
| 4240 |
res = self.interp_operations(f, [x]) |
| 4241 |
assert res == x or math.isnan(x) and math.isnan(res) |
| 4242 |
|
| 4243 |
|
| 4244 |
class TestLLtype(BaseLLtypeTests, LLJitMixin): |
| 4245 |
def test_rerased(self): |
| 4246 |
eraseX, uneraseX = rerased.new_erasing_pair("X") |
| 4247 |
|
| 4248 |
class X: |
| 4249 |
def __init__(self, a, b): |
| 4250 |
self.a = a |
| 4251 |
self.b = b |
| 4252 |
|
| 4253 |
def f(i, j): |
| 4254 |
|
| 4255 |
if j > 0: |
| 4256 |
e = eraseX(X(i, j)) |
| 4257 |
else: |
| 4258 |
try: |
| 4259 |
e = rerased.erase_int(i) |
| 4260 |
except OverflowError: |
| 4261 |
return -42 |
| 4262 |
if j & 1: |
| 4263 |
x = uneraseX(e) |
| 4264 |
return x.a - x.b |
| 4265 |
else: |
| 4266 |
return rerased.unerase_int(e) |
| 4267 |
|
| 4268 |
topt = {'taggedpointers': True} |
| 4269 |
x = self.interp_operations(f, [-128, 0], translationoptions=topt) |
| 4270 |
assert x == -128 |
| 4271 |
bigint = sys.maxint//2 + 1 |
| 4272 |
x = self.interp_operations(f, [bigint, 0], translationoptions=topt) |
| 4273 |
assert x == -42 |
| 4274 |
x = self.interp_operations(f, [1000, 1], translationoptions=topt) |
| 4275 |
assert x == 999 |
| 4276 |
|
| 4277 |
def test_retracing_bridge_from_interpreter_to_finnish(self): |
| 4278 |
myjitdriver = JitDriver(greens = [], reds = ['n', 'i', 'sa']) |
| 4279 |
def f(n): |
| 4280 |
sa = i = 0 |
| 4281 |
while i < n: |
| 4282 |
myjitdriver.jit_merge_point(n=n, i=i, sa=sa) |
| 4283 |
n = hint(n, promote=True) |
| 4284 |
sa += 2*n |
| 4285 |
i += 1 |
| 4286 |
return sa |
| 4287 |
def g(n): |
| 4288 |
return f(n) + f(n) + f(n) + f(n) + f(10*n) + f(11*n) |
| 4289 |
res = self.meta_interp(g, [1], repeat=3) |
| 4290 |
assert res == g(1) |
| 4291 |
|
| 4292 |
self.check_jitcell_token_count(2) |
| 4293 |
|
| 4294 |
|
| 4295 |
|
| 4296 |
|
| 4297 |
|
| 4298 |
|
| 4299 |
|
| 4300 |
|
| 4301 |
|
| 4302 |
def test_ll_arraycopy(self): |
| 4303 |
A = lltype.GcArray(lltype.Char) |
| 4304 |
a = lltype.malloc(A, 10) |
| 4305 |
for i in range(10): a[i] = chr(i) |
| 4306 |
b = lltype.malloc(A, 10) |
| 4307 |
|
| 4308 |
def f(c, d, e): |
| 4309 |
rgc.ll_arraycopy(a, b, c, d, e) |
| 4310 |
return 42 |
| 4311 |
self.interp_operations(f, [1, 2, 3]) |
| 4312 |
self.check_operations_history(call_n=1, guard_no_exception=0) |
| 4313 |
|
| 4314 |
def test_weakref(self): |
| 4315 |
import weakref |
| 4316 |
|
| 4317 |
class A(object): |
| 4318 |
def __init__(self, x): |
| 4319 |
self.x = x |
| 4320 |
|
| 4321 |
def f(i): |
| 4322 |
a = A(i) |
| 4323 |
w = weakref.ref(a) |
| 4324 |
return w().x + a.x |
| 4325 |
|
| 4326 |
assert self.interp_operations(f, [3]) == 6 |
| 4327 |
|
| 4328 |
def test_gc_add_memory_pressure(self): |
| 4329 |
def f(): |
| 4330 |
rgc.add_memory_pressure(1234) |
| 4331 |
return 3 |
| 4332 |
|
| 4333 |
self.interp_operations(f, []) |
| 4334 |
|
| 4335 |
def test_external_call(self): |
| 4336 |
from rpython.rlib import rgil |
| 4337 |
|
| 4338 |
TIME_T = lltype.Signed |
| 4339 |
|
| 4340 |
|
| 4341 |
T = rffi.CArrayPtr(TIME_T) |
| 4342 |
external = rffi.llexternal("time", [T], TIME_T) |
| 4343 |
|
| 4344 |
class Oups(Exception): |
| 4345 |
pass |
| 4346 |
class State: |
| 4347 |
pass |
| 4348 |
state = State() |
| 4349 |
|
| 4350 |
def after(): |
| 4351 |
if we_are_jitted(): |
| 4352 |
raise Oups |
| 4353 |
state.l.append("after") |
| 4354 |
|
| 4355 |
def f(): |
| 4356 |
state.l = [] |
| 4357 |
rgil.invoke_after_thread_switch(after) |
| 4358 |
external(lltype.nullptr(T.TO)) |
| 4359 |
return len(state.l) |
| 4360 |
|
| 4361 |
res = self.interp_operations(f, []) |
| 4362 |
assert res == 1 |
| 4363 |
res = self.interp_operations(f, []) |
| 4364 |
assert res == 1 |
| 4365 |
self.check_operations_history(call_release_gil_i=1, call_may_force_i=0) |
| 4366 |
|
| 4367 |
def test_unescaped_write_zero(self): |
| 4368 |
class A: |
| 4369 |
pass |
| 4370 |
def g(): |
| 4371 |
return A() |
| 4372 |
@dont_look_inside |
| 4373 |
def escape(): |
| 4374 |
print "hi!" |
| 4375 |
def f(n): |
| 4376 |
a = g() |
| 4377 |
a.x = n |
| 4378 |
escape() |
| 4379 |
a.x = 0 |
| 4380 |
escape() |
| 4381 |
return a.x |
| 4382 |
res = self.interp_operations(f, [42]) |
| 4383 |
assert res == 0 |
| 4384 |
|
| 4385 |
def test_conditions_without_guards(self): |
| 4386 |
def f(n): |
| 4387 |
if (n == 1) | (n == 3) | (n == 17): |
| 4388 |
return 42 |
| 4389 |
return 5 |
| 4390 |
res = self.interp_operations(f, [17]) |
| 4391 |
assert res == 42 |
| 4392 |
self.check_operations_history(guard_true=1, guard_false=0) |
| 4393 |
|
| 4394 |
def test_not_in_trace(self): |
| 4395 |
class X: |
| 4396 |
n = 0 |
| 4397 |
def g(x): |
| 4398 |
if we_are_jitted(): |
| 4399 |
raise NotImplementedError |
| 4400 |
x.n += 1 |
| 4401 |
g.oopspec = 'jit.not_in_trace()' |
| 4402 |
|
| 4403 |
jitdriver = JitDriver(greens=[], reds=['n', 'token', 'x']) |
| 4404 |
def f(n): |
| 4405 |
token = 0 |
| 4406 |
x = X() |
| 4407 |
while n >= 0: |
| 4408 |
jitdriver.jit_merge_point(n=n, x=x, token=token) |
| 4409 |
if not we_are_jitted(): |
| 4410 |
token += 1 |
| 4411 |
g(x) |
| 4412 |
n -= 1 |
| 4413 |
return x.n + token * 1000 |
| 4414 |
|
| 4415 |
res = self.meta_interp(f, [10]) |
| 4416 |
assert res == 2003 |
| 4417 |
self.check_resops(int_add=0, call_i=0, call_may_force_i=0, |
| 4418 |
call_r=0, call_may_force_r=0, call_f=0, |
| 4419 |
call_may_force_f=0) |
| 4420 |
|
| 4421 |
def test_not_in_trace_exception(self): |
| 4422 |
def g(): |
| 4423 |
if we_are_jitted(): |
| 4424 |
raise NotImplementedError |
| 4425 |
raise ValueError |
| 4426 |
g.oopspec = 'jit.not_in_trace()' |
| 4427 |
|
| 4428 |
jitdriver = JitDriver(greens=[], reds=['n']) |
| 4429 |
def f(n): |
| 4430 |
while n >= 0: |
| 4431 |
jitdriver.jit_merge_point(n=n) |
| 4432 |
try: |
| 4433 |
g() |
| 4434 |
except ValueError: |
| 4435 |
n -= 1 |
| 4436 |
return 42 |
| 4437 |
|
| 4438 |
res = self.meta_interp(f, [10]) |
| 4439 |
assert res == 42 |
| 4440 |
self.check_aborted_count(3) |
| 4441 |
|
| 4442 |
def test_not_in_trace_blackhole(self): |
| 4443 |
class X: |
| 4444 |
seen = 0 |
| 4445 |
def g(x): |
| 4446 |
if we_are_jitted(): |
| 4447 |
raise NotImplementedError |
| 4448 |
x.seen = 42 |
| 4449 |
g.oopspec = 'jit.not_in_trace()' |
| 4450 |
|
| 4451 |
jitdriver = JitDriver(greens=[], reds=['n']) |
| 4452 |
def f(n): |
| 4453 |
while n >= 0: |
| 4454 |
jitdriver.jit_merge_point(n=n) |
| 4455 |
n -= 1 |
| 4456 |
x = X() |
| 4457 |
g(x) |
| 4458 |
return x.seen |
| 4459 |
|
| 4460 |
res = self.meta_interp(f, [10]) |
| 4461 |
assert res == 42 |
| 4462 |
|
| 4463 |
def test_int_force_ge_zero(self): |
| 4464 |
def f(n): |
| 4465 |
return int_force_ge_zero(n) |
| 4466 |
res = self.interp_operations(f, [42]) |
| 4467 |
assert res == 42 |
| 4468 |
res = self.interp_operations(f, [-42]) |
| 4469 |
assert res == 0 |
| 4470 |
|
| 4471 |
def test_cmp_fastpaths(self): |
| 4472 |
class Z: pass |
| 4473 |
def make_int(cmp): |
| 4474 |
def f(x): |
| 4475 |
if cmp == 'eq': |
| 4476 |
return x == x and x == x |
| 4477 |
if cmp == 'ne': |
| 4478 |
return x != x or x != x |
| 4479 |
if cmp == 'lt': |
| 4480 |
return x < x or x != x |
| 4481 |
if cmp == 'le': |
| 4482 |
return x <= x and x <= x |
| 4483 |
if cmp == 'gt': |
| 4484 |
return x > x or x > x |
| 4485 |
if cmp == 'ge': |
| 4486 |
return x >= x and x >= x |
| 4487 |
assert 0 |
| 4488 |
return f |
| 4489 |
|
| 4490 |
def make_str(cmp): |
| 4491 |
def f(x): |
| 4492 |
x = str(x) |
| 4493 |
if cmp == 'eq': |
| 4494 |
return x is x or x is x |
| 4495 |
if cmp == 'ne': |
| 4496 |
return x is not x and x is not x |
| 4497 |
assert 0 |
| 4498 |
return f |
| 4499 |
|
| 4500 |
def make_object(cmp): |
| 4501 |
def f(x): |
| 4502 |
y = Z() |
| 4503 |
y.x = x |
| 4504 |
x = y |
| 4505 |
if cmp == 'eq': |
| 4506 |
return x is x |
| 4507 |
if cmp == 'ne': |
| 4508 |
return x is not x |
| 4509 |
assert 0 |
| 4510 |
return f |
| 4511 |
|
| 4512 |
for cmp in 'eq ne lt le gt ge'.split(): |
| 4513 |
f = make_int(cmp) |
| 4514 |
res = self.interp_operations(f, [42]) |
| 4515 |
assert res == f(42) |
| 4516 |
opname = "int_%s" % cmp |
| 4517 |
self.check_operations_history(**{opname: 0}) |
| 4518 |
|
| 4519 |
for cmp in 'eq ne'.split(): |
| 4520 |
f = make_str(cmp) |
| 4521 |
res = self.interp_operations(f, [42]) |
| 4522 |
assert res == f(42) |
| 4523 |
opname = "ptr_%s" % cmp |
| 4524 |
self.check_operations_history(**{opname: 0}) |
| 4525 |
|
| 4526 |
f = make_object(cmp) |
| 4527 |
res = self.interp_operations(f, [42]) |
| 4528 |
assert res == f(42) |
| 4529 |
opname = "instance_ptr_%s" % cmp |
| 4530 |
self.check_operations_history(**{opname: 0}) |
| 4531 |
|
| 4532 |
def test_compile_framework_9(self): |
| 4533 |
class X(object): |
| 4534 |
def __init__(self, x=0): |
| 4535 |
self.x = x |
| 4536 |
|
| 4537 |
next = None |
| 4538 |
|
| 4539 |
class CheckError(Exception): |
| 4540 |
pass |
| 4541 |
|
| 4542 |
def check(flag): |
| 4543 |
if not flag: |
| 4544 |
raise CheckError |
| 4545 |
|
| 4546 |
def before(n, x): |
| 4547 |
return n, x, None, None, None, None, None, None, None, None, [X(123)], None |
| 4548 |
def f(n, x, x0, x1, x2, x3, x4, x5, x6, x7, l, s): |
| 4549 |
if n < 1900: |
| 4550 |
check(l[0].x == 123) |
| 4551 |
num = 512 + (n & 7) |
| 4552 |
l = [None] * num |
| 4553 |
l[0] = X(123) |
| 4554 |
l[1] = X(n) |
| 4555 |
l[2] = X(n+10) |
| 4556 |
l[3] = X(n+20) |
| 4557 |
l[4] = X(n+30) |
| 4558 |
l[5] = X(n+40) |
| 4559 |
l[6] = X(n+50) |
| 4560 |
l[7] = X(n+60) |
| 4561 |
l[num-8] = X(n+70) |
| 4562 |
l[num-9] = X(n+80) |
| 4563 |
l[num-10] = X(n+90) |
| 4564 |
l[num-11] = X(n+100) |
| 4565 |
l[-12] = X(n+110) |
| 4566 |
l[-13] = X(n+120) |
| 4567 |
l[-14] = X(n+130) |
| 4568 |
l[-15] = X(n+140) |
| 4569 |
if n < 1800: |
| 4570 |
num = 512 + (n & 7) |
| 4571 |
check(len(l) == num) |
| 4572 |
check(l[0].x == 123) |
| 4573 |
check(l[1].x == n) |
| 4574 |
check(l[2].x == n+10) |
| 4575 |
check(l[3].x == n+20) |
| 4576 |
check(l[4].x == n+30) |
| 4577 |
check(l[5].x == n+40) |
| 4578 |
check(l[6].x == n+50) |
| 4579 |
check(l[7].x == n+60) |
| 4580 |
check(l[num-8].x == n+70) |
| 4581 |
check(l[num-9].x == n+80) |
| 4582 |
check(l[num-10].x == n+90) |
| 4583 |
check(l[num-11].x == n+100) |
| 4584 |
check(l[-12].x == n+110) |
| 4585 |
check(l[-13].x == n+120) |
| 4586 |
check(l[-14].x == n+130) |
| 4587 |
check(l[-15].x == n+140) |
| 4588 |
n -= x.foo |
| 4589 |
return n, x, x0, x1, x2, x3, x4, x5, x6, x7, l, s |
| 4590 |
def after(n, x, x0, x1, x2, x3, x4, x5, x6, x7, l, s): |
| 4591 |
check(len(l) >= 512) |
| 4592 |
check(l[0].x == 123) |
| 4593 |
check(l[1].x == 2) |
| 4594 |
check(l[2].x == 12) |
| 4595 |
check(l[3].x == 22) |
| 4596 |
check(l[4].x == 32) |
| 4597 |
check(l[5].x == 42) |
| 4598 |
check(l[6].x == 52) |
| 4599 |
check(l[7].x == 62) |
| 4600 |
check(l[-8].x == 72) |
| 4601 |
check(l[-9].x == 82) |
| 4602 |
check(l[-10].x == 92) |
| 4603 |
check(l[-11].x == 102) |
| 4604 |
check(l[-12].x == 112) |
| 4605 |
check(l[-13].x == 122) |
| 4606 |
check(l[-14].x == 132) |
| 4607 |
check(l[-15].x == 142) |
| 4608 |
|
| 4609 |
def allfuncs(num, n): |
| 4610 |
x = X() |
| 4611 |
x.foo = 2 |
| 4612 |
main_allfuncs(num, n, x) |
| 4613 |
x.foo = 5 |
| 4614 |
return x |
| 4615 |
def main_allfuncs(num, n, x): |
| 4616 |
n, x, x0, x1, x2, x3, x4, x5, x6, x7, l, s = before(n, x) |
| 4617 |
while n > 0: |
| 4618 |
myjitdriver.can_enter_jit(num=num, n=n, x=x, x0=x0, x1=x1, |
| 4619 |
x2=x2, x3=x3, x4=x4, x5=x5, x6=x6, x7=x7, l=l, s=s) |
| 4620 |
myjitdriver.jit_merge_point(num=num, n=n, x=x, x0=x0, x1=x1, |
| 4621 |
x2=x2, x3=x3, x4=x4, x5=x5, x6=x6, x7=x7, l=l, s=s) |
| 4622 |
|
| 4623 |
n, x, x0, x1, x2, x3, x4, x5, x6, x7, l, s = f( |
| 4624 |
n, x, x0, x1, x2, x3, x4, x5, x6, x7, l, s) |
| 4625 |
after(n, x, x0, x1, x2, x3, x4, x5, x6, x7, l, s) |
| 4626 |
myjitdriver = JitDriver(greens = ['num'], |
| 4627 |
reds = ['n', 'x', 'x0', 'x1', 'x2', 'x3', 'x4', |
| 4628 |
'x5', 'x6', 'x7', 'l', 's']) |
| 4629 |
|
| 4630 |
|
| 4631 |
self.meta_interp(allfuncs, [9, 2000]) |
| 4632 |
|
| 4633 |
def test_unichar_ord_is_never_signed_on_64bit(self): |
| 4634 |
import sys |
| 4635 |
if sys.maxunicode == 0xffff: |
| 4636 |
py.test.skip("test for 32-bit unicodes") |
| 4637 |
def f(x): |
| 4638 |
return ord(rffi.cast(lltype.UniChar, x)) |
| 4639 |
res = self.interp_operations(f, [-1]) |
| 4640 |
if sys.maxint == 2147483647: |
| 4641 |
assert res == -1 |
| 4642 |
else: |
| 4643 |
assert res == 4294967295 |
| 4644 |
|
| 4645 |
def test_issue2200_recursion(self): |
| 4646 |
|
| 4647 |
|
| 4648 |
|
| 4649 |
|
| 4650 |
|
| 4651 |
|
| 4652 |
|
| 4653 |
|
| 4654 |
|
| 4655 |
|
| 4656 |
driver = JitDriver(greens=[], reds=["i"]) |
| 4657 |
def enter(i): |
| 4658 |
driver.can_enter_jit(i=i) |
| 4659 |
def f(): |
| 4660 |
set_param(None, 'trace_eagerness', 999999) |
| 4661 |
i = 0 |
| 4662 |
while True: |
| 4663 |
driver.jit_merge_point(i=i) |
| 4664 |
i += 1 |
| 4665 |
if i >= 300: |
| 4666 |
return i |
| 4667 |
promote(i + 1) |
| 4668 |
enter(i) |
| 4669 |
|
| 4670 |
self.meta_interp(f, []) |
| 4671 |
|
| 4672 |
def test_issue2335_recursion(self): |
| 4673 |
|
| 4674 |
|
| 4675 |
driver = JitDriver(greens=["level"], reds=["i"]) |
| 4676 |
def enter(level, i): |
| 4677 |
if level == 0: |
| 4678 |
f(1) |
| 4679 |
driver.can_enter_jit(level=level, i=i) |
| 4680 |
def f(level): |
| 4681 |
i = 0 if level == 0 else 298 |
| 4682 |
while True: |
| 4683 |
driver.jit_merge_point(level=level, i=i) |
| 4684 |
i += 1 |
| 4685 |
if i >= 300: |
| 4686 |
return i |
| 4687 |
promote(i + 1) |
| 4688 |
enter(level, i) |
| 4689 |
def main(): |
| 4690 |
set_param(None, 'trace_eagerness', 999999) |
| 4691 |
f(0) |
| 4692 |
self.meta_interp(main, []) |
| 4693 |
|
| 4694 |
def test_pending_setarrayitem_with_indirect_constant_index(self): |
| 4695 |
driver = JitDriver(greens=[], reds='auto') |
| 4696 |
class X: |
| 4697 |
pass |
| 4698 |
def f(): |
| 4699 |
xs = [None] |
| 4700 |
i = 0 |
| 4701 |
while i < 17: |
| 4702 |
driver.jit_merge_point() |
| 4703 |
xs[noConst(0)] = X() |
| 4704 |
if i & 5: |
| 4705 |
pass |
| 4706 |
i += 1 |
| 4707 |
return i |
| 4708 |
self.meta_interp(f, []) |
| 4709 |
|
| 4710 |
def test_round_trip_raw_pointer(self): |
| 4711 |
|
| 4712 |
|
| 4713 |
|
| 4714 |
|
| 4715 |
|
| 4716 |
|
| 4717 |
|
| 4718 |
|
| 4719 |
|
| 4720 |
|
| 4721 |
|
| 4722 |
|
| 4723 |
|
| 4724 |
|
| 4725 |
|
| 4726 |
|
| 4727 |
driver = JitDriver(greens=[], reds=['i', 'val']) |
| 4728 |
|
| 4729 |
class Box(object): |
| 4730 |
_ptr = lltype.nullptr(rffi.CCHARP.TO) |
| 4731 |
|
| 4732 |
def new_int_buffer(value): |
| 4733 |
data = lltype.malloc(rffi.CCHARP.TO, rffi.sizeof(rffi.INT), flavor='raw') |
| 4734 |
rffi.cast(rffi.INTP, data)[0] = rffi.cast(rffi.INT, value) |
| 4735 |
return data |
| 4736 |
|
| 4737 |
def read_int_buffer(buf): |
| 4738 |
return rffi.cast(rffi.INTP, buf)[0] |
| 4739 |
|
| 4740 |
def f(): |
| 4741 |
i = 0 |
| 4742 |
val = Box() |
| 4743 |
val._ptr = new_int_buffer(1) |
| 4744 |
|
| 4745 |
set_param(None, 'retrace_limit', -1) |
| 4746 |
while i < 100: |
| 4747 |
driver.jit_merge_point(i=i, val=val) |
| 4748 |
driver.can_enter_jit(i=i, val=val) |
| 4749 |
|
| 4750 |
if i & 0b100: |
| 4751 |
i += 1 |
| 4752 |
i += int(read_int_buffer(val._ptr)) |
| 4753 |
lltype.free(val._ptr, flavor='raw') |
| 4754 |
val._ptr = new_int_buffer(1) |
| 4755 |
lltype.free(val._ptr, flavor='raw') |
| 4756 |
|
| 4757 |
self.meta_interp(f, []) |
| 4758 |
self.check_resops(guard_nonnull=0) |
| 4759 |
|
| 4760 |
def test_loop_before_main_loop(self): |
| 4761 |
fdriver = JitDriver(greens=[], reds='auto') |
| 4762 |
gdriver = JitDriver(greens=[], reds='auto') |
| 4763 |
def f(i, j): |
| 4764 |
while j > 0: |
| 4765 |
j -= 1 |
| 4766 |
while i > 0: |
| 4767 |
fdriver.jit_merge_point() |
| 4768 |
i -= 1 |
| 4769 |
def g(i, j, k): |
| 4770 |
while k > 0: |
| 4771 |
gdriver.jit_merge_point() |
| 4772 |
f(i, j) |
| 4773 |
k -= 1 |
| 4774 |
|
| 4775 |
self.meta_interp(g, [5, 5, 5]) |
| 4776 |
self.check_resops(guard_true=10) |
| 4777 |
|
| 4778 |
def test_conditional_call_value(self): |
| 4779 |
from rpython.rlib.jit import conditional_call_elidable |
| 4780 |
def g(j): |
| 4781 |
return j + 5 |
| 4782 |
def f(i, j): |
| 4783 |
return conditional_call_elidable(i, g, j) |
| 4784 |
res = self.interp_operations(f, [-42, 200]) |
| 4785 |
assert res == -42 |
| 4786 |
res = self.interp_operations(f, [0, 200]) |
| 4787 |
assert res == 205 |
| 4788 |
|
| 4789 |
def test_ll_assert_not_none(self): |
| 4790 |
|
| 4791 |
|
| 4792 |
from rpython.rlib.debug import ll_assert_not_none |
| 4793 |
class X: |
| 4794 |
pass |
| 4795 |
class Y(X): |
| 4796 |
pass |
| 4797 |
def g(x, check): |
| 4798 |
if check: |
| 4799 |
x = ll_assert_not_none(x) |
| 4800 |
return isinstance(x, Y) |
| 4801 |
@dont_look_inside |
| 4802 |
def make(i): |
| 4803 |
if i == 1: |
| 4804 |
return X() |
| 4805 |
if i == 2: |
| 4806 |
return Y() |
| 4807 |
return None |
| 4808 |
def f(a, b, check): |
| 4809 |
return g(make(a), check) + g(make(b), check) * 10 |
| 4810 |
res = self.interp_operations(f, [1, 2, 1]) |
| 4811 |
assert res == 10 |
| 4812 |
self.check_operations_history(guard_nonnull=0, guard_nonnull_class=0, |
| 4813 |
guard_class=2, |
| 4814 |
assert_not_none=2) |
| 4815 |
|
| 4816 |
@pytest.mark.skipif(sys.platform=='darwin', reason='symbolics comparison breaks the untranslated optimizer') |
| 4817 |
def test_call_time_clock(self): |
| 4818 |
import time |
| 4819 |
def g(): |
| 4820 |
time.clock() |
| 4821 |
return 0 |
| 4822 |
self.interp_operations(g, []) |
| 4823 |
|
| 4824 |
def test_issue2465(self): |
| 4825 |
driver = JitDriver(greens=[], reds=['i', 'a', 'b']) |
| 4826 |
class F(object): |
| 4827 |
def __init__(self, floatval): |
| 4828 |
self.floatval = floatval |
| 4829 |
def f(i): |
| 4830 |
a = F(0.0) |
| 4831 |
b = None |
| 4832 |
while i > 0: |
| 4833 |
driver.jit_merge_point(i=i, a=a, b=b) |
| 4834 |
b = F(a.floatval / 1.) |
| 4835 |
i -= 1 |
| 4836 |
return i |
| 4837 |
|
| 4838 |
self.meta_interp(f, [10]) |
| 4839 |
|
| 4840 |
def test_finalizer_bug(self): |
| 4841 |
py.test.skip("loops!") |
| 4842 |
from rpython.rlib import rgc |
| 4843 |
driver = JitDriver(greens=[], reds=[]) |
| 4844 |
class Fin(object): |
| 4845 |
@rgc.must_be_light_finalizer |
| 4846 |
def __del__(self): |
| 4847 |
holder[0].field = 7 |
| 4848 |
class Un(object): |
| 4849 |
def __init__(self): |
| 4850 |
self.field = 0 |
| 4851 |
holder = [Un()] |
| 4852 |
|
| 4853 |
def f(): |
| 4854 |
while True: |
| 4855 |
driver.jit_merge_point() |
| 4856 |
holder[0].field = 0 |
| 4857 |
Fin() |
| 4858 |
if holder[0].field: |
| 4859 |
break |
| 4860 |
return holder[0].field |
| 4861 |
|
| 4862 |
f() |
| 4863 |
self.meta_interp(f, []) |
| 4864 |
|
| 4865 |
def test_trace_too_long_bug(self): |
| 4866 |
driver = JitDriver(greens=[], reds=['i']) |
| 4867 |
@unroll_safe |
| 4868 |
def match(s): |
| 4869 |
l = len(s) |
| 4870 |
p = 0 |
| 4871 |
for i in range(2500): |
| 4872 |
c = s[p] |
| 4873 |
if c != 'a': |
| 4874 |
return False |
| 4875 |
p += 1 |
| 4876 |
if p >= l: |
| 4877 |
return True |
| 4878 |
c = s[p] |
| 4879 |
if c != '\n': |
| 4880 |
p += 1 |
| 4881 |
if p >= l: |
| 4882 |
return True |
| 4883 |
else: |
| 4884 |
return False |
| 4885 |
return True |
| 4886 |
|
| 4887 |
def f(i): |
| 4888 |
while i > 0: |
| 4889 |
driver.jit_merge_point(i=i) |
| 4890 |
match('a' * (500 * i)) |
| 4891 |
i -= 1 |
| 4892 |
return i |
| 4893 |
|
| 4894 |
res = self.meta_interp(f, [10]) |
| 4895 |
assert res == f(10) |
| 4896 |
|
| 4897 |
def test_cached_info_missing(self): |
| 4898 |
py.test.skip("XXX hitting a non-translated assert in optimizeopt/heap.py, but seems not to hurt the rest") |
| 4899 |
driver = JitDriver(greens = [], |
| 4900 |
reds=['iterations', 'total', 'c', 'height', 'h']) |
| 4901 |
|
| 4902 |
class IntVal: |
| 4903 |
_immutable_fields_ = ['intval'] |
| 4904 |
def __init__(self, value): |
| 4905 |
self.intval = value |
| 4906 |
|
| 4907 |
def f(height, iterations): |
| 4908 |
height = IntVal(height) |
| 4909 |
c = IntVal(0) |
| 4910 |
h = height |
| 4911 |
total = IntVal(0) |
| 4912 |
|
| 4913 |
while True: |
| 4914 |
driver.jit_merge_point(iterations=iterations, |
| 4915 |
total=total, c=c, height=height, h=h) |
| 4916 |
if h.intval > 0: |
| 4917 |
h = IntVal(h.intval - 1) |
| 4918 |
total = IntVal(total.intval + 1) |
| 4919 |
else: |
| 4920 |
c = IntVal(c.intval + 1) |
| 4921 |
if c.intval >= iterations: |
| 4922 |
return total.intval |
| 4923 |
h = height |
| 4924 |
|
| 4925 |
res = self.meta_interp(f, [2, 200]) |
| 4926 |
assert res == f(2, 200) |
| 4927 |
|
| 4928 |
def test_issue2904(self): |
| 4929 |
driver = JitDriver(greens = [], |
| 4930 |
reds=['iterations', 'total', 'c', 'height', 'h']) |
| 4931 |
|
| 4932 |
def f(height, iterations): |
| 4933 |
set_param(driver, 'threshold', 4) |
| 4934 |
set_param(driver, 'trace_eagerness', 1) |
| 4935 |
c = 0 |
| 4936 |
h = height |
| 4937 |
total = 0 |
| 4938 |
|
| 4939 |
while True: |
| 4940 |
driver.jit_merge_point(iterations=iterations, |
| 4941 |
total=total, c=c, height=height, h=h) |
| 4942 |
if h != 0: |
| 4943 |
h = h - 1 |
| 4944 |
total = total + 1 |
| 4945 |
else: |
| 4946 |
c = c + 1 |
| 4947 |
if c >= iterations: |
| 4948 |
return total |
| 4949 |
h = height - 1 |
| 4950 |
|
| 4951 |
res = self.meta_interp(f, [2, 200]) |
| 4952 |
assert res == f(2, 200) |
| 4953 |
|
| 4954 |
def test_issue2926(self): |
| 4955 |
driver = JitDriver(greens = [], reds=['i', 'total', 'p']) |
| 4956 |
|
| 4957 |
class Base(object): |
| 4958 |
def do_stuff(self): |
| 4959 |
return 1000 |
| 4960 |
class Int(Base): |
| 4961 |
def __init__(self, intval): |
| 4962 |
self.intval = intval |
| 4963 |
def do_stuff(self): |
| 4964 |
return self.intval |
| 4965 |
class SubInt(Int): |
| 4966 |
pass |
| 4967 |
class Float(Base): |
| 4968 |
def __init__(self, floatval): |
| 4969 |
self.floatval = floatval |
| 4970 |
def do_stuff(self): |
| 4971 |
return int(self.floatval) |
| 4972 |
|
| 4973 |
prebuilt = [Int(i) for i in range(10)] |
| 4974 |
|
| 4975 |
@dont_look_inside |
| 4976 |
def forget_intbounds(i): |
| 4977 |
return i |
| 4978 |
|
| 4979 |
@dont_look_inside |
| 4980 |
def escape(p): |
| 4981 |
pass |
| 4982 |
|
| 4983 |
def f(i): |
| 4984 |
total = 0 |
| 4985 |
p = Base() |
| 4986 |
while True: |
| 4987 |
driver.jit_merge_point(i=i, total=total, p=p) |
| 4988 |
|
| 4989 |
if i == 13: |
| 4990 |
break |
| 4991 |
total += p.do_stuff() |
| 4992 |
j = forget_intbounds(i) |
| 4993 |
if j < 10: |
| 4994 |
p = prebuilt[i] |
| 4995 |
p.intval = j |
| 4996 |
elif j < 12: |
| 4997 |
p = Int(i) |
| 4998 |
else: |
| 4999 |
p = Float(3.14) |
| 5000 |
escape(p) |
| 5001 |
i += 1 |
| 5002 |
return total |
| 5003 |
|
| 5004 |
res = self.meta_interp(f, [0]) |
| 5005 |
assert res == f(0) |
| 5006 |
|
| 5007 |
def test_record_exact_class_nonconst(self): |
| 5008 |
class Base(object): |
| 5009 |
def f(self): |
| 5010 |
raise NotImplementedError |
| 5011 |
def g(self): |
| 5012 |
raise NotImplementedError |
| 5013 |
class A(Base): |
| 5014 |
def f(self): |
| 5015 |
return self.a |
| 5016 |
def g(self): |
| 5017 |
return self.a + 1 |
| 5018 |
class B(Base): |
| 5019 |
def f(self): |
| 5020 |
return self.b |
| 5021 |
def g(self): |
| 5022 |
return self.b + 1 |
| 5023 |
class C(B): |
| 5024 |
def f(self): |
| 5025 |
self.c += 1 |
| 5026 |
return self.c |
| 5027 |
def g(self): |
| 5028 |
return self.c + 1 |
| 5029 |
@dont_look_inside |
| 5030 |
def make(x): |
| 5031 |
if x > 0: |
| 5032 |
a = A() |
| 5033 |
a.a = x + 1 |
| 5034 |
elif x < 0: |
| 5035 |
a = B() |
| 5036 |
a.b = -x |
| 5037 |
else: |
| 5038 |
a = C() |
| 5039 |
a.c = 10 |
| 5040 |
return a, type(a) |
| 5041 |
def f(x): |
| 5042 |
a, cls = make(x) |
| 5043 |
record_exact_class(a, cls) |
| 5044 |
if x > 0: |
| 5045 |
z = a.f() |
| 5046 |
elif x < 0: |
| 5047 |
z = a.f() |
| 5048 |
else: |
| 5049 |
z = a.f() |
| 5050 |
return z + a.g() |
| 5051 |
res1 = f(6) |
| 5052 |
res2 = self.interp_operations(f, [6]) |
| 5053 |
assert res1 == res2 |
| 5054 |
self.check_operations_history(guard_class=1, record_exact_class=0) |