Skip to content

Commit b3b3c71

Browse files
author
Diptorup Deb
committed
Removes code duplication and adds new unit tests.
- Removes duplicate code inside codegen module. - Adds a unit test to make sure a compiler warning is generated when the inline_threshold is set to >=3
1 parent 8fc7fbd commit b3b3c71

File tree

3 files changed

+38
-41
lines changed

3 files changed

+38
-41
lines changed

numba_dpex/core/codegen.py

Lines changed: 13 additions & 33 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,7 +43,7 @@ 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
@@ -55,44 +57,22 @@ def _optimize_final_module(self):
5557
# Make optimization level depending on config.DPEX_OPT variable
5658
pmb.opt_level = config.DPEX_OPT
5759
if config.DPEX_OPT > 2:
58-
logging.warning(
60+
warnings.warn(
5961
"Setting NUMBA_DPEX_OPT greater than 2 known to cause issues "
6062
+ "related to very aggressive optimizations that leads to "
6163
+ "broken code."
6264
)
6365

6466
pmb.disable_unit_at_a_time = False
6567

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."
68+
if hasattr(self, "_inline_threshold") and self._inline_threshold > 0:
69+
if self._inline_threshold >= 3:
70+
warnings.warn(
71+
"Due to an existing compiler bug, setting INLINE_THRESHOLD "
72+
f"to {self._inline_threshold} can lead to incorrect "
73+
"code generation on certain devices."
9474
)
95-
pmb.inlining_threshold = self._inline_threshold
75+
pmb.inlining_threshold = self._inline_threshold
9676

9777
pmb.disable_unroll_loops = True
9878
pmb.loop_vectorize = False

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()

0 commit comments

Comments
 (0)