MLIR  16.0.0git
TosaToLinalgPass.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 
13 #include "../PassDetail.h"
24 #include "mlir/IR/PatternMatch.h"
25 #include "mlir/Pass/PassManager.h"
28 #include "mlir/Transforms/Passes.h"
29 
30 using namespace mlir;
31 
32 namespace {
33 struct TosaToLinalg : public TosaToLinalgBase<TosaToLinalg> {
34 public:
35  void getDependentDialects(DialectRegistry &registry) const override {
36  registry
37  .insert<arith::ArithmeticDialect, linalg::LinalgDialect,
38  math::MathDialect, tensor::TensorDialect, scf::SCFDialect>();
39  }
40 
41  void runOnOperation() override {
42  RewritePatternSet patterns(&getContext());
43  ConversionTarget target(getContext());
44  target.addLegalDialect<linalg::LinalgDialect, tensor::TensorDialect,
45  scf::SCFDialect>();
46  target.addIllegalDialect<tosa::TosaDialect>();
47 
48  // Not every TOSA op can be legalized to linalg.
49  target.addLegalOp<tosa::ApplyScaleOp>();
50  target.addLegalOp<tosa::IfOp>();
51  target.addLegalOp<tosa::ConstOp>();
52  target.addLegalOp<tosa::WhileOp>();
53  target.addLegalOp<tosa::SliceOp>();
54 
55  target.markUnknownOpDynamicallyLegal([](Operation *) { return true; });
56 
57  FunctionOpInterface func = getOperation();
59  if (failed(applyFullConversion(func, target, std::move(patterns))))
60  signalPassFailure();
61  }
62 };
63 } // namespace
64 
65 std::unique_ptr<Pass> mlir::tosa::createTosaToLinalg() {
66  return std::make_unique<TosaToLinalg>();
67 }
68 
70  bool disableTosaDecompositions) {
71  // Optional decompositions are designed to benefit linalg.
72  if (!disableTosaDecompositions)
74  pm.addNestedPass<func::FuncOp>(createCanonicalizerPass());
75 
77  pm.addNestedPass<func::FuncOp>(tosa::createTosaToLinalgNamed());
78  pm.addNestedPass<func::FuncOp>(createCanonicalizerPass());
79  // TODO: Remove pass that operates on const tensor and enable optionality
82  pm.addNestedPass<func::FuncOp>(tosa::createTosaToLinalg());
83 }
Include the generated interface declarations.
std::unique_ptr< Pass > createTosaLayerwiseConstantFoldPass()
std::unique_ptr< Pass > createTosaToLinalg()
Operation is a basic unit of execution within MLIR.
Definition: Operation.h:28
std::unique_ptr< Pass > createTosaToLinalgNamed()
void addTosaToLinalgPasses(OpPassManager &pm, bool disableTosaDecompositions=false)
Populates passes to convert from TOSA to Linalg on buffers.
bool failed(LogicalResult result)
Utility function that returns true if the provided LogicalResult corresponds to a failure value...
Definition: LogicalResult.h:72
void addNestedPass(std::unique_ptr< Pass > pass)
Add the given pass to a nested pass manager for the given operation kind OpT.
Definition: PassManager.h:120
std::unique_ptr< Pass > createTosaMakeBroadcastablePass()
LogicalResult applyFullConversion(ArrayRef< Operation *> ops, ConversionTarget &target, const FrozenRewritePatternSet &patterns)
Apply a complete conversion on the given operations, and all nested operations.
std::unique_ptr< Pass > createTosaOptionalDecompositions()
The DialectRegistry maps a dialect namespace to a constructor for the matching dialect.
void populateTosaToLinalgConversionPatterns(RewritePatternSet *patterns)
Populates conversion passes from TOSA dialect to Linalg dialect.
This class describes a specific conversion target.
std::unique_ptr< Pass > createCanonicalizerPass()
Creates an instance of the Canonicalizer pass, configured with default settings (which can be overrid...
This class represents a pass manager that runs passes on either a specific operation type...
Definition: PassManager.h:52