2020-09-29 09:36:00 +08:00
|
|
|
//===- Registration.cpp - C Interface for MLIR Registration ---------------===//
|
|
|
|
//
|
|
|
|
// 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
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "npcomp-c/Registration.h"
|
|
|
|
|
|
|
|
#include "mlir/CAPI/IR.h"
|
2020-12-30 05:22:18 +08:00
|
|
|
#include "mlir/Conversion/Passes.h"
|
2021-04-09 04:05:16 +08:00
|
|
|
#include "mlir/Dialect/Linalg/Passes.h"
|
2020-12-30 05:22:18 +08:00
|
|
|
#include "mlir/Transforms/Passes.h"
|
2020-09-29 09:36:00 +08:00
|
|
|
#include "npcomp/InitAll.h"
|
[torch-mlir earthmoving (1/N)] C/C++ code movement.
This creates the `external/torch-mlir` directory as an
LLVM_EXTERNAL_PROJECTS-compatible project (analogous to
`iree-dialects`) and completes movement/rename of all pure MLIR C/C++
compiler code into there. The next step will be to move all the Python
code / code that links/includes PyTorch C++ code (which currently lives
in `frontends/pytorch`) into a subdirectory here.
I call this "earthmoving" because it is mostly mechanical changes and
renames. As a quick summary (we can change this down the road easily)
- C++ `mlir::NPCOMP::Torch -> mlir::torch::Torch`
- CAPI `npcompTorchListTypeGet -> torchMlirTorchListTypeGet`
- preprocessor `#ifndef NPCOMP_ -> #ifndef TORCHMLIR_`
- CMake `NPCOMPFoo -> TorchMLIRFoo`
The goal of this is to create a standalone project creating a center of
mass for entry into the MLIR ecosystem from PyTorch, suitable in scope
for eventual inclusion/ownership in PyTorch. The idea is that
`external/torch-mlir` will some day be pulled out into its own
repository, and then npcomp will simply pull it in as a submodule.
Layering-wise, what lives in `torch-mlir` lowers code from PyTorch
(currently TorchScript, but TorchFX or pytorch/xla-style tracing are
possible extensions) down to what we have been calling the "Torch
backend contract" which is cleaned up IR (inlining, simplifcation,
conversion to value tensors, ...) entirely in the `torch` dialect. This
is the branching off point for further lowering, of which npcomp takes
one opinion (outside `torch-mlir` of course!), namely the
`TorchConversion` dialect/transforms which lower to IR suitable for IREE
and other linalg-on-tensors based lower-level compilers.
Summary of changes:
- move `{include,lib,test}/Dialect/Torch` into `torch-mlir`
- move relevant parts of CAPI into `torch-mlir`.
- leave a few things related to the `torch-mlir` Python build commented
out, which should be resolved in a subsequent change.
2021-09-10 03:24:10 +08:00
|
|
|
#include "torch-mlir/InitAll.h"
|
2020-09-29 09:36:00 +08:00
|
|
|
|
|
|
|
void npcompRegisterAllDialects(MlirContext context) {
|
2021-02-23 04:08:17 +08:00
|
|
|
mlir::DialectRegistry registry;
|
|
|
|
mlir::NPCOMP::registerAllDialects(registry);
|
2021-09-11 02:44:38 +08:00
|
|
|
mlir::torch::registerAllDialects(registry);
|
2021-02-23 04:08:17 +08:00
|
|
|
unwrap(context)->appendDialectRegistry(registry);
|
2020-09-29 09:36:00 +08:00
|
|
|
// TODO: Don't eagerly load once D88162 is in and clients can do this.
|
2021-02-23 04:08:17 +08:00
|
|
|
unwrap(context)->loadAllAvailableDialects();
|
2020-09-29 09:36:00 +08:00
|
|
|
}
|
2020-12-30 05:22:18 +08:00
|
|
|
|
|
|
|
void npcompRegisterAllPasses() {
|
|
|
|
::mlir::NPCOMP::registerAllPasses();
|
|
|
|
|
|
|
|
// Upstream passes we depend on.
|
2021-03-20 05:08:04 +08:00
|
|
|
::mlir::registerSymbolDCEPass();
|
Add npcomp-verify-backend-contract pass.
This pass verifies that a given module satisfies the contract that we
have for backends. This is phrased as an "allowlist", because we want to
keep this interface tight. Also, this gives much better diagnostics than
a backend randomly crashing or failing to compile would (though they
could still be improved).
This was especially painful because if we had
`tensor<?x!numpy.any_dtype>` slip through, at some point RefBackend
would convert it to a memref type and trip the "verify type invariants"
assertion which gives no location or anything and crashed the process,
which was very unpleasant.
We implement this with the dialect conversion framework, which works
reasonably well and was quick to put together and familiar, but is still
very "op oriented". We probably want to make this hand-rolled
eventually, especially the error reporting (the most useful kind of
error for a dialect conversion user is not necessarily the best for this
use case). Also, in production, these error will go to users, and need
to be surfaced carefully such as "the compiler needs a type annotation
on this function parameter" which in general requires some special
analysis, wordsmithing, and overall awareness of the e2e use case (such
as how much we can lean into certain source locations) to provide a
meaningful user-level diagnostic.
Also, add `inline` to the current frontend lowering pass pipeline to
allow slightly more complicated programs that otherwise would fail on
shape inference.
2021-04-13 09:39:53 +08:00
|
|
|
::mlir::registerInlinerPass();
|
2020-12-30 05:22:18 +08:00
|
|
|
::mlir::registerCanonicalizerPass();
|
|
|
|
::mlir::registerSCFToStandardPass();
|
2021-04-09 04:05:16 +08:00
|
|
|
::mlir::registerConvertElementwiseToLinalgPass();
|
2020-12-30 05:22:18 +08:00
|
|
|
}
|