Debian Codegraph @

rpython/translator/backendopt/test/test_support.py

raw symbols SCIP
1 from rpython.translator.translator import TranslationContext, graphof
2 from rpython.translator.backendopt.support import \
3 find_loop_blocks, find_backedges, compute_reachability
4
5 #__________________________________________________________
6 # test compute_reachability
7
8 def test_simple_compute_reachability():
9 def f(x):
10 if x < 0:
11 if x == -1:
12 return x+1
13 else:
14 return x+2
15 else:
16 if x == 1:
17 return x-1
18 else:
19 return x-2
20 t = TranslationContext()
21 g = t.buildflowgraph(f)
22 reach = compute_reachability(g)
23 assert len(reach[g.startblock]) == 7
24 assert len(reach[g.startblock.exits[0].target]) == 3
25 assert len(reach[g.startblock.exits[1].target]) == 3
26 #__________________________________________________________
27 # test loop detection
28
29 def test_find_backedges():
30 def f(k):
31 result = 0
32 for i in range(k):
33 result += 1
34 for j in range(k):
35 result += 1
36 return result
37 t = TranslationContext()
38 t.buildannotator().build_types(f, [int])
39 t.buildrtyper().specialize()
40 graph = graphof(t, f)
41 backedges = find_backedges(graph)
42 assert len(backedges) == 2
43
44 def test_find_loop_blocks():
45 def f(k):
46 result = 0
47 for i in range(k):
48 result += 1
49 for j in range(k):
50 result += 1
51 return result
52 t = TranslationContext()
53 t.buildannotator().build_types(f, [int])
54 t.buildrtyper().specialize()
55 graph = graphof(t, f)
56 loop_blocks = find_loop_blocks(graph)
57 assert len(loop_blocks) == 4
58
59 def test_find_loop_blocks_simple():
60 def f(a):
61 if a <= 0:
62 return 1
63 return f(a - 1)
64 t = TranslationContext()
65 t.buildannotator().build_types(f, [int])
66 t.buildrtyper().specialize()
67 graph = graphof(t, f)
68 backedges = find_backedges(graph)
69 assert backedges == []
70 loop_blocks = find_loop_blocks(graph)
71 assert len(loop_blocks) == 0
72
73 def test_find_loop_blocks2():
74 class A:
75 pass
76 def f(n):
77 a1 = A()
78 a1.x = 1
79 a2 = A()
80 a2.x = 2
81 if n > 0:
82 a = a1
83 else:
84 a = a2
85 return a.x
86 t = TranslationContext()
87 t.buildannotator().build_types(f, [int])
88 t.buildrtyper().specialize()
89 graph = graphof(t, f)
90 backedges = find_backedges(graph)
91 assert backedges == []
92 loop_blocks = find_loop_blocks(graph)
93 assert len(loop_blocks) == 0
94
95 def test_find_loop_blocks3():
96 import os
97 def ps(loops):
98 return 42.0, 42.1
99 def f(loops):
100 benchtime, stones = ps(abs(loops))
101 s = '' # annotator happiness
102 if loops >= 0:
103 s = ("RPystone(%s) time for %d passes = %f" %
104 (23, loops, benchtime) + '\n' + (
105 "This machine benchmarks at %f pystones/second" % stones))
106 os.write(1, s)
107 if loops == 12345:
108 f(loops-1)
109 t = TranslationContext()
110 t.buildannotator().build_types(f, [int])
111 t.buildrtyper().specialize()
112 graph = graphof(t, f)
113 backedges = find_backedges(graph)
114 assert backedges == []
115 loop_blocks = find_loop_blocks(graph)
116 assert len(loop_blocks) == 0
117