| 1 |
|
| 2 |
|
| 3 |
import pytest |
| 4 |
|
| 5 |
import math |
| 6 |
|
| 7 |
from rpython.rlib import jit |
| 8 |
from rpython.jit.metainterp.test.support import LLJitMixin |
| 9 |
from rpython.jit.metainterp.optimizeopt.bridgeopt import serialize_optimizer_knowledge |
| 10 |
from rpython.jit.metainterp.optimizeopt.bridgeopt import deserialize_optimizer_knowledge |
| 11 |
from rpython.jit.metainterp.resoperation import InputArgRef, InputArgInt |
| 12 |
from rpython.jit.metainterp.resume import NumberingState |
| 13 |
from rpython.jit.metainterp.resumecode import unpack_numbering |
| 14 |
from rpython.jit.metainterp.optimizeopt.info import InstancePtrInfo |
| 15 |
|
| 16 |
from hypothesis import strategies, given |
| 17 |
|
| 18 |
|
| 19 |
class FakeCPU(object): |
| 20 |
def __init__(self, dct): |
| 21 |
self.dct = dct |
| 22 |
|
| 23 |
def cls_of_box(self, box): |
| 24 |
return self.dct[box] |
| 25 |
|
| 26 |
class FakeOptimizer(object): |
| 27 |
metainterp_sd = None |
| 28 |
optheap = None |
| 29 |
optrewrite = None |
| 30 |
|
| 31 |
def __init__(self, cpu=None): |
| 32 |
self.constant_classes = {} |
| 33 |
self.cpu = cpu |
| 34 |
|
| 35 |
def make_constant_class(self, arg, cls): |
| 36 |
self.constant_classes[arg] = cls |
| 37 |
|
| 38 |
class FakeClass(object): |
| 39 |
pass |
| 40 |
|
| 41 |
class FakeStorage(object): |
| 42 |
def __init__(self, numb): |
| 43 |
self.rd_numb = numb |
| 44 |
|
| 45 |
def test_known_classes(): |
| 46 |
cls = FakeClass() |
| 47 |
box1 = InputArgRef() |
| 48 |
box1.set_forwarded(InstancePtrInfo(known_class=cls)) |
| 49 |
box2 = InputArgRef() |
| 50 |
box3 = InputArgRef() |
| 51 |
optimizer = FakeOptimizer() |
| 52 |
|
| 53 |
numb_state = NumberingState(4) |
| 54 |
numb_state.append_int(1) |
| 55 |
liveboxes = [InputArgInt(), box2, box1, box3] |
| 56 |
|
| 57 |
serialize_optimizer_knowledge(optimizer, numb_state, liveboxes, {}, None) |
| 58 |
|
| 59 |
assert unpack_numbering(numb_state.create_numbering()) == [ |
| 60 |
1, 0b010000, 0, 0, 0] |
| 61 |
|
| 62 |
rbox1 = InputArgRef() |
| 63 |
rbox2 = InputArgRef() |
| 64 |
rbox3 = InputArgRef() |
| 65 |
after_optimizer = FakeOptimizer(cpu=FakeCPU({rbox1: cls})) |
| 66 |
deserialize_optimizer_knowledge( |
| 67 |
after_optimizer, FakeStorage(numb_state.create_numbering()), |
| 68 |
[InputArgInt(), rbox2, rbox1, rbox3], liveboxes) |
| 69 |
assert box1 in after_optimizer.constant_classes |
| 70 |
assert box2 not in after_optimizer.constant_classes |
| 71 |
assert box3 not in after_optimizer.constant_classes |
| 72 |
|
| 73 |
|
| 74 |
box_strategy = strategies.builds(InputArgInt) | strategies.builds(InputArgRef) |
| 75 |
def _make_tup(box, known_class): |
| 76 |
if isinstance(box, InputArgInt): |
| 77 |
known_class = False |
| 78 |
return box, known_class |
| 79 |
tuples = strategies.builds(_make_tup, box_strategy, strategies.booleans()) |
| 80 |
boxes_known_classes = strategies.lists(tuples, min_size=1) |
| 81 |
|
| 82 |
@given(boxes_known_classes) |
| 83 |
def test_random_class_knowledge(boxes_known_classes): |
| 84 |
cls = FakeClass() |
| 85 |
for box, known_class in boxes_known_classes: |
| 86 |
if known_class: |
| 87 |
box.set_forwarded(InstancePtrInfo(known_class=cls)) |
| 88 |
optimizer = FakeOptimizer() |
| 89 |
|
| 90 |
refboxes = [box for (box, _) in boxes_known_classes |
| 91 |
if isinstance(box, InputArgRef)] |
| 92 |
|
| 93 |
numb_state = NumberingState(1) |
| 94 |
numb_state.append_int(1) |
| 95 |
liveboxes = [box for (box, _) in boxes_known_classes] |
| 96 |
|
| 97 |
serialize_optimizer_knowledge(optimizer, numb_state, liveboxes, {}, None) |
| 98 |
|
| 99 |
assert len(numb_state.create_numbering().code) == 4 + math.ceil(len(refboxes) / 6.0) |
| 100 |
|
| 101 |
dct = {box: cls |
| 102 |
for box, known_class in boxes_known_classes |
| 103 |
if known_class} |
| 104 |
after_optimizer = FakeOptimizer(cpu=FakeCPU(dct)) |
| 105 |
deserialize_optimizer_knowledge( |
| 106 |
after_optimizer, FakeStorage(numb_state.create_numbering()), |
| 107 |
liveboxes, liveboxes) |
| 108 |
for box, known_class in boxes_known_classes: |
| 109 |
assert (box in after_optimizer.constant_classes) == known_class |
| 110 |
|
| 111 |
class TestOptBridge(LLJitMixin): |
| 112 |
|
| 113 |
def test_bridge_guard_class(self): |
| 114 |
myjitdriver = jit.JitDriver(greens=[], reds=['y', 'res', 'n', 'a']) |
| 115 |
class A(object): |
| 116 |
def f(self): |
| 117 |
return 1 |
| 118 |
class B(A): |
| 119 |
def f(self): |
| 120 |
return 2 |
| 121 |
def f(x, y, n): |
| 122 |
if x: |
| 123 |
a = A() |
| 124 |
else: |
| 125 |
a = B() |
| 126 |
a.x = 0 |
| 127 |
res = 0 |
| 128 |
while y > 0: |
| 129 |
myjitdriver.jit_merge_point(y=y, n=n, res=res, a=a) |
| 130 |
res += a.f() |
| 131 |
a.x += 1 |
| 132 |
if y > n: |
| 133 |
res += 1 |
| 134 |
res += a.f() |
| 135 |
y -= 1 |
| 136 |
return res |
| 137 |
res = self.meta_interp(f, [6, 32, 16]) |
| 138 |
assert res == f(6, 32, 16) |
| 139 |
self.check_trace_count(3) |
| 140 |
self.check_resops(guard_class=1) |
| 141 |
|
| 142 |
def test_bridge_guard_class_return(self): |
| 143 |
myjitdriver = jit.JitDriver(greens=[], reds=['y', 'res', 'n', 'a']) |
| 144 |
class A(object): |
| 145 |
def f(self): |
| 146 |
return 1 |
| 147 |
class B(A): |
| 148 |
def f(self): |
| 149 |
return 2 |
| 150 |
def f(x, y, n): |
| 151 |
if x: |
| 152 |
a = A() |
| 153 |
else: |
| 154 |
a = B() |
| 155 |
a.x = 0 |
| 156 |
res = 0 |
| 157 |
while y > 0: |
| 158 |
myjitdriver.jit_merge_point(y=y, n=n, res=res, a=a) |
| 159 |
res += a.f() |
| 160 |
a.x += 1 |
| 161 |
if y < n: |
| 162 |
res += 1 |
| 163 |
res += a.f() |
| 164 |
return res |
| 165 |
res += a.f() |
| 166 |
y -= 1 |
| 167 |
return res |
| 168 |
def g(i): |
| 169 |
res = 0 |
| 170 |
for i in range(i): |
| 171 |
res += f(6, 32, 16-i) |
| 172 |
res1 = g(10) |
| 173 |
res2 = self.meta_interp(g, [10]) |
| 174 |
assert res1 == res2 |
| 175 |
self.check_trace_count(2) |
| 176 |
self.check_resops(guard_class=1, omit_finish=False) |
| 177 |
|
| 178 |
def test_bridge_field_read(self): |
| 179 |
myjitdriver = jit.JitDriver(greens=[], reds=['y', 'res', 'n', 'a']) |
| 180 |
class A(object): |
| 181 |
pass |
| 182 |
class M(object): |
| 183 |
_immutable_fields_ = ['x'] |
| 184 |
def __init__(self, x): |
| 185 |
self.x = x |
| 186 |
|
| 187 |
m1 = M(1) |
| 188 |
m2 = M(2) |
| 189 |
def f(x, y, n): |
| 190 |
a = A() |
| 191 |
a.n = n |
| 192 |
if x: |
| 193 |
a.m = m1 |
| 194 |
else: |
| 195 |
a.m = m2 |
| 196 |
a.x = 0 |
| 197 |
res = 0 |
| 198 |
while y > 0: |
| 199 |
myjitdriver.jit_merge_point(y=y, n=n, res=res, a=a) |
| 200 |
n1 = a.n |
| 201 |
m = jit.promote(a.m) |
| 202 |
res += m.x |
| 203 |
a.x += 1 |
| 204 |
if y > n: |
| 205 |
res += 1 |
| 206 |
m = jit.promote(a.m) |
| 207 |
res += m.x |
| 208 |
res += n1 + a.n |
| 209 |
y -= 1 |
| 210 |
return res |
| 211 |
res = self.meta_interp(f, [6, 32, 16]) |
| 212 |
assert res == f(6, 32, 16) |
| 213 |
self.check_trace_count(3) |
| 214 |
self.check_resops(guard_value=1) |
| 215 |
self.check_resops(getfield_gc_i=4) |
| 216 |
self.check_resops(getfield_gc_r=1) |
| 217 |
|
| 218 |
def test_bridge_field_read_constants(self): |
| 219 |
myjitdriver = jit.JitDriver(greens=[], reds=['y', 'res', 'n']) |
| 220 |
class A(object): |
| 221 |
pass |
| 222 |
class M(object): |
| 223 |
_immutable_fields_ = ['x'] |
| 224 |
def __init__(self, x): |
| 225 |
self.x = x |
| 226 |
|
| 227 |
m1 = M(1) |
| 228 |
m2 = M(2) |
| 229 |
a = A() |
| 230 |
a.m = m1 |
| 231 |
a.n = 0 |
| 232 |
def f(x, y, n): |
| 233 |
if x: |
| 234 |
a.m = m1 |
| 235 |
a.n = n |
| 236 |
else: |
| 237 |
a.m = m2 |
| 238 |
a.n = n |
| 239 |
a.x = 0 |
| 240 |
res = 0 |
| 241 |
while y > 0: |
| 242 |
myjitdriver.jit_merge_point(y=y, n=n, res=res) |
| 243 |
n1 = a.n |
| 244 |
m = jit.promote(a.m) |
| 245 |
res += m.x |
| 246 |
a.x += 1 |
| 247 |
if y > n: |
| 248 |
res += 1 |
| 249 |
m = jit.promote(a.m) |
| 250 |
res += m.x |
| 251 |
res += n1 + a.n |
| 252 |
y -= 1 |
| 253 |
return res |
| 254 |
res = self.meta_interp(f, [6, 32, 16]) |
| 255 |
assert res == f(6, 32, 16) |
| 256 |
self.check_trace_count(3) |
| 257 |
self.check_resops(guard_value=1) |
| 258 |
self.check_resops(getfield_gc_i=4) |
| 259 |
self.check_resops(getfield_gc_r=1) |
| 260 |
|
| 261 |
def test_bridge_array_read(self): |
| 262 |
myjitdriver = jit.JitDriver(greens=[], reds=['y', 'res', 'n', 'a']) |
| 263 |
def f(x, y, n): |
| 264 |
if x: |
| 265 |
a = [1, n, 0] |
| 266 |
else: |
| 267 |
a = [2, n, 0] |
| 268 |
res = 0 |
| 269 |
while y > 0: |
| 270 |
myjitdriver.jit_merge_point(y=y, n=n, res=res, a=a) |
| 271 |
n1 = a[1] |
| 272 |
m = jit.promote(a[0]) |
| 273 |
res += m |
| 274 |
a[2] += 1 |
| 275 |
if y > n: |
| 276 |
res += 1 |
| 277 |
m = jit.promote(a[0]) |
| 278 |
res += m |
| 279 |
res += n1 + a[1] |
| 280 |
y -= 1 |
| 281 |
return res |
| 282 |
res = self.meta_interp(f, [6, 32, 16]) |
| 283 |
assert res == f(6, 32, 16) |
| 284 |
self.check_trace_count(3) |
| 285 |
self.check_resops(guard_value=1) |
| 286 |
self.check_resops(getarrayitem_gc_i=4) |
| 287 |
|
| 288 |
def test_bridge_array_read_dont_crash_on_huge_index(self): |
| 289 |
myjitdriver = jit.JitDriver(greens=[], reds=['y', 'res', 'n', 'a']) |
| 290 |
def f(x, y, n): |
| 291 |
a = [0] * 40000 |
| 292 |
if x: |
| 293 |
a[37000] = 1 |
| 294 |
a[38000] = n |
| 295 |
a[39000] = 0 |
| 296 |
else: |
| 297 |
a[37000] = 2 |
| 298 |
a[38000] = n |
| 299 |
a[39000] = 0 |
| 300 |
res = 0 |
| 301 |
while y > 0: |
| 302 |
myjitdriver.jit_merge_point(y=y, n=n, res=res, a=a) |
| 303 |
n1 = a[38000] |
| 304 |
m = jit.promote(a[37000]) |
| 305 |
res += m |
| 306 |
a[39000] += 1 |
| 307 |
if y > n: |
| 308 |
res += 1 |
| 309 |
m = jit.promote(a[37000]) |
| 310 |
res += m |
| 311 |
res += n1 + a[1] |
| 312 |
y -= 1 |
| 313 |
return res |
| 314 |
res = self.meta_interp(f, [6, 32, 16]) |
| 315 |
assert res == f(6, 32, 16) |
| 316 |
self.check_trace_count(3) |
| 317 |
|
| 318 |
self.check_resops(guard_value=3) |
| 319 |
self.check_resops(getarrayitem_gc_i=10) |
| 320 |
|
| 321 |
def test_bridge_array_read_constant(self): |
| 322 |
myjitdriver = jit.JitDriver(greens=[], reds=['y', 'res', 'n']) |
| 323 |
class A(object): |
| 324 |
pass |
| 325 |
a = A() |
| 326 |
a.l = [1, -65, 0] |
| 327 |
def f(x, y, n): |
| 328 |
if x: |
| 329 |
a.l[0] = 1 |
| 330 |
else: |
| 331 |
a.l[0] = 2 |
| 332 |
a.l[1] = n |
| 333 |
a.l[2] = 0 |
| 334 |
res = 0 |
| 335 |
while y > 0: |
| 336 |
myjitdriver.jit_merge_point(y=y, n=n, res=res) |
| 337 |
n1 = a.l[1] |
| 338 |
m = jit.promote(a.l[0]) |
| 339 |
res += m |
| 340 |
a.l[2] += 1 |
| 341 |
if y > n: |
| 342 |
res += 1 |
| 343 |
m = jit.promote(a.l[0]) |
| 344 |
res += m |
| 345 |
res += n1 + a.l[1] |
| 346 |
y -= 1 |
| 347 |
return res |
| 348 |
res = self.meta_interp(f, [6, 32, 16]) |
| 349 |
assert res == f(6, 32, 16) |
| 350 |
self.check_trace_count(3) |
| 351 |
self.check_resops(guard_value=1) |
| 352 |
self.check_resops(getarrayitem_gc_i=5) |
| 353 |
|
| 354 |
def test_bridge_call_loopinvariant(self): |
| 355 |
class A(object): |
| 356 |
pass |
| 357 |
class B(object): |
| 358 |
pass |
| 359 |
|
| 360 |
aholder = B() |
| 361 |
aholder.a = A() |
| 362 |
|
| 363 |
@jit.loop_invariant |
| 364 |
def get(): |
| 365 |
return aholder.a |
| 366 |
|
| 367 |
myjitdriver = jit.JitDriver(greens=[], reds=['y', 'res', 'n']) |
| 368 |
def f(x, y, n): |
| 369 |
if x == 10001121: |
| 370 |
aholder.a = A() |
| 371 |
if x: |
| 372 |
get().x = 1 |
| 373 |
else: |
| 374 |
get().x = 2 |
| 375 |
res = 0 |
| 376 |
while y > 0: |
| 377 |
myjitdriver.jit_merge_point(y=y, n=n, res=res) |
| 378 |
a = get() |
| 379 |
a = get() |
| 380 |
res += a.x |
| 381 |
if y > n: |
| 382 |
res += 1 |
| 383 |
res += get().x + a.x |
| 384 |
y -= 1 |
| 385 |
return res |
| 386 |
res = self.meta_interp(f, [6, 32, 16]) |
| 387 |
self.check_trace_count(3) |
| 388 |
self.check_resops(call_r=1) |
| 389 |
|
| 390 |
@pytest.mark.xfail() |
| 391 |
def test_bridge_call_loopinvariant_2(self): |
| 392 |
class A(object): |
| 393 |
pass |
| 394 |
class B(object): |
| 395 |
pass |
| 396 |
|
| 397 |
aholder = B() |
| 398 |
aholder.a = A() |
| 399 |
|
| 400 |
@jit.loop_invariant |
| 401 |
def get(): |
| 402 |
return aholder.a |
| 403 |
|
| 404 |
myjitdriver = jit.JitDriver(greens=[], reds=['y', 'res', 'n']) |
| 405 |
def f(x, y, n): |
| 406 |
if x == 10001121: |
| 407 |
aholder.a = A() |
| 408 |
if x: |
| 409 |
get().x = 1 |
| 410 |
else: |
| 411 |
get().x = 2 |
| 412 |
res = 0 |
| 413 |
while y > 0: |
| 414 |
myjitdriver.jit_merge_point(y=y, n=n, res=res) |
| 415 |
if y > n: |
| 416 |
res += get().x |
| 417 |
res += 1 |
| 418 |
res += get().x |
| 419 |
y -= 1 |
| 420 |
return res |
| 421 |
res = self.meta_interp(f, [6, 32, 16]) |
| 422 |
self.check_trace_count(3) |
| 423 |
self.check_resops(call_r=1) |