| 1 |
from rpython.jit.backend.llsupport.rewrite import cpu_simplify_scale |
| 2 |
from rpython.jit.backend.llsupport.descr import (unpack_arraydescr, |
| 3 |
unpack_fielddescr, unpack_interiorfielddescr, ArrayDescr) |
| 4 |
from rpython.rlib.objectmodel import specialize, always_inline |
| 5 |
from rpython.jit.metainterp.history import (VECTOR, FLOAT, INT, ConstInt) |
| 6 |
from rpython.jit.metainterp.resoperation import rop |
| 7 |
from rpython.jit.metainterp.optimizeopt.schedule import (forwarded_vecinfo, |
| 8 |
failnbail_transformation) |
| 9 |
from rpython.jit.metainterp.jitexc import NotAVectorizeableLoop |
| 10 |
from rpython.rlib.objectmodel import we_are_translated |
| 11 |
from rpython.rtyper.lltypesystem.lloperation import llop |
| 12 |
from rpython.rtyper.lltypesystem import lltype |
| 13 |
from rpython.rlib.debug import debug_print |
| 14 |
|
| 15 |
class TypeRestrict(object): |
| 16 |
ANY_TYPE = '\x00' |
| 17 |
ANY_SIZE = -1 |
| 18 |
ANY_SIGN = -1 |
| 19 |
ANY_COUNT = -1 |
| 20 |
SIGNED = 1 |
| 21 |
UNSIGNED = 0 |
| 22 |
|
| 23 |
def __init__(self, |
| 24 |
type=ANY_TYPE, |
| 25 |
bytesize=ANY_SIZE, |
| 26 |
count=ANY_SIGN, |
| 27 |
sign=ANY_COUNT): |
| 28 |
self.type = type |
| 29 |
self.bytesize = bytesize |
| 30 |
self.sign = sign |
| 31 |
self.count = count |
| 32 |
|
| 33 |
@always_inline |
| 34 |
def any_size(self): |
| 35 |
return self.bytesize == TypeRestrict.ANY_SIZE |
| 36 |
|
| 37 |
@always_inline |
| 38 |
def any_count(self): |
| 39 |
return self.count == TypeRestrict.ANY_COUNT |
| 40 |
|
| 41 |
def check(self, value): |
| 42 |
vecinfo = forwarded_vecinfo(value) |
| 43 |
assert vecinfo.datatype != '\x00' |
| 44 |
if self.type != TypeRestrict.ANY_TYPE: |
| 45 |
if self.type != vecinfo.datatype: |
| 46 |
msg = "type mismatch %s != %s" % \ |
| 47 |
(self.type, vecinfo.datatype) |
| 48 |
failnbail_transformation(msg) |
| 49 |
assert vecinfo.bytesize > 0 |
| 50 |
if not self.any_size(): |
| 51 |
if self.bytesize != vecinfo.bytesize: |
| 52 |
msg = "bytesize mismatch %s != %s" % \ |
| 53 |
(self.bytesize, vecinfo.bytesize) |
| 54 |
failnbail_transformation(msg) |
| 55 |
assert vecinfo.count > 0 |
| 56 |
if self.count != TypeRestrict.ANY_COUNT: |
| 57 |
if vecinfo.count < self.count: |
| 58 |
msg = "count mismatch %s < %s" % \ |
| 59 |
(self.count, vecinfo.count) |
| 60 |
failnbail_transformation(msg) |
| 61 |
if self.sign != TypeRestrict.ANY_SIGN: |
| 62 |
if bool(self.sign) == vecinfo.sign: |
| 63 |
msg = "sign mismatch %s < %s" % \ |
| 64 |
(self.sign, vecinfo.sign) |
| 65 |
failnbail_transformation(msg) |
| 66 |
|
| 67 |
def max_input_count(self, count): |
| 68 |
""" How many """ |
| 69 |
if self.count != TypeRestrict.ANY_COUNT: |
| 70 |
return self.count |
| 71 |
return count |
| 72 |
|
| 73 |
class OpRestrict(object): |
| 74 |
def __init__(self, argument_restris): |
| 75 |
self.argument_restrictions = argument_restris |
| 76 |
|
| 77 |
def check_operation(self, state, pack, op): |
| 78 |
return None |
| 79 |
|
| 80 |
def crop_vector(self, op, newsize, size): |
| 81 |
return newsize, size |
| 82 |
|
| 83 |
def must_crop_vector(self, op, index): |
| 84 |
restrict = self.argument_restrictions[index] |
| 85 |
vecinfo = forwarded_vecinfo(op.getarg(index)) |
| 86 |
size = vecinfo.bytesize |
| 87 |
newsize = self.crop_to_size(op, index) |
| 88 |
return not restrict.any_size() and newsize != size |
| 89 |
|
| 90 |
@always_inline |
| 91 |
def crop_to_size(self, op, index): |
| 92 |
restrict = self.argument_restrictions[index] |
| 93 |
return restrict.bytesize |
| 94 |
|
| 95 |
def opcount_filling_vector_register(self, op, vec_reg_size): |
| 96 |
""" How many operations of that kind can one execute |
| 97 |
with a machine instruction of register size X? |
| 98 |
""" |
| 99 |
if op.is_typecast(): |
| 100 |
if op.casts_down(): |
| 101 |
size = op.cast_input_bytesize(vec_reg_size) |
| 102 |
return size // op.cast_from_bytesize() |
| 103 |
else: |
| 104 |
return vec_reg_size // op.cast_to_bytesize() |
| 105 |
vecinfo = forwarded_vecinfo(op) |
| 106 |
return vec_reg_size // vecinfo.bytesize |
| 107 |
|
| 108 |
class GuardRestrict(OpRestrict): |
| 109 |
def opcount_filling_vector_register(self, op, vec_reg_size): |
| 110 |
arg = op.getarg(0) |
| 111 |
vecinfo = forwarded_vecinfo(arg) |
| 112 |
return vec_reg_size // vecinfo.bytesize |
| 113 |
|
| 114 |
class LoadRestrict(OpRestrict): |
| 115 |
def check_operation(self, state, pack, op): |
| 116 |
opnum = op.getopnum() |
| 117 |
descr = op.getdescr() |
| 118 |
if not we_are_translated() and not isinstance(descr, ArrayDescr): |
| 119 |
itemsize = descr.get_item_size_in_bytes() |
| 120 |
ofs = 0 |
| 121 |
else: |
| 122 |
itemsize, ofs, _ = unpack_arraydescr(op.getdescr()) |
| 123 |
args = [op.getarg(0), op.getarg(1), ConstInt(1), ConstInt(ofs)] |
| 124 |
if rop.is_getarrayitem(opnum) or \ |
| 125 |
opnum in (rop.GETARRAYITEM_RAW_I, rop.GETARRAYITEM_RAW_F): |
| 126 |
index_box = op.getarg(1) |
| 127 |
scale, offset, changed, emit = cpu_simplify_scale(state.cpu, index_box, itemsize, ofs) |
| 128 |
args[2] = ConstInt(scale) |
| 129 |
args[3] = ConstInt(offset) |
| 130 |
if emit: |
| 131 |
state.oplist.append(changed) |
| 132 |
args[1] = changed |
| 133 |
|
| 134 |
return args |
| 135 |
|
| 136 |
|
| 137 |
def opcount_filling_vector_register(self, op, vec_reg_size): |
| 138 |
assert rop.is_primitive_load(op.opnum) |
| 139 |
descr = op.getdescr() |
| 140 |
return vec_reg_size // descr.get_item_size_in_bytes() |
| 141 |
|
| 142 |
class StoreRestrict(OpRestrict): |
| 143 |
def __init__(self, argument_restris): |
| 144 |
self.argument_restrictions = argument_restris |
| 145 |
|
| 146 |
def check_operation(self, state, pack, op): |
| 147 |
opnum = op.getopnum() |
| 148 |
descr = op.getdescr() |
| 149 |
if not we_are_translated() and not isinstance(descr, ArrayDescr): |
| 150 |
itemsize = descr.get_item_size_in_bytes() |
| 151 |
ofs = 0 |
| 152 |
else: |
| 153 |
itemsize, ofs, _ = unpack_arraydescr(op.getdescr()) |
| 154 |
args = [op.getarg(0), op.getarg(1), op.getarg(2), ConstInt(1), ConstInt(ofs)] |
| 155 |
if opnum in (rop.SETARRAYITEM_GC, rop.SETARRAYITEM_RAW): |
| 156 |
index_box = op.getarg(1) |
| 157 |
scale, offset, changed, emit = cpu_simplify_scale(state.cpu, index_box, itemsize, ofs) |
| 158 |
args[3] = ConstInt(scale) |
| 159 |
args[4] = ConstInt(offset) |
| 160 |
if emit: |
| 161 |
state.oplist.append(changed) |
| 162 |
args[1] = changed |
| 163 |
return args |
| 164 |
|
| 165 |
def must_crop_vector(self, op, index): |
| 166 |
vecinfo = forwarded_vecinfo(op.getarg(index)) |
| 167 |
bytesize = vecinfo.bytesize |
| 168 |
return self.crop_to_size(op, index) != bytesize |
| 169 |
|
| 170 |
@always_inline |
| 171 |
def crop_to_size(self, op, index): |
| 172 |
|
| 173 |
descr = op.getdescr() |
| 174 |
return descr.get_item_size_in_bytes() |
| 175 |
|
| 176 |
def opcount_filling_vector_register(self, op, vec_reg_size): |
| 177 |
assert rop.is_primitive_store(op.opnum) |
| 178 |
descr = op.getdescr() |
| 179 |
return vec_reg_size // descr.get_item_size_in_bytes() |
| 180 |
|
| 181 |
class OpMatchSizeTypeFirst(OpRestrict): |
| 182 |
def check_operation(self, state, pack, op): |
| 183 |
i = 0 |
| 184 |
infos = [forwarded_vecinfo(o) for o in op.getarglist()] |
| 185 |
arg0 = op.getarg(i) |
| 186 |
while arg0.is_constant() and i < op.numargs(): |
| 187 |
i += 1 |
| 188 |
arg0 = op.getarg(i) |
| 189 |
vecinfo = forwarded_vecinfo(arg0) |
| 190 |
bytesize = vecinfo.bytesize |
| 191 |
datatype = vecinfo.datatype |
| 192 |
|
| 193 |
for arg in op.getarglist(): |
| 194 |
if arg.is_constant(): |
| 195 |
continue |
| 196 |
curvecinfo = forwarded_vecinfo(arg) |
| 197 |
if curvecinfo.bytesize != bytesize: |
| 198 |
debug_print("op match size first type failed") |
| 199 |
raise NotAVectorizeableLoop |
| 200 |
if curvecinfo.datatype != datatype: |
| 201 |
debug_print("op match size first type failed (datatype)") |
| 202 |
raise NotAVectorizeableLoop |
| 203 |
return None |
| 204 |
|
| 205 |
TR_ANY = TypeRestrict() |
| 206 |
TR_ANY_FLOAT = TypeRestrict(FLOAT) |
| 207 |
TR_ANY_INTEGER = TypeRestrict(INT) |
| 208 |
TR_FLOAT_2 = TypeRestrict(FLOAT, 4, 2) |
| 209 |
TR_DOUBLE_2 = TypeRestrict(FLOAT, 8, 2) |
| 210 |
TR_INT32_2 = TypeRestrict(INT, 4, 2) |
| 211 |
TR_INT64_2 = TypeRestrict(INT, 8, 2) |
| 212 |
|
| 213 |
OR_MSTF_I = OpMatchSizeTypeFirst([TR_ANY_INTEGER, TR_ANY_INTEGER]) |
| 214 |
OR_MSTF_F = OpMatchSizeTypeFirst([TR_ANY_FLOAT, TR_ANY_FLOAT]) |
| 215 |
STORE_RESTRICT = StoreRestrict([None, None, TR_ANY]) |
| 216 |
LOAD_RESTRICT = LoadRestrict([]) |
| 217 |
GUARD_RESTRICT = GuardRestrict([TR_ANY_INTEGER]) |
| 218 |
|
| 219 |
|
| 220 |
class VectorExt(object): |
| 221 |
|
| 222 |
should_align_unroll = True |
| 223 |
|
| 224 |
def __init__(self): |
| 225 |
self._enabled = False |
| 226 |
self.register_size = 0 |
| 227 |
self.accum = False |
| 228 |
self._setup = False |
| 229 |
|
| 230 |
def is_setup(self): |
| 231 |
return self._setup |
| 232 |
|
| 233 |
def setup_once(self): |
| 234 |
raise NotImplementedError |
| 235 |
|
| 236 |
def enable(self, vec_size, accum=False): |
| 237 |
self._enabled = vec_size != 0 |
| 238 |
self.register_size = vec_size |
| 239 |
self.accum = accum |
| 240 |
|
| 241 |
def is_enabled(self): |
| 242 |
return self._enabled |
| 243 |
|
| 244 |
def vec_size(self): |
| 245 |
return self.register_size |
| 246 |
|
| 247 |
def supports_accumulation(self): |
| 248 |
return self.accum |
| 249 |
|
| 250 |
|
| 251 |
TR_MAPPING = { |
| 252 |
rop.VEC_INT_ADD: OR_MSTF_I, |
| 253 |
rop.VEC_INT_SUB: OR_MSTF_I, |
| 254 |
rop.VEC_INT_MUL: OR_MSTF_I, |
| 255 |
rop.VEC_INT_AND: OR_MSTF_I, |
| 256 |
rop.VEC_INT_OR: OR_MSTF_I, |
| 257 |
rop.VEC_INT_XOR: OR_MSTF_I, |
| 258 |
rop.VEC_INT_EQ: OR_MSTF_I, |
| 259 |
rop.VEC_INT_NE: OR_MSTF_I, |
| 260 |
|
| 261 |
rop.VEC_FLOAT_ADD: OR_MSTF_F, |
| 262 |
rop.VEC_FLOAT_SUB: OR_MSTF_F, |
| 263 |
rop.VEC_FLOAT_MUL: OR_MSTF_F, |
| 264 |
rop.VEC_FLOAT_TRUEDIV: OR_MSTF_F, |
| 265 |
rop.VEC_FLOAT_ABS: OpRestrict([TR_ANY_FLOAT]), |
| 266 |
rop.VEC_FLOAT_NEG: OpRestrict([TR_ANY_FLOAT]), |
| 267 |
|
| 268 |
rop.VEC_STORE: STORE_RESTRICT, |
| 269 |
|
| 270 |
rop.VEC_LOAD_I: LOAD_RESTRICT, |
| 271 |
rop.VEC_LOAD_F: LOAD_RESTRICT, |
| 272 |
|
| 273 |
rop.VEC_GUARD_TRUE: GUARD_RESTRICT, |
| 274 |
rop.VEC_GUARD_FALSE: GUARD_RESTRICT, |
| 275 |
|
| 276 |
|
| 277 |
rop.VEC_INT_SIGNEXT: OpRestrict([TR_ANY_INTEGER]), |
| 278 |
|
| 279 |
rop.VEC_CAST_FLOAT_TO_SINGLEFLOAT: OpRestrict([TR_DOUBLE_2]), |
| 280 |
|
| 281 |
rop.VEC_CAST_SINGLEFLOAT_TO_FLOAT: OpRestrict([TR_INT32_2]), |
| 282 |
rop.VEC_CAST_FLOAT_TO_INT: OpRestrict([TR_DOUBLE_2]), |
| 283 |
rop.VEC_CAST_INT_TO_FLOAT: OpRestrict([TR_INT32_2]), |
| 284 |
|
| 285 |
rop.VEC_FLOAT_EQ: OpRestrict([TR_ANY_FLOAT,TR_ANY_FLOAT]), |
| 286 |
rop.VEC_FLOAT_NE: OpRestrict([TR_ANY_FLOAT,TR_ANY_FLOAT]), |
| 287 |
rop.VEC_INT_IS_TRUE: OpRestrict([TR_ANY_INTEGER,TR_ANY_INTEGER]), |
| 288 |
} |
| 289 |
|
| 290 |
def get_operation_restriction(self, op): |
| 291 |
res = self.TR_MAPPING.get(op.vector, None) |
| 292 |
if not res: |
| 293 |
failnbail_transformation("could not get OpRestrict for " + str(op)) |
| 294 |
return res |
| 295 |
|