2020-06-20 08:17:22 +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-11-14 10:06:59 +08:00
|
|
|
import io
|
|
|
|
import subprocess
|
|
|
|
|
|
|
|
from mlir.ir import *
|
|
|
|
from mlir.passmanager import *
|
|
|
|
from npcomp.compiler.generic.backend import iree as iree_backend
|
2020-11-11 10:03:25 +08:00
|
|
|
from npcomp.compiler.utils import logging
|
2020-06-20 08:17:22 +08:00
|
|
|
|
|
|
|
__all__ = [
|
|
|
|
"is_enabled",
|
|
|
|
"CompilerBackend",
|
|
|
|
]
|
|
|
|
|
2020-06-20 08:25:18 +08:00
|
|
|
FRONTEND_PASSES = (
|
2020-11-14 10:06:59 +08:00
|
|
|
"func(basicpy-type-inference)",
|
|
|
|
"func(convert-basicpy-to-std)",
|
|
|
|
"func(canonicalize)",
|
|
|
|
"func(convert-scf-to-std)",
|
2020-06-20 08:25:18 +08:00
|
|
|
)
|
|
|
|
|
2020-06-20 08:17:22 +08:00
|
|
|
_ireert = None
|
|
|
|
_cached_config = None
|
|
|
|
|
|
|
|
|
|
|
|
def _get_iree():
|
|
|
|
"""Dynamically resolves the iree backend module."""
|
|
|
|
global _ireert
|
|
|
|
try:
|
|
|
|
from pyiree import rt as imported_rt
|
|
|
|
except ImportError:
|
|
|
|
raise ImportError("IREE runtime library not found (pyiree.rt)")
|
|
|
|
_ireert = imported_rt
|
2020-11-14 10:06:59 +08:00
|
|
|
return _ireert
|
2020-06-20 08:17:22 +08:00
|
|
|
|
|
|
|
|
|
|
|
def is_enabled() -> bool:
|
|
|
|
"""Returns whether the backend is enabled for the current build."""
|
|
|
|
try:
|
|
|
|
_get_iree()
|
|
|
|
return True
|
|
|
|
except ImportError:
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
class CompilerBackend:
|
|
|
|
"""Main entry-point for the backend."""
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
super().__init__()
|
2020-11-14 10:06:59 +08:00
|
|
|
self._ireert = _get_iree()
|
2020-06-20 08:17:22 +08:00
|
|
|
self._debug = logging.debug_enabled()
|
|
|
|
|
2020-12-30 05:22:18 +08:00
|
|
|
def compile(self, imported_module: Module):
|
2020-06-20 08:17:22 +08:00
|
|
|
"""Compiles an imported module.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
imported_ir_module: The MLIR module as imported from the ImportFrontend.
|
|
|
|
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-12-30 05:22:18 +08:00
|
|
|
with imported_module.context:
|
|
|
|
# Frontend.
|
|
|
|
if self._debug:
|
|
|
|
logging.debug("Input IR:\n{}", imported_module)
|
|
|
|
assert (
|
|
|
|
imported_module.operation.verify()), "Imported module does not verify"
|
2020-11-14 10:06:59 +08:00
|
|
|
# Frontend.
|
|
|
|
pm = PassManager.parse(",".join(FRONTEND_PASSES))
|
|
|
|
pm.run(imported_module)
|
|
|
|
if self._debug:
|
|
|
|
logging.debug("Frontend IR:{}", imported_module)
|
|
|
|
|
|
|
|
# TODO: There should be some common utility for invoking backend processes
|
|
|
|
# safely (and have options like saving temps, etc).
|
|
|
|
args = [
|
|
|
|
iree_backend.get_translate_exe(), "--iree-mlir-to-vm-bytecode-module"
|
|
|
|
]
|
|
|
|
p = subprocess.Popen(args, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
|
|
|
|
imported_module.operation.print(binary=True,
|
|
|
|
enable_debug_info=True,
|
|
|
|
file=p.stdin)
|
|
|
|
out, err = p.communicate()
|
|
|
|
return out
|
2020-06-20 08:17:22 +08:00
|
|
|
|
|
|
|
def load(self, vm_blob):
|
|
|
|
"""Loads a compiled artifact into the runtime.
|
|
|
|
|
|
|
|
This is meant as a simple mechanism for testing and is not optimized or
|
|
|
|
highly parameterized. It loads a compiled result into a new runtime
|
|
|
|
instance and returns an object that exposes a python function for each
|
|
|
|
public function compiled in the imported_ir_module that was compiled.
|
|
|
|
"""
|
|
|
|
ireert = self._ireert
|
|
|
|
m = ireert.VmModule.from_flatbuffer(vm_blob)
|
|
|
|
global _cached_config
|
|
|
|
if not _cached_config:
|
|
|
|
# TODO: Need to make the configuration more flexible.
|
|
|
|
_cached_config = ireert.Config(driver_name="vmla")
|
|
|
|
ctx = ireert.SystemContext(config=_cached_config)
|
|
|
|
ctx.add_module(m)
|
|
|
|
# TODO: The implicit tying of the 'module' name has got to go.
|
|
|
|
return ctx.modules.module
|