How to use the llvmlite.ir function in llvmlite

To help you get started, we’ve selected a few llvmlite examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github m-labs / artiq / artiq / py2llvm / arrays.py View on Github external
def o_subscript(self, index, builder):
        r = self.el_init.new()
        if builder is not None:
            index = index.o_int(builder).auto_load(builder)
            ssa_r = builder.gep(self.llvm_value, [
                ll.Constant(ll.IntType(32), 0), index])
            r.auto_store(builder, ssa_r)
        return r
github IntelPython / sdc / experimental / daal4py_ext.py View on Github external
def codegen(context, builder, sig, args):
                assert(len(args) == 1)
                c_func_ret_type = lir.IntType(8).as_pointer() if is_array else context.get_data_type(attr_type)
                # First call the getter
                fnty = lir.FunctionType(c_func_ret_type, [lir.IntType(8).as_pointer()])
                fn = builder.module.get_or_insert_function(fnty, name=c_func)
                ptr = builder.call(fn, args)
                return nt2nd(context, builder, ptr, sig.return_type) if is_array else ptr
github numba / numba / numba / targets / numbers.py View on Github external
def hash_float(context, builder, sig, args):
    ty, = sig.args
    retty = sig.return_type
    val, = args

    # NOTE: CPython's algorithm is more involved as it seeks to maintain
    # the invariant that hash(float(x)) == hash(x) for every integer x
    # exactly representable as a float.
    # Numba doesn't care as it doesn't support heterogeneous associative
    # containers.

    intty = types.Integer("int%d" % ty.bitwidth)
    ll_intty = ir.IntType(ty.bitwidth)

    # XXX Disabled as llvm.canonicalize doesn't work:
    # http://lists.llvm.org/pipermail/llvm-dev/2016-February/095746.html
    #func_name = "llvm.canonicalize.f%d" % (ty.bitwidth,)
    #fnty = ir.FunctionType(val.type, (val.type,))
    #fn = builder.module.get_or_insert_function(fnty, func_name)
    #val = builder.call(fn, (val,))

    # Take the float's binary representation as an int
    val_p = cgutils.alloca_once_value(builder, val)
    # y = *(int *)(&val)
    y = builder.load(builder.bitcast(val_p, ll_intty.as_pointer()))

    if intty.bitwidth > retty.bitwidth:
        # Value is wider than hash => fold MSB into LSB
        nbits = intty.bitwidth - retty.bitwidth
github jingle-lang / jingle / source / compiler / jinglec.py View on Github external
def _config_llvm(self):
        # Config LLVM
        self.module = ir.Module(name=__file__)
        self.module.triple = self.binding.get_default_triple()
        func_type = ir.FunctionType(ir.VoidType(), [], False)
        base_func = ir.Function(self.module, func_type, name="main")
        block = base_func.append_basic_block(name="entry")
        self.builder = ir.IRBuilder(block)
github cea-sec / miasm / miasm / jitter / llvmconvert.py View on Github external
unsigned_cmps = {
                "==": "==",
                ">", "<<", "a>>"]:
                assert len(expr.args) == 2
                # Undefined behavior must be enforced to 0
                count = self.add_ir(expr.args[1])
                value = self.add_ir(expr.args[0])
                itype = LLVMType.IntType(expr.size)
                cond_ok = self.builder.icmp_unsigned(
                    "<",
                    count,
                    itype(expr.size)
                )
                zero = itype(0)
github IntelPython / sdc / sdc / str_arr_ext.py View on Github external
def codegen(context, builder, sig, args):
        in_str_arr, ind = args
        string_array = context.make_helper(builder, string_array_type, in_str_arr)

        # bits[i / 8] |= kBitmask[i % 8];
        byte_ind = builder.lshr(ind, lir.Constant(lir.IntType(64), 3))
        bit_ind = builder.urem(ind, lir.Constant(lir.IntType(64), 8))
        byte_ptr = builder.gep(string_array.null_bitmap, [byte_ind], inbounds=True)
        byte = builder.load(byte_ptr)
        ll_typ_mask = lir.ArrayType(lir.IntType(8), 8)
        mask_tup = cgutils.alloca_once_value(builder, lir.Constant(ll_typ_mask, (1, 2, 4, 8, 16, 32, 64, 128)))
        mask = builder.load(builder.gep(mask_tup, [lir.Constant(lir.IntType(64), 0), bit_ind], inbounds=True))
        # flip all bits of mask e.g. 11111101
        mask = builder.xor(mask, lir.Constant(lir.IntType(8), -1))
        # unset masked bit
        builder.store(builder.and_(byte, mask), byte_ptr)
        return context.get_dummy_value()
github numba / numba / numba / cuda / cudadrv / nvvm.py View on Github external
def set_cuda_kernel(lfunc):
    from llvmlite.llvmpy.core import MetaData, MetaDataString, Constant, Type

    m = lfunc.module

    ops = lfunc, MetaDataString.get(m, "kernel"), Constant.int(Type.int(), 1)
    md = MetaData.get(m, ops)

    nmd = m.get_or_insert_named_metadata('nvvm.annotations')
    nmd.add(md)

    # set nvvm ir version
    i32 = ir.IntType(32)
    md_ver = m.add_metadata([i32(1), i32(2), i32(2), i32(0)])
    m.add_named_metadata('nvvmir.version', md_ver)
github APrioriInvestments / typed_python / typed_python / compiler / native_ast_to_llvm.py View on Github external
nt = native_ast.Type.Int(bits=8, signed=False).pointer()

        return TypedLLVMValue(
            builder.bitcast(value, llvm_i8ptr),
            nt
        )

    if c.matches.Void:
        return TypedLLVMValue(None, native_ast.Type.Void())

    if c.matches.Array:
        vals = [constant_to_typed_llvm_value(module, builder, t) for _, t in c.values]

        t = llvmlite.ir.ArrayType(type_to_llvm_type(c.value_type.element_type), c.value_type.count)

        llvm_c = llvmlite.ir.Constant(t, [t.llvm_value for t in vals])

        return TypedLLVMValue(llvm_c, c.value_type)

    assert False, (c, type(c))
github numba / numba / numba / datamodel / models.py View on Github external
def __init__(self, dmm, fe_type):
        sig = fe_type.sig
        # Since the function is non-Numba, there is no adaptation
        # of arguments and return value, hence get_value_type().
        retty = dmm.lookup(sig.return_type).get_value_type()
        args = [dmm.lookup(t).get_value_type() for t in sig.args]
        be_type = ir.PointerType(ir.FunctionType(retty, args))
        super(ExternalFuncPointerModel, self).__init__(dmm, fe_type, be_type)
github numba / numba / numba / datamodel / models.py View on Github external
def __init__(self, dmm, fe_type):
        super(CharSeq, self).__init__(dmm, fe_type)
        charty = ir.IntType(8)
        self._be_type = ir.ArrayType(charty, fe_type.count)