Rob Suderman | f0cb77d | 2021-12-24 00:25:53 | [diff] [blame] | 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 "../PassDetail.h" |
| 14 | #include "mlir/Conversion/TosaToLinalg/TosaToLinalg.h" |
| 15 | #include "mlir/Dialect/Arithmetic/IR/Arithmetic.h" |
River Riddle | 23aa5a7 | 2022-02-26 22:49:54 | [diff] [blame^] | 16 | #include "mlir/Dialect/Func/IR/FuncOps.h" |
Rob Suderman | f0cb77d | 2021-12-24 00:25:53 | [diff] [blame] | 17 | #include "mlir/Dialect/Linalg/IR/Linalg.h" |
| 18 | #include "mlir/Dialect/Math/IR/Math.h" |
| 19 | #include "mlir/Dialect/SCF/SCF.h" |
Rob Suderman | f0cb77d | 2021-12-24 00:25:53 | [diff] [blame] | 20 | #include "mlir/Dialect/Tensor/IR/Tensor.h" |
| 21 | #include "mlir/Dialect/Tosa/IR/TosaOps.h" |
| 22 | #include "mlir/Dialect/Tosa/Transforms/PassDetail.h" |
| 23 | #include "mlir/Dialect/Tosa/Transforms/Passes.h" |
| 24 | #include "mlir/Dialect/Tosa/Utils/QuantUtils.h" |
| 25 | #include "mlir/IR/PatternMatch.h" |
| 26 | #include "mlir/Pass/PassManager.h" |
| 27 | #include "mlir/Transforms/DialectConversion.h" |
| 28 | #include "mlir/Transforms/GreedyPatternRewriteDriver.h" |
| 29 | |
| 30 | using namespace mlir; |
| 31 | |
| 32 | namespace { |
| 33 | struct TosaToLinalgNamed : public TosaToLinalgNamedBase<TosaToLinalgNamed> { |
| 34 | public: |
| 35 | void getDependentDialects(DialectRegistry ®istry) const override { |
| 36 | registry.insert<arith::ArithmeticDialect, linalg::LinalgDialect, |
River Riddle | 23aa5a7 | 2022-02-26 22:49:54 | [diff] [blame^] | 37 | math::MathDialect, func::FuncDialect, tensor::TensorDialect, |
| 38 | scf::SCFDialect>(); |
Rob Suderman | f0cb77d | 2021-12-24 00:25:53 | [diff] [blame] | 39 | } |
| 40 | |
River Riddle | 4157455 | 2022-01-04 23:41:17 | [diff] [blame] | 41 | void runOnOperation() override { |
Rob Suderman | f0cb77d | 2021-12-24 00:25:53 | [diff] [blame] | 42 | RewritePatternSet patterns(&getContext()); |
| 43 | ConversionTarget target(getContext()); |
River Riddle | 23aa5a7 | 2022-02-26 22:49:54 | [diff] [blame^] | 44 | target.addLegalDialect<linalg::LinalgDialect, func::FuncDialect, |
Rob Suderman | f0cb77d | 2021-12-24 00:25:53 | [diff] [blame] | 45 | tosa::TosaDialect, tensor::TensorDialect, |
| 46 | scf::SCFDialect>(); |
| 47 | |
| 48 | // Not every TOSA op can be legalized to linalg. |
| 49 | target.addIllegalOp<tosa::Conv2DOp>(); |
| 50 | target.addIllegalOp<tosa::DepthwiseConv2DOp>(); |
| 51 | target.addIllegalOp<tosa::MaxPool2dOp>(); |
| 52 | target.addIllegalOp<tosa::AvgPool2dOp>(); |
| 53 | target.addIllegalOp<tosa::MatMulOp>(); |
| 54 | target.addIllegalOp<tosa::FullyConnectedOp>(); |
| 55 | |
| 56 | target.markUnknownOpDynamicallyLegal([](Operation *) { return true; }); |
| 57 | |
River Riddle | 4157455 | 2022-01-04 23:41:17 | [diff] [blame] | 58 | FuncOp func = getOperation(); |
Rob Suderman | f0cb77d | 2021-12-24 00:25:53 | [diff] [blame] | 59 | mlir::tosa::populateTosaToLinalgNamedConversionPatterns(&patterns); |
| 60 | if (failed(applyFullConversion(func, target, std::move(patterns)))) |
| 61 | signalPassFailure(); |
| 62 | } |
| 63 | }; |
| 64 | } // namespace |
| 65 | |
| 66 | std::unique_ptr<Pass> mlir::tosa::createTosaToLinalgNamed() { |
| 67 | return std::make_unique<TosaToLinalgNamed>(); |
| 68 | } |