blob: 391b4ba5b6412eb07495a3413a19b243415db75c [file] [log] [blame]
Rob Sudermanf0cb77d2021-12-24 00:25:531//===- 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 Riddle23aa5a72022-02-26 22:49:5416#include "mlir/Dialect/Func/IR/FuncOps.h"
Rob Sudermanf0cb77d2021-12-24 00:25:5317#include "mlir/Dialect/Linalg/IR/Linalg.h"
18#include "mlir/Dialect/Math/IR/Math.h"
19#include "mlir/Dialect/SCF/SCF.h"
Rob Sudermanf0cb77d2021-12-24 00:25:5320#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
30using namespace mlir;
31
32namespace {
33struct TosaToLinalgNamed : public TosaToLinalgNamedBase<TosaToLinalgNamed> {
34public:
35 void getDependentDialects(DialectRegistry &registry) const override {
36 registry.insert<arith::ArithmeticDialect, linalg::LinalgDialect,
River Riddle23aa5a72022-02-26 22:49:5437 math::MathDialect, func::FuncDialect, tensor::TensorDialect,
38 scf::SCFDialect>();
Rob Sudermanf0cb77d2021-12-24 00:25:5339 }
40
River Riddle41574552022-01-04 23:41:1741 void runOnOperation() override {
Rob Sudermanf0cb77d2021-12-24 00:25:5342 RewritePatternSet patterns(&getContext());
43 ConversionTarget target(getContext());
River Riddle23aa5a72022-02-26 22:49:5444 target.addLegalDialect<linalg::LinalgDialect, func::FuncDialect,
Rob Sudermanf0cb77d2021-12-24 00:25:5345 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 Riddle41574552022-01-04 23:41:1758 FuncOp func = getOperation();
Rob Sudermanf0cb77d2021-12-24 00:25:5359 mlir::tosa::populateTosaToLinalgNamedConversionPatterns(&patterns);
60 if (failed(applyFullConversion(func, target, std::move(patterns))))
61 signalPassFailure();
62 }
63};
64} // namespace
65
66std::unique_ptr<Pass> mlir::tosa::createTosaToLinalgNamed() {
67 return std::make_unique<TosaToLinalgNamed>();
68}