| 1 |
from rpython.jit.codewriter.effectinfo import EffectInfo |
| 2 |
from rpython.jit.metainterp import history |
| 3 |
from rpython.jit.metainterp.warmstate import wrap, unwrap |
| 4 |
from rpython.rlib.unroll import unrolling_iterable |
| 5 |
from rpython.rtyper import rvirtualizable |
| 6 |
from rpython.rtyper.lltypesystem import lltype, llmemory |
| 7 |
from rpython.rtyper.rclass import IR_IMMUTABLE_ARRAY, IR_IMMUTABLE |
| 8 |
|
| 9 |
|
| 10 |
class VirtualizableInfo(object): |
| 11 |
|
| 12 |
def __init__(self, warmrunnerdesc, VTYPEPTR): |
| 13 |
self.warmrunnerdesc = warmrunnerdesc |
| 14 |
cpu = warmrunnerdesc.cpu |
| 15 |
self.cpu = cpu |
| 16 |
|
| 17 |
VTYPE = VTYPEPTR.TO |
| 18 |
while 'virtualizable_accessor' not in VTYPE._hints: |
| 19 |
VTYPE = VTYPE._first_struct()[1] |
| 20 |
if VTYPE is None: |
| 21 |
raise ValueError( |
| 22 |
"%r is listed in the jit driver's 'virtualizables', " |
| 23 |
"but that class doesn't have a '_virtualizable_' attribute " |
| 24 |
"(if it has _virtualizable2_, rename it to _virtualizable_)" |
| 25 |
% (VTYPEPTR,)) |
| 26 |
self.VTYPE = VTYPE |
| 27 |
self.VTYPEPTR = VTYPEPTR = lltype.Ptr(VTYPE) |
| 28 |
self.vable_token_descr = cpu.fielddescrof(VTYPE, 'vable_token') |
| 29 |
|
| 30 |
accessor = VTYPE._hints['virtualizable_accessor'] |
| 31 |
all_fields = accessor.fields |
| 32 |
static_fields = [] |
| 33 |
array_fields = [] |
| 34 |
for name, tp in all_fields.iteritems(): |
| 35 |
if tp == IR_IMMUTABLE_ARRAY: |
| 36 |
array_fields.append(name) |
| 37 |
elif tp == IR_IMMUTABLE: |
| 38 |
static_fields.append(name) |
| 39 |
else: |
| 40 |
raise Exception("unknown type: %s" % tp) |
| 41 |
self.static_fields = static_fields |
| 42 |
self.array_fields = array_fields |
| 43 |
|
| 44 |
FIELDTYPES = [getattr(VTYPE, name) for name in static_fields] |
| 45 |
ARRAYITEMTYPES = [] |
| 46 |
for name in array_fields: |
| 47 |
ARRAYPTR = getattr(VTYPE, name) |
| 48 |
assert isinstance(ARRAYPTR, lltype.Ptr) |
| 49 |
ARRAY = ARRAYPTR.TO |
| 50 |
if not isinstance(ARRAY, lltype.GcArray): |
| 51 |
raise Exception( |
| 52 |
"The virtualizable field '%s' is not an array (found %r)." |
| 53 |
" It usually means that you must try harder to ensure that" |
| 54 |
" the list is not resized at run-time. You can do that by" |
| 55 |
" using rpython.rlib.debug.make_sure_not_resized()." % |
| 56 |
(name, ARRAY)) |
| 57 |
ARRAYITEMTYPES.append(ARRAY.OF) |
| 58 |
self.array_descrs = [cpu.arraydescrof(getattr(VTYPE, name).TO) |
| 59 |
for name in array_fields] |
| 60 |
|
| 61 |
self.num_static_extra_boxes = len(static_fields) |
| 62 |
self.num_arrays = len(array_fields) |
| 63 |
self.static_field_to_extra_box = dict( |
| 64 |
[(name, i) for (i, name) in enumerate(static_fields)]) |
| 65 |
self.array_field_counter = dict( |
| 66 |
[(name, i) for (i, name) in enumerate(array_fields)]) |
| 67 |
self.static_extra_types = [history.getkind(TYPE) |
| 68 |
for TYPE in FIELDTYPES] |
| 69 |
self.arrayitem_extra_types = [history.getkind(ITEM) |
| 70 |
for ITEM in ARRAYITEMTYPES] |
| 71 |
self.static_field_descrs = [cpu.fielddescrof(VTYPE, name) |
| 72 |
for name in static_fields] |
| 73 |
self.array_field_descrs = [cpu.fielddescrof(VTYPE, name) |
| 74 |
for name in array_fields] |
| 75 |
|
| 76 |
for descr in self.static_field_descrs: |
| 77 |
descr.vinfo = self |
| 78 |
for descr in self.array_field_descrs: |
| 79 |
descr.vinfo = self |
| 80 |
|
| 81 |
self.static_field_by_descrs = dict( |
| 82 |
[(descr, i) for (i, descr) in enumerate(self.static_field_descrs)]) |
| 83 |
self.array_field_by_descrs = dict( |
| 84 |
[(descr, i) for (i, descr) in enumerate(self.array_field_descrs)]) |
| 85 |
|
| 86 |
def read_boxes(cpu, virtualizable, startindex): |
| 87 |
assert lltype.typeOf(virtualizable) == llmemory.GCREF |
| 88 |
virtualizable = cast_gcref_to_vtype(virtualizable) |
| 89 |
boxes = [] |
| 90 |
for _, fieldname in unroll_static_fields: |
| 91 |
x = getattr(virtualizable, fieldname) |
| 92 |
boxes.append(wrap(cpu, x, startindex)) |
| 93 |
startindex += 1 |
| 94 |
for _, fieldname in unroll_array_fields: |
| 95 |
lst = getattr(virtualizable, fieldname) |
| 96 |
for i in range(len(lst)): |
| 97 |
boxes.append(wrap(cpu, lst[i], startindex + i)) |
| 98 |
startindex += len(lst) |
| 99 |
return boxes |
| 100 |
|
| 101 |
def write_boxes(virtualizable, boxes): |
| 102 |
virtualizable = cast_gcref_to_vtype(virtualizable) |
| 103 |
i = 0 |
| 104 |
for FIELDTYPE, fieldname in unroll_static_fields: |
| 105 |
x = unwrap(FIELDTYPE, boxes[i]) |
| 106 |
setattr(virtualizable, fieldname, x) |
| 107 |
i = i + 1 |
| 108 |
for ARRAYITEMTYPE, fieldname in unroll_array_fields: |
| 109 |
lst = getattr(virtualizable, fieldname) |
| 110 |
for j in range(len(lst)): |
| 111 |
lst[j] = unwrap(ARRAYITEMTYPE, boxes[i]) |
| 112 |
i = i + 1 |
| 113 |
assert len(boxes) == i + 1 |
| 114 |
|
| 115 |
def get_total_size(virtualizable): |
| 116 |
virtualizable = cast_gcref_to_vtype(virtualizable) |
| 117 |
size = num_static_fields |
| 118 |
for _, fieldname in unroll_array_fields: |
| 119 |
lst = getattr(virtualizable, fieldname) |
| 120 |
size += len(lst) |
| 121 |
return size |
| 122 |
|
| 123 |
def minimum_size(): |
| 124 |
return num_static_fields |
| 125 |
|
| 126 |
def write_from_resume_data_partial(virtualizable, reader): |
| 127 |
virtualizable = cast_gcref_to_vtype(virtualizable) |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
for FIELDTYPE, fieldname in unroll_static_fields: |
| 132 |
x = reader.load_next_value_of_type(FIELDTYPE) |
| 133 |
setattr(virtualizable, fieldname, x) |
| 134 |
for ARRAYITEMTYPE, fieldname in unroll_array_fields: |
| 135 |
lst = getattr(virtualizable, fieldname) |
| 136 |
for j in range(len(lst)): |
| 137 |
lst[j] = reader.load_next_value_of_type(ARRAYITEMTYPE) |
| 138 |
|
| 139 |
def load_list_of_boxes(virtualizable, reader, vable_box): |
| 140 |
virtualizable = cast_gcref_to_vtype(virtualizable) |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
boxes = [] |
| 146 |
for FIELDTYPE, fieldname in unroll_static_fields: |
| 147 |
box = reader.next_box_of_type(FIELDTYPE) |
| 148 |
boxes.append(box) |
| 149 |
for ARRAYITEMTYPE, fieldname in unroll_array_fields: |
| 150 |
lst = getattr(virtualizable, fieldname) |
| 151 |
for j in range(len(lst)): |
| 152 |
box = reader.next_box_of_type(ARRAYITEMTYPE) |
| 153 |
boxes.append(box) |
| 154 |
boxes.append(vable_box) |
| 155 |
return boxes |
| 156 |
|
| 157 |
def check_boxes(virtualizable, boxes): |
| 158 |
virtualizable = cast_gcref_to_vtype(virtualizable) |
| 159 |
|
| 160 |
i = 0 |
| 161 |
for FIELDTYPE, fieldname in unroll_static_fields: |
| 162 |
x = unwrap(FIELDTYPE, boxes[i]) |
| 163 |
assert getattr(virtualizable, fieldname) == x |
| 164 |
i = i + 1 |
| 165 |
for ARRAYITEMTYPE, fieldname in unroll_array_fields: |
| 166 |
lst = getattr(virtualizable, fieldname) |
| 167 |
for j in range(len(lst)): |
| 168 |
assert lst[j] == unwrap(ARRAYITEMTYPE, boxes[i]) |
| 169 |
i = i + 1 |
| 170 |
assert len(boxes) == i + 1 |
| 171 |
|
| 172 |
def get_index_in_array(virtualizable, arrayindex, index): |
| 173 |
virtualizable = cast_gcref_to_vtype(virtualizable) |
| 174 |
index += self.num_static_extra_boxes |
| 175 |
j = 0 |
| 176 |
for _, fieldname in unroll_array_fields: |
| 177 |
if arrayindex == j: |
| 178 |
return index |
| 179 |
lst = getattr(virtualizable, fieldname) |
| 180 |
index += len(lst) |
| 181 |
j = j + 1 |
| 182 |
assert False, "invalid arrayindex" |
| 183 |
|
| 184 |
def get_array_length(virtualizable, arrayindex): |
| 185 |
virtualizable = cast_gcref_to_vtype(virtualizable) |
| 186 |
j = 0 |
| 187 |
for _, fieldname in unroll_array_fields: |
| 188 |
if arrayindex == j: |
| 189 |
lst = getattr(virtualizable, fieldname) |
| 190 |
return len(lst) |
| 191 |
j += 1 |
| 192 |
assert False, "invalid arrayindex" |
| 193 |
|
| 194 |
unroll_static_fields = unrolling_iterable(zip(FIELDTYPES, |
| 195 |
static_fields)) |
| 196 |
num_static_fields = len(static_fields) |
| 197 |
unroll_array_fields = unrolling_iterable(zip(ARRAYITEMTYPES, |
| 198 |
array_fields)) |
| 199 |
self.read_boxes = read_boxes |
| 200 |
self.write_boxes = write_boxes |
| 201 |
self.write_from_resume_data_partial = write_from_resume_data_partial |
| 202 |
self.load_list_of_boxes = load_list_of_boxes |
| 203 |
self.check_boxes = check_boxes |
| 204 |
self.get_index_in_array = get_index_in_array |
| 205 |
self.get_array_length = get_array_length |
| 206 |
self.get_total_size = get_total_size |
| 207 |
self.minimum_size = minimum_size |
| 208 |
|
| 209 |
def cast_to_vtype(virtualizable): |
| 210 |
return lltype.cast_pointer(VTYPEPTR, virtualizable) |
| 211 |
self.cast_to_vtype = cast_to_vtype |
| 212 |
|
| 213 |
def cast_gcref_to_vtype(virtualizable): |
| 214 |
assert lltype.typeOf(virtualizable) == llmemory.GCREF |
| 215 |
return lltype.cast_opaque_ptr(VTYPEPTR, virtualizable) |
| 216 |
self.cast_gcref_to_vtype = cast_gcref_to_vtype |
| 217 |
|
| 218 |
def clear_vable_token(virtualizable): |
| 219 |
virtualizable = cast_gcref_to_vtype(virtualizable) |
| 220 |
if virtualizable.vable_token: |
| 221 |
force_now(virtualizable) |
| 222 |
assert not virtualizable.vable_token |
| 223 |
self.clear_vable_token = clear_vable_token |
| 224 |
|
| 225 |
def tracing_before_residual_call(virtualizable): |
| 226 |
virtualizable = cast_gcref_to_vtype(virtualizable) |
| 227 |
assert not virtualizable.vable_token |
| 228 |
virtualizable.vable_token = TOKEN_TRACING_RESCALL |
| 229 |
self.tracing_before_residual_call = tracing_before_residual_call |
| 230 |
|
| 231 |
def tracing_after_residual_call(virtualizable): |
| 232 |
""" |
| 233 |
Returns whether or not the virtualizable was forced during a |
| 234 |
CALL_MAY_FORCE. |
| 235 |
""" |
| 236 |
virtualizable = cast_gcref_to_vtype(virtualizable) |
| 237 |
if virtualizable.vable_token: |
| 238 |
|
| 239 |
|
| 240 |
assert virtualizable.vable_token == TOKEN_TRACING_RESCALL |
| 241 |
virtualizable.vable_token = TOKEN_NONE |
| 242 |
return False |
| 243 |
else: |
| 244 |
|
| 245 |
return True |
| 246 |
self.tracing_after_residual_call = tracing_after_residual_call |
| 247 |
|
| 248 |
def force_now(virtualizable): |
| 249 |
token = virtualizable.vable_token |
| 250 |
if token == TOKEN_TRACING_RESCALL: |
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
virtualizable.vable_token = TOKEN_NONE |
| 256 |
else: |
| 257 |
from rpython.jit.metainterp.compile import ResumeGuardForcedDescr |
| 258 |
ResumeGuardForcedDescr.force_now(cpu, token) |
| 259 |
assert virtualizable.vable_token == TOKEN_NONE |
| 260 |
force_now._dont_inline_ = True |
| 261 |
self.force_now = force_now |
| 262 |
|
| 263 |
def is_token_nonnull_gcref(virtualizable): |
| 264 |
virtualizable = cast_gcref_to_vtype(virtualizable) |
| 265 |
return bool(virtualizable.vable_token) |
| 266 |
self.is_token_nonnull_gcref = is_token_nonnull_gcref |
| 267 |
|
| 268 |
def reset_token_gcref(virtualizable): |
| 269 |
virtualizable = cast_gcref_to_vtype(virtualizable) |
| 270 |
virtualizable.vable_token = TOKEN_NONE |
| 271 |
self.reset_token_gcref = reset_token_gcref |
| 272 |
|
| 273 |
def reset_vable_token(virtualizable): |
| 274 |
virtualizable.vable_token = TOKEN_NONE |
| 275 |
self.reset_vable_token = reset_vable_token |
| 276 |
|
| 277 |
def _freeze_(self): |
| 278 |
return True |
| 279 |
|
| 280 |
def finish(self): |
| 281 |
|
| 282 |
def force_virtualizable_if_necessary(virtualizable): |
| 283 |
if virtualizable.vable_token: |
| 284 |
self.force_now(virtualizable) |
| 285 |
force_virtualizable_if_necessary._always_inline_ = True |
| 286 |
|
| 287 |
all_graphs = self.warmrunnerdesc.translator.graphs |
| 288 |
FUNC = lltype.FuncType([self.VTYPEPTR], lltype.Void) |
| 289 |
funcptr = self.warmrunnerdesc.helper_func( |
| 290 |
lltype.Ptr(FUNC), force_virtualizable_if_necessary) |
| 291 |
rvirtualizable.replace_force_virtualizable_with_call( |
| 292 |
all_graphs, self.VTYPEPTR, funcptr) |
| 293 |
FUNC = lltype.FuncType([llmemory.GCREF], lltype.Void) |
| 294 |
self.clear_vable_ptr = self.warmrunnerdesc.helper_func( |
| 295 |
lltype.Ptr(FUNC), self.clear_vable_token) |
| 296 |
ei = EffectInfo([], [], [], [], [], [], EffectInfo.EF_CANNOT_RAISE, |
| 297 |
can_invalidate=False, |
| 298 |
oopspecindex=EffectInfo.OS_JIT_FORCE_VIRTUALIZABLE) |
| 299 |
|
| 300 |
self.clear_vable_descr = self.cpu.calldescrof(FUNC, FUNC.ARGS, |
| 301 |
FUNC.RESULT, ei) |
| 302 |
|
| 303 |
def unwrap_virtualizable_box(self, virtualizable_box): |
| 304 |
return virtualizable_box.getref(llmemory.GCREF) |
| 305 |
|
| 306 |
def is_vtypeptr(self, TYPE): |
| 307 |
return TYPE == self.VTYPEPTR |
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
_DUMMY = lltype.GcStruct('JITFRAME_DUMMY') |
| 327 |
_dummy = lltype.malloc(_DUMMY) |
| 328 |
|
| 329 |
TOKEN_NONE = lltype.nullptr(llmemory.GCREF.TO) |
| 330 |
TOKEN_TRACING_RESCALL = lltype.cast_opaque_ptr(llmemory.GCREF, _dummy) |