Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion numba/core/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
NopythonRewrites, PreParforPass,
ParforPass, DumpParforDiagnostics,
IRLegalization, NoPythonBackend,
InlineOverloads, PreLowerStripPhis)
InlineOverloads, PreLowerStripPhis,
NativeLowering)

from numba.core.object_mode_passes import (ObjectModeFrontEnd,
ObjectModeBackEnd)
Expand Down Expand Up @@ -476,6 +477,7 @@ def define_nopython_lowering_pipeline(state, name='nopython_lowering'):
"ensure IR is legal prior to lowering")

# lower
pm.add_pass(NativeLowering, "native lowering")
pm.add_pass(NoPythonBackend, "nopython mode backend")
pm.add_pass(DumpParforDiagnostics, "dump parfor diagnostics")
pm.finalize()
Expand Down
18 changes: 8 additions & 10 deletions numba/core/typed_passes.py
Original file line number Diff line number Diff line change
Expand Up @@ -358,8 +358,15 @@ def __init__(self):
LoweringPass.__init__(self)

def run_pass(self, state):
targetctx = state.targetctx
if state.library is None:
codegen = state.targetctx.codegen()
state.library = codegen.create_library(state.func_id.func_qualname)
# Enable object caching upfront, so that the library can
# be later serialized.
state.library.enable_object_caching()

library = state.library
targetctx = state.targetctx
interp = state.func_ir # why is it called this?!
typemap = state.typemap
restype = state.return_type
Expand Down Expand Up @@ -452,15 +459,6 @@ def run_pass(self, state):
"""
Back-end: Generate LLVM IR from Numba IR, compile to machine code
"""
if state.library is None:
codegen = state.targetctx.codegen()
state.library = codegen.create_library(state.func_id.func_qualname)
# Enable object caching upfront, so that the library can
# be later serialized.
state.library.enable_object_caching()

# TODO: Pull this out into the pipeline
NativeLowering().run_pass(state)
lowered = state['cr']
signature = typing.signature(state.return_type, *state.args)

Expand Down
67 changes: 66 additions & 1 deletion numba/cuda/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,14 @@
from numba.core import (types, typing, utils, funcdesc, serialize, config,
compiler, sigutils)
from numba.core.typeconv.rules import default_type_manager
from numba.core.compiler import (CompilerBase, DefaultPassBuilder,
compile_result)
from numba.core.compiler_lock import global_compiler_lock
from numba.core.compiler_machinery import (LoweringPass, PassManager,
register_pass)
from numba.core.dispatcher import OmittedArg
from numba.core.errors import NumbaDeprecationWarning
from numba.core.typed_passes import IRLegalization, NativeLowering
from numba.core.typing.typeof import Purpose, typeof
from warnings import warn
import numba
Expand All @@ -28,6 +33,65 @@
from .args import wrap_arg


@register_pass(mutates_CFG=True, analysis_only=False)
class CUDABackend(LoweringPass):

_name = "cuda_backend"

def __init__(self):
LoweringPass.__init__(self)

def run_pass(self, state):
"""
Back-end: Packages lowering output in a compile result
"""
lowered = state['cr']
signature = typing.signature(state.return_type, *state.args)

state.cr = compile_result(
typing_context=state.typingctx,
target_context=state.targetctx,
typing_error=state.status.fail_reason,
type_annotation=state.type_annotation,
library=state.library,
call_helper=lowered.call_helper,
signature=signature,
fndesc=lowered.fndesc,
)
return True


class CUDACompiler(CompilerBase):
def define_pipelines(self):
dpb = DefaultPassBuilder
pm = PassManager('cuda')

untyped_passes = dpb.define_untyped_pipeline(self.state)
pm.passes.extend(untyped_passes.passes)

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

lowering_passes = self.define_cuda_lowering_pipeline(self.state)
pm.passes.extend(lowering_passes.passes)

pm.finalize()
return [pm]

def define_cuda_lowering_pipeline(self, state):
pm = PassManager('cuda_lowering')
# legalise
pm.add_pass(IRLegalization,
"ensure IR is legal prior to lowering")

# lower
pm.add_pass(NativeLowering, "native lowering")
pm.add_pass(CUDABackend, "cuda backend")

pm.finalize()
return pm


@global_compiler_lock
def compile_cuda(pyfunc, return_type, args, debug=False, inline=False):
# First compilation will trigger the initialization of the CUDA backend.
Expand All @@ -52,7 +116,8 @@ def compile_cuda(pyfunc, return_type, args, debug=False, inline=False):
args=args,
return_type=return_type,
flags=flags,
locals={})
locals={},
pipeline_class=CUDACompiler)

library = cres.library
library.finalize()
Expand Down
3 changes: 2 additions & 1 deletion numba/tests/test_errorhandling.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from numba.core.untyped_passes import (TranslateByteCode, FixupArgs,
IRProcessing,)
from numba.core.typed_passes import (NopythonTypeInference, DeadCodeElimination,
NoPythonBackend)
NoPythonBackend, NativeLowering)
from numba.core.compiler_machinery import PassManager
from numba.core.types.functions import _err_reasons as error_reasons

Expand Down Expand Up @@ -110,6 +110,7 @@ def define_pipelines(self):
pm.add_pass(DeadCodeElimination, "DCE")
# typing
pm.add_pass(NopythonTypeInference, "nopython frontend")
pm.add_pass(NativeLowering, "native lowering")
pm.add_pass(NoPythonBackend, "nopython mode backend")
pm.finalize()
return [pm]
Expand Down
3 changes: 2 additions & 1 deletion numba/tests/test_inlining.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from numba.core.typed_passes import (NopythonTypeInference, AnnotateTypes,
NopythonRewrites, PreParforPass, ParforPass,
DumpParforDiagnostics, NativeLowering,
IRLegalization, NoPythonBackend)
IRLegalization, NoPythonBackend, NativeLowering)

from numba.core.compiler_machinery import FunctionPass, PassManager, register_pass
import unittest
Expand Down Expand Up @@ -98,6 +98,7 @@ def gen_pipeline(state, test_pass):
pm.add_pass(PreserveIR, "preserve IR")

# lower
pm.add_pass(NativeLowering, "native lowering")
pm.add_pass(NoPythonBackend, "nopython mode backend")
pm.add_pass(DumpParforDiagnostics, "dump parfor diagnostics")
return pm
Expand Down
5 changes: 4 additions & 1 deletion numba/tests/test_mixed_tuple_unroller.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
SimplifyCFG, IterLoopCanonicalization,
LiteralUnroll, PreserveIR)
from numba.core.typed_passes import (NopythonTypeInference, IRLegalization,
NoPythonBackend, PartialTypeInference)
NoPythonBackend, PartialTypeInference,
NativeLowering)
from numba.core.ir_utils import (compute_cfg_from_blocks, flatten_labels)
from numba.core.types.functions import _header_lead

Expand Down Expand Up @@ -108,6 +109,7 @@ def define_pipelines(self):
pm.add_pass(PreserveIR, "save IR for later inspection")

# lower
pm.add_pass(NativeLowering, "native lowering")
pm.add_pass(NoPythonBackend, "nopython mode backend")

# finalise the contents
Expand Down Expand Up @@ -1859,6 +1861,7 @@ def add_pass(x, y):
"ensure IR is legal prior to lowering")

# lower
add_pass(NativeLowering, "native lowering")
add_pass(NoPythonBackend, "nopython mode backend")
pm.finalize()
return [pm]
Expand Down
3 changes: 2 additions & 1 deletion numba/tests/test_remove_dead.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
from numba.core.typed_passes import (NopythonTypeInference, AnnotateTypes,
NopythonRewrites, PreParforPass, ParforPass,
DumpParforDiagnostics, NativeLowering,
IRLegalization, NoPythonBackend)
IRLegalization, NoPythonBackend, NativeLowering)
import numpy as np
from numba.tests.support import skip_parfors_unsupported, needs_blas
import unittest
Expand Down Expand Up @@ -288,6 +288,7 @@ def define_pipelines(self):
pm.add_pass(AnnotateTypes, "annotate types")

# lower
pm.add_pass(NativeLowering, "native lowering")
pm.add_pass(NoPythonBackend, "nopython mode backend")
pm.finalize()
return [pm]
Expand Down