Skip to content

Commit

Permalink
Adds a new dpjit compiler pipeline.
Browse files Browse the repository at this point in the history
   - The new dpjit compiler pipeline uses the new parfor and
     parfor lowering passes.
   - The dpjit deocrator is updated to use the dpjit compiler
     pipeline.
  • Loading branch information
diptorupd committed Feb 28, 2023
1 parent 51a6169 commit 25a34ce
Show file tree
Hide file tree
Showing 2 changed files with 123 additions and 2 deletions.
120 changes: 120 additions & 0 deletions numba_dpex/core/pipelines/dpjit_compiler.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
# SPDX-FileCopyrightText: 2020 - 2022 Intel Corporation
#
# SPDX-License-Identifier: Apache-2.0

from numba.core.compiler import CompilerBase, DefaultPassBuilder
from numba.core.compiler_machinery import PassManager
from numba.core.typed_passes import (
AnnotateTypes,
InlineOverloads,
IRLegalization,
NopythonRewrites,
NoPythonSupportedFeatureValidation,
NopythonTypeInference,
PreLowerStripPhis,
PreParforPass,
)

from numba_dpex.core.exceptions import UnsupportedCompilationModeError
from numba_dpex.core.passes import ParforLoweringPass
from numba_dpex.core.passes.passes import (
DumpParforDiagnostics,
NoPythonBackend,
ParforFusionPass,
ParforPass,
ParforPreLoweringPass,
)
from numba_dpex.parfor_diagnostics import ExtendedParforDiagnostics


class _DpjitPassBuilder(object):
"""
A pass builder for dpex's DpjitCompiler that adds supports for
offloading dpnp array expressions and dpnp library calls to a SYCL device.
The pass builder does not implement pipelines for objectmode or interpreted
execution.
"""

@staticmethod
def define_typed_pipeline(state, name="dpex_dpjit_typed"):
"""Returns the typed part of the nopython pipeline"""
pm = PassManager(name)
# typing
pm.add_pass(NopythonTypeInference, "nopython frontend")
# Annotate only once legalized
pm.add_pass(AnnotateTypes, "annotate types")
# strip phis
pm.add_pass(PreLowerStripPhis, "remove phis nodes")

# optimization
pm.add_pass(InlineOverloads, "inline overloaded functions")
pm.add_pass(PreParforPass, "Preprocessing for parfors")
if not state.flags.no_rewrites:
pm.add_pass(NopythonRewrites, "nopython rewrites")
pm.add_pass(ParforPass, "convert to parfors")
pm.add_pass(ParforFusionPass, "fuse parfors")
pm.add_pass(ParforPreLoweringPass, "parfor prelowering")

pm.finalize()
return pm

@staticmethod
def define_nopython_lowering_pipeline(state, name="dpex_dpjit_lowering"):
"""Returns an nopython mode pipeline based PassManager"""
pm = PassManager(name)

# legalize
pm.add_pass(
NoPythonSupportedFeatureValidation,
"ensure features that are in use are in a valid form",
)
pm.add_pass(IRLegalization, "ensure IR is legal prior to lowering")

# lower
pm.add_pass(ParforLoweringPass, "Custom lowerer for dpex parfor nodes")
pm.add_pass(NoPythonBackend, "nopython mode backend")
pm.add_pass(DumpParforDiagnostics, "dump parfor diagnostics")

pm.finalize()
return pm

@staticmethod
def define_nopython_pipeline(state, name="dpex_dpjit_nopython"):
"""Returns an nopython mode pipeline based PassManager"""
# compose pipeline from untyped, typed and lowering parts
dpb = _DpjitPassBuilder
pm = PassManager(name)
untyped_passes = DefaultPassBuilder.define_untyped_pipeline(state)
pm.passes.extend(untyped_passes.passes)

typed_passes = dpb.define_typed_pipeline(state)
pm.passes.extend(typed_passes.passes)

lowering_passes = dpb.define_nopython_lowering_pipeline(state)
pm.passes.extend(lowering_passes.passes)

pm.finalize()
return pm


class DpjitCompiler(CompilerBase):
"""Dpex's offload compiler pipeline that can offload parfor nodes and
other NumPy-based library calls to a SYCL device.
.. note:: Deprecated in 0.20
The offload compiler is deprecated and will be removed in a future
release.
"""

def define_pipelines(self):
pms = []
self.state.parfor_diagnostics = ExtendedParforDiagnostics()
self.state.metadata[
"parfor_diagnostics"
] = self.state.parfor_diagnostics
if not self.state.flags.force_pyobject:
pms.append(_DpjitPassBuilder.define_nopython_pipeline(self.state))
if self.state.status.can_fallback or self.state.flags.force_pyobject:
raise UnsupportedCompilationModeError()
return pms
5 changes: 3 additions & 2 deletions numba_dpex/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
compile_func,
compile_func_template,
)
from numba_dpex.core.pipelines.offload_compiler import OffloadCompiler
from numba_dpex.core.pipelines.dpjit_compiler import DpjitCompiler


def kernel(
Expand Down Expand Up @@ -165,7 +165,8 @@ def dpjit(*args, **kws):
)
del kws["forceobj"]
kws.update({"nopython": True})
kws.update({"pipeline_class": OffloadCompiler})
kws.update({"parallel": True})
kws.update({"pipeline_class": DpjitCompiler})

# FIXME: When trying to use dpex's target context, overloads do not work
# properly. We will turn on dpex target once the issue is fixed.
Expand Down

0 comments on commit 25a34ce

Please sign in to comment.