| 1 |
import py |
| 2 |
from rpython.jit.backend.detect_cpu import getcpuclass |
| 3 |
from rpython.jit.backend.test.runner_test import LLtypeBackendTest,\ |
| 4 |
boxfloat, constfloat |
| 5 |
from rpython.jit.metainterp.history import BasicFailDescr, BasicFinalDescr |
| 6 |
from rpython.jit.metainterp.resoperation import (ResOperation, rop, |
| 7 |
InputArgInt) |
| 8 |
from rpython.jit.tool.oparser import parse |
| 9 |
from rpython.rtyper.lltypesystem import lltype, llmemory |
| 10 |
from rpython.rtyper import rclass |
| 11 |
from rpython.rtyper.annlowlevel import llhelper |
| 12 |
from rpython.jit.codewriter.effectinfo import EffectInfo |
| 13 |
from rpython.jit.metainterp.history import JitCellToken, TargetToken |
| 14 |
from rpython.jit.backend.arm.detect import detect_arch_version |
| 15 |
from rpython.jit.codewriter import longlong |
| 16 |
|
| 17 |
|
| 18 |
CPU = getcpuclass() |
| 19 |
|
| 20 |
class FakeStats(object): |
| 21 |
pass |
| 22 |
|
| 23 |
|
| 24 |
class TestARM(LLtypeBackendTest): |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
add_loop_instructions = 'ldr; adds; cmp; beq; b;' |
| 30 |
arch_version = detect_arch_version() |
| 31 |
if arch_version == 7: |
| 32 |
bridge_loop_instructions = ('ldr; movw; nop; cmp; bge; ' |
| 33 |
'push; movw; movt; push; movw; movt; ' |
| 34 |
'blx; movw; movt; bx;') |
| 35 |
else: |
| 36 |
bridge_loop_instructions = ('ldr; mov; nop; nop; nop; cmp; bge; ' |
| 37 |
'push; ldr; mov; ' |
| 38 |
'[^;]+; ' |
| 39 |
'push; ldr; mov; ' |
| 40 |
'[^;]+; ' |
| 41 |
'blx; ldr; mov; ' |
| 42 |
'[^;]+; ' |
| 43 |
'bx;') |
| 44 |
|
| 45 |
def get_cpu(self): |
| 46 |
cpu = CPU(rtyper=None, stats=FakeStats()) |
| 47 |
cpu.setup_once() |
| 48 |
return cpu |
| 49 |
|
| 50 |
def test_result_is_spilled(self): |
| 51 |
cpu = self.cpu |
| 52 |
inp = [InputArgInt(i) for i in range(1, 15)] |
| 53 |
looptoken = JitCellToken() |
| 54 |
targettoken = TargetToken() |
| 55 |
operations = [ |
| 56 |
ResOperation(rop.LABEL, inp, descr=targettoken), |
| 57 |
ResOperation(rop.INT_ADD, [inp[0], inp[1]]), |
| 58 |
ResOperation(rop.INT_ADD, [inp[2], inp[3]]), |
| 59 |
ResOperation(rop.INT_ADD, [inp[4], inp[5]]), |
| 60 |
ResOperation(rop.INT_ADD, [inp[6], inp[7]]), |
| 61 |
ResOperation(rop.INT_ADD, [inp[8], inp[9]]), |
| 62 |
ResOperation(rop.INT_ADD, [inp[10], inp[11]]), |
| 63 |
ResOperation(rop.INT_ADD, [inp[12], inp[13]]), |
| 64 |
ResOperation(rop.INT_ADD, [inp[0], inp[1]]), |
| 65 |
ResOperation(rop.INT_ADD, [inp[2], inp[3]]), |
| 66 |
ResOperation(rop.INT_ADD, [inp[4], inp[5]]), |
| 67 |
ResOperation(rop.INT_ADD, [inp[6], inp[7]]), |
| 68 |
ResOperation(rop.INT_ADD, [inp[8], inp[9]]), |
| 69 |
ResOperation(rop.INT_ADD, [inp[10], inp[11]]), |
| 70 |
ResOperation(rop.INT_ADD, [inp[12], inp[13]]), |
| 71 |
ResOperation(rop.GUARD_FALSE, [inp[1]], descr=BasicFailDescr(1)), |
| 72 |
ResOperation(rop.FINISH, [inp[1]], descr=BasicFinalDescr(1)), |
| 73 |
] |
| 74 |
operations[-2].setfailargs(operations[1:15]) |
| 75 |
cpu.compile_loop(inp, operations, looptoken) |
| 76 |
args = [i for i in range(1, 15)] |
| 77 |
deadframe = self.cpu.execute_token(looptoken, *args) |
| 78 |
output = [self.cpu.get_int_value(deadframe, i - 1) for i in range(1, 15)] |
| 79 |
expected = [3, 7, 11, 15, 19, 23, 27, 3, 7, 11, 15, 19, 23, 27] |
| 80 |
assert output == expected |
| 81 |
|
| 82 |
def test_redirect_call_assembler2(self): |
| 83 |
def assembler_helper(deadframe, virtualizable): |
| 84 |
x = self.cpu.get_int_value(deadframe, 0) |
| 85 |
assert x == 11 |
| 86 |
return 7 |
| 87 |
|
| 88 |
FUNCPTR = lltype.Ptr(lltype.FuncType([llmemory.GCREF, llmemory.GCREF], |
| 89 |
lltype.Signed)) |
| 90 |
|
| 91 |
class FakeJitDriverSD: |
| 92 |
index_of_virtualizable = -1 |
| 93 |
_assembler_helper_ptr = llhelper(FUNCPTR, assembler_helper) |
| 94 |
assembler_helper_adr = llmemory.cast_ptr_to_adr( |
| 95 |
_assembler_helper_ptr) |
| 96 |
FakeJitDriverSD.portal_calldescr = self.cpu.calldescrof( |
| 97 |
lltype.Ptr(lltype.FuncType([lltype.Signed], lltype.Signed)), |
| 98 |
[lltype.Signed], lltype.Signed, EffectInfo.MOST_GENERAL) |
| 99 |
lt1, lt2, lt3 = [JitCellToken() for x in range(3)] |
| 100 |
lt2.outermost_jitdriver_sd = FakeJitDriverSD() |
| 101 |
loop1 = parse(''' |
| 102 |
[i0] |
| 103 |
i1 = call_assembler_i(i0, descr=lt2) |
| 104 |
guard_not_forced()[] |
| 105 |
finish(i1) |
| 106 |
''', namespace=locals()) |
| 107 |
loop2 = parse(''' |
| 108 |
[i0] |
| 109 |
i1 = int_add(i0, 1) |
| 110 |
finish(i1) |
| 111 |
''') |
| 112 |
loop3 = parse(''' |
| 113 |
[i0] |
| 114 |
i1 = int_sub(i0, 1) |
| 115 |
finish(i1) |
| 116 |
''') |
| 117 |
self.cpu.compile_loop(loop2.inputargs, loop2.operations, lt2) |
| 118 |
self.cpu.compile_loop(loop3.inputargs, loop3.operations, lt3) |
| 119 |
self.cpu.compile_loop(loop1.inputargs, loop1.operations, lt1) |
| 120 |
df = self.cpu.execute_token(lt1, 10) |
| 121 |
assert self.cpu.get_int_value(df, 0) == 7 |
| 122 |
|
| 123 |
self.cpu.redirect_call_assembler(lt2, lt3) |
| 124 |
df = self.cpu.execute_token(lt1, 12) |
| 125 |
assert self.cpu.get_int_value(df, 0) == 7 |
| 126 |
|
| 127 |
SFloat = lltype.GcForwardReference() |
| 128 |
SFloat.become(lltype.GcStruct('SFloat', ('parent', rclass.OBJECT), |
| 129 |
('v1', lltype.Signed), ('v2', lltype.Signed), |
| 130 |
('v3', lltype.Signed), ('v4', lltype.Signed), |
| 131 |
('v5', lltype.Signed), ('v6', lltype.Signed), |
| 132 |
('v7', lltype.Signed), ('v8', lltype.Signed), |
| 133 |
('v9', lltype.Signed), ('v10', lltype.Signed), |
| 134 |
('v11', lltype.Signed), ('v12', lltype.Signed), |
| 135 |
('v13', lltype.Signed), ('v14', lltype.Signed), |
| 136 |
('v15', lltype.Signed), ('v16', lltype.Signed), |
| 137 |
('v17', lltype.Signed), ('v18', lltype.Signed), |
| 138 |
('v19', lltype.Signed), ('v20', lltype.Signed), |
| 139 |
|
| 140 |
('w1', lltype.Signed), ('w2', lltype.Signed), |
| 141 |
('w3', lltype.Signed), ('w4', lltype.Signed), |
| 142 |
('w5', lltype.Signed), ('w6', lltype.Signed), |
| 143 |
('w7', lltype.Signed), ('w8', lltype.Signed), |
| 144 |
('w9', lltype.Signed), ('w10', lltype.Signed), |
| 145 |
('w11', lltype.Signed), ('w12', lltype.Signed), |
| 146 |
('w13', lltype.Signed), ('w14', lltype.Signed), |
| 147 |
('w15', lltype.Signed), ('w16', lltype.Signed), |
| 148 |
('w17', lltype.Signed), ('w18', lltype.Signed), |
| 149 |
('w19', lltype.Signed), ('w20', lltype.Signed), |
| 150 |
|
| 151 |
('x1', lltype.Signed), ('x2', lltype.Signed), |
| 152 |
('x3', lltype.Signed), ('x4', lltype.Signed), |
| 153 |
('x5', lltype.Signed), ('x6', lltype.Signed), |
| 154 |
('x7', lltype.Signed), ('x8', lltype.Signed), |
| 155 |
('x9', lltype.Signed), ('x10', lltype.Signed), |
| 156 |
('x11', lltype.Signed), ('x12', lltype.Signed), |
| 157 |
('x13', lltype.Signed), ('x14', lltype.Signed), |
| 158 |
('x15', lltype.Signed), ('x16', lltype.Signed), |
| 159 |
('x17', lltype.Signed), ('x18', lltype.Signed), |
| 160 |
('x19', lltype.Signed), ('x20', lltype.Signed), |
| 161 |
|
| 162 |
('y1', lltype.Signed), ('y2', lltype.Signed), |
| 163 |
('y3', lltype.Signed), ('y4', lltype.Signed), |
| 164 |
('y5', lltype.Signed), ('y6', lltype.Signed), |
| 165 |
('y7', lltype.Signed), ('y8', lltype.Signed), |
| 166 |
('y9', lltype.Signed), ('y10', lltype.Signed), |
| 167 |
('y11', lltype.Signed), ('y12', lltype.Signed), |
| 168 |
('y13', lltype.Signed), ('y14', lltype.Signed), |
| 169 |
('y15', lltype.Signed), ('y16', lltype.Signed), |
| 170 |
('y17', lltype.Signed), ('y18', lltype.Signed), |
| 171 |
('y19', lltype.Signed), ('y20', lltype.Signed), |
| 172 |
('float', lltype.Float))) |
| 173 |
|
| 174 |
TFloat = lltype.GcStruct('TFloat', ('parent', SFloat), |
| 175 |
('next', lltype.Ptr(SFloat))) |
| 176 |
|
| 177 |
def test_float_field(self): |
| 178 |
if not self.cpu.supports_floats: |
| 179 |
py.test.skip('requires floats') |
| 180 |
t_box, T_box, _ = self.alloc_instance(self.TFloat) |
| 181 |
floatdescr = self.cpu.fielddescrof(self.SFloat, 'float') |
| 182 |
self.execute_operation(rop.SETFIELD_GC, [t_box, boxfloat(3.4)], |
| 183 |
'void', descr=floatdescr) |
| 184 |
res = self.execute_operation(rop.GETFIELD_GC_F, [t_box], |
| 185 |
'float', descr=floatdescr) |
| 186 |
assert longlong.getrealfloat(res) == 3.4 |
| 187 |
|
| 188 |
self.execute_operation(rop.SETFIELD_GC, [t_box, constfloat(-3.6)], |
| 189 |
'void', descr=floatdescr) |
| 190 |
res = self.execute_operation(rop.GETFIELD_GC_F, [t_box], |
| 191 |
'float', descr=floatdescr) |
| 192 |
assert longlong.getrealfloat(res) == -3.6 |
| 193 |
|
| 194 |
def test_compile_loop_many_int_args(self): |
| 195 |
for numargs in range(2, 30): |
| 196 |
ops = [] |
| 197 |
arglist = "[%s]\n" % ", ".join(["i%d" % i for i in range(numargs)]) |
| 198 |
ops.append(arglist) |
| 199 |
|
| 200 |
arg1 = 0 |
| 201 |
arg2 = 1 |
| 202 |
res = numargs |
| 203 |
for i in range(numargs - 1): |
| 204 |
op = "i%d = int_add(i%d, i%d)\n" % (res, arg1, arg2) |
| 205 |
arg1 = res |
| 206 |
res += 1 |
| 207 |
arg2 += 1 |
| 208 |
ops.append(op) |
| 209 |
ops.append("finish(i%d)" % (res - 1)) |
| 210 |
|
| 211 |
ops = "".join(ops) |
| 212 |
loop = parse(ops) |
| 213 |
looptoken = JitCellToken() |
| 214 |
self.cpu.compile_loop(loop.inputargs, loop.operations, looptoken) |
| 215 |
ARGS = [lltype.Signed] * numargs |
| 216 |
RES = lltype.Signed |
| 217 |
args = [i+1 for i in range(numargs)] |
| 218 |
deadframe = self.cpu.execute_token(looptoken, *args) |
| 219 |
assert self.cpu.get_int_value(deadframe, 0) == sum(args) |
| 220 |
|
| 221 |
def test_debugger_on(self): |
| 222 |
py.test.skip("I don't care for now") |
| 223 |
from rpython.rlib import debug |
| 224 |
|
| 225 |
targettoken, preambletoken = TargetToken(), TargetToken() |
| 226 |
loop = """ |
| 227 |
[i0] |
| 228 |
label(i0, descr=preambletoken) |
| 229 |
debug_merge_point('xyz', 0) |
| 230 |
i1 = int_add(i0, 1) |
| 231 |
i2 = int_ge(i1, 10) |
| 232 |
guard_false(i2) [] |
| 233 |
label(i1, descr=targettoken) |
| 234 |
debug_merge_point('xyz', 0) |
| 235 |
i11 = int_add(i1, 1) |
| 236 |
i12 = int_ge(i11, 10) |
| 237 |
guard_false(i12) [] |
| 238 |
jump(i11, descr=targettoken) |
| 239 |
""" |
| 240 |
ops = parse(loop, namespace={'targettoken': targettoken, |
| 241 |
'preambletoken': preambletoken}) |
| 242 |
debug._log = dlog = debug.DebugLog() |
| 243 |
try: |
| 244 |
self.cpu.assembler.set_debug(True) |
| 245 |
looptoken = JitCellToken() |
| 246 |
self.cpu.compile_loop(ops.inputargs, ops.operations, looptoken) |
| 247 |
self.cpu.execute_token(looptoken, 0) |
| 248 |
|
| 249 |
struct = self.cpu.assembler.loop_run_counters[0] |
| 250 |
assert struct.i == 1 |
| 251 |
struct = self.cpu.assembler.loop_run_counters[1] |
| 252 |
assert struct.i == 1 |
| 253 |
struct = self.cpu.assembler.loop_run_counters[2] |
| 254 |
assert struct.i == 9 |
| 255 |
self.cpu.finish_once() |
| 256 |
finally: |
| 257 |
debug._log = None |
| 258 |
l0 = ('debug_print', 'entry -1:1') |
| 259 |
l1 = ('debug_print', preambletoken.repr_of_descr() + ':1') |
| 260 |
l2 = ('debug_print', targettoken.repr_of_descr() + ':9') |
| 261 |
assert ('jit-backend-counts', [l0, l1, l2]) in dlog |
| 262 |
|
| 263 |
|
| 264 |
def test_label_float_in_reg_and_on_stack(self): |
| 265 |
targettoken = TargetToken() |
| 266 |
ops = """ |
| 267 |
[i0, f3] |
| 268 |
i2 = same_as_i(i0) # but forced to be in a register |
| 269 |
force_spill(i2) |
| 270 |
force_spill(f3) |
| 271 |
f4 = float_add(f3, 5.0) |
| 272 |
label(f3, f4, descr=targettoken) |
| 273 |
force_spill(f3) |
| 274 |
f5 = same_as_f(f3) # but forced to be in a register |
| 275 |
finish(f5) |
| 276 |
""" |
| 277 |
faildescr = BasicFailDescr(2) |
| 278 |
loop = parse(ops, self.cpu, namespace=locals()) |
| 279 |
looptoken = JitCellToken() |
| 280 |
info = self.cpu.compile_loop(loop.inputargs, loop.operations, looptoken) |
| 281 |
ops2 = """ |
| 282 |
[i0, f1] |
| 283 |
i1 = same_as_i(i0) |
| 284 |
f2 = same_as_f(f1) |
| 285 |
f3 = float_add(f1, 10.0) |
| 286 |
force_spill(f3) |
| 287 |
force_spill(i1) |
| 288 |
f4 = float_add(f3, f1) |
| 289 |
jump(f3, f4, descr=targettoken) |
| 290 |
""" |
| 291 |
loop2 = parse(ops2, self.cpu, namespace=locals()) |
| 292 |
looptoken2 = JitCellToken() |
| 293 |
info = self.cpu.compile_loop(loop2.inputargs, loop2.operations, looptoken2) |
| 294 |
|
| 295 |
deadframe = self.cpu.execute_token(looptoken, -9, longlong.getfloatstorage(-13.5)) |
| 296 |
res = longlong.getrealfloat(self.cpu.get_float_value(deadframe, 0)) |
| 297 |
assert res == -13.5 |
| 298 |
|
| 299 |
deadframe = self.cpu.execute_token(looptoken2, -9, longlong.getfloatstorage(-13.5)) |
| 300 |
res = longlong.getrealfloat(self.cpu.get_float_value(deadframe, 0)) |
| 301 |
assert res == -3.5 |