1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496
|
import py
from rpython.flowspace.model import checkgraph, Constant, summary, mkentrymap
from rpython.translator.translator import TranslationContext, graphof
from rpython.rtyper.llinterp import LLInterpreter
from rpython.rtyper.lltypesystem import lltype
from rpython.rtyper.lltypesystem.lloperation import llop
from rpython.rtyper import rclass
from rpython.rlib import objectmodel
from rpython.translator.backendopt.constfold import constant_fold_graph
from rpython.translator.backendopt.constfold import replace_we_are_jitted
from rpython.translator.backendopt.constfold import same_constant
from rpython.conftest import option
from rpython.rlib import jit
from rpython.rlib import rarithmetic
def get_graph(fn, signature):
t = TranslationContext()
t.buildannotator().build_types(fn, signature)
t.buildrtyper().specialize()
graph = graphof(t, fn)
if option.view:
t.view()
return graph, t
def check_graph(graph, args, expected_result, t):
if option.view:
t.view()
checkgraph(graph)
interp = LLInterpreter(t.rtyper)
res = interp.eval_graph(graph, args)
assert res == expected_result
def test_simple(S1=None):
if S1 is None:
S1 = lltype.GcStruct('S1', ('x', lltype.Signed),
hints={'immutable': True})
s1 = lltype.malloc(S1)
s1.x = 123
def g(y):
return y + 1
def fn():
return g(s1.x)
graph, t = get_graph(fn, [])
assert summary(graph) == {'getfield': 1, 'direct_call': 1}
constant_fold_graph(graph)
assert summary(graph) == {'direct_call': 1}
check_graph(graph, [], 124, t)
def test_immutable_fields():
accessor = rclass.FieldListAccessor()
S2 = lltype.GcStruct('S2', ('x', lltype.Signed),
hints={'immutable_fields': accessor})
accessor.initialize(S2, {'x': rclass.IR_IMMUTABLE})
test_simple(S2)
def test_along_link():
S1 = lltype.GcStruct('S1', ('x', lltype.Signed), hints={'immutable': True})
s1 = lltype.malloc(S1)
s1.x = 123
s2 = lltype.malloc(S1)
s2.x = 60
def fn(x):
if x:
x = s1.x
else:
x = s2.x
return x+1
graph, t = get_graph(fn, [int])
assert summary(graph) == {'int_is_true': 1,
'getfield': 2,
'int_add': 1}
constant_fold_graph(graph)
assert summary(graph) == {'int_is_true': 1}
check_graph(graph, [-1], 124, t)
check_graph(graph, [0], 61, t)
def test_multiple_incoming_links():
S1 = lltype.GcStruct('S1', ('x', lltype.Signed), hints={'immutable': True})
s1 = lltype.malloc(S1)
s1.x = 123
s2 = lltype.malloc(S1)
s2.x = 60
s3 = lltype.malloc(S1)
s3.x = 15
def fn(x):
y = x * 10
if x == 1:
x = s1.x
elif x == 2:
x = s2.x
elif x == 3:
x = s3.x
y = s1.x
return (x+1) + y
graph, t = get_graph(fn, [int])
constant_fold_graph(graph)
assert summary(graph) == {'int_mul': 1, 'int_eq': 3, 'int_add': 2}
for link in graph.iterlinks():
if Constant(139) in link.args:
break
else:
raise AssertionError("139 not found in the graph as a constant")
for i in range(4):
check_graph(graph, [i], fn(i), t)
def test_fold_exitswitch():
S1 = lltype.GcStruct('S1', ('x', lltype.Signed), hints={'immutable': True})
s1 = lltype.malloc(S1)
s1.x = 123
s2 = lltype.malloc(S1)
s2.x = 60
def fn(n):
if s1.x:
return n * 5
else:
return n - 7
graph, t = get_graph(fn, [int])
assert summary(graph) == {'getfield': 1,
'int_is_true': 1,
'int_mul': 1,
'int_sub': 1}
constant_fold_graph(graph)
assert summary(graph) == {'int_mul': 1}
check_graph(graph, [12], 60, t)
def test_exception():
def g():
return 15
def fn(n):
try:
g()
except ValueError:
pass
return n
graph, t = get_graph(fn, [int])
constant_fold_graph(graph)
check_graph(graph, [12], 12, t)
def test_malloc():
S1 = lltype.GcStruct('S1', ('x', lltype.Signed), hints={'immutable': True})
def fn():
s = lltype.malloc(S1)
s.x = 12
objectmodel.keepalive_until_here(s)
return s.x
graph, t = get_graph(fn, [])
constant_fold_graph(graph)
check_graph(graph, [], 12, t)
def xxx_test_later_along_link():
S1 = lltype.GcStruct('S1', ('x', lltype.Signed), hints={'immutable': True})
s1 = lltype.malloc(S1)
s1.x = 123
s2 = lltype.malloc(S1)
s2.x = 60
def fn(x, y):
if x:
x = s1.x
else:
x = s2.x
y *= 2
return (x+1) - y
graph, t = get_graph(fn, [int, int])
assert summary(graph) == {'int_is_true': 1,
'getfield': 2,
'int_mul': 1,
'int_add': 1,
'int_sub': 1}
constant_fold_graph(graph)
assert summary(graph) == {'int_is_true': 1,
'int_mul': 1,
'int_sub': 1}
check_graph(graph, [-1], 124, t)
check_graph(graph, [0], 61, t)
def test_keepalive_const_fieldptr():
S1 = lltype.GcStruct('S1', ('x', lltype.Signed))
s1 = lltype.malloc(S1)
s1.x = 1234
def fn():
p1 = lltype.direct_fieldptr(s1, 'x')
return p1[0]
graph, t = get_graph(fn, [])
assert summary(graph) == {'direct_fieldptr': 1, 'getarrayitem': 1}
constant_fold_graph(graph)
# kill all references to 's1'
s1 = fn = None
del graph.func
import gc; gc.collect()
assert summary(graph) == {'getarrayitem': 1}
check_graph(graph, [], 1234, t)
def test_keepalive_const_arrayitems():
A1 = lltype.GcArray(lltype.Signed)
a1 = lltype.malloc(A1, 10)
a1[6] = 1234
def fn():
p1 = lltype.direct_arrayitems(a1)
p2 = lltype.direct_ptradd(p1, 6)
return p2[0]
graph, t = get_graph(fn, [])
assert summary(graph) == {'direct_arrayitems': 1, 'direct_ptradd': 1,
'getarrayitem': 1}
constant_fold_graph(graph)
# kill all references to 'a1'
a1 = fn = None
del graph.func
import gc; gc.collect()
assert summary(graph) == {'getarrayitem': 1}
check_graph(graph, [], 1234, t)
def test_dont_constfold_debug_print():
def fn():
llop.debug_print(lltype.Void, "hello world")
graph, t = get_graph(fn, [])
assert summary(graph) == {'debug_print': 1}
constant_fold_graph(graph)
assert summary(graph) == {'debug_print': 1}
def test_fold_exitswitch_along_one_path():
def g(n):
if n == 42:
return 5
else:
return n+1
def fn(n):
if g(n) == 5:
return 100
else:
return 0
graph, t = get_graph(fn, [int])
from rpython.translator.backendopt import removenoops, inline
inline.auto_inline_graphs(t, t.graphs, threshold=999)
constant_fold_graph(graph)
removenoops.remove_same_as(graph)
if option.view:
t.view()
# check that the graph starts with a condition (which should be 'n==42')
# and that if this condition is true, it goes directly to 'return 100'.
assert len(graph.startblock.exits) == 2
assert graph.startblock.exits[1].exitcase == True
assert graph.startblock.exits[1].target is graph.returnblock
check_graph(graph, [10], 0, t)
check_graph(graph, [42], 100, t)
def test_knownswitch_after_exitswitch():
def fn(n):
cond = n > 10
if cond:
return cond + 5
else:
return cond + 17
graph, t = get_graph(fn, [int])
from rpython.translator.backendopt import removenoops
removenoops.remove_same_as(graph)
constant_fold_graph(graph)
if option.view:
t.view()
assert summary(graph) == {'int_gt': 1}
check_graph(graph, [2], 17, t)
check_graph(graph, [42], 6, t)
def test_coalesce_exitswitchs():
def g(n):
return n > 5 and n < 20
def fn(n):
if g(n):
return 100
else:
return 0
graph, t = get_graph(fn, [int])
from rpython.translator.backendopt import removenoops, inline
inline.auto_inline_graphs(t, t.graphs, threshold=999)
removenoops.remove_same_as(graph)
constant_fold_graph(graph)
if option.view:
t.view()
# check that the graph starts with a condition (which should be 'n > 5')
# and that if this condition is false, it goes directly to 'return 0'.
assert summary(graph) == {'int_gt': 1, 'int_lt': 1}
assert len(graph.startblock.exits) == 2
assert graph.startblock.exits[0].exitcase == False
assert graph.startblock.exits[0].target is graph.returnblock
check_graph(graph, [2], 0, t)
check_graph(graph, [10], 100, t)
check_graph(graph, [42], 0, t)
def test_merge_if_blocks_bug():
def fn(n):
if n == 1: return 5
elif n == 2: return 6
elif n == 3: return 8
elif n == 4: return -123
elif n == 5: return 12973
else: return n
graph, t = get_graph(fn, [int])
from rpython.translator.backendopt.removenoops import remove_same_as
from rpython.translator.backendopt import merge_if_blocks
remove_same_as(graph)
merge_if_blocks.merge_if_blocks_once(graph)
constant_fold_graph(graph)
check_graph(graph, [4], -123, t)
check_graph(graph, [9], 9, t)
def test_merge_if_blocks_bug_2():
def fn():
n = llop.same_as(lltype.Signed, 66)
if n == 1: return 5
elif n == 2: return 6
elif n == 3: return 8
elif n == 4: return -123
elif n == 5: return 12973
else: return n
graph, t = get_graph(fn, [])
from rpython.translator.backendopt.removenoops import remove_same_as
from rpython.translator.backendopt import merge_if_blocks
remove_same_as(graph)
merge_if_blocks.merge_if_blocks_once(graph)
constant_fold_graph(graph)
check_graph(graph, [], 66, t)
def test_replace_we_are_jitted():
def fn():
if jit.we_are_jitted():
return 1
return 2 + jit.we_are_jitted()
graph, t = get_graph(fn, [])
result = replace_we_are_jitted(graph)
assert result
checkgraph(graph)
# check shape of graph
assert len(graph.startblock.operations) == 0
assert graph.startblock.exitswitch is None
assert graph.startblock.exits[0].target.exits[0].args[0].value == 2
def test_int_ovf():
import sys
def fn():
# just using we_are_jitted as a way to introduce constants late
if jit.we_are_jitted():
x = 1
y = 5
else:
x = 2
y = 12
x += 2
try:
return rarithmetic.ovfcheck(x + y)
except OverflowError:
return -12
graph, t = get_graph(fn, [])
result = replace_we_are_jitted(graph)
assert summary(graph).get('int_add_nonneg_ovf', 0) == 0
check_graph(graph, [], 16, t)
def fn():
if jit.we_are_jitted():
x = 1
y = 5
else:
x = 2
y = sys.maxint
x += 2
try:
return rarithmetic.ovfcheck(x + y)
except OverflowError:
return -12
graph, t = get_graph(fn, [])
result = replace_we_are_jitted(graph)
assert summary(graph).get('int_add_nonneg_ovf', 0) == 0
check_graph(graph, [], -12, t)
def test_int_ovf_bug():
import sys
def fn(a, b):
if a:
if jit.we_are_jitted():
b = 1
else:
b = 2
b += 2
else:
b += a
try:
return rarithmetic.ovfcheck(12 + b)
except OverflowError:
return -12
graph, t = get_graph(fn, [int, int])
result = replace_we_are_jitted(graph)
assert result
check_graph(graph, [0, sys.maxint-1], -12, t)
assert len(mkentrymap(graph)[graph.returnblock]) == 3
def fn(a, b):
if a:
if jit.we_are_jitted():
b = 1
else:
b = 2
b += 2
else:
b += a
try:
x = rarithmetic.ovfcheck(12 + b)
except OverflowError:
x = -12
return x + a * b
graph, t = get_graph(fn, [int, int])
result = replace_we_are_jitted(graph)
assert result
check_graph(graph, [0, sys.maxint-1], fn(0, sys.maxint-1), t)
entrymap = mkentrymap(graph)
assert len(entrymap[entrymap[graph.returnblock][0].prevblock]) == 3
def test_constant_diffuse_bug():
import sys
class A:
pass
a1 = A()
def fn(arg, arg2):
# just using we_are_jitted as a way to introduce constants late
if jit.we_are_jitted():
a = a1
b = 1
elif arg:
arg += 1
a = None
b = 2
else:
a = None
b = 3
if arg:
arg = 0
arg += arg2
return a is None and arg == 100
graph, t = get_graph(fn, [int, int])
result = replace_we_are_jitted(graph)
assert result
assert summary(graph).get('ptr_iszero', 0) == 0
def test_float_zero_sign():
# flowmodel.Constant has the correct logic for this, but let's have a test
# to be sure
def fn(arg):
# just using we_are_jitted as a way to introduce constants late
import math
if jit.we_are_jitted():
a = 12
if arg:
a = 0.0
else:
a = -0.0
return math.atan2(0.0, a * -0.0)
graph, t = get_graph(fn, [int])
result = replace_we_are_jitted(graph)
check_graph(graph, [0], fn(0), t)
check_graph(graph, [1], fn(1), t)
def test_same_constant():
c1 = Constant(0.0, lltype.Float)
c2 = Constant(-0.0, lltype.Float)
assert not same_constant(c1, c2)
T = lltype.Ptr(lltype.GcStruct('abc'))
c1 = Constant(lltype.nullptr(T.TO), T)
c2 = Constant(lltype.nullptr(T.TO), T)
assert same_constant(c1, c2)
|