| 1 |
""" explicit integration tests for register allocation in the x86 backend """ |
| 2 |
|
| 3 |
import pytest |
| 4 |
|
| 5 |
from rpython.jit.backend.llsupport.test import test_regalloc_integration |
| 6 |
from rpython.jit.backend.x86.assembler import Assembler386 |
| 7 |
from rpython.jit.backend.x86.arch import IS_X86_64, WIN64 |
| 8 |
|
| 9 |
class LogEntry(object): |
| 10 |
def __init__(self, position, name, *args): |
| 11 |
self.position = position |
| 12 |
self.name = name |
| 13 |
self.args = args |
| 14 |
|
| 15 |
def __repr__(self): |
| 16 |
r = repr(self.args) |
| 17 |
if self.name == "op": |
| 18 |
r = repr(self.args[1:]) |
| 19 |
return "<%s: %s %s %s>" % (self.position, self.name, self.args[0], r.strip("(),")) |
| 20 |
return "<%s: %s %s>" % (self.position, self.name, r.strip("(),")) |
| 21 |
|
| 22 |
class LoggingAssembler(Assembler386): |
| 23 |
def __init__(self, *args, **kwargs): |
| 24 |
self._instr_log = [] |
| 25 |
Assembler386.__init__(self, *args, **kwargs) |
| 26 |
|
| 27 |
def _log(self, name, *args): |
| 28 |
self._instr_log.append(LogEntry(self._regalloc.rm.position, name, *args)) |
| 29 |
|
| 30 |
def mov(self, from_loc, to_loc): |
| 31 |
self._log("mov", from_loc, to_loc) |
| 32 |
return Assembler386.mov(self, from_loc, to_loc) |
| 33 |
|
| 34 |
def regalloc_mov(self, from_loc, to_loc): |
| 35 |
self._log("mov", from_loc, to_loc) |
| 36 |
return Assembler386.mov(self, from_loc, to_loc) |
| 37 |
|
| 38 |
def regalloc_perform(self, op, arglocs, resloc): |
| 39 |
self._log("op", op.getopname(), arglocs, resloc) |
| 40 |
return Assembler386.regalloc_perform(self, op, arglocs, resloc) |
| 41 |
|
| 42 |
def regalloc_perform_discard(self, op, arglocs): |
| 43 |
self._log("op", op.getopname(), arglocs) |
| 44 |
return Assembler386.regalloc_perform_discard(self, op, arglocs) |
| 45 |
|
| 46 |
def regalloc_perform_guard(self, guard_op, faillocs, arglocs, resloc, |
| 47 |
frame_depth): |
| 48 |
self._log("guard", guard_op.getopname(), arglocs, faillocs, resloc) |
| 49 |
return Assembler386.regalloc_perform_guard(self, guard_op, faillocs, |
| 50 |
arglocs, resloc, frame_depth) |
| 51 |
|
| 52 |
def malloc_cond(self, nursery_free_adr, nursery_top_adr, size, gcmap): |
| 53 |
self._log("malloc_cond", size, "ecx") |
| 54 |
|
| 55 |
def label(self): |
| 56 |
self._log("label", self._regalloc.final_jump_op.getdescr()._x86_arglocs) |
| 57 |
return Assembler386.label(self) |
| 58 |
|
| 59 |
def closing_jump(self, jump_target_descr): |
| 60 |
self._log("jump", self._regalloc.final_jump_op.getdescr()._x86_arglocs) |
| 61 |
return Assembler386.closing_jump(self, jump_target_descr) |
| 62 |
|
| 63 |
class BaseTestCheckRegistersExplicitly(test_regalloc_integration.BaseTestRegalloc): |
| 64 |
def setup_class(cls): |
| 65 |
cls.cpu.assembler = LoggingAssembler(cls.cpu, False) |
| 66 |
cls.cpu.assembler.setup_once() |
| 67 |
|
| 68 |
def setup_method(self, meth): |
| 69 |
self.cpu.assembler._instr_log = self.log = [] |
| 70 |
|
| 71 |
def teardown_method(self, meth): |
| 72 |
for l in self.log: |
| 73 |
print l |
| 74 |
|
| 75 |
def filter_log_moves(self): |
| 76 |
return [entry for entry in self.log if entry.name == "mov"] |
| 77 |
|
| 78 |
class TestCheckRegistersExplicitly(BaseTestCheckRegistersExplicitly): |
| 79 |
def test_unused(self): |
| 80 |
ops = ''' |
| 81 |
[i0, i1, i2, i3] |
| 82 |
i7 = int_add(i0, i1) # unused |
| 83 |
i9 = int_add(i2, i3) |
| 84 |
finish(i9) |
| 85 |
''' |
| 86 |
|
| 87 |
self.interpret(ops, [5, 6, 7, 8]) |
| 88 |
assert len([entry for entry in self.log if entry.args[0] == "int_add"]) == 1 |
| 89 |
|
| 90 |
def test_bug_const(self): |
| 91 |
ops = ''' |
| 92 |
[i0, i1, i2, i3] |
| 93 |
i9 = int_add(1, i3) |
| 94 |
finish(i9) |
| 95 |
''' |
| 96 |
|
| 97 |
self.interpret(ops, [5, 6, 7, 8]) |
| 98 |
assert len([entry for entry in self.log if entry.args[0] == "int_add"]) == 1 |
| 99 |
|
| 100 |
def test_use_lea_even_for_stack(self): |
| 101 |
ops = ''' |
| 102 |
[i0, i1, i2, i3] |
| 103 |
i9 = int_add(i3, 16) |
| 104 |
i4 = int_add(i3, 26) |
| 105 |
i6 = int_add(i9, i4) |
| 106 |
finish(i6) |
| 107 |
''' |
| 108 |
self.interpret(ops, [5, 6, 7, 8]) |
| 109 |
assert len(self.filter_log_moves()) == 2 |
| 110 |
|
| 111 |
def test_call_use_correct_regs(self): |
| 112 |
ops = ''' |
| 113 |
[i0, i1, i2, i3] |
| 114 |
i7 = int_add(i0, i1) |
| 115 |
i8 = int_add(i2, 13) |
| 116 |
i9 = call_i(ConstClass(f1ptr), i7, descr=f1_calldescr) |
| 117 |
i10 = int_is_true(i9) |
| 118 |
guard_true(i10) [i8] |
| 119 |
finish(i9) |
| 120 |
''' |
| 121 |
self.interpret(ops, [5, 6, 7, 8]) |
| 122 |
|
| 123 |
|
| 124 |
assert len(self.filter_log_moves()) == 3 |
| 125 |
|
| 126 |
@pytest.mark.skip("later") |
| 127 |
def test_same_stack_entry_many_times(self): |
| 128 |
ops = ''' |
| 129 |
[i0, i1, i2, i3] |
| 130 |
i7 = int_add(i0, i1) |
| 131 |
i8 = int_add(i2, i1) |
| 132 |
i9 = int_add(i3, i1) |
| 133 |
i10 = int_is_true(i9) |
| 134 |
guard_true(i10) [i8] |
| 135 |
finish(i7) |
| 136 |
''' |
| 137 |
self.interpret(ops, [5, 6, 7, 8]) |
| 138 |
|
| 139 |
assert len(self.filter_log_moves()) == 5 |
| 140 |
|
| 141 |
def test_coalescing(self): |
| 142 |
ops = ''' |
| 143 |
[i0, i1, i3] |
| 144 |
i7 = int_add(i0, i1) |
| 145 |
i8 = int_add(i7, i3) |
| 146 |
i9 = call_i(ConstClass(f1ptr), i8, descr=f1_calldescr) |
| 147 |
i10 = int_is_true(i9) |
| 148 |
guard_true(i10) [] |
| 149 |
finish(i9) |
| 150 |
''' |
| 151 |
self.interpret(ops, [5, 6, 8]) |
| 152 |
|
| 153 |
assert len(self.filter_log_moves()) == 2 |
| 154 |
|
| 155 |
def test_coalescing_sub(self): |
| 156 |
ops = ''' |
| 157 |
[i0, i1, i3] |
| 158 |
i7 = int_sub(i0, i1) |
| 159 |
i8 = int_sub(i7, i3) |
| 160 |
i9 = call_i(ConstClass(f1ptr), i8, descr=f1_calldescr) |
| 161 |
i10 = int_is_true(i9) |
| 162 |
guard_true(i10) [] |
| 163 |
finish(i9) |
| 164 |
''' |
| 165 |
self.interpret(ops, [5, 6, 8]) |
| 166 |
|
| 167 |
assert len(self.filter_log_moves()) == 2 |
| 168 |
|
| 169 |
def test_coalescing_mul(self): |
| 170 |
|
| 171 |
ops = ''' |
| 172 |
[i0, i1, i3] |
| 173 |
i7 = int_mul(i0, i1) |
| 174 |
i8 = int_mul(i7, i3) |
| 175 |
i9 = call_i(ConstClass(f1ptr), i8, descr=f1_calldescr) |
| 176 |
i10 = int_is_true(i9) |
| 177 |
guard_true(i10) [] |
| 178 |
finish(i9) |
| 179 |
''' |
| 180 |
self.interpret(ops, [5, 6, 8]) |
| 181 |
assert len(self.filter_log_moves()) == 2 |
| 182 |
|
| 183 |
def test_lshift(self): |
| 184 |
ops = ''' |
| 185 |
[i0, i1, i2, i3] |
| 186 |
i5 = int_add(i2, i3) |
| 187 |
i7 = int_lshift(i0, i5) |
| 188 |
i8 = int_lshift(i7, i3) |
| 189 |
i9 = call_i(ConstClass(f1ptr), 42, i8, descr=f2_calldescr) |
| 190 |
i10 = int_is_true(i9) |
| 191 |
guard_true(i10) [] |
| 192 |
finish(i9) |
| 193 |
''' |
| 194 |
self.interpret(ops, [5, 6, 7, 8]) |
| 195 |
if IS_X86_64: |
| 196 |
|
| 197 |
assert len(self.filter_log_moves()) == 5 |
| 198 |
else: |
| 199 |
assert len(self.filter_log_moves()) == 4 |
| 200 |
|
| 201 |
def test_binop_dont_swap_unnecessarily(self): |
| 202 |
ops = ''' |
| 203 |
[i0, i1, i2, i3] |
| 204 |
i7 = int_add(i0, i1) |
| 205 |
i8 = int_add(i2, 13) |
| 206 |
i9 = int_add(i7, i8) |
| 207 |
i10 = int_is_true(i9) |
| 208 |
guard_true(i10) [] |
| 209 |
finish(i9) |
| 210 |
''' |
| 211 |
self.interpret(ops, [5, 6, 7, 8]) |
| 212 |
add1 = self.log[2] |
| 213 |
op = self.log[5] |
| 214 |
assert op.name == "op" |
| 215 |
|
| 216 |
|
| 217 |
assert op.args[1][0] is add1.args[-1] |
| 218 |
|
| 219 |
def test_jump_hinting(self): |
| 220 |
self.targettoken._ll_loop_code = 0 |
| 221 |
ops = ''' |
| 222 |
[i0] |
| 223 |
i1 = int_add(i0, 1) |
| 224 |
i10 = int_add(i1, 1) |
| 225 |
i2 = int_add(i1, 1) |
| 226 |
i3 = int_lt(i2, 20) |
| 227 |
guard_true(i3) [i1, i10] |
| 228 |
label(i2, descr=targettoken) |
| 229 |
i4 = int_add(i2, 1) |
| 230 |
i11 = int_add(i4, 1) |
| 231 |
i5 = int_add(i4, 1) |
| 232 |
i6 = int_lt(i5, 20) |
| 233 |
guard_true(i6) [i4, i11] |
| 234 |
jump(i5, descr=targettoken) |
| 235 |
''' |
| 236 |
self.interpret(ops, [0], run=False) |
| 237 |
assert len(self.filter_log_moves()) == 1 |
| 238 |
|
| 239 |
def test_jump_hinting_duplicate(self): |
| 240 |
self.targettoken._ll_loop_code = 0 |
| 241 |
ops = ''' |
| 242 |
[i0] |
| 243 |
i1 = int_add(i0, 1) |
| 244 |
i10 = int_add(i1, 1) |
| 245 |
i2 = int_add(i1, 1) |
| 246 |
i3 = int_lt(i2, 20) |
| 247 |
guard_true(i3) [i1, i10] |
| 248 |
label(i2, i10, descr=targettoken) |
| 249 |
i4 = int_add(i2, 1) |
| 250 |
i11 = int_add(i4, i10) |
| 251 |
i5 = int_add(i4, 1) |
| 252 |
i6 = int_lt(i5, 20) |
| 253 |
guard_true(i6) [i4, i11] |
| 254 |
jump(i5, i5, descr=targettoken) |
| 255 |
''' |
| 256 |
self.interpret(ops, [0], run=False) |
| 257 |
assert len(self.filter_log_moves()) == 3 |
| 258 |
|
| 259 |
|
| 260 |
def test_jump_hinting_int_add(self): |
| 261 |
self.targettoken._ll_loop_code = 0 |
| 262 |
ops = ''' |
| 263 |
[i0] |
| 264 |
i1 = int_add(i0, 1) |
| 265 |
i3 = int_lt(i1, 20) |
| 266 |
guard_true(i3) [i1] |
| 267 |
label(i1, descr=targettoken) |
| 268 |
i4 = int_add(i1, 1) |
| 269 |
i6 = int_lt(i4, 20) |
| 270 |
guard_true(i6) [i4] |
| 271 |
jump(i4, descr=targettoken) |
| 272 |
''' |
| 273 |
self.interpret(ops, [0], run=False) |
| 274 |
assert len(self.filter_log_moves()) == 1 |
| 275 |
|
| 276 |
@pytest.mark.skip("later") |
| 277 |
def test_jump_different_args2(self): |
| 278 |
ops = ''' |
| 279 |
[i0, i4, i6] |
| 280 |
i1 = int_add(i0, i6) |
| 281 |
i2 = int_lt(i1, 20) |
| 282 |
guard_true(i2) [i1] |
| 283 |
label(i4, i1, i6, descr=targettoken) |
| 284 |
i3 = int_add(i4, i6) |
| 285 |
i7 = int_lt(i3, 20) |
| 286 |
guard_true(i7) [i3] |
| 287 |
jump(i1, i3, i6, descr=targettoken) |
| 288 |
''' |
| 289 |
self.interpret(ops, [0], run=False) |
| 290 |
|
| 291 |
|
| 292 |
def test_flowcontext(self): |
| 293 |
|
| 294 |
|
| 295 |
ops = """ |
| 296 |
[i1, i2] |
| 297 |
i3 = int_and(i1, 255) |
| 298 |
i4 = int_rshift(i1, 8) |
| 299 |
i5 = int_and(i4, 255) |
| 300 |
i6 = int_lt(0, i5) |
| 301 |
guard_false(i6) [i1] |
| 302 |
i7 = int_eq(i3, 0) |
| 303 |
guard_false(i7) [i1] |
| 304 |
i8 = int_neg(i3) |
| 305 |
i9 = int_lt(i8, 0) |
| 306 |
guard_true(i9) [i1] |
| 307 |
i10 = int_lt(i2, 0) |
| 308 |
guard_false(i10) [i1] |
| 309 |
i11 = int_add(i8, i2) |
| 310 |
i12 = int_lt(i11, 0) |
| 311 |
guard_false(i12) [i1] |
| 312 |
i13 = int_gt(i11, i2) |
| 313 |
guard_false(i13) [i1] |
| 314 |
i14 = int_sub(i2, i11) |
| 315 |
i15 = int_is_zero(i14) |
| 316 |
guard_false(i15) [i1] |
| 317 |
# this simulates the arraycopy call |
| 318 |
i16 = call_i(ConstClass(f2ptr), i11, i14, descr=f2_calldescr) |
| 319 |
finish(i16) |
| 320 |
""" |
| 321 |
self.interpret(ops, [0], run=False) |
| 322 |
|
| 323 |
assert len(self.filter_log_moves()) == 4 |
| 324 |
|
| 325 |
class TestCheckRegistersExplicitly64(BaseTestCheckRegistersExplicitly): |
| 326 |
def setup_class(self): |
| 327 |
if not IS_X86_64: |
| 328 |
pytest.skip("needs 64 bit") |
| 329 |
|
| 330 |
def test_call_use_argument_twice(self): |
| 331 |
ops = ''' |
| 332 |
[i0, i1, i2, i3] |
| 333 |
i7 = int_add(i0, i1) |
| 334 |
i8 = int_add(i2, 13) |
| 335 |
i9 = call_i(ConstClass(f2ptr), i7, i7, descr=f2_calldescr) |
| 336 |
i10 = int_is_true(i9) |
| 337 |
guard_true(i10) [i8] |
| 338 |
finish(i9) |
| 339 |
''' |
| 340 |
self.interpret(ops, [5, 6, 7, 8]) |
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
assert len(self.filter_log_moves()) == 4 |
| 345 |
|
| 346 |
def test_coalescing_float(self): |
| 347 |
ops = ''' |
| 348 |
[f0, f1, f3] |
| 349 |
f7 = float_add(f0, f1) |
| 350 |
f8 = float_add(f7, f3) |
| 351 |
f9 = call_f(ConstClass(ffptr), f8, 1.0, descr=ff_calldescr) |
| 352 |
i10 = float_ne(f9, 0.0) |
| 353 |
guard_true(i10) [] |
| 354 |
finish(f9) |
| 355 |
''' |
| 356 |
self.interpret(ops, [5.0, 6.0, 8.0]) |
| 357 |
assert len(self.filter_log_moves()) == 3 |
| 358 |
|
| 359 |
def test_malloc(self, monkeypatch): |
| 360 |
ops = ''' |
| 361 |
[i0] |
| 362 |
i1 = int_add(i0, 1) # this is using ecx or edx because it fits |
| 363 |
i6 = int_add(i0, 6) # this is using ecx or edx because it fits |
| 364 |
i2 = int_add(i6, i1) |
| 365 |
p0 = call_malloc_nursery(16) |
| 366 |
gc_store(p0, 0, 83944, 8) |
| 367 |
gc_store(p0, 8, i2, 8) |
| 368 |
i10 = int_is_true(i2) |
| 369 |
guard_true(i10) [p0, i0] |
| 370 |
finish(p0) |
| 371 |
''' |
| 372 |
monkeypatch.setattr(self.cpu.gc_ll_descr, "get_nursery_top_addr", lambda: 61) |
| 373 |
monkeypatch.setattr(self.cpu.gc_ll_descr, "get_nursery_free_addr", lambda: 68) |
| 374 |
self.interpret(ops, [0], run=False) |
| 375 |
|
| 376 |
|
| 377 |
assert len(self.filter_log_moves()) == 2 |
| 378 |
|
| 379 |
def test_dict_lookup(self, monkeypatch): |
| 380 |
monkeypatch.setattr(self.cpu.gc_ll_descr, "get_nursery_top_addr", lambda: 61) |
| 381 |
monkeypatch.setattr(self.cpu.gc_ll_descr, "get_nursery_free_addr", lambda: 68) |
| 382 |
|
| 383 |
ops = """ |
| 384 |
[i172, i182, i201, p209, p0, p219] |
| 385 |
i184 = int_lt(i172, 0) |
| 386 |
guard_false(i184) [] |
| 387 |
i185 = int_ge(i172, i182) |
| 388 |
guard_false(i185) [] |
| 389 |
i187 = int_add(i172, 1) |
| 390 |
i202 = uint_ge(i172, i201) |
| 391 |
guard_false(i202) [i172] |
| 392 |
i221 = int_xor(i172, 3430008) |
| 393 |
i223 = int_mul(i221, 1000003) |
| 394 |
i230 = call_i(ConstClass(fgcrefptr), p209, descr=fgcref_calldescr) |
| 395 |
guard_no_exception() [p209, i230, i172] |
| 396 |
i232 = int_eq(i230, -1) |
| 397 |
i233 = int_sub(i230, i232) |
| 398 |
i234 = int_xor(i223, i233) |
| 399 |
i236 = int_mul(i234, 1082525) |
| 400 |
i238 = int_add(i236, 97531) |
| 401 |
i240 = int_eq(i238, -1) |
| 402 |
i241 = int_sub(i238, i240) |
| 403 |
p242 = force_token() |
| 404 |
p244 = call_malloc_nursery(40) |
| 405 |
gc_store(p244, 0, 83568, 8) |
| 406 |
p249 = nursery_ptr_increment(p244, 24) |
| 407 |
gc_store(p249, 0, 4656, 8) |
| 408 |
gc_store(p249, 8, i172, 8) |
| 409 |
#cond_call_gc_wb(p0) |
| 410 |
gc_store(p0, 8, p242, 8) |
| 411 |
i263 = call_may_force_i(ConstClass(fppiiptr), p219, p244, i241, 0, descr=fppii_calldescr) |
| 412 |
guard_not_forced() [p0, p249, p244, i263, p219] |
| 413 |
guard_no_exception() [p0, p249, p244, i263, p219] |
| 414 |
i265 = int_lt(i263, 0) |
| 415 |
guard_true(i265) [p0, p249, p244, i263, p219] |
| 416 |
finish(i263) |
| 417 |
""" |
| 418 |
self.interpret(ops, [0], run=False) |
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
if not WIN64: |
| 428 |
assert len(self.filter_log_moves()) == 12 |
| 429 |
else: |
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
assert len(self.filter_log_moves()) == 10 |