Repository: numba/numba

Issue:
parallel bugs with bool arrays?
<!--

Thanks for opening an issue! To help the Numba team handle your information
efficiently, please first ensure that there is no other issue present that
already describes the issue you have
(search at https://github.com/numba/numba/issues?&q=is%3Aissue).

-->

## Reporting a bug

<!--

Before submitting a bug report please ensure that you can check off these boxes:

-->

- [ ] I have tried using the latest released version of Numba (most recent is
 visible in the change log (https://github.com/numba/numba/blob/master/CHANGE_LOG).
- [x] I have included a self contained code sample to reproduce the problem.
  i.e. it's possible to run as 'python bug.py'.

<!--

Please include details of the bug here, including, if applicable, what you
expected to happen!

-->
my numba version is '0.52.0'

```
@njit(parallel=True)
def test_1(x, d):
    na_mask = np.isfinite(x)  # np.isnan 
    n = len(x)
    result = np.empty(n)
    for i in prange(n - d + 1):
        result[i] = np.sum(na_mask[i:i + d])
    return result


>>> test_1(np.random.randn(10), 2)
```

```
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)
```

```
@njit
def test_2(x, d):
    na_mask = np.isfinite(x)
    n = len(x)
    result = np.empty(n)
    for i in prange(n - d + 1):
        result[i] = np.sum(na_mask[i:i + d])
    return result


>>> test_2(np.random.randn(10), 2)
```

```
array([2.00000000e+000, 2.00000000e+000, 2.00000000e+000, 2.00000000e+000,
       2.00000000e+000, 2.00000000e+000, 2.00000000e+000, 2.00000000e+000,
       2.00000000e+000, 4.63631694e-310])
```

```
@njit(parallel=True)
def test_3(x, d):
    n = len(x)
    na_mask = np.full(n, True)
    result = np.empty(n)
    for i in prange(n - d + 1):
        result[i] = np.sum(na_mask[i:i + d])
    return result


>>>  test_3(np.random.randn(10), 2)
```

```
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-82-4acf17ef1a65>", line 11, in <module>
    test_3(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-82-4acf17ef1a65>", line 6:
def test_3(x, d):
    <source elided>
    result = np.empty(n)
    for i in prange(n - d + 1):
    ^
During: lowering "id=220[LoopNest(index_variable = parfor_index.19567, range = (0, $42binary_add.17, 1))]{50: <ir.Block at <ipython-input-82-4acf17ef1a65> (6)>}Var(parfor_index.19567, <ipython-input-82-4acf17ef1a65>:6)" at <ipython-input-82-4acf17ef1a65> (6)
```




Discussion:
Thanks for the report. I can reproduce, this is a MWE:
```python
from numba import njit, prange
import numpy as np

@njit(parallel=True)
def test_1():
    na_mask = np.ones((5,))
    result = np.empty((5,))
    for i in prange(1):
        result[i] = np.sum(na_mask[i:i + 1])

test_1()
```
This issue comes from the `np.sum(na_mask[i:i + 1])`.
CC @DrTodd13 

Resolving patch:
diff --git a/numba/parfors/array_analysis.py b/numba/parfors/array_analysis.py
--- a/numba/parfors/array_analysis.py
+++ b/numba/parfors/array_analysis.py
@@ -91,8 +91,13 @@ def wrap_index(typingctx, idx, size):
     during slice/range analysis.
     """
     unified_ty = typingctx.unify_types(idx, size)
-    if not unified_ty:
-        raise ValueError("Argument types for wrap_index must match")
+    # 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)


Test patch:
diff --git a/numba/tests/test_parfors.py b/numba/tests/test_parfors.py
--- a/numba/tests/test_parfors.py
+++ b/numba/tests/test_parfors.py
@@ -3363,6 +3363,19 @@ def f(r):
         r = np.array([[0., 0., 0.], [0., 0., 1.]])
         self.assertPreciseEqual(f(r), f.py_func(r))
 
+    @skip_parfors_unsupported
+    def test_issue6774(self):
+        @njit(parallel=True)
+        def test_impl():
+            n = 5
+            na_mask = np.ones((n,))
+            result = np.empty((n - 1,))
+            for i in prange(len(result)):
+                result[i] = np.sum(na_mask[i:i + 1])
+            return result
+
+        self.check(test_impl)
+
 
 class TestParforsOptions(TestParforsBase):