| 1 |
from rpython.translator.backendopt.merge_if_blocks import merge_if_blocks_once |
| 2 |
from rpython.translator.backendopt.merge_if_blocks import merge_if_blocks |
| 3 |
from rpython.translator.backendopt.all import backend_optimizations |
| 4 |
from rpython.translator.translator import TranslationContext, graphof as tgraphof |
| 5 |
from rpython.flowspace.model import Block, checkgraph |
| 6 |
from rpython.translator.backendopt.removenoops import remove_same_as |
| 7 |
from rpython.rtyper.llinterp import LLInterpreter |
| 8 |
from rpython.rlib.rarithmetic import r_uint, r_ulonglong, r_longlong, r_int |
| 9 |
from rpython.annotator.model import SomeChar, SomeUnicodeCodePoint |
| 10 |
from rpython.rlib.objectmodel import CDefinedIntSymbolic |
| 11 |
|
| 12 |
def do_test_merge(fn, testvalues): |
| 13 |
t = TranslationContext() |
| 14 |
a = t.buildannotator() |
| 15 |
a.build_types(fn, [type(testvalues[0])]) |
| 16 |
rtyper = t.buildrtyper() |
| 17 |
rtyper.specialize() |
| 18 |
graph = tgraphof(t, fn) |
| 19 |
assert len(list(graph.iterblocks())) == 4 |
| 20 |
remove_same_as(graph) |
| 21 |
merge_if_blocks_once(graph) |
| 22 |
assert len(graph.startblock.exits) == 4 |
| 23 |
assert len(list(graph.iterblocks())) == 2 |
| 24 |
interp = LLInterpreter(rtyper) |
| 25 |
for i in testvalues: |
| 26 |
expected = fn(i) |
| 27 |
actual = interp.eval_graph(graph, [i]) |
| 28 |
assert actual == expected |
| 29 |
|
| 30 |
def test_merge1(): |
| 31 |
def merge_int(n): |
| 32 |
n += 1 |
| 33 |
if n == 1: |
| 34 |
return 1 |
| 35 |
elif n == 2: |
| 36 |
return 2 |
| 37 |
elif n == 3: |
| 38 |
return 3 |
| 39 |
return 4 |
| 40 |
do_test_merge(merge_int, range(4)) |
| 41 |
do_test_merge(merge_int, [r_uint(i) for i in range(4)]) |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
def merge_chr(n): |
| 48 |
c = chr(n + 1) |
| 49 |
if c == 'a': |
| 50 |
return 'a' |
| 51 |
elif c == 'b': |
| 52 |
return 'b' |
| 53 |
elif c == 'c': |
| 54 |
return 'c' |
| 55 |
return 'd' |
| 56 |
do_test_merge(merge_chr, range(96, 101)) |
| 57 |
|
| 58 |
def merge_uchr(n): |
| 59 |
c = unichr(n + 1) |
| 60 |
if c == u'a': |
| 61 |
return u'a' |
| 62 |
elif c == u'b': |
| 63 |
return u'b' |
| 64 |
elif c == u'c': |
| 65 |
return u'c' |
| 66 |
return u'd' |
| 67 |
do_test_merge(merge_uchr, range(96, 101)) |
| 68 |
|
| 69 |
def test_merge_passonvars(): |
| 70 |
def merge(n, m): |
| 71 |
if n == 1: |
| 72 |
return m + 1 |
| 73 |
elif n == 2: |
| 74 |
return m + 2 |
| 75 |
elif n == 3: |
| 76 |
return m + 3 |
| 77 |
return m + 4 |
| 78 |
t = TranslationContext() |
| 79 |
a = t.buildannotator() |
| 80 |
a.build_types(merge, [int, int]) |
| 81 |
rtyper = t.buildrtyper() |
| 82 |
rtyper.specialize() |
| 83 |
graph = tgraphof(t, merge) |
| 84 |
assert len(list(graph.iterblocks())) == 8 |
| 85 |
remove_same_as(graph) |
| 86 |
merge_if_blocks_once(graph) |
| 87 |
assert len(graph.startblock.exits) == 4 |
| 88 |
interp = LLInterpreter(rtyper) |
| 89 |
for i in range(1, 5): |
| 90 |
res = interp.eval_graph(graph, [i, 1]) |
| 91 |
assert res == i + 1 |
| 92 |
|
| 93 |
def test_merge_several(): |
| 94 |
def merge(n, m): |
| 95 |
r = -1 |
| 96 |
if n == 0: |
| 97 |
if m == 0: |
| 98 |
r = 0 |
| 99 |
elif m == 1: |
| 100 |
r = 1 |
| 101 |
else: |
| 102 |
r = 2 |
| 103 |
elif n == 1: |
| 104 |
r = 4 |
| 105 |
else: |
| 106 |
r = 6 |
| 107 |
return r |
| 108 |
t = TranslationContext() |
| 109 |
a = t.buildannotator() |
| 110 |
a.build_types(merge, [int, int]) |
| 111 |
rtyper = t.buildrtyper() |
| 112 |
rtyper.specialize() |
| 113 |
graph = tgraphof(t, merge) |
| 114 |
remove_same_as(graph) |
| 115 |
merge_if_blocks(graph) |
| 116 |
assert len(graph.startblock.exits) == 3 |
| 117 |
assert len(list(graph.iterblocks())) == 3 |
| 118 |
interp = LLInterpreter(rtyper) |
| 119 |
for m in range(3): |
| 120 |
res = interp.eval_graph(graph, [0, m]) |
| 121 |
assert res == m |
| 122 |
res = interp.eval_graph(graph, [1, 0]) |
| 123 |
assert res == 4 |
| 124 |
res = interp.eval_graph(graph, [2, 0]) |
| 125 |
assert res == 6 |
| 126 |
|
| 127 |
|
| 128 |
def test_merge_with_or(): |
| 129 |
def merge(n): |
| 130 |
if n == 5: |
| 131 |
return 4 |
| 132 |
elif n == 14 or n == 2: |
| 133 |
return 16 |
| 134 |
else: |
| 135 |
return 7 |
| 136 |
do_test_merge(merge, [5, 6, 14, 2, 3, 123]) |
| 137 |
|
| 138 |
|
| 139 |
def test_dont_merge(): |
| 140 |
def merge(n, m): |
| 141 |
r = -1 |
| 142 |
if n == 0: |
| 143 |
r += m |
| 144 |
if n == 1: |
| 145 |
r += 2 * m |
| 146 |
else: |
| 147 |
r += 6 |
| 148 |
return r |
| 149 |
t = TranslationContext() |
| 150 |
a = t.buildannotator() |
| 151 |
a.build_types(merge, [int, int]) |
| 152 |
rtyper = t.buildrtyper() |
| 153 |
rtyper.specialize() |
| 154 |
graph = tgraphof(t, merge) |
| 155 |
remove_same_as(graph) |
| 156 |
blocknum = len(list(graph.iterblocks())) |
| 157 |
merge_if_blocks(graph) |
| 158 |
assert blocknum == len(list(graph.iterblocks())) |
| 159 |
|
| 160 |
def test_two_constants(): |
| 161 |
def fn(): |
| 162 |
r = range(10, 37, 4) |
| 163 |
r.reverse() |
| 164 |
return r[0] |
| 165 |
t = TranslationContext() |
| 166 |
a = t.buildannotator() |
| 167 |
a.build_types(fn, []) |
| 168 |
rtyper = t.buildrtyper() |
| 169 |
rtyper.specialize() |
| 170 |
backend_optimizations(t, merge_if_blocks=True) |
| 171 |
graph = tgraphof(t, fn) |
| 172 |
blocknum = len(list(graph.iterblocks())) |
| 173 |
merge_if_blocks(graph) |
| 174 |
assert blocknum == len(list(graph.iterblocks())) |
| 175 |
|
| 176 |
def test_same_cases(): |
| 177 |
def fn(x): |
| 178 |
if x == 42: |
| 179 |
r = 1 |
| 180 |
elif x == 42: |
| 181 |
r = 2 |
| 182 |
else: |
| 183 |
r = 3 |
| 184 |
return r |
| 185 |
t = TranslationContext() |
| 186 |
a = t.buildannotator() |
| 187 |
a.build_types(fn, [int]) |
| 188 |
rtyper = t.buildrtyper() |
| 189 |
rtyper.specialize() |
| 190 |
backend_optimizations(t, merge_if_blocks=True) |
| 191 |
graph = tgraphof(t, fn) |
| 192 |
assert len(graph.startblock.exits) == 2 |
| 193 |
interp = LLInterpreter(rtyper) |
| 194 |
for i in [42, 43]: |
| 195 |
expected = fn(i) |
| 196 |
actual = interp.eval_graph(graph, [i]) |
| 197 |
assert actual == expected |
| 198 |
|
| 199 |
def test_replace_exitswitch_by_constant_bug(): |
| 200 |
class X: |
| 201 |
pass |
| 202 |
def constant9(): |
| 203 |
x = X() |
| 204 |
x.n = 3 |
| 205 |
x.n = 9 |
| 206 |
return x.n |
| 207 |
def fn(): |
| 208 |
n = constant9() |
| 209 |
if n == 1: return 5 |
| 210 |
elif n == 2: return 6 |
| 211 |
elif n == 3: return 8 |
| 212 |
elif n == 4: return -123 |
| 213 |
elif n == 5: return 12973 |
| 214 |
else: return n |
| 215 |
|
| 216 |
t = TranslationContext() |
| 217 |
a = t.buildannotator() |
| 218 |
a.build_types(fn, []) |
| 219 |
rtyper = t.buildrtyper() |
| 220 |
rtyper.specialize() |
| 221 |
graph = t.graphs[0] |
| 222 |
remove_same_as(graph) |
| 223 |
merge_if_blocks_once(graph) |
| 224 |
from rpython.translator.backendopt import malloc, inline |
| 225 |
inline.auto_inlining(t, 20) |
| 226 |
malloc.remove_mallocs(t, t.graphs) |
| 227 |
from rpython.translator import simplify |
| 228 |
simplify.join_blocks(graph) |
| 229 |
|
| 230 |
def test_switch_on_symbolic(): |
| 231 |
symb1 = CDefinedIntSymbolic("1", 1) |
| 232 |
symb2 = CDefinedIntSymbolic("2", 2) |
| 233 |
symb3 = CDefinedIntSymbolic("3", 3) |
| 234 |
def fn(x): |
| 235 |
res = 0 |
| 236 |
if x == symb1: |
| 237 |
res += x + 1 |
| 238 |
elif x == symb2: |
| 239 |
res += x + 2 |
| 240 |
elif x == symb3: |
| 241 |
res += x + 3 |
| 242 |
res += 1 |
| 243 |
return res |
| 244 |
t = TranslationContext() |
| 245 |
a = t.buildannotator() |
| 246 |
a.build_types(fn, [int]) |
| 247 |
rtyper = t.buildrtyper() |
| 248 |
rtyper.specialize() |
| 249 |
graph = t.graphs[0] |
| 250 |
remove_same_as(graph) |
| 251 |
res = merge_if_blocks_once(graph) |
| 252 |
assert not res |
| 253 |
checkgraph(graph) |
| 254 |
|