| 1 |
from __future__ import print_function |
| 2 |
|
| 3 |
import sys |
| 4 |
from rpython.rtyper.extregistry import ExtRegistryEntry |
| 5 |
from rpython.rtyper.lltypesystem import lltype, llmemory, rffi |
| 6 |
from rpython.rtyper.annlowlevel import ( |
| 7 |
cast_gcref_to_instance, cast_instance_to_gcref) |
| 8 |
from rpython.rlib.objectmodel import ( |
| 9 |
we_are_translated, Symbolic, compute_unique_id, specialize, r_dict) |
| 10 |
from rpython.rlib.rarithmetic import r_int64, is_valid_int |
| 11 |
from rpython.rlib.rarithmetic import LONG_BIT, intmask, r_uint |
| 12 |
from rpython.rlib.jit import Counters |
| 13 |
|
| 14 |
from rpython.conftest import option |
| 15 |
|
| 16 |
from rpython.jit.metainterp.resoperation import ResOperation, rop,\ |
| 17 |
AbstractValue, oparity, AbstractResOp, IntOp, RefOp, FloatOp,\ |
| 18 |
opclasses |
| 19 |
from rpython.jit.metainterp.support import ptr2int, int2adr |
| 20 |
from rpython.jit.codewriter import longlong |
| 21 |
import weakref |
| 22 |
from rpython.jit.metainterp import jitexc |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
INT = 'i' |
| 27 |
REF = 'r' |
| 28 |
FLOAT = 'f' |
| 29 |
STRUCT = 's' |
| 30 |
HOLE = '_' |
| 31 |
VOID = 'v' |
| 32 |
VECTOR = 'V' |
| 33 |
|
| 34 |
FAILARGS_LIMIT = 1000 |
| 35 |
|
| 36 |
class SwitchToBlackhole(jitexc.JitException): |
| 37 |
def __init__(self, reason, raising_exception=False): |
| 38 |
self.reason = reason |
| 39 |
self.raising_exception = raising_exception |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
def getkind(TYPE, supports_floats=True, |
| 46 |
supports_longlong=True, |
| 47 |
supports_singlefloats=True): |
| 48 |
if TYPE is lltype.Void: |
| 49 |
return "void" |
| 50 |
elif isinstance(TYPE, lltype.Primitive): |
| 51 |
if TYPE is lltype.Float and supports_floats: |
| 52 |
return 'float' |
| 53 |
if TYPE is lltype.SingleFloat and supports_singlefloats: |
| 54 |
return 'int' |
| 55 |
if TYPE in (lltype.Float, lltype.SingleFloat): |
| 56 |
raise NotImplementedError("type %s not supported" % TYPE) |
| 57 |
if (TYPE != llmemory.Address and |
| 58 |
rffi.sizeof(TYPE) > rffi.sizeof(lltype.Signed)): |
| 59 |
if supports_longlong and TYPE is not lltype.LongFloat: |
| 60 |
assert rffi.sizeof(TYPE) == 8 |
| 61 |
return 'float' |
| 62 |
raise NotImplementedError("type %s is too large" % TYPE) |
| 63 |
return "int" |
| 64 |
elif isinstance(TYPE, lltype.Ptr): |
| 65 |
if TYPE.TO._gckind == 'raw': |
| 66 |
return "int" |
| 67 |
else: |
| 68 |
return "ref" |
| 69 |
else: |
| 70 |
raise NotImplementedError("type %s not supported" % TYPE) |
| 71 |
getkind._annspecialcase_ = 'specialize:memo' |
| 72 |
|
| 73 |
def repr_pointer(box): |
| 74 |
from rpython.rtyper.lltypesystem import rstr |
| 75 |
try: |
| 76 |
T = box.value._obj.container._normalizedcontainer(check=False)._TYPE |
| 77 |
if T is rstr.STR: |
| 78 |
return repr(box._get_str()) |
| 79 |
return '*%s' % (T._name,) |
| 80 |
except AttributeError: |
| 81 |
return box.value |
| 82 |
|
| 83 |
def repr_object(box): |
| 84 |
try: |
| 85 |
TYPE = box.value.obj._TYPE |
| 86 |
return repr(TYPE) |
| 87 |
except AttributeError: |
| 88 |
return box.value |
| 89 |
|
| 90 |
def repr_rpython(box, typechars): |
| 91 |
return '%s/%s' % (box._get_hash_(), typechars, |
| 92 |
) |
| 93 |
|
| 94 |
|
| 95 |
class AbstractDescr(AbstractValue): |
| 96 |
_attrs_ = [] |
| 97 |
__slots__ = () |
| 98 |
llopaque = True |
| 99 |
|
| 100 |
def get_descr_index(self): |
| 101 |
return -1 |
| 102 |
|
| 103 |
def get_ei_index(self): |
| 104 |
return sys.maxint |
| 105 |
|
| 106 |
def repr_of_descr(self): |
| 107 |
return '%r' % (self,) |
| 108 |
|
| 109 |
def hide(self, cpu): |
| 110 |
return cast_instance_to_gcref(self) |
| 111 |
|
| 112 |
@staticmethod |
| 113 |
def show(cpu, descr_gcref): |
| 114 |
return cast_gcref_to_instance(AbstractDescr, descr_gcref) |
| 115 |
|
| 116 |
def get_vinfo(self): |
| 117 |
raise NotImplementedError |
| 118 |
|
| 119 |
def get_type_id(self): |
| 120 |
return -1 |
| 121 |
|
| 122 |
def get_vtable(self): |
| 123 |
from rpython.rtyper import rclass |
| 124 |
return ptr2int(lltype.nullptr(rclass.OBJECT_VTABLE)) |
| 125 |
|
| 126 |
DONT_CHANGE = AbstractDescr() |
| 127 |
|
| 128 |
class AbstractFailDescr(AbstractDescr): |
| 129 |
index = -1 |
| 130 |
final_descr = False |
| 131 |
|
| 132 |
_attrs_ = ('adr_jump_offset', 'rd_locs', 'rd_loop_token', 'rd_vector_info') |
| 133 |
|
| 134 |
rd_vector_info = None |
| 135 |
|
| 136 |
def handle_fail(self, deadframe, metainterp_sd, jitdriver_sd): |
| 137 |
raise NotImplementedError |
| 138 |
def compile_and_attach(self, metainterp, new_loop, orig_inputargs): |
| 139 |
raise NotImplementedError |
| 140 |
|
| 141 |
def exits_early(self): |
| 142 |
|
| 143 |
|
| 144 |
return False |
| 145 |
|
| 146 |
def loop_version(self): |
| 147 |
|
| 148 |
return False |
| 149 |
|
| 150 |
def attach_vector_info(self, info): |
| 151 |
from rpython.jit.metainterp.resume import VectorInfo |
| 152 |
assert isinstance(info, VectorInfo) |
| 153 |
info.prev = self.rd_vector_info |
| 154 |
self.rd_vector_info = info |
| 155 |
|
| 156 |
class BasicFinalDescr(AbstractFailDescr): |
| 157 |
final_descr = True |
| 158 |
|
| 159 |
def __init__(self, identifier=None): |
| 160 |
self.identifier = identifier |
| 161 |
|
| 162 |
|
| 163 |
class BasicFailDescr(AbstractFailDescr): |
| 164 |
def __init__(self, identifier=None): |
| 165 |
self.identifier = identifier |
| 166 |
|
| 167 |
def make_a_counter_per_value(self, op, index): |
| 168 |
pass |
| 169 |
|
| 170 |
|
| 171 |
@specialize.argtype(0) |
| 172 |
def newconst(value): |
| 173 |
if value is None: |
| 174 |
return ConstPtr(lltype.nullptr(llmemory.GCREF.TO)) |
| 175 |
elif lltype.typeOf(value) == lltype.Signed: |
| 176 |
return ConstInt(value) |
| 177 |
elif isinstance(value, bool): |
| 178 |
return ConstInt(int(value)) |
| 179 |
elif lltype.typeOf(value) == longlong.FLOATSTORAGE: |
| 180 |
return ConstFloat(value) |
| 181 |
else: |
| 182 |
assert lltype.typeOf(value) == llmemory.GCREF |
| 183 |
return ConstPtr(value) |
| 184 |
|
| 185 |
class MissingValue(object): |
| 186 |
"NOT_RPYTHON" |
| 187 |
|
| 188 |
|
| 189 |
class Const(AbstractValue): |
| 190 |
_attrs_ = () |
| 191 |
|
| 192 |
@staticmethod |
| 193 |
def _new(x): |
| 194 |
"NOT_RPYTHON" |
| 195 |
T = lltype.typeOf(x) |
| 196 |
kind = getkind(T) |
| 197 |
if kind == "int": |
| 198 |
if isinstance(T, lltype.Ptr): |
| 199 |
intval = ptr2int(x) |
| 200 |
else: |
| 201 |
intval = lltype.cast_primitive(lltype.Signed, x) |
| 202 |
return ConstInt(intval) |
| 203 |
elif kind == "float": |
| 204 |
return ConstFloat(longlong.getfloatstorage(x)) |
| 205 |
else: |
| 206 |
raise NotImplementedError(kind) |
| 207 |
|
| 208 |
def constbox(self): |
| 209 |
return self |
| 210 |
|
| 211 |
def same_box(self, other): |
| 212 |
return self.same_constant(other) |
| 213 |
|
| 214 |
def same_constant(self, other): |
| 215 |
raise NotImplementedError |
| 216 |
|
| 217 |
def repr(self, memo): |
| 218 |
return self.repr_rpython() |
| 219 |
|
| 220 |
def is_constant(self): |
| 221 |
return True |
| 222 |
|
| 223 |
def __repr__(self): |
| 224 |
return 'Const(%s)' % self._getrepr_() |
| 225 |
|
| 226 |
|
| 227 |
class ConstInt(Const): |
| 228 |
type = INT |
| 229 |
value = 0 |
| 230 |
_attrs_ = ('value',) |
| 231 |
|
| 232 |
def __init__(self, value): |
| 233 |
if not we_are_translated(): |
| 234 |
if is_valid_int(value): |
| 235 |
value = int(value) |
| 236 |
else: |
| 237 |
assert isinstance(value, Symbolic) |
| 238 |
self.value = value |
| 239 |
|
| 240 |
def getint(self): |
| 241 |
return self.value |
| 242 |
|
| 243 |
getvalue = getint |
| 244 |
|
| 245 |
def getaddr(self): |
| 246 |
return int2adr(self.value) |
| 247 |
|
| 248 |
def _get_hash_(self): |
| 249 |
return make_hashable_int(self.value) |
| 250 |
|
| 251 |
def same_constant(self, other): |
| 252 |
if isinstance(other, ConstInt): |
| 253 |
return self.value == other.value |
| 254 |
return False |
| 255 |
|
| 256 |
def nonnull(self): |
| 257 |
return self.value != 0 |
| 258 |
|
| 259 |
def _getrepr_(self): |
| 260 |
return self.value |
| 261 |
|
| 262 |
def repr_rpython(self): |
| 263 |
return repr_rpython(self, 'ci') |
| 264 |
|
| 265 |
CONST_FALSE = ConstInt(0) |
| 266 |
CONST_TRUE = ConstInt(1) |
| 267 |
|
| 268 |
class ConstFloat(Const): |
| 269 |
type = FLOAT |
| 270 |
value = longlong.ZEROF |
| 271 |
_attrs_ = ('value',) |
| 272 |
|
| 273 |
def __init__(self, valuestorage): |
| 274 |
assert lltype.typeOf(valuestorage) is longlong.FLOATSTORAGE |
| 275 |
self.value = valuestorage |
| 276 |
|
| 277 |
@staticmethod |
| 278 |
def fromfloat(x): |
| 279 |
return ConstFloat(longlong.getfloatstorage(x)) |
| 280 |
|
| 281 |
def getfloatstorage(self): |
| 282 |
return self.value |
| 283 |
|
| 284 |
def getfloat(self): |
| 285 |
return longlong.getrealfloat(self.value) |
| 286 |
|
| 287 |
getvalue = getfloatstorage |
| 288 |
|
| 289 |
def _get_hash_(self): |
| 290 |
return longlong.gethash(self.value) |
| 291 |
|
| 292 |
def same_constant(self, other): |
| 293 |
if isinstance(other, ConstFloat): |
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
return (longlong.extract_bits(self.value) == |
| 300 |
longlong.extract_bits(other.value)) |
| 301 |
return False |
| 302 |
|
| 303 |
def nonnull(self): |
| 304 |
return bool(longlong.extract_bits(self.value)) |
| 305 |
|
| 306 |
def _getrepr_(self): |
| 307 |
return self.getfloat() |
| 308 |
|
| 309 |
def repr_rpython(self): |
| 310 |
return repr_rpython(self, 'cf') |
| 311 |
|
| 312 |
CONST_FZERO = ConstFloat(longlong.ZEROF) |
| 313 |
|
| 314 |
class ConstPtr(Const): |
| 315 |
type = REF |
| 316 |
value = lltype.nullptr(llmemory.GCREF.TO) |
| 317 |
_attrs_ = ('value',) |
| 318 |
|
| 319 |
def __init__(self, value): |
| 320 |
assert lltype.typeOf(value) == llmemory.GCREF |
| 321 |
self.value = value |
| 322 |
|
| 323 |
def getref_base(self): |
| 324 |
return self.value |
| 325 |
|
| 326 |
getvalue = getref_base |
| 327 |
|
| 328 |
def getref(self, PTR): |
| 329 |
return lltype.cast_opaque_ptr(PTR, self.getref_base()) |
| 330 |
getref._annspecialcase_ = 'specialize:arg(1)' |
| 331 |
|
| 332 |
def _get_hash_(self): |
| 333 |
if self.value: |
| 334 |
return lltype.identityhash(self.value) |
| 335 |
else: |
| 336 |
return 0 |
| 337 |
|
| 338 |
def same_constant(self, other): |
| 339 |
if isinstance(other, ConstPtr): |
| 340 |
return self.value == other.value |
| 341 |
return False |
| 342 |
|
| 343 |
def nonnull(self): |
| 344 |
return bool(self.value) |
| 345 |
|
| 346 |
_getrepr_ = repr_pointer |
| 347 |
|
| 348 |
def repr_rpython(self): |
| 349 |
return repr_rpython(self, 'cp') |
| 350 |
|
| 351 |
def _get_str(self): |
| 352 |
from rpython.rtyper.annlowlevel import hlstr |
| 353 |
from rpython.rtyper.lltypesystem import rstr |
| 354 |
try: |
| 355 |
return hlstr(lltype.cast_opaque_ptr(lltype.Ptr(rstr.STR), |
| 356 |
self.value)) |
| 357 |
except lltype.UninitializedMemoryAccess: |
| 358 |
return '<uninitialized string>' |
| 359 |
|
| 360 |
|
| 361 |
class ConstPtrJitCode(ConstPtr): |
| 362 |
""" a ConstPtr that comes from a constant in the jitcode. has an extra |
| 363 |
field to cache the encoding in the opencoder. """ |
| 364 |
_attrs_ = ('opencoder_index', ) |
| 365 |
opencoder_index = -1 |
| 366 |
|
| 367 |
|
| 368 |
CONST_NULL = ConstPtr(ConstPtr.value) |
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
def make_hashable_int(i): |
| 374 |
from rpython.rtyper.lltypesystem.ll2ctypes import NotCtypesAllocatedStructure |
| 375 |
if not we_are_translated() and isinstance(i, llmemory.AddressAsInt): |
| 376 |
|
| 377 |
adr = int2adr(i) |
| 378 |
try: |
| 379 |
return llmemory.cast_adr_to_int(adr, "emulated") |
| 380 |
except NotCtypesAllocatedStructure: |
| 381 |
return 12345 |
| 382 |
return i |
| 383 |
|
| 384 |
def get_const_ptr_for_string(s): |
| 385 |
from rpython.rtyper.annlowlevel import llstr |
| 386 |
if not we_are_translated(): |
| 387 |
try: |
| 388 |
return _const_ptr_for_string[s] |
| 389 |
except KeyError: |
| 390 |
pass |
| 391 |
result = ConstPtr(lltype.cast_opaque_ptr(llmemory.GCREF, llstr(s))) |
| 392 |
if not we_are_translated(): |
| 393 |
_const_ptr_for_string[s] = result |
| 394 |
return result |
| 395 |
_const_ptr_for_string = {} |
| 396 |
|
| 397 |
def get_const_ptr_for_unicode(s): |
| 398 |
from rpython.rtyper.annlowlevel import llunicode |
| 399 |
if not we_are_translated(): |
| 400 |
try: |
| 401 |
return _const_ptr_for_unicode[s] |
| 402 |
except KeyError: |
| 403 |
pass |
| 404 |
if isinstance(s, str): |
| 405 |
s = unicode(s) |
| 406 |
result = ConstPtr(lltype.cast_opaque_ptr(llmemory.GCREF, llunicode(s))) |
| 407 |
if not we_are_translated(): |
| 408 |
_const_ptr_for_unicode[s] = result |
| 409 |
return result |
| 410 |
_const_ptr_for_unicode = {} |
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
@specialize.call_location() |
| 415 |
def new_ref_dict(): |
| 416 |
return r_dict(rd_eq, rd_hash, simple_hash_eq=True) |
| 417 |
|
| 418 |
def rd_eq(ref1, ref2): |
| 419 |
return ref1 == ref2 |
| 420 |
|
| 421 |
def rd_hash(ref): |
| 422 |
assert ref |
| 423 |
return lltype.identityhash(ref) |
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
class JitCellToken(AbstractDescr): |
| 432 |
"""Used for rop.JUMP, giving the target of the jump. |
| 433 |
This is different from TreeLoop: the TreeLoop class contains the |
| 434 |
whole loop, including 'operations', and goes away after the loop |
| 435 |
was compiled; but the LoopDescr remains alive and points to the |
| 436 |
generated assembler. |
| 437 |
""" |
| 438 |
FORCE_BRIDGE_SEGMENTING = 1 |
| 439 |
|
| 440 |
target_tokens = None |
| 441 |
failed_states = None |
| 442 |
retraced_count = 0 |
| 443 |
invalidated = False |
| 444 |
outermost_jitdriver_sd = None |
| 445 |
|
| 446 |
number = -1 |
| 447 |
generation = r_int64(0) |
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
compiled_loop_token = None |
| 453 |
|
| 454 |
def __init__(self): |
| 455 |
|
| 456 |
self._keepalive_jitcell_tokens = {} |
| 457 |
|
| 458 |
def record_jump_to(self, jitcell_token): |
| 459 |
assert isinstance(jitcell_token, JitCellToken) |
| 460 |
self._keepalive_jitcell_tokens[jitcell_token] = None |
| 461 |
|
| 462 |
def __repr__(self): |
| 463 |
return '<Loop %d, gen=%d>' % (self.number, self.generation) |
| 464 |
|
| 465 |
def repr_of_descr(self): |
| 466 |
return '<Loop%d>' % self.number |
| 467 |
|
| 468 |
def dump(self): |
| 469 |
self.compiled_loop_token.cpu.dump_loop_token(self) |
| 470 |
|
| 471 |
def get_retraced_count(self): |
| 472 |
return self.retraced_count >> 1 |
| 473 |
|
| 474 |
def set_retraced_count(self, value): |
| 475 |
self.retraced_count = (value << 1) | (self.retraced_count & 1) |
| 476 |
|
| 477 |
class TargetToken(AbstractDescr): |
| 478 |
_ll_loop_code = 0 |
| 479 |
|
| 480 |
|
| 481 |
def __init__(self, targeting_jitcell_token=None, |
| 482 |
original_jitcell_token=None): |
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
self.targeting_jitcell_token = targeting_jitcell_token |
| 500 |
self.original_jitcell_token = original_jitcell_token |
| 501 |
|
| 502 |
self.virtual_state = None |
| 503 |
self.short_preamble = None |
| 504 |
|
| 505 |
def repr_of_descr(self): |
| 506 |
return 'TargetToken(%d)' % compute_unique_id(self) |
| 507 |
|
| 508 |
class TreeLoop(object): |
| 509 |
inputargs = None |
| 510 |
operations = None |
| 511 |
call_pure_results = None |
| 512 |
logops = None |
| 513 |
quasi_immutable_deps = None |
| 514 |
|
| 515 |
def _token(*args): |
| 516 |
raise Exception("TreeLoop.token is killed") |
| 517 |
token = property(_token, _token) |
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
original_jitcell_token = None |
| 525 |
|
| 526 |
def __init__(self, name): |
| 527 |
self.name = name |
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
def _all_operations(self, omit_finish=False): |
| 533 |
"NOT_RPYTHON" |
| 534 |
result = [] |
| 535 |
_list_all_operations(result, self.operations, omit_finish) |
| 536 |
return result |
| 537 |
|
| 538 |
def summary(self, adding_insns={}, omit_finish=True): |
| 539 |
"NOT_RPYTHON" |
| 540 |
insns = adding_insns.copy() |
| 541 |
for op in self._all_operations(omit_finish=omit_finish): |
| 542 |
opname = op.getopname() |
| 543 |
insns[opname] = insns.get(opname, 0) + 1 |
| 544 |
return insns |
| 545 |
|
| 546 |
def get_operations(self): |
| 547 |
return self.operations |
| 548 |
|
| 549 |
def get_display_text(self, memo): |
| 550 |
return '%s\n[%s]' % ( |
| 551 |
self.name, |
| 552 |
', '.join([box.repr(memo) for box in self.inputargs])) |
| 553 |
|
| 554 |
def show(self, errmsg=None): |
| 555 |
"NOT_RPYTHON" |
| 556 |
from rpython.jit.metainterp.graphpage import display_procedures |
| 557 |
display_procedures([self], errmsg) |
| 558 |
|
| 559 |
def check_consistency(self, check_descr=True): |
| 560 |
"NOT_RPYTHON" |
| 561 |
self.check_consistency_of(self.inputargs, self.operations, |
| 562 |
check_descr=check_descr) |
| 563 |
for op in self.operations: |
| 564 |
descr = op.getdescr() |
| 565 |
if check_descr and op.getopnum() == rop.LABEL and isinstance(descr, TargetToken): |
| 566 |
assert descr.original_jitcell_token is self.original_jitcell_token |
| 567 |
|
| 568 |
@staticmethod |
| 569 |
def check_consistency_of(inputargs, operations, check_descr=True): |
| 570 |
"NOT_RPYTHON" |
| 571 |
for box in inputargs: |
| 572 |
assert not isinstance(box, Const), "Loop.inputargs contains %r" % (box,) |
| 573 |
seen = dict.fromkeys(inputargs) |
| 574 |
assert len(seen) == len(inputargs), ( |
| 575 |
"duplicate Box in the Loop.inputargs") |
| 576 |
TreeLoop.check_consistency_of_branch(operations, seen, |
| 577 |
check_descr=check_descr) |
| 578 |
|
| 579 |
@staticmethod |
| 580 |
def check_consistency_of_branch(operations, seen, check_descr=True): |
| 581 |
"NOT_RPYTHON" |
| 582 |
for num, op in enumerate(operations): |
| 583 |
if op.is_ovf(): |
| 584 |
assert operations[num + 1].getopnum() in (rop.GUARD_NO_OVERFLOW, |
| 585 |
rop.GUARD_OVERFLOW) |
| 586 |
for i in range(op.numargs()): |
| 587 |
box = op.getarg(i) |
| 588 |
if not isinstance(box, Const): |
| 589 |
assert box in seen |
| 590 |
if op.is_guard() and check_descr: |
| 591 |
assert op.getdescr() is not None |
| 592 |
if hasattr(op.getdescr(), '_debug_suboperations'): |
| 593 |
ops = op.getdescr()._debug_suboperations |
| 594 |
TreeLoop.check_consistency_of_branch(ops, seen.copy()) |
| 595 |
for box in op.getfailargs() or []: |
| 596 |
if box is not None: |
| 597 |
assert not isinstance(box, Const) |
| 598 |
assert box in seen |
| 599 |
elif check_descr: |
| 600 |
assert op.getfailargs() is None |
| 601 |
if op.type != 'v': |
| 602 |
seen[op] = True |
| 603 |
if op.getopnum() == rop.LABEL: |
| 604 |
inputargs = op.getarglist() |
| 605 |
for box in inputargs: |
| 606 |
assert not isinstance(box, Const), "LABEL contains %r" % (box,) |
| 607 |
seen = dict.fromkeys(inputargs) |
| 608 |
assert len(seen) == len(inputargs), ( |
| 609 |
"duplicate Box in the LABEL arguments") |
| 610 |
|
| 611 |
|
| 612 |
if operations[-1].getopnum() == rop.JUMP: |
| 613 |
target = operations[-1].getdescr() |
| 614 |
if target is not None: |
| 615 |
assert isinstance(target, TargetToken) |
| 616 |
|
| 617 |
def dump(self): |
| 618 |
|
| 619 |
print('%r: inputargs =' % self, self._dump_args(self.inputargs)) |
| 620 |
for op in self.operations: |
| 621 |
args = op.getarglist() |
| 622 |
print('\t', op.getopname(), self._dump_args(args), |
| 623 |
self._dump_box(op.result)) |
| 624 |
|
| 625 |
def _dump_args(self, boxes): |
| 626 |
return '[' + ', '.join([self._dump_box(box) for box in boxes]) + ']' |
| 627 |
|
| 628 |
def _dump_box(self, box): |
| 629 |
if box is None: |
| 630 |
return 'None' |
| 631 |
else: |
| 632 |
return box.repr_rpython() |
| 633 |
|
| 634 |
def __repr__(self): |
| 635 |
return '<%s>' % (self.name,) |
| 636 |
|
| 637 |
def _list_all_operations(result, operations, omit_finish=True): |
| 638 |
if omit_finish and operations[-1].getopnum() == rop.FINISH: |
| 639 |
|
| 640 |
return |
| 641 |
result.extend(operations) |
| 642 |
for op in operations: |
| 643 |
if op.is_guard() and op.getdescr(): |
| 644 |
if hasattr(op.getdescr(), '_debug_suboperations'): |
| 645 |
ops = op.getdescr()._debug_suboperations |
| 646 |
_list_all_operations(result, ops, omit_finish) |
| 647 |
|
| 648 |
|
| 649 |
|
| 650 |
|
| 651 |
FO_REPLACED_WITH_CONST = r_uint(1) |
| 652 |
FO_POSITION_SHIFT = 1 |
| 653 |
FO_POSITION_MASK = r_uint(0xFFFFFFFE) |
| 654 |
|
| 655 |
|
| 656 |
class FrontendOp(AbstractResOp): |
| 657 |
type = 'v' |
| 658 |
_attrs_ = ('position_and_flags',) |
| 659 |
|
| 660 |
def __init__(self, pos): |
| 661 |
|
| 662 |
|
| 663 |
p = rffi.cast(rffi.UINT, pos << FO_POSITION_SHIFT) |
| 664 |
self.position_and_flags = r_uint(p) |
| 665 |
|
| 666 |
def get_position(self): |
| 667 |
|
| 668 |
p = rffi.cast(rffi.INT, self.position_and_flags) |
| 669 |
return intmask(p) >> FO_POSITION_SHIFT |
| 670 |
|
| 671 |
def is_replaced_with_const(self): |
| 672 |
return bool(self.position_and_flags & FO_REPLACED_WITH_CONST) |
| 673 |
|
| 674 |
def set_replaced_with_const(self): |
| 675 |
self.position_and_flags |= FO_REPLACED_WITH_CONST |
| 676 |
|
| 677 |
def __repr__(self): |
| 678 |
return '%s(0x%x)' % (self.__class__.__name__, self.position_and_flags) |
| 679 |
|
| 680 |
class IntFrontendOp(IntOp, FrontendOp): |
| 681 |
_attrs_ = ('position_and_flags', '_resint') |
| 682 |
|
| 683 |
def __init__(self, pos, value): |
| 684 |
FrontendOp.__init__(self, pos) |
| 685 |
self._resint = value |
| 686 |
|
| 687 |
|
| 688 |
class FloatFrontendOp(FloatOp, FrontendOp): |
| 689 |
_attrs_ = ('position_and_flags', '_resfloat') |
| 690 |
|
| 691 |
def __init__(self, pos, value): |
| 692 |
FrontendOp.__init__(self, pos) |
| 693 |
self._resfloat = value |
| 694 |
|
| 695 |
|
| 696 |
class RefFrontendOp(RefOp, FrontendOp): |
| 697 |
_attrs_ = ('position_and_flags', '_resref', '_heapc_deps') |
| 698 |
if LONG_BIT == 32: |
| 699 |
_attrs_ += ('_heapc_flags',) |
| 700 |
_heapc_flags = r_uint(0) |
| 701 |
_heapc_deps = None |
| 702 |
|
| 703 |
def __init__(self, pos, value): |
| 704 |
FrontendOp.__init__(self, pos) |
| 705 |
self._resref = value |
| 706 |
|
| 707 |
if LONG_BIT == 32: |
| 708 |
def _get_heapc_flags(self): |
| 709 |
return self._heapc_flags |
| 710 |
def _set_heapc_flags(self, value): |
| 711 |
self._heapc_flags = value |
| 712 |
else: |
| 713 |
def _get_heapc_flags(self): |
| 714 |
return self.position_and_flags >> 32 |
| 715 |
def _set_heapc_flags(self, value): |
| 716 |
self.position_and_flags = ( |
| 717 |
(self.position_and_flags & 0xFFFFFFFF) | |
| 718 |
(value << 32)) |
| 719 |
|
| 720 |
|
| 721 |
class History(object): |
| 722 |
trace = None |
| 723 |
|
| 724 |
def __init__(self, max_num_inputargs, metainterp_sd): |
| 725 |
from rpython.jit.metainterp.opencoder import Trace |
| 726 |
self.trace = Trace(max_num_inputargs, metainterp_sd) |
| 727 |
|
| 728 |
def set_inputargs(self, inpargs): |
| 729 |
self.trace.set_inputargs(inpargs) |
| 730 |
self.inputargs = inpargs |
| 731 |
|
| 732 |
def length(self): |
| 733 |
return self.trace._count - len(self.trace.inputargs) |
| 734 |
|
| 735 |
def trace_tag_overflow(self): |
| 736 |
return self.trace.tag_overflow |
| 737 |
|
| 738 |
def get_trace_position(self): |
| 739 |
return self.trace.cut_point() |
| 740 |
|
| 741 |
def cut(self, cut_at): |
| 742 |
self.trace.cut_at(cut_at) |
| 743 |
|
| 744 |
def any_operation(self): |
| 745 |
return self.trace._count > self.trace._start |
| 746 |
|
| 747 |
@specialize.argtype(2) |
| 748 |
def _record_op(self, opnum, argboxes, descr=None): |
| 749 |
return self.trace.record_op(opnum, argboxes, descr) |
| 750 |
|
| 751 |
@specialize.argtype(3) |
| 752 |
def record(self, opnum, argboxes, value, descr=None): |
| 753 |
pos = self._record_op(opnum, argboxes, descr) |
| 754 |
op = self._make_op(pos, value) |
| 755 |
return op |
| 756 |
|
| 757 |
@specialize.argtype(2) |
| 758 |
def record0(self, opnum, value, descr=None): |
| 759 |
pos = self.trace.record_op0(opnum, descr) |
| 760 |
op = self._make_op(pos, value) |
| 761 |
return op |
| 762 |
|
| 763 |
@specialize.argtype(3) |
| 764 |
def record1(self, opnum, argbox1, value, descr=None): |
| 765 |
pos = self.trace.record_op1(opnum, argbox1, descr) |
| 766 |
op = self._make_op(pos, value) |
| 767 |
return op |
| 768 |
|
| 769 |
@specialize.argtype(4) |
| 770 |
def record2(self, opnum, argbox1, argbox2, value, descr=None): |
| 771 |
pos = self.trace.record_op2(opnum, argbox1, argbox2, descr) |
| 772 |
op = self._make_op(pos, value) |
| 773 |
return op |
| 774 |
|
| 775 |
@specialize.argtype(5) |
| 776 |
def record3(self, opnum, argbox1, argbox2, argbox3, value, descr=None): |
| 777 |
pos = self.trace.record_op3(opnum, argbox1, argbox2, argbox3, descr) |
| 778 |
op = self._make_op(pos, value) |
| 779 |
return op |
| 780 |
|
| 781 |
@specialize.argtype(2) |
| 782 |
def _make_op(self, pos, value): |
| 783 |
if value is None: |
| 784 |
op = FrontendOp(pos) |
| 785 |
elif isinstance(value, bool): |
| 786 |
op = IntFrontendOp(pos, int(value)) |
| 787 |
elif lltype.typeOf(value) == lltype.Signed: |
| 788 |
op = IntFrontendOp(pos, value) |
| 789 |
elif lltype.typeOf(value) is longlong.FLOATSTORAGE: |
| 790 |
op = FloatFrontendOp(pos, value) |
| 791 |
else: |
| 792 |
assert lltype.typeOf(value) == llmemory.GCREF |
| 793 |
op = RefFrontendOp(pos, value) |
| 794 |
return op |
| 795 |
|
| 796 |
def record_nospec(self, opnum, argboxes, valueconst, descr=None): |
| 797 |
tp = opclasses[opnum].type |
| 798 |
pos = self._record_op(opnum, argboxes, descr) |
| 799 |
if tp == 'v': |
| 800 |
assert valueconst is None |
| 801 |
return FrontendOp(pos) |
| 802 |
elif tp == 'i': |
| 803 |
return IntFrontendOp(pos, valueconst.getint()) |
| 804 |
elif tp == 'f': |
| 805 |
return FloatFrontendOp(pos, valueconst.getfloatstorage()) |
| 806 |
assert tp == 'r' |
| 807 |
return RefFrontendOp(pos, valueconst.getref_base()) |
| 808 |
|
| 809 |
def record_same_as(self, box): |
| 810 |
if box.type == 'i': |
| 811 |
return self.record1(rop.SAME_AS_I, box, box.getint()) |
| 812 |
elif box.type == 'r': |
| 813 |
return self.record1(rop.SAME_AS_R, box, box.getref_base()) |
| 814 |
else: |
| 815 |
assert box.type == 'f' |
| 816 |
return self.record1(rop.SAME_AS_F, box, box.getfloatstorage()) |
| 817 |
|
| 818 |
|
| 819 |
|
| 820 |
|
| 821 |
|
| 822 |
class NoStats(object): |
| 823 |
|
| 824 |
def set_history(self, history): |
| 825 |
pass |
| 826 |
|
| 827 |
def aborted(self): |
| 828 |
pass |
| 829 |
|
| 830 |
def entered(self): |
| 831 |
pass |
| 832 |
|
| 833 |
def compiled(self): |
| 834 |
pass |
| 835 |
|
| 836 |
def add_merge_point_location(self, loc): |
| 837 |
pass |
| 838 |
|
| 839 |
def name_for_new_loop(self): |
| 840 |
return 'Loop' |
| 841 |
|
| 842 |
def add_new_loop(self, loop): |
| 843 |
pass |
| 844 |
|
| 845 |
def record_aborted(self, greenkey): |
| 846 |
pass |
| 847 |
|
| 848 |
def view(self, **kwds): |
| 849 |
pass |
| 850 |
|
| 851 |
def clear(self): |
| 852 |
pass |
| 853 |
|
| 854 |
def add_jitcell_token(self, token): |
| 855 |
pass |
| 856 |
|
| 857 |
class Stats(object): |
| 858 |
"""For tests.""" |
| 859 |
|
| 860 |
compiled_count = 0 |
| 861 |
enter_count = 0 |
| 862 |
aborted_count = 0 |
| 863 |
|
| 864 |
def __init__(self, metainterp_sd): |
| 865 |
self.loops = [] |
| 866 |
self.locations = [] |
| 867 |
self.aborted_keys = [] |
| 868 |
self.invalidated_token_numbers = set() |
| 869 |
self.jitcell_token_wrefs = [] |
| 870 |
self.jitcell_dicts = [] |
| 871 |
self.metainterp_sd = metainterp_sd |
| 872 |
|
| 873 |
def clear(self): |
| 874 |
del self.loops[:] |
| 875 |
del self.locations[:] |
| 876 |
del self.aborted_keys[:] |
| 877 |
del self.jitcell_token_wrefs[:] |
| 878 |
self.invalidated_token_numbers.clear() |
| 879 |
self.compiled_count = 0 |
| 880 |
self.enter_count = 0 |
| 881 |
self.aborted_count = 0 |
| 882 |
for dict in self.jitcell_dicts: |
| 883 |
dict.clear() |
| 884 |
|
| 885 |
def add_jitcell_token(self, token): |
| 886 |
assert isinstance(token, JitCellToken) |
| 887 |
self.jitcell_token_wrefs.append(weakref.ref(token)) |
| 888 |
|
| 889 |
def set_history(self, history): |
| 890 |
self.history = history |
| 891 |
|
| 892 |
def aborted(self): |
| 893 |
self.aborted_count += 1 |
| 894 |
|
| 895 |
def entered(self): |
| 896 |
self.enter_count += 1 |
| 897 |
|
| 898 |
def compiled(self): |
| 899 |
self.compiled_count += 1 |
| 900 |
|
| 901 |
def add_merge_point_location(self, loc): |
| 902 |
self.locations.append(loc) |
| 903 |
|
| 904 |
def name_for_new_loop(self): |
| 905 |
return 'Loop #%d' % len(self.loops) |
| 906 |
|
| 907 |
def add_new_loop(self, loop): |
| 908 |
self.loops.append(loop) |
| 909 |
|
| 910 |
def record_aborted(self, greenkey): |
| 911 |
self.aborted_keys.append(greenkey) |
| 912 |
|
| 913 |
|
| 914 |
|
| 915 |
def get_all_loops(self): |
| 916 |
return self.loops |
| 917 |
|
| 918 |
def get_all_jitcell_tokens(self): |
| 919 |
tokens = [t() for t in self.jitcell_token_wrefs] |
| 920 |
if None in tokens: |
| 921 |
assert False, ("get_all_jitcell_tokens will not work as " |
| 922 |
"loops have been freed") |
| 923 |
return tokens |
| 924 |
|
| 925 |
def check_history(self, expected=None, **check): |
| 926 |
insns = {} |
| 927 |
t = self.history.trace.get_iter() |
| 928 |
while not t.done(): |
| 929 |
op = t.next() |
| 930 |
opname = op.getopname() |
| 931 |
insns[opname] = insns.get(opname, 0) + 1 |
| 932 |
if expected is not None: |
| 933 |
insns.pop('debug_merge_point', None) |
| 934 |
insns.pop('enter_portal_frame', None) |
| 935 |
insns.pop('leave_portal_frame', None) |
| 936 |
assert insns == expected |
| 937 |
for insn, expected_count in check.items(): |
| 938 |
getattr(rop, insn.upper()) |
| 939 |
found = insns.get(insn, 0) |
| 940 |
assert found == expected_count, ( |
| 941 |
"found %d %r, expected %d" % (found, insn, expected_count)) |
| 942 |
return insns |
| 943 |
|
| 944 |
def check_resops(self, expected=None, omit_finish=True, **check): |
| 945 |
insns = {} |
| 946 |
if 'call' in check: |
| 947 |
assert check.pop('call') == 0 |
| 948 |
check['call_i'] = check['call_r'] = check['call_f'] = check['call_n'] = 0 |
| 949 |
if 'call_pure' in check: |
| 950 |
assert check.pop('call_pure') == 0 |
| 951 |
check['call_pure_i'] = check['call_pure_r'] = check['call_pure_f'] = 0 |
| 952 |
if 'call_may_force' in check: |
| 953 |
assert check.pop('call_may_force') == 0 |
| 954 |
check['call_may_force_i'] = check['call_may_force_r'] = check['call_may_force_f'] = check['call_may_force_n'] = 0 |
| 955 |
if 'call_assembler' in check: |
| 956 |
assert check.pop('call_assembler') == 0 |
| 957 |
check['call_assembler_i'] = check['call_assembler_r'] = check['call_assembler_f'] = check['call_assembler_n'] = 0 |
| 958 |
if 'getfield_gc' in check: |
| 959 |
assert check.pop('getfield_gc') == 0 |
| 960 |
check['getfield_gc_i'] = check['getfield_gc_r'] = check['getfield_gc_f'] = 0 |
| 961 |
if 'getarrayitem_gc_pure' in check: |
| 962 |
assert check.pop('getarrayitem_gc_pure') == 0 |
| 963 |
check['getarrayitem_gc_pure_i'] = check['getarrayitem_gc_pure_r'] = check['getarrayitem_gc_pure_f'] = 0 |
| 964 |
if 'getarrayitem_gc' in check: |
| 965 |
assert check.pop('getarrayitem_gc') == 0 |
| 966 |
check['getarrayitem_gc_i'] = check['getarrayitem_gc_r'] = check['getarrayitem_gc_f'] = 0 |
| 967 |
for loop in self.get_all_loops(): |
| 968 |
insns = loop.summary(adding_insns=insns, omit_finish=omit_finish) |
| 969 |
return self._check_insns(insns, expected, check) |
| 970 |
|
| 971 |
def _check_insns(self, insns, expected, check): |
| 972 |
if expected is not None: |
| 973 |
insns.pop('debug_merge_point', None) |
| 974 |
insns.pop('enter_portal_frame', None) |
| 975 |
insns.pop('leave_portal_frame', None) |
| 976 |
insns.pop('label', None) |
| 977 |
assert insns == expected |
| 978 |
for insn, expected_count in check.items(): |
| 979 |
getattr(rop, insn.upper()) |
| 980 |
found = insns.get(insn, 0) |
| 981 |
assert found == expected_count, ( |
| 982 |
"found %d %r, expected %d" % (found, insn, expected_count)) |
| 983 |
return insns |
| 984 |
|
| 985 |
def check_simple_loop(self, expected=None, **check): |
| 986 |
""" Useful in the simplest case when we have only one trace ending with |
| 987 |
a jump back to itself and possibly a few bridges. |
| 988 |
Only the operations within the loop formed by that single jump will |
| 989 |
be counted. |
| 990 |
""" |
| 991 |
loops = self.get_all_loops() |
| 992 |
assert len(loops) == 1 |
| 993 |
loop = loops[0] |
| 994 |
jumpop = loop.operations[-1] |
| 995 |
assert jumpop.getopnum() == rop.JUMP |
| 996 |
labels = [op for op in loop.operations if op.getopnum() == rop.LABEL] |
| 997 |
targets = [op._descr_wref() for op in labels] |
| 998 |
assert None not in targets |
| 999 |
target = jumpop._descr_wref() |
| 1000 |
assert target |
| 1001 |
assert targets.count(target) == 1 |
| 1002 |
i = loop.operations.index(labels[targets.index(target)]) |
| 1003 |
insns = {} |
| 1004 |
for op in loop.operations[i:]: |
| 1005 |
opname = op.getopname() |
| 1006 |
insns[opname] = insns.get(opname, 0) + 1 |
| 1007 |
return self._check_insns(insns, expected, check) |
| 1008 |
|
| 1009 |
def check_loops(self, expected=None, everywhere=False, **check): |
| 1010 |
insns = {} |
| 1011 |
for loop in self.get_all_loops(): |
| 1012 |
|
| 1013 |
|
| 1014 |
|
| 1015 |
insns = loop.summary(adding_insns=insns) |
| 1016 |
if expected is not None: |
| 1017 |
insns.pop('debug_merge_point', None) |
| 1018 |
print() |
| 1019 |
print() |
| 1020 |
print(" self.check_resops(%s)" % str(insns)) |
| 1021 |
print() |
| 1022 |
import pdb; pdb.set_trace() |
| 1023 |
else: |
| 1024 |
chk = ['%s=%d' % (i, insns.get(i, 0)) for i in check] |
| 1025 |
print() |
| 1026 |
print() |
| 1027 |
print(" self.check_resops(%s)" % ', '.join(chk)) |
| 1028 |
print() |
| 1029 |
import pdb; pdb.set_trace() |
| 1030 |
return |
| 1031 |
|
| 1032 |
for insn, expected_count in check.items(): |
| 1033 |
getattr(rop, insn.upper()) |
| 1034 |
found = insns.get(insn, 0) |
| 1035 |
assert found == expected_count, ( |
| 1036 |
"found %d %r, expected %d" % (found, insn, expected_count)) |
| 1037 |
return insns |
| 1038 |
|
| 1039 |
def check_consistency(self): |
| 1040 |
"NOT_RPYTHON" |
| 1041 |
for loop in self.get_all_loops(): |
| 1042 |
loop.check_consistency() |
| 1043 |
|
| 1044 |
def maybe_view(self): |
| 1045 |
if option.view: |
| 1046 |
self.view() |
| 1047 |
|
| 1048 |
def view(self, errmsg=None, extraprocedures=[], metainterp_sd=None): |
| 1049 |
from rpython.jit.metainterp.graphpage import display_procedures |
| 1050 |
procedures = self.get_all_loops()[:] |
| 1051 |
for procedure in extraprocedures: |
| 1052 |
if procedure in procedures: |
| 1053 |
procedures.remove(procedure) |
| 1054 |
procedures.append(procedure) |
| 1055 |
highlight_procedures = dict.fromkeys(extraprocedures, 1) |
| 1056 |
for procedure in procedures: |
| 1057 |
if hasattr(procedure, '_looptoken_number') and ( |
| 1058 |
procedure._looptoken_number in self.invalidated_token_numbers): |
| 1059 |
highlight_procedures.setdefault(procedure, 2) |
| 1060 |
display_procedures(procedures, errmsg, highlight_procedures, metainterp_sd) |
| 1061 |
|
| 1062 |
|
| 1063 |
|
| 1064 |
class Options: |
| 1065 |
def __init__(self, listops=False, failargs_limit=FAILARGS_LIMIT): |
| 1066 |
self.listops = listops |
| 1067 |
self.failargs_limit = failargs_limit |
| 1068 |
def _freeze_(self): |
| 1069 |
return True |
| 1070 |
|
| 1071 |
|
| 1072 |
|
| 1073 |
def check_descr(x): |
| 1074 |
"""Check that 'x' is None or an instance of AbstractDescr. |
| 1075 |
Explodes if the annotator only thinks it is an instance of AbstractValue. |
| 1076 |
""" |
| 1077 |
if x is not None: |
| 1078 |
assert isinstance(x, AbstractDescr) |
| 1079 |
|
| 1080 |
class Entry(ExtRegistryEntry): |
| 1081 |
_about_ = check_descr |
| 1082 |
|
| 1083 |
def compute_result_annotation(self, s_x): |
| 1084 |
|
| 1085 |
|
| 1086 |
from rpython.annotator import model as annmodel |
| 1087 |
if not annmodel.s_None.contains(s_x): |
| 1088 |
assert isinstance(s_x, annmodel.SomeInstance) |
| 1089 |
|
| 1090 |
|
| 1091 |
|
| 1092 |
assert issubclass(s_x.classdef.classdesc.pyobj, AbstractDescr) |
| 1093 |
|
| 1094 |
def specialize_call(self, hop): |
| 1095 |
hop.exception_cannot_occur() |
| 1096 |
|
| 1097 |
|
| 1098 |
class BackendDescr(AbstractDescr): |
| 1099 |
descr_index = -1 |
| 1100 |
|
| 1101 |
def get_descr_index(self): |
| 1102 |
return self.descr_index |
| 1103 |
|
| 1104 |
def get_ei_index(self): |
| 1105 |
return self.ei_index |