MLIR  21.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  TosaToLinalgNamed(const TosaToLinalgNamedOptions &options)
41  : impl::TosaToLinalgNamedBase<TosaToLinalgNamed>(options) {}
42 
43  void getDependentDialects(DialectRegistry &registry) const override {
44  registry
45  .insert<arith::ArithDialect, linalg::LinalgDialect, math::MathDialect,
46  tensor::TensorDialect, scf::SCFDialect>();
47  }
48 
49  void runOnOperation() override {
50  TypeConverter converter;
52 
54  ConversionTarget target(getContext());
55  target.addLegalDialect<linalg::LinalgDialect, tosa::TosaDialect,
56  tensor::TensorDialect, scf::SCFDialect>();
57 
58  // Not every TOSA op can be legalized to linalg.
59  target.addIllegalOp<tosa::Conv2DOp>();
60  target.addIllegalOp<tosa::Conv3DOp>();
61  target.addIllegalOp<tosa::DepthwiseConv2DOp>();
62  target.addIllegalOp<tosa::MaxPool2dOp>();
63  target.addIllegalOp<tosa::AvgPool2dOp>();
64  target.addIllegalOp<tosa::MatMulOp>();
65  target.addIllegalOp<tosa::TransposeOp>();
66 
67  target.markUnknownOpDynamicallyLegal([](Operation *) { return true; });
68 
69  FunctionOpInterface func = getOperation();
70  TosaToLinalgNamedOptions options;
71  options.preferConv2DKernelLayoutHWCF = preferConv2DKernelLayoutHWCF;
73  options);
74  if (failed(applyFullConversion(func, target, std::move(patterns))))
75  signalPassFailure();
76  }
77 };
78 } // namespace
79 
80 std::unique_ptr<Pass>
81 mlir::tosa::createTosaToLinalgNamed(const TosaToLinalgNamedOptions &options) {
82  return std::make_unique<TosaToLinalgNamed>(options);
83 }
static MLIRContext * getContext(OpFoldResult val)
static llvm::ManagedStatic< PassManagerOptions > options
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:88
Type conversion class.
std::unique_ptr< Pass > createTosaToLinalgNamed(const TosaToLinalgNamedOptions &options=TosaToLinalgNamedOptions())
void populateTosaTypeConversion(TypeConverter &converter)
void populateTosaToLinalgNamedConversionPatterns(const TypeConverter &converter, RewritePatternSet *patterns, const TosaToLinalgNamedOptions &options)
Populates conversion passes from TOSA dialect to Linalg named operations.
Include the generated interface declarations.
LogicalResult applyFullConversion(ArrayRef< Operation * > ops, const ConversionTarget &target, const FrozenRewritePatternSet &patterns, ConversionConfig config=ConversionConfig())
Apply a complete conversion on the given operations, and all nested operations.
const FrozenRewritePatternSet & patterns