Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds tests for PR 5659 #6664

Merged
merged 7 commits into from
Feb 3, 2021
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
31 changes: 28 additions & 3 deletions numba/core/ir_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def next_label():
return _max_label


def mk_alloc(typemap, calltypes, lhs, size_var, dtype, scope, loc):
def mk_alloc(typemap, calltypes, lhs, size_var, dtype, scope, loc, lhs_typ):
"""generate an array allocation with np.empty() and return list of nodes.
size_var can be an int variable or tuple of int variables.
"""
Expand Down Expand Up @@ -113,9 +113,34 @@ def mk_alloc(typemap, calltypes, lhs, size_var, dtype, scope, loc):
# signature(
# types.npytypes.Array(dtype, ndims, 'C'), size_typ,
# types.functions.NumberClass(dtype))
alloc_assign = ir.Assign(alloc_call, lhs, loc)

out.extend([g_np_assign, attr_assign, typ_var_assign, alloc_assign])
if lhs_typ.layout == 'F':
empty_c_typ = lhs_typ.copy(layout='C')
empty_c_var = ir.Var(scope, mk_unique_var("$empty_c_var"), loc)
if typemap:
typemap[empty_c_var.name] = lhs_typ.copy(layout='C')
empty_c_assign = ir.Assign(alloc_call, empty_c_var, loc)

# attr call: asfortranarray = getattr(g_np_var, asfortranarray)
asfortranarray_attr_call = ir.Expr.getattr(g_np_var, "asfortranarray", loc)
afa_attr_var = ir.Var(scope, mk_unique_var("$asfortran_array_attr"), loc)
if typemap:
typemap[afa_attr_var.name] = get_np_ufunc_typ(numpy.asfortranarray)
afa_attr_assign = ir.Assign(asfortranarray_attr_call, afa_attr_var, loc)
# call asfortranarray
asfortranarray_call = ir.Expr.call(afa_attr_var, [empty_c_var], (), loc)
if calltypes:
calltypes[asfortranarray_call] = typemap[afa_attr_var.name].get_call_type(
typing.Context(), [empty_c_typ], {})

asfortranarray_assign = ir.Assign(asfortranarray_call, lhs, loc)

out.extend([g_np_assign, attr_assign, typ_var_assign, empty_c_assign,
afa_attr_assign, asfortranarray_assign])
else:
alloc_assign = ir.Assign(alloc_call, lhs, loc)
out.extend([g_np_assign, attr_assign, typ_var_assign, alloc_assign])

return out


Expand Down
31 changes: 26 additions & 5 deletions numba/parfors/parfor.py
Original file line number Diff line number Diff line change
Expand Up @@ -1920,7 +1920,8 @@ def run(self, blocks):
if isinstance(instr, ir.Assign):
expr = instr.value
lhs = instr.target
if self._is_C_order(lhs.name):
lhs_typ = self.pass_states.typemap[lhs.name]
if self._is_C_or_F_order(lhs_typ):
# only translate C order since we can't allocate F
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove this comment?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this comment should go.

if guard(self._is_supported_npycall, expr):
new_instr = self._numpy_to_parfor(equiv_set, lhs, expr)
Expand All @@ -1945,8 +1946,26 @@ def run(self, blocks):
block.body = new_body

def _is_C_order(self, arr_name):
typ = self.pass_states.typemap[arr_name]
return isinstance(typ, types.npytypes.Array) and typ.layout == 'C' and typ.ndim > 0
if isinstance(arr_name, types.npytypes.Array):
return arr_name.layout == 'C' and arr_name.ndim > 0
elif arr_name is str:
typ = self.pass_states.typemap[arr_name]
return (isinstance(typ, types.npytypes.Array) and
typ.layout == 'C' and
typ.ndim > 0)
else:
return False

def _is_C_or_F_order(self, arr_name):
if isinstance(arr_name, types.npytypes.Array):
return (arr_name.layout == 'C' or arr_name.layout == 'F') and arr_name.ndim > 0
elif arr_name is str:
typ = self.pass_states.typemap[arr_name]
return (isinstance(typ, types.npytypes.Array) and
(typ.layout == 'C' or typ.layout == 'F') and
typ.ndim > 0)
else:
return False

def _arrayexpr_to_parfor(self, equiv_set, lhs, arrayexpr, avail_vars):
"""generate parfor from arrayexpr node, which is essentially a
Expand All @@ -1966,7 +1985,8 @@ def _arrayexpr_to_parfor(self, equiv_set, lhs, arrayexpr, avail_vars):
# generate init block and body
init_block = ir.Block(scope, loc)
init_block.body = mk_alloc(pass_states.typemap, pass_states.calltypes, lhs,
tuple(size_vars), el_typ, scope, loc)
tuple(size_vars), el_typ, scope, loc,
pass_states.typemap[lhs.name])
body_label = next_label()
body_block = ir.Block(scope, loc)
expr_out_var = ir.Var(scope, mk_unique_var("$expr_out_var"), loc)
Expand Down Expand Up @@ -2049,7 +2069,8 @@ def _numpy_map_to_parfor(self, equiv_set, call_name, lhs, args, kws, expr):
# generate init block and body
init_block = ir.Block(scope, loc)
init_block.body = mk_alloc(pass_states.typemap, pass_states.calltypes, lhs,
tuple(size_vars), el_typ, scope, loc)
tuple(size_vars), el_typ, scope, loc,
pass_states.typemap[lhs.name])
body_label = next_label()
body_block = ir.Block(scope, loc)
expr_out_var = ir.Var(scope, mk_unique_var("$expr_out_var"), loc)
Expand Down