| 1 |
from rpython.jit.backend.llsupport import jitframe |
| 2 |
from rpython.jit.backend.llsupport.memcpy import memcpy_fn, memset_fn |
| 3 |
from rpython.jit.backend.llsupport.symbolic import WORD |
| 4 |
from rpython.jit.backend.llsupport.codemap import CodemapBuilder |
| 5 |
from rpython.jit.metainterp.history import (INT, REF, FLOAT, JitCellToken, |
| 6 |
ConstInt, AbstractFailDescr, VECTOR) |
| 7 |
from rpython.jit.metainterp.resoperation import ResOperation, rop |
| 8 |
from rpython.rlib import rgc, rmmap |
| 9 |
from rpython.rlib.debug import (debug_start, debug_stop, have_debug_prints_for, |
| 10 |
debug_print) |
| 11 |
from rpython.rlib.rarithmetic import r_uint |
| 12 |
from rpython.rlib.objectmodel import specialize, compute_unique_id |
| 13 |
from rpython.rtyper.annlowlevel import cast_instance_to_gcref, llhelper |
| 14 |
from rpython.rtyper.lltypesystem import rffi, lltype |
| 15 |
from rpython.rlib.rjitlog import rjitlog as jl |
| 16 |
|
| 17 |
DEBUG_COUNTER = lltype.Struct('DEBUG_COUNTER', |
| 18 |
|
| 19 |
('i', lltype.Signed), |
| 20 |
('type', lltype.Char), |
| 21 |
('number', lltype.Signed) |
| 22 |
) |
| 23 |
|
| 24 |
class GuardToken(object): |
| 25 |
def __init__(self, cpu, gcmap, faildescr, failargs, fail_locs, |
| 26 |
guard_opnum, frame_depth, faildescrindex): |
| 27 |
assert isinstance(faildescr, AbstractFailDescr) |
| 28 |
self.cpu = cpu |
| 29 |
self.faildescr = faildescr |
| 30 |
self.faildescrindex = faildescrindex |
| 31 |
self.failargs = failargs |
| 32 |
self.fail_locs = fail_locs |
| 33 |
self.gcmap = self.compute_gcmap(gcmap, failargs, |
| 34 |
fail_locs, frame_depth) |
| 35 |
self.guard_opnum = guard_opnum |
| 36 |
|
| 37 |
def guard_not_invalidated(self): |
| 38 |
return self.guard_opnum == rop.GUARD_NOT_INVALIDATED |
| 39 |
|
| 40 |
def must_save_exception(self): |
| 41 |
guard_opnum = self.guard_opnum |
| 42 |
return (guard_opnum == rop.GUARD_EXCEPTION or |
| 43 |
guard_opnum == rop.GUARD_NO_EXCEPTION or |
| 44 |
guard_opnum == rop.GUARD_NOT_FORCED) |
| 45 |
|
| 46 |
def compute_gcmap(self, gcmap, failargs, fail_locs, frame_depth): |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
input_i = 0 |
| 51 |
for i in range(len(failargs)): |
| 52 |
arg = failargs[i] |
| 53 |
if arg is None: |
| 54 |
continue |
| 55 |
loc = fail_locs[input_i] |
| 56 |
input_i += 1 |
| 57 |
if arg.type == REF: |
| 58 |
loc = fail_locs[i] |
| 59 |
if loc.is_core_reg(): |
| 60 |
val = self.cpu.all_reg_indexes[loc.value] |
| 61 |
else: |
| 62 |
val = loc.get_position() + self.cpu.JITFRAME_FIXED_SIZE |
| 63 |
gcmap[val // WORD // 8] |= r_uint(1) << (val % (WORD * 8)) |
| 64 |
return gcmap |
| 65 |
|
| 66 |
|
| 67 |
class BaseAssembler(object): |
| 68 |
""" Base class for Assembler generator in real backends |
| 69 |
""" |
| 70 |
|
| 71 |
def __init__(self, cpu, translate_support_code=False): |
| 72 |
self.cpu = cpu |
| 73 |
self.memcpy_addr = 0 |
| 74 |
self.memset_addr = 0 |
| 75 |
self.rtyper = cpu.rtyper |
| 76 |
|
| 77 |
self._debug = False |
| 78 |
self.loop_run_counters = [] |
| 79 |
|
| 80 |
|
| 81 |
self.num_moves_calls = 0 |
| 82 |
self.num_moves_jump = 0 |
| 83 |
self.num_spills = 0 |
| 84 |
self.num_spills_to_existing = 0 |
| 85 |
self.num_reloads = 0 |
| 86 |
|
| 87 |
self.preamble_num_moves_calls = 0 |
| 88 |
self.preamble_num_moves_jump = 0 |
| 89 |
self.preamble_num_spills = 0 |
| 90 |
self.preamble_num_spills_to_existing = 0 |
| 91 |
self.preamble_num_reloads = 0 |
| 92 |
|
| 93 |
|
| 94 |
def stitch_bridge(self, faildescr, target): |
| 95 |
raise NotImplementedError |
| 96 |
|
| 97 |
def setup_once(self): |
| 98 |
|
| 99 |
rmmap.enter_assembler_writing() |
| 100 |
gc_ll_descr = self.cpu.gc_ll_descr |
| 101 |
gc_ll_descr.initialize() |
| 102 |
if hasattr(gc_ll_descr, 'minimal_size_in_nursery'): |
| 103 |
self.gc_minimal_size_in_nursery = gc_ll_descr.minimal_size_in_nursery |
| 104 |
else: |
| 105 |
self.gc_minimal_size_in_nursery = 0 |
| 106 |
if hasattr(gc_ll_descr, 'gcheaderbuilder'): |
| 107 |
self.gc_size_of_header = gc_ll_descr.gcheaderbuilder.size_gc_header |
| 108 |
else: |
| 109 |
self.gc_size_of_header = WORD |
| 110 |
self.memcpy_addr = rffi.cast(lltype.Signed, memcpy_fn) |
| 111 |
self.memset_addr = rffi.cast(lltype.Signed, memset_fn) |
| 112 |
self._build_failure_recovery(False, withfloats=False) |
| 113 |
self._build_failure_recovery(True, withfloats=False) |
| 114 |
self._build_wb_slowpath(False) |
| 115 |
self._build_wb_slowpath(True) |
| 116 |
self._build_wb_slowpath(False, for_frame=True) |
| 117 |
|
| 118 |
self.build_frame_realloc_slowpath() |
| 119 |
if self.cpu.supports_floats: |
| 120 |
self._build_failure_recovery(False, withfloats=True) |
| 121 |
self._build_failure_recovery(True, withfloats=True) |
| 122 |
self._build_wb_slowpath(False, withfloats=True) |
| 123 |
self._build_wb_slowpath(True, withfloats=True) |
| 124 |
self._build_propagate_exception_path() |
| 125 |
if gc_ll_descr.get_malloc_slowpath_addr is not None: |
| 126 |
|
| 127 |
self.malloc_slowpath = self._build_malloc_slowpath(kind='fixed') |
| 128 |
self.malloc_slowpath_varsize = self._build_malloc_slowpath( |
| 129 |
kind='var') |
| 130 |
if hasattr(gc_ll_descr, 'malloc_str'): |
| 131 |
self.malloc_slowpath_str = self._build_malloc_slowpath(kind='str') |
| 132 |
else: |
| 133 |
self.malloc_slowpath_str = None |
| 134 |
if hasattr(gc_ll_descr, 'malloc_unicode'): |
| 135 |
self.malloc_slowpath_unicode = self._build_malloc_slowpath( |
| 136 |
kind='unicode') |
| 137 |
else: |
| 138 |
self.malloc_slowpath_unicode = None |
| 139 |
lst = [0, 0, 0, 0] |
| 140 |
lst[0] = self._build_cond_call_slowpath(False, False) |
| 141 |
lst[1] = self._build_cond_call_slowpath(False, True) |
| 142 |
if self.cpu.supports_floats: |
| 143 |
lst[2] = self._build_cond_call_slowpath(True, False) |
| 144 |
lst[3] = self._build_cond_call_slowpath(True, True) |
| 145 |
self.cond_call_slowpath = lst |
| 146 |
|
| 147 |
self._build_stack_check_slowpath() |
| 148 |
self._build_release_gil(gc_ll_descr.gcrootmap) |
| 149 |
|
| 150 |
if not self._debug: |
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
should_debug = have_debug_prints_for('jit-backend-counts') |
| 155 |
self.set_debug(should_debug) |
| 156 |
|
| 157 |
self.gcmap_for_finish = lltype.malloc(jitframe.GCMAP, 1, |
| 158 |
flavor='raw', |
| 159 |
track_allocation=False) |
| 160 |
self.gcmap_for_finish[0] = r_uint(1) |
| 161 |
rmmap.leave_assembler_writing() |
| 162 |
|
| 163 |
def setup(self, looptoken): |
| 164 |
if self.cpu.HAS_CODEMAP: |
| 165 |
self.codemap_builder = CodemapBuilder() |
| 166 |
self._finish_gcmap = lltype.nullptr(jitframe.GCMAP) |
| 167 |
|
| 168 |
def setup_gcrefs_list(self, allgcrefs): |
| 169 |
self._allgcrefs = allgcrefs |
| 170 |
self._allgcrefs_faildescr_next = 0 |
| 171 |
|
| 172 |
def teardown_gcrefs_list(self): |
| 173 |
self._allgcrefs = None |
| 174 |
|
| 175 |
def get_gcref_from_faildescr(self, descr): |
| 176 |
"""This assumes that it is called in order for all faildescrs.""" |
| 177 |
search = cast_instance_to_gcref(descr) |
| 178 |
while not _safe_eq( |
| 179 |
self._allgcrefs[self._allgcrefs_faildescr_next], search): |
| 180 |
self._allgcrefs_faildescr_next += 1 |
| 181 |
assert self._allgcrefs_faildescr_next < len(self._allgcrefs) |
| 182 |
return self._allgcrefs_faildescr_next |
| 183 |
|
| 184 |
def get_asmmemmgr_blocks(self, looptoken): |
| 185 |
clt = looptoken.compiled_loop_token |
| 186 |
if clt.asmmemmgr_blocks is None: |
| 187 |
clt.asmmemmgr_blocks = [] |
| 188 |
return clt.asmmemmgr_blocks |
| 189 |
|
| 190 |
def get_asmmemmgr_gcreftracers(self, looptoken): |
| 191 |
clt = looptoken.compiled_loop_token |
| 192 |
if clt.asmmemmgr_gcreftracers is None: |
| 193 |
clt.asmmemmgr_gcreftracers = [] |
| 194 |
return clt.asmmemmgr_gcreftracers |
| 195 |
|
| 196 |
def set_debug(self, v): |
| 197 |
r = self._debug |
| 198 |
self._debug = v |
| 199 |
return r |
| 200 |
|
| 201 |
def rebuild_faillocs_from_descr(self, descr, inputargs): |
| 202 |
locs = [] |
| 203 |
GPR_REGS = len(self.cpu.gen_regs) |
| 204 |
XMM_REGS = len(self.cpu.float_regs) |
| 205 |
input_i = 0 |
| 206 |
if self.cpu.IS_64_BIT: |
| 207 |
coeff = 1 |
| 208 |
else: |
| 209 |
coeff = 2 |
| 210 |
for pos in descr.rd_locs: |
| 211 |
pos = rffi.cast(lltype.Signed, pos) |
| 212 |
if pos == 0xFFFF: |
| 213 |
continue |
| 214 |
elif pos < GPR_REGS: |
| 215 |
locs.append(self.cpu.gen_regs[pos]) |
| 216 |
elif pos < GPR_REGS + XMM_REGS * coeff: |
| 217 |
pos = (pos - GPR_REGS) // coeff |
| 218 |
locs.append(self.cpu.float_regs[pos]) |
| 219 |
else: |
| 220 |
i = pos - self.cpu.JITFRAME_FIXED_SIZE |
| 221 |
assert i >= 0 |
| 222 |
tp = inputargs[input_i].type |
| 223 |
locs.append(self.new_stack_loc(i, tp)) |
| 224 |
input_i += 1 |
| 225 |
return locs |
| 226 |
|
| 227 |
_previous_rd_locs = [] |
| 228 |
|
| 229 |
def store_info_on_descr(self, startspos, guardtok): |
| 230 |
withfloats = False |
| 231 |
for box in guardtok.failargs: |
| 232 |
if box is not None and \ |
| 233 |
(box.type == FLOAT or box.type == VECTOR): |
| 234 |
withfloats = True |
| 235 |
break |
| 236 |
exc = guardtok.must_save_exception() |
| 237 |
target = self.failure_recovery_code[exc + 2 * withfloats] |
| 238 |
faildescrindex = guardtok.faildescrindex |
| 239 |
base_ofs = self.cpu.get_baseofs_of_frame_field() |
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
if len(self._previous_rd_locs) == len(guardtok.fail_locs): |
| 245 |
positions = self._previous_rd_locs |
| 246 |
shared = True |
| 247 |
else: |
| 248 |
positions = [rffi.cast(rffi.USHORT, 0)] * len(guardtok.fail_locs) |
| 249 |
shared = False |
| 250 |
|
| 251 |
for i, loc in enumerate(guardtok.fail_locs): |
| 252 |
if loc is None: |
| 253 |
position = 0xFFFF |
| 254 |
elif loc.is_stack(): |
| 255 |
assert (loc.value & (WORD - 1)) == 0, \ |
| 256 |
"store_info_on_descr: misaligned" |
| 257 |
position = (loc.value - base_ofs) // WORD |
| 258 |
assert 0 < position < 0xFFFF, "store_info_on_descr: overflow!" |
| 259 |
else: |
| 260 |
assert loc is not self.cpu.frame_reg |
| 261 |
if self.cpu.IS_64_BIT: |
| 262 |
coeff = 1 |
| 263 |
else: |
| 264 |
coeff = 2 |
| 265 |
if loc.is_float(): |
| 266 |
position = len(self.cpu.gen_regs) + loc.value * coeff |
| 267 |
else: |
| 268 |
position = self.cpu.all_reg_indexes[loc.value] |
| 269 |
|
| 270 |
if shared: |
| 271 |
if (rffi.cast(lltype.Signed, self._previous_rd_locs[i]) == |
| 272 |
rffi.cast(lltype.Signed, position)): |
| 273 |
continue |
| 274 |
positions = positions[:] |
| 275 |
shared = False |
| 276 |
positions[i] = rffi.cast(rffi.USHORT, position) |
| 277 |
self._previous_rd_locs = positions |
| 278 |
|
| 279 |
guardtok.faildescr.rd_locs = positions |
| 280 |
return faildescrindex, target |
| 281 |
|
| 282 |
def enter_portal_frame(self, op): |
| 283 |
if self.cpu.HAS_CODEMAP: |
| 284 |
pos = self.mc.get_relative_pos(break_basic_block=False) |
| 285 |
self.codemap_builder.enter_portal_frame(op.getarg(0).getint(), |
| 286 |
op.getarg(1).getint(), |
| 287 |
pos) |
| 288 |
|
| 289 |
def leave_portal_frame(self, op): |
| 290 |
if self.cpu.HAS_CODEMAP: |
| 291 |
pos = self.mc.get_relative_pos(break_basic_block=False) |
| 292 |
self.codemap_builder.leave_portal_frame(op.getarg(0).getint(), |
| 293 |
pos) |
| 294 |
|
| 295 |
def call_assembler(self, op, argloc, vloc, result_loc, tmploc): |
| 296 |
""" |
| 297 |
* argloc: location of the frame argument that we're passing to |
| 298 |
the called assembler (this is the first return value |
| 299 |
of locs_for_call_assembler()) |
| 300 |
|
| 301 |
* vloc: location of the virtualizable (not in a register; |
| 302 |
this is the optional second return value of |
| 303 |
locs_for_call_assembler(), or imm(0) if none returned) |
| 304 |
|
| 305 |
* result_loc: location of op.result (which is not be |
| 306 |
confused with the next one) |
| 307 |
|
| 308 |
* tmploc: location where the actual call to the other piece |
| 309 |
of assembler will return its jitframe result |
| 310 |
(which is always a REF), before the helper may be |
| 311 |
called |
| 312 |
""" |
| 313 |
descr = op.getdescr() |
| 314 |
assert isinstance(descr, JitCellToken) |
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
jd = descr.outermost_jitdriver_sd |
| 320 |
self._call_assembler_emit_call(self.imm(descr._ll_function_addr), |
| 321 |
argloc, tmploc) |
| 322 |
|
| 323 |
if op.type == 'v': |
| 324 |
assert result_loc is None |
| 325 |
value = self.cpu.done_with_this_frame_descr_void |
| 326 |
else: |
| 327 |
kind = op.type |
| 328 |
if kind == INT: |
| 329 |
assert result_loc is tmploc |
| 330 |
value = self.cpu.done_with_this_frame_descr_int |
| 331 |
elif kind == REF: |
| 332 |
assert result_loc is tmploc |
| 333 |
value = self.cpu.done_with_this_frame_descr_ref |
| 334 |
elif kind == FLOAT: |
| 335 |
value = self.cpu.done_with_this_frame_descr_float |
| 336 |
else: |
| 337 |
raise AssertionError(kind) |
| 338 |
|
| 339 |
gcref = cast_instance_to_gcref(value) |
| 340 |
if gcref: |
| 341 |
rgc._make_sure_does_not_move(gcref) |
| 342 |
value = rffi.cast(lltype.Signed, gcref) |
| 343 |
je_location = self._call_assembler_check_descr(value, tmploc) |
| 344 |
|
| 345 |
|
| 346 |
assert jd is not None |
| 347 |
asm_helper_adr = self.cpu.cast_adr_to_int(jd.assembler_helper_adr) |
| 348 |
|
| 349 |
self._call_assembler_emit_helper_call(self.imm(asm_helper_adr), |
| 350 |
[tmploc, vloc], result_loc) |
| 351 |
|
| 352 |
jmp_location = self._call_assembler_patch_je(result_loc, je_location) |
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
self._call_assembler_load_result(op, result_loc) |
| 358 |
|
| 359 |
|
| 360 |
self._call_assembler_patch_jmp(jmp_location) |
| 361 |
|
| 362 |
def get_loop_run_counters(self, index): |
| 363 |
return self.loop_run_counters[index] |
| 364 |
|
| 365 |
@specialize.argtype(1) |
| 366 |
def _inject_debugging_code(self, looptoken, operations, tp, number): |
| 367 |
if self._debug or jl.jitlog_enabled(): |
| 368 |
newoperations = [] |
| 369 |
self._append_debugging_code(newoperations, tp, number, None) |
| 370 |
for op in operations: |
| 371 |
newoperations.append(op) |
| 372 |
if op.getopnum() == rop.LABEL: |
| 373 |
self._append_debugging_code(newoperations, 'l', number, |
| 374 |
op.getdescr()) |
| 375 |
operations = newoperations |
| 376 |
return operations |
| 377 |
|
| 378 |
def _append_debugging_code(self, operations, tp, number, token): |
| 379 |
counter = self._register_counter(tp, number, token) |
| 380 |
c_adr = ConstInt(rffi.cast(lltype.Signed, counter)) |
| 381 |
operations.append( |
| 382 |
ResOperation(rop.INCREMENT_DEBUG_COUNTER, [c_adr])) |
| 383 |
|
| 384 |
def _register_counter(self, tp, number, token): |
| 385 |
|
| 386 |
|
| 387 |
struct = lltype.malloc(DEBUG_COUNTER, flavor='raw', |
| 388 |
track_allocation=False) |
| 389 |
struct.i = 0 |
| 390 |
struct.type = tp |
| 391 |
if tp == 'b' or tp == 'e': |
| 392 |
struct.number = number |
| 393 |
else: |
| 394 |
assert token |
| 395 |
struct.number = compute_unique_id(token) |
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
self.loop_run_counters.append(struct) |
| 400 |
return struct |
| 401 |
|
| 402 |
def finish_once(self): |
| 403 |
if self._debug: |
| 404 |
|
| 405 |
debug_start('jit-backend-counts') |
| 406 |
length = len(self.loop_run_counters) |
| 407 |
for i in range(length): |
| 408 |
struct = self.loop_run_counters[i] |
| 409 |
if struct.type == 'l': |
| 410 |
prefix = 'TargetToken(%d)' % struct.number |
| 411 |
else: |
| 412 |
num = struct.number |
| 413 |
if num == -1: |
| 414 |
num = '-1' |
| 415 |
else: |
| 416 |
num = str(r_uint(num)) |
| 417 |
if struct.type == 'b': |
| 418 |
prefix = 'bridge %s' % num |
| 419 |
else: |
| 420 |
prefix = 'entry %s' % num |
| 421 |
debug_print(prefix + ':' + str(struct.i)) |
| 422 |
debug_stop('jit-backend-counts') |
| 423 |
|
| 424 |
self.flush_trace_counters() |
| 425 |
|
| 426 |
def flush_trace_counters(self): |
| 427 |
|
| 428 |
length = len(self.loop_run_counters) |
| 429 |
for i in range(length): |
| 430 |
struct = self.loop_run_counters[i] |
| 431 |
|
| 432 |
if struct.i > 0: |
| 433 |
jl._log_jit_counter(struct) |
| 434 |
|
| 435 |
|
| 436 |
struct.i = 0 |
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
@staticmethod |
| 442 |
@rgc.no_collect |
| 443 |
def _reacquire_gil_shadowstack(): |
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
from rpython.rlib import rgil |
| 449 |
rgil.acquire() |
| 450 |
|
| 451 |
_REACQGIL0_FUNC = lltype.Ptr(lltype.FuncType([], lltype.Void)) |
| 452 |
_REACQGIL2_FUNC = lltype.Ptr(lltype.FuncType([rffi.CCHARP, lltype.Signed], |
| 453 |
lltype.Void)) |
| 454 |
|
| 455 |
def _build_release_gil(self, gcrootmap): |
| 456 |
if gcrootmap is None or gcrootmap.is_shadow_stack: |
| 457 |
reacqgil_func = llhelper(self._REACQGIL0_FUNC, |
| 458 |
self._reacquire_gil_shadowstack) |
| 459 |
self.reacqgil_addr = self.cpu.cast_ptr_to_int(reacqgil_func) |
| 460 |
else: |
| 461 |
raise AssertionError("!is_shadow_stack") |
| 462 |
|
| 463 |
def _is_asmgcc(self): |
| 464 |
return False |
| 465 |
|
| 466 |
|
| 467 |
def debug_bridge(descr_number, rawstart, codeendpos): |
| 468 |
debug_start("jit-backend-addr") |
| 469 |
debug_print("bridge out of Guard 0x%x has address 0x%x to 0x%x" % |
| 470 |
(r_uint(descr_number), r_uint(rawstart), |
| 471 |
r_uint(rawstart + codeendpos))) |
| 472 |
debug_stop("jit-backend-addr") |
| 473 |
|
| 474 |
def _safe_eq(x, y): |
| 475 |
try: |
| 476 |
return x == y |
| 477 |
except AttributeError: |
| 478 |
return False |