1//===- TosaToTensorPass.cpp - Lowering Tosa to Tensor 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 Tensor dialect.
10//
11//===----------------------------------------------------------------------===//
12
13#include "mlir/Conversion/TosaToTensor/TosaToTensor.h"
14
15#include "mlir/Dialect/Arith/IR/Arith.h"
16#include "mlir/Dialect/ControlFlow/IR/ControlFlow.h"
17#include "mlir/Dialect/Tensor/IR/Tensor.h"
18#include "mlir/Dialect/Tosa/IR/TosaOps.h"
19#include "mlir/Dialect/Tosa/Transforms/Passes.h"
20#include "mlir/IR/PatternMatch.h"
21#include "mlir/Transforms/DialectConversion.h"
22
23namespace mlir {
24#define GEN_PASS_DEF_TOSATOTENSORPASS
25#include "mlir/Conversion/Passes.h.inc"
26} // namespace mlir
27
28using namespace mlir;
29using namespace tosa;
30
31namespace {
32struct TosaToTensor : public impl::TosaToTensorPassBase<TosaToTensor> {
33public:
34 void runOnOperation() override {
35 RewritePatternSet patterns(&getContext());
36 ConversionTarget target(getContext());
37 target.addIllegalOp<tosa::ConcatOp>();
38 target.addIllegalOp<tosa::ReshapeOp>();
39 target.addIllegalOp<tosa::SliceOp>();
40 target.addIllegalOp<tosa::PadOp>();
41 target.addLegalDialect<arith::ArithDialect>();
42 target.addLegalDialect<cf::ControlFlowDialect>();
43 target.addLegalDialect<tensor::TensorDialect>();
44
45 TypeConverter converter;
46 mlir::tosa::populateTosaTypeConversion(converter);
47
48 mlir::tosa::populateTosaToTensorConversionPatterns(converter, patterns: &patterns);
49
50 if (failed(Result: applyPartialConversion(op: getOperation(), target,
51 patterns: std::move(patterns))))
52 signalPassFailure();
53 }
54};
55} // namespace
56