Debian Codegraph @

rpython/translator/backendopt/test/test_storesink.py

raw symbols SCIP
1
2 import py
3 from rpython.rlib.nonconst import NonConstant
4 from rpython.translator.translator import TranslationContext, graphof
5 from rpython.translator.backendopt.storesink import storesink_graph
6 from rpython.translator.backendopt import removenoops
7 from rpython.flowspace.model import checkgraph
8 from rpython.conftest import option
9
10 class TestStoreSink(object):
11 def translate(self, func, argtypes):
12 t = TranslationContext()
13 t.buildannotator().build_types(func, argtypes)
14 t.buildrtyper().specialize()
15 return t
16
17 def check(self, f, argtypes, no_getfields=0):
18 t = self.translate(f, argtypes)
19 getfields = 0
20 graph = graphof(t, f)
21 removenoops.remove_same_as(graph)
22 checkgraph(graph)
23 storesink_graph(graph)
24 checkgraph(graph)
25 if option.view:
26 t.view()
27 for block in graph.iterblocks():
28 for op in block.operations:
29 if op.opname == 'getfield':
30 getfields += 1
31 if no_getfields != getfields:
32 py.test.fail("Expected %d, got %d getfields" %
33 (no_getfields, getfields))
34
35 def test_infrastructure(self):
36 class A(object):
37 pass
38
39 def f(i):
40 a = A()
41 a.x = i
42 return a.x
43
44 self.check(f, [int], 0)
45
46 def test_simple(self):
47 class A(object):
48 pass
49
50 def f(i):
51 a = A()
52 a.x = i
53 return a.x + a.x
54
55 self.check(f, [int], 0)
56
57 def test_irrelevant_setfield(self):
58 class A(object):
59 pass
60
61 def f(i):
62 a = A()
63 a.x = i
64 one = a.x
65 a.y = 3
66 two = a.x
67 return one + two
68
69 self.check(f, [int], 0)
70
71 def test_relevant_setfield(self):
72 class A(object):
73 pass
74
75 def f(i):
76 a = A()
77 b = A()
78 a.x = i
79 b.x = i + 1
80 one = a.x
81 b.x = i
82 two = a.x
83 return one + two
84
85 self.check(f, [int], 2)
86
87 def test_different_concretetype(self):
88 class A(object):
89 pass
90
91 class B(object):
92 pass
93
94 def f(i):
95 a = A()
96 b = B()
97 a.x = i
98 one = a.x
99 b.x = i + 1
100 two = a.x
101 return one + two
102
103 self.check(f, [int], 0)
104
105 def test_subclass(self):
106 class A(object):
107 pass
108
109 class B(A):
110 pass
111
112 def f(i):
113 a = A()
114 b = B()
115 a.x = i
116 one = a.x
117 b.x = i + 1
118 two = a.x
119 return one + two
120
121 self.check(f, [int], 1)
122
123 def test_bug_1(self):
124 class A(object):
125 pass
126
127 def f(i):
128 a = A()
129 a.cond = i > 0
130 n = a.cond
131 if a.cond:
132 return True
133 return n
134
135 self.check(f, [int], 0)
136
137
138 def test_cfg_splits(self):
139 class A(object):
140 pass
141
142 def f(i):
143 a = A()
144 j = i
145 for i in range(i):
146 a.x = i
147 if i:
148 j = a.x + a.x
149 else:
150 j = a.x * 5
151 return j
152
153 self.check(f, [int], 0)
154
155 def test_malloc_does_not_invalidate(self):
156 class A(object):
157 pass
158 class B(object):
159 pass
160
161 def f(i):
162 a = A()
163 a.x = i
164 b = B()
165 return a.x
166
167 self.check(f, [int], 0)
168
169 def test_debug_assert_not_none(self):
170 from rpython.rlib.debug import ll_assert_not_none
171 class A(object):
172 pass
173
174 def g(i):
175 if i:
176 return None
177 else:
178 return A()
179
180 def f(i):
181 a1 = g(i)
182 a = A()
183 a.x = i
184 ll_assert_not_none(a1)
185 return a.x
186
187 self.check(f, [int], 0)
188
189 def test_read_none_field_bug(self):
190 from rpython.translator.backendopt import inline, constfold
191 class A(object):
192 _immutable_fields_ = ['next']
193
194 def g(i):
195 if i == 1:
196 return None
197 a = A()
198 a.next = 12 * i
199 return a
200
201 def f(i):
202 g(i)
203 a = g(1)
204 return a.next
205
206 t = self.translate(f, [int])
207 graph = graphof(t, f)
208 inline.auto_inline_graphs(t, [graph, graphof(t, g)], 100)
209 constfold.constant_fold_graph(graph)
210 removenoops.remove_same_as(graph)
211 checkgraph(graph)
212 storesink_graph(graph)
213 checkgraph(graph)
214
215 def test_cast_pointer_bug(self):
216 from rpython.rtyper.lltypesystem import lltype
217 S2 = lltype.GcStruct("s2", ('a', lltype.Signed))
218 S1 = lltype.GcStruct("s1", ('sub1', S2))
219 p2 = lltype.malloc(S2)
220 p2.a = 12
221 def f():
222 return lltype.cast_pointer(lltype.Ptr(S1), p2)
223 t = self.translate(f, [])
224 graph = graphof(t, f)
225 storesink_graph(graph) # used to crash with RuntimeError