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  TypeConverter converter;
68 
69  FunctionOpInterface func = getOperation();
71  if (failed(applyFullConversion(func, target, std::move(patterns))))
72  signalPassFailure();
73  }
74 };
75 } // namespace
76 
77 std::unique_ptr<Pass> mlir::tosa::createTosaToLinalg() {
78  return std::make_unique<TosaToLinalg>();
79 }
80 
82  OpPassManager &pm, const TosaToLinalgOptions &options,
83  const TosaToLinalgNamedOptions &tosaToLinalgNamedOptions,
84  std::optional<tosa::TosaValidationOptions> validationOptions) {
85  // Optional decompositions are designed to benefit linalg.
86  if (!options.disableTosaDecompositions)
88  pm.addNestedPass<func::FuncOp>(createCanonicalizerPass());
89 
92  pm.addNestedPass<func::FuncOp>(
93  tosa::createTosaToLinalgNamed(tosaToLinalgNamedOptions));
94  pm.addNestedPass<func::FuncOp>(createCanonicalizerPass());
95  // TODO: Remove pass that operates on const tensor and enable optionality
97  {options.aggressiveReduceConstant}));
99  if (validationOptions)
100  pm.addPass(tosa::createTosaValidation(*validationOptions));
101  pm.addNestedPass<func::FuncOp>(tosa::createTosaToLinalg());
102 }
103 
104 //===----------------------------------------------------------------------===//
105 // Pipeline registration.
106 //===----------------------------------------------------------------------===//
107 
110  "tosa-to-linalg-pipeline",
111  "The default pipeline for converting TOSA operators to the equivalent "
112  "operations using the tensor operations in LinAlg as well as LinAlg "
113  "named operations.",
114  [](OpPassManager &pm) {
115  TosaToLinalgOptions tosaToLinalgOptions;
116  TosaToLinalgNamedOptions tosaToLinalgNamedOptions;
117  TosaValidationOptions validationOptions;
118  validationOptions.profile = tosa::TosaProfileEnum::BaseInference;
119  validationOptions.StrictOperationSpecAlignment = true;
120  validationOptions.level = tosa::TosaLevelEnum::EightK;
121  tosa::addTosaToLinalgPasses(pm, tosaToLinalgOptions,
122  tosaToLinalgNamedOptions,
123  validationOptions);
124  });
125 }
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:47
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:116
Operation is the basic unit of execution within MLIR.
Definition: Operation.h:88
Type conversion class.
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 populateTosaToLinalgConversionPatterns(TypeConverter &converter, RewritePatternSet *patterns)
Populates conversion passes from TOSA dialect to Linalg dialect.
void populateTosaTypeConversion(TypeConverter &converter)
void registerTosaToLinalgPipelines()
Populates TOSA to linalg pipelines Currently, this includes only the "tosa-to-linalg-pipeline".
void addTosaToLinalgPasses(OpPassManager &pm, const TosaToLinalgOptions &options, const TosaToLinalgNamedOptions &tosaToLinalgNamedOptions=TosaToLinalgNamedOptions(), std::optional< tosa::TosaValidationOptions > validationOptions=tosa::TosaValidationOptions{tosa::TosaProfileEnum::Undefined, false, tosa::TosaLevelEnum::None})
Populates passes to convert from TOSA to Linalg on buffers.
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...
PassPipelineRegistration provides a global initializer that registers a Pass pipeline builder routine...
Definition: PassRegistry.h:174