Debian Codegraph @

rpython/translator/backendopt/test/test_constfold.py

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