Skip to content

Commit

Permalink
Propagate semantic constants ahead of static rewrites.
Browse files Browse the repository at this point in the history
This makes it such that semantic constant propagation happens
before any rewrites for e.g. static getitem are performed to
ensure that there are the most constants possible in the IR and
therefore the maximum opportunities for rewrites to spot them.

Fixes numba#5015
  • Loading branch information
stuartarchibald committed Jan 3, 2020
1 parent 68c8900 commit 2e48486
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
2 changes: 1 addition & 1 deletion numba/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -441,9 +441,9 @@ def define_nopython_pipeline(state, name='nopython'):

# pre typing
if not state.flags.no_rewrites:
pm.add_pass(GenericRewrites, "nopython rewrites")
pm.add_pass(RewriteSemanticConstants, "rewrite semantic constants")
pm.add_pass(DeadBranchPrune, "dead branch pruning")
pm.add_pass(GenericRewrites, "nopython rewrites")

pm.add_pass(InlineClosureLikes,
"inline calls to locally defined closures")
Expand Down
11 changes: 11 additions & 0 deletions numba/tests/test_analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -633,3 +633,14 @@ def impl(fa):
FakeArrayType = types.NamedUniTuple(types.int64, 1, FakeArray)
self.assert_prune(impl, (FakeArrayType,), [None], fa,
flags=enable_pyobj_flags)

def test_semantic_const_propagates_before_static_rewrites(self):
# see issue #5015, the ndim needs writing in as a const before
# the rewrite passes run to make e.g. getitems static where possible
@njit
def impl(a, b):
return a.shape[:b.ndim]

args = (np.zeros((5, 4, 3, 2)), np.zeros((1, 1)))

self.assertPreciseEqual(impl(*args), impl.py_func(*args))

0 comments on commit 2e48486

Please sign in to comment.