Example #1
0
    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()
Example #2
0
 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
Example #3
0
 def codegen(context, builder, sig, args):
     int8ptr = ir.PointerType(ir.IntType(8))
     fntype = ir.FunctionType(ir.IntType(32), [int8ptr])
     fn = irutils.get_or_insert_function(builder.module, fntype,
                                         name="table_function_error")
     assert fn.is_declaration
     #
     msg_bytes = message.literal_value.encode('utf-8')
     msg_const = make_bytearray(msg_bytes + b'\0')
     msg_global_var = global_constant(builder.module, "table_function_error_message",
                                      msg_const)
     msg_ptr = builder.bitcast(msg_global_var, int8ptr)
     return builder.call(fn, [msg_ptr])
    def codegen(context, builder, signature, args):
        mgr_ptr = args[0]

        mgr_i8ptr = builder.bitcast(mgr_ptr, i8p)

        msg_bytes = msg.literal_value.encode('utf-8')
        msg_const = make_bytearray(msg_bytes + b'\0')
        msg_global_var = global_constant(
            builder.module,
            "table_function_manager_error_message",
            msg_const)
        msg_ptr = builder.bitcast(msg_global_var, i8p)

        fnty = ir.FunctionType(i32, [i8p, i8p])
        fn = irutils.get_or_insert_function(
            builder.module, fnty, "TableFunctionManager_error_message")

        return builder.call(fn, [mgr_i8ptr, msg_ptr])