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): # FIXME raise TypeError("Do not support structure dtype as constant " "array, yet.") 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 reduce_datetime_for_unit(builder, dt_val, src_unit, dest_unit): dest_unit_code = npdatetime.DATETIME_UNITS[dest_unit] src_unit_code = npdatetime.DATETIME_UNITS[src_unit] if dest_unit_code < 2 or src_unit_code >= 2: return dt_val, src_unit # Need to compute the day ordinal for *dt_val* if src_unit_code == 0: # Years to days year_val = dt_val days_val = year_to_days(builder, year_val) else: # Months to days leap_array = cgutils.global_constant(builder, "leap_year_months_acc", leap_year_months_acc) normal_array = cgutils.global_constant(builder, "normal_year_months_acc", normal_year_months_acc) days = cgutils.alloca_once(builder, TIMEDELTA64) # First compute year number and month number year, month = cgutils.divmod_by_constant(builder, dt_val, 12) # Then deduce the number of days with builder.if_else(is_leap_year(builder, year)) as (then, otherwise): with then: addend = builder.load( cgutils.gep(builder, leap_array, 0, month, inbounds=True)) builder.store(addend, days) with otherwise: addend = builder.load( cgutils.gep(builder, normal_array, 0, month, inbounds=True)) builder.store(addend, days) days_val = year_to_days(builder, year) days_val = builder.add(days_val, builder.load(days)) if dest_unit_code == 2: # Need to scale back to weeks weeks, _ = cgutils.divmod_by_constant(builder, days_val, 7) return weeks, 'W' else: return days_val, 'D'
def reduce_datetime_for_unit(builder, dt_val, src_unit, dest_unit): dest_unit_code = npdatetime.DATETIME_UNITS[dest_unit] src_unit_code = npdatetime.DATETIME_UNITS[src_unit] if dest_unit_code < 2 or src_unit_code >= 2: return dt_val, src_unit # Need to compute the day ordinal for *dt_val* if src_unit_code == 0: # Years to days year_val = dt_val days_val = year_to_days(builder, year_val) else: # Months to days leap_array = cgutils.global_constant(builder, "leap_year_months_acc", leap_year_months_acc) normal_array = cgutils.global_constant(builder, "normal_year_months_acc", normal_year_months_acc) days = cgutils.alloca_once(builder, TIMEDELTA64) # First compute year number and month number year, month = cgutils.divmod_by_constant(builder, dt_val, 12) # Then deduce the number of days with cgutils.ifelse(builder, is_leap_year(builder, year)) as (then, otherwise): with then: addend = builder.load(cgutils.gep(builder, leap_array, 0, month)) builder.store(addend, days) with otherwise: addend = builder.load(cgutils.gep(builder, normal_array, 0, month)) builder.store(addend, days) days_val = year_to_days(builder, year) days_val = builder.add(days_val, builder.load(days)) if dest_unit_code == 2: # Need to scale back to weeks weeks, _ = cgutils.divmod_by_constant(builder, days_val, 7) return weeks, 'W' else: return days_val, 'D'
def insert_unique_const(self, mod, name, val): """ Insert a unique internal constant named *name*, with LLVM value *val*, into module *mod*. """ gv = mod.get_global(name) if gv is not None: return gv else: return cgutils.global_constant(mod, name, val)
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 insert_const_string(self, mod, string): stringtype = GENERIC_POINTER text = Constant.stringz(string) name = ".const.%s" % string for gv in mod.global_variables: if gv.name == name and gv.type.pointee == text.type: break else: gv = cgutils.global_constant(mod, name, text) return Constant.bitcast(gv, stringtype)
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 insert_unique_const(self, mod, name, val): """ Insert a unique internal constant named *name*, with LLVM value *val*, into module *mod*. """ try: gv = mod.get_global(name) except KeyError: return cgutils.global_constant(mod, name, val) else: return gv
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. """ 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): 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): 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()