def make_constant_array(self, builder, typ, ary): assert typ.layout == 'C' # assumed in typeinfer.py ary = numpy.ascontiguousarray(ary) flat = ary.flatten() # Handle data if self.is_struct_type(typ.dtype): values = [ self.get_constant_struct(builder, typ.dtype, flat[i]) for i in range(flat.size) ] else: values = [ self.get_constant(typ.dtype, flat[i]) for i in range(flat.size) ] lldtype = values[0].type consts = Constant.array(lldtype, values) data = cgutils.global_constant(builder, ".const.array.data", consts) # Handle shape llintp = self.get_value_type(types.intp) shapevals = [self.get_constant(types.intp, s) for s in ary.shape] cshape = Constant.array(llintp, shapevals) # Handle strides stridevals = [self.get_constant(types.intp, s) for s in ary.strides] cstrides = Constant.array(llintp, stridevals) # Create array structure cary = self.make_array(typ)(self, builder) cary.data = builder.bitcast(data, cary.data.type) cary.shape = cshape cary.strides = cstrides return cary._getvalue()
def make_constant_array(self, builder, typ, ary): assert typ.layout == "C" # assumed in typeinfer.py ary = numpy.ascontiguousarray(ary) flat = ary.flatten() # Handle data if self.is_struct_type(typ.dtype): values = [self.get_constant_struct(builder, typ.dtype, flat[i]) for i in range(flat.size)] else: values = [self.get_constant(typ.dtype, flat[i]) for i in range(flat.size)] lldtype = values[0].type consts = Constant.array(lldtype, values) data = cgutils.global_constant(builder, ".const.array.data", consts) # Handle shape llintp = self.get_value_type(types.intp) shapevals = [self.get_constant(types.intp, s) for s in ary.shape] cshape = Constant.array(llintp, shapevals) # Handle strides stridevals = [self.get_constant(types.intp, s) for s in ary.strides] cstrides = Constant.array(llintp, stridevals) # Create array structure cary = self.make_array(typ)(self, builder) cary.data = builder.bitcast(data, cary.data.type) cary.shape = cshape cary.strides = cstrides return cary._getvalue()
def make_constant_array(self, builder, typ, ary): """ Create an array structure reifying the given constant array. A low-level contiguous array constant is created in the LLVM IR. """ datatype = self.get_data_type(typ.dtype) # don't freeze ary of non-contig or bigger than 1MB size_limit = 10**6 if self.allow_dynamic_globals and (typ.layout not in "FC" or ary.nbytes > size_limit): # get pointer from the ary dataptr = ary.ctypes.data data = self.add_dynamic_addr(builder, dataptr, info=str(type(dataptr))) rt_addr = self.add_dynamic_addr(builder, id(ary), info=str(type(ary))) else: # Handle data: reify the flattened array in "C" or "F" order as a # global array of bytes. flat = ary.flatten(order=typ.layout) # Note: we use `bytearray(flat.data)` instead of `bytearray(flat)` to # workaround issue #1850 which is due to numpy issue #3147 consts = Constant.array(Type.int(8), bytearray(flat.data)) data = cgutils.global_constant(builder, ".const.array.data", consts) # Ensure correct data alignment (issue #1933) data.align = self.get_abi_alignment(datatype) # No reference to parent ndarray rt_addr = None # Handle shape llintp = self.get_value_type(types.intp) shapevals = [self.get_constant(types.intp, s) for s in ary.shape] cshape = Constant.array(llintp, shapevals) # Handle strides stridevals = [self.get_constant(types.intp, s) for s in ary.strides] cstrides = Constant.array(llintp, stridevals) # Create array structure cary = self.make_array(typ)(self, builder) intp_itemsize = self.get_constant(types.intp, ary.dtype.itemsize) self.populate_array( cary, data=builder.bitcast(data, cary.data.type), shape=cshape, strides=cstrides, itemsize=intp_itemsize, parent=rt_addr, meminfo=None, ) return cary._getvalue()
def make_constant_array(self, builder, typ, ary): """ Create an array structure reifying the given constant array. A low-level contiguous array constant is created in the LLVM IR. """ assert typ.layout == 'C' # assumed in typeinfer.py datatype = self.get_data_type(typ.dtype) # Handle data: reify the flattened array in "C" order as a # global array of bytes. flat = ary.flatten() # Note: we use `bytearray(flat.data)` instead of `bytearray(flat)` to # workaround issue #1850 which is due to numpy issue #3147 consts = Constant.array(Type.int(8), bytearray(flat.data)) data = cgutils.global_constant(builder, ".const.array.data", consts) # Ensure correct data alignment (issue #1933) data.align = self.get_abi_alignment(datatype) # Handle shape llintp = self.get_value_type(types.intp) shapevals = [self.get_constant(types.intp, s) for s in ary.shape] cshape = Constant.array(llintp, shapevals) # Handle strides if ary.ndim > 0: # Use strides of the equivalent C-contiguous array. contig = np.ascontiguousarray(ary) stridevals = [ self.get_constant(types.intp, s) for s in contig.strides ] else: stridevals = [] cstrides = Constant.array(llintp, stridevals) # Create array structure cary = self.make_array(typ)(self, builder) rt_addr = self.get_constant(types.uintp, id(ary)).inttoptr( self.get_value_type(types.pyobject)) intp_itemsize = self.get_constant(types.intp, ary.dtype.itemsize) self.populate_array(cary, data=builder.bitcast(data, cary.data.type), shape=cshape, strides=cstrides, itemsize=intp_itemsize, parent=rt_addr, meminfo=None) return cary._getvalue()
def make_constant_array(self, builder, typ, ary): """ Create an array structure reifying the given constant array. A low-level contiguous array constant is created in the LLVM IR. """ datatype = self.get_data_type(typ.dtype) # don't freeze ary of non-contig or bigger than 1MB size_limit = 10**6 if (self.allow_dynamic_globals and (typ.layout not in 'FC' or ary.nbytes > size_limit)): # get pointer from the ary dataptr = ary.ctypes.data data = self.add_dynamic_addr(builder, dataptr, info=str(type(dataptr))) rt_addr = self.add_dynamic_addr(builder, id(ary), info=str(type(ary))) else: # Handle data: reify the flattened array in "C" or "F" order as a # global array of bytes. flat = ary.flatten(order=typ.layout) # Note: we use `bytearray(flat.data)` instead of `bytearray(flat)` to # workaround issue #1850 which is due to numpy issue #3147 consts = Constant.array(Type.int(8), bytearray(flat.data)) data = cgutils.global_constant(builder, ".const.array.data", consts) # Ensure correct data alignment (issue #1933) data.align = self.get_abi_alignment(datatype) # No reference to parent ndarray rt_addr = None # Handle shape llintp = self.get_value_type(types.intp) shapevals = [self.get_constant(types.intp, s) for s in ary.shape] cshape = Constant.array(llintp, shapevals) # Handle strides stridevals = [self.get_constant(types.intp, s) for s in ary.strides] cstrides = Constant.array(llintp, stridevals) # Create array structure cary = self.make_array(typ)(self, builder) intp_itemsize = self.get_constant(types.intp, ary.dtype.itemsize) self.populate_array(cary, data=builder.bitcast(data, cary.data.type), shape=cshape, strides=cstrides, itemsize=intp_itemsize, parent=rt_addr, meminfo=None) return cary._getvalue()
def make_constant_array(self, builder, typ, ary): """ Create an array structure reifying the given constant array. A low-level contiguous array constant is created in the LLVM IR. """ assert typ.layout == 'C' # assumed in typeinfer.py datatype = self.get_data_type(typ.dtype) # Handle data: reify the flattened array in "C" order as a # global array of bytes. flat = ary.flatten() # Note: we use `bytearray(flat.data)` instead of `bytearray(flat)` to # workaround issue #1850 which is due to numpy issue #3147 consts = Constant.array(Type.int(8), bytearray(flat.data)) data = cgutils.global_constant(builder, ".const.array.data", consts) # Ensure correct data alignment (issue #1933) data.align = self.get_abi_alignment(datatype) # Handle shape llintp = self.get_value_type(types.intp) shapevals = [self.get_constant(types.intp, s) for s in ary.shape] cshape = Constant.array(llintp, shapevals) # Handle strides if ary.ndim > 0: # Use strides of the equivalent C-contiguous array. contig = np.ascontiguousarray(ary) stridevals = [self.get_constant(types.intp, s) for s in contig.strides] else: stridevals = [] cstrides = Constant.array(llintp, stridevals) # Create array structure cary = self.make_array(typ)(self, builder) rt_addr = self.get_constant(types.uintp, id(ary)).inttoptr( self.get_value_type(types.pyobject)) intp_itemsize = self.get_constant(types.intp, ary.dtype.itemsize) self.populate_array(cary, data=builder.bitcast(data, cary.data.type), shape=cshape, strides=cstrides, itemsize=intp_itemsize, parent=rt_addr, meminfo=None) return cary._getvalue()
def get_constant(self, ty, val): assert not self.is_struct_type(ty) lty = self.get_value_type(ty) if ty == types.none: assert val is None return self.get_dummy_value() elif ty == types.boolean: return Constant.int(Type.int(1), int(val)) elif ty in types.signed_domain: return Constant.int_signextend(lty, val) elif ty in types.unsigned_domain: return Constant.int(lty, val) elif ty in types.real_domain: return Constant.real(lty, val) elif isinstance(ty, (types.NPDatetime, types.NPTimedelta)): return Constant.real(lty, val.astype(numpy.int64)) elif isinstance(ty, (types.UniTuple, types.NamedUniTuple)): consts = [self.get_constant(ty.dtype, v) for v in val] return Constant.array(consts[0].type, consts) raise NotImplementedError("cannot lower constant of type '%s'" % (ty, ))
def get_constant_struct(self, builder, ty, val): assert self.is_struct_type(ty) if ty in types.complex_domain: if ty == types.complex64: innertype = types.float32 elif ty == types.complex128: innertype = types.float64 else: raise Exception("unreachable") real = self.get_constant(innertype, val.real) imag = self.get_constant(innertype, val.imag) const = Constant.struct([real, imag]) return const elif isinstance(ty, types.Tuple): consts = [self.get_constant_generic(builder, ty.types[i], v) for i, v in enumerate(val)] return Constant.struct(consts) elif isinstance(ty, types.Record): consts = [self.get_constant(types.int8, b) for b in bytearray(val.tostring())] return Constant.array(consts[0].type, consts) else: raise NotImplementedError("%s as constant unsupported" % ty)
def get_constant(self, ty, val): assert not self.is_struct_type(ty) lty = self.get_value_type(ty) if ty == types.none: assert val is None return self.get_dummy_value() elif ty == types.boolean: return Constant.int(Type.int(1), int(val)) elif ty in types.signed_domain: return Constant.int_signextend(lty, val) elif ty in types.unsigned_domain: return Constant.int(lty, val) elif ty in types.real_domain: return Constant.real(lty, val) elif isinstance(ty, types.UniTuple): consts = [self.get_constant(ty.dtype, v) for v in val] return Constant.array(consts[0].type, consts) raise NotImplementedError(ty)
def get_constant_struct(self, builder, ty, val): assert self.is_struct_type(ty) module = cgutils.get_module(builder) if ty in types.complex_domain: if ty == types.complex64: innertype = types.float32 elif ty == types.complex128: innertype = types.float64 else: raise Exception("unreachable") real = self.get_constant(innertype, val.real) imag = self.get_constant(innertype, val.imag) const = Constant.struct([real, imag]) return const elif isinstance(ty, types.Tuple): consts = [ self.get_constant_generic(builder, ty.types[i], v) for i, v in enumerate(val) ] return Constant.struct(consts) elif isinstance(ty, types.Record): consts = [ self.get_constant(types.int8, b) for b in bytearray(val.tostring()) ] return Constant.array(consts[0].type, consts) else: raise NotImplementedError("%s as constant unsupported" % ty)
def get_constant(self, ty, val): assert not self.is_struct_type(ty) lty = self.get_value_type(ty) if ty == types.none: assert val is None return self.get_dummy_value() elif ty == types.boolean: return Constant.int(Type.int(1), int(val)) elif ty in types.signed_domain: return Constant.int_signextend(lty, val) elif ty in types.unsigned_domain: return Constant.int(lty, val) elif ty in types.real_domain: return Constant.real(lty, val) elif isinstance(ty, types.UniTuple): consts = [self.get_constant(ty.dtype, v) for v in val] return Constant.array(consts[0].type, consts) raise NotImplementedError("cannot lower constant of type '%s'" % (ty,))
def make_constant_array(self, builder, typ, ary): assert typ.layout == 'C' # assumed in typeinfer.py ary = numpy.ascontiguousarray(ary) flat = ary.flatten() # Handle data if self.is_struct_type(typ.dtype): values = [ self.get_constant_struct(builder, typ.dtype, flat[i]) for i in range(flat.size) ] else: values = [ self.get_constant(typ.dtype, flat[i]) for i in range(flat.size) ] lldtype = values[0].type consts = Constant.array(lldtype, values) data = cgutils.global_constant(builder, ".const.array.data", consts) # Handle shape llintp = self.get_value_type(types.intp) shapevals = [self.get_constant(types.intp, s) for s in ary.shape] cshape = Constant.array(llintp, shapevals) # Handle strides stridevals = [self.get_constant(types.intp, s) for s in ary.strides] cstrides = Constant.array(llintp, stridevals) # Create array structure cary = self.make_array(typ)(self, builder) rt_addr = self.get_constant(types.uintp, id(ary)).inttoptr( self.get_value_type(types.pyobject)) intp_itemsize = self.get_constant(types.intp, ary.dtype.itemsize) self.populate_array(cary, data=builder.bitcast(data, cary.data.type), shape=cshape, strides=cstrides, itemsize=intp_itemsize, parent=rt_addr, meminfo=None) return cary._getvalue()
def make_keywords(self, kws): strings = [] stringtype = Type.pointer(Type.int(8)) for k in kws: strings.append(self.make_const_string(k)) strings.append(Constant.null(stringtype)) kwlist = Constant.array(stringtype, strings) kwlist = cgutils.global_constant(self.module, ".kwlist", kwlist) return Constant.bitcast(kwlist, Type.pointer(stringtype))
def make_constant_array(self, builder, typ, ary): assert typ.layout == 'C' # assumed in typeinfer.py ary = numpy.ascontiguousarray(ary) flat = ary.flatten() # Handle data if self.is_struct_type(typ.dtype): values = [self.get_constant_struct(builder, typ.dtype, flat[i]) for i in range(flat.size)] else: values = [self.get_constant(typ.dtype, flat[i]) for i in range(flat.size)] lldtype = values[0].type consts = Constant.array(lldtype, values) data = cgutils.global_constant(builder, ".const.array.data", consts) # Handle shape llintp = self.get_value_type(types.intp) shapevals = [self.get_constant(types.intp, s) for s in ary.shape] cshape = Constant.array(llintp, shapevals) # Handle strides stridevals = [self.get_constant(types.intp, s) for s in ary.strides] cstrides = Constant.array(llintp, stridevals) # Create array structure cary = self.make_array(typ)(self, builder) rt_addr = self.get_constant(types.uintp, id(ary)).inttoptr( self.get_value_type(types.pyobject)) intp_itemsize = self.get_constant(types.intp, ary.dtype.itemsize) self.populate_array(cary, data=builder.bitcast(data, cary.data.type), shape=cshape, strides=cstrides, itemsize=intp_itemsize, parent=rt_addr, meminfo=None) return cary._getvalue()
def make_constant_array(self, builder, typ, ary): """ Create an array structure reifying the given constant array. A low-level contiguous array constant is created in the LLVM IR. """ assert typ.layout == 'C' # assumed in typeinfer.py # Handle data: reify the flattened array in "C" order as a # global array of bytes. flat = ary.flatten() consts = Constant.array(Type.int(8), bytearray(flat)) data = cgutils.global_constant(builder, ".const.array.data", consts) # Handle shape llintp = self.get_value_type(types.intp) shapevals = [self.get_constant(types.intp, s) for s in ary.shape] cshape = Constant.array(llintp, shapevals) # Handle strides: use strides of the equivalent C-contiguous array. contig = numpy.ascontiguousarray(ary) stridevals = [self.get_constant(types.intp, s) for s in contig.strides] cstrides = Constant.array(llintp, stridevals) # Create array structure cary = self.make_array(typ)(self, builder) rt_addr = self.get_constant(types.uintp, id(ary)).inttoptr( self.get_value_type(types.pyobject)) intp_itemsize = self.get_constant(types.intp, ary.dtype.itemsize) self.populate_array(cary, data=builder.bitcast(data, cary.data.type), shape=cshape, strides=cstrides, itemsize=intp_itemsize, parent=rt_addr, meminfo=None) return cary._getvalue()
def make_constant_array(vals): consts = [Constant.int(TIMEDELTA64, v) for v in vals] return Constant.array(TIMEDELTA64, consts)