# Buggy code
def wrap_index(typingctx, idx, size):
    """
    Calculate index value "idx" relative to a size "size" value as
    (idx % size), where "size" is known to be positive.
    Note that we use the mod(%) operation here instead of
    (idx < 0 ? idx + size : idx) because we may have situations
    where idx > size due to the way indices are calculated
    during slice/range analysis.
    """
    unified_ty = typingctx.unify_types(idx, size)
    if not unified_ty:
        raise ValueError("Argument types for wrap_index must match")

    def codegen(context, builder, sig, args):
        ll_unified_ty = context.get_data_type(unified_ty)
        idx = builder.sext(args[0], ll_unified_ty)
        size = builder.sext(args[1], ll_unified_ty)
        neg_size = builder.neg(size)
        zero = llvmlite.ir.Constant(ll_unified_ty, 0)
        idx_negative = builder.icmp_signed("<", idx, zero)
        pos_oversize = builder.icmp_signed(">=", idx, size)
        neg_oversize = builder.icmp_signed("<=", idx, neg_size)
        pos_res = builder.select(pos_oversize, size, idx)
        neg_res = builder.select(neg_oversize, zero, builder.add(idx, size))
        mod = builder.select(idx_negative, neg_res, pos_res)
        return mod

    return signature(unified_ty, idx, size), codegen


# Traceback (RuntimeError)
Traceback (most recent call last):
File "/home/auderson/miniconda3/lib/python3.8/site-packages/numba/core/errors.py", line 744, in new_error_context
yield
File "/home/auderson/miniconda3/lib/python3.8/site-packages/numba/core/lowering.py", line 230, in lower_block
self.lower_inst(inst)
File "/home/auderson/miniconda3/lib/python3.8/site-packages/numba/core/lowering.py", line 443, in lower_inst
func(self, inst)
File "/home/auderson/miniconda3/lib/python3.8/site-packages/numba/parfors/parfor_lowering.py", line 259, in _lower_parfor_parallel
exp_name_to_tuple_var) = _create_gufunc_for_parfor_body(
File "/home/auderson/miniconda3/lib/python3.8/site-packages/numba/parfors/parfor_lowering.py", line 1361, in _create_gufunc_for_parfor_body
kernel_func = compiler.compile_ir(
File "/home/auderson/miniconda3/lib/python3.8/site-packages/numba/core/compiler.py", line 665, in compile_ir
return pipeline.compile_ir(func_ir=func_ir, lifted=lifted,
File "/home/auderson/miniconda3/lib/python3.8/site-packages/numba/core/compiler.py", line 362, in compile_ir
return self._compile_ir()
File "/home/auderson/miniconda3/lib/python3.8/site-packages/numba/core/compiler.py", line 421, in _compile_ir
return self._compile_core()
File "/home/auderson/miniconda3/lib/python3.8/site-packages/numba/core/compiler.py", line 394, in _compile_core
raise e
File "/home/auderson/miniconda3/lib/python3.8/site-packages/numba/core/compiler.py", line 385, in _compile_core
pm.run(self.state)
File "/home/auderson/miniconda3/lib/python3.8/site-packages/numba/core/compiler_machinery.py", line 339, in run
raise patched_exception
File "/home/auderson/miniconda3/lib/python3.8/site-packages/numba/core/compiler_machinery.py", line 330, in run
self._runPass(idx, pass_inst, state)
File "/home/auderson/miniconda3/lib/python3.8/site-packages/numba/core/compiler_lock.py", line 32, in _acquire_compile_lock
return func(*args, **kwargs)
File "/home/auderson/miniconda3/lib/python3.8/site-packages/numba/core/compiler_machinery.py", line 289, in _runPass
mutated |= check(pss.run_pass, internal_state)
File "/home/auderson/miniconda3/lib/python3.8/site-packages/numba/core/compiler_machinery.py", line 262, in check
mangled = func(compiler_state)
File "/home/auderson/miniconda3/lib/python3.8/site-packages/numba/core/typed_passes.py", line 449, in run_pass
NativeLowering().run_pass(state)
File "/home/auderson/miniconda3/lib/python3.8/site-packages/numba/core/typed_passes.py", line 373, in run_pass
lower.lower()
File "/home/auderson/miniconda3/lib/python3.8/site-packages/numba/core/lowering.py", line 174, in lower
self.library.add_ir_module(self.module)
File "/home/auderson/miniconda3/lib/python3.8/site-packages/numba/core/codegen.py", line 648, in add_ir_module
ll_module = ll.parse_assembly(ir)
File "/home/auderson/miniconda3/lib/python3.8/site-packages/llvmlite/binding/module.py", line 25, in parse_assembly
raise RuntimeError("LLVM IR parsing error\n{0}".format(errmsg))
RuntimeError: Failed in nopython mode pipeline (step: nopython mode backend)
LLVM IR parsing error
<string>:447:18: error: invalid cast opcode for cast from 'i64' to 'double'
%".364" = sext i64 %".362" to double
^
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/auderson/miniconda3/lib/python3.8/site-packages/IPython/core/interactiveshell.py", line 3437, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-77-81f1eb0ce800>", line 11, in <module>
test(np.random.randn(10), 2)
File "/home/auderson/miniconda3/lib/python3.8/site-packages/numba/core/dispatcher.py", line 433, in _compile_for_args
raise e
File "/home/auderson/miniconda3/lib/python3.8/site-packages/numba/core/dispatcher.py", line 366, in _compile_for_args
return self.compile(tuple(argtypes))
File "/home/auderson/miniconda3/lib/python3.8/site-packages/numba/core/compiler_lock.py", line 32, in _acquire_compile_lock
return func(*args, **kwargs)
File "/home/auderson/miniconda3/lib/python3.8/site-packages/numba/core/dispatcher.py", line 857, in compile
cres = self._compiler.compile(args, return_type)
File "/home/auderson/miniconda3/lib/python3.8/site-packages/numba/core/dispatcher.py", line 77, in compile
status, retval = self._compile_cached(args, return_type)
File "/home/auderson/miniconda3/lib/python3.8/site-packages/numba/core/dispatcher.py", line 91, in _compile_cached
retval = self._compile_core(args, return_type)
File "/home/auderson/miniconda3/lib/python3.8/site-packages/numba/core/dispatcher.py", line 104, in _compile_core
cres = compiler.compile_extra(self.targetdescr.typing_context,
File "/home/auderson/miniconda3/lib/python3.8/site-packages/numba/core/compiler.py", line 602, in compile_extra
return pipeline.compile_extra(func)
File "/home/auderson/miniconda3/lib/python3.8/site-packages/numba/core/compiler.py", line 352, in compile_extra
return self._compile_bytecode()
File "/home/auderson/miniconda3/lib/python3.8/site-packages/numba/core/compiler.py", line 414, in _compile_bytecode
return self._compile_core()
File "/home/auderson/miniconda3/lib/python3.8/site-packages/numba/core/compiler.py", line 394, in _compile_core
raise e
File "/home/auderson/miniconda3/lib/python3.8/site-packages/numba/core/compiler.py", line 385, in _compile_core
pm.run(self.state)
File "/home/auderson/miniconda3/lib/python3.8/site-packages/numba/core/compiler_machinery.py", line 339, in run
raise patched_exception
File "/home/auderson/miniconda3/lib/python3.8/site-packages/numba/core/compiler_machinery.py", line 330, in run
self._runPass(idx, pass_inst, state)
File "/home/auderson/miniconda3/lib/python3.8/site-packages/numba/core/compiler_lock.py", line 32, in _acquire_compile_lock
return func(*args, **kwargs)
File "/home/auderson/miniconda3/lib/python3.8/site-packages/numba/core/compiler_machinery.py", line 289, in _runPass
mutated |= check(pss.run_pass, internal_state)
File "/home/auderson/miniconda3/lib/python3.8/site-packages/numba/core/compiler_machinery.py", line 262, in check
mangled = func(compiler_state)
File "/home/auderson/miniconda3/lib/python3.8/site-packages/numba/core/typed_passes.py", line 449, in run_pass
NativeLowering().run_pass(state)
File "/home/auderson/miniconda3/lib/python3.8/site-packages/numba/core/typed_passes.py", line 373, in run_pass
lower.lower()
File "/home/auderson/miniconda3/lib/python3.8/site-packages/numba/core/lowering.py", line 136, in lower
self.lower_normal_function(self.fndesc)
File "/home/auderson/miniconda3/lib/python3.8/site-packages/numba/core/lowering.py", line 190, in lower_normal_function
entry_block_tail = self.lower_function_body()
File "/home/auderson/miniconda3/lib/python3.8/site-packages/numba/core/lowering.py", line 216, in lower_function_body
self.lower_block(block)
File "/home/auderson/miniconda3/lib/python3.8/site-packages/numba/core/lowering.py", line 230, in lower_block
self.lower_inst(inst)
File "/home/auderson/miniconda3/lib/python3.8/contextlib.py", line 131, in __exit__
self.gen.throw(type, value, traceback)
File "/home/auderson/miniconda3/lib/python3.8/site-packages/numba/core/errors.py", line 751, in new_error_context
raise newerr.with_traceback(tb)
numba.core.errors.LoweringError: Failed in nopython mode pipeline (step: nopython mode backend)
Failed in nopython mode pipeline (step: nopython mode backend)
LLVM IR parsing error
<string>:447:18: error: invalid cast opcode for cast from 'i64' to 'double'
%".364" = sext i64 %".362" to double
^
File "<ipython-input-77-81f1eb0ce800>", line 6:
def test(x, d):
<source elided>
result = np.empty(n)
for i in prange(n - d + 1):
^
During: lowering "id=215[LoopNest(index_variable = parfor_index.19055, range = (0, $40binary_add.16, 1))]{48: <ir.Block at <ipython-input-77-81f1eb0ce800> (6)>}Var(parfor_index.19055, <ipython-input-77-81f1eb0ce800>:6)" at <ipython-input-77-81f1eb0ce800> (6)

# Fixed code
def wrap_index(typingctx, idx, size):
    """
    Calculate index value "idx" relative to a size "size" value as
    (idx % size), where "size" is known to be positive.
    Note that we use the mod(%) operation here instead of
    (idx < 0 ? idx + size : idx) because we may have situations
    where idx > size due to the way indices are calculated
    during slice/range analysis.
    """
    unified_ty = typingctx.unify_types(idx, size)
    # Mixing signed and unsigned ints will unify to double which is
    # no good for indexing.  If the unified type is not an integer
    # then just use int64 as the common index type.  This does have
    # some overflow potential if the unsigned value is greater than
    # 2**63.
    if not isinstance(unified_ty, types.Integer):
        unified_ty = types.int64

    def codegen(context, builder, sig, args):
        ll_unified_ty = context.get_data_type(unified_ty)
        idx = builder.sext(args[0], ll_unified_ty)
        size = builder.sext(args[1], ll_unified_ty)
        neg_size = builder.neg(size)
        zero = llvmlite.ir.Constant(ll_unified_ty, 0)
        idx_negative = builder.icmp_signed("<", idx, zero)
        pos_oversize = builder.icmp_signed(">=", idx, size)
        neg_oversize = builder.icmp_signed("<=", idx, neg_size)
        pos_res = builder.select(pos_oversize, size, idx)
        neg_res = builder.select(neg_oversize, zero, builder.add(idx, size))
        mod = builder.select(idx_negative, neg_res, pos_res)
        return mod

    return signature(unified_ty, idx, size), codegen