| 1 |
import pytest |
| 2 |
|
| 3 |
from rpython.conftest import option |
| 4 |
from rpython.annotator.model import UnionError |
| 5 |
from rpython.rlib.jit import (hint, we_are_jitted, JitDriver, elidable_promote, |
| 6 |
JitHintError, oopspec, isconstant, conditional_call, |
| 7 |
elidable, unroll_safe, dont_look_inside, conditional_call_elidable, |
| 8 |
enter_portal_frame, leave_portal_frame, record_known_result, |
| 9 |
record_exact_value) |
| 10 |
from rpython.rlib.rarithmetic import r_uint |
| 11 |
from rpython.rtyper.test.tool import BaseRtypingTest |
| 12 |
from rpython.rtyper.lltypesystem import lltype |
| 13 |
from rpython.translator.translator import TranslationContext |
| 14 |
from rpython.rtyper.annlowlevel import MixLevelHelperAnnotator |
| 15 |
from rpython.annotator import model as annmodel |
| 16 |
|
| 17 |
|
| 18 |
def test_oopspec(): |
| 19 |
@oopspec('foobar') |
| 20 |
def fn(): |
| 21 |
pass |
| 22 |
assert fn.oopspec == 'foobar' |
| 23 |
|
| 24 |
def test_jitdriver_autoreds(): |
| 25 |
driver = JitDriver(greens=['foo'], reds='auto') |
| 26 |
assert driver.autoreds |
| 27 |
assert driver.reds == [] |
| 28 |
assert driver.numreds is None |
| 29 |
with pytest.raises(TypeError): |
| 30 |
driver.can_enter_jit(foo='something') |
| 31 |
with pytest.raises(AssertionError): |
| 32 |
JitDriver(greens=['foo'], reds='auto', confirm_enter_jit='something') |
| 33 |
|
| 34 |
def test_jitdriver_numreds(): |
| 35 |
driver = JitDriver(greens=['foo'], reds=['a', 'b']) |
| 36 |
assert driver.reds == ['a', 'b'] |
| 37 |
assert driver.numreds == 2 |
| 38 |
|
| 39 |
class MyJitDriver(JitDriver): |
| 40 |
greens = ['foo'] |
| 41 |
reds = ['a', 'b'] |
| 42 |
driver = MyJitDriver() |
| 43 |
assert driver.reds == ['a', 'b'] |
| 44 |
assert driver.numreds == 2 |
| 45 |
|
| 46 |
def test_jitdriver_inline(): |
| 47 |
pytest.skip("@inline off: see skipped failures in test_warmspot.") |
| 48 |
driver = JitDriver(greens=[], reds='auto') |
| 49 |
calls = [] |
| 50 |
def foo(a, b): |
| 51 |
calls.append(('foo', a, b)) |
| 52 |
|
| 53 |
@driver.inline(foo) |
| 54 |
def bar(a, b): |
| 55 |
calls.append(('bar', a, b)) |
| 56 |
return a+b |
| 57 |
|
| 58 |
assert bar._inline_jit_merge_point_ is foo |
| 59 |
assert driver.inline_jit_merge_point |
| 60 |
assert bar(40, 2) == 42 |
| 61 |
assert calls == [ |
| 62 |
('foo', 40, 2), |
| 63 |
('bar', 40, 2), |
| 64 |
] |
| 65 |
|
| 66 |
def test_jitdriver_clone(): |
| 67 |
pytest.skip("@inline off: see skipped failures in test_warmspot.") |
| 68 |
def bar(): pass |
| 69 |
def foo(): pass |
| 70 |
driver = JitDriver(greens=[], reds=[]) |
| 71 |
with pytest.raises(AssertionError): |
| 72 |
driver.inline(bar)(foo) |
| 73 |
|
| 74 |
driver = JitDriver(greens=[], reds='auto') |
| 75 |
with pytest.raises(AssertionError): |
| 76 |
driver.clone() |
| 77 |
foo = driver.inline(bar)(foo) |
| 78 |
assert foo._inline_jit_merge_point_ == bar |
| 79 |
|
| 80 |
driver.foo = 'bar' |
| 81 |
driver2 = driver.clone() |
| 82 |
assert driver is not driver2 |
| 83 |
assert driver2.foo == 'bar' |
| 84 |
driver.foo = 'xxx' |
| 85 |
assert driver2.foo == 'bar' |
| 86 |
|
| 87 |
|
| 88 |
def test_merge_enter_different(): |
| 89 |
myjitdriver = JitDriver(greens=[], reds=['n']) |
| 90 |
def fn(n): |
| 91 |
while n > 0: |
| 92 |
myjitdriver.jit_merge_point(n=n) |
| 93 |
myjitdriver.can_enter_jit(n=n) |
| 94 |
n -= 1 |
| 95 |
return n |
| 96 |
with pytest.raises(JitHintError): |
| 97 |
fn(100) |
| 98 |
|
| 99 |
myjitdriver = JitDriver(greens=['n'], reds=[]) |
| 100 |
with pytest.raises(JitHintError): |
| 101 |
fn(100) |
| 102 |
|
| 103 |
def test_invalid_hint_combinations_error(): |
| 104 |
with pytest.raises(TypeError): |
| 105 |
@unroll_safe |
| 106 |
@elidable |
| 107 |
def f(): |
| 108 |
pass |
| 109 |
with pytest.raises(TypeError): |
| 110 |
@unroll_safe |
| 111 |
@elidable |
| 112 |
def f(): |
| 113 |
pass |
| 114 |
with pytest.raises(TypeError): |
| 115 |
@unroll_safe |
| 116 |
@dont_look_inside |
| 117 |
def f(): |
| 118 |
pass |
| 119 |
with pytest.raises(TypeError): |
| 120 |
@unroll_safe |
| 121 |
@dont_look_inside |
| 122 |
def f(): |
| 123 |
pass |
| 124 |
|
| 125 |
def test_record_exact_value(): |
| 126 |
class A(object): |
| 127 |
pass |
| 128 |
a = A() |
| 129 |
b = A() |
| 130 |
record_exact_value(a,a) |
| 131 |
with pytest.raises(AssertionError): |
| 132 |
record_exact_value(a, b) |
| 133 |
|
| 134 |
class TestJIT(BaseRtypingTest): |
| 135 |
def test_hint(self): |
| 136 |
def f(): |
| 137 |
x = hint(5, hello="world") |
| 138 |
return x |
| 139 |
res = self.interpret(f, []) |
| 140 |
assert res == 5 |
| 141 |
|
| 142 |
def test_we_are_jitted(self): |
| 143 |
def f(x): |
| 144 |
try: |
| 145 |
if we_are_jitted(): |
| 146 |
return x |
| 147 |
return x + 1 |
| 148 |
except Exception: |
| 149 |
return 5 |
| 150 |
res = self.interpret(f, [4]) |
| 151 |
assert res == 5 |
| 152 |
|
| 153 |
def test_elidable_promote(self): |
| 154 |
@elidable_promote() |
| 155 |
def g(func): |
| 156 |
return func + 1 |
| 157 |
def f(x): |
| 158 |
return g(x * 2) |
| 159 |
res = self.interpret(f, [2]) |
| 160 |
assert res == 5 |
| 161 |
|
| 162 |
def test_elidable_promote_args(self): |
| 163 |
@elidable_promote(promote_args='0') |
| 164 |
def g(func, x): |
| 165 |
return func + 1 |
| 166 |
def f(x): |
| 167 |
return g(x * 2, x) |
| 168 |
|
| 169 |
import dis |
| 170 |
from StringIO import StringIO |
| 171 |
import sys |
| 172 |
|
| 173 |
s = StringIO() |
| 174 |
prev = sys.stdout |
| 175 |
sys.stdout = s |
| 176 |
try: |
| 177 |
dis.dis(g) |
| 178 |
finally: |
| 179 |
sys.stdout = prev |
| 180 |
x = s.getvalue().find('CALL_FUNCTION') |
| 181 |
assert x != -1 |
| 182 |
x = s.getvalue().find('CALL_FUNCTION', x) |
| 183 |
assert x != -1 |
| 184 |
x = s.getvalue().find('CALL_FUNCTION', x) |
| 185 |
assert x != -1 |
| 186 |
res = self.interpret(f, [2]) |
| 187 |
assert res == 5 |
| 188 |
|
| 189 |
def test_annotate_hooks(self): |
| 190 |
|
| 191 |
def get_printable_location(m): pass |
| 192 |
|
| 193 |
myjitdriver = JitDriver(greens=['m'], reds=['n'], |
| 194 |
get_printable_location=get_printable_location) |
| 195 |
def fn(n): |
| 196 |
m = 42.5 |
| 197 |
while n > 0: |
| 198 |
myjitdriver.can_enter_jit(m=m, n=n) |
| 199 |
myjitdriver.jit_merge_point(m=m, n=n) |
| 200 |
n -= 1 |
| 201 |
return n |
| 202 |
|
| 203 |
t, rtyper, fngraph = self.gengraph(fn, [int]) |
| 204 |
|
| 205 |
|
| 206 |
assert fn._dont_reach_me_in_del_ == True |
| 207 |
|
| 208 |
def getargs(func): |
| 209 |
for graph in t.graphs: |
| 210 |
if getattr(graph, 'func', None) is func: |
| 211 |
return [v.concretetype for v in graph.getargs()] |
| 212 |
raise Exception('function %r has not been annotated' % func) |
| 213 |
|
| 214 |
get_printable_location_args = getargs(get_printable_location) |
| 215 |
assert get_printable_location_args == [lltype.Float] |
| 216 |
|
| 217 |
def test_annotate_argumenterror(self): |
| 218 |
myjitdriver = JitDriver(greens=['m'], reds=['n']) |
| 219 |
def fn(n): |
| 220 |
while n > 0: |
| 221 |
myjitdriver.can_enter_jit(m=42.5, n=n) |
| 222 |
myjitdriver.jit_merge_point(n=n) |
| 223 |
n -= 1 |
| 224 |
return n |
| 225 |
with pytest.raises(JitHintError): |
| 226 |
self.gengraph(fn, [int]) |
| 227 |
|
| 228 |
def test_annotate_typeerror(self): |
| 229 |
myjitdriver = JitDriver(greens=['m'], reds=['n']) |
| 230 |
class A(object): |
| 231 |
pass |
| 232 |
class B(object): |
| 233 |
pass |
| 234 |
def fn(n): |
| 235 |
while n > 0: |
| 236 |
myjitdriver.can_enter_jit(m=A(), n=n) |
| 237 |
myjitdriver.jit_merge_point(m=B(), n=n) |
| 238 |
n -= 1 |
| 239 |
return n |
| 240 |
with pytest.raises(UnionError): |
| 241 |
self.gengraph(fn, [int]) |
| 242 |
|
| 243 |
def test_green_field(self): |
| 244 |
def get_printable_location(xfoo): |
| 245 |
return str(ord(xfoo)) |
| 246 |
|
| 247 |
with pytest.raises(ValueError): |
| 248 |
JitDriver(greens=['x.foo'], reds=['n', 'x'], |
| 249 |
get_printable_location=get_printable_location) |
| 250 |
return |
| 251 |
class A(object): |
| 252 |
_immutable_fields_ = ['foo'] |
| 253 |
def fn(n): |
| 254 |
x = A() |
| 255 |
x.foo = chr(n) |
| 256 |
while n > 0: |
| 257 |
myjitdriver.can_enter_jit(x=x, n=n) |
| 258 |
myjitdriver.jit_merge_point(x=x, n=n) |
| 259 |
n -= 1 |
| 260 |
return n |
| 261 |
t = self.gengraph(fn, [int])[0] |
| 262 |
if option.view: |
| 263 |
t.view() |
| 264 |
|
| 265 |
|
| 266 |
def test_isconstant(self): |
| 267 |
def f(n): |
| 268 |
assert isconstant(n) is False |
| 269 |
l = [] |
| 270 |
l.append(n) |
| 271 |
return len(l) |
| 272 |
res = self.interpret(f, [-234]) |
| 273 |
assert res == 1 |
| 274 |
|
| 275 |
def test_argument_order_ok(self): |
| 276 |
myjitdriver = JitDriver(greens=['i1', 'r1', 'f1'], reds=[]) |
| 277 |
class A(object): |
| 278 |
pass |
| 279 |
myjitdriver.jit_merge_point(i1=42, r1=A(), f1=3.5) |
| 280 |
|
| 281 |
|
| 282 |
def test_argument_order_wrong(self): |
| 283 |
myjitdriver = JitDriver(greens=['r1', 'i1', 'f1'], reds=[]) |
| 284 |
class A(object): |
| 285 |
pass |
| 286 |
with pytest.raises(AssertionError): |
| 287 |
myjitdriver.jit_merge_point(i1=42, r1=A(), f1=3.5) |
| 288 |
|
| 289 |
def test_argument_order_more_precision_later(self): |
| 290 |
myjitdriver = JitDriver(greens=['r1', 'i1', 'r2', 'f1'], reds=[]) |
| 291 |
class A(object): |
| 292 |
pass |
| 293 |
myjitdriver.jit_merge_point(i1=42, r1=None, r2=None, f1=3.5) |
| 294 |
with pytest.raises(AssertionError) as e: |
| 295 |
myjitdriver.jit_merge_point(i1=42, r1=A(), r2=None, f1=3.5) |
| 296 |
assert "got ['2:REF', '1:INT', '?', '3:FLOAT']" in repr(e.value) |
| 297 |
|
| 298 |
def test_argument_order_more_precision_later_2(self): |
| 299 |
myjitdriver = JitDriver(greens=['r1', 'i1', 'r2', 'f1'], reds=[]) |
| 300 |
class A(object): |
| 301 |
pass |
| 302 |
myjitdriver.jit_merge_point(i1=42, r1=None, r2=A(), f1=3.5) |
| 303 |
with pytest.raises(AssertionError) as e: |
| 304 |
myjitdriver.jit_merge_point(i1=42, r1=A(), r2=None, f1=3.5) |
| 305 |
assert "got ['2:REF', '1:INT', '2:REF', '3:FLOAT']" in repr(e.value) |
| 306 |
|
| 307 |
def test_argument_order_accept_r_uint(self): |
| 308 |
|
| 309 |
myjitdriver = JitDriver(greens=['i1'], reds=[]) |
| 310 |
myjitdriver.jit_merge_point(i1=r_uint(42)) |
| 311 |
|
| 312 |
def test_conditional_call(self): |
| 313 |
def g(): |
| 314 |
pass |
| 315 |
def f(n): |
| 316 |
conditional_call(n >= 0, g) |
| 317 |
def later(m): |
| 318 |
conditional_call(m, g) |
| 319 |
t = TranslationContext() |
| 320 |
t.buildannotator().build_types(f, [int]) |
| 321 |
t.buildrtyper().specialize() |
| 322 |
mix = MixLevelHelperAnnotator(t.rtyper) |
| 323 |
mix.getgraph(later, [annmodel.s_Bool], annmodel.s_None) |
| 324 |
mix.finish() |
| 325 |
|
| 326 |
def test_conditional_call_elidable(self): |
| 327 |
def g(x, y): |
| 328 |
return x - y + 5 |
| 329 |
def f(n, x, y): |
| 330 |
return conditional_call_elidable(n, g, x, y) |
| 331 |
assert f(0, 1000, 100) == 905 |
| 332 |
res = self.interpret(f, [0, 1000, 100]) |
| 333 |
assert res == 905 |
| 334 |
assert f(-42, 1000, 100) == -42 |
| 335 |
res = self.interpret(f, [-42, 1000, 100]) |
| 336 |
assert res == -42 |
| 337 |
|
| 338 |
def test_conditional_call_elidable_annotates_nonnull(self): |
| 339 |
class X: |
| 340 |
pass |
| 341 |
def g(n): |
| 342 |
return X() |
| 343 |
def f(x, n): |
| 344 |
y = conditional_call_elidable(x, g, n) |
| 345 |
return y |
| 346 |
def main(n): |
| 347 |
if n > 100: |
| 348 |
x = X() |
| 349 |
else: |
| 350 |
x = None |
| 351 |
return f(x, n) |
| 352 |
t = TranslationContext() |
| 353 |
s = t.buildannotator().build_types(main, [int]) |
| 354 |
assert s.can_be_None is False |
| 355 |
|
| 356 |
def test_enter_leave_portal_frame(self): |
| 357 |
from rpython.translator.interactive import Translation |
| 358 |
def g(): |
| 359 |
enter_portal_frame(1) |
| 360 |
leave_portal_frame() |
| 361 |
t = Translation(g, []) |
| 362 |
t.compile_c() |
| 363 |
|
| 364 |
def test_record_exact_value(self): |
| 365 |
class A(object): |
| 366 |
pass |
| 367 |
def g(): |
| 368 |
a = A() |
| 369 |
b = A() |
| 370 |
record_exact_value(a, a) |
| 371 |
self.interpret(g, []) |
| 372 |
|
| 373 |
def test_record_exact_value_2(self): |
| 374 |
class A(object): |
| 375 |
pass |
| 376 |
def make(j): |
| 377 |
if j < 5: return None |
| 378 |
return A() |
| 379 |
def g(j): |
| 380 |
a = make(j) |
| 381 |
record_exact_value(a, None) |
| 382 |
self.interpret(g, [3]) |
| 383 |
|
| 384 |
def test_record_exact_value_int(self): |
| 385 |
@dont_look_inside |
| 386 |
def f(): |
| 387 |
return 4 |
| 388 |
def g(): |
| 389 |
a = f() |
| 390 |
record_exact_value(a >= 0, True) |
| 391 |
self.interpret(g, []) |
| 392 |
|
| 393 |
def test_record_known_result(self): |
| 394 |
from rpython.translator.interactive import Translation |
| 395 |
@elidable |
| 396 |
def f(x): |
| 397 |
return x + 1 |
| 398 |
@elidable |
| 399 |
def g(x): |
| 400 |
return x - 1 |
| 401 |
|
| 402 |
def call_f(x): |
| 403 |
y = f(x) |
| 404 |
record_known_result(x, g, y) |
| 405 |
return y |
| 406 |
|
| 407 |
call_f(10) |
| 408 |
t = Translation(call_f, [int]) |
| 409 |
t.rtype() |