How to use the numba.ir.Var function in numba

To help you get started, we’ve selected a few numba 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 IntelPython / sdc / sdc / hiframes / hiframes_typed.py View on Github external
def default_handler(index, param, default):
                d_var = ir.Var(scope, mk_unique_var('defaults'), loc)
                self.state.typemap[d_var.name] = numba.typeof(default)
                node = ir.Assign(ir.Const(default, loc), d_var, loc)
                pre_nodes.append(node)
                return d_var
github numba / numba / numba / parfor.py View on Github external
inline_closure_call(self.func_ir, g,
                                            block, i, new_func, self.typingctx, typs,
                                            self.typemap, self.calltypes, work_list)
                            return True
                        if guard(replace_func):
                            break
                    elif (isinstance(expr, ir.Expr) and expr.op == 'getattr' and
                          expr.attr == 'dtype'):
                        # Replace getattr call "A.dtype" with the actual type itself.
                        # This helps remove superfulous dependencies from parfor.
                        typ = self.typemap[expr.value.name]
                        if isinstance(typ, types.npytypes.Array):
                            dtype = typ.dtype
                            scope = block.scope
                            loc = instr.loc
                            g_np_var = ir.Var(scope, mk_unique_var("$np_g_var"), loc)
                            self.typemap[g_np_var.name] = types.misc.Module(numpy)
                            g_np = ir.Global('np', numpy, loc)
                            g_np_assign = ir.Assign(g_np, g_np_var, loc)
                            typ_var = ir.Var(scope, mk_unique_var("$np_typ_var"), loc)
                            self.typemap[typ_var.name] = types.DType(dtype)
                            dtype_str = str(dtype)
                            if dtype_str == 'bool':
                                dtype_str = 'bool_'
                            np_typ_getattr = ir.Expr.getattr(g_np_var, dtype_str, loc)
                            typ_var_assign = ir.Assign(np_typ_getattr, typ_var, loc)
                            instr.value = typ_var
                            block.body.insert(0, typ_var_assign)
                            block.body.insert(0, g_np_assign)
                            break
github numba / numba / numba / parfor.py View on Github external
# X_val = getitem(X, tuple_var)
    X_val = ir.Var(scope, mk_unique_var("$" + in1.name + "_val"), loc)
    typemap[X_val.name] = el_typ
    getitem_call = ir.Expr.getitem(in1, tuple_var, loc)
    calltypes[getitem_call] = signature(el_typ, typemap[in1.name],
                                        typemap[tuple_var.name])
    getitem_assign = ir.Assign(getitem_call, X_val, loc)
    # v_val = getitem(V, inner_index)
    v_val = ir.Var(scope, mk_unique_var("$" + in2.name + "_val"), loc)
    typemap[v_val.name] = el_typ
    v_getitem_call = ir.Expr.getitem(in2, inner_index, loc)
    calltypes[v_getitem_call] = signature(
        el_typ, typemap[in2.name], types.intp)
    v_getitem_assign = ir.Assign(v_getitem_call, v_val, loc)
    # add_var = X_val * v_val
    add_var = ir.Var(scope, mk_unique_var("$add_var"), loc)
    typemap[add_var.name] = el_typ
    add_call = ir.Expr.binop('*', X_val, v_val, loc)
    calltypes[add_call] = signature(el_typ, el_typ, el_typ)
    add_assign = ir.Assign(add_call, add_var, loc)
    # acc_var = sum_var + add_var
    acc_var = ir.Var(scope, mk_unique_var("$acc_var"), loc)
    typemap[acc_var.name] = el_typ
    acc_call = ir.Expr.inplace_binop('+=', '+', sum_var, add_var, loc)
    calltypes[acc_call] = signature(el_typ, el_typ, el_typ)
    acc_assign = ir.Assign(acc_call, acc_var, loc)
    # sum_var = acc_var
    final_assign = ir.Assign(acc_var, sum_var, loc)
    # jump to header
    b_jump_header = ir.Jump(-1, loc)
    body_block.body = [
        inner_index_assign,
github IntelPython / sdc / sdc / hiframes / hiframes_typed.py View on Github external
"""creates an index list and passes it to a Sort node as data
        """
        # get data array
        nodes = []
        data = self._get_series_data(series_var, nodes)

        # get index array
        if self.state.typemap[series_var.name].index != types.none:
            index_var = self._get_series_index(series_var, nodes)
        else:
            index_var = self._get_index_values(data, nodes)

        # output data arrays for results, before conversion to Series
        out_data = ir.Var(lhs.scope, mk_unique_var(lhs.name + '_data'), lhs.loc)
        self.state.typemap[out_data.name] = self.state.typemap[lhs.name].data
        out_index = ir.Var(lhs.scope, mk_unique_var(lhs.name + '_index'), lhs.loc)
        self.state.typemap[out_index.name] = self.state.typemap[index_var.name]

        # indexes are input/output Sort data
        in_df = {'inds': index_var}
        out_df = {'inds': out_index}
        # data arrays are Sort key
        in_keys = [data]
        out_keys = [out_data]
        args = [out_data, out_index]

        if is_argsort:
            # output of argsort doesn't have new index so assign None
            none_index = ir.Var(lhs.scope, mk_unique_var(lhs.name + '_index'), lhs.loc)
            self.state.typemap[none_index.name] = types.none
            nodes.append(ir.Assign(
                ir.Const(None, lhs.loc), none_index, lhs.loc))
github IntelPython / sdc / sdc / hiframes / hiframes_typed.py View on Github external
# S3_data = S1_data + S2_data; S3 = init_series(S3_data)
        if isinstance(typ1, SeriesType):
            arg1 = self._get_series_data(arg1, nodes)
        if isinstance(typ2, SeriesType):
            arg2 = self._get_series_data(arg2, nodes)

        rhs.lhs, rhs.rhs = arg1, arg2
        self._convert_series_calltype(rhs)

        # output stays as Array in A += B where A is Array
        if isinstance(self.state.typemap[assign.target.name], types.Array):
            assert isinstance(self.state.calltypes[rhs].return_type, types.Array)
            nodes.append(assign)
            return nodes

        out_data = ir.Var(
            arg1.scope, mk_unique_var(assign.target.name + '_data'), rhs.loc)
        self.state.typemap[out_data.name] = self.state.calltypes[rhs].return_type
        nodes.append(ir.Assign(rhs, out_data, rhs.loc))
        return self._replace_func(
            lambda data: sdc.hiframes.api.init_series(data, None, None),
            [out_data],
            pre_nodes=nodes
        )
github numba / numba / numba / parfor.py View on Github external
const_assign = ir.Assign(const_node, const_var, loc)
        out_ir.append(const_assign)
        index_var = const_var
    elif ndims == 1:
        # Use last index for 1D arrays
        index_var = all_parfor_indices[-1]
    elif any([x != None for x in size_consts]):
        # Need a tuple as index
        ind_offset = num_indices - ndims
        tuple_var = ir.Var(var.scope, mk_unique_var(
            "$parfor_index_tuple_var_bcast"), loc)
        typemap[tuple_var.name] = types.containers.UniTuple(types.uintp, ndims)
        # Just in case, const var for size 1 dim access index: $const0 =
        # Const(0)
        const_node = ir.Const(0, var.loc)
        const_var = ir.Var(var.scope, mk_unique_var("$const_ind_0"), loc)
        typemap[const_var.name] = types.uintp
        const_assign = ir.Assign(const_node, const_var, loc)
        out_ir.append(const_assign)
        index_vars = []
        for i in reversed(range(ndims)):
            size_var = size_vars[i]
            size_const = size_consts[i]
            if size_const == 1:
                index_vars.append(const_var)
            else:
                index_vars.append(all_parfor_indices[ind_offset + i])
        index_vars = list(reversed(index_vars))
        tuple_call = ir.Expr.build_tuple(index_vars, loc)
        tuple_assign = ir.Assign(tuple_call, tuple_var, loc)
        out_ir.append(tuple_assign)
        index_var = tuple_var
github numba / numba / numba / array_analysis.py View on Github external
def gen_explicit_neg(self, arg, arg_rel, arg_typ, size_typ, loc, scope,
                         dsize, stmts, equiv_set):
        assert(not isinstance(size_typ, int))
        # Create var to hold the calculated slice size.
        explicit_neg_var = ir.Var(scope, mk_unique_var("explicit_neg"), loc)
        explicit_neg_val = ir.Expr.binop(operator.add, dsize, arg, loc=loc)
        # Determine the type of that var.  Can be literal if we know the
        # literal size of the dimension.
        explicit_neg_typ = types.intp
        self.calltypes[explicit_neg_val] = signature(explicit_neg_typ,
                                                     size_typ, arg_typ)
        # We'll prepend this slice size calculation to the get/setitem.
        stmts.append(ir.Assign(value=explicit_neg_val,
                               target=explicit_neg_var, loc=loc))
        self._define(equiv_set, explicit_neg_var,
                     explicit_neg_typ, explicit_neg_val)
        return explicit_neg_var, explicit_neg_typ
github numba / numba / numba / array_analysis.py View on Github external
print("after rewriting negatives", "lhs_rel", lhs_rel, "rhs_rel", rhs_rel)

        if lhs_known and rhs_known:
            if config.DEBUG_ARRAY_OPT >= 2:
                print("lhs and rhs known so return static size")
            return self.gen_static_slice_size(lhs_rel, rhs_rel, loc, scope, stmts, equiv_set), replacement_slice_var

        if (lhs_rel == 0 and isinstance(rhs_rel, tuple) and
            equiv_set.is_equiv(dsize, rhs_rel[0]) and
            rhs_rel[1] == 0):
            return dsize, None

        slice_typ = types.intp
        orig_slice_typ = slice_typ

        size_var = ir.Var(scope, mk_unique_var("slice_size"), loc)
        size_val = ir.Expr.binop(operator.sub, rhs, lhs, loc=loc)
        self.calltypes[size_val] = signature(slice_typ, rhs_typ, lhs_typ)
        self._define(equiv_set, size_var, slice_typ, size_val)
        size_rel = equiv_set.get_rel(size_var)
        if config.DEBUG_ARRAY_OPT >= 2:
            print("size_rel", size_rel, type(size_rel))

        wrap_var = ir.Var(scope, mk_unique_var("wrap"), loc)
        wrap_def = ir.Global('wrap_index', wrap_index, loc=loc)
        fnty = get_global_func_typ(wrap_index)
        sig = self.context.resolve_function_type(fnty, (orig_slice_typ, size_typ,), {})
        self._define(equiv_set, wrap_var, fnty, wrap_def)

        def gen_wrap_if_not_known(val, val_typ, known):
            if not known:
                var = ir.Var(scope, mk_unique_var("var"), loc)
github IntelPython / sdc / sdc / hiframes / hiframes_typed.py View on Github external
def _run_call_series_rolling(self, assign, lhs, rhs, rolling_var, func_name):
        """
        Handle Series rolling calls like:
          A = df.column.rolling(3).sum()
        """
        rolling_call = guard(get_definition, self.state.func_ir, rolling_var)
        assert isinstance(rolling_call, ir.Expr) and rolling_call.op == 'call'
        call_def = guard(get_definition, self.state.func_ir, rolling_call.func)
        assert isinstance(call_def, ir.Expr) and call_def.op == 'getattr'
        series_var = call_def.value
        nodes = []
        data = self._get_series_data(series_var, nodes)

        window, center, on = get_rolling_setup_args(self.state.func_ir, rolling_call, False)
        if not isinstance(center, ir.Var):
            center_var = ir.Var(lhs.scope, mk_unique_var("center"), lhs.loc)
            self.state.typemap[center_var.name] = types.bool_
            nodes.append(ir.Assign(ir.Const(center, lhs.loc), center_var, lhs.loc))
            center = center_var

        if func_name in ('cov', 'corr'):
            # TODO: variable window
            if len(rhs.args) == 1:
                other = self._get_series_data(rhs.args[0], nodes)
            else:
                other = data
            if func_name == 'cov':
                def f(a, b, w, c):
                    return sdc.hiframes.api.init_series(sdc.hiframes.rolling.rolling_cov(a, b, w, c))
            if func_name == 'corr':
                def f(a, b, w, c):
                    return sdc.hiframes.api.init_series(sdc.hiframes.rolling.rolling_corr(a, b, w, c))
github numba / numba / numba / parfor.py View on Github external
def _mk_mvdot_body(typemap, calltypes, phi_b_var, index_var, in1, in2, sum_var,
                   scope, loc, el_typ):
    """generate array inner product (X[p,:], v[:]) for parfor of np.dot(X,v)"""
    body_block = ir.Block(scope, loc)
    # inner_index = phi_b_var
    inner_index = ir.Var(scope, mk_unique_var("$inner_index"), loc)
    typemap[inner_index.name] = types.intp
    inner_index_assign = ir.Assign(phi_b_var, inner_index, loc)
    # tuple_var = build_tuple(index_var, inner_index)
    tuple_var = ir.Var(scope, mk_unique_var("$tuple_var"), loc)
    typemap[tuple_var.name] = types.containers.UniTuple(types.intp, 2)
    tuple_call = ir.Expr.build_tuple([index_var, inner_index], loc)
    tuple_assign = ir.Assign(tuple_call, tuple_var, loc)
    # X_val = getitem(X, tuple_var)
    X_val = ir.Var(scope, mk_unique_var("$" + in1.name + "_val"), loc)
    typemap[X_val.name] = el_typ
    getitem_call = ir.Expr.getitem(in1, tuple_var, loc)
    calltypes[getitem_call] = signature(el_typ, typemap[in1.name],
                                        typemap[tuple_var.name])
    getitem_assign = ir.Assign(getitem_call, X_val, loc)
    # v_val = getitem(V, inner_index)
    v_val = ir.Var(scope, mk_unique_var("$" + in2.name + "_val"), loc)