I was pruning the if-else branch based on whether a flag is None. A toy code would look like
from numba import njit
from numba.typed import List
from numba import prange
@njit(parallel=True)
def aNumbaFunc(flag=None):
if flag is None:
aNumbaList = None
else:
aNumbaList = List()
for i in range(some_iter):
aNumbaList.append(SomeClass())
for i in prange(some_iter):
if flag is None:
aVariable = None
else:
aVariable = aNumbaList[i]
someOtherNumbaFunc(aVariable)
return aNumbaList
The above was giving segmentation faults.
But just by adding an assert statement, the issue seems to be resolved.
from numba import njit
from numba.typed import List
from numba import prange
@njit(parallel=True)
def aNumbaFunc(flag=None):
if flag is None:
aNumbaList = None
else:
aNumbaList = List()
for i in range(some_iter):
aNumbaList.append(SomeClass())
for i in prange(some_iter):
if flag is None:
aVariable = None
else:
assert flag is not None
aVariable = aNumbaList[i]
someOtherNumbaFunc(aVariable)
return aNumbaList
It would be helpful if anyone could help me understand why the assert statement seems to force the branch pruning. Also, there was not much documentation on branch pruning, and seeing that it’s an essential part of programming in Python, I believe it would be beneficial for newcomers if they found that there is a thing called BranchPruning and how it works.