| 1 |
from rpython.rtyper.lltypesystem import lltype, llmemory |
| 2 |
from rpython.rtyper import rclass |
| 3 |
from rpython.rtyper.annlowlevel import ( |
| 4 |
cast_base_ptr_to_instance, cast_gcref_to_instance, cast_instance_to_gcref) |
| 5 |
from rpython.jit.metainterp.history import ( |
| 6 |
AbstractDescr, ConstPtr, ConstInt, ConstFloat) |
| 7 |
from rpython.rlib.objectmodel import we_are_translated |
| 8 |
from rpython.rlib.debug import ll_assert, debug_print, debug_start, debug_stop |
| 9 |
|
| 10 |
|
| 11 |
def get_mutate_field_name(fieldname): |
| 12 |
if fieldname.startswith('inst_'): |
| 13 |
return 'mutate_' + fieldname[5:] |
| 14 |
else: |
| 15 |
raise AssertionError(fieldname) |
| 16 |
|
| 17 |
def get_current_qmut_instance(cpu, gcref, mutatefielddescr): |
| 18 |
"""Returns the current QuasiImmut instance in the field, |
| 19 |
possibly creating one. |
| 20 |
""" |
| 21 |
qmut_gcref = cpu.bh_getfield_gc_r(gcref, mutatefielddescr) |
| 22 |
if qmut_gcref: |
| 23 |
qmut = QuasiImmut.show(qmut_gcref) |
| 24 |
else: |
| 25 |
qmut = QuasiImmut(cpu) |
| 26 |
cpu.bh_setfield_gc_r(gcref, qmut.hide(), mutatefielddescr) |
| 27 |
return qmut |
| 28 |
|
| 29 |
def make_invalidation_function(STRUCT, mutatefieldname): |
| 30 |
|
| 31 |
descr_repr = "FieldDescr(%s, '%s')" % (STRUCT.TO, mutatefieldname) |
| 32 |
|
| 33 |
def _invalidate_now(p): |
| 34 |
qmut_ptr = getattr(p, mutatefieldname) |
| 35 |
setattr(p, mutatefieldname, lltype.nullptr(rclass.OBJECT)) |
| 36 |
qmut = cast_base_ptr_to_instance(QuasiImmut, qmut_ptr) |
| 37 |
qmut.invalidate(descr_repr) |
| 38 |
_invalidate_now._dont_inline_ = True |
| 39 |
|
| 40 |
def invalidation(p): |
| 41 |
if getattr(p, mutatefieldname): |
| 42 |
_invalidate_now(p) |
| 43 |
|
| 44 |
return invalidation |
| 45 |
|
| 46 |
def do_force_quasi_immutable(cpu, p, mutatefielddescr): |
| 47 |
qmut_ref = cpu.bh_getfield_gc_r(p, mutatefielddescr) |
| 48 |
if qmut_ref: |
| 49 |
cpu.bh_setfield_gc_r(p, ConstPtr.value, mutatefielddescr) |
| 50 |
qmut = cast_gcref_to_instance(QuasiImmut, qmut_ref) |
| 51 |
qmut.invalidate(mutatefielddescr.repr_of_descr()) |
| 52 |
|
| 53 |
|
| 54 |
class QuasiImmut(object): |
| 55 |
llopaque = True |
| 56 |
compress_limit = 30 |
| 57 |
looptokens_wrefs = None |
| 58 |
|
| 59 |
def __init__(self, cpu): |
| 60 |
self.cpu = cpu |
| 61 |
|
| 62 |
|
| 63 |
self.looptokens_wrefs = [] |
| 64 |
|
| 65 |
def hide(self): |
| 66 |
return cast_instance_to_gcref(self) |
| 67 |
|
| 68 |
@staticmethod |
| 69 |
def show(qmut_gcref): |
| 70 |
return cast_gcref_to_instance(QuasiImmut, qmut_gcref) |
| 71 |
|
| 72 |
def register_loop_token(self, wref_looptoken): |
| 73 |
if len(self.looptokens_wrefs) > self.compress_limit: |
| 74 |
self.compress_looptokens_list() |
| 75 |
self.looptokens_wrefs.append(wref_looptoken) |
| 76 |
|
| 77 |
def compress_looptokens_list(self): |
| 78 |
self.looptokens_wrefs = [wref for wref in self.looptokens_wrefs |
| 79 |
if wref() is not None] |
| 80 |
|
| 81 |
|
| 82 |
self.compress_limit = (len(self.looptokens_wrefs) + 15) * 2 |
| 83 |
|
| 84 |
def invalidate(self, descr_repr=None): |
| 85 |
debug_start("jit-invalidate-quasi-immutable") |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
if self.looptokens_wrefs is None: |
| 90 |
|
| 91 |
return |
| 92 |
wrefs = self.looptokens_wrefs |
| 93 |
self.looptokens_wrefs = [] |
| 94 |
invalidated = 0 |
| 95 |
for wref in wrefs: |
| 96 |
looptoken = wref() |
| 97 |
if looptoken is not None: |
| 98 |
invalidated += 1 |
| 99 |
looptoken.invalidated = True |
| 100 |
self.cpu.invalidate_loop(looptoken) |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
if not we_are_translated(): |
| 107 |
self.cpu.stats.invalidated_token_numbers.add( |
| 108 |
looptoken.number) |
| 109 |
debug_print("fieldname", descr_repr or "<unknown>", "invalidated", invalidated) |
| 110 |
debug_stop("jit-invalidate-quasi-immutable") |
| 111 |
|
| 112 |
|
| 113 |
class QuasiImmutDescr(AbstractDescr): |
| 114 |
|
| 115 |
|
| 116 |
struct = lltype.nullptr(llmemory.GCREF.TO) |
| 117 |
fielddescr = None |
| 118 |
|
| 119 |
def __init__(self, cpu, struct, fielddescr, mutatefielddescr): |
| 120 |
self.cpu = cpu |
| 121 |
self.struct = struct |
| 122 |
self.fielddescr = fielddescr |
| 123 |
self.mutatefielddescr = mutatefielddescr |
| 124 |
self.qmut = get_current_qmut_instance(cpu, struct, mutatefielddescr) |
| 125 |
self.constantfieldbox = self.get_current_constant_fieldvalue() |
| 126 |
|
| 127 |
def get_parent_descr(self): |
| 128 |
if self.fielddescr is not None: |
| 129 |
return self.fielddescr.get_parent_descr() |
| 130 |
|
| 131 |
def get_index(self): |
| 132 |
if self.fielddescr is not None: |
| 133 |
return self.fielddescr.get_index() |
| 134 |
return 0 |
| 135 |
|
| 136 |
def get_current_constant_fieldvalue(self): |
| 137 |
struct = self.struct |
| 138 |
fielddescr = self.fielddescr |
| 139 |
if self.fielddescr.is_pointer_field(): |
| 140 |
return ConstPtr(self.cpu.bh_getfield_gc_r(struct, fielddescr)) |
| 141 |
elif self.fielddescr.is_float_field(): |
| 142 |
return ConstFloat(self.cpu.bh_getfield_gc_f(struct, fielddescr)) |
| 143 |
else: |
| 144 |
return ConstInt(self.cpu.bh_getfield_gc_i(struct, fielddescr)) |
| 145 |
|
| 146 |
def is_still_valid_for(self, structconst): |
| 147 |
assert self.struct |
| 148 |
if self.struct != structconst.getref_base(): |
| 149 |
return False |
| 150 |
cpu = self.cpu |
| 151 |
qmut = get_current_qmut_instance(cpu, self.struct, |
| 152 |
self.mutatefielddescr) |
| 153 |
if qmut is not self.qmut: |
| 154 |
return False |
| 155 |
else: |
| 156 |
currentbox = self.get_current_constant_fieldvalue() |
| 157 |
assert self.constantfieldbox.same_constant(currentbox) |
| 158 |
return True |