Exemplo n.º 1
0
def build_full_slice_tuple(tyctx, sz):
    """Creates a sz-tuple of full slices."""
    if not isinstance(sz, types.IntegerLiteral):
        raise errors.RequireLiteralValue(sz)

    size = int(sz.literal_value)
    tuple_type = types.UniTuple(dtype=types.slice2_type, count=size)
    sig = tuple_type(sz)

    def codegen(context, builder, signature, args):
        def impl(length, empty_tuple):
            out = empty_tuple
            for i in range(length):
                out = tuple_setitem(out, i, slice(None, None))
            return out

        inner_argtypes = [types.intp, tuple_type]
        inner_sig = typing.signature(tuple_type, *inner_argtypes)
        ll_idx_type = context.get_value_type(types.intp)
        # Allocate an empty tuple
        empty_tuple = context.get_constant_undef(tuple_type)
        inner_args = [ll_idx_type(size), empty_tuple]

        res = context.compile_internal(builder, impl, inner_sig, inner_args)
        return res

    return sig, codegen
Exemplo n.º 2
0
 def typer(ndim):
     if not isinstance(ndim, types.IntegerLiteral):
         raise errors.RequireLiteralValue(ndim)
     val = ndim.literal_value
     if val == 1:
         restype = types.int32
     elif val in (2, 3):
         restype = types.UniTuple(types.int32, val)
     else:
         raise ValueError('argument can only be 1, 2, 3')
     return signature(restype, types.int32)
Exemplo n.º 3
0
        def myintrin(typingctx, x, arr):
            if not isinstance(x, types.IntegerLiteral):
                raise errors.RequireLiteralValue(given_reason1)

            if arr.ndim != 1:
                raise ValueError(given_reason2)

            sig = types.intp(x, arr)

            def codegen(context, builder, signature, args):
                pass
            return sig, codegen
Exemplo n.º 4
0
def init_gdb_codegen(cgctx,
                     builder,
                     signature,
                     args,
                     const_args,
                     do_break=False):

    int8_t = ir.IntType(8)
    int32_t = ir.IntType(32)
    intp_t = ir.IntType(utils.MACHINE_BITS)
    char_ptr = ir.PointerType(ir.IntType(8))
    zero_i32t = int32_t(0)

    mod = builder.module
    pid = cgutils.alloca_once(builder, int32_t, size=1)

    # 32bit pid, 11 char max + terminator
    pidstr = cgutils.alloca_once(builder, int8_t, size=12)

    # str consts
    intfmt = cgctx.insert_const_string(mod, '%d')
    gdb_str = cgctx.insert_const_string(mod, config.GDB_BINARY)
    attach_str = cgctx.insert_const_string(mod, 'attach')

    new_args = []
    # add break point command to known location
    # this command file thing is due to commands attached to a breakpoint
    # requiring an interactive prompt
    # https://sourceware.org/bugzilla/show_bug.cgi?id=10079
    new_args.extend(['-x', os.path.join(_path, 'cmdlang.gdb')])
    # issue command to continue execution from sleep function
    new_args.extend(['-ex', 'c'])
    # then run the user defined args if any
    if any([not isinstance(x, types.StringLiteral) for x in const_args]):
        raise errors.RequireLiteralValue(const_args)
    new_args.extend([x.literal_value for x in const_args])
    cmdlang = [cgctx.insert_const_string(mod, x) for x in new_args]

    # insert getpid, getpid is always successful, call without concern!
    fnty = ir.FunctionType(int32_t, tuple())
    getpid = cgutils.get_or_insert_function(mod, fnty, "getpid")

    # insert snprintf
    # int snprintf(char *str, size_t size, const char *format, ...);
    fnty = ir.FunctionType(int32_t, (char_ptr, intp_t, char_ptr), var_arg=True)
    snprintf = cgutils.get_or_insert_function(mod, fnty, "snprintf")

    # insert fork
    fnty = ir.FunctionType(int32_t, tuple())
    fork = cgutils.get_or_insert_function(mod, fnty, "fork")

    # insert execl
    fnty = ir.FunctionType(int32_t, (char_ptr, char_ptr), var_arg=True)
    execl = cgutils.get_or_insert_function(mod, fnty, "execl")

    # insert sleep
    fnty = ir.FunctionType(int32_t, (int32_t, ))
    sleep = cgutils.get_or_insert_function(mod, fnty, "sleep")

    # insert break point
    fnty = ir.FunctionType(ir.VoidType(), tuple())
    breakpoint = cgutils.get_or_insert_function(mod, fnty,
                                                "numba_gdb_breakpoint")

    # do the work
    parent_pid = builder.call(getpid, tuple())
    builder.store(parent_pid, pid)
    pidstr_ptr = builder.gep(pidstr, [zero_i32t], inbounds=True)
    pid_val = builder.load(pid)

    # call snprintf to write the pid into a char *
    stat = builder.call(snprintf, (pidstr_ptr, intp_t(12), intfmt, pid_val))
    invalid_write = builder.icmp_signed('>', stat, int32_t(12))
    with builder.if_then(invalid_write, likely=False):
        msg = "Internal error: `snprintf` buffer would have overflowed."
        cgctx.call_conv.return_user_exc(builder, RuntimeError, (msg, ))

    # fork, check pids etc
    child_pid = builder.call(fork, tuple())
    fork_failed = builder.icmp_signed('==', child_pid, int32_t(-1))
    with builder.if_then(fork_failed, likely=False):
        msg = "Internal error: `fork` failed."
        cgctx.call_conv.return_user_exc(builder, RuntimeError, (msg, ))

    is_child = builder.icmp_signed('==', child_pid, zero_i32t)
    with builder.if_else(is_child) as (then, orelse):
        with then:
            # is child
            nullptr = ir.Constant(char_ptr, None)
            gdb_str_ptr = builder.gep(gdb_str, [zero_i32t], inbounds=True)
            attach_str_ptr = builder.gep(attach_str, [zero_i32t],
                                         inbounds=True)
            cgutils.printf(builder, "Attaching to PID: %s\n", pidstr)
            buf = (gdb_str_ptr, gdb_str_ptr, attach_str_ptr, pidstr_ptr)
            buf = buf + tuple(cmdlang) + (nullptr, )
            builder.call(execl, buf)
        with orelse:
            # is parent
            builder.call(sleep, (int32_t(10), ))
            # if breaking is desired, break now
            if do_break is True:
                builder.call(breakpoint, tuple())