MLIR 24.0.0git
TosaToLinalgNamedPass.cpp
Go to the documentation of this file.
1//===- TosaToLinalgPass.cpp - Lowering Tosa to Linalg Dialect -------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This transformation pass legalizes Tosa operations to the Linalg dialect.
10//
11//===----------------------------------------------------------------------===//
12
14
27
28namespace mlir {
29#define GEN_PASS_DEF_TOSATOLINALGNAMED
30#include "mlir/Conversion/Passes.h.inc"
31} // namespace mlir
32
33using namespace mlir;
34
35namespace {
36struct TosaToLinalgNamed
37 : public impl::TosaToLinalgNamedBase<TosaToLinalgNamed> {
38public:
39 TosaToLinalgNamed(const TosaToLinalgNamedOptions &options)
40 : impl::TosaToLinalgNamedBase<TosaToLinalgNamed>(options) {}
41
42 void getDependentDialects(DialectRegistry &registry) const override {
43 registry.insert<arith::ArithDialect, cf::ControlFlowDialect,
44 linalg::LinalgDialect, math::MathDialect,
45 tensor::TensorDialect, scf::SCFDialect>();
46 }
47
48 void runOnOperation() override {
49 TypeConverter converter;
51
52 RewritePatternSet patterns(&getContext());
53 ConversionTarget target(getContext());
54 target.addLegalDialect<cf::ControlFlowDialect, linalg::LinalgDialect,
55 tosa::TosaDialect, tensor::TensorDialect,
56 scf::SCFDialect>();
57
58 // Not every TOSA op can be legalized to linalg.
59 target.addIllegalOp<tosa::Conv2DOp>();
60 target.addIllegalOp<tosa::Conv3DOp>();
61 target.addIllegalOp<tosa::DepthwiseConv2DOp>();
62 target.addIllegalOp<tosa::MaxPool2dOp>();
63 target.addIllegalOp<tosa::MaxPool2dAdaptiveOp>();
64 target.addIllegalOp<tosa::AvgPool2dOp>();
65 target.addIllegalOp<tosa::AvgPool2dAdaptiveOp>();
66 target.addIllegalOp<tosa::MatMulOp>();
67 // Linalg has a quantized batch matmul, but no quantized transpose-B
68 // variant. Defer nonzero zero points to the generic pass's adjusted
69 // contraction.
70 target.addDynamicallyLegalOp<tosa::MatMulTOp>([](tosa::MatMulTOp op) {
71 FailureOr<int64_t> aZp = op.getAZeroPoint();
72 FailureOr<int64_t> bZp = op.getBZeroPoint();
73 return succeeded(aZp) && succeeded(bZp) && (*aZp != 0 || *bZp != 0);
74 });
75 target.addIllegalOp<tosa::TransposeOp>();
76
77 // TOSA elementwise ops are legal unless they can be lowered to a
78 // linalg.elementwise named op.
79 target.addDynamicallyLegalOp<
80 tosa::AbsOp, tosa::CeilOp, tosa::FloorOp, tosa::ExpOp, tosa::LogOp,
81 tosa::RsqrtOp, tosa::SinOp, tosa::CosOp, tosa::TanhOp, tosa::ErfOp,
82 tosa::ReciprocalOp, tosa::PowOp, tosa::AddOp, tosa::SubOp,
83 tosa::IntDivOp, tosa::MulOp, tosa::NegateOp, tosa::MaximumOp,
84 tosa::MinimumOp, tosa::SelectOp>([](Operation *op) {
85 return !tosa::isConvertibleToLinalgElementwise(op).has_value();
86 });
87
88 target.markUnknownOpDynamicallyLegal([](Operation *) { return true; });
89
90 FunctionOpInterface func = getOperation();
91 TosaToLinalgNamedOptions options;
92 options.preferConv2DKernelLayoutHWCF = preferConv2DKernelLayoutHWCF;
93 options.allowNonFinites = allowNonFinites;
95 options);
96 if (failed(applyFullConversion(func, target, std::move(patterns))))
97 signalPassFailure();
98 }
99};
100} // namespace
101
102std::unique_ptr<Pass>
103mlir::tosa::createTosaToLinalgNamed(const TosaToLinalgNamedOptions &options) {
104 return std::make_unique<TosaToLinalgNamed>(options);
105}
b getContext())
static llvm::ManagedStatic< PassManagerOptions > options
detail::InFlightRemark failed(Location loc, RemarkOpts opts)
Report an optimization remark that failed.
Definition Remarks.h:886
std::unique_ptr< Pass > createTosaToLinalgNamed(const TosaToLinalgNamedOptions &options=TosaToLinalgNamedOptions())
void populateTosaTypeConversion(TypeConverter &converter)
void populateTosaToLinalgNamedConversionPatterns(const TypeConverter &converter, RewritePatternSet *patterns, const TosaToLinalgNamedOptions &options)
Populates conversion passes from TOSA dialect to Linalg named operations.
std::optional< linalg::ElementwiseKind > isConvertibleToLinalgElementwise(Operation *op)
Returns the linalg.elementwise kind that the TosaToLinalgNamed conversion lowers op to,...
Include the generated interface declarations.