Rob Suderman | 1d973b7 | 2021-01-05 22:26:48 | [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" |
Mogball | a54f4ea | 2021-10-12 23:14:57 | [diff] [blame] | 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" |
gysit | b7f2c10 | 2021-12-15 12:14:35 | [diff] [blame] | 17 | #include "mlir/Dialect/Linalg/IR/Linalg.h" |
Stephan Herhut | 4348d8a | 2021-02-12 09:34:42 | [diff] [blame] | 18 | #include "mlir/Dialect/Math/IR/Math.h" |
natashaknk | d4d50e4 | 2021-09-09 22:57:22 | [diff] [blame] | 19 | #include "mlir/Dialect/SCF/SCF.h" |
Rob Suderman | 4157a07 | 2021-03-19 23:04:50 | [diff] [blame] | 20 | #include "mlir/Dialect/Tensor/IR/Tensor.h" |
Rob Suderman | 1d973b7 | 2021-01-05 22:26:48 | [diff] [blame] | 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" |
Rob Suderman | f0cb77d | 2021-12-24 00:25:53 | [diff] [blame] | 29 | #include "mlir/Transforms/Passes.h" |
Rob Suderman | 1d973b7 | 2021-01-05 22:26:48 | [diff] [blame] | 30 | |
| 31 | using namespace mlir; |
| 32 | |
| 33 | namespace { |
Geoffrey Martin-Noble | efc6fe96 | 2021-10-15 22:40:42 | [diff] [blame] | 34 | struct TosaToLinalg : public TosaToLinalgBase<TosaToLinalg> { |
Rob Suderman | 1d973b7 | 2021-01-05 22:26:48 | [diff] [blame] | 35 | public: |
| 36 | void getDependentDialects(DialectRegistry ®istry) const override { |
Mogball | a54f4ea | 2021-10-12 23:14:57 | [diff] [blame] | 37 | registry.insert<arith::ArithmeticDialect, linalg::LinalgDialect, |
River Riddle | 23aa5a7 | 2022-02-26 22:49:54 | [diff] [blame^] | 38 | math::MathDialect, func::FuncDialect, tensor::TensorDialect, |
| 39 | scf::SCFDialect>(); |
Rob Suderman | 1d973b7 | 2021-01-05 22:26:48 | [diff] [blame] | 40 | } |
| 41 | |
River Riddle | 4157455 | 2022-01-04 23:41:17 | [diff] [blame] | 42 | void runOnOperation() override { |
Chris Lattner | dc4e913 | 2021-03-22 23:58:34 | [diff] [blame] | 43 | RewritePatternSet patterns(&getContext()); |
Rob Suderman | 1d973b7 | 2021-01-05 22:26:48 | [diff] [blame] | 44 | ConversionTarget target(getContext()); |
River Riddle | 23aa5a7 | 2022-02-26 22:49:54 | [diff] [blame^] | 45 | target.addLegalDialect<linalg::LinalgDialect, func::FuncDialect, |
natashaknk | d4d50e4 | 2021-09-09 22:57:22 | [diff] [blame] | 46 | tensor::TensorDialect, scf::SCFDialect>(); |
Rob Suderman | 1d973b7 | 2021-01-05 22:26:48 | [diff] [blame] | 47 | target.addIllegalDialect<tosa::TosaDialect>(); |
Rob Suderman | 286a9d4 | 2021-03-18 23:14:05 | [diff] [blame] | 48 | |
| 49 | // Not every TOSA op can be legalized to linalg. |
| 50 | target.addLegalOp<tosa::ApplyScaleOp>(); |
| 51 | target.addLegalOp<tosa::IfOp>(); |
| 52 | target.addLegalOp<tosa::ConstOp>(); |
| 53 | target.addLegalOp<tosa::WhileOp>(); |
Rob Suderman | 54eec7c | 2021-11-23 03:43:06 | [diff] [blame] | 54 | target.addLegalOp<tosa::SliceOp>(); |
Rob Suderman | 286a9d4 | 2021-03-18 23:14:05 | [diff] [blame] | 55 | |
Rob Suderman | 1d973b7 | 2021-01-05 22:26:48 | [diff] [blame] | 56 | target.markUnknownOpDynamicallyLegal([](Operation *) { return true; }); |
| 57 | |
River Riddle | 4157455 | 2022-01-04 23:41:17 | [diff] [blame] | 58 | FuncOp func = getOperation(); |
Geoffrey Martin-Noble | efc6fe96 | 2021-10-15 22:40:42 | [diff] [blame] | 59 | mlir::tosa::populateTosaToLinalgConversionPatterns(&patterns); |
Rob Suderman | 1d973b7 | 2021-01-05 22:26:48 | [diff] [blame] | 60 | if (failed(applyFullConversion(func, target, std::move(patterns)))) |
| 61 | signalPassFailure(); |
| 62 | } |
| 63 | }; |
| 64 | } // namespace |
| 65 | |
Geoffrey Martin-Noble | efc6fe96 | 2021-10-15 22:40:42 | [diff] [blame] | 66 | std::unique_ptr<Pass> mlir::tosa::createTosaToLinalg() { |
| 67 | return std::make_unique<TosaToLinalg>(); |
Rob Suderman | 1d973b7 | 2021-01-05 22:26:48 | [diff] [blame] | 68 | } |
| 69 | |
not-jenni | fd2550d | 2022-02-28 23:30:13 | [diff] [blame] | 70 | void mlir::tosa::addTosaToLinalgPasses(OpPassManager &pm, |
| 71 | bool disableTosaDecompositions) { |
Rob Suderman | 173fce4 | 2022-01-13 17:54:42 | [diff] [blame] | 72 | // Optional decompositions are designed to benefit linalg. |
not-jenni | fd2550d | 2022-02-28 23:30:13 | [diff] [blame] | 73 | if (!disableTosaDecompositions) |
| 74 | pm.addNestedPass<FuncOp>(mlir::tosa::createTosaOptionalDecompositions()); |
Rob Suderman | f0cb77d | 2021-12-24 00:25:53 | [diff] [blame] | 75 | pm.addNestedPass<FuncOp>(mlir::createCanonicalizerPass()); |
Rob Suderman | 173fce4 | 2022-01-13 17:54:42 | [diff] [blame] | 76 | |
| 77 | pm.addNestedPass<FuncOp>(tosa::createTosaMakeBroadcastablePass()); |
| 78 | pm.addNestedPass<FuncOp>(tosa::createTosaToLinalgNamed()); |
| 79 | pm.addNestedPass<FuncOp>(mlir::createCanonicalizerPass()); |
| 80 | pm.addNestedPass<FuncOp>(tosa::createTosaMakeBroadcastablePass()); |
| 81 | pm.addNestedPass<FuncOp>(tosa::createTosaToLinalg()); |
Rob Suderman | 1d973b7 | 2021-01-05 22:26:48 | [diff] [blame] | 82 | } |