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/Linalg.h"
18#include "mlir/Dialect/Math/IR/Math.h"
19#include "mlir/Dialect/SCF/IR/SCF.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
27namespace mlir {
28#define GEN_PASS_DEF_TOSATOLINALGNAMED
29#include "mlir/Conversion/Passes.h.inc"
30} // namespace mlir
31
32using namespace mlir;
33
34namespace {
35struct TosaToLinalgNamed
36 : public impl::TosaToLinalgNamedBase<TosaToLinalgNamed> {
37public:
38 TosaToLinalgNamed(const TosaToLinalgNamedOptions &options)
39 : impl::TosaToLinalgNamedBase<TosaToLinalgNamed>(options) {}
40
41 void getDependentDialects(DialectRegistry &registry) 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::AvgPool2dOp>();
62 target.addIllegalOp<tosa::MatMulOp>();
63 target.addIllegalOp<tosa::TransposeOp>();
64
65 target.markUnknownOpDynamicallyLegal(fn: [](Operation *) { return true; });
66
67 FunctionOpInterface func = getOperation();
68 TosaToLinalgNamedOptions options;
69 options.preferConv2DKernelLayoutHWCF = preferConv2DKernelLayoutHWCF;
70 tosa::populateTosaToLinalgNamedConversionPatterns(converter, patterns: &patterns,
71 options);
72 if (failed(Result: applyFullConversion(op: func, target, patterns: std::move(patterns))))
73 signalPassFailure();
74 }
75};
76} // namespace
77
78std::unique_ptr<Pass>
79mlir::tosa::createTosaToLinalgNamed(const TosaToLinalgNamedOptions &options) {
80 return std::make_unique<TosaToLinalgNamed>(args: options);
81}
82

source code of mlir/lib/Conversion/TosaToLinalg/TosaToLinalgNamedPass.cpp