| 1 |
import py |
| 2 |
from _pytest.monkeypatch import monkeypatch |
| 3 |
import sys |
| 4 |
import ctypes, math |
| 5 |
from rpython.rtyper.lltypesystem import lltype, rffi |
| 6 |
from rpython.rtyper.annlowlevel import llhelper |
| 7 |
from rpython.jit.metainterp.test.support import LLJitMixin |
| 8 |
from rpython.jit.codewriter.longlong import is_longlong, is_64_bit |
| 9 |
from rpython.rlib import jit |
| 10 |
from rpython.rlib import jit_libffi |
| 11 |
from rpython.rlib.jit_libffi import (types, CIF_DESCRIPTION, FFI_TYPE_PP, |
| 12 |
jit_ffi_call) |
| 13 |
from rpython.rlib.unroll import unrolling_iterable |
| 14 |
from rpython.rlib.rarithmetic import intmask, r_longlong, r_singlefloat, r_uint |
| 15 |
from rpython.rlib.longlong2float import float2longlong |
| 16 |
|
| 17 |
def get_description(atypes, rtype): |
| 18 |
p = lltype.malloc(CIF_DESCRIPTION, len(atypes), |
| 19 |
flavor='raw', immortal=True) |
| 20 |
p.abi = 1 |
| 21 |
p.nargs = len(atypes) |
| 22 |
p.rtype = rtype |
| 23 |
p.atypes = lltype.malloc(FFI_TYPE_PP.TO, len(atypes), |
| 24 |
flavor='raw', immortal=True) |
| 25 |
for i in range(len(atypes)): |
| 26 |
p.atypes[i] = atypes[i] |
| 27 |
return p |
| 28 |
|
| 29 |
class FakeFFI(object): |
| 30 |
""" |
| 31 |
Context manager to monkey patch jit_libffi with our custom "libffi-like" |
| 32 |
function |
| 33 |
""" |
| 34 |
|
| 35 |
def __init__(self, fake_call_impl_any): |
| 36 |
self.fake_call_impl_any = fake_call_impl_any |
| 37 |
self.monkey = monkeypatch() |
| 38 |
|
| 39 |
def __enter__(self, *args): |
| 40 |
self.monkey.setattr(jit_libffi, 'jit_ffi_call_impl_any', self.fake_call_impl_any) |
| 41 |
|
| 42 |
def __exit__(self, *args): |
| 43 |
self.monkey.undo() |
| 44 |
|
| 45 |
|
| 46 |
class FfiCallTests(object): |
| 47 |
|
| 48 |
def _run(self, atypes, rtype, avalues, rvalue, |
| 49 |
expected_call_release_gil_i=1, |
| 50 |
expected_call_release_gil_f=0, |
| 51 |
expected_call_release_gil_n=0, |
| 52 |
expected_call_may_force_f=0, |
| 53 |
supports_floats=True, |
| 54 |
supports_longlong=False, |
| 55 |
supports_singlefloats=False): |
| 56 |
|
| 57 |
cif_description = get_description(atypes, rtype) |
| 58 |
|
| 59 |
def verify(*args): |
| 60 |
for a, exp_a in zip(args, avalues): |
| 61 |
if (lltype.typeOf(exp_a) == rffi.ULONG and |
| 62 |
lltype.typeOf(a) == lltype.Signed): |
| 63 |
a = rffi.cast(rffi.ULONG, a) |
| 64 |
assert a == exp_a |
| 65 |
return rvalue |
| 66 |
FUNC = lltype.FuncType([lltype.typeOf(avalue) for avalue in avalues], |
| 67 |
lltype.typeOf(rvalue)) |
| 68 |
func = lltype.functionptr(FUNC, 'verify', _callable=verify) |
| 69 |
func_addr = rffi.cast(rffi.VOIDP, func) |
| 70 |
|
| 71 |
for i in range(len(avalues)): |
| 72 |
cif_description.exchange_args[i] = (i+1) * 16 |
| 73 |
cif_description.exchange_result = (len(avalues)+1) * 16 |
| 74 |
|
| 75 |
unroll_avalues = unrolling_iterable(avalues) |
| 76 |
BIG_ENDIAN = (sys.byteorder == 'big') |
| 77 |
|
| 78 |
def fake_call_impl_any(cif_description, func_addr, exchange_buffer): |
| 79 |
ofs = 16 |
| 80 |
for avalue in unroll_avalues: |
| 81 |
TYPE = rffi.CArray(lltype.typeOf(avalue)) |
| 82 |
data = rffi.ptradd(exchange_buffer, ofs) |
| 83 |
got = rffi.cast(lltype.Ptr(TYPE), data)[0] |
| 84 |
tp = lltype.typeOf(avalue) |
| 85 |
if tp is lltype.SingleFloat: |
| 86 |
got = float(got) |
| 87 |
avalue = float(avalue) |
| 88 |
elif (isinstance(tp, lltype.Number) and |
| 89 |
rffi.sizeof(tp) < rffi.sizeof(lltype.Signed)): |
| 90 |
got = intmask(got) |
| 91 |
avalue = intmask(avalue) |
| 92 |
assert got == avalue |
| 93 |
ofs += 16 |
| 94 |
write_to_ofs = 0 |
| 95 |
if rvalue is not None: |
| 96 |
write_rvalue = rvalue |
| 97 |
if BIG_ENDIAN: |
| 98 |
if (lltype.typeOf(write_rvalue) is rffi.SIGNEDCHAR or |
| 99 |
lltype.typeOf(write_rvalue) is rffi.UCHAR): |
| 100 |
|
| 101 |
write_to_ofs = rffi.sizeof(rffi.LONG) - 1 |
| 102 |
else: |
| 103 |
write_rvalue = 12923 |
| 104 |
TYPE = rffi.CArray(lltype.typeOf(write_rvalue)) |
| 105 |
data = rffi.ptradd(exchange_buffer, ofs) |
| 106 |
rffi.cast(lltype.Ptr(TYPE), data)[write_to_ofs] = write_rvalue |
| 107 |
|
| 108 |
def f(i): |
| 109 |
exbuf = lltype.malloc(rffi.CCHARP.TO, (len(avalues)+2) * 16, |
| 110 |
flavor='raw') |
| 111 |
|
| 112 |
targetptr = rffi.ptradd(exbuf, 16) |
| 113 |
for avalue in unroll_avalues: |
| 114 |
TYPE = rffi.CArray(lltype.typeOf(avalue)) |
| 115 |
if i >= 9: |
| 116 |
pass |
| 117 |
rffi.cast(lltype.Ptr(TYPE), targetptr)[0] = avalue |
| 118 |
targetptr = rffi.ptradd(targetptr, 16) |
| 119 |
|
| 120 |
jit_ffi_call(cif_description, func_addr, exbuf) |
| 121 |
|
| 122 |
if rvalue is None: |
| 123 |
res = 654321 |
| 124 |
else: |
| 125 |
TYPE = rffi.CArray(lltype.typeOf(rvalue)) |
| 126 |
res = rffi.cast(lltype.Ptr(TYPE), targetptr)[0] |
| 127 |
lltype.free(exbuf, flavor='raw') |
| 128 |
if lltype.typeOf(res) is lltype.SingleFloat: |
| 129 |
res = float(res) |
| 130 |
return res |
| 131 |
|
| 132 |
def matching_result(res, rvalue): |
| 133 |
if rvalue is None: |
| 134 |
return res == 654321 |
| 135 |
if isinstance(rvalue, r_singlefloat): |
| 136 |
rvalue = float(rvalue) |
| 137 |
if lltype.typeOf(rvalue) is rffi.ULONG: |
| 138 |
res = intmask(res) |
| 139 |
rvalue = intmask(rvalue) |
| 140 |
return res == rvalue |
| 141 |
|
| 142 |
with FakeFFI(fake_call_impl_any): |
| 143 |
res = f(-42) |
| 144 |
assert matching_result(res, rvalue) |
| 145 |
res = self.interp_operations(f, [-42], |
| 146 |
supports_floats = supports_floats, |
| 147 |
supports_longlong = supports_longlong, |
| 148 |
supports_singlefloats = supports_singlefloats) |
| 149 |
if is_longlong(FUNC.RESULT): |
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
res = float2longlong(res) |
| 154 |
assert matching_result(res, rvalue) |
| 155 |
self.check_operations_history(call_may_force_i=0, |
| 156 |
call_may_force_f=expected_call_may_force_f, |
| 157 |
call_may_force_n=0, |
| 158 |
call_release_gil_i=expected_call_release_gil_i, |
| 159 |
call_release_gil_f=expected_call_release_gil_f, |
| 160 |
call_release_gil_n=expected_call_release_gil_n) |
| 161 |
|
| 162 |
|
| 163 |
driver = jit.JitDriver(reds=['i'], greens=[]) |
| 164 |
def main(): |
| 165 |
i = 0 |
| 166 |
while 1: |
| 167 |
driver.jit_merge_point(i=i) |
| 168 |
res = f(i) |
| 169 |
i += 1 |
| 170 |
if i == 12: |
| 171 |
return res |
| 172 |
self.meta_interp(main, []) |
| 173 |
|
| 174 |
|
| 175 |
def test_simple_call_int(self): |
| 176 |
self._run([types.signed] * 2, types.signed, [456, 789], -42) |
| 177 |
|
| 178 |
def test_many_arguments(self): |
| 179 |
for i in [0, 6, 20]: |
| 180 |
self._run([types.signed] * i, types.signed, |
| 181 |
[-123456*j for j in range(i)], |
| 182 |
-42434445) |
| 183 |
|
| 184 |
def test_simple_call_float(self, **kwds): |
| 185 |
kwds.setdefault('supports_floats', True) |
| 186 |
kwds['expected_call_release_gil_f'] = kwds.pop('expected_call_release_gil', 1) |
| 187 |
kwds['expected_call_release_gil_i'] = 0 |
| 188 |
self._run([types.double] * 2, types.double, [45.6, 78.9], -4.2, **kwds) |
| 189 |
|
| 190 |
def test_simple_call_longlong(self, **kwds): |
| 191 |
kwds.setdefault('supports_longlong', True) |
| 192 |
if is_64_bit: |
| 193 |
kwds['expected_call_release_gil_i'] = kwds.pop('expected_call_release_gil', 1) |
| 194 |
else: |
| 195 |
kwds['expected_call_release_gil_f'] = kwds.pop('expected_call_release_gil', 1) |
| 196 |
kwds['expected_call_release_gil_i'] = 0 |
| 197 |
maxint32 = 2147483647 |
| 198 |
a = r_longlong(maxint32) + 1 |
| 199 |
b = r_longlong(maxint32) + 2 |
| 200 |
self._run([types.slonglong] * 2, types.slonglong, [a, b], a, **kwds) |
| 201 |
|
| 202 |
def test_simple_call_singlefloat_args(self, **kwds): |
| 203 |
kwds.setdefault('supports_singlefloats', True) |
| 204 |
kwds['expected_call_release_gil_f'] = kwds.pop('expected_call_release_gil', 1) |
| 205 |
kwds['expected_call_release_gil_i'] = 0 |
| 206 |
self._run([types.float] * 2, types.double, |
| 207 |
[r_singlefloat(10.5), r_singlefloat(31.5)], |
| 208 |
-4.5, **kwds) |
| 209 |
|
| 210 |
def test_simple_call_singlefloat(self, **kwds): |
| 211 |
kwds.setdefault('supports_singlefloats', True) |
| 212 |
kwds['expected_call_release_gil_i'] = kwds.pop('expected_call_release_gil', 1) |
| 213 |
self._run([types.float] * 2, types.float, |
| 214 |
[r_singlefloat(10.5), r_singlefloat(31.5)], |
| 215 |
r_singlefloat(-4.5), **kwds) |
| 216 |
|
| 217 |
def test_simple_call_longdouble(self): |
| 218 |
|
| 219 |
self._run([types.longdouble] * 2, types.longdouble, [12.3, 45.6], 78.9, |
| 220 |
expected_call_release_gil_i=0, expected_call_release_gil_f=0, |
| 221 |
) |
| 222 |
|
| 223 |
def test_returns_none(self): |
| 224 |
self._run([types.signed] * 2, types.void, [456, 789], None, |
| 225 |
expected_call_release_gil_i=0, expected_call_release_gil_n=1) |
| 226 |
|
| 227 |
def test_returns_signedchar(self): |
| 228 |
self._run([types.sint8], types.sint8, |
| 229 |
[rffi.cast(rffi.SIGNEDCHAR, -28)], |
| 230 |
rffi.cast(rffi.SIGNEDCHAR, -42)) |
| 231 |
|
| 232 |
def test_handle_unsigned(self): |
| 233 |
self._run([types.ulong], types.ulong, |
| 234 |
[rffi.cast(rffi.ULONG, r_uint(sys.maxint + 91348))], |
| 235 |
rffi.cast(rffi.ULONG, r_uint(sys.maxint + 4242))) |
| 236 |
|
| 237 |
def test_handle_unsignedchar(self): |
| 238 |
self._run([types.uint8], types.uint8, |
| 239 |
[rffi.cast(rffi.UCHAR, 191)], |
| 240 |
rffi.cast(rffi.UCHAR, 180)) |
| 241 |
|
| 242 |
def _add_libffi_types_to_ll2types_maybe(self): |
| 243 |
|
| 244 |
|
| 245 |
pass |
| 246 |
|
| 247 |
def test_guard_not_forced_fails(self): |
| 248 |
self._add_libffi_types_to_ll2types_maybe() |
| 249 |
FUNC = lltype.FuncType([lltype.Signed], lltype.Signed) |
| 250 |
|
| 251 |
cif_description = get_description([types.slong], types.slong) |
| 252 |
cif_description.exchange_args[0] = 16 |
| 253 |
cif_description.exchange_result = 32 |
| 254 |
|
| 255 |
ARRAY = lltype.Ptr(rffi.CArray(lltype.Signed)) |
| 256 |
|
| 257 |
@jit.dont_look_inside |
| 258 |
def fn(n): |
| 259 |
if n >= 50: |
| 260 |
exctx.m = exctx.topframeref().n |
| 261 |
return n*2 |
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
def fake_call_impl_any(cif_description, func_addr, exchange_buffer): |
| 267 |
|
| 268 |
data_in = rffi.ptradd(exchange_buffer, 16) |
| 269 |
n = rffi.cast(ARRAY, data_in)[0] |
| 270 |
|
| 271 |
|
| 272 |
func_ptr = rffi.cast(lltype.Ptr(FUNC), func_addr) |
| 273 |
n = func_ptr(n) |
| 274 |
|
| 275 |
|
| 276 |
data_out = rffi.ptradd(exchange_buffer, 32) |
| 277 |
rffi.cast(ARRAY, data_out)[0] = n |
| 278 |
|
| 279 |
def do_call(n): |
| 280 |
func_ptr = llhelper(lltype.Ptr(FUNC), fn) |
| 281 |
exbuf = lltype.malloc(rffi.CCHARP.TO, 48, flavor='raw', zero=True) |
| 282 |
data_in = rffi.ptradd(exbuf, 16) |
| 283 |
rffi.cast(ARRAY, data_in)[0] = n |
| 284 |
jit_ffi_call(cif_description, func_ptr, exbuf) |
| 285 |
data_out = rffi.ptradd(exbuf, 32) |
| 286 |
res = rffi.cast(ARRAY, data_out)[0] |
| 287 |
lltype.free(exbuf, flavor='raw') |
| 288 |
return res |
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
class XY: |
| 293 |
pass |
| 294 |
class ExCtx: |
| 295 |
pass |
| 296 |
exctx = ExCtx() |
| 297 |
myjitdriver = jit.JitDriver(greens = [], reds = ['n']) |
| 298 |
def f(): |
| 299 |
n = 0 |
| 300 |
while n < 100: |
| 301 |
myjitdriver.jit_merge_point(n=n) |
| 302 |
xy = XY() |
| 303 |
xy.n = n |
| 304 |
exctx.topframeref = vref = jit.virtual_ref(xy) |
| 305 |
res = do_call(n) |
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
assert res == n*2 |
| 317 |
jit.virtual_ref_finish(vref, xy) |
| 318 |
exctx.topframeref = jit.vref_None |
| 319 |
n += 1 |
| 320 |
return n |
| 321 |
|
| 322 |
with FakeFFI(fake_call_impl_any): |
| 323 |
assert f() == 100 |
| 324 |
res = self.meta_interp(f, []) |
| 325 |
assert res == 100 |
| 326 |
|
| 327 |
|
| 328 |
class TestFfiCall(FfiCallTests, LLJitMixin): |
| 329 |
def test_jit_ffi_vref(self): |
| 330 |
py.test.skip("unsupported so far") |
| 331 |
from rpython.rlib import clibffi |
| 332 |
from rpython.rlib.jit_libffi import jit_ffi_prep_cif, jit_ffi_call |
| 333 |
|
| 334 |
math_sin = intmask(ctypes.cast(ctypes.CDLL(None).sin, |
| 335 |
ctypes.c_void_p).value) |
| 336 |
math_sin = rffi.cast(rffi.VOIDP, math_sin) |
| 337 |
|
| 338 |
cd = lltype.malloc(CIF_DESCRIPTION, 1, flavor='raw') |
| 339 |
cd.abi = clibffi.FFI_DEFAULT_ABI |
| 340 |
cd.nargs = 1 |
| 341 |
cd.rtype = clibffi.cast_type_to_ffitype(rffi.DOUBLE) |
| 342 |
atypes = lltype.malloc(clibffi.FFI_TYPE_PP.TO, 1, flavor='raw') |
| 343 |
atypes[0] = clibffi.cast_type_to_ffitype(rffi.DOUBLE) |
| 344 |
cd.atypes = atypes |
| 345 |
cd.exchange_size = 64 |
| 346 |
cd.exchange_result = 24 |
| 347 |
cd.exchange_args[0] = 16 |
| 348 |
|
| 349 |
def f(): |
| 350 |
|
| 351 |
jit_ffi_prep_cif(cd) |
| 352 |
|
| 353 |
assert rffi.sizeof(rffi.DOUBLE) == 8 |
| 354 |
exb = lltype.malloc(rffi.DOUBLEP.TO, 8, flavor='raw') |
| 355 |
exb[2] = 1.23 |
| 356 |
jit_ffi_call(cd, math_sin, rffi.cast(rffi.CCHARP, exb)) |
| 357 |
res = exb[3] |
| 358 |
lltype.free(exb, flavor='raw') |
| 359 |
|
| 360 |
return res |
| 361 |
|
| 362 |
res = self.interp_operations(f, []) |
| 363 |
lltype.free(cd, flavor='raw') |
| 364 |
assert res == math.sin(1.23) |
| 365 |
|
| 366 |
lltype.free(atypes, flavor='raw') |
| 367 |
|
| 368 |
def test_simple_call_float_unsupported(self): |
| 369 |
self.test_simple_call_float(supports_floats=False, |
| 370 |
expected_call_release_gil=0) |
| 371 |
|
| 372 |
def test_simple_call_longlong_unsupported(self): |
| 373 |
self.test_simple_call_longlong(supports_longlong=False, |
| 374 |
expected_call_release_gil=is_64_bit) |
| 375 |
|
| 376 |
def test_simple_call_singlefloat_unsupported(self): |
| 377 |
self.test_simple_call_singlefloat(supports_singlefloats=False, |
| 378 |
expected_call_release_gil=0) |
| 379 |
|
| 380 |
def test_calldescrof_dynamic_returning_none(self): |
| 381 |
from rpython.jit.backend.llgraph.runner import LLGraphCPU |
| 382 |
old = LLGraphCPU.calldescrof_dynamic |
| 383 |
try: |
| 384 |
LLGraphCPU.calldescrof_dynamic = lambda *args: None |
| 385 |
self.test_simple_call_float(expected_call_release_gil=0, |
| 386 |
expected_call_may_force_f=1) |
| 387 |
finally: |
| 388 |
LLGraphCPU.calldescrof_dynamic = old |