Repository: numba/numba

Issue:
test_fuse_argmin unexpected IR in lowering
## Reporting a bug
- [x] I am using the ~~latest version~~ 0.40.0dev  version of Numba (most recent is visible in
 the change log (https://github.com/numba/numba/blob/master/CHANGE_LOG).
- [ ] I have included below a minimal working reproducer (if you are unsure how
 to write one see http://matthewrocklin.com/blog/work/2018/02/28/minimal-bug-reports).

For this PR https://github.com/numba/numba/pull/3270 Travis CI testing failed for combination `PYTHON=3.5 NUMPY=1.10`, looks like there's both openblas and MKL in there... but the actual error reported is Parfors related, traceback:
```
ERROR: test_fuse_argmin (numba.tests.test_parfors.TestParfors)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/travis/build/numba/numba/numba/errors.py", line 601, in new_error_context
    yield
  File "/home/travis/build/numba/numba/numba/lowering.py", line 254, in lower_block
    self.lower_inst(inst)
  File "/home/travis/build/numba/numba/numba/lowering.py", line 403, in lower_inst
    func(self, inst)
  File "/home/travis/build/numba/numba/numba/npyufunc/parfor.py", line 276, in _lower_parfor_parallel
    parfor.races)
  File "/home/travis/build/numba/numba/numba/npyufunc/parfor.py", line 1206, in call_parallel_gufunc
    ary = context.make_array(aty)(context, builder, arg)
  File "/home/travis/build/numba/numba/numba/cgutils.py", line 117, in __init__
    % (outer_ref.type.pointee, value.type))
AssertionError: bad value type: expected {i8*, i8*, i64, i64, {i64, double}*, [1 x i64], [1 x i64]}, got {i8*, i8*, i64, i64, double*, [1 x i64], [1 x i64]}
```


Discussion:
Here's the test command that will reproduce the problem.  
```bash
PYTHONHASHSEED=0 python -m numba.runtests -vfb numba.tests.test_extending.TestIntrinsic.test_serialization numba.tests.test_ufuncs.TestArrayOperators.test_inplace_or numba.tests.test_lists.TestLists.test_delslice1 numba.tests.test_ufuncs.TestLoopTypesIntNoPython.test_fmod_ee_e numba.tests.test_ufuncs.TestLoopTypesFloatNoPython.test_arccos_e_e numba.tests.test_ufuncs.TestLoopTypesDatetimeNoPython.test_exp2_D_D numba.tests.test_ufuncs.TestLoopTypesDatetimeNoPython.test_invert_L_L numba.tests.test_ufuncs.TestLoopTypesFloatNoPython.test_logical_and_qq_? numba.tests.test_mathlib.TestMathLib.test_frexp_npm numba.tests.test_ufuncs.TestLoopTypesFloatNoPython.test_minimum_mm_m numba.tests.test_ufuncs.TestLoopTypesComplexNoPython.test_not_equal_ll_? numba.tests.test_ufuncs.TestLoopTypesComplexNoPython.test_cosh_F_F numba.tests.test_array_exprs.TestRewriteIssues.test_annotations numba.tests.test_random.TestRandomArrays.test_numpy_randn numba.tests.test_ufuncs.TestLoopTypesReciprocalNoPython.test_reciprocal_F_F numba.tests.test_ufuncs.TestLoopTypesDatetimeNoPython.test_cos_D_D numba.tests.test_array_reductions.TestArrayReductions.test_array_nanvar_bool_3d numba.tests.test_array_exprs.TestRewriteIssues.test_unary_arrayexpr numba.tests.test_ufuncs.TestLoopTypesComplexNoPython.test_logical_xor_II_? numba.tests.test_ufuncs.TestLoopTypesFloatNoPython.test_sinh_F_F numba.tests.test_ufuncs.TestLoopTypesIntNoPython.test_log1p_g_g numba.tests.test_ufuncs.TestLoopTypesDatetimeNoPython.test_hypot_ee_e numba.tests.test_ufuncs.TestLoopTypesComplexNoPython.test_equal_ff_? numba.tests.test_ufuncs.TestLoopTypesDatetimeNoPython.test_maximum_??_? numba.tests.test_ufuncs.TestLoopTypesComplexNoPython.test_not_equal_BB_? numba.tests.test_ufuncs.TestLoopTypesFloatNoPython.test_add_HH_H numba.tests.test_ufuncs.TestLoopTypesComplexNoPython.test_sinh_e_e numba.tests.test_ufuncs.TestLoopTypesComplexNoPython.test_ldexp_fi_f numba.tests.test_ufuncs.TestLoopTypesDatetimeNoPython.test_add_OO_O numba.tests.test_datamodel.TestUInt32.test_as_data numba.tests.test_ufuncs.TestLoopTypesIntNoPython.test_bitwise_and_qq_q numba.tests.test_random.TestThreads.test_np_thread_safety numba.tests.test_ufuncs.TestLoopTypesFloatNoPython.test_isnan_D_? numba.tests.test_dyn_array.TestNpyEmptyKeyword.test_with_dtype_kws numba.tests.test_dummyarray.TestSlicing.test_slice0_2d numba.tests.test_ufuncs.TestLoopTypesFloorDivideNoPython.test_remainder_OO_O numba.tests.test_svml.TestSVMLGeneration.test_int32_prange4_fastmath_usecase numba.tests.test_indexing.TestTyping.test_layout numba.tests.test_types.TestPickling.test_generator numba.tests.test_ufuncs.TestLoopTypesDatetimeNoPython.test_absolute_m_m numba.tests.test_ufuncs.TestLoopTypesDatetimeNoPython.test_ldexp_gi_g numba.tests.test_ufuncs.TestLoopTypesComplexNoPython.test_greater_ii_? numba.tests.test_ufuncs.TestLoopTypesDatetimeNoPython.test_sign_d_d numba.tests.test_ufuncs.TestLoopTypesIntNoPython.test_rint_f_f numba.tests.test_ufuncs.TestLoopTypesFloatNoPython.test_logical_not_h_? numba.tests.test_operators.TestOperators.test_bitshift_iright_ints numba.tests.test_looplifting.TestLoopLiftingAnnotate.test_annotate_2 numba.tests.test_indexing.TestSetItem.test_1d_slicing_add numba.tests.test_array_reductions.TestArrayReductions.test_array_var_global_complex64_1d numba.tests.test_array_reductions.TestArrayReductions.test_array_std_int32_1d numba.tests.test_ufuncs.TestLoopTypesFloatNoPython.test_logaddexp2_dd_d numba.tests.test_record_dtype.TestRecordDtypeWithStructArraysAndDispatcher.test_record_read_2d_array numba.tests.test_ufuncs.TestLoopTypesFloatNoPython.test_ldexp_fl_f numba.tests.test_parfors.TestParfors.test_reduction_var_reuse numba.tests.test_np_functions.TestNPFunctions.test_sinc numba.tests.test_ufuncs.TestLoopTypesIntNoPython.test_not_equal_BB_? numba.tests.test_ufuncs.TestLoopTypesIntNoPython.test_arctan2_gg_g numba.tests.test_sort.TestQuicksortPurePython.test_insertion_sort numba.tests.test_withlifting.TestWithFinding.test_lift4 numba.tests.test_ufuncs.TestLoopTypesComplexNoPython.test_fmod_OO_O numba.tests.test_ufuncs.TestLoopTypesFloatNoPython.test_trunc_f_f numba.tests.test_generators.TestGenerators.test_gen6 numba.tests.test_ufuncs.TestLoopTypesDatetimeNoPython.test_signbit_e_? numba.tests.test_ufuncs.TestLoopTypesIntNoPython.test_cosh_O_O numba.tests.test_mangling.TestMangling.test_one_args numba.tests.test_ufuncs.TestLoopTypesDatetimeNoPython.test_power_gg_g numba.tests.test_ufuncs.TestLoopTypesIntNoPython.test_multiply_OO_O numba.tests.test_ufuncs.TestLoopTypesDatetimeNoPython.test_minimum_HH_H numba.tests.test_generators.TestGenerators.test_np_flat numba.tests.test_ufuncs.TestLoopTypesFloatNoPython.test_right_shift_HH_H numba.tests.test_complex.TestComplex.test_div_npm numba.tests.test_ufuncs.TestLoopTypesDatetimeNoPython.test_isnan_f_? numba.tests.test_ufuncs.TestLoopTypesDatetimeNoPython.test_equal_bb_? numba.tests.test_ufuncs.TestLoopTypesIntNoPython.test_true_divide_OO_O numba.tests.test_operators.TestOperatorModule.test_idiv_floats_npm numba.tests.test_ufuncs.TestLoopTypesIntNoPython.test_logical_or_ll_? numba.tests.test_generators.TestNrtNestedGen.test_nrt_nested_gen_refct numba.tests.test_ufuncs.TestLoopTypesDatetimeNoPython.test_add_mm_m numba.tests.test_ufuncs.TestLoopTypesComplexNoPython.test_multiply_md_m numba.tests.test_numconv.TestNumberConversion.test_int16_to_float64 numba.tests.npyufunc.test_gufunc.TestGUVectorizeScalar.test_scalar_input numba.tests.test_ufuncs.TestLoopTypesFloatNoPython.test_logaddexp2_gg_g numba.tests.test_ufuncs.TestLoopTypesDatetimeNoPython.test_less_equal_ee_? numba.tests.test_random.TestRandom.test_numpy_pareto numba.tests.test_ufuncs.TestLoopTypesFloatNoPython.test_sin_G_G numba.tests.test_dummyarray.TestReshape.test_reshape_3d1d numba.tests.test_ufuncs.TestLoopTypesComplexNoPython.test_logical_xor_LL_? numba.tests.test_ufuncs.TestLoopTypesFloatNoPython.test_absolute_?_? numba.tests.test_ufuncs.TestLoopTypesFloorDivideNoPython.test_remainder_ii_i numba.tests.test_svml.TestSVMLGeneration.test_int32_scalar8_usecase numba.tests.test_array_attr.TestNestedArrayAttr.test_ndim numba.tests.test_ufuncs.TestLoopTypesFloatNoPython.test_greater_II_? numba.tests.test_ufuncs.TestLoopTypesFloatNoPython.test_absolute_H_H numba.tests.test_ufuncs.TestLoopTypesComplexNoPython.test_tan_G_G numba.tests.test_ufuncs.TestLoopTypesComplexNoPython.test_deg2rad_O_O numba.tests.test_ufuncs.TestLoopTypesDatetimeNoPython.test_minimum_hh_h numba.tests.test_ufuncs.TestLoopTypesComplexNoPython.test_logical_xor_gg_? numba.tests.test_ufuncs.TestLoopTypesComplexNoPython.test_arccosh_F_F numba.tests.test_ufuncs.TestLoopTypesFloatNoPython.test_maximum_??_? numba.tests.test_array_reductions.TestArrayReductions.test_array_mean_global_complex64_3d numba.tests.test_complex.TestCMath.test_sqrt_npm numba.tests.test_array_iterators.TestNdIter.test_errors numba.tests.test_enums.TestEnum.test_compare numba.tests.test_ufuncs.TestLoopTypesFloatNoPython.test_greater_equal_ll_? numba.tests.test_array_reductions.TestArrayReductions.test_array_argmax_global_float32_2d numba.tests.test_array_reductions.TestArrayReductions.test_array_nanvar_bool_1d numba.tests.test_ufuncs.TestLoopTypesDatetimeNoPython.test_less_equal_GG_? numba.tests.test_ufuncs.TestScalarUFuncs.test_scalar_binary_uniform_ufunc numba.tests.test_ufuncs.TestLoopTypesDatetimeNoPython.test_not_equal_II_? numba.tests.test_complex.TestCMath.test_log_npm numba.tests.test_ufuncs.TestUFuncs.test_invert_ufunc numba.tests.test_ufuncs.TestLoopTypesFloatNoPython.test_equal_??_? numba.tests.test_linalg.TestLinalgEigenSystems.test_linalg_eigvals numba.tests.test_ufuncs.TestLoopTypesDatetimeNoPython.test_true_divide_mq_m numba.tests.test_ufuncs.TestLoopTypesIntNoPython.test_exp_f_f numba.tests.test_extending.TestRegisterJitable.test_flags_no_nrt numba.tests.test_operators.TestOperatorModule.test_bitwise_and_bools numba.tests.test_numconv.TestNumberConversion.test_int16_to_uint8 numba.tests.test_ufuncs.TestLoopTypesComplexNoPython.test_log_D_D numba.tests.test_ufuncs.TestLoopTypesFloatNoPython.test_minimum_dd_d numba.tests.test_record_dtype.TestRecordDtypeWithDispatcher.test_record_return numba.tests.test_parfors.TestParforsSlice.test_parfor_slice3 numba.tests.test_ufuncs.TestLoopTypesFloatNoPython.test_cosh_g_g numba.tests.test_ufuncs.TestLoopTypesSubtractAndNegativeNoPython.test_subtract_HH_H numba.tests.test_ufuncs.TestLoopTypesFloatNoPython.test_sign_D_D numba.tests.test_ufuncs.TestLoopTypesIntNoPython.test_greater_equal_??_? numba.tests.test_ufuncs.TestLoopTypesDatetimeNoPython.test_not_equal_GG_? numba.tests.test_ufuncs.TestLoopTypesFloatNoPython.test_log_O_O numba.tests.test_numberctor.TestScalarNumberCtor.test_complex numba.tests.test_array_reductions.TestArrayReductions.test_argmin_basic numba.tests.test_operators.TestOperators.test_bitwise_xor_ints numba.tests.test_ufuncs.TestLoopTypesComplexNoPython.test_absolute_m_m numba.tests.test_ufuncs.TestLoopTypesIntNoPython.test_logical_not_e_? numba.tests.test_ufuncs.TestLoopTypesDatetimeNoPython.test_multiply_II_I numba.tests.test_array_constants.TestConstantArray.test_record_array_const_noncontig numba.tests.npyufunc.test_dufunc.TestDUFunc.test_npm_call numba.tests.test_auto_constants.TestAutoConstants.test_numpy_nan numba.tests.test_ufuncs.TestLoopTypesDatetimeNoPython.test_not_equal_OO_? numba.tests.test_dyn_array.TestNdOnes.test_1d numba.tests.test_dyn_array.TestNpStack.test_0d numba.tests.test_ufuncs.TestLoopTypesFloatNoPython.test_invert_I_I numba.tests.test_parfors.TestParfors.test_fuse_argmin
```
I confirmed it on a py3.5np1.10 env on linux64.
Will look at it now that there's a reproducer I can use.

Here's a smaller reproducer with just 3 tests:
```
PYTHONHASHSEED=0 python -m numba.runtests -vfb numba.tests.test_parfors.TestParforsSlice.test_parfor_slice3 numba.tests.test_parfors.TestParfors.test_reduction_var_reuse numba.tests.test_parfors.TestParfors.test_fuse_argmin
```
Doing:
```
conda create -n fix3279_2 -c numba python=3.5 numpy=1.10 llvmlite=0.25.0* gcc_linux-64 gxx_linux-64
```
then installing Numba from master, then...
```bash
for i in `seq 0 10000`; do export PYTHONHASHSEED=${i}; echo "HASHSEED=${PYTHONHASHSEED}"; python -m numba.runtests -vfb numba.tests.test_parfors.TestParforsSlice.test_parfor_slice3 numba.tests.test_parfors.TestParfors.test_reduction_var_reuse numba.tests.test_parfors.TestParfors.test_fuse_argmin; done
```
Having `PYTHONHASHSEED` as `3, 5, 17, 32, 54, 86, 89, 98` tripped it for me.
Interesting. Mine triggers at 13.

Resolving patch:
diff --git a/numba/npyufunc/parfor.py b/numba/npyufunc/parfor.py
--- a/numba/npyufunc/parfor.py
+++ b/numba/npyufunc/parfor.py
@@ -1161,7 +1161,7 @@ def load_range(v):
                     types.intp, debug_flag)])
 
     # Get the LLVM vars for the Numba IR reduction array vars.
-    redarrs = [lowerer.loadvar(x.name) for x in list(redarrdict.values())]
+    redarrs = [lowerer.loadvar(redarrdict[x].name) for x in redvars]
 
     nredvars = len(redvars)
     ninouts = len(expr_args) - nredvars


Test patch: