# Buggy code
def maximize_fusion(blocks):
    for block in blocks.values():
        order_changed = True
        while order_changed:
            order_changed = False
            i = 0
            while i < len(block.body) - 2:
                stmt = block.body[i]
                next_stmt = block.body[i + 1]
                # swap only parfors with non-parfors
                # only read-read dependencies are OK
                # make sure there is no write-write, write-read dependencies
                if isinstance(stmt, Parfor) and not isinstance(next_stmt, Parfor):
                    stmt_accesses = {v.name for v in stmt.list_vars()}
                    stmt_writes = get_parfor_writes(stmt)
                    next_accesses = {v.name for v in next_stmt.list_vars()}
                    next_writes = get_stmt_writes(next_stmt)
                    if (
                        len(
                            (stmt_writes & next_accesses)
                            | (next_writes & stmt_accesses)
                        )
                        == 0
                    ):
                        block.body[i] = next_stmt
                        block.body[i + 1] = stmt
                        order_changed = True
                i += 1
    return


# Traceback (TypeError)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-15-3b23f7331ceb> in <module>()
----> 1 @jit(arg_types=[numba.double], ret_type=numba.double)
2 def is_REALLY_five(some_value):
3     for i in range(5):
4         if some_value == 5.0:
5             return 1.0

/Library/Frameworks/EPD64.framework/Versions/7.3/lib/python2.7/site-packages/numba/decorators.pyc in _jit(func)
77                   "garbage collected!" % (func,))
78         t = Translate(func, *args, **kws)
---> 79         t.translate()
80         __tr_map__[func] = t
81         return t.get_ctypes_func(llvm)

/Library/Frameworks/EPD64.framework/Versions/7.3/lib/python2.7/site-packages/numba/translate.pyc in translate(self)
926                     # Ensure we are playing with locals that might
927                     # actually precede the next block.
--> 928                     self.check_locals(i)
929
930                 self.crnt_block = i

/Library/Frameworks/EPD64.framework/Versions/7.3/lib/python2.7/site-packages/numba/translate.pyc in check_locals(self, i)
1135                 else:
1136                     assert next_locals is not None, "Internal compiler error!"
-> 1137             self._locals = next_locals[:]
1138
1139     def get_ctypes_func(self, llvm=True):

TypeError: 'NoneType' object has no attribute '__getitem__'

# Fixed code
def maximize_fusion(blocks):
    call_table, _ = get_call_table(blocks)
    for block in blocks.values():
        order_changed = True
        while order_changed:
            order_changed = False
            i = 0
            while i < len(block.body) - 2:
                stmt = block.body[i]
                next_stmt = block.body[i + 1]
                # swap only parfors with non-parfors
                # don't reorder calls with side effects (e.g. file close)
                # only read-read dependencies are OK
                # make sure there is no write-write, write-read dependencies
                if (
                    isinstance(stmt, Parfor)
                    and not isinstance(next_stmt, Parfor)
                    and (
                        not isinstance(next_stmt, ir.Assign)
                        or has_no_side_effect(next_stmt.value, set(), call_table)
                    )
                ):
                    stmt_accesses = {v.name for v in stmt.list_vars()}
                    stmt_writes = get_parfor_writes(stmt)
                    next_accesses = {v.name for v in next_stmt.list_vars()}
                    next_writes = get_stmt_writes(next_stmt)
                    if (
                        len(
                            (stmt_writes & next_accesses)
                            | (next_writes & stmt_accesses)
                        )
                        == 0
                    ):
                        block.body[i] = next_stmt
                        block.body[i + 1] = stmt
                        order_changed = True
                i += 1
    return