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

Split optimisation passes. #6335

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
33 changes: 25 additions & 8 deletions numba/core/codegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -520,8 +520,13 @@ def _optimize_final_module(self):
"""
Internal: optimize this library's final module.
"""
self._codegen._mpm.run(self._final_module)
# A cheaper optimisation pass is run first to try and get as many
# refops into the same function as possible via inlining
self._codegen._mpm_cheap.run(self._final_module)
# Refop pruning is then run on the heavily inlined function
self._final_module = remove_redundant_nrt_refct(self._final_module)
# The full optimisation suite is then run on the refop pruned IR
self._codegen._mpm_full.run(self._final_module)

def _get_module_for_linking(self):
"""
Expand Down Expand Up @@ -1037,7 +1042,9 @@ def _init(self, llvm_module):
self._engine = JitEngine(engine)
self._target_data = engine.target_data
self._data_layout = str(self._target_data)
self._mpm = self._module_pass_manager()
self._mpm_cheap = self._module_pass_manager(loop_vectorize=False,
opt=2)
Copy link
Member

Choose a reason for hiding this comment

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

Just curious... why O2 and not O1?

Also, do we need to override the inlining_threshold? e.g. cheap and full run has different threshold.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Was think that producing more optimised code might let the refpruner run quicker and also permit more inlining if the complexity is reduce. Turns out in some checks @esc did that O2 massively increases compile time, whereas O1 increases it a small bit, but both cases leading to huge performance gains, so I think O1 is probably the way to go for now. RE inline threshold, I've been thinking lately that it'd be a good idea to put more of these "trade-off" options into the hands of users, some will want to optimise something as much as possible regardless of the compilation cost, others will want to optimise for short compilation times, others may be inbetween!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

4cf27a9 moves to O1

self._mpm_full = self._module_pass_manager()

self._engine.set_object_cache(self._library_class._object_compiled_hook,
self._library_class._object_getbuffer_hook)
Expand Down Expand Up @@ -1066,21 +1073,21 @@ def create_library(self, name):
def unserialize_library(self, serialized):
return self._library_class._unserialize(self, serialized)

def _module_pass_manager(self):
def _module_pass_manager(self, **kwargs):
pm = ll.create_module_pass_manager()
self._tm.add_analysis_passes(pm)
with self._pass_manager_builder() as pmb:
with self._pass_manager_builder(**kwargs) as pmb:
pmb.populate(pm)
return pm

def _function_pass_manager(self, llvm_module):
def _function_pass_manager(self, llvm_module, **kwargs):
pm = ll.create_function_pass_manager(llvm_module)
self._tm.add_analysis_passes(pm)
with self._pass_manager_builder() as pmb:
with self._pass_manager_builder(**kwargs) as pmb:
pmb.populate(pm)
return pm

def _pass_manager_builder(self):
def _pass_manager_builder(self, **kwargs):
"""
Create a PassManagerBuilder.

Expand All @@ -1089,8 +1096,18 @@ def _pass_manager_builder(self):
or function pass manager. Otherwise some optimizations will be
missed...
"""
if 'opt' in kwargs:
opt_level = kwargs.pop('opt')
else:
opt_level = config.OPT
stuartarchibald marked this conversation as resolved.
Show resolved Hide resolved

if 'loop_vectorize' in kwargs:
loop_vectorize = kwargs.pop('loop_vectorize')
else:
loop_vectorize = config.LOOP_VECTORIZE
stuartarchibald marked this conversation as resolved.
Show resolved Hide resolved

pmb = lp.create_pass_manager_builder(
opt=config.OPT, loop_vectorize=config.LOOP_VECTORIZE)
opt=opt_level, loop_vectorize=loop_vectorize, **kwargs)
return pmb

def _check_llvm_bugs(self):
Expand Down
36 changes: 36 additions & 0 deletions numba/tests/test_vectorization.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import numpy as np
from numba import njit, types
from unittest import TestCase
from numba.tests.support import override_env_config

_DEBUG = False
if _DEBUG:
from llvmlite import binding as llvm
# Prints debug info from the LLVMs vectorizer
llvm.set_option("", "--debug-only=loop-vectorize")


class TestVectorization(TestCase):
"""
Tests to assert that code which should vectorize does indeed vectorize
"""
def gen_ir(self, func, args_tuple, **flags):
with override_env_config(
"NUMBA_CPU_NAME", "skylake-avx512"
), override_env_config("NUMBA_CPU_FEATURES", ""):
jobj = njit(**flags)(func)
jobj.compile(args_tuple)
ol = jobj.overloads[jobj.signatures[0]]
return ol.library.get_llvm_str()

def test_nditer_loop(self):
# see https://github.com/numba/numba/issues/5033
def do_sum(x):
acc = 0
for v in np.nditer(x):
acc += v.item()
return acc

llvm_ir = self.gen_ir(do_sum, (types.float64[::1],), fastmath=True)
self.assertIn("vector.body", llvm_ir)
self.assertIn("llvm.loop.isvectorized", llvm_ir)