Skip to content

Commit f4b618f

Browse files
author
Diptorup Deb
authored
Merge pull request #1238 from IntelPython/fix/leftover_duplicate_code
Removes code duplication and adds new unit tests.
2 parents 8fc7fbd + cc4593f commit f4b618f

File tree

5 files changed

+79
-53
lines changed

5 files changed

+79
-53
lines changed

numba_dpex/core/codegen.py

Lines changed: 18 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#
33
# SPDX-License-Identifier: Apache-2.0
44

5-
import logging
5+
import warnings
66

77
from llvmlite import binding as ll
88
from llvmlite import ir as llvmir
@@ -31,7 +31,9 @@ def _optimize_functions(self, ll_module):
3131

3232
@property
3333
def inline_threshold(self):
34-
"""The inlining threshold value to be used to optimize the final library"""
34+
"""
35+
The inlining threshold value to be used to optimize the final library.
36+
"""
3537
if hasattr(self, "_inline_threshold"):
3638
return self._inline_threshold
3739
else:
@@ -41,11 +43,17 @@ def inline_threshold(self):
4143
def inline_threshold(self, value: int):
4244
"""Returns the current inlining threshold level for the library."""
4345
if value < 0 or value > 3:
44-
logging.warning(
46+
warnings.warn(
4547
"Unsupported inline threshold. Set a value between 0 and 3"
4648
)
4749
self._inline_threshold = 0
4850
else:
51+
if value == 3:
52+
warnings.warn(
53+
"Due to an existing compiler bug, setting INLINE_THRESHOLD "
54+
f"to {value} can lead to incorrect code generation on "
55+
"certain devices."
56+
)
4957
self._inline_threshold = value
5058

5159
def _optimize_final_module(self):
@@ -55,44 +63,20 @@ def _optimize_final_module(self):
5563
# Make optimization level depending on config.DPEX_OPT variable
5664
pmb.opt_level = config.DPEX_OPT
5765
if config.DPEX_OPT > 2:
58-
logging.warning(
66+
warnings.warn(
5967
"Setting NUMBA_DPEX_OPT greater than 2 known to cause issues "
6068
+ "related to very aggressive optimizations that leads to "
6169
+ "broken code."
6270
)
6371

6472
pmb.disable_unit_at_a_time = False
6573

66-
if config.INLINE_THRESHOLD is not None:
67-
# Check if a decorator-level inline threshold was set and use that
68-
# instead of the global configuration.
69-
if (
70-
hasattr(self, "_inline_threshold")
71-
and self._inline_threshold > 0
72-
and self._inline_threshold <= 3
73-
):
74-
logging.warning(
75-
"Setting INLINE_THRESHOLD leads to very aggressive "
76-
+ "optimizations that may produce incorrect binary."
77-
)
78-
pmb.inlining_threshold = self._inline_threshold
79-
elif not hasattr(self, "_inline_threshold"):
80-
logging.warning(
81-
"Setting INLINE_THRESHOLD leads to very aggressive "
82-
+ "optimizations that may produce incorrect binary."
83-
)
84-
pmb.inlining_threshold = config.INLINE_THRESHOLD
85-
else:
86-
if (
87-
hasattr(self, "_inline_threshold")
88-
and self._inline_threshold > 0
89-
and self._inline_threshold <= 3
90-
):
91-
logging.warning(
92-
"Setting INLINE_THRESHOLD leads to very aggressive "
93-
+ "optimizations that may produce incorrect binary."
94-
)
95-
pmb.inlining_threshold = self._inline_threshold
74+
# The PassManagerBuilder's inlining_threshold property is set only when
75+
# inline_threshold is g.t. 0. Doing otherwise, *i.e.*, setting the
76+
# pmb.inlining_threshold to 0 will lead to at minimum `alwaysinline`
77+
# pass to run.
78+
if self.inline_threshold > 0:
79+
pmb.inlining_threshold = self.inline_threshold
9680

9781
pmb.disable_unroll_loops = True
9882
pmb.loop_vectorize = False

numba_dpex/core/kernel_interface/spirv_kernel.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,15 @@ def compile(
136136
kernel = cres.target_context.prepare_spir_kernel(
137137
func, cres.signature.args
138138
)
139+
140+
# XXX: Setting the inline_threshold in the following way is a temporary
141+
# workaround till the JitKernel dispatcher is replaced by
142+
# experimental.dispatcher.KernelDispatcher.
143+
if config.INLINE_THRESHOLD is not None:
144+
cres.library.inline_threshold = config.INLINE_THRESHOLD
145+
else:
146+
cres.library.inline_threshold = 0
147+
139148
cres.library._optimize_final_module()
140149
self._llvm_module = kernel.module.__str__()
141150
self._module_name = kernel.name

numba_dpex/tests/experimental/test_compiler_warnings.py

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,33 @@
22
#
33
# SPDX-License-Identifier: Apache-2.0
44

5-
import warnings
6-
5+
import dpctl
76
import pytest
7+
from numba.core import types
88

99
import numba_dpex as dpex
10+
from numba_dpex import DpctlSyclQueue, DpnpNdArray
1011
from numba_dpex import experimental as dpex_exp
12+
from numba_dpex import int64
13+
14+
15+
def _kernel(a, b, c):
16+
i = dpex.get_global_id(0)
17+
c[i] = a[i] + b[i]
1118

1219

1320
def test_compilation_mode_option_user_definition():
14-
def kernel_func(a, b, c):
15-
i = dpex.get_global_id(0)
16-
c[i] = a[i] + b[i]
21+
with pytest.warns(UserWarning):
22+
dpex_exp.kernel(_compilation_mode="kernel")(_kernel)
23+
24+
25+
def test_inline_threshold_level_warning():
26+
"""
27+
Test compiler warning generation with an inline_threshold value of 3.
28+
"""
1729

18-
with pytest.warns(warnings.warn(UserWarning)):
19-
dpex_exp.kernel(_compilation_mode="kernel")(kernel_func)
30+
with pytest.warns(UserWarning):
31+
queue_ty = DpctlSyclQueue(dpctl.SyclQueue())
32+
i64arr_ty = DpnpNdArray(ndim=1, dtype=int64, layout="C", queue=queue_ty)
33+
kernel_sig = types.void(i64arr_ty, i64arr_ty, i64arr_ty)
34+
dpex_exp.kernel(inline_threshold=3)(_kernel).compile(kernel_sig)

numba_dpex/tests/experimental/test_inline_threshold_config.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@ def test_inline_threshold_set_using_config():
3434

3535

3636
def test_inline_threshold_set_using_decorator_option():
37-
"""Test setting the inline_threshold value using the kernel decorator flag"""
37+
"""
38+
Test setting the inline_threshold value using the kernel decorator flag
39+
"""
3840

3941
disp = dpex_exp.kernel(inline_threshold=2)(kernel_func)
4042
flags = compiler.Flags()
Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import logging
1+
import warnings
22

33
import dpnp
4+
import pytest
45

56
import numba_dpex as dpex
67
import numba_dpex.config as config
@@ -11,32 +12,47 @@ def foo(a):
1112
a[dpex.get_global_id(0)] = 0
1213

1314

14-
def test_opt_warning(caplog):
15+
def test_opt_warning():
1516
bkp = config.DPEX_OPT
1617
config.DPEX_OPT = 3
1718

18-
with caplog.at_level(logging.WARNING):
19+
with pytest.warns(UserWarning):
1920
foo[dpex.Range(10)](dpnp.arange(10))
2021

2122
config.DPEX_OPT = bkp
2223

23-
assert "NUMBA_DPEX_OPT" in caplog.text
24+
25+
def test_inline_threshold_eq_3_warning():
26+
bkp = config.INLINE_THRESHOLD
27+
config.INLINE_THRESHOLD = 3
28+
29+
with pytest.warns(UserWarning):
30+
foo[dpex.Range(10)](dpnp.arange(10))
31+
32+
config.INLINE_THRESHOLD = bkp
2433

2534

26-
def test_inline_warning(caplog):
35+
def test_inline_threshold_negative_val_warning_():
2736
bkp = config.INLINE_THRESHOLD
28-
config.INLINE_THRESHOLD = 2
37+
config.INLINE_THRESHOLD = -1
2938

30-
with caplog.at_level(logging.WARNING):
39+
with pytest.warns(UserWarning):
3140
foo[dpex.Range(10)](dpnp.arange(10))
3241

3342
config.INLINE_THRESHOLD = bkp
3443

35-
assert "INLINE_THRESHOLD" in caplog.text
3644

45+
def test_inline_threshold_gt_3_warning():
46+
bkp = config.INLINE_THRESHOLD
47+
config.INLINE_THRESHOLD = 4
3748

38-
def test_no_warning(caplog):
39-
with caplog.at_level(logging.WARNING):
49+
with pytest.warns(UserWarning):
4050
foo[dpex.Range(10)](dpnp.arange(10))
4151

42-
assert caplog.text == ""
52+
config.INLINE_THRESHOLD = bkp
53+
54+
55+
def test_no_warning():
56+
with warnings.catch_warnings():
57+
warnings.simplefilter("error")
58+
foo[dpex.Range(10)](dpnp.arange(10))

0 commit comments

Comments
 (0)