| 1 |
import sys |
| 2 |
import py |
| 3 |
from rpython.jit.codewriter.policy import contains_unsupported_variable_type |
| 4 |
from rpython.jit.codewriter.policy import JitPolicy |
| 5 |
from rpython.jit.codewriter import support |
| 6 |
from rpython.rlib.rarithmetic import r_singlefloat, r_longlong |
| 7 |
from rpython.rlib import jit |
| 8 |
|
| 9 |
def test_contains_unsupported_variable_type(): |
| 10 |
def f(x): |
| 11 |
return x |
| 12 |
graph = support.getgraph(f, [5]) |
| 13 |
for sf in [False, True]: |
| 14 |
for sll in [False, True]: |
| 15 |
for ssf in [False, True]: |
| 16 |
assert not contains_unsupported_variable_type(graph, sf, |
| 17 |
sll, ssf) |
| 18 |
|
| 19 |
graph = support.getgraph(f, [5.5]) |
| 20 |
for sf in [False, True]: |
| 21 |
for sll in [False, True]: |
| 22 |
for ssf in [False, True]: |
| 23 |
res = contains_unsupported_variable_type(graph, sf, sll, ssf) |
| 24 |
assert res is not sf |
| 25 |
|
| 26 |
graph = support.getgraph(f, [r_singlefloat(5.5)]) |
| 27 |
for sf in [False, True]: |
| 28 |
for sll in [False, True]: |
| 29 |
for ssf in [False, True]: |
| 30 |
res = contains_unsupported_variable_type(graph, sf, sll, ssf) |
| 31 |
assert res == (not ssf) |
| 32 |
|
| 33 |
graph = support.getgraph(f, [r_longlong(5)]) |
| 34 |
for sf in [False, True]: |
| 35 |
for sll in [False, True]: |
| 36 |
for ssf in [False, True]: |
| 37 |
res = contains_unsupported_variable_type(graph, sf, sll, ssf) |
| 38 |
assert res == (sys.maxint == 2147483647 and not sll) |
| 39 |
|
| 40 |
|
| 41 |
def test_regular_function(): |
| 42 |
graph = support.getgraph(lambda x: x+3, [5]) |
| 43 |
assert JitPolicy().look_inside_graph(graph) |
| 44 |
|
| 45 |
def test_without_floats(): |
| 46 |
graph = support.getgraph(lambda x: x+3.2, [5.4]) |
| 47 |
policy = JitPolicy() |
| 48 |
policy.set_supports_floats(True) |
| 49 |
assert policy.look_inside_graph(graph) |
| 50 |
policy = JitPolicy() |
| 51 |
policy.set_supports_floats(False) |
| 52 |
assert not policy.look_inside_graph(graph) |
| 53 |
|
| 54 |
def test_elidable(): |
| 55 |
@jit.elidable |
| 56 |
def g(x): |
| 57 |
return x + 2 |
| 58 |
graph = support.getgraph(g, [5]) |
| 59 |
assert not JitPolicy().look_inside_graph(graph) |
| 60 |
|
| 61 |
def test_dont_look_inside(): |
| 62 |
@jit.dont_look_inside |
| 63 |
def h(x): |
| 64 |
return x + 3 |
| 65 |
graph = support.getgraph(h, [5]) |
| 66 |
assert not JitPolicy().look_inside_graph(graph) |
| 67 |
|
| 68 |
def test_look_inside(): |
| 69 |
def h1(x): |
| 70 |
return x + 1 |
| 71 |
@jit.look_inside |
| 72 |
def h2(x): |
| 73 |
return x + 2 |
| 74 |
class MyPolicy(JitPolicy): |
| 75 |
def look_inside_function(self, func): |
| 76 |
return False |
| 77 |
graph1 = support.getgraph(h1, [5]) |
| 78 |
graph2 = support.getgraph(h2, [5]) |
| 79 |
assert not MyPolicy().look_inside_graph(graph1) |
| 80 |
assert MyPolicy().look_inside_graph(graph2) |
| 81 |
|
| 82 |
def test_loops(): |
| 83 |
def g(x): |
| 84 |
i = 0 |
| 85 |
while i < x: |
| 86 |
i += 1 |
| 87 |
return i |
| 88 |
graph = support.getgraph(g, [5]) |
| 89 |
assert not JitPolicy().look_inside_graph(graph) |
| 90 |
|
| 91 |
def test_unroll_safe(): |
| 92 |
@jit.unroll_safe |
| 93 |
def h(x): |
| 94 |
i = 0 |
| 95 |
while i < x: |
| 96 |
i += 1 |
| 97 |
return i |
| 98 |
graph = support.getgraph(h, [5]) |
| 99 |
assert JitPolicy().look_inside_graph(graph) |
| 100 |
|
| 101 |
def test_unroll_safe_and_inline(): |
| 102 |
@jit.unroll_safe |
| 103 |
def h(x): |
| 104 |
i = 0 |
| 105 |
while i < x: |
| 106 |
i += 1 |
| 107 |
return i |
| 108 |
h._always_inline_ = True |
| 109 |
|
| 110 |
def g(x): |
| 111 |
return h(x) |
| 112 |
|
| 113 |
graph = support.getgraph(h, [5]) |
| 114 |
assert JitPolicy().look_inside_graph(graph) |
| 115 |
|
| 116 |
def test_str_join(): |
| 117 |
def f(x, y): |
| 118 |
return "hello".join([str(x), str(y), "bye"]) |
| 119 |
|
| 120 |
graph = support.getgraph(f, [5, 83]) |
| 121 |
for block in graph.iterblocks(): |
| 122 |
for op in block.operations: |
| 123 |
if op.opname == 'direct_call': |
| 124 |
funcptr = op.args[0].value |
| 125 |
called_graph = funcptr._obj.graph |
| 126 |
if JitPolicy().look_inside_graph(called_graph): |
| 127 |
|
| 128 |
mod = called_graph.func.__module__ |
| 129 |
assert (mod == 'rpython.rtyper.rlist' or |
| 130 |
mod == 'rpython.rtyper.lltypesystem.rlist') |
| 131 |
|
| 132 |
def test_access_directly_but_not_seen(): |
| 133 |
class X: |
| 134 |
_virtualizable_ = ["a"] |
| 135 |
def h(x, y): |
| 136 |
w = 0 |
| 137 |
for i in range(y): |
| 138 |
w += 4 |
| 139 |
return w |
| 140 |
def f(y): |
| 141 |
x = jit.hint(X(), access_directly=True) |
| 142 |
h(x, y) |
| 143 |
rtyper = support.annotate(f, [3]) |
| 144 |
h_graph = rtyper.annotator.translator.graphs[1] |
| 145 |
assert h_graph.func is h |
| 146 |
py.test.raises(ValueError, JitPolicy().look_inside_graph, h_graph) |