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
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "PassDetail.h"
|
|
|
|
|
|
|
|
#include "mlir/Dialect/StandardOps/IR/Ops.h"
|
|
|
|
#include "mlir/IR/BlockAndValueMapping.h"
|
|
|
|
#include "mlir/IR/Builders.h"
|
|
|
|
#include "mlir/IR/BuiltinOps.h"
|
|
|
|
#include "mlir/Transforms/DialectConversion.h"
|
|
|
|
#include "mlir/Transforms/GreedyPatternRewriteDriver.h"
|
|
|
|
#include "npcomp/Dialect/Torch/IR/TorchDialect.h"
|
|
|
|
#include "npcomp/Dialect/Torch/IR/TorchOps.h"
|
|
|
|
#include "npcomp/Dialect/Torch/Transforms/Passes.h"
|
|
|
|
|
|
|
|
using namespace mlir;
|
|
|
|
using namespace mlir::NPCOMP;
|
|
|
|
using namespace mlir::NPCOMP::Torch;
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
using TypeBoundMap = DenseMap<std::pair<StringRef, int>, Type> ;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
class AdjustCallingConventionForFunc : public OpConversionPattern<FuncOp> {
|
|
|
|
public:
|
|
|
|
using OpConversionPattern::OpConversionPattern;
|
|
|
|
LogicalResult
|
|
|
|
matchAndRewrite(FuncOp func, ArrayRef<Value> operands,
|
|
|
|
ConversionPatternRewriter &rewriter) const override {
|
|
|
|
MLIRContext *context = func.getContext();
|
|
|
|
auto typeBoundIdent = Identifier::get("torch.type_bound", context);
|
|
|
|
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())) {
|
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 (type.value().isa<NonValueTensorType>()) {
|
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();
|
|
|
|
if (!bound.isa<ValueTensorType>())
|
|
|
|
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;
|
2021-06-15 02:36:10 +08:00
|
|
|
} else if (auto none = type.value().dyn_cast<Torch::NoneType>()) {
|
2021-04-02 08:36:18 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// TODO: add tuple type.
|
|
|
|
conversion.addInputs(type.index(), type.value());
|
|
|
|
}
|
|
|
|
SmallVector<Type> newResultTypes;
|
|
|
|
for (auto type : func.getType().getResults()) {
|
2021-06-15 02:36:10 +08:00
|
|
|
if (auto none = type.dyn_cast<Torch::NoneType>()) {
|
2021-04-02 08:36:18 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
newResultTypes.push_back(type);
|
|
|
|
}
|
|
|
|
rewriter.applySignatureConversion(&func.getBody(), conversion,
|
|
|
|
typeConverter);
|
|
|
|
rewriter.updateRootInPlace(func, [&] {
|
|
|
|
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 {
|
|
|
|
class AdjustCallingConventionForCall : public OpConversionPattern<CallOp> {
|
|
|
|
public:
|
|
|
|
AdjustCallingConventionForCall(TypeConverter &converter, MLIRContext *context,
|
|
|
|
TypeBoundMap &typeBoundMap)
|
|
|
|
: OpConversionPattern<CallOp>(converter, context),
|
|
|
|
typeBoundMap(typeBoundMap) {}
|
|
|
|
LogicalResult
|
|
|
|
matchAndRewrite(CallOp call, ArrayRef<Value> operands,
|
|
|
|
ConversionPatternRewriter &rewriter) const override {
|
|
|
|
SmallVector<Type> convertedResults;
|
|
|
|
if (failed(typeConverter->convertTypes(call.getResultTypes(),
|
|
|
|
convertedResults)))
|
|
|
|
return failure();
|
|
|
|
|
|
|
|
SmallVector<Value> newOperands;
|
|
|
|
for (auto operand : llvm::enumerate(operands)) {
|
2021-06-15 02:36:10 +08:00
|
|
|
if (operand.value().getType().isa<Torch::NoneType>())
|
2021-04-02 08:36:18 +08:00
|
|
|
continue;
|
|
|
|
auto it = typeBoundMap.find({call.callee(), operand.index()});
|
|
|
|
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());
|
|
|
|
}
|
|
|
|
|
|
|
|
CallOp newCall = rewriter.create<CallOp>(call.getLoc(), call.callee(),
|
|
|
|
convertedResults, newOperands);
|
|
|
|
int newOpResultIdx = 0;
|
|
|
|
SmallVector<Value> newResults;
|
|
|
|
for (auto type : call.getResultTypes()) {
|
2021-06-15 02:36:10 +08:00
|
|
|
if (type.isa<Torch::NoneType>()) {
|
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;
|
|
|
|
}
|
|
|
|
newResults.push_back(newCall.getResult(newOpResultIdx++));
|
|
|
|
}
|
|
|
|
rewriter.replaceOp(call, newResults);
|
|
|
|
return success();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
TypeBoundMap &typeBoundMap;
|
|
|
|
};
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
class AdjustCallingConventionForReturn : public OpConversionPattern<ReturnOp> {
|
|
|
|
public:
|
|
|
|
using OpConversionPattern::OpConversionPattern;
|
|
|
|
LogicalResult
|
|
|
|
matchAndRewrite(ReturnOp op, ArrayRef<Value> operands,
|
|
|
|
ConversionPatternRewriter &rewriter) const override {
|
|
|
|
|
|
|
|
SmallVector<Value> newOperands;
|
|
|
|
for (auto operand : llvm::enumerate(operands)) {
|
|
|
|
if (!operand.value())
|
|
|
|
continue;
|
2021-06-15 02:36:10 +08:00
|
|
|
if (operand.value().getType().isa<Torch::NoneType>())
|
2021-04-02 08:36:18 +08:00
|
|
|
continue;
|
|
|
|
newOperands.push_back(operand.value());
|
|
|
|
}
|
|
|
|
rewriter.replaceOpWithNewOp<ReturnOp>(op, newOperands);
|
|
|
|
return success();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
static LogicalResult adjustCallingConventions(FuncOp func,
|
|
|
|
TypeBoundMap &typeBoundMap) {
|
|
|
|
MLIRContext *context = func.getContext();
|
|
|
|
RewritePatternSet patterns(context);
|
|
|
|
// TODO: TupleTypes
|
|
|
|
TypeConverter typeConverter;
|
|
|
|
typeConverter.addConversion([](Type type) { return type; });
|
|
|
|
typeConverter.addConversion(
|
2021-06-15 02:36:10 +08:00
|
|
|
[](Torch::NoneType type,
|
2021-04-02 08:36:18 +08:00
|
|
|
SmallVectorImpl<Type> &types) -> Optional<LogicalResult> {
|
|
|
|
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);
|
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
|
|
|
assert(inputs[0].getType().isa<BaseTensorType>());
|
|
|
|
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);
|
|
|
|
target.addDynamicallyLegalOp<FuncOp>([](FuncOp func) {
|
|
|
|
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++) {
|
2021-06-15 02:36:10 +08:00
|
|
|
if (func.getType().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([&](CallOp op) { opsInOriginalProgram.insert(op.getOperation()); });
|
|
|
|
func.walk(
|
|
|
|
[&](ReturnOp op) { opsInOriginalProgram.insert(op.getOperation()); });
|
|
|
|
target.addDynamicallyLegalOp<CallOp>([&](CallOp op) {
|
|
|
|
return !opsInOriginalProgram.contains(op.getOperation());
|
|
|
|
});
|
|
|
|
target.addDynamicallyLegalOp<ReturnOp>([&](ReturnOp op) {
|
|
|
|
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-04-02 08:36:18 +08:00
|
|
|
// We don't know how to rewrite it, so mark it as illegal.
|
|
|
|
target.addIllegalOp<CallIndirectOp>();
|
|
|
|
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;
|
|
|
|
for (auto func : module.getOps<FuncOp>()) {
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (auto func : module.getOps<FuncOp>()) {
|
|
|
|
if (failed(adjustCallingConventions(func, typeBoundMap)))
|
|
|
|
return signalPassFailure();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
std::unique_ptr<OperationPass<ModuleOp>>
|
|
|
|
mlir::NPCOMP::Torch::createAdjustCallingConventionsPass() {
|
|
|
|
return std::make_unique<AdjustCallingConventionsPass>();
|
|
|
|
}
|