| 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 "mlir/Conversion/TosaToLinalg/TosaToLinalg.h" |
| 14 | |
| 15 | #include "mlir/Dialect/Arith/IR/Arith.h" |
| 16 | #include "mlir/Dialect/Func/IR/FuncOps.h" |
| 17 | #include "mlir/Dialect/Index/IR/IndexDialect.h" |
| 18 | #include "mlir/Dialect/Linalg/IR/LinalgDialect.h" |
| 19 | #include "mlir/Dialect/Math/IR/Math.h" |
| 20 | #include "mlir/Dialect/SCF/IR/SCFDialect.h" |
| 21 | #include "mlir/Dialect/Tensor/IR/Tensor.h" |
| 22 | #include "mlir/Dialect/Tosa/IR/TargetEnv.h" |
| 23 | #include "mlir/Dialect/Tosa/IR/TosaOps.h" |
| 24 | #include "mlir/Dialect/Tosa/Transforms/Passes.h" |
| 25 | #include "mlir/IR/PatternMatch.h" |
| 26 | #include "mlir/Pass/PassManager.h" |
| 27 | #include "mlir/Pass/PassOptions.h" |
| 28 | #include "mlir/Transforms/DialectConversion.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 | TosaToLinalg(const TosaToLinalgOptions &options) |
| 42 | : impl::TosaToLinalgBase<TosaToLinalg>(options) {} |
| 43 | |
| 44 | void getDependentDialects(DialectRegistry ®istry) const override { |
| 45 | registry |
| 46 | .insert<arith::ArithDialect, linalg::LinalgDialect, math::MathDialect, |
| 47 | index::IndexDialect, tensor::TensorDialect, scf::SCFDialect>(); |
| 48 | } |
| 49 | |
| 50 | void runOnOperation() override { |
| 51 | RewritePatternSet patterns(&getContext()); |
| 52 | ConversionTarget target(getContext()); |
| 53 | target.addLegalDialect<linalg::LinalgDialect, tensor::TensorDialect, |
| 54 | scf::SCFDialect>(); |
| 55 | target.addIllegalDialect<tosa::TosaDialect>(); |
| 56 | |
| 57 | // Not every TOSA op can be legalized to linalg. |
| 58 | target.addLegalOp<tosa::ApplyScaleOp>(); |
| 59 | target.addLegalOp<tosa::IfOp>(); |
| 60 | target.addLegalOp<tosa::ConstOp>(); |
| 61 | target.addLegalOp<tosa::ConstShapeOp>(); |
| 62 | target.addLegalOp<tosa::WhileOp>(); |
| 63 | target.addLegalOp<tosa::ConcatOp>(); |
| 64 | target.addLegalOp<tosa::SliceOp>(); |
| 65 | target.addLegalOp<tosa::ReshapeOp>(); |
| 66 | target.addLegalOp<tosa::PadOp>(); |
| 67 | |
| 68 | target.markUnknownOpDynamicallyLegal(fn: [](Operation *) { return true; }); |
| 69 | |
| 70 | TypeConverter converter; |
| 71 | tosa::populateTosaTypeConversion(converter); |
| 72 | |
| 73 | FunctionOpInterface func = getOperation(); |
| 74 | TosaToLinalgOptions options; |
| 75 | options.allowNonFinites = allowNonFinites; |
| 76 | mlir::tosa::populateTosaToLinalgConversionPatterns(converter, patterns: &patterns, |
| 77 | options); |
| 78 | if (failed(Result: applyFullConversion(op: func, target, patterns: std::move(patterns)))) |
| 79 | signalPassFailure(); |
| 80 | } |
| 81 | }; |
| 82 | } // namespace |
| 83 | |
| 84 | std::unique_ptr<Pass> |
| 85 | mlir::tosa::createTosaToLinalg(const TosaToLinalgOptions &options) { |
| 86 | return std::make_unique<TosaToLinalg>(args: options); |
| 87 | } |
| 88 | |
| 89 | void mlir::tosa::addTosaToLinalgPasses( |
| 90 | OpPassManager &pm, const TosaToLinalgOptions &options, |
| 91 | const TosaToLinalgNamedOptions &tosaToLinalgNamedOptions, |
| 92 | std::optional<tosa::TosaValidationOptions> validationOptions, |
| 93 | std::optional<TosaAttachTargetOptions> attachTargetOptions) { |
| 94 | // Optional decompositions are designed to benefit linalg. |
| 95 | if (!options.disableTosaDecompositions) |
| 96 | pm.addNestedPass<func::FuncOp>( |
| 97 | pass: tosa::createTosaOptionalDecompositionsPass()); |
| 98 | pm.addNestedPass<func::FuncOp>(pass: createCanonicalizerPass()); |
| 99 | |
| 100 | pm.addNestedPass<func::FuncOp>(pass: tosa::createTosaInferShapesPass()); |
| 101 | pm.addNestedPass<func::FuncOp>(pass: tosa::createTosaMakeBroadcastablePass()); |
| 102 | pm.addNestedPass<func::FuncOp>( |
| 103 | pass: tosa::createTosaToLinalgNamed(options: tosaToLinalgNamedOptions)); |
| 104 | pm.addNestedPass<func::FuncOp>(pass: createCanonicalizerPass()); |
| 105 | // TODO: Remove pass that operates on const tensor and enable optionality |
| 106 | pm.addNestedPass<func::FuncOp>(pass: tosa::createTosaLayerwiseConstantFoldPass( |
| 107 | options: {.aggressiveReduceConstant: options.aggressiveReduceConstant})); |
| 108 | pm.addNestedPass<func::FuncOp>(pass: tosa::createTosaMakeBroadcastablePass()); |
| 109 | // tosa-attach-target writes a tosa.target_env module attribute, schedule it |
| 110 | // only when the caller actually needs one. Callers that opt out of both no |
| 111 | // longer get a tosa.target_env attribute they did not ask for. |
| 112 | if (validationOptions || attachTargetOptions) { |
| 113 | if (!attachTargetOptions) { |
| 114 | attachTargetOptions = TosaAttachTargetOptions(); |
| 115 | attachTargetOptions->profiles = {"pro_int" , "pro_fp" }; |
| 116 | // TODO: populate with all the extensions that the tosa->linalg |
| 117 | // conversion supports |
| 118 | attachTargetOptions->extensions = {"doubleround" }; |
| 119 | } |
| 120 | pm.addPass(pass: tosa::createTosaAttachTarget(options: *attachTargetOptions)); |
| 121 | } |
| 122 | if (validationOptions) |
| 123 | pm.addPass(pass: tosa::createTosaValidation(options: *validationOptions)); |
| 124 | pm.addNestedPass<func::FuncOp>(pass: tosa::createTosaToLinalg(options)); |
| 125 | } |
| 126 | |
| 127 | //===----------------------------------------------------------------------===// |
| 128 | // Pipeline registration. |
| 129 | //===----------------------------------------------------------------------===// |
| 130 | |
| 131 | namespace { |
| 132 | /// Options controlling the registered `tosa-to-linalg-pipeline`. |
| 133 | struct TosaToLinalgPipelineOptions |
| 134 | : public PassPipelineOptions<TosaToLinalgPipelineOptions> { |
| 135 | PassOptions::Option<bool> validation{ |
| 136 | *this, "validation" , |
| 137 | llvm::cl::desc("Run tosa-attach-target and tosa-validate as part of the " |
| 138 | "pipeline." ), |
| 139 | llvm::cl::init(Val: true)}; |
| 140 | }; |
| 141 | } // namespace |
| 142 | |
| 143 | void mlir::tosa::registerTosaToLinalgPipelines() { |
| 144 | PassPipelineRegistration<TosaToLinalgPipelineOptions>( |
| 145 | "tosa-to-linalg-pipeline" , |
| 146 | "The default pipeline for converting TOSA operators to the equivalent " |
| 147 | "operations using the tensor operations in LinAlg as well as LinAlg " |
| 148 | "named operations." , |
| 149 | [](OpPassManager &pm, const TosaToLinalgPipelineOptions &pipelineOpts) { |
| 150 | TosaToLinalgOptions tosaToLinalgOptions; |
| 151 | TosaToLinalgNamedOptions tosaToLinalgNamedOptions; |
| 152 | std::optional<TosaValidationOptions> validationOptions; |
| 153 | if (pipelineOpts.validation) { |
| 154 | validationOptions = TosaValidationOptions{ |
| 155 | /*strictOpSpecAlignment=*/false, |
| 156 | /*allowInvalidOpDatatypeCombinations=*/false, |
| 157 | /*validateFunctionSignature=*/false}; |
| 158 | } |
| 159 | tosa::addTosaToLinalgPasses(pm, options: tosaToLinalgOptions, |
| 160 | tosaToLinalgNamedOptions, |
| 161 | validationOptions); |
| 162 | }); |
| 163 | } |
| 164 | |