Skip to content

Commit db5e318

Browse files
author
Diptorup Deb
committed
Add a decorator-level option to set inlining threshold.
- A new target option "inlining threshold" was added to DpexKernelTarget to define how the LLVM inlining passes should optimize the final codegen library. The decorator-level option will supersede any global configuration setting.
1 parent 4a1b3dc commit db5e318

File tree

3 files changed

+60
-6
lines changed

3 files changed

+60
-6
lines changed

numba_dpex/core/codegen.py

Lines changed: 50 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,25 @@ class SPIRVCodeLibrary(CPUCodeLibrary):
2929
def _optimize_functions(self, ll_module):
3030
pass
3131

32+
@property
33+
def inline_threshold(self):
34+
"""The inlining threshold value to be used to optimize the final library"""
35+
if hasattr(self, "_inline_threshold"):
36+
return self._inline_threshold
37+
else:
38+
return 0
39+
40+
@inline_threshold.setter
41+
def inline_threshold(self, value: int):
42+
"""Returns the current inlining threshold level for the library."""
43+
if value < 0 or value > 3:
44+
logging.warning(
45+
"Unsupported inline threshold. Set a value between 0 and 3"
46+
)
47+
self._inline_threshold = 0
48+
else:
49+
self._inline_threshold = value
50+
3251
def _optimize_final_module(self):
3352
# Run some lightweight optimization to simplify the module.
3453
pmb = ll.PassManagerBuilder()
@@ -43,12 +62,38 @@ def _optimize_final_module(self):
4362
)
4463

4564
pmb.disable_unit_at_a_time = False
65+
4666
if config.INLINE_THRESHOLD is not None:
47-
logging.warning(
48-
"Setting INLINE_THRESHOLD leads to very aggressive "
49-
+ "optimizations that may produce incorrect binary."
50-
)
51-
pmb.inlining_threshold = config.INLINE_THRESHOLD
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
96+
5297
pmb.disable_unroll_loops = True
5398
pmb.loop_vectorize = False
5499
pmb.slp_vectorize = False

numba_dpex/core/descriptor.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ class DpexTargetOptions(CPUTargetOptions):
4141
release_gil = _option_mapping("release_gil")
4242
no_compile = _option_mapping("no_compile")
4343
use_mlir = _option_mapping("use_mlir")
44+
inline_threshold = _option_mapping("inline_threshold")
4445
_compilation_mode = _option_mapping("_compilation_mode")
4546

4647
def finalize(self, flags, options):
@@ -49,6 +50,7 @@ def finalize(self, flags, options):
4950
_inherit_if_not_set(flags, options, "release_gil", False)
5051
_inherit_if_not_set(flags, options, "no_compile", True)
5152
_inherit_if_not_set(flags, options, "use_mlir", False)
53+
_inherit_if_not_set(flags, options, "inline_threshold", 0)
5254
_inherit_if_not_set(
5355
flags, options, "_compilation_mode", CompilationMode.KERNEL
5456
)

numba_dpex/experimental/kernel_dispatcher.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,14 @@ def _compile_to_spirv(
8484
kernel_fn = kernel_targetctx.prepare_spir_kernel(
8585
kernel_func, kernel_fndesc.argtypes
8686
)
87-
87+
# If the inline_threshold option was set then set the property in the
88+
# kernel_library to force inlining ``overload`` calls into a kernel.
89+
try:
90+
kernel_library.inline_threshold = self.targetoptions[
91+
"inline_threshold"
92+
]
93+
except KeyError:
94+
pass
8895
# Call finalize on the LLVM module. Finalization will result in
8996
# all linking libraries getting linked together and final optimization
9097
# including inlining of functions if an inlining level is specified.

0 commit comments

Comments
 (0)