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 
25 #include "mlir/IR/PatternMatch.h"
26 #include "mlir/Pass/PassManager.h"
29 #include "mlir/Transforms/Passes.h"
30 
31 namespace mlir {
32 #define GEN_PASS_DEF_TOSATOLINALG
33 #include "mlir/Conversion/Passes.h.inc"
34 } // namespace mlir
35 
36 using namespace mlir;
37 
38 namespace {
39 struct TosaToLinalg : public impl::TosaToLinalgBase<TosaToLinalg> {
40 public:
41  void getDependentDialects(DialectRegistry &registry) const override {
42  registry
43  .insert<arith::ArithDialect, linalg::LinalgDialect, math::MathDialect,
44  index::IndexDialect, tensor::TensorDialect, scf::SCFDialect>();
45  }
46 
47  void runOnOperation() override {
48  RewritePatternSet patterns(&getContext());
49  ConversionTarget target(getContext());
50  target.addLegalDialect<linalg::LinalgDialect, tensor::TensorDialect,
51  scf::SCFDialect>();
52  target.addIllegalDialect<tosa::TosaDialect>();
53 
54  // Not every TOSA op can be legalized to linalg.
55  target.addLegalOp<tosa::ApplyScaleOp>();
56  target.addLegalOp<tosa::IfOp>();
57  target.addLegalOp<tosa::ConstOp>();
58  target.addLegalOp<tosa::WhileOp>();
59  target.addLegalOp<tosa::ConcatOp>();
60  target.addLegalOp<tosa::SliceOp>();
61  target.addLegalOp<tosa::ReshapeOp>();
62  target.addLegalOp<tosa::PadOp>();
63 
64  target.markUnknownOpDynamicallyLegal([](Operation *) { return true; });
65 
66  FunctionOpInterface func = getOperation();
68  if (failed(applyFullConversion(func, target, std::move(patterns))))
69  signalPassFailure();
70  }
71 };
72 } // namespace
73 
74 std::unique_ptr<Pass> mlir::tosa::createTosaToLinalg() {
75  return std::make_unique<TosaToLinalg>();
76 }
77 
79  OpPassManager &pm, const TosaToLinalgOptions &options,
80  const TosaToLinalgNamedOptions &tosaToLinalgNamedOptions,
81  tosa::TosaValidationOptions const &validationOptions) {
82  // Optional decompositions are designed to benefit linalg.
83  if (!options.disableTosaDecompositions)
85  pm.addNestedPass<func::FuncOp>(createCanonicalizerPass());
86 
89  pm.addNestedPass<func::FuncOp>(
90  tosa::createTosaToLinalgNamed(tosaToLinalgNamedOptions));
91  pm.addNestedPass<func::FuncOp>(createCanonicalizerPass());
92  // TODO: Remove pass that operates on const tensor and enable optionality
94  {options.aggressiveReduceConstant}));
96  pm.addPass(tosa::createTosaValidation(validationOptions));
97  pm.addNestedPass<func::FuncOp>(tosa::createTosaToLinalg());
98 }
99 
100 //===----------------------------------------------------------------------===//
101 // Pipeline registration.
102 //===----------------------------------------------------------------------===//
103 
106  "tosa-to-linalg-pipeline",
107  "The default pipeline for converting TOSA operators to the equivalent "
108  "operations using the tensor operations in LinAlg as well as LinAlg "
109  "named operations.",
110  [](OpPassManager &pm) {
111  TosaToLinalgOptions tosaToLinalgOptions;
112  TosaToLinalgNamedOptions tosaToLinalgNamedOptions;
113  tosa::addTosaToLinalgPasses(pm, tosaToLinalgOptions,
114  tosaToLinalgNamedOptions,
115  /* validationOptions = */
116  {tosa::TosaProfileEnum::BaseInference,
117  /* StrictOperationSpecAlignment = */ true,
118  tosa::TosaLevelEnum::EightK});
119  });
120 }
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