| 1 |
from rpython.rtyper.rmodel import inputconst, log |
| 2 |
from rpython.rtyper.lltypesystem import lltype, llmemory |
| 3 |
from rpython.rtyper import rclass |
| 4 |
from rpython.jit.metainterp import history |
| 5 |
from rpython.jit.metainterp.support import ptr2int |
| 6 |
from rpython.jit.metainterp.virtualizable import ( |
| 7 |
TOKEN_NONE, TOKEN_TRACING_RESCALL) |
| 8 |
from rpython.jit.codewriter import heaptracker |
| 9 |
from rpython.rlib.jit import InvalidVirtualRef |
| 10 |
|
| 11 |
class VirtualRefInfo: |
| 12 |
|
| 13 |
def __init__(self, warmrunnerdesc): |
| 14 |
self.warmrunnerdesc = warmrunnerdesc |
| 15 |
self.cpu = warmrunnerdesc.cpu |
| 16 |
|
| 17 |
self.JIT_VIRTUAL_REF = lltype.GcStruct('JitVirtualRef', |
| 18 |
('super', rclass.OBJECT), |
| 19 |
('virtual_token', llmemory.GCREF), |
| 20 |
('forced', rclass.OBJECTPTR)) |
| 21 |
self.jit_virtual_ref_vtable = lltype.malloc(rclass.OBJECT_VTABLE, |
| 22 |
zero=True, flavor='raw', |
| 23 |
immortal=True) |
| 24 |
if hasattr(self.cpu, 'gc_ll_descr'): |
| 25 |
heaptracker.setup_cache_gcstruct2vtable(self.cpu.gc_ll_descr) |
| 26 |
self.cpu.gc_ll_descr._cache_gcstruct2vtable[self.JIT_VIRTUAL_REF] = self.jit_virtual_ref_vtable |
| 27 |
|
| 28 |
|
| 29 |
if hasattr(self.warmrunnerdesc, 'rtyper'): |
| 30 |
self.warmrunnerdesc.rtyper.set_type_for_typeptr( |
| 31 |
self.jit_virtual_ref_vtable, self.JIT_VIRTUAL_REF) |
| 32 |
self.descr = self.cpu.sizeof(self.JIT_VIRTUAL_REF, |
| 33 |
vtable=self.jit_virtual_ref_vtable) |
| 34 |
self.jit_virtual_ref_vtable.name = rclass.alloc_array_name( |
| 35 |
'jit_virtual_ref') |
| 36 |
|
| 37 |
adr = ptr2int(self.jit_virtual_ref_vtable) |
| 38 |
self.jit_virtual_ref_const_class = history.ConstInt(adr) |
| 39 |
fielddescrof = self.cpu.fielddescrof |
| 40 |
self.descr_virtual_token = fielddescrof(self.JIT_VIRTUAL_REF, |
| 41 |
'virtual_token') |
| 42 |
self.descr_forced = fielddescrof(self.JIT_VIRTUAL_REF, 'forced') |
| 43 |
|
| 44 |
def _freeze_(self): |
| 45 |
return True |
| 46 |
|
| 47 |
def replace_force_virtual_with_call(self, graphs): |
| 48 |
|
| 49 |
c_force_virtual_ptr = None |
| 50 |
c_is_virtual_ptr = None |
| 51 |
force_virtual_count = 0 |
| 52 |
for graph in graphs: |
| 53 |
for block in graph.iterblocks(): |
| 54 |
for op in block.operations: |
| 55 |
if op.opname == 'jit_force_virtual': |
| 56 |
|
| 57 |
|
| 58 |
if c_force_virtual_ptr is None: |
| 59 |
c_force_virtual_ptr = self.get_force_virtual_fnptr() |
| 60 |
|
| 61 |
op.opname = 'direct_call' |
| 62 |
op.args = [c_force_virtual_ptr, op.args[0]] |
| 63 |
force_virtual_count += 1 |
| 64 |
|
| 65 |
if op.opname == 'jit_is_virtual': |
| 66 |
if c_is_virtual_ptr is None: |
| 67 |
c_is_virtual_ptr = self.get_is_virtual_fnptr() |
| 68 |
|
| 69 |
op.opname = 'direct_call' |
| 70 |
op.args = [c_is_virtual_ptr, op.args[0]] |
| 71 |
|
| 72 |
if c_force_virtual_ptr is not None: |
| 73 |
log("replaced %d 'jit_force_virtual' with %r" % (force_virtual_count, |
| 74 |
c_force_virtual_ptr.value)) |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
def virtual_ref_during_tracing(self, real_object): |
| 86 |
assert real_object |
| 87 |
vref = lltype.malloc(self.JIT_VIRTUAL_REF) |
| 88 |
p = lltype.cast_pointer(rclass.OBJECTPTR, vref) |
| 89 |
p.typeptr = self.jit_virtual_ref_vtable |
| 90 |
vref.virtual_token = TOKEN_NONE |
| 91 |
vref.forced = lltype.cast_opaque_ptr(rclass.OBJECTPTR, real_object) |
| 92 |
return lltype.cast_opaque_ptr(llmemory.GCREF, vref) |
| 93 |
|
| 94 |
def is_virtual_ref(self, gcref): |
| 95 |
if not gcref: |
| 96 |
return False |
| 97 |
inst = lltype.cast_opaque_ptr(rclass.OBJECTPTR, gcref) |
| 98 |
return inst.typeptr == self.jit_virtual_ref_vtable |
| 99 |
|
| 100 |
def tracing_before_residual_call(self, gcref): |
| 101 |
if not self.is_virtual_ref(gcref): |
| 102 |
return |
| 103 |
vref = lltype.cast_opaque_ptr(lltype.Ptr(self.JIT_VIRTUAL_REF), gcref) |
| 104 |
assert vref.virtual_token == TOKEN_NONE |
| 105 |
vref.virtual_token = TOKEN_TRACING_RESCALL |
| 106 |
|
| 107 |
def tracing_after_residual_call(self, gcref): |
| 108 |
if not self.is_virtual_ref(gcref): |
| 109 |
return False |
| 110 |
vref = lltype.cast_opaque_ptr(lltype.Ptr(self.JIT_VIRTUAL_REF), gcref) |
| 111 |
assert vref.forced |
| 112 |
if vref.virtual_token != TOKEN_NONE: |
| 113 |
|
| 114 |
|
| 115 |
assert vref.virtual_token == TOKEN_TRACING_RESCALL |
| 116 |
vref.virtual_token = TOKEN_NONE |
| 117 |
return False |
| 118 |
else: |
| 119 |
|
| 120 |
return True |
| 121 |
|
| 122 |
def continue_tracing(self, gcref, real_object): |
| 123 |
if not self.is_virtual_ref(gcref): |
| 124 |
return |
| 125 |
assert real_object |
| 126 |
vref = lltype.cast_opaque_ptr(lltype.Ptr(self.JIT_VIRTUAL_REF), gcref) |
| 127 |
assert vref.virtual_token != TOKEN_TRACING_RESCALL |
| 128 |
vref.virtual_token = TOKEN_NONE |
| 129 |
vref.forced = lltype.cast_opaque_ptr(rclass.OBJECTPTR, real_object) |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
def get_force_virtual_fnptr(self): |
| 134 |
|
| 135 |
def force_virtual_if_necessary(inst): |
| 136 |
if not inst or inst.typeptr != self.jit_virtual_ref_vtable: |
| 137 |
return inst |
| 138 |
return self.force_virtual(inst) |
| 139 |
|
| 140 |
FUNC = lltype.FuncType([rclass.OBJECTPTR], rclass.OBJECTPTR) |
| 141 |
funcptr = self.warmrunnerdesc.helper_func( |
| 142 |
lltype.Ptr(FUNC), |
| 143 |
force_virtual_if_necessary) |
| 144 |
return inputconst(lltype.typeOf(funcptr), funcptr) |
| 145 |
|
| 146 |
def get_is_virtual_fnptr(self): |
| 147 |
|
| 148 |
def is_virtual(inst): |
| 149 |
if not inst: |
| 150 |
return False |
| 151 |
return inst.typeptr == self.jit_virtual_ref_vtable |
| 152 |
|
| 153 |
FUNC = lltype.FuncType([rclass.OBJECTPTR], lltype.Bool) |
| 154 |
funcptr = self.warmrunnerdesc.helper_func(lltype.Ptr(FUNC), is_virtual) |
| 155 |
return inputconst(lltype.typeOf(funcptr), funcptr) |
| 156 |
|
| 157 |
def force_virtual(self, inst): |
| 158 |
vref = lltype.cast_pointer(lltype.Ptr(self.JIT_VIRTUAL_REF), inst) |
| 159 |
token = vref.virtual_token |
| 160 |
if token != TOKEN_NONE: |
| 161 |
if token == TOKEN_TRACING_RESCALL: |
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
assert vref.forced |
| 167 |
vref.virtual_token = TOKEN_NONE |
| 168 |
else: |
| 169 |
assert not vref.forced |
| 170 |
from rpython.jit.metainterp.compile import ResumeGuardForcedDescr |
| 171 |
ResumeGuardForcedDescr.force_now(self.cpu, token) |
| 172 |
assert vref.virtual_token == TOKEN_NONE |
| 173 |
assert vref.forced |
| 174 |
elif not vref.forced: |
| 175 |
|
| 176 |
raise InvalidVirtualRef |
| 177 |
return vref.forced |