Skip to content

Commit

Permalink
Add TargetLibraryInfo pass to CPU LLVM pipeline.
Browse files Browse the repository at this point in the history
The TLI pass makes sure that the optimisations that take place
during call simplification are appropriate for the target, without
this the target is assumed to be Linux and code will be optimised
to produce e.g. math symbols that do not exit on Windows.

This addresses numba#8898 in a more performant manner, it is almost a
total revert of numba#8907 (original fix for numba#8898), only the test
cases are kept.
  • Loading branch information
stuartarchibald committed Dec 6, 2023
1 parent 5240e79 commit d12f718
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 44 deletions.
11 changes: 11 additions & 0 deletions docs/upcoming_changes/9336.improvement.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Add ``TargetLibraryInfo`` pass to CPU LLVM pipeline.
""""""""""""""""""""""""""""""""""""""""""""""""""""

The ``TargetLibraryInfo`` pass makes sure that the optimisations that take place
during call simplification are appropriate for the target, without this the
target is assumed to be Linux and code will be optimised to produce e.g. math
symbols that do not exit on Windows. Historically this issue has been avoided
through the use of Numba internal libraries carrying wrapped symbols, but doing
so potentially detriments performance. As a result of this change Numba internal
libraries are smaller and there is an increase in optimisation opportunity in
code using ``exp2`` and ``log2`` functions.
24 changes: 0 additions & 24 deletions numba/_helperlib.c
Original file line number Diff line number Diff line change
Expand Up @@ -122,30 +122,6 @@ numba_ldexpf(float x, int exp)
return x;
}

NUMBA_EXPORT_FUNC(double)
numba_exp2(double x)
{
return exp2(x);
}

NUMBA_EXPORT_FUNC(float)
numba_exp2f(float x)
{
return exp2f(x);
}

NUMBA_EXPORT_FUNC(double)
numba_log2(double x)
{
return log2(x);
}

NUMBA_EXPORT_FUNC(float)
numba_log2f(float x)
{
return log2f(x);
}

/* provide complex power */
NUMBA_EXPORT_FUNC(void)
numba_cpow(Py_complex *a, Py_complex *b, Py_complex *out) {
Expand Down
4 changes: 0 additions & 4 deletions numba/_helpermod.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,6 @@ build_c_helpers_dict(void)
declmethod(frexpf);
declmethod(ldexp);
declmethod(ldexpf);
declmethod(exp2);
declmethod(exp2f);
declmethod(log2);
declmethod(log2f);
declmethod(cpow);
declmethod(cpowf);
declmethod(erf);
Expand Down
2 changes: 2 additions & 0 deletions numba/core/codegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -1225,6 +1225,7 @@ def _create_empty_module(self, name):

def _module_pass_manager(self, **kwargs):
pm = ll.create_module_pass_manager()
pm.add_target_library_info(ll.get_process_triple())
self._tm.add_analysis_passes(pm)
cost = kwargs.pop("cost", None)
with self._pass_manager_builder(**kwargs) as pmb:
Expand Down Expand Up @@ -1256,6 +1257,7 @@ def _module_pass_manager(self, **kwargs):

def _function_pass_manager(self, llvm_module, **kwargs):
pm = ll.create_function_pass_manager(llvm_module)
pm.add_target_library_info(llvm_module.triple)
self._tm.add_analysis_passes(pm)
with self._pass_manager_builder(**kwargs) as pmb:
pmb.populate(pm)
Expand Down
24 changes: 10 additions & 14 deletions numba/np/npyfuncs.py
Original file line number Diff line number Diff line change
Expand Up @@ -648,13 +648,11 @@ def np_complex_exp_impl(context, builder, sig, args):
def np_real_exp2_impl(context, builder, sig, args):
_check_arity_and_homogeneity(sig, args, 1)

dispatch_table = {
types.float32: 'numba_exp2f',
types.float64: 'numba_exp2',
}

return _dispatch_func_by_name_type(context, builder, sig, args,
dispatch_table, 'exp2')
ll_ty = args[0].type
fnty = llvmlite.ir.FunctionType(ll_ty, [ll_ty,])
fn = cgutils.insert_pure_function(builder.module, fnty,
name='llvm.exp2')
return builder.call(fn, [args[0]])


def np_complex_exp2_impl(context, builder, sig, args):
Expand Down Expand Up @@ -687,13 +685,11 @@ def np_complex_log_impl(context, builder, sig, args):
def np_real_log2_impl(context, builder, sig, args):
_check_arity_and_homogeneity(sig, args, 1)

dispatch_table = {
types.float32: 'numba_log2f',
types.float64: 'numba_log2',
}

return _dispatch_func_by_name_type(context, builder, sig, args,
dispatch_table, 'log2')
ll_ty = args[0].type
fnty = llvmlite.ir.FunctionType(ll_ty, [ll_ty,])
fn = cgutils.insert_pure_function(builder.module, fnty,
name='llvm.log2')
return builder.call(fn, [args[0]])

def np_complex_log2_impl(context, builder, sig, args):
_check_arity_and_homogeneity(sig, args, 1)
Expand Down
4 changes: 2 additions & 2 deletions numba/tests/npyufunc/test_ufunc.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ class TestUFuncsMisc(TestCase):
# Test for miscellaneous ufunc issues

def test_exp2(self):
# See issue #8898
# See issue #8898, and TargetLibraryInfo based fix in #9336
@njit
def foo(x):
return np.exp2(x)
Expand All @@ -157,7 +157,7 @@ def foo(x):
self.assertPreciseEqual(expected, got)

def test_log2(self):
# See issue #8898
# See issue #8898, and TargetLibraryInfo based fix in #9336
@njit
def foo(x):
return np.log2(x)
Expand Down

0 comments on commit d12f718

Please sign in to comment.