MLIR  19.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 
14 
24 #include "mlir/IR/PatternMatch.h"
25 #include "mlir/Pass/PassManager.h"
28 #include "mlir/Transforms/Passes.h"
29 
30 namespace mlir {
31 #define GEN_PASS_DEF_TOSATOLINALG
32 #include "mlir/Conversion/Passes.h.inc"
33 } // namespace mlir
34 
35 using namespace mlir;
36 
37 namespace {
38 struct TosaToLinalg : public impl::TosaToLinalgBase<TosaToLinalg> {
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, tensor::TensorDialect,
50  scf::SCFDialect>();
51  target.addIllegalDialect<tosa::TosaDialect>();
52 
53  // Not every TOSA op can be legalized to linalg.
54  target.addLegalOp<tosa::ApplyScaleOp>();
55  target.addLegalOp<tosa::IfOp>();
56  target.addLegalOp<tosa::ConstOp>();
57  target.addLegalOp<tosa::WhileOp>();
58  target.addLegalOp<tosa::ConcatOp>();
59  target.addLegalOp<tosa::SliceOp>();
60  target.addLegalOp<tosa::ReshapeOp>();
61  target.addLegalOp<tosa::PadOp>();
62 
63  target.markUnknownOpDynamicallyLegal([](Operation *) { return true; });
64 
65  FunctionOpInterface func = getOperation();
67  if (failed(applyFullConversion(func, target, std::move(patterns))))
68  signalPassFailure();
69  }
70 };
71 } // namespace
72 
73 std::unique_ptr<Pass> mlir::tosa::createTosaToLinalg() {
74  return std::make_unique<TosaToLinalg>();
75 }
76 
78  OpPassManager &pm, const TosaToLinalgOptions &options,
79  const TosaToLinalgNamedOptions &tosaToLinalgNamedOptions,
80  tosa::TosaValidationOptions const &validationOptions) {
81  // Optional decompositions are designed to benefit linalg.
82  if (!options.disableTosaDecompositions)
84  pm.addNestedPass<func::FuncOp>(createCanonicalizerPass());
85 
88  pm.addNestedPass<func::FuncOp>(
89  tosa::createTosaToLinalgNamed(tosaToLinalgNamedOptions));
90  pm.addNestedPass<func::FuncOp>(createCanonicalizerPass());
91  // TODO: Remove pass that operates on const tensor and enable optionality
93  {options.aggressiveReduceConstant}));
95  pm.addPass(tosa::createTosaValidation(validationOptions));
96  pm.addNestedPass<func::FuncOp>(tosa::createTosaToLinalg());
97 }
98 
99 //===----------------------------------------------------------------------===//
100 // Pipeline registration.
101 //===----------------------------------------------------------------------===//
102 
105  "tosa-to-linalg-pipeline",
106  "The default pipeline for converting TOSA operators to the equivalent "
107  "operations using the tensor operations in LinAlg as well as LinAlg "
108  "named operations.",
109  [](OpPassManager &pm) {
110  TosaToLinalgOptions tosaToLinalgOptions;
111  TosaToLinalgNamedOptions tosaToLinalgNamedOptions;
112  tosa::addTosaToLinalgPasses(pm, tosaToLinalgOptions,
113  tosaToLinalgNamedOptions,
114  /* validationOptions = */
115  {tosa::TosaProfileEnum::BaseInference,
116  /* StrictOperationSpecAlignment = */ true,
117  tosa::TosaLevelEnum::EightK});
118  });
119 }
static MLIRContext * getContext(OpFoldResult val)
static llvm::ManagedStatic< PassManagerOptions > options
This class describes a specific conversion target.
void addLegalOp(OperationName op)
Register the given operations as legal.
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 addIllegalDialect(StringRef name, Names... names)
Register the operations of the given dialects as illegal, i.e.
The DialectRegistry maps a dialect namespace to a constructor for the matching dialect.
This class represents a pass manager that runs passes on either a specific operation type,...
Definition: PassManager.h:48
void addPass(std::unique_ptr< Pass > pass)
Add the given pass to this pass manager.
Definition: Pass.cpp:364
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:117
Operation is the basic unit of execution within MLIR.
Definition: Operation.h:88
void populateTosaToLinalgConversionPatterns(RewritePatternSet *patterns)
Populates conversion passes from TOSA dialect to Linalg dialect.
void addTosaToLinalgPasses(OpPassManager &pm, const TosaToLinalgOptions &options, const TosaToLinalgNamedOptions &tosaToLinalgNamedOptions=TosaToLinalgNamedOptions(), tosa::TosaValidationOptions const &validationOptions={ tosa::TosaProfileEnum::Undefined, false, tosa::TosaLevelEnum::None})
Populates passes to convert from TOSA to Linalg on buffers.
std::unique_ptr< Pass > createTosaMakeBroadcastablePass()
std::unique_ptr< Pass > createTosaToLinalgNamed(const TosaToLinalgNamedOptions &options=TosaToLinalgNamedOptions())
std::unique_ptr< Pass > createTosaToLinalg()
std::unique_ptr< Pass > createTosaOptionalDecompositions()
void registerTosaToLinalgPipelines()
Populates TOSA to linalg pipelines Currently, this includes only the "tosa-to-linalg-pipeline".
std::unique_ptr< Pass > createTosaLayerwiseConstantFoldPass()
std::unique_ptr< Pass > createTosaInferShapesPass()
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.
std::unique_ptr< Pass > createCanonicalizerPass()
Creates an instance of the Canonicalizer pass, configured with default settings (which can be overrid...
bool failed(LogicalResult result)
Utility function that returns true if the provided LogicalResult corresponds to a failure value.
Definition: LogicalResult.h:72
PassPipelineRegistration provides a global initializer that registers a Pass pipeline builder routine...
Definition: PassRegistry.h:174