Debian Codegraph @

rpython/jit/backend/test/calling_convention_test.py

raw symbols SCIP
1
2 from rpython.jit.metainterp.history import BasicFinalDescr,\
3 JitCellToken, ConstInt, ConstFloat
4 from rpython.jit.metainterp.resoperation import rop, InputArgInt, InputArgFloat
5 from rpython.jit.metainterp.support import ptr2int
6 from rpython.jit.codewriter.effectinfo import EffectInfo
7 from rpython.jit.tool.oparser import parse
8 from rpython.rtyper.lltypesystem import lltype, llmemory
9 from rpython.rtyper.annlowlevel import llhelper
10 from rpython.jit.codewriter import longlong
11 from rpython.jit.backend.detect_cpu import getcpuclass
12 from rpython.jit.backend.test.runner_test import Runner
13 import py
14 import sys
15 import platform
16
17 def boxfloat(x):
18 return InputArgFloat(longlong.getfloatstorage(x))
19
20 def constfloat(x):
21 return ConstFloat(longlong.getfloatstorage(x))
22
23 class FakeStats(object):
24 pass
25
26 class CallingConvTests(Runner):
27 Ptr = lltype.Ptr
28 FuncType = lltype.FuncType
29
30 def setup_class(cls):
31 cls.cpu = getcpuclass()(rtyper=None, stats=FakeStats())
32 cls.cpu.setup_once()
33
34 def _prepare_args(self, args, floats, ints):
35 local_floats = list(floats)
36 local_ints = list(ints)
37 expected_result = 0.0
38 arguments = []
39 for i in range(len(args)):
40 x = args[i]
41 if x[0] == 'f':
42 x = local_floats.pop()
43 t = longlong.getfloatstorage(x)
44 arguments.append(t)
45 else:
46 x = local_ints.pop()
47 arguments.append(x)
48 expected_result += x
49 return arguments, expected_result
50
51 @classmethod
52 def get_funcbox(cls, cpu, func_ptr):
53 return ConstInt(ptr2int(func_ptr))
54
55 def test_call_aligned_with_spilled_values(self):
56 cpu = self.cpu
57 if not cpu.supports_floats:
58 py.test.skip('requires floats')
59
60
61 def func(*args):
62 return float(sum(args))
63
64 F = lltype.Float
65 I = lltype.Signed
66 floats = [0.7, 5.8, 0.1, 0.3, 0.9, -2.34, -3.45, -4.56]
67 ints = [7, 11, 23, 13, -42, 1111, 95, 1]
68 for case in range(256):
69 local_floats = list(floats)
70 local_ints = list(ints)
71 args = []
72 spills = []
73 funcargs = []
74 float_count = 0
75 int_count = 0
76 for i in range(8):
77 if case & (1<<i):
78 args.append('f%d' % float_count)
79 spills.append('force_spill(f%d)' % float_count)
80 float_count += 1
81 funcargs.append(F)
82 else:
83 args.append('i%d' % int_count)
84 spills.append('force_spill(i%d)' % int_count)
85 int_count += 1
86 funcargs.append(I)
87
88 arguments = ', '.join(args)
89 spill_ops = '\n'.join(spills)
90
91 FUNC = self.FuncType(funcargs, F)
92 FPTR = self.Ptr(FUNC)
93 func_ptr = llhelper(FPTR, func)
94 calldescr = cpu.calldescrof(FUNC, FUNC.ARGS, FUNC.RESULT,
95 EffectInfo.MOST_GENERAL)
96 funcbox = self.get_funcbox(cpu, func_ptr)
97
98 ops = '[%s]\n' % arguments
99 ops += '%s\n' % spill_ops
100 ops += 'f99 = call_f(ConstClass(func_ptr), %s, descr=calldescr)\n' % arguments
101 ops += 'i99 = same_as_i(0)\n'
102 ops += 'guard_true(i99) [f99, %s]\n' % arguments
103 ops += 'finish()\n'
104
105 loop = parse(ops, namespace=locals())
106 looptoken = JitCellToken()
107 self.cpu.compile_loop(loop.inputargs, loop.operations, looptoken)
108 argvals, expected_result = self._prepare_args(args, floats, ints)
109
110 deadframe = self.cpu.execute_token(looptoken, *argvals)
111 x = longlong.getrealfloat(cpu.get_float_value(deadframe, 0))
112 assert abs(x - expected_result) < 0.0001
113
114 def test_call_aligned_with_imm_values(self):
115 cpu = self.cpu
116 if not cpu.supports_floats:
117 py.test.skip('requires floats')
118
119
120 def func(*args):
121 return float(sum(args))
122
123 F = lltype.Float
124 I = lltype.Signed
125 floats = [0.7, 5.8, 0.1, 0.3, 0.9, -2.34, -3.45, -4.56]
126 ints = [7, 11, 23, 13, -42, 1111, 95, 1]
127 for case in range(256):
128 result = 0.0
129 args = []
130 argslist = []
131 local_floats = list(floats)
132 local_ints = list(ints)
133 for i in range(8):
134 if case & (1<<i):
135 args.append(F)
136 arg = local_floats.pop()
137 result += arg
138 argslist.append(constfloat(arg))
139 else:
140 args.append(I)
141 arg = local_ints.pop()
142 result += arg
143 argslist.append(ConstInt(arg))
144 FUNC = self.FuncType(args, F)
145 FPTR = self.Ptr(FUNC)
146 func_ptr = llhelper(FPTR, func)
147 calldescr = cpu.calldescrof(FUNC, FUNC.ARGS, FUNC.RESULT,
148 EffectInfo.MOST_GENERAL)
149 funcbox = self.get_funcbox(cpu, func_ptr)
150
151 res = self.execute_operation(rop.CALL_F,
152 [funcbox] + argslist,
153 'float', descr=calldescr)
154 res = longlong.getrealfloat(res)
155 assert abs(res - result) < 0.0001
156
157 def test_call_aligned_with_args_on_the_stack(self):
158 cpu = self.cpu
159 if not cpu.supports_floats:
160 py.test.skip('requires floats')
161
162
163 def func(*args):
164 return float(sum(args))
165
166 F = lltype.Float
167 I = lltype.Signed
168 floats = [0.7, 5.8, 0.1, 0.3, 0.9, -2.34, -3.45, -4.56]
169 ints = [7, 11, 23, 13, -42, 1111, 95, 1]
170 for case in range(256):
171 result = 0.0
172 args = []
173 argslist = []
174 local_floats = list(floats)
175 local_ints = list(ints)
176 for i in range(8):
177 if case & (1<<i):
178 args.append(F)
179 arg = local_floats.pop()
180 result += arg
181 argslist.append(boxfloat(arg))
182 else:
183 args.append(I)
184 arg = local_ints.pop()
185 result += arg
186 argslist.append(InputArgInt(arg))
187 FUNC = self.FuncType(args, F)
188 FPTR = self.Ptr(FUNC)
189 func_ptr = llhelper(FPTR, func)
190 calldescr = cpu.calldescrof(FUNC, FUNC.ARGS, FUNC.RESULT,
191 EffectInfo.MOST_GENERAL)
192 funcbox = self.get_funcbox(cpu, func_ptr)
193
194 res = self.execute_operation(rop.CALL_F,
195 [funcbox] + argslist,
196 'float', descr=calldescr)
197 res = longlong.getrealfloat(res)
198 assert abs(res - result) < 0.0001
199
200 def test_call_alignment_call_assembler(self):
201 cpu = self.cpu
202 if not cpu.supports_floats:
203 py.test.skip('requires floats')
204
205 fdescr3 = BasicFinalDescr(3)
206 fdescr4 = BasicFinalDescr(4)
207
208 def assembler_helper(failindex, virtualizable):
209 assert 0, 'should not be called, but was with failindex (%d)' % failindex
210 return 13
211
212 FUNCPTR = lltype.Ptr(lltype.FuncType([lltype.Signed, llmemory.GCREF],
213 lltype.Signed))
214 class FakeJitDriverSD:
215 index_of_virtualizable = -1
216 _assembler_helper_ptr = llhelper(FUNCPTR, assembler_helper)
217 assembler_helper_adr = llmemory.cast_ptr_to_adr(
218 _assembler_helper_ptr)
219
220 floats = [0.7, 5.8, 0.1, 0.3, 0.9, -2.34, -3.45, -4.56]
221 ints = [7, 11, 23, 42, -42, 1111, 95, 1]
222
223 for case in range(256):
224 float_count = 0
225 int_count = 0
226 args = []
227 called_ops = ''
228 total_index = -1
229 for i in range(8):
230 if case & (1<<i):
231 args.append('f%d' % float_count)
232 else:
233 args.append('i%d' % int_count)
234 called_ops += 'f%d = cast_int_to_float(i%d)\n' % (
235 float_count, int_count)
236 int_count += 1
237 if total_index == -1:
238 total_index = float_count
239 float_count += 1
240 else:
241 called_ops += 'f%d = float_add(f%d, f%d)\n' % (
242 float_count + 1, total_index, float_count)
243 total_index = float_count + 1
244 float_count += 2
245 arguments = ', '.join(args)
246 called_ops = '[%s]\n' % arguments + called_ops
247 called_ops += 'finish(f%d, descr=fdescr3)\n' % total_index
248 # compile called loop
249 called_loop = parse(called_ops, namespace=locals())
250 called_looptoken = JitCellToken()
251 called_looptoken.outermost_jitdriver_sd = FakeJitDriverSD()
252 done_descr = called_loop.operations[-1].getdescr()
253 self.cpu.compile_loop(called_loop.inputargs, called_loop.operations, called_looptoken)
254
255 argvals, expected_result = self._prepare_args(args, floats, ints)
256 deadframe = cpu.execute_token(called_looptoken, *argvals)
257 assert cpu.get_latest_descr(deadframe) == fdescr3
258 t = longlong.getrealfloat(cpu.get_float_value(deadframe, 0))
259 assert abs(t - expected_result) < 0.0001
260
261 ARGS = []
262 RES = lltype.Float
263 for x in args:
264 if x[0] == 'f':
265 ARGS.append(lltype.Float)
266 else:
267 ARGS.append(lltype.Signed)
268 FakeJitDriverSD.portal_calldescr = self.cpu.calldescrof(
269 lltype.Ptr(lltype.FuncType(ARGS, RES)), ARGS, RES,
270 EffectInfo.MOST_GENERAL)
271 ops = '''
272 [%s]
273 f99 = call_assembler_f(%s, descr=called_looptoken)
274 guard_not_forced()[]
275 finish(f99, descr=fdescr4)
276 ''' % (arguments, arguments)
277 loop = parse(ops, namespace=locals())
278 # we want to take the fast path
279 self.cpu.done_with_this_frame_descr_float = done_descr
280 try:
281 othertoken = JitCellToken()
282 self.cpu.compile_loop(loop.inputargs, loop.operations, othertoken)
283
284 # prepare call to called_loop
285 argvals, _ = self._prepare_args(args, floats, ints)
286 deadframe = cpu.execute_token(othertoken, *argvals)
287 x = longlong.getrealfloat(
288 cpu.get_float_value(deadframe, 0))
289 assert cpu.get_latest_descr(deadframe) == fdescr4
290 assert abs(x - expected_result) < 0.0001
291 finally:
292 del self.cpu.done_with_this_frame_descr_float
293
294
295 def test_call_with_imm_values_bug_constint0(self):
296 cpu = self.cpu
297
298 I = lltype.Signed
299 ints = [7, 11, 23, 13, -42, 0, 0, 9]
300
301 def func(*args):
302 for i in range(len(args)):
303 assert args[i] == ints[i]
304 return sum(args)
305
306 result = sum(ints)
307 args = [I] * len(ints)
308 argslist = [ConstInt(i) for i in ints]
309 FUNC = self.FuncType(args, I)
310 FPTR = self.Ptr(FUNC)
311 func_ptr = llhelper(FPTR, func)
312 calldescr = cpu.calldescrof(FUNC, FUNC.ARGS, FUNC.RESULT, EffectInfo.MOST_GENERAL)
313 funcbox = self.get_funcbox(cpu, func_ptr)
314
315 res = self.execute_operation(rop.CALL_I,
316 [funcbox] + argslist,
317 'int', descr=calldescr)
318 assert res == result
319
320
321 def test_call_with_singlefloats(self):
322 cpu = self.cpu
323 if not cpu.supports_floats or not cpu.supports_singlefloats:
324 py.test.skip('requires floats and singlefloats')
325
326 import random
327 from rpython.rlib.rarithmetic import r_singlefloat
328
329 def func(*args):
330 res = 0.0
331 for i, x in enumerate(args):
332 res += (i + 1.1) * float(x)
333 return res
334
335 F = lltype.Float
336 S = lltype.SingleFloat
337 I = lltype.Signed
338 floats = [random.random() - 0.5 for i in range(20)]
339 singlefloats = [r_singlefloat(random.random() - 0.5) for i in range(20)]
340 ints = [random.randrange(-99, 99) for i in range(20)]
341 for repeat in range(100):
342 args = []
343 argvalues = []
344 argslist = []
345 local_floats = list(floats)
346 local_singlefloats = list(singlefloats)
347 local_ints = list(ints)
348 for i in range(random.randrange(4, 20)):
349 case = random.randrange(0, 6)
350 if case & 1: boxme = InputArgInt
351 else: boxme = ConstInt
352 if case < 2:
353 args.append(F)
354 arg = arg1 = local_floats.pop()
355 if case & 1: boxme = boxfloat
356 else: boxme = constfloat
357 elif case < 4:
358 args.append(S)
359 arg = local_singlefloats.pop()
360 arg1 = longlong.singlefloat2int(arg)
361 else:
362 args.append(I)
363 arg = arg1 = local_ints.pop()
364 argslist.append(boxme(arg1))
365 argvalues.append(arg)
366 FUNC = self.FuncType(args, F)
367 FPTR = self.Ptr(FUNC)
368 func_ptr = llhelper(FPTR, func)
369 calldescr = cpu.calldescrof(FUNC, FUNC.ARGS, FUNC.RESULT,
370 EffectInfo.MOST_GENERAL)
371 funcbox = self.get_funcbox(cpu, func_ptr)
372
373 res = self.execute_operation(rop.CALL_F,
374 [funcbox] + argslist,
375 'float', descr=calldescr)
376 expected = func(*argvalues)
377 res = longlong.getrealfloat(res)
378 assert abs(res - expected) < 0.0001
379
380
381 def make_function_returning_stack_pointer(self):
382 raise NotImplementedError
383
384 def get_alignment_requirements(self):
385 raise NotImplementedError
386
387 def test_call_aligned_explicit_check(self):
388 if (not platform.machine().startswith('arm') and
389 sys.maxint == 2 ** 31 - 1): # XXX is still necessary on x86?
390 py.test.skip("libffi on 32bit is broken")
391 cpu = self.cpu
392 if not cpu.supports_floats:
393 py.test.skip('requires floats')
394
395 func_addr = self.make_function_returning_stack_pointer()
396
397 F = lltype.Float
398 I = lltype.Signed
399 floats = [0.7, 5.8, 0.1, 0.3, 0.9, -2.34, -3.45, -4.56]
400 ints = [7, 11, 23, 13, -42, 1111, 95, 1]
401 for case in range(256):
402 args = []
403 funcargs = []
404 float_count = 0
405 int_count = 0
406 for i in range(8):
407 if case & (1<<i):
408 args.append('f%d' % float_count)
409 float_count += 1
410 funcargs.append(F)
411 else:
412 args.append('i%d' % int_count)
413 int_count += 1
414 funcargs.append(I)
415
416 arguments = ', '.join(args)
417
418 FUNC = self.FuncType(funcargs, I)
419 calldescr = cpu.calldescrof(FUNC, FUNC.ARGS, FUNC.RESULT,
420 EffectInfo.MOST_GENERAL)
421
422 ops = '[%s]\n' % arguments
423 ops += 'i99 = call_i(%d, %s, descr=calldescr)\n' % (func_addr,
424 arguments)
425 ops += 'finish(i99)\n'
426
427 loop = parse(ops, namespace=locals())
428 looptoken = JitCellToken()
429 self.cpu.compile_loop(loop.inputargs, loop.operations, looptoken)
430 argvals, expected_result = self._prepare_args(args, floats, ints)
431
432 deadframe = self.cpu.execute_token(looptoken, *argvals)
433 x = cpu.get_int_value(deadframe, 0)
434 align_req = self.get_alignment_requirements()
435 assert x % align_req == 0