| 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 | |
| 13 | #include "mlir/Conversion/TosaToLinalg/TosaToLinalg.h" |
| 14 | |
| 15 | #include "mlir/Dialect/Arith/IR/Arith.h" |
| 16 | #include "mlir/Dialect/Func/IR/FuncOps.h" |
| 17 | #include "mlir/Dialect/Linalg/IR/LinalgDialect.h" |
| 18 | #include "mlir/Dialect/Math/IR/Math.h" |
| 19 | #include "mlir/Dialect/SCF/IR/SCFDialect.h" |
| 20 | #include "mlir/Dialect/Tensor/IR/Tensor.h" |
| 21 | #include "mlir/Dialect/Tosa/IR/TosaOps.h" |
| 22 | #include "mlir/Dialect/Tosa/Transforms/Passes.h" |
| 23 | #include "mlir/IR/PatternMatch.h" |
| 24 | #include "mlir/Pass/PassManager.h" |
| 25 | #include "mlir/Transforms/DialectConversion.h" |
| 26 | |
| 27 | namespace mlir { |
| 28 | #define GEN_PASS_DEF_TOSATOLINALGNAMED |
| 29 | #include "mlir/Conversion/Passes.h.inc" |
| 30 | } // namespace mlir |
| 31 | |
| 32 | using namespace mlir; |
| 33 | |
| 34 | namespace { |
| 35 | struct TosaToLinalgNamed |
| 36 | : public impl::TosaToLinalgNamedBase<TosaToLinalgNamed> { |
| 37 | public: |
| 38 | TosaToLinalgNamed(const TosaToLinalgNamedOptions &options) |
| 39 | : impl::TosaToLinalgNamedBase<TosaToLinalgNamed>(options) {} |
| 40 | |
| 41 | void getDependentDialects(DialectRegistry ®istry) const override { |
| 42 | registry |
| 43 | .insert<arith::ArithDialect, linalg::LinalgDialect, math::MathDialect, |
| 44 | tensor::TensorDialect, scf::SCFDialect>(); |
| 45 | } |
| 46 | |
| 47 | void runOnOperation() override { |
| 48 | TypeConverter converter; |
| 49 | tosa::populateTosaTypeConversion(converter); |
| 50 | |
| 51 | RewritePatternSet patterns(&getContext()); |
| 52 | ConversionTarget target(getContext()); |
| 53 | target.addLegalDialect<linalg::LinalgDialect, tosa::TosaDialect, |
| 54 | tensor::TensorDialect, scf::SCFDialect>(); |
| 55 | |
| 56 | // Not every TOSA op can be legalized to linalg. |
| 57 | target.addIllegalOp<tosa::Conv2DOp>(); |
| 58 | target.addIllegalOp<tosa::Conv3DOp>(); |
| 59 | target.addIllegalOp<tosa::DepthwiseConv2DOp>(); |
| 60 | target.addIllegalOp<tosa::MaxPool2dOp>(); |
| 61 | target.addIllegalOp<tosa::MaxPool2dAdaptiveOp>(); |
| 62 | target.addIllegalOp<tosa::AvgPool2dOp>(); |
| 63 | target.addIllegalOp<tosa::AvgPool2dAdaptiveOp>(); |
| 64 | target.addIllegalOp<tosa::MatMulOp>(); |
| 65 | // Linalg has a quantized batch matmul, but no quantized transpose-B |
| 66 | // variant. Defer nonzero zero points to the generic pass's adjusted |
| 67 | // contraction. |
| 68 | target.addDynamicallyLegalOp<tosa::MatMulTOp>(callback: [](tosa::MatMulTOp op) { |
| 69 | FailureOr<int64_t> aZp = op.getAZeroPoint(); |
| 70 | FailureOr<int64_t> bZp = op.getBZeroPoint(); |
| 71 | return succeeded(Result: aZp) && succeeded(Result: bZp) && (*aZp != 0 || *bZp != 0); |
| 72 | }); |
| 73 | target.addIllegalOp<tosa::TransposeOp>(); |
| 74 | |
| 75 | // TOSA elementwise ops are legal unless they can be lowered to a |
| 76 | // linalg.elementwise named op. |
| 77 | target.addDynamicallyLegalOp< |
| 78 | tosa::AbsOp, tosa::CeilOp, tosa::FloorOp, tosa::ExpOp, tosa::LogOp, |
| 79 | tosa::RsqrtOp, tosa::SinOp, tosa::CosOp, tosa::TanhOp, tosa::ErfOp, |
| 80 | tosa::ReciprocalOp, tosa::PowOp, tosa::AddOp, tosa::SubOp, |
| 81 | tosa::IntDivOp, tosa::MulOp, tosa::NegateOp, tosa::MaximumOp, |
| 82 | tosa::MinimumOp, tosa::SelectOp>(callback: [](Operation *op) { |
| 83 | return !tosa::isConvertibleToLinalgElementwise(op).has_value(); |
| 84 | }); |
| 85 | |
| 86 | target.markUnknownOpDynamicallyLegal(fn: [](Operation *) { return true; }); |
| 87 | |
| 88 | FunctionOpInterface func = getOperation(); |
| 89 | TosaToLinalgNamedOptions options; |
| 90 | options.preferConv2DKernelLayoutHWCF = preferConv2DKernelLayoutHWCF; |
| 91 | options.allowNonFinites = allowNonFinites; |
| 92 | tosa::populateTosaToLinalgNamedConversionPatterns(converter, patterns: &patterns, |
| 93 | options); |
| 94 | if (failed(Result: applyFullConversion(op: func, target, patterns: std::move(patterns)))) |
| 95 | signalPassFailure(); |
| 96 | } |
| 97 | }; |
| 98 | } // namespace |
| 99 | |
| 100 | std::unique_ptr<Pass> |
| 101 | mlir::tosa::createTosaToLinalgNamed(const TosaToLinalgNamedOptions &options) { |
| 102 | return std::make_unique<TosaToLinalgNamed>(args: options); |
| 103 | } |
| 104 | |