Debian Codegraph @

rpython/rtyper/test/test_rvirtualizable.py

raw symbols SCIP
1 import py
2 from rpython.rtyper.llannotation import SomePtr
3 from rpython.rtyper.lltypesystem import lltype, llmemory
4 from rpython.rtyper.test.tool import BaseRtypingTest
5 from rpython.rtyper.rvirtualizable import replace_force_virtualizable_with_call
6 from rpython.rlib.jit import hint
7 from rpython.flowspace.model import summary
8 from rpython.rtyper.llinterp import LLInterpreter
9 from rpython.rtyper.rclass import IR_IMMUTABLE, IR_IMMUTABLE_ARRAY
10 from rpython.conftest import option
11
12
13 class V(object):
14 _virtualizable_ = ['v']
15 v = -12
16 w = -62
17
18 def __init__(self, v):
19 self.v = v
20 self.w = v+1
21
22 class SubclassV(V):
23 pass
24
25 class VArray(object):
26 _virtualizable_ = ['lst[*]']
27
28 def __init__(self, lst):
29 self.lst = lst
30 class B(object):
31 _virtualizable_ = ['v0']
32
33 x = "XX"
34
35 def __init__(self, v0):
36 self.v0 = v0
37
38 def get_force_virtualizable_flags(graph):
39 res = []
40 for block, op in graph.iterblockops():
41 if op.opname == 'jit_force_virtualizable':
42 res.append(op.args[-1].value)
43 return res
44
45 class TestVirtualizable(BaseRtypingTest):
46 prefix = 'inst_'
47 GETFIELD = 'getfield'
48 SETFIELD = 'setfield'
49
50 def gettype(self, v):
51 return v.concretetype.TO
52
53 def test_generate_force_virtualizable(self):
54 def fn(n):
55 vinst = V(n)
56 return vinst.v
57 _, _, graph = self.gengraph(fn, [int])
58 block = graph.startblock
59 op_promote = block.operations[-2]
60 op_getfield = block.operations[-1]
61 assert op_getfield.opname == 'getfield'
62 v_inst = op_getfield.args[0]
63 assert op_promote.opname == 'jit_force_virtualizable'
64 assert op_promote.args[0] is v_inst
65 assert op_promote.args[-1].value == {}
66
67 def test_generate_force_virtualizable_subclass(self):
68 def fn(n):
69 V(n) # to attach v to V
70 vinst = SubclassV(n)
71 return vinst.v
72 _, _, graph = self.gengraph(fn, [int])
73 block = graph.startblock
74 op_promote = block.operations[-2]
75 op_getfield = block.operations[-1]
76 assert op_getfield.opname == 'getfield'
77 v_inst = op_getfield.args[0]
78 assert op_promote.opname == 'jit_force_virtualizable'
79 assert op_promote.args[0] is v_inst
80 assert op_promote.args[-1].value == {}
81
82 def test_no_force_virtualizable_for_other_fields(self):
83 def fn(n):
84 vinst = V(n)
85 return vinst.w
86 _, _, graph = self.gengraph(fn, [int])
87 block = graph.startblock
88 op_getfield = block.operations[-1]
89 op_call = block.operations[-2]
90 assert op_getfield.opname == 'getfield'
91 assert op_call.opname == 'direct_call' # to V.__init__
92
93 def test_generate_force_virtualizable_array(self):
94 def fn(n):
95 vinst = VArray([n, n+1])
96 return vinst.lst[1]
97 _, _, graph = self.gengraph(fn, [int])
98 block = graph.startblock
99 op_promote = block.operations[-3]
100 op_getfield = block.operations[-2]
101 op_getarrayitem = block.operations[-1]
102 assert op_getarrayitem.opname == 'direct_call' # to ll_getitem_xxx
103 assert op_getfield.opname == 'getfield'
104 v_inst = op_getfield.args[0]
105 assert op_promote.opname == 'jit_force_virtualizable'
106 assert op_promote.args[0] is v_inst
107 assert op_promote.args[-1].value == {}
108
109 def test_accessor(self):
110 class Base(object):
111 pass
112 class V(Base):
113 _virtualizable_ = ['v1', 'v2[*]']
114 class W(V):
115 pass
116 #
117 def fn1(n):
118 Base().base1 = 42
119 V().v1 = 43
120 V().v2 = ['x', 'y']
121 W().w1 = 44
122 return V()
123 _, _, graph = self.gengraph(fn1, [int])
124 v_inst = graph.getreturnvar()
125 TYPE = self.gettype(v_inst)
126 accessor = TYPE._hints['virtualizable_accessor']
127 assert accessor.TYPE == TYPE
128 assert accessor.fields == {self.prefix + 'v1': IR_IMMUTABLE,
129 self.prefix + 'v2': IR_IMMUTABLE_ARRAY}
130 #
131 def fn2(n):
132 Base().base1 = 42
133 V().v1 = 43
134 V().v2 = ['x', 'y']
135 W().w1 = 44
136 return W()
137 _, _, graph = self.gengraph(fn2, [int])
138 w_inst = graph.getreturnvar()
139 TYPE = self.gettype(w_inst)
140 assert 'virtualizable_accessor' not in TYPE._hints
141
142 def replace_force_virtualizable(self, rtyper, graphs):
143 from rpython.annotator import model as annmodel
144 from rpython.rtyper.annlowlevel import MixLevelHelperAnnotator
145 graph = graphs[0]
146
147 for block, op in graph.iterblockops():
148 if op.opname == 'jit_force_virtualizable':
149 v_inst_ll_type = op.args[0].concretetype
150 break
151
152 def mycall(vinst_ll):
153 if vinst_ll.vable_token:
154 raise ValueError
155 annhelper = MixLevelHelperAnnotator(rtyper)
156 s_vinst = SomePtr(v_inst_ll_type)
157 funcptr = annhelper.delayedfunction(mycall, [s_vinst], annmodel.s_None)
158 annhelper.finish()
159 replace_force_virtualizable_with_call(graphs, v_inst_ll_type, funcptr)
160 return funcptr
161
162 def test_replace_force_virtualizable_with_call(self):
163 def fn(n):
164 vinst = V(n)
165 return vinst.v
166 _, rtyper, graph = self.gengraph(fn, [int])
167 block = graph.startblock
168 op_getfield = block.operations[-1]
169 assert op_getfield.opname == 'getfield'
170 funcptr = self.replace_force_virtualizable(rtyper, [graph])
171 if getattr(option, 'view', False):
172 graph.show()
173 op_promote = block.operations[-2]
174 op_getfield = block.operations[-1]
175 assert op_getfield.opname == 'getfield'
176 assert op_promote.opname == 'direct_call'
177 assert op_promote.args[0].value == funcptr
178 assert op_promote.args[1] == op_getfield.args[0]
179 #
180 interp = LLInterpreter(rtyper)
181 res = interp.eval_graph(graph, [61])
182 assert res == 61
183
184 def test_access_directly(self):
185 def g(b):
186 b.v0 += 1
187 return b.v0
188
189 def f(n):
190 b = B(n)
191 b = hint(b, access_directly=True)
192 return g(b)
193
194 t, typer, graph = self.gengraph(f, [int])
195 g_graph = t._graphof(g)
196
197 expected = [{'access_directly': True}] * 3
198 assert get_force_virtualizable_flags(g_graph) == expected
199
200 self.replace_force_virtualizable(typer, [g_graph])
201 assert summary(g_graph) == {self.GETFIELD: 2, self.SETFIELD: 1, 'int_add': 1}
202
203 res = self.interpret(f, [23])
204 assert res == 24
205
206 def test_access_directly_exception(self):
207 def g(b):
208 return b.v0
209
210 def f(n):
211 b = B(n)
212 b = hint(b, access_directly=True)
213 if not b.v0:
214 raise Exception
215 return g(b)
216
217 t, typer, graph = self.gengraph(f, [int])
218 f_graph = t._graphof(f)
219 g_graph = t._graphof(g)
220
221 self.replace_force_virtualizable(typer, [f_graph, g_graph])
222 t.checkgraphs()
223
224 res = self.interpret(f, [23])
225 assert res == 23
226
227 def test_access_directly_specialized(self):
228 def g(b):
229 return b.v0
230
231 def f(n):
232 b = B(n)
233 x = g(b)
234 y = g(hint(b, access_directly=True))
235 return x + y
236
237 t, typer, graph = self.gengraph(f, [int])
238 desc = typer.annotator.bookkeeper.getdesc(g)
239 g_graphs = desc._cache.items()
240 assert len(g_graphs) == 2
241 g_graphs.sort()
242 assert g_graphs[0][0] is None
243
244 assert get_force_virtualizable_flags(g_graphs[0][1]) == [{}]
245 expected = [{'access_directly': True}]
246 assert get_force_virtualizable_flags(g_graphs[1][1]) == expected
247
248 self.replace_force_virtualizable(typer, [g_graphs[0][1],
249 g_graphs[1][1]])
250
251 assert summary(g_graphs[0][1]) == {'direct_call': 1, self.GETFIELD: 1}
252 assert summary(g_graphs[1][1]) == {self.GETFIELD: 1}
253
254 res = self.interpret(f, [23])
255 assert res == 46
256
257 def test_access_directly_escape(self):
258 class Global:
259 pass
260 glob = Global()
261
262 def g(b):
263 glob.b = b
264
265 def h(b):
266 return b.v0
267
268 def f(n):
269 b = B(n)
270 g(b)
271 g(hint(b, access_directly=True))
272 return h(glob.b)
273
274 t, typer, graph = self.gengraph(f, [int])
275 desc = typer.annotator.bookkeeper.getdesc(g)
276 g_graphs = desc._cache.items()
277 assert len(g_graphs) == 2
278 g_graphs.sort()
279 assert g_graphs[0][0] is None
280 assert summary(g_graphs[0][1]) == {self.SETFIELD: 1}
281 assert summary(g_graphs[1][1]) == {self.SETFIELD: 1}
282
283 h_graph = t._graphof(h)
284 assert summary(h_graph) == {'jit_force_virtualizable': 1,
285 self.GETFIELD: 1}
286 assert get_force_virtualizable_flags(h_graph) == [{}]
287
288 res = self.interpret(f, [23])
289 assert res == 23
290
291 def test_access_directly_method(self):
292 class A:
293 _virtualizable_ = ['v0']
294
295 def __init__(self, v):
296 self.v0 = v
297
298 def meth1(self, x):
299 return self.g(x+1)
300
301 def g(self, y):
302 return self.v0 * y
303
304 def f(n):
305 a = A(n)
306 a = hint(a, access_directly=True)
307 return a.meth1(100)
308
309 t, typer, graph = self.gengraph(f, [int])
310 g_graph = t._graphof(A.g.im_func)
311
312 self.replace_force_virtualizable(typer, [g_graph])
313
314 assert summary(g_graph) == {self.GETFIELD: 1, 'int_mul': 1}
315
316 res = self.interpret(f, [23])
317 assert res == 2323
318
319 def test_access_directly_stop_at_dont_look_inside(self):
320 from rpython.rlib.jit import dont_look_inside
321
322 class A:
323 _virtualizable_ = ['x']
324
325 def h(a):
326 g(a)
327 h = dont_look_inside(h)
328
329 def g(a):
330 a.x = 2
331 h(a)
332
333 def f():
334 a = A()
335 a = hint(a, access_directly=True)
336 a.x = 1
337 g(a)
338
339 t, typer, graph = self.gengraph(f, [])
340
341 desc = typer.annotator.bookkeeper.getdesc(g)
342 g_graphs = desc._cache.items()
343 assert len(g_graphs) == 2
344 g_graphs.sort()
345
346 assert g_graphs[0][0] is None # default
347 g_graph = g_graphs[0][1]
348 g_graph_directly = g_graphs[1][1]
349
350 f_graph = t._graphof(f)
351 h_graph = t._graphof(h) # 1 graph!
352
353 def get_direct_call_graph(graph):
354 for block, op in graph.iterblockops():
355 if op.opname == 'direct_call':
356 return op.args[0].value._obj.graph
357 return None
358
359 assert get_direct_call_graph(f_graph) is g_graph_directly
360 assert get_direct_call_graph(g_graph) is h_graph
361 assert get_direct_call_graph(g_graph_directly) is h_graph
362 assert get_direct_call_graph(h_graph) is g_graph
363
364 def test_simple(self):
365 def f(v):
366 vinst = V(v)
367 return vinst, vinst.v
368 res = self.interpret(f, [42])
369 assert res.item1 == 42
370 res = lltype.normalizeptr(res.item0)
371 assert res.inst_v == 42
372 assert res.vable_token == lltype.nullptr(llmemory.GCREF.TO)