MLIR  17.0.0git
TosaToLinalgNamedPass.cpp
Go to the documentation of this file.
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 
14 
24 #include "mlir/IR/PatternMatch.h"
25 #include "mlir/Pass/PassManager.h"
28 
29 namespace mlir {
30 #define GEN_PASS_DEF_TOSATOLINALGNAMED
31 #include "mlir/Conversion/Passes.h.inc"
32 } // namespace mlir
33 
34 using namespace mlir;
35 
36 namespace {
37 struct TosaToLinalgNamed
38  : public impl::TosaToLinalgNamedBase<TosaToLinalgNamed> {
39 public:
40  void getDependentDialects(DialectRegistry &registry) const override {
41  registry
42  .insert<arith::ArithDialect, linalg::LinalgDialect, math::MathDialect,
43  tensor::TensorDialect, scf::SCFDialect>();
44  }
45 
46  void runOnOperation() override {
47  RewritePatternSet patterns(&getContext());
48  ConversionTarget target(getContext());
49  target.addLegalDialect<linalg::LinalgDialect, tosa::TosaDialect,
50  tensor::TensorDialect, scf::SCFDialect>();
51 
52  // Not every TOSA op can be legalized to linalg.
53  target.addIllegalOp<tosa::Conv2DOp>();
54  target.addIllegalOp<tosa::Conv3DOp>();
55  target.addIllegalOp<tosa::DepthwiseConv2DOp>();
56  target.addIllegalOp<tosa::MaxPool2dOp>();
57  target.addIllegalOp<tosa::AvgPool2dOp>();
58  target.addIllegalOp<tosa::MatMulOp>();
59  target.addIllegalOp<tosa::FullyConnectedOp>();
60 
61  target.markUnknownOpDynamicallyLegal([](Operation *) { return true; });
62 
63  FunctionOpInterface func = getOperation();
65  if (failed(applyFullConversion(func, target, std::move(patterns))))
66  signalPassFailure();
67  }
68 };
69 } // namespace
70 
71 std::unique_ptr<Pass> mlir::tosa::createTosaToLinalgNamed() {
72  return std::make_unique<TosaToLinalgNamed>();
73 }
This class describes a specific conversion target.
void addLegalDialect(StringRef name, Names... names)
Register the operations of the given dialects as legal.
void markUnknownOpDynamicallyLegal(const DynamicLegalityCallbackFn &fn)
Register unknown operations as dynamically legal.
void addIllegalOp(OperationName op)
Register the given operation as illegal, i.e.
The DialectRegistry maps a dialect namespace to a constructor for the matching dialect.
Operation is the basic unit of execution within MLIR.
Definition: Operation.h:75
std::unique_ptr< Pass > createTosaToLinalgNamed()
void populateTosaToLinalgNamedConversionPatterns(RewritePatternSet *patterns)
Populates conversion passes from TOSA dialect to Linalg named operations.
Include the generated interface declarations.
LogicalResult applyFullConversion(ArrayRef< Operation * > ops, ConversionTarget &target, const FrozenRewritePatternSet &patterns)
Apply a complete conversion on the given operations, and all nested operations.
bool failed(LogicalResult result)
Utility function that returns true if the provided LogicalResult corresponds to a failure value.
Definition: LogicalResult.h:72