Debian Codegraph @

rpython/jit/codewriter/test/test_call.py

raw symbols SCIP
1 import py
2
3 from rpython.flowspace.model import SpaceOperation, Constant, Variable
4 from rpython.rtyper.lltypesystem import lltype, llmemory, rffi
5 from rpython.translator.unsimplify import varoftype
6 from rpython.rlib import jit
7 from rpython.jit.codewriter import support, call
8 from rpython.jit.codewriter.call import CallControl
9 from rpython.jit.codewriter.effectinfo import EffectInfo
10
11
12 class FakePolicy:
13 def look_inside_graph(self, graph):
14 return True
15
16
17 def test_graphs_from_direct_call():
18 cc = CallControl()
19 F = lltype.FuncType([], lltype.Signed)
20 f = lltype.functionptr(F, 'f', graph='fgraph')
21 v = varoftype(lltype.Signed)
22 op = SpaceOperation('direct_call', [Constant(f, lltype.Ptr(F))], v)
23 #
24 lst = cc.graphs_from(op, {}.__contains__)
25 assert lst is None # residual call
26 #
27 lst = cc.graphs_from(op, {'fgraph': True}.__contains__)
28 assert lst == ['fgraph'] # normal call
29
30 def test_graphs_from_indirect_call():
31 cc = CallControl()
32 F = lltype.FuncType([], lltype.Signed)
33 v = varoftype(lltype.Signed)
34 graphlst = ['f1graph', 'f2graph']
35 op = SpaceOperation('indirect_call', [varoftype(lltype.Ptr(F)),
36 Constant(graphlst, lltype.Void)], v)
37 #
38 lst = cc.graphs_from(op, {'f1graph': True, 'f2graph': True}.__contains__)
39 assert lst == ['f1graph', 'f2graph'] # normal indirect call
40 #
41 lst = cc.graphs_from(op, {'f1graph': True}.__contains__)
42 assert lst == ['f1graph'] # indirect call, look only inside some graphs
43 #
44 lst = cc.graphs_from(op, {}.__contains__)
45 assert lst is None # indirect call, don't look inside any graph
46
47 def test_graphs_from_no_target():
48 cc = CallControl()
49 F = lltype.FuncType([], lltype.Signed)
50 v = varoftype(lltype.Signed)
51 op = SpaceOperation('indirect_call', [varoftype(lltype.Ptr(F)),
52 Constant(None, lltype.Void)], v)
53 lst = cc.graphs_from(op, {}.__contains__)
54 assert lst is None
55
56 # ____________________________________________________________
57
58 class FakeJitDriverSD:
59 def __init__(self, portal_graph):
60 self.portal_graph = portal_graph
61 self.portal_runner_ptr = "???"
62
63 def test_find_all_graphs():
64 def g(x):
65 return x + 2
66 def f(x):
67 return g(x) + 1
68 rtyper = support.annotate(f, [7])
69 jitdriver_sd = FakeJitDriverSD(rtyper.annotator.translator.graphs[0])
70 cc = CallControl(jitdrivers_sd=[jitdriver_sd])
71 res = cc.find_all_graphs(FakePolicy())
72 funcs = set([graph.func for graph in res])
73 assert funcs == set([f, g])
74
75 def test_find_all_graphs_without_g():
76 def g(x):
77 return x + 2
78 def f(x):
79 return g(x) + 1
80 rtyper = support.annotate(f, [7])
81 jitdriver_sd = FakeJitDriverSD(rtyper.annotator.translator.graphs[0])
82 cc = CallControl(jitdrivers_sd=[jitdriver_sd])
83 class CustomFakePolicy:
84 def look_inside_graph(self, graph):
85 assert graph.name == 'g'
86 return False
87 res = cc.find_all_graphs(CustomFakePolicy())
88 funcs = [graph.func for graph in res]
89 assert funcs == [f]
90
91 # ____________________________________________________________
92
93 def test_guess_call_kind_and_calls_from_graphs():
94 class portal_runner_obj:
95 graph = object()
96 class FakeJitDriverSD:
97 portal_runner_ptr = portal_runner_obj
98 g = object()
99 g1 = object()
100 cc = CallControl(jitdrivers_sd=[FakeJitDriverSD()])
101 cc.candidate_graphs = [g, g1]
102
103 op = SpaceOperation('direct_call', [Constant(portal_runner_obj)],
104 Variable())
105 assert cc.guess_call_kind(op) == 'recursive'
106
107 class fakeresidual:
108 _obj = object()
109 op = SpaceOperation('direct_call', [Constant(fakeresidual)],
110 Variable())
111 assert cc.guess_call_kind(op) == 'residual'
112
113 class funcptr:
114 class _obj:
115 class graph:
116 class func:
117 oopspec = "spec"
118 op = SpaceOperation('direct_call', [Constant(funcptr)],
119 Variable())
120 assert cc.guess_call_kind(op) == 'builtin'
121
122 class funcptr:
123 class _obj:
124 graph = g
125 op = SpaceOperation('direct_call', [Constant(funcptr)],
126 Variable())
127 res = cc.graphs_from(op)
128 assert res == [g]
129 assert cc.guess_call_kind(op) == 'regular'
130
131 class funcptr:
132 class _obj:
133 graph = object()
134 op = SpaceOperation('direct_call', [Constant(funcptr)],
135 Variable())
136 res = cc.graphs_from(op)
137 assert res is None
138 assert cc.guess_call_kind(op) == 'residual'
139
140 h = object()
141 op = SpaceOperation('indirect_call', [Variable(),
142 Constant([g, g1, h])],
143 Variable())
144 res = cc.graphs_from(op)
145 assert res == [g, g1]
146 assert cc.guess_call_kind(op) == 'regular'
147
148 op = SpaceOperation('indirect_call', [Variable(),
149 Constant([h])],
150 Variable())
151 res = cc.graphs_from(op)
152 assert res is None
153 assert cc.guess_call_kind(op) == 'residual'
154
155 # ____________________________________________________________
156
157 def test_get_jitcode(monkeypatch):
158 from rpython.jit.codewriter.test.test_flatten import FakeCPU
159 class FakeRTyper:
160 class annotator:
161 translator = None
162
163 def getfunctionptr(graph):
164 F = lltype.FuncType([], lltype.Signed)
165 return lltype.functionptr(F, 'bar')
166
167 monkeypatch.setattr(call, 'getfunctionptr', getfunctionptr)
168 cc = CallControl(FakeCPU(FakeRTyper()))
169 class somegraph:
170 name = "foo"
171 jitcode = cc.get_jitcode(somegraph)
172 assert jitcode is cc.get_jitcode(somegraph) # caching
173 assert jitcode.name == "foo"
174 pending = list(cc.enum_pending_graphs())
175 assert pending == [(somegraph, jitcode)]
176
177 # ____________________________________________________________
178
179 def test_jit_force_virtualizable_effectinfo():
180 py.test.skip("XXX add a test for CallControl.getcalldescr() -> EF_xxx")
181
182 def test_releases_gil_analyzer():
183 from rpython.jit.backend.llgraph.runner import LLGraphCPU
184
185 T = rffi.CArrayPtr(rffi.TIME_T)
186 external = rffi.llexternal("time", [T], rffi.TIME_T, releasegil=True)
187
188 @jit.dont_look_inside
189 def f():
190 return external(lltype.nullptr(T.TO))
191
192 rtyper = support.annotate(f, [])
193 jitdriver_sd = FakeJitDriverSD(rtyper.annotator.translator.graphs[0])
194 cc = CallControl(LLGraphCPU(rtyper), jitdrivers_sd=[jitdriver_sd])
195 res = cc.find_all_graphs(FakePolicy())
196
197 [f_graph] = [x for x in res if x.func is f]
198 [block, _] = list(f_graph.iterblocks())
199 [op] = block.operations
200 call_descr = cc.getcalldescr(op)
201 assert call_descr.extrainfo.has_random_effects()
202 assert call_descr.extrainfo.is_call_release_gil() is False
203
204 def test_call_release_gil():
205 from rpython.jit.backend.llgraph.runner import LLGraphCPU
206
207 T = rffi.CArrayPtr(rffi.TIME_T)
208 external = rffi.llexternal("time", [T], rffi.TIME_T, releasegil=True,
209 save_err=rffi.RFFI_SAVE_ERRNO)
210
211 # no jit.dont_look_inside in this test
212 def f():
213 return external(lltype.nullptr(T.TO))
214
215 rtyper = support.annotate(f, [])
216 jitdriver_sd = FakeJitDriverSD(rtyper.annotator.translator.graphs[0])
217 cc = CallControl(LLGraphCPU(rtyper), jitdrivers_sd=[jitdriver_sd])
218 res = cc.find_all_graphs(FakePolicy())
219
220 [llext_graph] = [x for x in res if x.func is external]
221 [block, _] = list(llext_graph.iterblocks())
222 [op] = block.operations
223 tgt_tuple = op.args[0].value._obj.graph.func._call_aroundstate_target_
224 assert type(tgt_tuple) is tuple and len(tgt_tuple) == 2
225 call_target, saveerr = tgt_tuple
226 assert saveerr == rffi.RFFI_SAVE_ERRNO
227 call_target = llmemory.cast_ptr_to_adr(call_target)
228 call_descr = cc.getcalldescr(op)
229 assert call_descr.extrainfo.has_random_effects()
230 assert call_descr.extrainfo.is_call_release_gil() is True
231 assert call_descr.extrainfo.call_release_gil_target == (
232 call_target, rffi.RFFI_SAVE_ERRNO)
233
234 def test_random_effects_on_stacklet_switch():
235 from rpython.jit.backend.llgraph.runner import LLGraphCPU
236 from rpython.translator.platform import CompilationError
237 try:
238 from rpython.rlib._rffi_stacklet import switch, handle
239 except CompilationError as e:
240 if "Unsupported platform!" in e.out:
241 py.test.skip("Unsupported platform!")
242 else:
243 raise e
244 @jit.dont_look_inside
245 def f():
246 switch(rffi.cast(handle, 0))
247
248 rtyper = support.annotate(f, [])
249 jitdriver_sd = FakeJitDriverSD(rtyper.annotator.translator.graphs[0])
250 cc = CallControl(LLGraphCPU(rtyper), jitdrivers_sd=[jitdriver_sd])
251 res = cc.find_all_graphs(FakePolicy())
252
253 [f_graph] = [x for x in res if x.func is f]
254 [block, _] = list(f_graph.iterblocks())
255 op = block.operations[-1]
256 call_descr = cc.getcalldescr(op)
257 assert call_descr.extrainfo.has_random_effects()
258
259 def test_no_random_effects_for_rotateLeft():
260 from rpython.jit.backend.llgraph.runner import LLGraphCPU
261 from rpython.rlib.rarithmetic import r_uint
262
263 if r_uint.BITS == 32:
264 py.test.skip("64-bit only")
265
266 from rpython.rlib.rmd5 import _rotateLeft
267 def f(n, m):
268 return _rotateLeft(r_uint(n), m)
269
270 rtyper = support.annotate(f, [7, 9])
271 jitdriver_sd = FakeJitDriverSD(rtyper.annotator.translator.graphs[0])
272 cc = CallControl(LLGraphCPU(rtyper), jitdrivers_sd=[jitdriver_sd])
273 res = cc.find_all_graphs(FakePolicy())
274
275 [f_graph] = [x for x in res if x.func is f]
276 [block, _] = list(f_graph.iterblocks())
277 op = block.operations[-1]
278 call_descr = cc.getcalldescr(op)
279 assert not call_descr.extrainfo.has_random_effects()
280 assert call_descr.extrainfo.check_is_elidable()
281
282 def test_elidable_kinds():
283 from rpython.jit.backend.llgraph.runner import LLGraphCPU
284 from rpython.rlib.objectmodel import compute_hash
285 from rpython.rlib.rsiphash import enable_siphash24
286
287 @jit.elidable
288 def f1(n, m):
289 return n + m
290 @jit.elidable
291 def f2(n, m):
292 return [n, m] # may raise MemoryError
293 @jit.elidable
294 def f3(n, m):
295 if n > m:
296 raise ValueError
297 return n + m
298 @jit.elidable
299 def f4(n, m):
300 return compute_hash(str(n) + str(m))
301
302 T = rffi.CArrayPtr(rffi.TIME_T)
303 external = rffi.llexternal("time", [T], rffi.TIME_T, releasegil=True)
304
305 def effect():
306 return external(lltype.nullptr(T.TO))
307
308 @jit.elidable
309 def f5(n, m):
310 effect()
311 return 1
312
313 def f(n, m):
314 a = f1(n, m)
315 b = f2(n, m)
316 c = f3(n, m)
317 d = f4(n, m)
318 f5(n, m)
319 enable_siphash24()
320 return a + len(b) + c + d
321
322 rtyper = support.annotate(f, [7, 9])
323 jitdriver_sd = FakeJitDriverSD(rtyper.annotator.translator.graphs[0])
324 cc = CallControl(LLGraphCPU(rtyper), jitdrivers_sd=[jitdriver_sd])
325 res = cc.find_all_graphs(FakePolicy())
326 [f_graph] = [x for x in res if x.func is f]
327
328 for index, expected in [
329 (0, EffectInfo.EF_ELIDABLE_CANNOT_RAISE),
330 (1, EffectInfo.EF_ELIDABLE_OR_MEMORYERROR),
331 (2, EffectInfo.EF_ELIDABLE_CAN_RAISE),
332 (3, EffectInfo.EF_ELIDABLE_OR_MEMORYERROR)]:
333 call_op = f_graph.startblock.operations[index]
334 assert call_op.opname == 'direct_call'
335 call_descr = cc.getcalldescr(call_op)
336 assert call_descr.extrainfo.extraeffect == expected
337
338 call_op = f_graph.startblock.operations[4]
339 assert call_op.opname == 'direct_call'
340 excinfo = py.test.raises(Exception, cc.getcalldescr, call_op)
341 lines = excinfo.value.args[0].splitlines()
342 assert "f5" in lines[2]
343 assert "effect" in lines[3]
344 assert "random effects" in lines[-1]
345
346 def test_raise_elidable_no_result():
347 from rpython.jit.backend.llgraph.runner import LLGraphCPU
348 l = []
349 @jit.elidable
350 def f1(n, m):
351 l.append(n)
352 def fancy_graph_name(n, m):
353 f1(n, m)
354 return n + m
355
356 rtyper = support.annotate(fancy_graph_name, [7, 9])
357 jitdriver_sd = FakeJitDriverSD(rtyper.annotator.translator.graphs[0])
358 cc = CallControl(LLGraphCPU(rtyper), jitdrivers_sd=[jitdriver_sd])
359 res = cc.find_all_graphs(FakePolicy())
360 [f_graph] = [x for x in res if x.func is fancy_graph_name]
361 call_op = f_graph.startblock.operations[0]
362 assert call_op.opname == 'direct_call'
363 x = py.test.raises(Exception, cc.getcalldescr, call_op, calling_graph=f_graph)
364 assert "fancy_graph_name" in str(x.value)
365
366 def test_can_or_cannot_collect():
367 from rpython.jit.backend.llgraph.runner import LLGraphCPU
368 prebuilts = [[5], [6]]
369 l = []
370 def f1(n):
371 if n > 1:
372 raise IndexError
373 return prebuilts[n] # cannot collect
374 f1._dont_inline_ = True
375
376 def f2(n):
377 return [n] # can collect
378 f2._dont_inline_ = True
379
380 def f(n):
381 a = f1(n)
382 b = f2(n)
383 return len(a) + len(b)
384
385 rtyper = support.annotate(f, [1])
386 jitdriver_sd = FakeJitDriverSD(rtyper.annotator.translator.graphs[0])
387 cc = CallControl(LLGraphCPU(rtyper), jitdrivers_sd=[jitdriver_sd])
388 res = cc.find_all_graphs(FakePolicy())
389 [f_graph] = [x for x in res if x.func is f]
390 for index, expected in [
391 (0, False), # f1()
392 (1, True), # f2()
393 (2, False), # len()
394 (3, False)]: # len()
395 call_op = f_graph.startblock.operations[index]
396 assert call_op.opname == 'direct_call'
397 call_descr = cc.getcalldescr(call_op)
398 assert call_descr.extrainfo.check_can_collect() == expected