2021-04-02 08:36:18 +08:00
|
|
|
//===- AdjustCallingConventions.cpp ------------------------*- C++-*-===//
|
|
|
|
//
|
|
|
|
// This file is licensed 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
|
2021-09-30 00:03:40 +08:00
|
|
|
// Also available under a BSD-style license. See LICENSE.
|
2021-04-02 08:36:18 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "PassDetail.h"
|
|
|
|
|
2022-03-16 18:44:23 +08:00
|
|
|
#include "mlir/Dialect/Func/IR/FuncOps.h"
|
2021-04-02 08:36:18 +08:00
|
|
|
#include "mlir/IR/Builders.h"
|
|
|
|
#include "mlir/IR/BuiltinOps.h"
|
2023-06-13 22:17:23 +08:00
|
|
|
#include "mlir/Support/LogicalResult.h"
|
2021-04-02 08:36:18 +08:00
|
|
|
#include "mlir/Transforms/DialectConversion.h"
|
|
|
|
#include "mlir/Transforms/GreedyPatternRewriteDriver.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/Dialect/Torch/IR/TorchDialect.h"
|
|
|
|
#include "torch-mlir/Dialect/Torch/IR/TorchOps.h"
|
|
|
|
#include "torch-mlir/Dialect/Torch/Transforms/Passes.h"
|
2021-04-02 08:36:18 +08:00
|
|
|
|
|
|
|
using namespace mlir;
|
[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
|
|
|
using namespace mlir::torch;
|
|
|
|
using namespace mlir::torch::Torch;
|
2021-04-02 08:36:18 +08:00
|
|
|
|
|
|
|
// Map from func name and arg index to the type bound for that arg.
|
|
|
|
// This is needed because to rewrite calls, we need the non-local information
|
|
|
|
// from the func definition.
|
|
|
|
// We also benefit from populating this all at once, which avoids ordering
|
|
|
|
// issues between rewriting of func ops vs call ops.
|
[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
|
|
|
using TypeBoundMap = DenseMap<std::pair<StringRef, int>, Type>;
|
2021-04-02 08:36:18 +08:00
|
|
|
|
|
|
|
namespace {
|
2022-04-27 03:27:51 +08:00
|
|
|
class AdjustCallingConventionForFunc
|
|
|
|
: public OpConversionPattern<func::FuncOp> {
|
2021-04-02 08:36:18 +08:00
|
|
|
public:
|
|
|
|
using OpConversionPattern::OpConversionPattern;
|
|
|
|
LogicalResult
|
2022-04-27 03:27:51 +08:00
|
|
|
matchAndRewrite(func::FuncOp func, OpAdaptor adaptor,
|
2021-04-02 08:36:18 +08:00
|
|
|
ConversionPatternRewriter &rewriter) const override {
|
|
|
|
MLIRContext *context = func.getContext();
|
2022-01-26 14:16:30 +08:00
|
|
|
auto typeBoundIdent = StringAttr::get(context, "torch.type_bound");
|
2021-04-02 08:36:18 +08:00
|
|
|
TypeConverter::SignatureConversion conversion(func.getNumArguments());
|
|
|
|
|
|
|
|
// The TypeConverter hooks for type conversion are "context free", so we
|
|
|
|
// cannot use the usual helpers here for populating SignatureConversion and
|
|
|
|
// new result types.
|
|
|
|
//
|
|
|
|
// The incoporation of the torch.type_bound arg attr is context-dependent.
|
|
|
|
|
|
|
|
for (auto type : llvm::enumerate(func.getArgumentTypes())) {
|
2024-04-28 05:00:56 +08:00
|
|
|
if (isa<NonValueTensorType>(type.value())) {
|
2021-04-02 08:36:18 +08:00
|
|
|
auto typeBoundAttr =
|
|
|
|
func.getArgAttrOfType<TypeAttr>(type.index(), typeBoundIdent);
|
Introduce `!torch.tensor` / `!torch.vtensor` types.
This removes our reliance on the numpy dialect and avoids our off-label
use of the builtin tnesor type for modeling unknown dtypes. The
`!torch.vtensor` (`ValueTensorType`) type is a value-semantic tensor.
The `!torch.tensor` (`NonValueTensorType`) type is a non-value-semantic
tensor. The new types look as follows syntactically:
```
// Least-static-information, non-value-semantic tensor.
!torch.tensor
// Explicit form of least-static-information variant.
!torch.tensor<*,unk>
// Least-static-information, value-semantic tensor.
!torch.vtensor
// Explicit form of least-static-information variant.
!torch.vtensor<*,unk>
// Fixed-set of allowable element types, with first-class support for
// Torch's frontend signedness semantics.
!torch.tensor<*,si32>
// First-class support for unknown dtypes.
!torch.tensor<[?,?,?],unk>
// Standard MLIR representation of `?` for unknown dimensions.
!torch.tensor<[?,2,?,4],unk>
// Statically shaped / dtyped example.
!torch.vtensor<[1,2,3,4],f32>
```
This required fairly significant changes throughout the compiler, but
overall it is a big cleanup. We now have a much clearer layering of "the
Torch frontend lowering" vs "lowering to std + linalg + etc.".
At the C++ level, there is `ValueTensorType`, `NonValueTensorType`.
We also have a helper `BaseTensorType` (kind of like ShapedType) which
interoperates with those two.
Included changes:
- New `torch.tensor(dense<0.0> : tensor<5xf32>) : !torch.tensor` op for
creating torch tensor literals in the frontend.
- Consistently use signedness for the types (except i1 which I didn't
touch -- we need to sort out the situation with !basicpy.BoolType
there anyway so will be attending to that soon)
- Frontend can annotate whether an argument to the function has value
semantics. We currently require this, as our backend contract does not
currently allow us to even model the non-value-semantic case. Before,
the value-semantic assumption was randomly injected in the middle of
the pass pipeline.
- Move ArrayToTensor (now called MaximizeValueSemantics) and
RefinePublicReturn passes to torch dialect.
- The TorchToStd and TorchToLinalg passes are now type conversions from
`!torch.vtensor` to `tensor` and use the dialect conversion infra.
The overall conversion pipeline is set up following the best practices
of the "Type Conversions the Not-So-Hard Way" talk. This required
introducing `torch-func-builtin-tensorize` and
`torch-finalizing-builtin-tensorize` passes analogous to the upstream
bufferization passes with the corresponding names (mostly just
copypasta from there).
- Misc Torch-level canonicalizations -- we now cleanly layer the
lowering to std later in the pipeline, so we are gradually lessening
our reliance on random std constant folding before we get to that
point.
Recommended review order:
- New types in TorchTypes.td/TorchTypes.h/TorchDialect.cpp
- New ops in TorchOps.td / TorchOps.cpp
- Less important / more mechanical stuff
- Frontend changes.
- Pass changes/additions in `Torch/Transforms` and `Conversion/`
2021-05-21 08:07:18 +08:00
|
|
|
Type bound = typeBoundAttr ? typeBoundAttr.getValue() : Type();
|
2024-04-11 21:47:35 +08:00
|
|
|
if (!isa<ValueTensorType>(bound))
|
Introduce `!torch.tensor` / `!torch.vtensor` types.
This removes our reliance on the numpy dialect and avoids our off-label
use of the builtin tnesor type for modeling unknown dtypes. The
`!torch.vtensor` (`ValueTensorType`) type is a value-semantic tensor.
The `!torch.tensor` (`NonValueTensorType`) type is a non-value-semantic
tensor. The new types look as follows syntactically:
```
// Least-static-information, non-value-semantic tensor.
!torch.tensor
// Explicit form of least-static-information variant.
!torch.tensor<*,unk>
// Least-static-information, value-semantic tensor.
!torch.vtensor
// Explicit form of least-static-information variant.
!torch.vtensor<*,unk>
// Fixed-set of allowable element types, with first-class support for
// Torch's frontend signedness semantics.
!torch.tensor<*,si32>
// First-class support for unknown dtypes.
!torch.tensor<[?,?,?],unk>
// Standard MLIR representation of `?` for unknown dimensions.
!torch.tensor<[?,2,?,4],unk>
// Statically shaped / dtyped example.
!torch.vtensor<[1,2,3,4],f32>
```
This required fairly significant changes throughout the compiler, but
overall it is a big cleanup. We now have a much clearer layering of "the
Torch frontend lowering" vs "lowering to std + linalg + etc.".
At the C++ level, there is `ValueTensorType`, `NonValueTensorType`.
We also have a helper `BaseTensorType` (kind of like ShapedType) which
interoperates with those two.
Included changes:
- New `torch.tensor(dense<0.0> : tensor<5xf32>) : !torch.tensor` op for
creating torch tensor literals in the frontend.
- Consistently use signedness for the types (except i1 which I didn't
touch -- we need to sort out the situation with !basicpy.BoolType
there anyway so will be attending to that soon)
- Frontend can annotate whether an argument to the function has value
semantics. We currently require this, as our backend contract does not
currently allow us to even model the non-value-semantic case. Before,
the value-semantic assumption was randomly injected in the middle of
the pass pipeline.
- Move ArrayToTensor (now called MaximizeValueSemantics) and
RefinePublicReturn passes to torch dialect.
- The TorchToStd and TorchToLinalg passes are now type conversions from
`!torch.vtensor` to `tensor` and use the dialect conversion infra.
The overall conversion pipeline is set up following the best practices
of the "Type Conversions the Not-So-Hard Way" talk. This required
introducing `torch-func-builtin-tensorize` and
`torch-finalizing-builtin-tensorize` passes analogous to the upstream
bufferization passes with the corresponding names (mostly just
copypasta from there).
- Misc Torch-level canonicalizations -- we now cleanly layer the
lowering to std later in the pipeline, so we are gradually lessening
our reliance on random std constant folding before we get to that
point.
Recommended review order:
- New types in TorchTypes.td/TorchTypes.h/TorchDialect.cpp
- New ops in TorchOps.td / TorchOps.cpp
- Less important / more mechanical stuff
- Frontend changes.
- Pass changes/additions in `Torch/Transforms` and `Conversion/`
2021-05-21 08:07:18 +08:00
|
|
|
return rewriter.notifyMatchFailure(
|
|
|
|
func, "unimplemented: preserving aliasing for non-value-semantic "
|
|
|
|
"type bounds");
|
2021-04-02 08:36:18 +08:00
|
|
|
conversion.addInputs(type.index(), typeBoundAttr
|
|
|
|
? typeBoundAttr.getValue()
|
|
|
|
: type.value());
|
|
|
|
continue;
|
2024-04-28 05:00:56 +08:00
|
|
|
} else if (auto none = dyn_cast<Torch::NoneType>(type.value())) {
|
2021-04-02 08:36:18 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// TODO: add tuple type.
|
|
|
|
conversion.addInputs(type.index(), type.value());
|
|
|
|
}
|
2021-11-08 23:56:40 +08:00
|
|
|
rewriter.applySignatureConversion(&func.getBody(), conversion,
|
|
|
|
typeConverter);
|
|
|
|
|
2021-04-02 08:36:18 +08:00
|
|
|
SmallVector<Type> newResultTypes;
|
2022-04-27 03:27:51 +08:00
|
|
|
for (auto type : func.getFunctionType().getResults()) {
|
2024-04-11 21:47:35 +08:00
|
|
|
if (auto none = dyn_cast<Torch::NoneType>(type)) {
|
2021-04-02 08:36:18 +08:00
|
|
|
continue;
|
|
|
|
}
|
2024-04-11 21:47:35 +08:00
|
|
|
if (auto tuple = dyn_cast<Torch::TupleType>(type)) {
|
2021-11-08 23:56:40 +08:00
|
|
|
llvm::append_range(newResultTypes, tuple.getContainedTypes());
|
|
|
|
continue;
|
|
|
|
}
|
2021-04-02 08:36:18 +08:00
|
|
|
newResultTypes.push_back(type);
|
|
|
|
}
|
2024-01-27 10:38:44 +08:00
|
|
|
rewriter.modifyOpInPlace(func, [&] {
|
2021-04-02 08:36:18 +08:00
|
|
|
func.setType(FunctionType::get(
|
|
|
|
getContext(), conversion.getConvertedTypes(), newResultTypes));
|
|
|
|
// Clear out the type bounds, now that the type incorporates them.
|
|
|
|
for (int i = 0, e = func.getNumArguments(); i != e; i++)
|
|
|
|
func.removeArgAttr(i, typeBoundIdent);
|
|
|
|
});
|
|
|
|
return success();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace {
|
2022-03-16 18:44:23 +08:00
|
|
|
class AdjustCallingConventionForCall
|
|
|
|
: public OpConversionPattern<func::CallOp> {
|
2021-04-02 08:36:18 +08:00
|
|
|
public:
|
|
|
|
AdjustCallingConventionForCall(TypeConverter &converter, MLIRContext *context,
|
|
|
|
TypeBoundMap &typeBoundMap)
|
2022-03-16 18:44:23 +08:00
|
|
|
: OpConversionPattern<func::CallOp>(converter, context),
|
2021-04-02 08:36:18 +08:00
|
|
|
typeBoundMap(typeBoundMap) {}
|
|
|
|
LogicalResult
|
2022-03-16 18:44:23 +08:00
|
|
|
matchAndRewrite(func::CallOp call, OpAdaptor adaptor,
|
2021-04-02 08:36:18 +08:00
|
|
|
ConversionPatternRewriter &rewriter) const override {
|
|
|
|
SmallVector<Type> convertedResults;
|
|
|
|
if (failed(typeConverter->convertTypes(call.getResultTypes(),
|
|
|
|
convertedResults)))
|
|
|
|
return failure();
|
|
|
|
|
|
|
|
SmallVector<Value> newOperands;
|
2021-11-16 07:00:53 +08:00
|
|
|
for (auto operand : llvm::enumerate(adaptor.getOperands())) {
|
2024-04-28 05:00:56 +08:00
|
|
|
if (isa<Torch::NoneType>(operand.value().getType()))
|
2021-04-02 08:36:18 +08:00
|
|
|
continue;
|
2022-01-05 00:00:48 +08:00
|
|
|
auto it = typeBoundMap.find({call.getCallee(), operand.index()});
|
2021-04-02 08:36:18 +08:00
|
|
|
if (it != typeBoundMap.end()) {
|
Introduce `!torch.tensor` / `!torch.vtensor` types.
This removes our reliance on the numpy dialect and avoids our off-label
use of the builtin tnesor type for modeling unknown dtypes. The
`!torch.vtensor` (`ValueTensorType`) type is a value-semantic tensor.
The `!torch.tensor` (`NonValueTensorType`) type is a non-value-semantic
tensor. The new types look as follows syntactically:
```
// Least-static-information, non-value-semantic tensor.
!torch.tensor
// Explicit form of least-static-information variant.
!torch.tensor<*,unk>
// Least-static-information, value-semantic tensor.
!torch.vtensor
// Explicit form of least-static-information variant.
!torch.vtensor<*,unk>
// Fixed-set of allowable element types, with first-class support for
// Torch's frontend signedness semantics.
!torch.tensor<*,si32>
// First-class support for unknown dtypes.
!torch.tensor<[?,?,?],unk>
// Standard MLIR representation of `?` for unknown dimensions.
!torch.tensor<[?,2,?,4],unk>
// Statically shaped / dtyped example.
!torch.vtensor<[1,2,3,4],f32>
```
This required fairly significant changes throughout the compiler, but
overall it is a big cleanup. We now have a much clearer layering of "the
Torch frontend lowering" vs "lowering to std + linalg + etc.".
At the C++ level, there is `ValueTensorType`, `NonValueTensorType`.
We also have a helper `BaseTensorType` (kind of like ShapedType) which
interoperates with those two.
Included changes:
- New `torch.tensor(dense<0.0> : tensor<5xf32>) : !torch.tensor` op for
creating torch tensor literals in the frontend.
- Consistently use signedness for the types (except i1 which I didn't
touch -- we need to sort out the situation with !basicpy.BoolType
there anyway so will be attending to that soon)
- Frontend can annotate whether an argument to the function has value
semantics. We currently require this, as our backend contract does not
currently allow us to even model the non-value-semantic case. Before,
the value-semantic assumption was randomly injected in the middle of
the pass pipeline.
- Move ArrayToTensor (now called MaximizeValueSemantics) and
RefinePublicReturn passes to torch dialect.
- The TorchToStd and TorchToLinalg passes are now type conversions from
`!torch.vtensor` to `tensor` and use the dialect conversion infra.
The overall conversion pipeline is set up following the best practices
of the "Type Conversions the Not-So-Hard Way" talk. This required
introducing `torch-func-builtin-tensorize` and
`torch-finalizing-builtin-tensorize` passes analogous to the upstream
bufferization passes with the corresponding names (mostly just
copypasta from there).
- Misc Torch-level canonicalizations -- we now cleanly layer the
lowering to std later in the pipeline, so we are gradually lessening
our reliance on random std constant folding before we get to that
point.
Recommended review order:
- New types in TorchTypes.td/TorchTypes.h/TorchDialect.cpp
- New ops in TorchOps.td / TorchOps.cpp
- Less important / more mechanical stuff
- Frontend changes.
- Pass changes/additions in `Torch/Transforms` and `Conversion/`
2021-05-21 08:07:18 +08:00
|
|
|
if (auto valueTensorType = it->second.dyn_cast<ValueTensorType>()) {
|
|
|
|
newOperands.push_back(copyTensorToType(
|
|
|
|
rewriter, call->getLoc(), valueTensorType, operand.value()));
|
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
return rewriter.notifyMatchFailure(
|
|
|
|
call, "unimplemented: preserving aliasing for non-value-semantic "
|
|
|
|
"type bounds");
|
|
|
|
}
|
2021-04-02 08:36:18 +08:00
|
|
|
}
|
|
|
|
newOperands.push_back(operand.value());
|
|
|
|
}
|
|
|
|
|
2022-03-16 18:44:23 +08:00
|
|
|
func::CallOp newCall = rewriter.create<func::CallOp>(
|
|
|
|
call.getLoc(), call.getCallee(), convertedResults, newOperands);
|
2021-04-02 08:36:18 +08:00
|
|
|
int newOpResultIdx = 0;
|
|
|
|
SmallVector<Value> newResults;
|
|
|
|
for (auto type : call.getResultTypes()) {
|
2024-04-11 21:47:35 +08:00
|
|
|
if (isa<Torch::NoneType>(type)) {
|
2021-04-02 08:36:18 +08:00
|
|
|
newResults.push_back(
|
2021-06-15 02:36:10 +08:00
|
|
|
rewriter.create<ConstantNoneOp>(call.getLoc(), type));
|
2021-04-02 08:36:18 +08:00
|
|
|
continue;
|
|
|
|
}
|
2024-04-11 21:47:35 +08:00
|
|
|
if (isa<Torch::TupleType>(type)) {
|
2021-11-08 23:56:40 +08:00
|
|
|
newResults.push_back(rewriter.create<PrimTupleConstructOp>(
|
|
|
|
call.getLoc(), type, newCall.getResults()));
|
|
|
|
continue;
|
|
|
|
}
|
2021-04-02 08:36:18 +08:00
|
|
|
newResults.push_back(newCall.getResult(newOpResultIdx++));
|
|
|
|
}
|
|
|
|
rewriter.replaceOp(call, newResults);
|
|
|
|
return success();
|
|
|
|
}
|
|
|
|
|
[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
|
|
|
private:
|
|
|
|
TypeBoundMap &typeBoundMap;
|
2021-04-02 08:36:18 +08:00
|
|
|
};
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace {
|
2022-03-16 18:44:23 +08:00
|
|
|
class AdjustCallingConventionForReturn
|
|
|
|
: public OpConversionPattern<func::ReturnOp> {
|
2021-04-02 08:36:18 +08:00
|
|
|
public:
|
|
|
|
using OpConversionPattern::OpConversionPattern;
|
|
|
|
LogicalResult
|
2022-03-16 18:44:23 +08:00
|
|
|
matchAndRewrite(func::ReturnOp op, OpAdaptor adaptor,
|
2021-04-02 08:36:18 +08:00
|
|
|
ConversionPatternRewriter &rewriter) const override {
|
|
|
|
|
|
|
|
SmallVector<Value> newOperands;
|
2021-11-08 23:56:40 +08:00
|
|
|
for (auto operand : adaptor.getOperands()) {
|
|
|
|
if (!operand)
|
2021-04-02 08:36:18 +08:00
|
|
|
continue;
|
2024-04-28 05:00:56 +08:00
|
|
|
if (isa<Torch::NoneType>(operand.getType()))
|
2021-04-02 08:36:18 +08:00
|
|
|
continue;
|
2024-04-28 05:00:56 +08:00
|
|
|
if (auto tuple = dyn_cast<Torch::TupleType>(operand.getType())) {
|
2021-11-08 23:56:40 +08:00
|
|
|
Location loc = op.getLoc();
|
|
|
|
for (auto en : llvm::enumerate(tuple.getContainedTypes())) {
|
|
|
|
auto i = rewriter.create<ConstantIntOp>(
|
|
|
|
loc, rewriter.getI64IntegerAttr(en.index()));
|
|
|
|
newOperands.push_back(
|
|
|
|
rewriter.create<PrimTupleIndexOp>(loc, en.value(), operand, i));
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
newOperands.push_back(operand);
|
2021-04-02 08:36:18 +08:00
|
|
|
}
|
2022-03-16 18:44:23 +08:00
|
|
|
rewriter.replaceOpWithNewOp<func::ReturnOp>(op, newOperands);
|
2021-04-02 08:36:18 +08:00
|
|
|
return success();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
} // namespace
|
|
|
|
|
2022-04-27 03:27:51 +08:00
|
|
|
static LogicalResult adjustCallingConventions(func::FuncOp func,
|
2021-04-02 08:36:18 +08:00
|
|
|
TypeBoundMap &typeBoundMap) {
|
|
|
|
MLIRContext *context = func.getContext();
|
|
|
|
RewritePatternSet patterns(context);
|
|
|
|
TypeConverter typeConverter;
|
|
|
|
typeConverter.addConversion([](Type type) { return type; });
|
2021-11-08 23:56:40 +08:00
|
|
|
typeConverter.addConversion(
|
2024-01-27 10:38:44 +08:00
|
|
|
[](Torch::TupleType type, SmallVectorImpl<Type> &types) -> LogicalResult {
|
2021-11-08 23:56:40 +08:00
|
|
|
llvm::append_range(types, type.getContainedTypes());
|
|
|
|
return success();
|
|
|
|
});
|
2021-04-02 08:36:18 +08:00
|
|
|
typeConverter.addConversion(
|
2024-01-27 10:38:44 +08:00
|
|
|
[](Torch::NoneType type, SmallVectorImpl<Type> &types) -> LogicalResult {
|
2021-04-02 08:36:18 +08:00
|
|
|
return success();
|
|
|
|
});
|
|
|
|
|
|
|
|
typeConverter.addArgumentMaterialization(
|
Introduce `!torch.tensor` / `!torch.vtensor` types.
This removes our reliance on the numpy dialect and avoids our off-label
use of the builtin tnesor type for modeling unknown dtypes. The
`!torch.vtensor` (`ValueTensorType`) type is a value-semantic tensor.
The `!torch.tensor` (`NonValueTensorType`) type is a non-value-semantic
tensor. The new types look as follows syntactically:
```
// Least-static-information, non-value-semantic tensor.
!torch.tensor
// Explicit form of least-static-information variant.
!torch.tensor<*,unk>
// Least-static-information, value-semantic tensor.
!torch.vtensor
// Explicit form of least-static-information variant.
!torch.vtensor<*,unk>
// Fixed-set of allowable element types, with first-class support for
// Torch's frontend signedness semantics.
!torch.tensor<*,si32>
// First-class support for unknown dtypes.
!torch.tensor<[?,?,?],unk>
// Standard MLIR representation of `?` for unknown dimensions.
!torch.tensor<[?,2,?,4],unk>
// Statically shaped / dtyped example.
!torch.vtensor<[1,2,3,4],f32>
```
This required fairly significant changes throughout the compiler, but
overall it is a big cleanup. We now have a much clearer layering of "the
Torch frontend lowering" vs "lowering to std + linalg + etc.".
At the C++ level, there is `ValueTensorType`, `NonValueTensorType`.
We also have a helper `BaseTensorType` (kind of like ShapedType) which
interoperates with those two.
Included changes:
- New `torch.tensor(dense<0.0> : tensor<5xf32>) : !torch.tensor` op for
creating torch tensor literals in the frontend.
- Consistently use signedness for the types (except i1 which I didn't
touch -- we need to sort out the situation with !basicpy.BoolType
there anyway so will be attending to that soon)
- Frontend can annotate whether an argument to the function has value
semantics. We currently require this, as our backend contract does not
currently allow us to even model the non-value-semantic case. Before,
the value-semantic assumption was randomly injected in the middle of
the pass pipeline.
- Move ArrayToTensor (now called MaximizeValueSemantics) and
RefinePublicReturn passes to torch dialect.
- The TorchToStd and TorchToLinalg passes are now type conversions from
`!torch.vtensor` to `tensor` and use the dialect conversion infra.
The overall conversion pipeline is set up following the best practices
of the "Type Conversions the Not-So-Hard Way" talk. This required
introducing `torch-func-builtin-tensorize` and
`torch-finalizing-builtin-tensorize` passes analogous to the upstream
bufferization passes with the corresponding names (mostly just
copypasta from there).
- Misc Torch-level canonicalizations -- we now cleanly layer the
lowering to std later in the pipeline, so we are gradually lessening
our reliance on random std constant folding before we get to that
point.
Recommended review order:
- New types in TorchTypes.td/TorchTypes.h/TorchDialect.cpp
- New ops in TorchOps.td / TorchOps.cpp
- Less important / more mechanical stuff
- Frontend changes.
- Pass changes/additions in `Torch/Transforms` and `Conversion/`
2021-05-21 08:07:18 +08:00
|
|
|
[](OpBuilder &builder, Torch::BaseTensorType type, ValueRange inputs,
|
2021-04-02 08:36:18 +08:00
|
|
|
Location loc) -> Value {
|
|
|
|
assert(inputs.size() == 1);
|
2024-04-28 05:00:56 +08:00
|
|
|
assert(isa<BaseTensorType>(inputs[0].getType()));
|
Introduce `!torch.tensor` / `!torch.vtensor` types.
This removes our reliance on the numpy dialect and avoids our off-label
use of the builtin tnesor type for modeling unknown dtypes. The
`!torch.vtensor` (`ValueTensorType`) type is a value-semantic tensor.
The `!torch.tensor` (`NonValueTensorType`) type is a non-value-semantic
tensor. The new types look as follows syntactically:
```
// Least-static-information, non-value-semantic tensor.
!torch.tensor
// Explicit form of least-static-information variant.
!torch.tensor<*,unk>
// Least-static-information, value-semantic tensor.
!torch.vtensor
// Explicit form of least-static-information variant.
!torch.vtensor<*,unk>
// Fixed-set of allowable element types, with first-class support for
// Torch's frontend signedness semantics.
!torch.tensor<*,si32>
// First-class support for unknown dtypes.
!torch.tensor<[?,?,?],unk>
// Standard MLIR representation of `?` for unknown dimensions.
!torch.tensor<[?,2,?,4],unk>
// Statically shaped / dtyped example.
!torch.vtensor<[1,2,3,4],f32>
```
This required fairly significant changes throughout the compiler, but
overall it is a big cleanup. We now have a much clearer layering of "the
Torch frontend lowering" vs "lowering to std + linalg + etc.".
At the C++ level, there is `ValueTensorType`, `NonValueTensorType`.
We also have a helper `BaseTensorType` (kind of like ShapedType) which
interoperates with those two.
Included changes:
- New `torch.tensor(dense<0.0> : tensor<5xf32>) : !torch.tensor` op for
creating torch tensor literals in the frontend.
- Consistently use signedness for the types (except i1 which I didn't
touch -- we need to sort out the situation with !basicpy.BoolType
there anyway so will be attending to that soon)
- Frontend can annotate whether an argument to the function has value
semantics. We currently require this, as our backend contract does not
currently allow us to even model the non-value-semantic case. Before,
the value-semantic assumption was randomly injected in the middle of
the pass pipeline.
- Move ArrayToTensor (now called MaximizeValueSemantics) and
RefinePublicReturn passes to torch dialect.
- The TorchToStd and TorchToLinalg passes are now type conversions from
`!torch.vtensor` to `tensor` and use the dialect conversion infra.
The overall conversion pipeline is set up following the best practices
of the "Type Conversions the Not-So-Hard Way" talk. This required
introducing `torch-func-builtin-tensorize` and
`torch-finalizing-builtin-tensorize` passes analogous to the upstream
bufferization passes with the corresponding names (mostly just
copypasta from there).
- Misc Torch-level canonicalizations -- we now cleanly layer the
lowering to std later in the pipeline, so we are gradually lessening
our reliance on random std constant folding before we get to that
point.
Recommended review order:
- New types in TorchTypes.td/TorchTypes.h/TorchDialect.cpp
- New ops in TorchOps.td / TorchOps.cpp
- Less important / more mechanical stuff
- Frontend changes.
- Pass changes/additions in `Torch/Transforms` and `Conversion/`
2021-05-21 08:07:18 +08:00
|
|
|
return copyTensorToType(builder, loc, type, inputs[0]);
|
2021-04-02 08:36:18 +08:00
|
|
|
});
|
|
|
|
patterns.add<AdjustCallingConventionForFunc>(typeConverter, context);
|
|
|
|
patterns.add<AdjustCallingConventionForCall>(typeConverter, context,
|
|
|
|
typeBoundMap);
|
|
|
|
patterns.add<AdjustCallingConventionForReturn>(typeConverter, context);
|
|
|
|
|
|
|
|
ConversionTarget target(*context);
|
2022-04-27 03:27:51 +08:00
|
|
|
target.addDynamicallyLegalOp<func::FuncOp>([](func::FuncOp func) {
|
2021-04-02 08:36:18 +08:00
|
|
|
for (int i = 0, e = func.getNumArguments(); i != e; i++) {
|
|
|
|
if (func.getArgAttr(i, "torch.type_bound"))
|
|
|
|
return false;
|
2021-06-15 02:36:10 +08:00
|
|
|
if (func.getArgumentTypes()[i].isa<Torch::NoneType>())
|
2021-04-02 08:36:18 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
for (int i = 0, e = func.getNumResults(); i != e; i++) {
|
2022-04-27 03:27:51 +08:00
|
|
|
if (func.getFunctionType().getResults()[i].isa<Torch::NoneType>())
|
2021-04-02 08:36:18 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
// The dynamic legality conditions for call and return are a pain to write...
|
|
|
|
// Just run the patterns once and call it a day.
|
|
|
|
//
|
|
|
|
// Bug for doing this better https://bugs.llvm.org/show_bug.cgi?id=49812
|
|
|
|
DenseSet<Operation *> opsInOriginalProgram;
|
|
|
|
func.walk(
|
2022-03-16 18:44:23 +08:00
|
|
|
[&](func::CallOp op) { opsInOriginalProgram.insert(op.getOperation()); });
|
|
|
|
func.walk([&](func::ReturnOp op) {
|
|
|
|
opsInOriginalProgram.insert(op.getOperation());
|
|
|
|
});
|
|
|
|
target.addDynamicallyLegalOp<func::CallOp>([&](func::CallOp op) {
|
2021-04-02 08:36:18 +08:00
|
|
|
return !opsInOriginalProgram.contains(op.getOperation());
|
|
|
|
});
|
2022-03-16 18:44:23 +08:00
|
|
|
target.addDynamicallyLegalOp<func::ReturnOp>([&](func::ReturnOp op) {
|
2021-04-02 08:36:18 +08:00
|
|
|
return !opsInOriginalProgram.contains(op.getOperation());
|
|
|
|
});
|
2021-06-19 04:47:47 +08:00
|
|
|
target.addLegalOp<CopyToNonValueTensorOp, CopyToValueTensorOp>();
|
Introduce `!torch.tensor` / `!torch.vtensor` types.
This removes our reliance on the numpy dialect and avoids our off-label
use of the builtin tnesor type for modeling unknown dtypes. The
`!torch.vtensor` (`ValueTensorType`) type is a value-semantic tensor.
The `!torch.tensor` (`NonValueTensorType`) type is a non-value-semantic
tensor. The new types look as follows syntactically:
```
// Least-static-information, non-value-semantic tensor.
!torch.tensor
// Explicit form of least-static-information variant.
!torch.tensor<*,unk>
// Least-static-information, value-semantic tensor.
!torch.vtensor
// Explicit form of least-static-information variant.
!torch.vtensor<*,unk>
// Fixed-set of allowable element types, with first-class support for
// Torch's frontend signedness semantics.
!torch.tensor<*,si32>
// First-class support for unknown dtypes.
!torch.tensor<[?,?,?],unk>
// Standard MLIR representation of `?` for unknown dimensions.
!torch.tensor<[?,2,?,4],unk>
// Statically shaped / dtyped example.
!torch.vtensor<[1,2,3,4],f32>
```
This required fairly significant changes throughout the compiler, but
overall it is a big cleanup. We now have a much clearer layering of "the
Torch frontend lowering" vs "lowering to std + linalg + etc.".
At the C++ level, there is `ValueTensorType`, `NonValueTensorType`.
We also have a helper `BaseTensorType` (kind of like ShapedType) which
interoperates with those two.
Included changes:
- New `torch.tensor(dense<0.0> : tensor<5xf32>) : !torch.tensor` op for
creating torch tensor literals in the frontend.
- Consistently use signedness for the types (except i1 which I didn't
touch -- we need to sort out the situation with !basicpy.BoolType
there anyway so will be attending to that soon)
- Frontend can annotate whether an argument to the function has value
semantics. We currently require this, as our backend contract does not
currently allow us to even model the non-value-semantic case. Before,
the value-semantic assumption was randomly injected in the middle of
the pass pipeline.
- Move ArrayToTensor (now called MaximizeValueSemantics) and
RefinePublicReturn passes to torch dialect.
- The TorchToStd and TorchToLinalg passes are now type conversions from
`!torch.vtensor` to `tensor` and use the dialect conversion infra.
The overall conversion pipeline is set up following the best practices
of the "Type Conversions the Not-So-Hard Way" talk. This required
introducing `torch-func-builtin-tensorize` and
`torch-finalizing-builtin-tensorize` passes analogous to the upstream
bufferization passes with the corresponding names (mostly just
copypasta from there).
- Misc Torch-level canonicalizations -- we now cleanly layer the
lowering to std later in the pipeline, so we are gradually lessening
our reliance on random std constant folding before we get to that
point.
Recommended review order:
- New types in TorchTypes.td/TorchTypes.h/TorchDialect.cpp
- New ops in TorchOps.td / TorchOps.cpp
- Less important / more mechanical stuff
- Frontend changes.
- Pass changes/additions in `Torch/Transforms` and `Conversion/`
2021-05-21 08:07:18 +08:00
|
|
|
target.addLegalOp<TensorStaticInfoCastOp>();
|
2021-06-15 02:36:10 +08:00
|
|
|
target.addLegalOp<ConstantNoneOp>();
|
2021-11-08 23:56:40 +08:00
|
|
|
target.addLegalOp<ConstantIntOp>();
|
|
|
|
target.addLegalOp<PrimTupleIndexOp>();
|
|
|
|
target.addLegalOp<PrimTupleConstructOp>();
|
2021-04-02 08:36:18 +08:00
|
|
|
// We don't know how to rewrite it, so mark it as illegal.
|
2022-03-16 18:44:23 +08:00
|
|
|
target.addIllegalOp<func::CallIndirectOp>();
|
2021-04-02 08:36:18 +08:00
|
|
|
if (failed(applyPartialConversion(func.getOperation(), target,
|
|
|
|
std::move(patterns))))
|
|
|
|
return failure();
|
|
|
|
return success();
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
class AdjustCallingConventionsPass
|
|
|
|
: public AdjustCallingConventionsBase<AdjustCallingConventionsPass> {
|
|
|
|
void runOnOperation() override {
|
|
|
|
auto module = getOperation();
|
|
|
|
TypeBoundMap typeBoundMap;
|
2022-04-27 03:27:51 +08:00
|
|
|
for (auto func : module.getOps<func::FuncOp>()) {
|
2021-04-02 08:36:18 +08:00
|
|
|
for (int i = 0, e = func.getNumArguments(); i != e; i++) {
|
|
|
|
auto typeBoundAttr =
|
|
|
|
func.getArgAttrOfType<TypeAttr>(i, "torch.type_bound");
|
|
|
|
if (!typeBoundAttr)
|
|
|
|
continue;
|
|
|
|
typeBoundMap[{func.getName(), i}] = typeBoundAttr.getValue();
|
|
|
|
}
|
|
|
|
}
|
2022-04-27 03:27:51 +08:00
|
|
|
for (auto func : module.getOps<func::FuncOp>()) {
|
2021-04-02 08:36:18 +08:00
|
|
|
if (failed(adjustCallingConventions(func, typeBoundMap)))
|
|
|
|
return signalPassFailure();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
std::unique_ptr<OperationPass<ModuleOp>>
|
[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
|
|
|
mlir::torch::Torch::createAdjustCallingConventionsPass() {
|
2021-04-02 08:36:18 +08:00
|
|
|
return std::make_unique<AdjustCallingConventionsPass>();
|
|
|
|
}
|