| 1 |
|
| 2 |
import py |
| 3 |
|
| 4 |
from rpython.rtyper.lltypesystem import lltype, llmemory |
| 5 |
from rpython.rtyper import rclass |
| 6 |
from rpython.rtyper.rclass import FieldListAccessor, IR_QUASIIMMUTABLE |
| 7 |
from rpython.jit.metainterp.quasiimmut import QuasiImmut |
| 8 |
from rpython.jit.metainterp.quasiimmut import get_current_qmut_instance |
| 9 |
from rpython.jit.metainterp.test.support import LLJitMixin |
| 10 |
from rpython.jit.codewriter.policy import StopAtXPolicy |
| 11 |
from rpython.rlib.jit import JitDriver, dont_look_inside, unroll_safe, promote |
| 12 |
|
| 13 |
|
| 14 |
def test_get_current_qmut_instance(): |
| 15 |
accessor = FieldListAccessor() |
| 16 |
accessor.initialize(None, {'inst_x': IR_QUASIIMMUTABLE}) |
| 17 |
STRUCT = lltype.GcStruct('Foo', ('inst_x', lltype.Signed), |
| 18 |
('mutate_x', rclass.OBJECTPTR), |
| 19 |
hints={'immutable_fields': accessor}) |
| 20 |
foo = lltype.malloc(STRUCT, zero=True) |
| 21 |
foo.inst_x = 42 |
| 22 |
assert not foo.mutate_x |
| 23 |
|
| 24 |
class FakeCPU: |
| 25 |
def bh_getfield_gc_r(self, gcref, fielddescr): |
| 26 |
assert fielddescr == mutatefielddescr |
| 27 |
foo = lltype.cast_opaque_ptr(lltype.Ptr(STRUCT), gcref) |
| 28 |
result = foo.mutate_x |
| 29 |
return lltype.cast_opaque_ptr(llmemory.GCREF, result) |
| 30 |
|
| 31 |
def bh_setfield_gc_r(self, gcref, newvalue_gcref, fielddescr): |
| 32 |
assert fielddescr == mutatefielddescr |
| 33 |
foo = lltype.cast_opaque_ptr(lltype.Ptr(STRUCT), gcref) |
| 34 |
newvalue = lltype.cast_opaque_ptr(rclass.OBJECTPTR, newvalue_gcref) |
| 35 |
foo.mutate_x = newvalue |
| 36 |
|
| 37 |
cpu = FakeCPU() |
| 38 |
mutatefielddescr = ('fielddescr', STRUCT, 'mutate_x') |
| 39 |
|
| 40 |
foo_gcref = lltype.cast_opaque_ptr(llmemory.GCREF, foo) |
| 41 |
qmut1 = get_current_qmut_instance(cpu, foo_gcref, mutatefielddescr) |
| 42 |
assert isinstance(qmut1, QuasiImmut) |
| 43 |
qmut2 = get_current_qmut_instance(cpu, foo_gcref, mutatefielddescr) |
| 44 |
assert qmut1 is qmut2 |
| 45 |
|
| 46 |
|
| 47 |
class QuasiImmutTests(object): |
| 48 |
|
| 49 |
def setup_method(self, meth): |
| 50 |
self.prev_compress_limit = QuasiImmut.compress_limit |
| 51 |
QuasiImmut.compress_limit = 1 |
| 52 |
|
| 53 |
def teardown_method(self, meth): |
| 54 |
QuasiImmut.compress_limit = self.prev_compress_limit |
| 55 |
|
| 56 |
def test_simple_1(self): |
| 57 |
myjitdriver = JitDriver(greens=['foo'], reds=['x', 'total']) |
| 58 |
class Foo: |
| 59 |
_immutable_fields_ = ['a?'] |
| 60 |
def __init__(self, a): |
| 61 |
self.a = a |
| 62 |
def f(a, x): |
| 63 |
foo = Foo(a) |
| 64 |
total = 0 |
| 65 |
while x > 0: |
| 66 |
myjitdriver.jit_merge_point(foo=foo, x=x, total=total) |
| 67 |
|
| 68 |
total += foo.a |
| 69 |
x -= 1 |
| 70 |
return total |
| 71 |
|
| 72 |
res = self.meta_interp(f, [100, 7]) |
| 73 |
assert res == 700 |
| 74 |
self.check_resops(guard_not_invalidated=2) |
| 75 |
|
| 76 |
from rpython.jit.metainterp.warmspot import get_stats |
| 77 |
loops = get_stats().loops |
| 78 |
for loop in loops: |
| 79 |
assert len(loop.quasi_immutable_deps) == 1 |
| 80 |
assert isinstance(loop.quasi_immutable_deps.keys()[0], QuasiImmut) |
| 81 |
|
| 82 |
def test_simple_optimize_during_tracing(self): |
| 83 |
myjitdriver = JitDriver(greens=['foo'], reds=['x', 'total']) |
| 84 |
class Foo: |
| 85 |
_immutable_fields_ = ['a?'] |
| 86 |
def __init__(self, a): |
| 87 |
self.a = a |
| 88 |
def f(a, x): |
| 89 |
foo = Foo(a) |
| 90 |
total = 0 |
| 91 |
while x > 0: |
| 92 |
myjitdriver.jit_merge_point(foo=foo, x=x, total=total) |
| 93 |
|
| 94 |
total += foo.a |
| 95 |
x -= 1 |
| 96 |
return total |
| 97 |
|
| 98 |
res = self.meta_interp(f, [100, 7], enable_opts="") |
| 99 |
assert res == 700 |
| 100 |
|
| 101 |
self.check_resops(guard_not_invalidated=1) |
| 102 |
|
| 103 |
def test_nonopt_1(self): |
| 104 |
myjitdriver = JitDriver(greens=[], reds=['x', 'total', 'lst']) |
| 105 |
class Foo: |
| 106 |
_immutable_fields_ = ['a?'] |
| 107 |
def __init__(self, a): |
| 108 |
self.a = a |
| 109 |
def setup(x): |
| 110 |
return [Foo(100 + i) for i in range(x)] |
| 111 |
def f(a, x): |
| 112 |
lst = setup(x) |
| 113 |
total = 0 |
| 114 |
while x > 0: |
| 115 |
myjitdriver.jit_merge_point(lst=lst, x=x, total=total) |
| 116 |
|
| 117 |
x -= 1 |
| 118 |
total += lst[x].a |
| 119 |
return total |
| 120 |
|
| 121 |
assert f(100, 7) == 721 |
| 122 |
res = self.meta_interp(f, [100, 7]) |
| 123 |
assert res == 721 |
| 124 |
self.check_resops(guard_not_invalidated=2, getfield_gc_r=1, getfield_gc_i=2) |
| 125 |
|
| 126 |
from rpython.jit.metainterp.warmspot import get_stats |
| 127 |
loops = get_stats().loops |
| 128 |
for loop in loops: |
| 129 |
assert loop.quasi_immutable_deps is None |
| 130 |
|
| 131 |
def test_opt_via_virtual_1(self): |
| 132 |
myjitdriver = JitDriver(greens=['foo'], reds=['x', 'total']) |
| 133 |
class Foo: |
| 134 |
_immutable_fields_ = ['a?'] |
| 135 |
def __init__(self, a): |
| 136 |
self.a = a |
| 137 |
class A: |
| 138 |
pass |
| 139 |
def f(a, x): |
| 140 |
foo = Foo(a) |
| 141 |
total = 0 |
| 142 |
while x > 0: |
| 143 |
myjitdriver.jit_merge_point(foo=foo, x=x, total=total) |
| 144 |
|
| 145 |
a = A() |
| 146 |
a.foo = foo |
| 147 |
foo = a.foo |
| 148 |
|
| 149 |
total += foo.a |
| 150 |
x -= 1 |
| 151 |
return total |
| 152 |
|
| 153 |
res = self.meta_interp(f, [100, 7]) |
| 154 |
assert res == 700 |
| 155 |
self.check_resops(guard_not_invalidated=2) |
| 156 |
|
| 157 |
def test_change_during_tracing_1(self): |
| 158 |
myjitdriver = JitDriver(greens=['foo'], reds=['x', 'total']) |
| 159 |
class Foo: |
| 160 |
_immutable_fields_ = ['a?'] |
| 161 |
def __init__(self, a): |
| 162 |
self.a = a |
| 163 |
@dont_look_inside |
| 164 |
def residual_call(foo): |
| 165 |
foo.a += 1 |
| 166 |
def f(a, x): |
| 167 |
foo = Foo(a) |
| 168 |
total = 0 |
| 169 |
while x > 0: |
| 170 |
myjitdriver.jit_merge_point(foo=foo, x=x, total=total) |
| 171 |
|
| 172 |
total += foo.a |
| 173 |
residual_call(foo) |
| 174 |
x -= 1 |
| 175 |
return total |
| 176 |
|
| 177 |
assert f(100, 7) == 721 |
| 178 |
res = self.meta_interp(f, [100, 7]) |
| 179 |
assert res == 721 |
| 180 |
|
| 181 |
self.check_aborted_count(2) |
| 182 |
|
| 183 |
def test_change_during_tracing_2(self): |
| 184 |
myjitdriver = JitDriver(greens=['foo'], reds=['x', 'total']) |
| 185 |
class Foo: |
| 186 |
_immutable_fields_ = ['a?'] |
| 187 |
def __init__(self, a): |
| 188 |
self.a = a |
| 189 |
@dont_look_inside |
| 190 |
def residual_call(foo, difference): |
| 191 |
foo.a += difference |
| 192 |
def f(a, x): |
| 193 |
foo = Foo(a) |
| 194 |
total = 0 |
| 195 |
while x > 0: |
| 196 |
myjitdriver.jit_merge_point(foo=foo, x=x, total=total) |
| 197 |
|
| 198 |
total += foo.a |
| 199 |
residual_call(foo, +1) |
| 200 |
residual_call(foo, -1) |
| 201 |
x -= 1 |
| 202 |
return total |
| 203 |
|
| 204 |
assert f(100, 7) == 700 |
| 205 |
res = self.meta_interp(f, [100, 7]) |
| 206 |
assert res == 700 |
| 207 |
self.check_resops(guard_not_invalidated=0) |
| 208 |
|
| 209 |
def test_change_invalidate_reentering(self): |
| 210 |
myjitdriver = JitDriver(greens=['foo'], reds=['x', 'total']) |
| 211 |
class Foo: |
| 212 |
_immutable_fields_ = ['a?'] |
| 213 |
def __init__(self, a): |
| 214 |
self.a = a |
| 215 |
def f(foo, x): |
| 216 |
total = 0 |
| 217 |
while x > 0: |
| 218 |
myjitdriver.jit_merge_point(foo=foo, x=x, total=total) |
| 219 |
|
| 220 |
total += foo.a |
| 221 |
x -= 1 |
| 222 |
return total |
| 223 |
def g(a, x): |
| 224 |
foo = Foo(a) |
| 225 |
res1 = f(foo, x) |
| 226 |
foo.a += 1 |
| 227 |
res2 = f(foo, x) |
| 228 |
return res1 * 1000 + res2 |
| 229 |
|
| 230 |
assert g(100, 7) == 700707 |
| 231 |
res = self.meta_interp(g, [100, 7]) |
| 232 |
assert res == 700707 |
| 233 |
self.check_resops(guard_not_invalidated=4) |
| 234 |
|
| 235 |
def test_invalidate_while_running(self): |
| 236 |
jitdriver = JitDriver(greens=['foo'], reds=['i', 'total']) |
| 237 |
|
| 238 |
class Foo(object): |
| 239 |
_immutable_fields_ = ['a?'] |
| 240 |
def __init__(self, a): |
| 241 |
self.a = a |
| 242 |
|
| 243 |
def external(foo, v): |
| 244 |
if v: |
| 245 |
foo.a = 2 |
| 246 |
|
| 247 |
def f(foo): |
| 248 |
i = 0 |
| 249 |
total = 0 |
| 250 |
while i < 10: |
| 251 |
jitdriver.jit_merge_point(i=i, foo=foo, total=total) |
| 252 |
external(foo, i > 7) |
| 253 |
i += 1 |
| 254 |
total += foo.a |
| 255 |
return total |
| 256 |
|
| 257 |
def g(): |
| 258 |
return f(Foo(1)) |
| 259 |
|
| 260 |
assert self.meta_interp(g, [], policy=StopAtXPolicy(external)) == g() |
| 261 |
|
| 262 |
def test_invalidate_by_setfield(self): |
| 263 |
jitdriver = JitDriver(greens=['bc', 'foo'], reds=['i', 'total']) |
| 264 |
|
| 265 |
class Foo(object): |
| 266 |
_immutable_fields_ = ['a?'] |
| 267 |
def __init__(self, a): |
| 268 |
self.a = a |
| 269 |
|
| 270 |
def f(foo, bc): |
| 271 |
i = 0 |
| 272 |
total = 0 |
| 273 |
while i < 10: |
| 274 |
jitdriver.jit_merge_point(bc=bc, i=i, foo=foo, total=total) |
| 275 |
if bc == 0: |
| 276 |
f(foo, 1) |
| 277 |
if bc == 1: |
| 278 |
foo.a = int(i > 5) |
| 279 |
i += 1 |
| 280 |
total += foo.a |
| 281 |
return total |
| 282 |
|
| 283 |
def g(): |
| 284 |
return f(Foo(1), 0) |
| 285 |
|
| 286 |
assert self.meta_interp(g, []) == g() |
| 287 |
|
| 288 |
def test_invalidate_bridge(self): |
| 289 |
jitdriver = JitDriver(greens=['foo'], reds=['i', 'total']) |
| 290 |
|
| 291 |
class Foo(object): |
| 292 |
_immutable_fields_ = ['a?'] |
| 293 |
|
| 294 |
def f(foo): |
| 295 |
i = 0 |
| 296 |
total = 0 |
| 297 |
while i < 10: |
| 298 |
jitdriver.jit_merge_point(i=i, total=total, foo=foo) |
| 299 |
if i > 5: |
| 300 |
total += foo.a |
| 301 |
else: |
| 302 |
total += 2*foo.a |
| 303 |
i += 1 |
| 304 |
return total |
| 305 |
|
| 306 |
def main(): |
| 307 |
foo = Foo() |
| 308 |
foo.a = 1 |
| 309 |
total = f(foo) |
| 310 |
foo.a = 2 |
| 311 |
total += f(foo) |
| 312 |
foo.a = 1 |
| 313 |
total += f(foo) |
| 314 |
return total |
| 315 |
|
| 316 |
res = self.meta_interp(main, []) |
| 317 |
self.check_trace_count(6) |
| 318 |
self.check_jitcell_token_count(3) |
| 319 |
assert res == main() |
| 320 |
|
| 321 |
def test_change_during_running(self): |
| 322 |
myjitdriver = JitDriver(greens=['foo'], reds=['x', 'total']) |
| 323 |
class Foo: |
| 324 |
_immutable_fields_ = ['a?'] |
| 325 |
def __init__(self, a): |
| 326 |
self.a = a |
| 327 |
@dont_look_inside |
| 328 |
def residual_call(foo, x): |
| 329 |
if x == 10: |
| 330 |
foo.a += 1 |
| 331 |
def f(a, x): |
| 332 |
foo = Foo(a) |
| 333 |
total = 0 |
| 334 |
while x > 0: |
| 335 |
myjitdriver.jit_merge_point(foo=foo, x=x, total=total) |
| 336 |
|
| 337 |
total += foo.a |
| 338 |
residual_call(foo, x) |
| 339 |
total += foo.a |
| 340 |
x -= 1 |
| 341 |
return total |
| 342 |
|
| 343 |
assert f(100, 30) == 6019 |
| 344 |
res = self.meta_interp(f, [100, 30]) |
| 345 |
assert res == 6019 |
| 346 |
self.check_resops(guard_not_invalidated=8, guard_not_forced=0, |
| 347 |
call_may_force=0) |
| 348 |
|
| 349 |
def test_list_simple_1(self): |
| 350 |
myjitdriver = JitDriver(greens=['foo'], reds=['x', 'total']) |
| 351 |
class Foo: |
| 352 |
_immutable_fields_ = ['lst?[*]'] |
| 353 |
def __init__(self, lst): |
| 354 |
self.lst = lst |
| 355 |
def f(a, x): |
| 356 |
lst1 = [0, 0] |
| 357 |
lst1[1] = a |
| 358 |
foo = Foo(lst1) |
| 359 |
total = 0 |
| 360 |
while x > 0: |
| 361 |
myjitdriver.jit_merge_point(foo=foo, x=x, total=total) |
| 362 |
|
| 363 |
total += foo.lst[1] |
| 364 |
x -= 1 |
| 365 |
return total |
| 366 |
|
| 367 |
res = self.meta_interp(f, [100, 7]) |
| 368 |
assert res == 700 |
| 369 |
self.check_resops(getarrayitem_gc_pure_i=0, guard_not_invalidated=2, |
| 370 |
getarrayitem_gc_pure_r=0, |
| 371 |
getarrayitem_gc_i=0, |
| 372 |
getarrayitem_gc_r=0, |
| 373 |
getfield_gc_i=0, getfield_gc_r=0) |
| 374 |
|
| 375 |
from rpython.jit.metainterp.warmspot import get_stats |
| 376 |
loops = get_stats().loops |
| 377 |
for loop in loops: |
| 378 |
assert len(loop.quasi_immutable_deps) == 1 |
| 379 |
assert isinstance(loop.quasi_immutable_deps.keys()[0], QuasiImmut) |
| 380 |
|
| 381 |
def test_list_optimized_while_tracing(self): |
| 382 |
myjitdriver = JitDriver(greens=['foo'], reds=['x', 'total']) |
| 383 |
class Foo: |
| 384 |
_immutable_fields_ = ['lst?[*]'] |
| 385 |
def __init__(self, lst): |
| 386 |
self.lst = lst |
| 387 |
def f(a, x): |
| 388 |
lst1 = [0, 0] |
| 389 |
lst1[1] = a |
| 390 |
foo = Foo(lst1) |
| 391 |
total = 0 |
| 392 |
while x > 0: |
| 393 |
myjitdriver.jit_merge_point(foo=foo, x=x, total=total) |
| 394 |
|
| 395 |
total += foo.lst[1] |
| 396 |
x -= 1 |
| 397 |
return total |
| 398 |
|
| 399 |
res = self.meta_interp(f, [100, 7], enable_opts="") |
| 400 |
assert res == 700 |
| 401 |
|
| 402 |
self.check_resops(getarrayitem_gc_pure_i=0, guard_not_invalidated=1, |
| 403 |
getarrayitem_gc_i=0, getfield_gc_i=0, getfield_gc_r=0) |
| 404 |
|
| 405 |
def test_list_length_1(self): |
| 406 |
myjitdriver = JitDriver(greens=['foo'], reds=['x', 'total']) |
| 407 |
class Foo: |
| 408 |
_immutable_fields_ = ['lst?[*]'] |
| 409 |
def __init__(self, lst): |
| 410 |
self.lst = lst |
| 411 |
class A: |
| 412 |
pass |
| 413 |
def f(a, x): |
| 414 |
lst1 = [0, 0] |
| 415 |
lst1[1] = a |
| 416 |
foo = Foo(lst1) |
| 417 |
total = 0 |
| 418 |
while x > 0: |
| 419 |
myjitdriver.jit_merge_point(foo=foo, x=x, total=total) |
| 420 |
|
| 421 |
a = A() |
| 422 |
a.foo = foo |
| 423 |
foo = a.foo |
| 424 |
|
| 425 |
total += foo.lst[1] |
| 426 |
|
| 427 |
total += len(foo.lst) |
| 428 |
x -= 1 |
| 429 |
return total |
| 430 |
|
| 431 |
res = self.meta_interp(f, [100, 7]) |
| 432 |
assert res == 714 |
| 433 |
self.check_resops(getarrayitem_gc_pure=0, guard_not_invalidated=2, |
| 434 |
arraylen_gc=0, getarrayitem_gc=0, getfield_gc=0) |
| 435 |
|
| 436 |
from rpython.jit.metainterp.warmspot import get_stats |
| 437 |
loops = get_stats().loops |
| 438 |
for loop in loops: |
| 439 |
assert len(loop.quasi_immutable_deps) == 1 |
| 440 |
assert isinstance(loop.quasi_immutable_deps.keys()[0], QuasiImmut) |
| 441 |
|
| 442 |
def test_list_pass_around(self): |
| 443 |
py.test.skip("think about a way to fix it") |
| 444 |
myjitdriver = JitDriver(greens=['foo'], reds=['x', 'total']) |
| 445 |
class Foo: |
| 446 |
_immutable_fields_ = ['lst?[*]'] |
| 447 |
def __init__(self, lst): |
| 448 |
self.lst = lst |
| 449 |
def g(lst): |
| 450 |
|
| 451 |
|
| 452 |
return lst[1] |
| 453 |
def f(a, x): |
| 454 |
lst1 = [0, 0] |
| 455 |
g(lst1) |
| 456 |
lst1[1] = a |
| 457 |
foo = Foo(lst1) |
| 458 |
total = 0 |
| 459 |
while x > 0: |
| 460 |
myjitdriver.jit_merge_point(foo=foo, x=x, total=total) |
| 461 |
|
| 462 |
total += g(foo.lst) |
| 463 |
x -= 1 |
| 464 |
return total |
| 465 |
|
| 466 |
res = self.meta_interp(f, [100, 7]) |
| 467 |
assert res == 700 |
| 468 |
self.check_resops(guard_not_invalidated=2, getfield_gc=0, |
| 469 |
getarrayitem_gc=0, getarrayitem_gc_pure=0) |
| 470 |
|
| 471 |
from rpython.jit.metainterp.warmspot import get_stats |
| 472 |
loops = get_stats().loops |
| 473 |
for loop in loops: |
| 474 |
assert len(loop.quasi_immutable_deps) == 1 |
| 475 |
assert isinstance(loop.quasi_immutable_deps.keys()[0], QuasiImmut) |
| 476 |
|
| 477 |
def test_list_change_during_running(self): |
| 478 |
myjitdriver = JitDriver(greens=['foo'], reds=['x', 'total']) |
| 479 |
class Foo: |
| 480 |
_immutable_fields_ = ['lst?[*]'] |
| 481 |
def __init__(self, lst): |
| 482 |
self.lst = lst |
| 483 |
@dont_look_inside |
| 484 |
def residual_call(foo, x): |
| 485 |
if x == 10: |
| 486 |
lst2 = [0, 0] |
| 487 |
lst2[1] = foo.lst[1] + 1 |
| 488 |
foo.lst = lst2 |
| 489 |
def f(a, x): |
| 490 |
lst1 = [0, 0] |
| 491 |
lst1[1] = a |
| 492 |
foo = Foo(lst1) |
| 493 |
total = 0 |
| 494 |
while x > 0: |
| 495 |
myjitdriver.jit_merge_point(foo=foo, x=x, total=total) |
| 496 |
|
| 497 |
total += foo.lst[1] |
| 498 |
residual_call(foo, x) |
| 499 |
total += foo.lst[1] |
| 500 |
x -= 1 |
| 501 |
return total |
| 502 |
|
| 503 |
assert f(100, 30) == 6019 |
| 504 |
res = self.meta_interp(f, [100, 30]) |
| 505 |
assert res == 6019 |
| 506 |
self.check_resops(call_may_force=0, getfield_gc=0, |
| 507 |
getarrayitem_gc_pure=0, guard_not_forced=0, |
| 508 |
getarrayitem_gc=0, guard_not_invalidated=8) |
| 509 |
|
| 510 |
def test_invalidated_loop_is_not_used_any_more_as_target(self): |
| 511 |
myjitdriver = JitDriver(greens=['foo'], reds=['x']) |
| 512 |
class Foo: |
| 513 |
_immutable_fields_ = ['step?'] |
| 514 |
@dont_look_inside |
| 515 |
def residual(x, foo): |
| 516 |
if x == 20: |
| 517 |
foo.step = 1 |
| 518 |
def f(x): |
| 519 |
foo = Foo() |
| 520 |
foo.step = 2 |
| 521 |
while x > 0: |
| 522 |
myjitdriver.jit_merge_point(foo=foo, x=x) |
| 523 |
residual(x, foo) |
| 524 |
x -= foo.step |
| 525 |
return foo.step |
| 526 |
res = self.meta_interp(f, [60]) |
| 527 |
assert res == 1 |
| 528 |
self.check_jitcell_token_count(2) |
| 529 |
|
| 530 |
def test_for_loop_array(self): |
| 531 |
myjitdriver = JitDriver(greens=[], reds=["n", "i"]) |
| 532 |
class Foo(object): |
| 533 |
_immutable_fields_ = ["x?[*]"] |
| 534 |
def __init__(self, x): |
| 535 |
self.x = x |
| 536 |
f = Foo([1, 3, 5, 6]) |
| 537 |
@unroll_safe |
| 538 |
def g(v): |
| 539 |
for x in f.x: |
| 540 |
if x & 1 == 0: |
| 541 |
v += 1 |
| 542 |
return v |
| 543 |
def main(n): |
| 544 |
i = 0 |
| 545 |
while i < n: |
| 546 |
myjitdriver.jit_merge_point(n=n, i=i) |
| 547 |
i = g(i) |
| 548 |
return i |
| 549 |
res = self.meta_interp(main, [10]) |
| 550 |
assert res == 10 |
| 551 |
self.check_resops({ |
| 552 |
"int_add": 2, "int_lt": 2, "jump": 1, "guard_true": 2, |
| 553 |
"guard_not_invalidated": 2 |
| 554 |
}) |
| 555 |
|
| 556 |
def test_issue1080(self): |
| 557 |
myjitdriver = JitDriver(greens=[], reds=["n", "sa", "a"]) |
| 558 |
class Foo(object): |
| 559 |
_immutable_fields_ = ["x?"] |
| 560 |
def __init__(self, x): |
| 561 |
self.x = x |
| 562 |
one, two = Foo(1), Foo(2) |
| 563 |
def main(n): |
| 564 |
sa = 0 |
| 565 |
a = one |
| 566 |
while n: |
| 567 |
myjitdriver.jit_merge_point(n=n, sa=sa, a=a) |
| 568 |
sa += a.x |
| 569 |
if a.x == 1: |
| 570 |
a = two |
| 571 |
elif a.x == 2: |
| 572 |
a = one |
| 573 |
n -= 1 |
| 574 |
return sa |
| 575 |
res = self.meta_interp(main, [10]) |
| 576 |
assert res == main(10) |
| 577 |
|
| 578 |
def test_dont_emit_too_many_guard_not_invalidated(self): |
| 579 |
myjitdriver = JitDriver(greens=['foo'], reds=['x', 'total']) |
| 580 |
class Foo: |
| 581 |
_immutable_fields_ = ['a?', 'b?', 'c?'] |
| 582 |
def __init__(self, a): |
| 583 |
self.a = a |
| 584 |
self.b = a - 1 |
| 585 |
self.c = a - 3 |
| 586 |
def f(a, x): |
| 587 |
foo = Foo(a) |
| 588 |
total = 0 |
| 589 |
while x > 0: |
| 590 |
myjitdriver.jit_merge_point(foo=foo, x=x, total=total) |
| 591 |
|
| 592 |
total += foo.a + foo.b + foo.c |
| 593 |
x -= 1 |
| 594 |
return total |
| 595 |
|
| 596 |
res = self.meta_interp(f, [100, 7], enable_opts="") |
| 597 |
assert res == f(100, 7) |
| 598 |
|
| 599 |
self.check_resops(guard_not_invalidated=1) |
| 600 |
|
| 601 |
|
| 602 |
class TestLLtypeGreenFieldsTests(QuasiImmutTests, LLJitMixin): |
| 603 |
pass |