2020-07-11 08:36:32 +08:00
|
|
|
# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
# See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
|
|
|
2020-07-11 13:45:30 +08:00
|
|
|
import os
|
|
|
|
|
2020-11-11 13:38:13 +08:00
|
|
|
from mlir.ir import *
|
|
|
|
from mlir.passmanager import *
|
|
|
|
from _npcomp import register_dialects
|
|
|
|
from _npcomp import mlir as legacy_mlir
|
|
|
|
from npcomp.compiler.generic.backend import refjit as refjit_backend
|
2020-11-11 10:03:25 +08:00
|
|
|
from npcomp.compiler.utils import logging
|
2020-07-11 08:36:32 +08:00
|
|
|
|
|
|
|
__all__ = [
|
|
|
|
"is_enabled",
|
|
|
|
"CompilerBackend",
|
|
|
|
]
|
|
|
|
|
|
|
|
FRONTEND_PASSES = (
|
2020-11-11 13:38:13 +08:00
|
|
|
"func(npcomp-cpa-type-inference)",
|
2020-07-11 08:36:32 +08:00
|
|
|
"numpy-public-functions-to-tensor",
|
2020-11-11 13:38:13 +08:00
|
|
|
"func(convert-numpy-to-tcf)",
|
|
|
|
"func(convert-scf-to-std)",
|
|
|
|
"func(canonicalize)",
|
|
|
|
"func(tcf-shape-refinement)",
|
2020-07-11 08:36:32 +08:00
|
|
|
)
|
|
|
|
|
2020-11-11 13:38:13 +08:00
|
|
|
# Re-export.
|
|
|
|
is_enabled = refjit_backend.is_enabled
|
2020-07-11 13:45:30 +08:00
|
|
|
|
|
|
|
|
2020-07-11 08:36:32 +08:00
|
|
|
class CompilerBackend:
|
|
|
|
"""Main entry-point for the backend."""
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
super().__init__()
|
2020-11-11 13:38:13 +08:00
|
|
|
self._refjit = refjit_backend.get_refjit()
|
2020-07-11 08:36:32 +08:00
|
|
|
self._debug = logging.debug_enabled()
|
|
|
|
|
2020-11-11 13:38:13 +08:00
|
|
|
def compile(self, legacy_imported_ir_module: legacy_mlir.ir.ModuleOp):
|
2020-07-11 08:36:32 +08:00
|
|
|
"""Compiles an imported module.
|
|
|
|
|
|
|
|
Args:
|
2020-11-11 13:38:13 +08:00
|
|
|
legacy_imported_ir_module: The MLIR module as imported from the
|
|
|
|
ImportFrontend.
|
2020-07-11 08:36:32 +08:00
|
|
|
Returns:
|
|
|
|
An opaque, backend specific module object that can be passed to load.
|
|
|
|
The object may actually be something more specific to the backend (i.e.
|
|
|
|
for IREE, it is a serialized VM flatbuffer) but the contract is that
|
|
|
|
it is operated on by methods on this class.
|
|
|
|
"""
|
2020-11-11 13:38:13 +08:00
|
|
|
# TODO: Once transitioned to new Python API, don't reparse the module.
|
|
|
|
with Context() as context:
|
|
|
|
register_dialects(context)
|
|
|
|
imported_module = Module.parse(legacy_imported_ir_module.to_asm())
|
|
|
|
# Frontend.
|
|
|
|
pm = PassManager.parse(",".join(FRONTEND_PASSES))
|
|
|
|
pm.run(imported_module)
|
|
|
|
if self._debug:
|
|
|
|
logging.debug("Frontend IR:{}", imported_module)
|
|
|
|
|
|
|
|
# Backend.
|
|
|
|
# Note that this is a separate pass manager purely to aid in debugging.
|
|
|
|
pm = PassManager()
|
|
|
|
self._refjit.build_backend_compilation_pipeline(pm)
|
|
|
|
pm.run(imported_module)
|
|
|
|
if self._debug:
|
|
|
|
logging.debug("Backend IR:{}", imported_module)
|
2020-07-11 12:51:03 +08:00
|
|
|
|
2020-07-11 13:45:30 +08:00
|
|
|
jit_module = self._refjit.JITModule.from_compiled_module(
|
2020-11-11 13:38:13 +08:00
|
|
|
imported_module, refjit_backend.get_runtime_libs())
|
2020-07-11 08:36:32 +08:00
|
|
|
return jit_module
|
|
|
|
|
|
|
|
def load(self, jit_module):
|
|
|
|
"""Loads a compiled artifact into the runtime.
|
|
|
|
|
|
|
|
Since this is a JIT instead of an AOT compiler,
|
|
|
|
"""
|
2020-11-11 13:38:13 +08:00
|
|
|
return refjit_backend.JitModuleInvoker(jit_module)
|